fix(mcp): sanitize tool names for Claude API compatibility#1311
Merged
fix(mcp): sanitize tool names for Claude API compatibility#1311
Conversation
Claude API rejects tool names containing ':' (requires ^[a-zA-Z0-9_-]{1,128}).
MCP tools use "server_id:tool_name" format which was causing 400 errors.
- Add McpTool::sanitized_id() replacing disallowed chars with '_', truncated
to 128 chars to stay within the API limit
- Use sanitized_id() in tool_definitions() so the LLM sees valid names
- Look up tool by sanitized_id() in execute_tool_call(); preserve
qualified_name() in ToolOutput.tool_name to maintain MCP detection
via contains(':') in sanitize_tool_output()
- Add collision warning in set_tools() when two tools map to the same
sanitized id
- Document the ':' invariant relied on by sanitize_tool_output()
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.
Summary
Claude API rejects tool names containing
:(requires^[a-zA-Z0-9_-]{1,128}).MCP tools use
server_id:tool_nameformat — causing 400 errors on every request with MCP enabled.Changes
McpTool::sanitized_id()— replaces disallowed chars with_, truncates to 128 chars with warn!tool_definitions()— usessanitized_id()as ToolDef id sent to LLMexecute_tool_call()— looks up tool bysanitized_id(), preservesqualified_name()inToolOutput.tool_namefor MCP quarantine detection viacontains(':')set_tools()— warns on sanitized_id collisions between registered tools:invariant relied on bysanitize_tool_output()Test plan
zeph-mcp(+10 new tests: unicode, boundary 128, truncation, collisions, special chars, routing)cargo +nightly fmt --check✅cargo clippy -p zeph-mcp -- -D warnings✅