Power your AI agents with MCP tools on Google Cloud Run

By Google Cloud Tech

AI Agent DevelopmentCloud Computing ServicesAPI IntegrationOpen Protocols
Share:

Key Concepts

  • Model Context Protocol (MCP): An open protocol developed by Anthropic that standardizes how context is provided to Large Language Models (LLMs).
  • Tools (in MCP): A component of MCP that enables LLMs or agents to take real-world actions by discovering and calling external APIs, querying databases, or running custom code.
  • MCP Server: A server that exposes tools via the MCP protocol, allowing AI agents to interact with them.
  • MCPToolSet: A data structure used by AI agents to specify the MCP servers they can interact with.
  • FastMCP: A Python library for creating MCP servers.
  • Agent Development Kit (ADK): A framework for building AI agents.
  • Cloud Run: A serverless platform on Google Cloud for deploying containerized applications.
  • OpenAPI Spec: A specification for describing RESTful APIs.

Main Topics and Key Points

What is MCP and Why is it Useful?

  • Definition: MCP stands for Model Context Protocol. It's an open protocol developed by Anthropic.
  • Purpose: It standardizes how context is provided to Large Language Models (LLMs).
  • Focus: The discussion primarily focuses on the "tools" component of MCP.
  • Functionality of Tools: Tools empower LLMs or agents to perform real-world actions. This includes:
    • Discovering and calling external APIs.
    • Querying databases.
    • Running custom code.
  • Target Audience:
    • API/Service Providers: Can package their APIs into MCP tools for easier consumption by clients and AI agents.
    • Developers: Building AI-assisted IDEs or AI agents can leverage MCP to enable agents to use APIs or perform actions on their behalf.
  • Example: An MCP server for Cloud Run allows developers to deploy a service using plain English commands instead of complex gcloud commands.

Deployment Options for MCP Servers

  • Local Deployment: Suitable for AI assistance during local code development.
  • Remote Deployment: Allows sharing MCP servers with others.
  • Cloud Run on Google Cloud: Highlighted as a great option for deploying MCP servers due to its serverless nature, simplifying infrastructure management and scaling.

Real-World Use Case: Sprinkler System Quotes

  • Scenario: Alice, an API provider for sprinkler systems, partners with lawn care companies.
  • Alice's Implementation:
    • She provides an MCP server with a tool to calculate price quotes for sprinkler systems.
    • The MCP server is deployed on Cloud Run for serverless operation.
  • David's Implementation:
    • David runs a lawn care company and uses an AI chatbot for customer interactions (lawn care tips, scheduling, orders).
    • He integrates Alice's MCP server into his agent.
  • End User Experience (Eve):
    • Eve uses David's chatbot to ask for lawn care tips.
    • She can also inquire about the cost of installing a sprinkler system.
    • David's chatbot, by calling Alice's MCP tool, can provide these quotes.

Developer Experience: Code Examples

Alice's MCP Server (Python with FastMCP)

  • Library: FastMCP is imported.
  • Tool Declaration: The @mcp.tool decorator is applied to the calculateQuote() function. This single annotation exposes the method via the MCP protocol.
  • Business Logic: The calculateQuote() function contains the logic for pricing, which depends on the customer's country and lawn square meters.

David's Agent (Calling Alice's Tool)

  • Tool Provision: When building an agent, an array of tools can be provided.
  • MCPToolSet: David's agent uses a single tool of type MCPToolSet, which holds the address (URL) of Alice's MCP server.
  • Automatic Discovery: Crucially, David's agent automatically discovers the input and output parameters and the description of Alice's tool by pointing to the MCP server URL, eliminating the need for manual specification.
  • Execution: The agent then calls Alice's API.

Cloud Run Logs for MCP Server

  • Alice can view the actual API call made by David's agent in the Cloud Run service logs.

Agent Deployment

  • David's agent was built using Google's Agent Development Kit (ADK) and also deployed to Cloud Run.

Advantages of MCP

  • Common Language for Tools: MCP acts as a standardized way to declare tools for AI agents.
  • Framework Agnosticism: Previously, tools were often tied to specific frameworks (e.g., LangChain, VS Code). MCP is a common protocol usable by many clients.
  • Unified API Exposure: APIs from different sources (e.g., calendar, email, file system) appear uniform to AI agents when exposed via MCP, simplifying integration.
  • Simplified Agent Interaction: Agent developers (like David) don't need to explicitly define input/output parameters for each tool; the agent discovers them from the MCP server.

Authentication for MCP Servers

  • Necessity: For private APIs, authentication is required to restrict access to registered partners.
  • MCP Support: MCP has evolving support for authentication.
  • Methods:
    • OAuth: Suitable for user-to-service authentication.
    • Cloud Run's Existing Infrastructure: Leveraging Cloud Run's authentication (e.g., Bearer Auth) is a good option for service-to-service authentication.

MCP Protocol Structure

  • MCP Inspector: A tool to visualize the MCP protocol.
  • Structure: The "tools" section is an array containing entries for each tool. Each tool entry specifies its name, required inputs (with their types), and potentially other details.
  • Comparison to OpenAPI Spec:
    • OpenAPI: Lower-level, includes implementation details like HTTP status codes.
    • MCP: Skips these implementation details, focusing on the functional interface.

Exposing Existing APIs with MCP

  • Scenario: A developer has a well-tested API and wants to expose it to AI agents without rewriting it.
  • FastMCP Solutions:
    • OpenAPI Spec Generation: Point FastMCP to an OpenAPI spec, and it can generate an MCP server.
    • Proxy Implementation: Write a small proxy using FastMCP that forwards calls to the existing underlying API.

Main Takeaways

  1. MCP is a common language for declaring tools for AI agents.
  2. Expose your API with MCP if you want agents to use it.
  3. Running an MCP server on Cloud Run is very easy.

Conclusion

The Model Context Protocol (MCP) offers a standardized and efficient way to enable AI agents to interact with real-world services and APIs. By abstracting away implementation details and providing a common interface for "tools," MCP simplifies the development of AI-powered applications. Its integration with serverless platforms like Cloud Run further streamlines deployment and scalability, making it an attractive solution for both API providers and AI agent developers. The ability to expose existing APIs with minimal changes and the evolving support for authentication enhance its practical utility.

Chat with this Video

AI-Powered

Hi! I can answer questions about this video "Power your AI agents with MCP tools on Google Cloud Run". 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