Python: Fix MCP metadata and tool name handling#6656
Open
eavanvalkenburg wants to merge 2 commits into
Open
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
Contributor
There was a problem hiding this comment.
Pull request overview
This PR tightens the Python MCP integration’s handling of request metadata (_meta) and remote tool identity, aiming for deterministic behavior when servers return metadata, tool names are prefixed, or multiple remote tool names normalize to the same local function name.
Changes:
- Add MCP
_metakey-name validation and enforce deterministic metadata precedence (caller/runtime < OpenTelemetry < tools/list). - Filter
allowed_tools/approval matching by raw remote tool identity and reject normalized-name collisions during tool loading. - Add regression tests for metadata precedence/stripping and raw-name allowlisting vs normalized-name collisions; update MCP guidance docs.
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 | Implements _meta validation + deterministic merge precedence; changes allow/approval matching to prefer raw remote tool identity; detects normalized-name collisions. |
| python/packages/core/tests/core/test_mcp.py | Adds regression tests for tool name collisions, raw-name allowlisting, _meta stripping, metadata precedence, and invalid _meta keys. |
| python/packages/core/AGENTS.md | Updates MCP guidance to document the new metadata precedence, model _meta stripping, and raw remote tool identity behavior. |
Co-authored-by: Copilot <223556219+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 & Context
MCP integrations need deterministic handling for request metadata and remote tool identity when servers provide metadata, prefixed tool names, or tool names that normalize to the same local function name.
This change keeps MCP request metadata and exposed tool filtering aligned with explicit framework-controlled inputs and remote tool identities.
Description & Review Guide
_metarequest metadata keys using the MCP 2025-06-18 key-name format._metaout of generated MCP function calls while preserving trusted caller/runtime metadata.allowed_toolsby raw remote MCP tool identity and reject ambiguous remote tool names that map to the same local function name..nameprotocol it requires.allowed_toolswith raw, normalized, and prefixed names._metamerge precedence between caller/runtime metadata, OpenTelemetry metadata, and tools/list metadata.Related Issue
N/A - no public issue provided.
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.