PRACTICAL FIELD GUIDE

How to test MCP and API write retries for duplicate records

A lost response does not establish whether a create request reached the server. Retrying an uncertain mutation can create duplicates unless the application has a reliable reconciliation strategy.

MCPBackend editorial team · · Examples are illustrative

The short answer

To test write retries for duplicates, give every create a reference the client picks before the first attempt (client_ref), put a UNIQUE constraint on it, then send the same create twice on purpose. A correct setup ends with exactly one row and a second attempt that fails loudly. If you end with two rows, your retries can double-write in production.

The reason is simple: a timeout or dropped connection tells you that you did not get a response. It does not tell you whether the row was written. Before you retry a create, check whether the first attempt already landed, by looking the row up by its client_ref.

With the unique column in place, a blind retry fails instead of quietly creating a second row. In MCPBackend a unique violation on POST comes back as a 400 with code bad_request and the SQLite constraint message, so your client can treat that response as "already created" and fetch the existing row.

The 4 retry tests at a glance

  • Double send: POST the same body and client_ref twice. Expect 201, then 400 bad_request, then exactly one row.
  • Lost response: POST, throw the response away, look up client_ref. Expect the row to be there, so no second POST is sent.
  • Real failure: POST a body that fails validation, look up client_ref. Expect no row, then retry and expect exactly one.
  • Agent retry: have your coding agent run the same seed step twice, then count rows by client_ref. Expect one per reference.
# Double-send test with curl (use a disposable table)
REF=$(uuidgen)
for i in 1 2; do
  curl -s -o /dev/null -w "%{http_code}\n" -X POST \
    "https://data.mcpbackend.com/v1/$PROJECT_ID/db/leads" \
    -H "Authorization: Bearer $MCPBACKEND_KEY" -H "content-type: application/json" \
    -d "{\"email\":\"test@example.com\",\"client_ref\":\"$REF\"}"
done
# Expected output: 201 then 400
curl -s "https://data.mcpbackend.com/v1/$PROJECT_ID/db/leads?client_ref=$REF" \
  -H "Authorization: Bearer $MCPBACKEND_KEY"   # meta.count should be 1

A retry pattern that cannot double-write

Wrap the fetch in your own timeout and run the lookup in the catch block as well. Only send the POST again when the lookup returns no row.

// 1. Generate the key once, before the first attempt, and keep it with the retry.
const ref = crypto.randomUUID();

async function createLead(body) {
  const res = await fetch(`${base}/v1/${projectId}/db/leads`, {
    method: "POST",
    headers: { "content-type": "application/json", authorization: `Bearer ${token}` },
    body: JSON.stringify({ ...body, client_ref: ref }), // client_ref is UNIQUE
  });
  if (res.ok) return res.json();
  // 2. On a timeout or a 400 unique violation, look the row up instead of re-posting.
  const found = await fetch(`${base}/v1/${projectId}/db/leads?client_ref=${ref}`, {
    headers: { authorization: `Bearer ${token}` },
  }).then((r) => r.json());
  return found.data?.[0] ?? null; // null: the first attempt really did not land
}

How to test MCP and API writes for duplicates and retry failures

  • Create the table with a unique client_ref column (create_table accepts unique: true per column), then confirm it with get_schema.
  • Happy path: POST once and check that exactly one row has that client_ref.
  • Lost response: POST, discard the response on purpose, POST again with the same client_ref. Expect a 400 the second time and still exactly one row.
  • Real failure: send a body that fails validation, look up the client_ref, expect no row, then retry and expect one row.
  • Agent writes: ask your MCP client to seed the same record twice and count rows by client_ref afterwards. Agents retry tool calls too, so the database constraint is what protects you, not the prompt.
  • Use a disposable project or table for these tests and delete the rows afterwards.

Put it into practice

  1. Keep a stable business reference for the intended operation where the schema supports it.
  2. Look up the resulting state through an authorized query before retrying.
  3. Use uniqueness and application reconciliation deliberately rather than assuming an undocumented idempotency header exists.

What this looks like

ILLUSTRATIVE EXAMPLE

A form times out after the server saves a lead. A blind resubmission creates a second lead unless the application can recognize the original operation.

A boundary to keep clear

A client-generated reference must be validated and scoped correctly. It is not a substitute for authorization.

MCPBackend context

Use get_project_api or the dashboard's generated OpenAPI contract for your project's exact data-plane routes. Machine keys and end-user tokens have different access models. Treat error status and JSON code together, keep credentials out of URLs, and test against disposable records before changing real data.

Take the next step

Inspect the current project contract, try the change with disposable data, and verify the result through the same credentials your app will use. Record the expected response and one denied-access case before release.

Frequently asked questions

Does MCPBackend support an Idempotency-Key header?

No. The data API does not read an idempotency header, so do not rely on one. Use a unique column that holds a client-generated reference and look the row up before retrying.

What happens if I retry a POST that already succeeded?

Without a unique column you get a second row. With one, the retry fails with a 400 bad_request that carries the constraint message, and the original row is untouched.

Is it safe to retry PATCH and DELETE?

Usually, yes. Setting the same fields twice leaves the same result, and a second DELETE of the same ID also returns 204. Check whether another writer changed the row in between before you repeat an update.

Sources and further reading

These references explain the underlying protocols and design principles. For supported MCPBackend operations and exact request shapes, inspect your project’s generated API contract.