Skip to content

feat(mcp): add sampling callback support for MCP sessions#4718

Open
Piyushmrya wants to merge 2 commits intogoogle:mainfrom
Piyushmrya:fix-mcp-sampling-callback
Open

feat(mcp): add sampling callback support for MCP sessions#4718
Piyushmrya wants to merge 2 commits intogoogle:mainfrom
Piyushmrya:fix-mcp-sampling-callback

Conversation

@Piyushmrya
Copy link

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

  • Closes: N/A
  • Related: N/A

2. Or, if no issue exists, describe the change:

Problem

ADK’s MCP integration currently does not expose the MCP sampling callback capability.
This prevents agent-side LLM sampling handlers from being used when interacting with MCP servers that support sampling.

The MCP Python SDK supports sampling callbacks, but these parameters are not propagated through the ADK MCP integration layers.

Solution

Add sampling callback support by propagating the parameters through the MCP stack:

  • Add sampling_callback and sampling_capabilities parameters to McpToolset
  • Forward them to MCPSessionManager
  • Forward them to SessionContext
  • Pass them into ClientSession initialization

This enables agent-side sampling handling when interacting with MCP servers.


Testing Plan

Unit Tests

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added test_mcp_sampling_callback.py to verify that the sampling callback is correctly invoked.

Example result:
pytest tests/unittests/tools/mcp_tool/test_mcp_sampling_callback.py
1 passed

Manual End-to-End (E2E) Tests

Manual testing was performed using a FastMCP sampling example server where the sampling callback was invoked from the agent side and returned the expected response.


Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code where necessary.
  • I have added tests proving the feature works.
  • Unit tests pass locally.
  • I have manually tested the change end-to-end.

Additional context

This change aligns ADK MCP support with the sampling capabilities available in the MCP Python SDK and enables agent implementations to handle sampling requests via a callback.

@gemini-code-assist
Copy link
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@adk-bot adk-bot added the mcp [Component] Issues about MCP support label Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mcp [Component] Issues about MCP support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants