Problem:
When MCP tools are converted to FunctionTool via MCPUtil.to_function_tool(), the server name is only in the on_invoke_tool closure. When ToolCallItem/ToolCallOutputItem are created from API responses, they lose which
MCP server the tool came from. With multiple MCP servers, you can't tell which server handled a call.
Given that there could be multiple MCPs, and each MCP's tools could change frequently, tracking MCP server labels externally by tool name is not feasible. This also has implications on StreamEvent by extension.
Proposed solution:
Add a general metadata field to RunItemBase:
@dataclass
class RunItemBase(Generic[T], abc.ABC):
agent: Agent[Any]
raw_item: T
metadata: dict[str, Any] = field(default_factory=dict)
"""SDK-level metadata for tracking and debugging. This metadata is NOT sent to the agent.
Examples:
- For MCP tools: {"mcp_server_name": "git_server"}
- For handoffs: {"handoff_source": "agent_a"}
- For debugging: {"execution_time_ms": 123}
"""
The solution to this specific problem:
- Store MCP server name as a private attribute on
FunctionTool when converting MCP tools
- When creating
ToolCallItem/ToolCallOutputItem, populate metadata from the tool if available
- Access metadata via
item.metadata.get('mcp_server_name') when needed
Additional Benefits: The solution is very general-purpose, it supports any future metadata needs, not just MCP, as in the examples in the code snippet.
I'd love to know what you think before implementing this. While this may not be a breaking change, it introduces a new feature at the very core of the SDK, and could have other implications I'm not aware of.
Problem:
When MCP tools are converted to
FunctionToolviaMCPUtil.to_function_tool(), the server name is only in theon_invoke_toolclosure. WhenToolCallItem/ToolCallOutputItemare created from API responses, they lose whichMCP server the tool came from. With multiple MCP servers, you can't tell which server handled a call.
Given that there could be multiple MCPs, and each MCP's tools could change frequently, tracking MCP server labels externally by tool name is not feasible. This also has implications on
StreamEventby extension.Proposed solution:
Add a general
metadatafield toRunItemBase:The solution to this specific problem:
FunctionToolwhen converting MCP toolsToolCallItem/ToolCallOutputItem, populatemetadatafrom the tool if availableitem.metadata.get('mcp_server_name')when neededAdditional Benefits: The solution is very general-purpose, it supports any future metadata needs, not just MCP, as in the examples in the code snippet.
I'd love to know what you think before implementing this. While this may not be a breaking change, it introduces a new feature at the very core of the SDK, and could have other implications I'm not aware of.