You've already forked adk-python
mirror of
https://github.com/encounter/adk-python.git
synced 2026-03-30 10:57:20 -07:00
d3796f9b33
Close #2893 Co-authored-by: Eliza Huang <heliza@google.com> PiperOrigin-RevId: 826070077
FastMCP Server-Side Sampling with ADK
This project demonstrates how to use server-side sampling with a fastmcp server connected to an ADK MCPToolset.
Description
The setup consists of two main components:
- ADK Agent (
agent.py): AnLlmAgentis configured with anMCPToolset. This toolset connects to a localfastmcpserver. - FastMCP Server (
mcp_server.py): Afastmcpserver 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.
The flow is as follows:
- The user provides a text prompt to the ADK agent.
- The agent decides to use the
analyze_sentimenttool from theMCPToolset. - The tool call is sent to the
mcp_server.py. - Inside the
analyze_sentimenttool,ctx.sample()is called. This delegates an LLM call to thefastmcpserver's own sampling handler. - The
mcp_server's LLM processes the prompt fromctx.sample()and returns the result to the server. - The server processes the LLM response and returns the final sentiment to the agent.
- The agent displays the result to the user.
Steps to Run
Prerequisites
- Python 3.10+
google-adklibrary installed.- A configured OpenAI API key.
1. Set up the Environment
Clone the project and navigate to the directory. Make sure your OPENAI_API_KEY is available as an environment variable.
2. Install Dependencies
Install the required Python libraries:
pip install fastmcp openai litellm
3. Run the Example
Navigate to the samples directory and choose this ADK agent:
adk web .
The agent will automatically start the FastMCP server in the background.
- Sample user prompt: "What is the sentiment of 'I love building things with Python'?"