Monday, August 11, 2025

Bridging the Gap: Building a FastMCP Server with a Google ADK Agent 🌉

 In previous posts, we’ve explored various methods for client-server communication using the FastMCP framework. We’ve seen how a local Python client or an AI Agent Development Kit (ADK) web agent can interact with a FastMCP server, whether it’s exposed as a pure Python file or a web service using Server-Sent Events (SSE).

Now, let’s reverse the dynamic. Instead of having an ADK agent consume a FastMCP server, we’ll build a FastMCP server that encapsulates and runs a Google ADK agent directly. This allows the FastMCP server to become a powerful intermediary, leveraging the ADK’s ability to use tools and interact with external services.

The goal is to create a powerful workflow: a client calls a single tool on our FastMCP server, which then triggers an internal ADK agent. This agent, in turn, uses its own tools to fetch data from a REST API, and finally, the result is sent back to the original client.

The entire project can be found on GitHub: FastMCP-ADK-Server-Client.


Step 1: Project Setup

Let’s start by setting up our project environment.

  1. Create a Project Directory and Virtual Environment Create a new folder named FastMCP-ADK-Server-Client. Inside this folder, create and activate a virtual environment. python -m venv .venv # On Windows .venv\Scripts\activate # On macOS/Linux source .venv/bin/activate
  2. Install Dependencies Create a requirements.txt file with the necessary libraries. fastmcp asyncio httpx google-adk google-generativeai python-dotenv Now install them with pip install -r requirements.txt.

Step 2: The Server (server.py)

This Python script uses FastMCP to create a server that exposes a single tool. This tool, when called, orchestrates a more complex workflow using an ADK agent.

Code Breakdown

Let’s analyze the key components of the server.py file.

  • 1. ADK Agent’s Tool (get_objects_by_ids)
    The get_objects_by_ids function is a standard asynchronous Python function. Its purpose is to perform an HTTP GET request to a public REST API (https://api.restful-api.dev/objects) to retrieve data for a specific list of object IDs. This function is designed to be a tool that the ADK LlmAgent can call.
  • 2. ADK LlmAgent Configuration
    The call_mcp_server_agent is an instance of LlmAgent.
    • model: We specify the large language model to use, in this case, gemini-2.0-flash.
    • instruction: This is a crucial prompt that defines the agent’s behavior. We instruct it to use the get_objects_by_ids tool when the user asks to fetch data for a specific object ID.
    • tools: A list of the functions the agent is allowed to execute. Here, we pass our get_objects_by_ids function.
  • 3. FastMCP Tool (get_objects_by_ids_using_adk_agent)
    The @mcp.tool() decorator is the core of our server. It registers the get_objects_by_ids_using_adk_agent function as a callable tool for any FastMCP client. When a client calls this tool, the function executes, triggering the ADK agent workflow. The agent then calls its own tool (get_objects_by_ids) to fetch the data.
  • 4. Server Execution
    The mcp.run() command starts the FastMCP server, making it listen for incoming requests from clients at the specified host and port (http://127.0.0.1:8001/mcp).

Step 3: The Client (client.py)

The client.py script is a simple FastMCP client. Its job is to connect to our running server and invoke the remote tool.

  • 1. Client Initialization
    The async with Client("http://127.0.0.1:8001/mcp") as client: block creates an asynchronous connection to our server.
  • 2. Remote Tool Invocation
    The await client.call_tool("get_objects_by_ids_using_adk_agent") line is where the magic happens. The client sends a request to the server, asking it to execute the named tool.
  • 3. Result
    The server processes the request (via the ADK agent and REST API), and the final result is returned to the client and printed to the console.

Output

When you run python client.py (after starting the server), the output will look similar to this:

1
2
(env) C:\vscode-python-workspace\FastMCP-ADK-Server-Client>python client.py
Fetched single: CallToolResult(content=[TextContent(type='text', text='OK. I have fetched the data for object ID 2. The result is: `{"get_objects_by_ids_response": {"result": [{"data": None, "id": "2", "name": "Apple iPhone 12 Mini, 256GB, Blue"}]}}`. I am ready to send it to the FASTMCP client.\n', annotations=None, structured_content=None, data=None, is_error=False)

This output confirms that our client successfully called the FastMCP server, which in turn activated the ADK agent, used the internal tool to hit an external API, and returned the final, processed data back through the server to the client. This demonstrates a powerful, layered architecture for building sophisticated, tool-using AI applications.

No comments: