Memory compare-and-set

$0.001 per call · USDC via x402 · POST /api/memory/cas

Atomically write (or release) a key only if its current value equals `expected` - the coordination primitive for distributed locks and optimistic concurrency across agents. Send POST /api/memory/cas with the required field key and pay $0.001 per call over x402 with USDC on an EVM chain (the paying wallet is the identity). It returns a JSON object with key, swapped, value, owner and expiresAt.

Acquire a lock: expected=null + a value + ttlSeconds. Release it: expected=<your token> with no value (deletes on match). Update safely: expected=<old>, value=<new>. Returns whether it swapped and the current value.

Category: Agent memory & coordination · Tags: memory cas compare-and-set lock coordination atomic · Also found as: memory-compare-and-set compare-and-set

TRY IN PLAYGROUND →

Parameters

NameTypeRequiredDescription
keystringyesKey to conditionally write
expectedanynoRequired current value to match (null or omitted = key absent/expired)
valueanynoNew value to set on match; omit to DELETE on match (lock release)
ttlSecondsnumbernoOptional TTL for the written value (lease for locks)
ownerstringnoOptional 0x namespace (requires a readwrite grant)

Example request

curl -i -X POST https://agent402.tools/api/memory/cas \
  -H "Content-Type: application/json" \
  -d '{"key":"locks/import","expected":null,"value":"agent-7","ttlSeconds":30}'

Without payment this returns HTTP 402 Payment Required with the exact price for memory-cas; any x402 v2 or MPP client pays it and retries.

Example response

{
  "key": "locks/import",
  "swapped": true,
  "value": "agent-7",
  "owner": "0x…",
  "expiresAt": 1760086430
}
FieldTypeAlways presentIn the example
keystringyeslocks/import
swappedbooleanyestrue
valuestringyesagent-7
ownerstringyes0x…
expiresAtnumberyes1760086430

From an MCP client

catalog.call {
  "slug": "memory-cas",
  "params": {
    "key": "locks/import",
    "expected": null,
    "value": "agent-7",
    "ttlSeconds": 30
  }
}

The hosted connector at https://agent402.tools/mcp needs a payment for memory-cas; the stdio package pays it from a wallet or from AGENT402_CREDITS_KEY (wallet only for this tool). Local install: npx -y agent402-mcp.

Errors and behavior

Paid call (JavaScript agent)

import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";

const client = new x402Client();
client.setSpendControls?.(false); // keep your own spending ceiling in code
registerExactEvmScheme(client, { signer: privateKeyToAccount(KEY) });
const payFetch = wrapFetchWithPayment(fetch, client);

const res = await payFetch("https://agent402.tools/api/memory/cas", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    "key": "locks/import",
    "expected": null,
    "value": "agent-7",
    "ttlSeconds": 30
  }),
});

Related tools

Memory counter

$0.001 · POST /api/memory/incr

Atomically increment (or decrement) a numeric key and return the new value - a coordination primitive for counters, lock…

Memory grant

$0.002 · POST /api/memory/grant

Share your namespace with another wallet so different agents can coordinate through it. Grant read or readwrite access t…

Memory forget

$0.001 · POST /api/memory/forget

Delete a remembered document by id from the recall store.

Memory grants list

$0.001 · GET /api/memory/grants

List the wallets you've granted access to your namespace, with their mode and expiry.

Memory audit log

$0.001 · GET /api/memory/log

Tamper-evident history of every change to a namespace - an append-only, hash-chained audit log the server attests to (pr…

Memory read

$0.001 · GET /api/memory

Read from a wallet-scoped namespace. ?key=… returns the stored value; omit key to list keys. The read half of memory-wri…