Python: Return MCP tool-use sampling results#7189
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes Python MCP sampling so that structured model outputs returned as Agent Framework function_call content are converted back into MCP ToolUseContent, allowing structured sampling clients to receive tool-use results instead of an internal error.
Changes:
- Add Agent Framework
function_call→ MCPToolUseContentconversion in_prepare_content_for_mcp(). - Update
sampling_callback()to aggregate content across all response messages and returnCreateMessageResultWithTools(stopReason="toolUse")when tool-use content is present. - Add a regression test covering multiple tool-use calls returned from a sampling response.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| python/packages/core/agent_framework/_mcp.py | Converts function_call content into MCP ToolUseContent and returns CreateMessageResultWithTools when tool-use content is produced during sampling. |
| python/packages/core/tests/core/test_mcp.py | Adds regression coverage ensuring multiple function calls are preserved and returned as MCP tool-use sampling results. |
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
|
Thanks again for the quick reviews. It looks like the merge queue removed this after the Python merge integration workflow failed in the Ollama-backed misc integration job. The repeated failure appears to be Would you be able to re-run or requeue when convenient? |
|
Thank you! 🙏 |
Motivation & Context
MCP sampling requests can forward tool schemas to the configured chat client, but structured responses that come back as Agent Framework function-call content were not converted back to MCP tool-use content. This caused structured sampling clients to receive an internal error instead of the tool-use result.
Fixes #7160
Description & Review Guide
What are the major changes?
function_callcontent into MCPToolUseContent.CreateMessageResultWithToolswithstopReason="toolUse"when sampling produces tool-use content.What is the impact of these changes?
What do you want reviewers to focus on?
ToolUseContentmapping and the precedence of structured tool-use responses over plain text/image selection.Related Issue
Fixes #7160
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.