MCP Servers and A2A Protocol
MCP (Model Context Protocol)
Definition
-
The Model Context Protocol (MCP) is an open-source standard developed by Anthropic (the creators of Claude AI). It serves as a universal interface for connecting large language models (LLMs) and AI agents to external tools, data sources, and services.
-
Think of MCP as a "USB-C for AI"—a plug-and-play protocol that standardizes how AI applications access and interact with the real world, eliminating the need for custom integrations for each tool or data source.
-
MCP enables AI models to securely fetch context (e.g., data from databases, APIs, or files) in a structured way, making AI more agentic (capable of taking actions) and interoperable. It's designed to bridge the gap between isolated AI models and dynamic external environments, allowing for two-way communication where the AI can query, update, or execute tasks on external systems.
-
By standardizing communication, MCP reduces development friction, promotes modularity, and supports secure data exchange. It's particularly useful in enterprise settings where AI needs to interact with proprietary tools without exposing sensitive data.
Components
MCP's architecture is divided into three main parts: the client, the server, and the protocol itself. These components work together to create a seamless, standardized pipeline for AI-tool interactions.
- MCP Client:
- Explanation: The client is the AI-side component that initiates connections and orchestrates interactions. It acts as the "bridge" between the LLM or AI agent and external resources. The client handles tasks like discovering available tools from servers, routing requests to the appropriate server, negotiating capabilities (e.g., what data formats or authentication methods are supported), and managing the LLM's context window by injecting retrieved data. Clients are typically applications or frameworks that embed LLMs and use MCP to extend their capabilities. They maintain a 1:1 connection with servers for efficiency and can handle multiple servers simultaneously.
- Role in Workflow: The client queries the server for tool schemas (e.g., via OpenAPI-like descriptions), prompts the LLM to decide on actions, and then executes calls. It also manages errors, retries, and session state.
-
Examples of MCP Clients:
- Claude Desktop: Anthropic's official desktop app for Claude AI, which uses MCP to connect to local files, databases, or web services for tasks like document analysis or scheduling.
- Cursor AI: An IDE-based client for code editing, integrating MCP servers like GitHub for automated code reviews or deployments.
- Windsurf: An open-source desktop AI assistant that supports MCP for file access and API calls, often used for productivity workflows.
- Cline/Continue: Terminal-based clients for developers, enabling MCP integration with tools like databases for querying data via natural language.
- LibreChat/Chainlit: Web-based chat interfaces that act as MCP clients for conversational AI, connecting to servers like MongoDB for data retrieval.
-
MCP Server:
-
Explanation: The server is the tool-side or data-source component that exposes resources (e.g., APIs, databases, or files) in a standardized MCP format. It acts as a secure gateway, providing tool descriptions (e.g., function schemas), handling authentication, and executing requests from clients.
Servers abstract away implementation details, such as differing API endpoints or auth methods, allowing the AI to interact uniformly. For instance, a server might normalize field names (e.g., converting "start_time" to "event_time") or handle OAuth flows transparently. Servers can be local (e.g., on-device) or remote (cloud-hosted), and they support real-time updates and secure data streaming. - Role in Workflow: When called by a client, the server validates requests, performs actions (e.g., querying a database), and returns results in a consistent JSON format. It ensures security through features like token-based auth and rate limiting. - Examples of MCP Servers: - GitHub MCP Server: Exposes GitHub APIs for code management, allowing AI to automate pull requests or issue tracking. - MongoDB MCP Server: Provides natural language querying for databases, enabling AI to fetch or update records without SQL knowledge. - Azure MCP Server: Integrates with Microsoft Azure services for cloud-based data operations, like blob storage or AI workflows. - Google Calendar MCP Server: A simple server for scheduling, as seen in demos where AI books meetings via natural language. - Custom Servers: Open-source examples like those on GitHub (e.g., punkpeye/awesome-mcp-servers) for file systems or APIs.
-
MCP Protocol:
- Explanation: The protocol is the communication standard that defines how clients and servers interact. It's based on RESTful APIs with JSON payloads, using HTTP/HTTPS for transport. Key elements include tool discovery (via endpoints like
/tools), request/response formats (e.g., JSON schemas for parameters), authentication (OAuth, API keys, JWT), and error handling (standardized codes). The protocol abstracts differences across tools, ensuring consistency—e.g., it normalizes auth flows so the LLM doesn't need to handle them directly. It supports both synchronous (request-response) and asynchronous (e.g., webhooks for long-running tasks) interactions.
How MCP Works
- Setup: A developer deploys an MCP server (e.g., for Google Calendar) and connects it to a client (e.g., Claude Desktop). The client discovers the server's tools by querying an endpoint like
/tools, which returns JSON schemas describing available functions (e.g., "create_event" with parameters like "start_time"). - Interaction: The user prompts the AI (e.g., "Schedule a meeting at 3 PM"). The client passes this to the LLM, which generates a tool call based on the schema. The client sends a POST request to the server (e.g.,
/execute) with the parameters. - Execution: The server authenticates the request, executes the action (e.g., creates the calendar event), and returns a JSON response with results or errors.
- Response: The client injects the response into the LLM's context, allowing the AI to refine or confirm the output to the user. This flow ensures secure, efficient, and standardized interactions, with features like rate limiting and encryption for enterprise use.
Python Code Demonstration: MCP Client Calling MCP Server
Here's a simplified Python example adapted from GitHub repos like RGGH/mcp-client-x and jlowin/fastmcp, demonstrating a basic MCP client (using OpenAI's API for the LLM) calling a custom MCP server (e.g., a TODO list manager). This uses FastAPI for the server and requests for the client. In practice, you'd run the server separately and connect via URL.
Server Code (mcp_server.py): Run this first to start the server.
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import uvicorn
app = FastAPI(title="Simple MCP TODO Server")
# Sample data store
todos = []
class TodoItem(BaseModel):
task: str
# MCP Tool Schema Endpoint
@app.get("/tools")
def get_tools():
return {
"tools": [
{
"name": "add_todo",
"description": "Add a new TODO item",
"parameters": {"task": {"type": "string", "description": "The task description"}}
}
]
}
# Execute Endpoint
@app.post("/execute")
def execute(request: dict):
tool_name = request.get("tool_name")
params = request.get("parameters")
if tool_name == "add_todo":
if "task" not in params:
raise HTTPException(status_code=400, detail="Missing 'task' parameter")
todos.append(params["task"])
return {"result": f"Added TODO: {params['task']}", "todos": todos}
raise HTTPException(status_code=404, detail="Tool not found")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
Client Code (mcp_client.py): This simulates an AI client querying the server.
import requests
import openai # Assuming OpenAI for LLM; replace with Anthropic/Claude if needed
# Server URL
SERVER_URL = "http://localhost:8000"
# Discover tools
tools_response = requests.get(f"{SERVER_URL}/tools")
tools = tools_response.json()["tools"]
print("Available Tools:", tools)
# Simulate LLM prompt to decide tool call (in real use, integrate with LLM)
prompt = "Add a TODO: Buy groceries"
# Here, mock LLM output: decide to call 'add_todo' with params
tool_call = {"tool_name": "add_todo", "parameters": {"task": "Buy groceries"}}
# Call server
execute_response = requests.post(f"{SERVER_URL}/execute", json=tool_call)
if execute_response.status_code == 200:
result = execute_response.json()
print("Server Response:", result)
else:
print("Error:", execute_response.json())
How to Run:
1. Install dependencies: pip install fastapi uvicorn pydantic requests openai.
2. Run server: python mcp_server.py.
3. Run client: python mcp_client.py.
This demonstrates discovery (/tools), calling (/execute), and response handling. In advanced setups, integrate with an LLM library like LangChain for dynamic tool selection.
Advantages
- Interoperability: Any MCP client can connect to any MCP server without custom code, fostering an ecosystem of reusable components.
- Security and Control: Built-in auth and data isolation prevent AI from accessing unauthorized resources.
- Efficiency: Reduces prompt engineering by providing structured tool calls, minimizing token usage in LLMs.
- Modularity: Enables "everything apps" where one client (e.g., Cursor) becomes multifunctional via multiple servers.
Disadvantages
- Security Risks in Production: Early implementations may have vulnerabilities in handling multiple integrations; requires robust infrastructure (e.g., Composio's MCP enhancements).
- Complexity for Simple Tasks: Overkill for basic API calls; setup involves learning the protocol.
- Dependency on Ecosystem: Adoption is growing but not universal; limited to compatible LLMs and tools.
- Performance Overhead: Asynchronous tasks or large data transfers can introduce latency.
Use Cases
- Productivity Tools: Integrating Claude Desktop with Google Calendar MCP server for natural language scheduling (e.g., "Book a meeting Thursday at 3 PM").
- Database Querying: Using MongoDB MCP server with Cursor AI to query databases via English, e.g., "Fetch recent sales data."
- Code Automation: GitHub MCP server in Continue IDE for AI-assisted code reviews or commits.
- Enterprise Data Access: Azure MCP server for secure cloud data retrieval in business AI apps.
- Custom Workflows: Building servers for internal tools, like a TODO manager for team collaboration.
Key Differences Between MCP and A2A
- Focus: MCP is for agent-tool/data integration (top-down, centralized); A2A is for agent-agent collaboration (peer-to-peer, decentralized).
- Communication: MCP uses REST/JSON for tool calls; A2A adds JSON-RPC, SSE, and dynamic discovery.
- Complementary: MCP equips agents with tools; A2A lets them collaborate—often used together (e.g., agents in A2A using MCP servers).