How to build an AI agent with MCP, ADK, and A2A on Google Cloud

By Google for Developers

AI Agent DevelopmentCloud Computing ServicesAPI ProtocolsSoftware Development Frameworks
Share:

Key Concepts

  • MCP (Model Context Protocol): A standard for connecting AI models to necessary resources, prompts, and tools, acting as a reverse proxy or API server for API servers.
  • ADK (Agent Development Kit): A framework for building and deploying AI agents, designed to simplify complex agent creation.
  • A2A (Agent-to-Agent Protocol): An open protocol enabling agents to communicate and collaborate, regardless of their underlying framework or language.
  • Cloud Run: A managed compute platform on Google Cloud that allows running containerized applications, offering auto-scaling and security.
  • Cloud Shell: A command-line environment provided by Google Cloud for managing resources and running commands.
  • Agent Card: A digital representation of an agent's capabilities, used for discovery and collaboration within the A2A protocol.
  • UV: A tool for managing Python dependencies.

Setting Up the Google Cloud Environment

The tutorial begins by guiding the user through setting up a Google Cloud environment. This involves:

  1. Creating a New Google Cloud Project: Ensuring billing is enabled for the project.
  2. Activating Cloud Shell: Accessing the command-line interface within the Google Cloud console.
  3. Authentication: Running a command to authenticate with Google Cloud.
  4. Setting Project ID: Exporting the project ID as an environment variable for easier resource association. This is crucial for ensuring all created resources belong to the correct project.
  5. Enabling APIs: Using a single command to enable necessary APIs: Cloud Run, Cloud Build, Artifact Registry, and Vertex AI. This step may take a few minutes.
  6. Verifying Python Version: Confirming that Python 3.10 or newer is installed, which is the default in Cloud Shell.

Downloading Project Source Code and Configuring Environment

Once the Google Cloud environment is ready, the next steps involve obtaining and configuring the project's code:

  1. Cloning the Repository: Downloading the project's source code from GitHub using git clone.
  2. Navigating to Project Directory: Changing the current directory to the newly cloned project folder.
  3. Installing UV: Installing the UV tool for Python dependency management using a curl command. A note is provided that a new terminal tab might be needed if a UV notfound error occurs.
  4. Configuring Environment Variables: Creating an .env file and populating it with environment variables, including the project ID. The contents of this file are then checked to ensure correct setup.

Setting Up and Deploying the MCP Server

The tutorial then focuses on building and deploying the MCP server, which will expose tools for the agent:

  1. MCP Server Functionality: The MCP server's role is to expose tools. In this case, a tool to fetch currency exchange rates is implemented.
  2. FastMCP Framework: The server is built using FastMCP, a Python framework for creating MCP servers and clients with decorators and Python functions.
  3. server.py Code: The MCP server/server.py file is examined. The get_exchange_rate function is highlighted as the core tool, decorated with @MCP_tool to register it. This function calls an external API for exchange rates.
  4. Running the Local MCP Server: The server is started locally in Cloud Shell on port 8080.
  5. Testing the Local Server: A separate Cloud Shell tab is used to run a test script that connects to the local MCP server and makes a tool call. A success message with the exchange rate confirms the server is operational.
  6. Stopping the Local Server: The server is stopped using Ctrl+C.
  7. Deploying to Cloud Run: To make the server permanent and accessible, it's deployed to Cloud Run.
    • Navigating to Servers Directory: Changing the directory to servers.
    • G-Cloud Deployment Command: A single gcloud command deploys the server. The no-allow-unauthenticated flag is emphasized for security.
  8. Securing the Deployed Server: Because the server is secured, a Cloud Run proxy is used to create an authenticated tunnel from Cloud Shell to the live service.
  9. Testing the Deployed Server: A new Cloud Shell tab is opened, and the same test script is run. This time, the request is routed through the proxy to the Cloud Run server, demonstrating successful external access.
  10. Checking Logs: The logs of the deployed service can be checked via the command line to verify its operation.

Building an Agent with ADK

With the MCP server deployed, the next step is to build an agent that can utilize it:

  1. Agent Code (currency_agent-a.py): The agent's code is examined in the Cloud Shell editor.
    • System Instruction: A system instruction defines the agent's purpose (currency-related queries) and rules.
    • MCP Tool Set: This section is crucial for connecting the agent to the deployed MCP server, granting it access to the get_exchange_rate tool.
  2. ADK Web Interface: The ADK provides a simple web interface for testing agents.
  3. Starting the ADK Web Server: The adk web command starts a server on port 8000. The Cloud Run proxy from the previous step must remain running.
  4. Accessing the Web Preview: The web preview button in the Cloud Shell toolbar is used to access the interface. The port might need to be changed to 8000.
  5. Testing the Agent: In the web interface, the "currency agent" is selected, and a question like "what is 250 Canadian dollars to USD?" is asked. The agent successfully uses the deployed MCP tool to provide the answer.

Exposing the Agent via A2A

The final stage involves making the agent available for inter-agent communication using the A2A protocol:

  1. A2A Protocol Overview:
    • Purpose: Standardizes agent collaboration across frameworks and vendors.
    • Underlying Standards: Built on HTTP, SSE, and JSON RPC, with enterprise-grade security.
    • Modality Agnostic: Supports communication across text, audio, video, and other unstructured formats.
    • Discovery and Collaboration: Agents discover each other's capabilities and collaborate using "agent cards."
  2. Agent Card: A digital business card advertising an agent's skills. For the currency agent, a skill for the get_exchange_rate tool would be defined and packaged into an agent card with details like name and description.
  3. Building the A2A Server:
    • ADK Utility Function: The adk a2a utility function simplifies the process by converting the existing agent into an A2A-compatible application served through Unicorn.
    • Automatic Agent Card Generation: This function also generates an agent card automatically.
    • Code Implementation: This is achieved with just two lines of code in currency_agent-agent.py.
  4. Launching the A2A Server:
    • Prerequisites: Ensure the Cloud Run proxy and ADK web server are still running in their respective tabs.
    • Command Execution: A new Cloud Shell terminal is used to run the command to launch the A2A server, which starts on port 10,000.
  5. Testing the A2A Server:
    • Test Client (currency_agent-testclient.py): This script uses the A2A Python SDK's A2AClient class to connect to the agent and run tests.
    • Test Execution: The client connects, sends a request (e.g., "how much is 100 USD in Canadian dollars?"), and receives a response using live exchange rate data.
    • Successful Communication: A successful run indicates that other programs and agents can communicate with the currency agent over A2A.
    • Remote Agent Configuration: To access this A2A agent from another agent, a remote A2A agent sub-agent needs to be defined, pointing to the public agent card.

Cleanup

To avoid future charges, it is crucial to clean up the created resources:

  1. Deleting the Project: The simplest method is to delete the entire Google Cloud project used for the tutorial.
  2. Navigation: Go to the "Manage resources" page in the Google Cloud Console.
  3. Confirmation: Select the project and click "delete," then type the project ID to confirm.

Conclusion

The tutorial successfully demonstrates the creation of a complete end-to-end agentic system. Key takeaways include:

  • Creating and deploying an MCP server on Cloud Run to provide tools for an agent.
  • Building an agent using ADK.
  • Exposing the agent to other agents via the A2A protocol.

The video concludes by encouraging viewers to build their own AI agents using these powerful tools.

Chat with this Video

AI-Powered

Hi! I can answer questions about this video "How to build an AI agent with MCP, ADK, and A2A on Google Cloud". What would you like to know?

Chat is based on the transcript of this video and may not be 100% accurate.

Related Videos

Ready to summarize another video?

Summarize YouTube Video