Skip to content

feat(mcp): expose idempotency_key on tools whose operation accepts Idempotency-Key - #132

Merged
Ooscaar merged 2 commits into
developfrom
feat/mcp-idempotency-key
Nov 26, 2026
Merged

Ooscaar merged 2 commits into
developfrom
feat/mcp-idempotency-key

Conversation

@Ooscaar

@Ooscaar Ooscaar commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Problem

The API supports Idempotency-Key on write operations: a retry with the same key within 24 hours returns the original result (or 409 idempotency_conflict while the first call is still processing). The MCP tools never exposed it. scripts/generate_mcp_tools.py dropped every optional header param, which was meant to hide plumbing like x-request-id but hid Idempotency-Key too.

So an agent whose chat stopped or timed out partway through posts_create_post, email_send_email_message, treasury_create_transfer, and similar tools had no safe way to retry. A retry could create a second post, message, transfer or campaign.

After merging the latest develop (whose spec now declares the header on PUT/PATCH/DELETE operations too), the new tests found a second, SDK-level bug: the SDK accepted header arguments on PUT/PATCH/DELETE and silently never sent them.

Change

MCP generator (scripts/generate_mcp_tools.py)

  • Wherever the OpenAPI spec declares Idempotency-Key, the tool exposes it as idempotency_key and forwards it to the SDK. Path-level and operation-level parameters are both checked.
    • It is optional, except where the spec marks it required (the tracking-site and tracking-link tools). Those tools already required it before this PR, and they still do.
    • Every tool uses one shared description written for an agent: reuse the key on retry, expect the original result or a 409 while the first call is still running, and use a new key for a new action.
    • The key is listed last in the arguments, so an agent reads the operation's own arguments first.
  • Other optional headers stay hidden from MCP: x-request-id (the SDK sets it), If-Match, X-Temp-Token, X-Connect-Token.
  • The 4 ad-create tools that derive a key from their arguments (SAME_ARGUMENTS_IDEMPOTENT_OPERATIONS) now use idempotency_key or same_arguments_key(...). A key passed by the caller takes precedence; without one, they behave as before.
  • No key is invented when the caller passes none. A key turns off the API's content-based duplicate check, and that check still catches accidental double-creates.

SDK (src/late/client/base.py, scripts/generate_resources.py)

  • _put, _patch and _delete (sync and async) now accept headers. The resource generator used to forward headers only on GET/POST, and now forwards them on every verb. Its old comment said "no operation on PUT/PATCH/DELETE currently has a header param", which is no longer true.
  • Before this fix, for example, update_ad(..., idempotency_key=...) and update_email_draft(..., if_match=...) accepted the argument and sent nothing. In total, 16 operations lost Idempotency-Key (updateAd, updateAdSet, updateAdCampaign, their *Status variants, updateAdKeyword, removeAdKeyword, and the negative-keyword and negative-keyword-list replace/edit/update/delete operations), and 2 lost If-Match (updateEmailDraft, updateAutomation).

Regenerated generated_tools.py and the 4 affected resources/_generated/* files, with the same ruff check --fix and ruff format steps as generate.yml. Running develop's generators on develop's spec gives files identical to develop, so this diff contains only the behavior change.

Coverage against the current openapi.yaml (develop @ 42d289e2)

84 MCP tools have operations that declare Idempotency-Key. Every one now exposes it and sends it:

Area Tools Key
Posts / profiles posts_create_post, profiles_create_profile optional
Inbox / messaging messages_send_inbox_message, comments_reply_to_inbox_post, reviews_reply_to_inbox_review, sms_send_sms, rcs_send_rcs_message, rcs_create_rcs_agent, voice_create_voice_call, whatsapp_calling_initiate_whats_app_call optional
Email send / reply / forward message, create / send draft optional
Money treasury_create_transfer, treasury_create_topup, commerce refund / order edit / draft order optional
Ad creates create ad account / campaign / ad set, duplicate ad / ad set / campaign, boost, upload campaign results, conversions_send_events, create negative keyword list, add keywords, bulk campaign status optional
Ad updates (PUT/PATCH/DELETE) update ad / ad set / campaign and their status, update / remove keyword, replace / edit campaign negative keywords and lists, update / delete / replace / edit negative keyword lists optional (also needed the SDK fix)
Ad creates with argument-derived key ad_campaigns_create_standalone_ad, messaging_ads_create_messaging_ad / _call_ad / _ctwa_ad optional, caller key overrides derived
Tracking conversions_create_tracking_site, conversions_rotate_tracking_site_key, tracking_links_create_tracking_link, tracking_links_rotate_tracking_link_postback_token required (per spec, unchanged)
Automations create automation / template, install template (own, shared, Zernio) optional
Creator campaigns 19 tools: create, budget, participants, payout batches, submissions, drafts, samples, adjustments, launch ads optional
Other blog article, branded-calling enterprise / identity, Google Business location optional

askSupportQuestion is skipped by the generator. Its hand-written support_ask already derives a key from its arguments, so it is unchanged.

Verification (local)

New tests/test_mcp_idempotency_key.py builds its list of tools from openapi.yaml, using the generator's own parameter merging and tool naming. If a future endpoint declares the header, the tests cover it automatically. For each of the 84 tools, the tests:

  • check that the MCP input schema has idempotency_key, required exactly where the spec requires it;
  • call the tool through the real MCP server (fastmcp.Client(mcp)) against a mocked API (respx), and assert the request carries Idempotency-Key: <caller key> on the wire. This test caught the SDK PUT/PATCH/DELETE bug: 16 tools failed with the header missing;
  • with no key, assert that no header is sent, except for the 4 ad-create tools, which still send their derived mcp-<tool>-… key;
  • for posts_create_post, run two calls with the same key and assert the same header goes out both times.

SDK tests also cover Idempotency-Key on a sync PUT and an async DELETE, and If-Match on a PATCH.

Results:

  • Full suite: 3811 passed, 44 skipped, 0 failed.
  • test_mcp_tool_sdk_signatures.py passes, so every generated SDK call binds to the real SDK method.
  • ruff check src tests passes.

Follow-up after merge

  • Deploy the hosted MCP server so mcp.zernio.com exposes the new argument.

🤖 Generated with Claude Code

Ooscaar and others added 2 commits November 26, 2026 16:12
…empotency-Key

The MCP generator dropped every optional header, so Idempotency-Key never
reached the tool surface even though the API and SDK support it. An agent
retrying after a timeout or a stopped chat could create a second copy.

- Generator exposes Idempotency-Key as idempotency_key wherever the spec
  declares it (optional, or required where the spec requires it) with one
  agent-oriented description; other optional headers stay hidden.
- Ad-create tools with an argument-derived key prefer the caller's key.
- No key is sent when the caller passes none, so the API's content
  duplicate check still applies.
- Tests drive every such tool through the MCP server and assert the header
  on the wire.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolved generate_mcp_tools.py (kept both IDEMPOTENCY_KEY_PARAM and
develop's READ_ONLY/OPEN_WORLD sets) and regenerated generated_tools.py
and the SDK resources from the merged generators and develop's spec.

The newer spec declares Idempotency-Key (and If-Match) on PUT/PATCH/DELETE
operations, which the SDK accepted but never sent: BaseClient's _put,
_patch and _delete took no headers and the resource generator forwarded
headers only on GET/POST. Both now forward headers on every verb.
idempotency_key is listed last in tool arguments so an agent reads the
operation's own arguments first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ooscaar
Ooscaar merged commit edc8983 into develop Nov 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant