Skip to content

Commit d3796f9

Browse files
lizzijcopybara-github
authored andcommitted
feat: Add example for using ADK with Fast MCP sampling
Close #2893 Co-authored-by: Eliza Huang <heliza@google.com> PiperOrigin-RevId: 826070077
1 parent 9704d27 commit d3796f9

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# FastMCP Server-Side Sampling with ADK
2+
3+
This project demonstrates how to use server-side sampling with a `fastmcp` server connected to an ADK `MCPToolset`.
4+
5+
## Description
6+
7+
The setup consists of two main components:
8+
9+
1. **ADK Agent (`agent.py`):** An `LlmAgent` is configured with an `MCPToolset`. This toolset connects to a local `fastmcp` server.
10+
2. **FastMCP Server (`mcp_server.py`):** A `fastmcp` server that exposes a single tool, `analyze_sentiment`. This server is configured to use its own LLM for sampling, independent of the ADK agent's LLM.
11+
12+
The flow is as follows:
13+
1. The user provides a text prompt to the ADK agent.
14+
2. The agent decides to use the `analyze_sentiment` tool from the `MCPToolset`.
15+
3. The tool call is sent to the `mcp_server.py`.
16+
4. Inside the `analyze_sentiment` tool, `ctx.sample()` is called. This delegates an LLM call to the `fastmcp` server's own sampling handler.
17+
5. The `mcp_server`'s LLM processes the prompt from `ctx.sample()` and returns the result to the server.
18+
6. The server processes the LLM response and returns the final sentiment to the agent.
19+
7. The agent displays the result to the user.
20+
21+
## Steps to Run
22+
23+
### Prerequisites
24+
25+
- Python 3.10+
26+
- `google-adk` library installed.
27+
- A configured OpenAI API key.
28+
29+
### 1. Set up the Environment
30+
31+
Clone the project and navigate to the directory. Make sure your `OPENAI_API_KEY` is available as an environment variable.
32+
33+
### 2. Install Dependencies
34+
35+
Install the required Python libraries:
36+
37+
```bash
38+
pip install fastmcp openai litellm
39+
```
40+
41+
### 3. Run the Example
42+
43+
Navigate to the `samples` directory and choose this ADK agent:
44+
45+
```bash
46+
adk web .
47+
```
48+
49+
The agent will automatically start the FastMCP server in the background.
50+
51+
- **Sample user prompt:** "What is the sentiment of 'I love building things with Python'?"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 . import agent
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
import os
16+
17+
from google.adk.agents import LlmAgent
18+
from google.adk.models.lite_llm import LiteLlm
19+
from google.adk.tools.mcp_tool import MCPToolset
20+
from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams
21+
from mcp import StdioServerParameters
22+
23+
# This example uses the OpenAI API for both the agent and the server.
24+
# Ensure your OPENAI_API_KEY is available as an environment variable.
25+
api_key = os.getenv('OPENAI_API_KEY')
26+
if not api_key:
27+
raise ValueError('The OPENAI_API_KEY environment variable must be set.')
28+
29+
# Configure the StdioServerParameters to start the mcp_server.py script
30+
# as a subprocess. The OPENAI_API_KEY is passed to the server's environment.
31+
server_params = StdioServerParameters(
32+
command='python',
33+
args=['mcp_server.py'],
34+
env={'OPENAI_API_KEY': api_key},
35+
)
36+
37+
# Create the ADK MCPToolset, which connects to the FastMCP server.
38+
# The `tool_filter` ensures that only the 'analyze_sentiment' tool is exposed
39+
# to the agent.
40+
mcp_toolset = MCPToolset(
41+
connection_params=StdioConnectionParams(
42+
server_params=server_params,
43+
),
44+
tool_filter=['analyze_sentiment'],
45+
)
46+
47+
# Define the ADK agent that uses the MCP toolset.
48+
root_agent = LlmAgent(
49+
model=LiteLlm(model='openai/gpt-4o'),
50+
name='SentimentAgent',
51+
instruction=(
52+
'You are an expert at analyzing text sentiment. Use the'
53+
' analyze_sentiment tool to classify user input.'
54+
),
55+
tools=[mcp_toolset],
56+
)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
import logging
16+
import os
17+
18+
from fastmcp import Context
19+
from fastmcp import FastMCP
20+
from fastmcp.experimental.sampling.handlers.openai import OpenAISamplingHandler
21+
from openai import OpenAI
22+
23+
logging.basicConfig(level=logging.INFO)
24+
API_KEY = os.getenv("OPENAI_API_KEY")
25+
26+
# Set up the server's LLM handler using the OpenAI API.
27+
# This handler will be used for all sampling requests from tools on this server.
28+
llm_handler = OpenAISamplingHandler(
29+
default_model="gpt-4o",
30+
client=OpenAI(
31+
api_key=API_KEY,
32+
),
33+
)
34+
35+
36+
# Create the FastMCP Server instance.
37+
# The `sampling_handler` is configured to use the server's own LLM.
38+
# `sampling_handler_behavior="always"` ensures the server never delegates
39+
# sampling back to the ADK agent.
40+
mcp = FastMCP(
41+
name="SentimentAnalysis",
42+
sampling_handler=llm_handler,
43+
sampling_handler_behavior="always",
44+
)
45+
46+
47+
@mcp.tool
48+
async def analyze_sentiment(text: str, ctx: Context) -> dict:
49+
"""Analyzes sentiment by delegating to the server's own LLM."""
50+
logging.info("analyze_sentiment tool called with text: %s", text)
51+
prompt = f"""Analyze the sentiment of the following text as positive,
52+
negative, or neutral. Just output a single word.
53+
Text to analyze: {text}"""
54+
55+
# This delegates the LLM call to the server's own sampling handler,
56+
# as configured in the FastMCP instance.
57+
logging.info("Attempting to call ctx.sample()")
58+
try:
59+
response = await ctx.sample(prompt)
60+
logging.info("ctx.sample() successful. Response: %s", response)
61+
except Exception as e:
62+
logging.error("ctx.sample() failed: %s", e, exc_info=True)
63+
raise
64+
65+
sentiment = response.text.strip().lower()
66+
67+
if "positive" in sentiment:
68+
result = "positive"
69+
elif "negative" in sentiment:
70+
result = "negative"
71+
else:
72+
result = "neutral"
73+
74+
logging.info("Sentiment analysis result: %s", result)
75+
return {"text": text, "sentiment": result}
76+
77+
78+
if __name__ == "__main__":
79+
print("Starting FastMCP server with tool 'analyze_sentiment'...")
80+
# This runs the server process, which the ADK agent will connect to.
81+
mcp.run()

0 commit comments

Comments
 (0)