Skip to content

Add mcp_server_name to tool metadata in convert_mcp_tool_to_langchain_tool#488

Open
RobinBaldeo wants to merge 2 commits intolangchain-ai:mainfrom
RobinBaldeo:server_name_metadata
Open

Add mcp_server_name to tool metadata in convert_mcp_tool_to_langchain_tool#488
RobinBaldeo wants to merge 2 commits intolangchain-ai:mainfrom
RobinBaldeo:server_name_metadata

Conversation

@RobinBaldeo
Copy link
Copy Markdown

Closes #484.

When MultiServerMCPClient.get_tools() aggregates tools from multiple MCP servers, the server identity is currently only recoverable by parsing the prefix string from tool.name (and only when tool_name_prefix=True). If tool_name_prefix=False, the server identity is lost entirely after the aggregate list is returned.

This PR writes tool.metadata["mcp_server_name"] at the point where each tool is constructed in convert_mcp_tool_to_langchain_tool. The server_name is already threaded end-to-end through the call chain (get_toolsload_mcp_toolsconvert_mcp_tool_to_langchain_tool), so this is an additive change with no signature impact.

This change is purely additive — it adds a new top-level key (mcp_server_name) to tool.metadata. Existing keys and consumers reading them are unaffected.

Use case: building a framework where MCP tools from multiple servers need attribution for telemetry and UI rendering. Parsing tool.name.split("__")[0] is fragile and doesn't work when tool_name_prefix=False.

Collision behavior: if an MCP server's annotations field contains a key named mcp_server_name, it will be overwritten by the client-configured server key (the key in self.connections). This is intentional — the metadata tag reflects client-side configuration, not a server self-declaration, so the value is consistent regardless of server behavior.

Tests:

  • Added test_get_tools_metadata_has_server_name covering both tool_name_prefix=True and tool_name_prefix=False.
  • Updated test_load_mcp_tools_with_annotations to include the new key in its expected dict.

63 passed, 2 skipped locally.

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.

Surfacing the originating server name on tools returned from MultiServerMCPClient.get_tools()

1 participant