Repository navigation
feat(mcp): expose idempotency_key on tools whose operation accepts Idempotency-Key - #132
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The API supports
Idempotency-Keyon write operations: a retry with the same key within 24 hours returns the original result (or409 idempotency_conflictwhile the first call is still processing). The MCP tools never exposed it.scripts/generate_mcp_tools.pydropped every optional header param, which was meant to hide plumbing likex-request-idbut hidIdempotency-Keytoo.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 onPUT/PATCH/DELETEoperations too), the new tests found a second, SDK-level bug: the SDK accepted header arguments onPUT/PATCH/DELETEand silently never sent them.Change
MCP generator (
scripts/generate_mcp_tools.py)Idempotency-Key, the tool exposes it asidempotency_keyand forwards it to the SDK. Path-level and operation-level parameters are both checked.x-request-id(the SDK sets it),If-Match,X-Temp-Token,X-Connect-Token.SAME_ARGUMENTS_IDEMPOTENT_OPERATIONS) now useidempotency_key or same_arguments_key(...). A key passed by the caller takes precedence; without one, they behave as before.SDK (
src/late/client/base.py,scripts/generate_resources.py)_put,_patchand_delete(sync and async) now acceptheaders. The resource generator used to forward headers only onGET/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.update_ad(..., idempotency_key=...)andupdate_email_draft(..., if_match=...)accepted the argument and sent nothing. In total, 16 operations lostIdempotency-Key(updateAd,updateAdSet,updateAdCampaign, their*Statusvariants,updateAdKeyword,removeAdKeyword, and the negative-keyword and negative-keyword-list replace/edit/update/delete operations), and 2 lostIf-Match(updateEmailDraft,updateAutomation).Regenerated
generated_tools.pyand the 4 affectedresources/_generated/*files, with the sameruff check --fixandruff formatsteps asgenerate.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:posts_create_post,profiles_create_profilemessages_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_calltreasury_create_transfer,treasury_create_topup, commerce refund / order edit / draft orderconversions_send_events, create negative keyword list, add keywords, bulk campaign statusad_campaigns_create_standalone_ad,messaging_ads_create_messaging_ad/_call_ad/_ctwa_adconversions_create_tracking_site,conversions_rotate_tracking_site_key,tracking_links_create_tracking_link,tracking_links_rotate_tracking_link_postback_tokenaskSupportQuestionis skipped by the generator. Its hand-writtensupport_askalready derives a key from its arguments, so it is unchanged.Verification (local)
New
tests/test_mcp_idempotency_key.pybuilds its list of tools fromopenapi.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:idempotency_key, required exactly where the spec requires it;fastmcp.Client(mcp)) against a mocked API (respx), and assert the request carriesIdempotency-Key: <caller key>on the wire. This test caught the SDKPUT/PATCH/DELETEbug: 16 tools failed with the header missing;mcp-<tool>-…key;posts_create_post, run two calls with the same key and assert the same header goes out both times.SDK tests also cover
Idempotency-Keyon a syncPUTand an asyncDELETE, andIf-Matchon aPATCH.Results:
test_mcp_tool_sdk_signatures.pypasses, so every generated SDK call binds to the real SDK method.ruff check src testspasses.Follow-up after merge
mcp.zernio.comexposes the new argument.🤖 Generated with Claude Code