Monday, August 11, 2025

Building an Agent-Powered Client with Google ADK and FASTMCPServer

 Let’s explore how to create a local Python file that acts as a client to a FASTMCPServer that has a Google Agent Development Kit (ADK) agent integrated with it. This client uses the ADK Web to call a FASTMCPClient to fetch data.

The Agent’s Role: Orchestrating Communication

The provided code snippet defines a client agent using the Google ADK. Its main purpose is to receive a user’s request, understand the need to fetch data for a specific ID, and then use a specialized tool to call a separate, running server process (the FASTMCPServer) to get that data.

This client-server pattern is very common in agent development. Here, a “client-side” agent orchestrates tasks by communicating with one or more “server-side” agents or services that expose specific capabilities as tools.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import asyncio
from fastmcp import Client
from typing import Any
from google.genai import types
from dotenv import load_dotenv
 
from google.adk.agents import LlmAgent
from pydantic import BaseModel, Field
 
from google.adk.agents import Agent
from google.adk.events import Event
from google.adk.runners import Runner
from google.adk.sessions import InMemorySessionService
 
async def get_mcp_data(object_id: str) -> dict:
    """Fetches an object by its ID from the MCP server."""
    print(f"Tool 'get_mcp_data' called with object_id: {object_id}")
    # async with Client("restapi-mcp-server.py") as client:
    #     single = await client.call_tool("get_object_by_id", {"object_id": object_id})
    #     print("Fetched single:", single)
    #     return single
    async with Client("http://127.0.0.1:8001/mcp") as client:
        single = await client.call_tool("get_objects_by_ids_using_adk_agent")
        print("Fetched single:", single)
        return single
         
call_local_mcp_adk_remote_server_agent = LlmAgent(
    model="gemini-2.0-flash",
    name="assistant",
    description="This agent is used to get data using FASTMCP client by calling the FASTMCP server ",
    instruction="""Help user to fetch the data from the FASTMCP Server using FASTMCP Client.
    When the user asks to fetch data for a specific object ID, use the `get_mcp_data` tool and pass the ID to it.
    """,
    tools=[get_mcp_data],
)
 
root_agent=call_local_mcp_adk_remote_server_agent

This code snippet defines a client agent using the Google Agent Development Kit (ADK). Its main purpose is to receive a user request, understand that it needs to fetch data for a specific ID, and then use a tool to call a separate, running server process (an MCP server) to get that data.

This pattern is very common in agent development, where you have a “client-side” agent that orchestrates tasks by communicating with one or more “server-side” agents or services that expose specific capabilities as tools.

Code Breakdown

The core of this client-side agent is comprised of two main components: a tool function and the agent definition itself.

1. get_mcp_data(object_id: str) -> dict:

This is an asynchronous Python function exposed to the agent as a tool. The agent’s underlying large language model (LLM) can decide to call this function when it determines it’s necessary based on the user’s request.

  • It uses fastmcp.Client to connect to a server running at http://127.0.0.1:8001/mcp. This is the address of the FASTMCPServer you are communicating with.
  • The line await client.call_tool("get_objects_by_ids_using_adk_agent") makes a remote procedure call (RPC) to the server. It asks the server to execute a tool named get_objects_by_ids_using_adk_agent.
  • The result from the server is then returned by this tool, completing the data retrieval process.

2. call_local_mcp_adk_remote_server_agent = LlmAgent(...)

This section creates an LlmAgent, which is an agent powered by a Large Language Model (in this case, gemini-2.0-flash).

  • instruction: This is the crucial prompt that guides the agent’s behavior. It’s explicitly instructed to use the get_mcp_data tool whenever a user asks to fetch data for an object ID.
  • tools=[get_mcp_data]: This is how the agent is given its capabilities. The ADK framework analyzes the get_mcp_data function (its name, parameters, and docstring) and makes it available for the LLM to call.
  • root_agent: This line assigns the agent to a variable named root_agent. In many ADK applications, this is the conventional name for the main agent that will be executed by the ADK Runner.

To see the complete source code, you can refer to the GitHub repositories:

No comments: