Skip to content

ADK MCP Toolset Session Management Issues #2979

@mr-nishanth

Description

@mr-nishanth

ADK MCP Toolset Session Management Issues

Issue Summary

Google ADK's MCP Toolset implementation has critical session management issues that prevent reliable use of MCP servers, particularly with stdio-based connections. These issues manifest as "Connection closed" errors and session persistence failures.

Current Code

# mongodb_agent/agent.py
import os
import logging
from dotenv import load_dotenv

from google.adk.agents import LlmAgent
from google.adk.tools.mcp_tool.mcp_toolset import McpToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams
from mcp.client.stdio import StdioServerParameters

from .prompt import INSTRUCTION

# Load environment variables
ROOT = os.path.dirname(os.path.dirname(__file__))
load_dotenv(dotenv_path=os.path.join(ROOT, ".env"))

# ------------------------------------------------------------------------------
# Config
# ------------------------------------------------------------------------------
MONGODB_CONNECTION_STRING = os.getenv("MDB_MCP_CONNECTION_STRING")
if not MONGODB_CONNECTION_STRING:
    raise EnvironmentError(
        "MDB_MCP_CONNECTION_STRING is not set. "
        "Please create a .env file with your MongoDB connection string.\n"
        "Example: MDB_MCP_CONNECTION_STRING=mongodb+srv://user:[email protected]/db"
    )

# ------------------------------------------------------------------------------
# MCP Toolset
# ------------------------------------------------------------------------------
mongodb_toolset = McpToolset(
    connection_params=StdioConnectionParams(
        server_params=StdioServerParameters(
            command="npx",
            args=["-y", "mongodb-mcp-server@latest"],
            env={
                "MDB_MCP_CONNECTION_STRING": MONGODB_CONNECTION_STRING,
            },
        ),
        timeout=60.0,
    )
)

# ------------------------------------------------------------------------------
# Agent
# ------------------------------------------------------------------------------
root_agent = LlmAgent(
    model="gemini-2.0-flash",
    name="mongodb_assistant_agent",
    instruction=INSTRUCTION,
    tools=[mongodb_toolset],
)

What's Happening

The issue occurs when using the ADK web interface:

  1. First conversation works: User can successfully query MongoDB databases and collections
  2. Second conversation fails: Subsequent conversations fail with "Connection closed" errors
  3. MCP server process terminates: The stdio-based MCP server terminates after each conversation
  4. Session cleanup issues: ADK attempts to reuse sessions that are already disconnected

Server Logs

.venv\Scripts\adk web

google-adk-mcp-server\.venv\Lib\site-packages\google\adk\cli\fast_api.py:175: UserWarning: [EXPERIMENTAL] InMemoryCredentialService: This feature is experimental and may change or be removed in future versions without notice. It may introduce breaking changes at any time.
  credential_service = InMemoryCredentialService()
C:\DoWhistle\R&D\google-adk-mcp-server\.venv\Lib\site-packages\google\adk\auth\credential_service\in_memory_credential_service.py:33: UserWarning: [EXPERIMENTAL] BaseCredentialService: This feature is experimental and may change or be removed in future versions without notice. It may introduce breaking changes at any time.
  super().__init__()
INFO:     Started server process [38260]
INFO:     Waiting for application startup.

+-----------------------------------------------------------------------------+
| ADK Web Server started                                                      |
|                                                                             |
| For local testing, access at http://127.0.0.1:8000.                         |
+-----------------------------------------------------------------------------+

INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     127.0.0.1:43706 - "POST /apps/mongodb_agent/users/user/sessions HTTP/1.1" 200 OK
INFO:     127.0.0.1:43706 - "GET /apps/mongodb_agent/users/user/sessions HTTP/1.1" 200 OK
INFO:     127.0.0.1:56018 - "GET / HTTP/1.1" 307 Temporary Redirect
INFO:     127.0.0.1:56018 - "GET /list-apps?relative_path=./ HTTP/1.1" 200 OK
INFO:     127.0.0.1:56018 - "POST /apps/mongodb_agent/users/user/sessions HTTP/1.1" 200 OK
INFO:     127.0.0.1:56018 - "GET /apps/mongodb_agent/eval_sets HTTP/1.1" 200 OK
INFO:     127.0.0.1:57494 - "GET /apps/mongodb_agent/eval_results HTTP/1.1" 200 OK
INFO:     127.0.0.1:57494 - "GET /apps/mongodb_agent/users/user/sessions HTTP/1.1" 200 OK
INFO:     127.0.0.1:57494 - "GET /apps/mongodb_agent/users/user/sessions HTTP/1.1" 200 OK
INFO:     127.0.0.1:2117 - "POST /run_sse HTTP/1.1" 200 OK
2025-09-19 15:09:12,641 - INFO - envs.py:47 - Loaded .env file for mongodb_agent at C:\DoWhistle\R&D\google-adk-mcp-server\.env
2025-09-19 15:09:12,643 - INFO - envs.py:47 - Loaded .env file for mongodb_agent at C:\DoWhistle\R&D\google-adk-mcp-server\.env
2025-09-19 15:09:12,670 - INFO - agent_loader.py:126 - Found root_agent in mongodb_agent.agent
C:\DoWhistle\R&D\google-adk-mcp-server\.venv\Lib\site-packages\google\adk\cli\adk_web_server.py:330: UserWarning: [EXPERIMENTAL] App: This feature is experimental and may change or be removed in future versions without notice. It may introduce breaking changes at any time.
  agentic_app = App(
C:\DoWhistle\R&D\google-adk\tools\mcp_tool\mcp_tool.py:88: UserWarning: [EXPERIMENTAL] BaseAuthenticatedTool: This feature is experimental and may change or be removed in future versions without notice. It may introduce breaking changes at any time.
  super().__init__(
2025-09-19 15:09:33,750 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,750 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,750 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,751 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,751 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,751 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,752 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,752 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,753 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,753 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,753 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,754 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,754 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,754 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,755 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,755 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,755 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,756 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,756 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,756 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:33,756 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:35,843 - INFO - google_llm.py:113 - Sending out request, model: gemini-2.0-flash, backend: GoogleLLMVariant.GEMINI_API, stream: False
2025-09-19 15:09:35,844 - INFO - models.py:8283 - AFC is enabled with max remote calls: 10.
2025-09-19 15:09:37,374 - INFO - _client.py:1740 - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent "HTTP/1.1 200 OK"
2025-09-19 15:09:37,381 - INFO - google_llm.py:161 - Response received from the model.
2025-09-19 15:09:37,382 - WARNING - types.py:5509 - Warning: there are non-text parts in the response: ['function_call'], returning concatenated text result from text parts. Check the full candidates.content.parts accessor to get the full model response.
2025-09-19 15:09:41,938 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,938 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,939 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,940 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,940 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,941 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,941 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,942 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,943 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,944 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,945 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,946 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,947 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,948 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,949 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,949 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,950 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,951 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,951 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,952 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:41,953 - WARNING - base_authenticated_tool.py:72 - auth_config or auth_config.auth_scheme is missing. Will skip authentication.Using FunctionTool instead if authentication is not required.
2025-09-19 15:09:44,159 - INFO - google_llm.py:113 - Sending out request, model: gemini-2.0-flash, backend: GoogleLLMVariant.GEMINI_API, stream: False
2025-09-19 15:09:44,162 - INFO - models.py:8283 - AFC is enabled with max remote calls: 10.
2025-09-19 15:09:46,454 - INFO - _client.py:1740 - HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent "HTTP/1.1 200 OK"
2025-09-19 15:09:46,461 - INFO - google_llm.py:161 - Response received from the model.
INFO:     127.0.0.1:2117 - "GET /apps/mongodb_agent/users/user/sessions/87cb5124-abb1-429b-b13d-1fe07a6693e3 HTTP/1.1" 200 OK
INFO:     127.0.0.1:6784 - "GET /debug/trace/session/87cb5124-abb1-429b-b13d-1fe07a6693e3 HTTP/1.1" 200 OK
INFO:     127.0.0.1:10957 - "POST /run_sse HTTP/1.1" 200 OK
2025-09-19 15:09:53,677 - INFO - envs.py:47 - Loaded .env file for mongodb_agent at C:\DoWhistle\R&D\google-adk-mcp-server\.env
2025-09-19 15:09:53,680 - INFO - mcp_session_manager.py:332 - Cleaning up disconnected session: stdio_session

Expected Behavior

  • MCP sessions should persist across multiple tool calls within a conversation
  • Stdio connections should handle MCP server process lifecycle properly
  • Connection failures should be handled gracefully without complex workarounds
  • API should be consistent and intuitive

Actual Behavior

  • Sessions are incorrectly detected as disconnected
  • "Connection closed" errors on subsequent conversations
  • Complex workarounds required for basic functionality
  • Inconsistent and confusing API design

Environment

  • ADK Version: 1.12.0
  • Python Version: 3.13.7
  • MCP Servers Tested: mongodb-mcp-server
  • Connection Type: Stdio
  • OS: Windows 11

Reproduction Steps

  1. Create an MCP Toolset with stdio connection
  2. Use it in an ADK agent for web interface
  3. First conversation works fine
  4. Second conversation fails with "Connection closed" error
  5. Subsequent conversations continue to fail

Chat Interface Images

Image

Related Issues

Model Information:

  • Are you using LiteLLM: No
  • Model : gemini-2.0-flash

Additional Context

The same MCP servers work reliably with other MCP clients (Claude Desktop, etc.), indicating this is an ADK-specific implementation issue rather than a problem with the MCP servers themselves.

Metadata

Metadata

Labels

help wanted[Community] Extra attention is neededmcp[Component] Issues about MCP support

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions