Sanitize server_tool_use IDs for Anthropic-bound requests - #1401
Draft
andreasfoo wants to merge 2 commits into
Draft
Sanitize server_tool_use IDs for Anthropic-bound requests#1401andreasfoo wants to merge 2 commits into
andreasfoo wants to merge 2 commits into
Conversation
Anthropic validates server_tool_use block IDs against ^srvtoolu_[a-zA-Z0-9_]+$ and rejects the whole request with a 400 when replayed history carries IDs minted by another provider (OpenAI-style call_... ids or third-party Anthropic-compatible endpoints). Rewrite non-conforming IDs deterministically in the vendor transform when the target is api.anthropic.com / claude.ai, and remap the tool_use_id of every result block that references a rewritten ID so the pairing stays intact. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AXH5DRSAGo9j58rShPCX4g
Cover already-prefixed ids with invalid chars, mixed valid/invalid ids, plain tool_result remapping, regular tool_use passthrough, nil requests, and vendor-transform dispatch (sanitize on api.anthropic.com, no-op on other targets). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AXH5DRSAGo9j58rShPCX4g
andreasfoo
marked this pull request as draft
July 22, 2026 09:53
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
User report:
api.anthropic.comstrictly validates theidofserver_tool_usecontent blocks against^srvtoolu_[a-zA-Z0-9_]+$. As a multi-provider gateway, tingly-box replays conversation history that may containserver_tool_useblocks (typically WebSearch) whose IDs were minted by a different upstream — e.g. OpenAI-stylecall_...IDs from a converted response, or a third-party Anthropic-compatible endpoint using its own ID format. When a later request in that conversation routes to real Anthropic, the history is forwarded verbatim and the entire request is rejected with a 400.The codebase previously had no sanitization for
server_tool_useIDs at all (nosrvtoolureference anywhere).Fix
New op
internal/protocol/ops/request_anthropic_server_tool_use.go:SanitizeAnthropicV1ServerToolUseIDs/SanitizeAnthropicBetaServerToolUseIDsrewrite non-conformingserver_tool_useIDs in the replayed message history:_; asrvtoolu_prefix is added when needed (an already-prefixed ID with bad chars is not double-prefixed).srvtoolu_missing_N) so they stay distinct.tool_use_id(web_search_tool_result,web_fetch_tool_result, plaintool_result, etc. — via the SDK'sGetToolUseID()accessor) is remapped so the use/result pairing stays intact.tool_useblocks are left untouched.Wired into
VendorTransform(internal/protocol/transform/vendor.go) for both the v1 and beta paths, only when the target provider URL isapi.anthropic.com/claude.ai— consistent with the existing per-vendor dispatch. Requests to other providers are unaffected.Tests
internal/protocol/ops/request_anthropic_server_tool_use_test.go:web_search_tool_resultremapped, serialized output no longer contains the old IDsrvtoolu_...IDs kept as-issrvtoolu_-prefixed ID with invalid chars is sanitized without double prefixtool_resultreferencing a rewritten ID is remappedtool_useblocks withcall_...IDs untouchedinternal/protocol/transform/vendor_test.go:api.anthropic.comtargets, no-op for non-Anthropic targets (api.deepseek.com)All
internal/protocol/opsandinternal/protocol/transformtests pass.Note
Beta
mcp_tool_useblocks have an analogous prefix constraint (mcptoolu_) with the same cross-provider risk; kept out of scope here since the report only coversserver_tool_use, but the same pattern extends directly.🤖 Generated with Claude Code