feat: read _meta from MCP tool call responses#3585
Conversation
Extracts the _meta field from MCP tool call results and makes it available as ToolCallOutputItem.mcp_meta for consumers to access server-returned metadata. Closes openai#3477 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d953ac1a47
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
|
||
| # Expose ``_meta`` from the MCP response so callers can access it via | ||
| # ``ToolCallOutputItem.mcp_meta``. | ||
| response_meta = getattr(result, "_meta", None) |
There was a problem hiding this comment.
Read MCP response metadata from .meta
When an MCP server returns CallToolResult(..., _meta={...}), the Python MCP SDK exposes that value through the meta field while _meta is only the wire alias, so getattr(result, "_meta", None) stays None for normal CallToolResult objects. In that scenario this never populates the ToolContext, and ToolCallOutputItem.mcp_meta remains unset despite the server returning metadata; read result.meta here (with any needed fallback) before copying it.
Useful? React with 👍 / 👎.
Summary
Extracts and exposes the
_metafield from MCP tool call responses viaToolCallOutputItem.mcp_meta.Background
PR #2375 enabled sending
_metain MCP tool call requests (client → server). This PR adds the complementary feature: reading_metafrom MCP tool call responses (server → client).The MCP protocol spec defines
CallToolResult._metaas an optional metadata dictionary that servers can return alongside the tool result. Currently the SDK ignores this field entirely.Changes
src/agents/items.py— Addmcp_meta: dict[str, Any] | Nonefield toToolCallOutputItemsrc/agents/tool_context.py— Add internal_mcp_response_metafield toToolContextto transport the meta from tool invocationsrc/agents/mcp/util.py— Extractresult._metaininvoke_mcp_tooland store it on theToolContextsrc/agents/run_internal/tool_execution.py— Thread the meta through_FunctionToolTaskStatetoToolCallOutputItemat result-build timeUsage
After an MCP tool call, consumers can access the server-returned
_metavia the event stream:Backward Compatibility
All new fields default to
None. Existing code that constructsToolCallOutputItemwithoutmcp_metacontinues to work unchanged.Closes #3477