Python: feat(mcp): add full _meta field support for CallToolResult objects (#2286)

* feat(mcp): add full _meta field support for CallToolResult objects

- Extract and preserve complete _meta field from MCP CallToolResult responses
- Merge metadata into additional_properties of converted content items
- Handle isError field for proper error state integration
- Support arbitrary metadata like token usage, costs, and performance metrics
- Maintain backward compatibility with existing tool execution workflows
- Add comprehensive test coverage for all metadata scenarios including edge cases
- Update documentation with metadata handling examples and patterns

Fixes protocol compliance violation where _meta fields were being dropped,
enables proper monitoring and cost tracking of MCP tool usage.

* Update python/packages/core/agent_framework/_mcp.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Clarify MCP _meta field test to use generic example metadata

- Updated test_mcp_call_tool_result_with_meta_arbitrary_data to use arbitrary metadata fields
- Added comments to emphasize that _meta structure is server-specific and not standardized

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Bryan Ostdiek
2025-11-21 03:43:24 -05:00
committed by GitHub
Unverified
parent a7a0660cac
commit d18ce24bf8
3 changed files with 258 additions and 2 deletions
@@ -10,6 +10,10 @@ OpenAI Chat Client with Local MCP Example
This sample demonstrates integrating Model Context Protocol (MCP) tools with
OpenAI Chat Client for extended functionality and external service access.
The Agent Framework now supports enhanced metadata extraction from MCP tool
results, including error states, token usage, costs, and other arbitrary
metadata through the _meta field of CallToolResult objects.
"""