Skip to content

MCP STDIO Performance Issue - Async Context Management Bug #2980

Closed

Description

@AlfredoJF

Describe the bug

STDIO MCP connections restart between requests, causing significant performance degradation (2-5 seconds per request instead of expected 10-50ms). This occurs due to an async context management issue where AsyncExitStack cleanup is attempted from a different async task than where it was created.

To Reproduce

  1. Create an ADK agent with STDIO MCP toolset:

    from google.adk.tools.mcp_tool import McpToolset
    from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams
    from mcp import StdioServerParameters
    
    tools=[McpToolset(
        connection_params=StdioConnectionParams(
            server_params=StdioServerParameters(
                command="node", 
                args=["path/to/mcp-server.js"]
            )
        )
    )]
  2. Run the agent and observe logs:

    INFO:google_adk.google.adk.tools.mcp_tool.mcp_session_manager:Cleaning up disconnected session: stdio_session
    WARNING:google_adk.google.adk.tools.mcp_tool.mcp_session_manager:Error during disconnected session cleanup: Attempted to exit cancel scope in a different task than it was entered in
    

Expected behaviour

  • MCP sessions should persist between tool calls (10-50ms per request)
  • No async context management errors
  • Sessions only cleaned up when actually disconnected

Desktop Information

  • OS: macOS (affects all platforms)
  • Python version: 3.10+
  • ADK version: current main branch
  • LiteLLM: No
  • Model: Any (reproduction doesn't depend on model)

Additional context

Root Cause: src/google/adk/tools/mcp_tool/mcp_session_manager.py:330-338

else:
    # Session is disconnected, clean it up
    logger.info('Cleaning up disconnected session: %s', session_key)
    try:
        await exit_stack.aclose()  # ← Problem: called from wrong async task
    except Exception as e:
        logger.warning('Error during disconnected session cleanup: %s', e)
    finally:
        del self._sessions[session_key]

Problem: AsyncExitStack created during agent initialisation (main task) but cleanup attempted from tool execution task (different async context), violating anyio's cancel scope requirements.

Impact: Affects all STDIO MCP users, especially plugin-based tools and performance-critical applications.

Activity

  1. added
    mcp
    on Sep 19, 2025
  2. added theissue type on Sep 19, 2025
  3. AlfredoJF commented on Sep 19, 2025

    @AlfredoJF
    Author

    Potential related issues:
    #2979
    #2927

  4. added this to the 10/27 ADK Week milestone on Oct 24, 2025
  5. llalitkumarrr commented on Dec 9, 2025

    @llalitkumarrr
    Collaborator

    Hello @AlfredoJF ,

    We recommend upgrading to google-adk==1.20.0 and testing again. If you still encounter the issue, could you please share your observations with us? This will help us understand the behavior and assist you more effectively.

  6. self-assigned this
    on Dec 9, 2025
  7. added
    answered
    request clarification
    on Dec 9, 2025
  8. adk-bot commented on Dec 18, 2025

    @adk-bot
    Collaborator

    This issue has been automatically marked as stale because it has not had recent activity for 7 days after a maintainer requested clarification. It will be closed if no further activity occurs within 7 days.

  9. added
    stale
    on Dec 18, 2025
  10. adk-bot commented on Dec 26, 2025

    @adk-bot
    Collaborator

    This has been automatically closed because it has been marked as stale for over 7 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

answeredmcprequest clarificationstale

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions