Module 3 · Communication & protocols
Model Context Protocol (MCP) and Agent-to-Agent (A2A)
5 min read
Every hand-written integration — a knowledge base, a CRM, a ticketing tool — is a connector you must maintain for each of your agents. Two open protocols standardize these exchanges: MCP, to connect an agent to tools and data, and A2A, to let agents talk to each other. They play for agents the role HTTP played for the web.
MCP: an agent ↔ tools protocol
The Model Context Protocol defines how an application (the server) exposes capabilities to an AI client:
| Primitive | Role | Example |
|---|---|---|
| Tools | Actions the model can request | search_knowledge, create_ticket |
| Resources | Data the application can read | a file, a record |
| Prompts | Reusable prompt templates | "summarize a ticket" |
An MCP server written once can be used by any compatible client: your in-house agent, an IDE, a desktop assistant. Integrations become reusable components rather than agent-specific code.
Transports
- stdio: the client launches the server as a subprocess and talks over standard input/output. Ideal locally.
- Streamable HTTP: the server is a web service. Required for a shared or remote server.
Writing an MCP server
The official Python SDK (the mcp package, version 2) provides the MCPServer class. Here is Atlas's server, atlas/mcp_server.py:
from mcp.server import MCPServer
from atlas import knowledge
mcp = MCPServer("atlas-knowledge")
@mcp.tool()
def search_knowledge(query: str, k: int = 3) -> list[dict]:
"""Searches the Atlas knowledge base. Returns at most k documents (id, title, excerpt)."""
return knowledge.search(query, k)
@mcp.tool()
def read_document(doc_id: str) -> str:
"""Returns the full text of a document from an id obtained with search_knowledge."""
return knowledge.read(doc_id)
if __name__ == "__main__":
mcp.run() # stdio transport by default
Each tool's input schema is inferred from the type annotations, and the description comes from the docstring. The rules of lesson 1.4 apply in full: the docstring is the description the model will read.
Versions. Version 2 of the SDK renamed several things (
FastMCPbecameMCPServer, and the old import path was removed). If you follow an older tutorial, check your installed version withpip show mcp. The code in this course is tested withmcp2.3.
To inspect your server interactively: mcp dev atlas/mcp_server.py.
Consuming an MCP server
The v2 client directly accepts stdio launch parameters, an HTTP URL, or even an in-memory server instance (handy for tests):
import sys
from mcp import Client, StdioServerParameters
server = StdioServerParameters(command=sys.executable, args=["-m", "atlas.mcp_server"])
async with Client(server) as mcp_client:
listing = await mcp_client.list_tools()
for tool in listing.tools:
print(f"- {tool.name}: {tool.description}")
result = await mcp_client.call_tool("search_knowledge", {"query": "handoff", "k": 2})
print(result.structured_content)
Actual output (the companion corpus is in French):
- search_knowledge : Recherche dans la base documentaire d'Atlas. Renvoie au plus k documents (id, titre, extrait).
- read_document : Renvoie le texte intégral d'un document à partir d'un identifiant obtenu avec search_knowledge.
{'result': [{'id': 'doc-handoff', 'title': 'Handoffs entre agents', 'excerpt': "Un handoff transfère…"}]}
Plugging MCP into Claude
The Anthropic SDK converts each MCP tool into a tool usable by the Tool Runner:
from anthropic import AsyncAnthropic
from anthropic.lib.tools.mcp import async_mcp_tool
runner = AsyncAnthropic().beta.messages.tool_runner(
model=MODEL,
max_tokens=16000,
tools=[async_mcp_tool(t, mcp_client.session) for t in listing.tools],
messages=[{"role": "user", "content": "Explain MCP and cite your sources."}],
)
async for message in runner:
...
The agent discovers tools at runtime: add a tool to the server, and every agent connecting to it gets it without any code change. (Install the matching extra: pip install "anthropic[mcp]".)
For a remote, public MCP server, the Claude API can also connect to it directly server-side, without your code relaying the calls — see the API's MCP connector documentation.
MCP and security
An MCP server is third-party code running with your permissions, and its tool descriptions enter your model's context. Treat it as a dependency:
- only install trusted servers, and pin their versions;
- apply least privilege (a read-only server doesn't need write tokens);
- treat MCP tool results as untrusted content (module 4).
A2A: an agent ↔ agent protocol
MCP connects an agent to tools. The Agent2Agent (A2A) protocol connects agents to each other, including agents built by different organizations with different frameworks.
Its key concepts:
- The Agent Card: a description document published by each agent (identity, skills, endpoint, authentication methods). It's the discovery mechanism.
- The task: the unit of work, with a life cycle (submitted, working, input required, completed, failed…). Suited to long-running work.
- Messages and artifacts: the exchanges during the task, and the deliverables produced.
| MCP | A2A | |
|---|---|---|
| Connects | An agent to tools and data | Agents to each other |
| The other side is | A passive service | An autonomous, opaque agent |
| Granularity | Function call | End-to-end task |
| Use case | Internal integrations | Cross-team or cross-company collaboration |
When is A2A relevant? When the other agent is a black box you don't control — a vendor's, a partner's, another team's agent. Within your own system, a LangGraph graph (or function calls) remains simpler, faster and easier to debug. The A2A specification evolves quickly: check its current version before implementing.
Key takeaways
- MCP standardizes the agent ↔ tools/data connection: a server written once, reusable by any compatible client.
- With the Python SDK v2:
MCPServerand@mcp.tool()on the server side,Clienton the client side;async_mcp_toolbridges to Anthropic's Tool Runner.- An MCP server is a dependency: trust, pinned version, least privilege, results treated as untrusted.
- A2A standardizes dialogue between autonomous agents; it's mostly justified across organizations.
Hands-on
- Run
python examples/03_mcp_client.pyand watch the tools being discovered. - Add a
list_documents()tool to the MCP server without declaring it on the client; rerun the client and check it is discovered automatically. - Write a test using
Client(mcp)(in-memory server) to check thatread_document("unknown")returns an actionable error message.