Repository navigation
mcp: insert server and tool names into generated handlers with repr() - #540
Merged
zfoong merged 1 commit intoNov 24, 2026
Merged
Conversation
MCP tool names are chosen by the MCP server and can contain any character, including quotes, backslashes and newlines. Build the generated handler source with repr() literals, so every server and tool name is kept exactly as a string, in the docstring as well as in the values the handler uses. Action names and calls are unchanged. Adds tests with names containing quotes, newlines, triple quotes and a line continuation. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 23, 2026
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
The handler for each MCP tool is generated as Python source. Server and tool names were inserted into that source between double quotes, but a tool's name is chosen by the MCP server. Names containing a quote, a newline, a backslash or a triple quote did not survive the round trip as plain strings.
This change inserts every name with
repr(), which always produces a valid string literal, so each name is kept exactly as given. That includes the handler's docstring. Action names and MCP calls are unchanged.Changes
agent_core/core/impl/mcp/adapter.py:create_mcp_handler_with_sourcebuilds the docstring and the_server/_toolvalues withrepr().tests/test_mcp_adapter_names.py(new, 5 tests): names with a quote, a quote and newlines, a triple quote, and a backslash line continuation. For each one, the generated source compiles, the names appear unchanged as constants of the handler, and defining and calling the handler has no other effect. A registered action whose tool has a quote in its name keeps that name.Testing
tests/test_mcp_adapter_names.py, Python 3.10 and 3.14tests/test_mcp*.py, Python 3.10 and 3.14