-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Describe the bug
When using the ADK LlmAgent with the MCPToolset (Playwright MCP), the browser context/session is closed after every tool call, even when the same MCPToolset instance is reused and the agent is designed to support multi-step workflows. This results in the Playwright MCP server launching and closing a new browser for each action, breaking workflows that require session persistence (e.g., login followed by navigation).
To Reproduce
Steps to reproduce the behavior:
- start a new project for adk with default setup
- add the below snippet in agent.py
# ./adk_agent_samples/mcp_agent/agent.py
import os # Required for path operations
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 import StdioServerParameters
root_agent = LlmAgent(
model="gemini-2.0-flash",
name="web_automation_assistant_agent",
instruction="""
You are an **Automation Agent** that uses the **Playwright MCP (Model Context Protocol)** to perform browser automation, UI testing, and workflow execution.
Your role is not to output Playwright code directly, but to **translate user requests into Playwright MCP tool calls** and orchestrate the automation process.
- Use natural language to confirm or explain actions.
- When executing, **call Playwright MCP tools directly**.
- For longer flows, summarize progress after each step.
""",
tools=[
MCPToolset(
connection_params=StdioConnectionParams(
server_params=StdioServerParameters(
command="npx",
args=[
"-y", # Argument for npx to auto-confirm install
"@playwright/mcp@latest",
],
),
),
# Optional: Filter which tools from the MCP server are exposed
# tool_filter=['list_directory', 'read_file']
)
],
)
- run "adk run <agent_file>"
- Run a multi-step workflow (e.g., navigate to a page, log in, then perform another action).
- Observe that the browser context is closed after each step, and session state is not preserved.
Expected Behavior
The LlmAgent should persist the MCPToolset (and its session) across multiple tool calls within the same agent instance.
The Playwright MCP server should keep the browser context/session alive for the duration of the agent's workflow, allowing multi-step automation in a single browser session.
Actual Behavior
Each tool call from the agent results in a new browser context/session.
The Playwright MCP server closes the browser after each tool call, losing all session state (cookies, navigation, etc.).
This occurs even when the same MCPToolset instance is passed to the agent and reused.
Desktop (please complete the following information):
- OS: MacOS
- Python version(python -V): Python 3.13.7
- ADK version(pip show google-adk): adk, version 1.12.0
Model Information:
gemini-2.5-flash
Additional Information
The same MCP server persists sessions for other clients (e.g., Claude Desktop), so this appears to be an ADK client issue.
Wrapping the MCPToolset in a persistent class does not resolve the issue.
Using both SSE and stdio connection modes produces the same result.