Repository navigation
feat(mcp): let to_mcp_server carry the conversation on an explicit session_id - #8164
Open
Shanmukh-l33t wants to merge 1 commit into
Open
Shanmukh-l33t wants to merge 1 commit into
Shanmukh-l33t wants to merge 1 commit into
Conversation
An MCP host that cannot hold a connection, or speaks a protocol revision without initialize and Mcp-Session-Id, loses the ADK session on every tool call, so each turn starts from scratch. Add an opt-in session_id_arg that makes the tool take an optional session_id and return the id it ran in, so the host names its own conversation.
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.
Fixes #8161.
Problem
to_mcp_serverkeys the ADK session on the MCP connection (_connection_keyreads the session object behind
ctx). That holds while one connection carriesone conversation. It does not hold when the connection is not stable:
connection;
initializenor sends anMcp-Session-Idheader.In all three cases every tool call creates a new ADK session and the agent loses
the conversation between turns, even with a persistent session service such as
VertexAiSessionService.Mechanism
Implemented the mechanism requested in the issue: the host names its own
conversation instead of relying on the connection.
to_mcp_server(..., session_id_arg=True)opts in. Default isFalse, sonothing changes for existing callers.
session_id: str | None = None. When an id ispassed and it resolves in
runner.session_service.get_session, that sessionis reused; otherwise a new one is created, matching the issue's "reuse it if
it exists, else create".
structuredContent: {"session_id": ...}, so the host can pass it back on the next call. An unknown or expired idstarts a new conversation and the host learns the new id from the response.
knows to reuse the id. Wording: "Pass back the returned session_id on every
later call of the same conversation. Omit it to start a new one."
not created: the host owns the session lifetime, so ADK does not delete a
session out from under it.
_run_agentgained an optionalsession_idthat takes precedence over theper-connection lookup, which keeps the existing path untouched when it is not
passed.
One note on the id: it is the only handle on the conversation, so any caller
that knows it joins that conversation. This is documented on the parameter and
in the guide, and it is part of why the feature is opt-in.
Backward compatibility
With
session_id_argunset, behavior is byte for byte what it was: same toolschema (
requestonly), same per-connection session reuse, same orphan reaping,same returned content, no structured content. A test pins that shape.
Testing
python -m pytest -q tests/unittests/tools/mcp_tool/test_agent_to_mcp.pyrunagainst both supported MCP SDK majors (
mcp 2.3.0andmcp 1.24.0): 32 passedon each.
The new tests, on unmodified
main(feature absent), fail:(
TypeError: to_mcp_server() got an unexpected keyword argument 'session_id_arg'on each. The one that passes is the backward compatibilitytest, which asserts the old shape and should pass on both sides.)
With the change,
tests/unittests/tools/mcp_tool/test_agent_to_mcp.py: 32passed. Whole directory: 496 passed, 1 failed
(
test_google_auth_async_transport_forwards_env_proxy, aModuleNotFoundErrorfrom a package missing in this environment, unrelated to the change).
The end to end test uses a real
RunnerandInMemorySessionServicewith anagent that reports how many user turns its session holds. Two calls that pass
the returned id answer
turn 1thenturn 2; the same two calls without an idwould both answer
turn 1.Files
src/google/adk/tools/mcp_tool/_agent_to_mcp.py: the flag, the toolsignature, the session resolution, and
_tool_result.tests/unittests/tools/mcp_tool/test_agent_to_mcp.py: eight tests (schema,reuse within one connection, reuse across a reconnect, unknown id, no
connection keying, no reaping, unchanged default shape, real runner history).
docs/guides/tools/mcp_tool/agent_to_mcp/index.md: option row and aparagraph under session continuity.