Files
adk-python/contributing/samples/mcp_in_agent_tool_remote
Wei Sun (Jack)andCopybara-Service fa5c546a55 fix(tools): Add proper cleanup for AgentTool to prevent MCP session errors
Merge https://github.com/google/adk-python/pull/3411

## Summary

Fixes AgentTool cleanup to prevent MCP session errors by calling `runner.close()` after sub-agent execution.

## Problem

When using AgentTool with MCP tools, the runner cleanup happened during garbage collection in a different async task context, causing:
```
RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
```

## Solution

- Call `await runner.close()` immediately after sub-agent execution in AgentTool
- This ensures MCP sessions and other resources are cleaned up in the correct async task context
- Updated test mock to include the close() method

## Demo Agents

Added two comprehensive demo agents showing how to use AgentTool with MCP tools:

### mcp_in_agent_tool_remote (SSE mode)
- Uses HTTP/SSE connection to remote MCP server
- Zero-installation setup with `uvx`
- Demonstrates server-side MCP deployment pattern

### mcp_in_agent_tool_stdio (stdio mode)
- Uses subprocess connection with automatic server launch
- Fully automatic setup with `uvx` subdirectory syntax
- Demonstrates embedded MCP deployment pattern

Both demos:
- Use Gemini 2.5 Flash
- Include example prompts for JSON Schema exploration
- Have comprehensive READMEs with architecture diagrams
- Follow ADK agent structure conventions

## Testing

-  All existing unit tests pass
-  Manual testing with both SSE and stdio modes
-  Verified cleanup happens in correct async context
-  No more cancel scope errors with MCP tools

## Related

- Fixes #1112
- Related to #929

Co-authored-by: Wei Sun (Jack) <weisun@google.com>
COPYBARA_INTEGRATE_REVIEW=https://github.com/google/adk-python/pull/3411 from google:fix/agent-tool-mcp-cleanup 9ae753b5a4
PiperOrigin-RevId: 828651896
2025-11-05 15:23:39 -08:00
..

AgentTool with MCP Demo (SSE Mode)

This demo shows how AgentTool works with MCP (Model Context Protocol) toolsets using SSE mode.

SSE vs Stdio Mode

This demo uses SSE (Server-Sent Events) mode where the MCP server runs as a separate HTTP server:

  • Remote connection - Connects to server via HTTP
  • Separate process - Server must be started manually
  • Network communication - Uses HTTP/SSE for messaging

For the stdio (subprocess) version, see mcp_in_agent_tool_stdio.

Setup

Start the MCP simple-tool server in SSE mode (in a separate terminal):

# Run the server using uvx (no installation needed)
# Port 3000 avoids conflict with adk web (which uses 8000)
uvx --from 'git+https://github.com/modelcontextprotocol/python-sdk.git#subdirectory=examples/servers/simple-tool' \
    mcp-simple-tool --transport sse --port 3000

The server should be accessible at http://localhost:3000/sse.

Running the Demo

adk web contributing/samples

Then select mcp_in_agent_tool_remote from the list and interact with the agent.

Try These Prompts

This demo uses Gemini 2.5 Flash as the model. Try these prompts:

  1. Check available tools:

    What tools do you have access to?
    
  2. Fetch and summarize JSON Schema specification:

    Use the mcp_helper to fetch https://json-schema.org/specification and summarize the key features of JSON Schema
    

Architecture

main_agent (root_agent)
  │
  └── AgentTool wrapping:
        │
        └── mcp_helper (sub_agent)
              │
              └── McpToolset (SSE connection)
                    │
                    └── http://localhost:3000/sse
                          │
                          └── MCP simple-tool server
                                │
                                └── Website Fetcher Tool