Repository navigation
FIX: Map dotted and over-long MCP tool names to valid tool names - #4489
Open
Roydon Sequeira (roydonsequeira) wants to merge 1 commit into
Open
Roydon Sequeira (roydonsequeira) wants to merge 1 commit into
Roydon Sequeira (roydonsequeira) wants to merge 1 commit into
Conversation
MCP allows tool names with dots and up to 128 characters, but Tool only
accepts [A-Za-z0-9_-]{1,64}, the function-name rule OpenAI documents.
_MCPTool passed the MCP name through, so one such tool made
get_tools_async raise and every tool on that server was lost.
_MCPTool now advertises a mapped name and calls the server with the MCP
name. Characters outside [A-Za-z0-9_-] become "_", and a name that is
still longer than 64 characters is cut and ends with 8 hex characters of
the MCP name's SHA-256. Names that already pass are unchanged. If two
tools on one server map to the same name, get_tools_async raises a
ValueError that names both.
Fixes microsoft#4314
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The new async definitions need the required _async suffix and corresponding assignment update, and the session helper and fixture parameter need type annotations.
1 open issue
What changed in this PR
Addresses #4314 by mapping MCP tool names to PyRIT-compatible names while preserving the original names for server calls.
Changes:
- Replaces unsupported characters and shortens long names with a hash suffix, leaving valid names unchanged.
- Rejects tools from the same server that map to an identical name.
- Adds tests for name mapping, collisions, and execution using the original MCP name.
| File | Description |
|---|---|
| tests/unit/prompt_target/target/test_mcp_tool_provider.py | Adds mapping, collision, and OpenAI target execution tests. |
| pyrit/prompt_target/common/mcp_tool_provider.py | Maps advertised names, preserves MCP call names, and detects collisions. |
Review effort: Balanced
| assert session.list_tools.call_args_list[1].kwargs["params"].cursor == "page-2" | ||
|
|
||
|
|
||
| async def test_get_tools_async_maps_mcp_names_to_valid_tool_names() -> None: |
Contributor
Author
There was a problem hiding this comment.
These follow the conventions already in this file: the existing create_session helpers and the patch_central_database parameters in the tests above have no suffix or annotation. check-async-suffix only runs on pyrit/, and ruff's ANN rules are off for tests/. I'm happy to rename them if you'd prefer the suffix in tests.
This branch has not been deployed
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.

Description
Fixes #4314. MCP allows tool names that
Toolrejects, such asnotes.searchor names over 64 characters. One such tool madeMCPToolProvider.get_tools_asyncraise, so the server's other tools were lost too and every send failed before the first model request._MCPTooladvertises a name that passesTool's check and keeps the MCP name forcall_tool_async. Characters outside[A-Za-z0-9_-]become_. A name that is still longer than 64 characters is cut and ends with 8 hex characters of the MCP name's SHA-256, so long names with a shared prefix stay distinct. Names that already pass are unchanged.notes.searchandnotes_search,get_tools_asyncraises aValueErrorthat names both. I chose this over adding a suffix so that a tool's advertised name depends only on its own MCP name and doesn't change when the server adds a tool.function_callpieces show the mapped name, for examplenotes_search.The OpenAI Agents SDK uses the same replacement character, length limit and hash length for its server-prefixed MCP tool names.