Python: feat(mcp): add full _meta field support for CallToolResult objects#2286
Merged
eavanvalkenburg merged 7 commits intomicrosoft:mainfrom Nov 21, 2025
Merged
Python: feat(mcp): add full _meta field support for CallToolResult objects#2286eavanvalkenburg merged 7 commits intomicrosoft:mainfrom
eavanvalkenburg merged 7 commits intomicrosoft:mainfrom
Conversation
- 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.
feat(mcp): add full _meta field support for CallToolResult objects
Member
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds full support for extracting and preserving the _meta field from MCP CallToolResult objects to ensure compliance with the Model Context Protocol specification. Previously, all metadata including error states, token usage, costs, and performance metrics were being dropped during conversion.
Key changes:
- Enhanced the
_mcp_call_tool_result_to_ai_contents()function to extract and merge_metafield data into content items'additional_properties - Added comprehensive test coverage with 8 new test functions covering error states, arbitrary metadata, object attributes, edge cases, and integration scenarios
- Updated sample documentation to reflect the new metadata extraction capabilities
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_mcp.py | Core implementation of _meta field extraction and merging logic with support for dicts, objects with __dict__, and non-dict values |
| python/packages/core/tests/core/test_mcp.py | Added 8 comprehensive test functions covering metadata extraction scenarios including errors, arbitrary data, object attributes, merging, and edge cases |
| python/samples/getting_started/agents/openai/openai_chat_client_with_local_mcp.py | Updated docstring to document the new metadata extraction feature |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Member
eavanvalkenburg
left a comment
There was a problem hiding this comment.
thanks for looking into this, let's make it a bit more complete!
- 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
eavanvalkenburg
approved these changes
Nov 18, 2025
alliscode
approved these changes
Nov 20, 2025
arisng
pushed a commit
to arisng/agent-framework
that referenced
this pull request
Feb 2, 2026
…jects (microsoft#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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
The Agent Framework library violated the official Model Context Protocol (MCP) specification by completely dropping the
_metafield fromCallToolResultresponses. This prevented applications from accessing critical metadata such as token usage, cost information, performance metrics, and error states that MCP servers provide.Problems this solves:
isErrorfield was not being preserved for proper error state detectionThis change ensures full compliance with the official MCP specification and enables proper tool metadata access for applications.
Description
Core Changes:
_mcp_call_tool_result_to_ai_contents()function to extract and preserve the complete_metafield fromCallToolResultobjectsadditional_propertiesfield of converted content items following the framework's existing patterns_metavalues to ensure robustnessTechnical Implementation:
getattr()for safe_metafield extraction to handle version compatibilityadditional_propertieswithout overwriting_metavalues under a special_metakey for edge case handlingEnhanced Functionality:
isErrorflags for proper error handling workflowsComprehensive Testing:
Added 8 new test functions covering all scenarios:
_metawithisError=Trueerror scenariosadditional_properties_metastructures_metafield handling_metavaluescall_tool()methodContribution Checklist