Skip to content

Python: Return MCP tool-use sampling results#7189

Open
scarab-systems wants to merge 2 commits into
microsoft:mainfrom
scarab-systems:scarab-systems/agent-framework-7160-mcp-sampling
Open

Python: Return MCP tool-use sampling results#7189
scarab-systems wants to merge 2 commits into
microsoft:mainfrom
scarab-systems:scarab-systems/agent-framework-7160-mcp-sampling

Conversation

@scarab-systems

Copy link
Copy Markdown

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?

    • Convert Agent Framework function_call content into MCP ToolUseContent.
    • Return CreateMessageResultWithTools with stopReason="toolUse" when sampling produces tool-use content.
    • Collect prepared content across all response messages so multiple function calls are preserved.
    • Add regression coverage for structured sampling output with multiple tool-use calls.
  • What is the impact of these changes?

    • Structured MCP sampling can return tool-use results while existing text/image sampling behavior is preserved.
    • The change is additive to the sampling response path and does not remove existing fallback/error handling.
  • What do you want reviewers to focus on?

    • The function-call to ToolUseContent mapping and the precedence of structured tool-use responses over plain text/image selection.

Related Issue

Fixes #7160

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Copilot AI review requested due to automatic review settings July 18, 2026 22:39
@giles17 giles17 added the python Usage: [Issues, PRs], Target: Python label Jul 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 → MCP ToolUseContent conversion in _prepare_content_for_mcp().
  • Update sampling_callback() to aggregate content across all response messages and return CreateMessageResultWithTools(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.

@scarab-systems
scarab-systems marked this pull request as ready for review July 18, 2026 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: MCP sampling callback cannot return structured tool-use responses

3 participants