Skip to content

FIX: Map dotted and over-long MCP tool names to valid tool names - #4489

Open
Roydon Sequeira (roydonsequeira) wants to merge 1 commit into
microsoft:mainfrom
roydonsequeira:fix/mcp-tool-names
Open

Roydon Sequeira (roydonsequeira) wants to merge 1 commit into
microsoft:mainfrom
roydonsequeira:fix/mcp-tool-names

Conversation

@roydonsequeira

Copy link
Copy Markdown
Contributor

Description

Fixes #4314. MCP allows tool names that Tool rejects, such as notes.search or names over 64 characters. One such tool made MCPToolProvider.get_tools_async raise, so the server's other tools were lost too and every send failed before the first model request.

  • _MCPTool advertises a name that passes Tool's check and keeps the MCP name for call_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.
  • If two tools on one server map to the same name, such as notes.search and notes_search, get_tools_async raises a ValueError that 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.
  • The model and the stored function_call pieces show the mapped name, for example notes_search.

The OpenAI Agents SDK uses the same replacement character, length limit and hash length for its server-prefixed MCP tool names.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: MCPToolProvider fails on MCP tool names that contain a dot or exceed 64 characters

2 participants