|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from google.adk.agents import Agent |
| 16 | +from google.adk.tools import AgentTool |
| 17 | +from google.adk.tools.mcp_tool import McpToolset |
| 18 | +from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams |
| 19 | +from mcp import StdioServerParameters |
| 20 | + |
| 21 | +# Create MCP toolset |
| 22 | +# This uses the simple-tool MCP server via stdio |
| 23 | +# The server will be launched automatically using uvx from the subdirectory |
| 24 | +mcp_toolset = McpToolset( |
| 25 | + connection_params=StdioConnectionParams( |
| 26 | + server_params=StdioServerParameters( |
| 27 | + command="uvx", |
| 28 | + args=[ |
| 29 | + "--from", |
| 30 | + "git+https://github.com/modelcontextprotocol/python-sdk.git#subdirectory=examples/servers/simple-tool", |
| 31 | + "mcp-simple-tool", |
| 32 | + ], |
| 33 | + ), |
| 34 | + timeout=10.0, |
| 35 | + ) |
| 36 | +) |
| 37 | + |
| 38 | +# Create sub-agent with MCP tools |
| 39 | +# This agent has direct access to MCP tools |
| 40 | +sub_agent = Agent( |
| 41 | + name="mcp_helper", |
| 42 | + model="gemini-2.5-flash", |
| 43 | + description=( |
| 44 | + "A helpful assistant with access to MCP tools for fetching websites." |
| 45 | + ), |
| 46 | + instruction="""You are a helpful assistant with access to MCP tools. |
| 47 | +
|
| 48 | +When the user asks for help: |
| 49 | +1. Explain what tools you have available (website fetching) |
| 50 | +2. Use the appropriate tool if needed |
| 51 | +3. Provide clear and helpful responses |
| 52 | +
|
| 53 | +You have access to a website fetcher tool via MCP. Use it to fetch and return website content.""", |
| 54 | + tools=[mcp_toolset], |
| 55 | +) |
| 56 | + |
| 57 | +# Wrap sub-agent as an AgentTool |
| 58 | +# This allows the main agent to delegate tasks to the sub-agent |
| 59 | +# The sub-agent has access to MCP tools for fetching websites |
| 60 | +mcp_agent_tool = AgentTool(agent=sub_agent) |
| 61 | + |
| 62 | +# Create main agent |
| 63 | +# This agent can delegate to the sub-agent via AgentTool |
| 64 | +root_agent = Agent( |
| 65 | + name="main_agent", |
| 66 | + model="gemini-2.5-flash", |
| 67 | + description="Main agent that can delegate to a sub-agent with MCP tools.", |
| 68 | + instruction="""You are a helpful assistant. You have access to a sub-agent (mcp_helper) |
| 69 | +that has MCP tools for fetching websites. |
| 70 | +
|
| 71 | +When the user asks for help: |
| 72 | +- If they need to fetch a website, call the mcp_helper tool |
| 73 | +- Otherwise, respond directly |
| 74 | +
|
| 75 | +Always be helpful and explain what you're doing.""", |
| 76 | + tools=[mcp_agent_tool], |
| 77 | +) |
0 commit comments