Skip to main content
Chapter 6 Multi-Agent Architectures

Model Context Protocol (MCP)

20 min read Lesson 23 / 28

Model Context Protocol (MCP)

The Model Context Protocol (MCP) is an open standard developed by Anthropic for connecting AI models to data sources and tools through a standardized interface. Think of it as USB-C for AI integrations — one protocol, many compatible tools.

Why MCP?

Without MCP, every AI integration requires custom code:

  • Connect Claude to database → write custom tool
  • Connect Claude to GitHub → write custom tool
  • Connect Claude to Slack → write custom tool

With MCP, you write one MCP server per service; any MCP-compatible AI client (Claude, Claude Code, etc.) can connect to it automatically.

MCP Architecture

┌─────────────┐     MCP Protocol     ┌──────────────────┐
│  MCP Client │ ◄──────────────────► │   MCP Server     │
│  (Claude)   │                      │  (Your Tools)    │
└─────────────┘                      └──────────────────┘
                                             │
                                    ┌────────┴─────────┐
                                    │  Resources       │
                                    │  Tools           │
                                    │  Prompts         │
                                    └──────────────────┘

Building an MCP Server in Python

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("My Knowledge Base")

@mcp.resource("docs://knowledge-base")
def get_knowledge_base() -> str:
    """Expose the knowledge base as a readable resource."""
    return "Contents of the knowledge base..."

@mcp.tool()
def search_documents(query: str, limit: int = 5) -> list[dict]:
    """Search documents and return relevant chunks."""
    # In production, call your vector database here
    return [
        {"title": "Doc 1", "content": f"Result for '{query}' ...", "score": 0.95},
    ]

@mcp.tool()
def create_document(title: str, content: str) -> dict:
    """Create a new document in the knowledge base."""
    # In production, save to database
    return {"id": "new_doc_123", "title": title, "status": "created"}

@mcp.prompt()
def research_prompt(topic: str) -> str:
    """Generate a research prompt for a given topic."""
    return f"Research the following topic thoroughly and provide a structured summary: {topic}"

if __name__ == "__main__":
    mcp.run(transport="stdio")

MCP Server in TypeScript

import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
  CallToolRequestSchema,
  ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";

const server = new Server(
  { name: "analytics-server", version: "1.0.0" },
  { capabilities: { tools: {} } }
);

server.setRequestHandler(ListToolsRequestSchema, async () => ({
  tools: [
    {
      name: "get_metrics",
      description: "Retrieve application metrics for a time range",
      inputSchema: {
        type: "object",
        properties: {
          metric: { type: "string" },
          from: { type: "string", description: "ISO 8601 start date" },
          to: { type: "string", description: "ISO 8601 end date" },
        },
        required: ["metric", "from", "to"],
      },
    },
  ],
}));

server.setRequestHandler(CallToolRequestSchema, async (request) => {
  if (request.params.name === "get_metrics") {
    // Fetch from your analytics system
    return { content: [{ type: "text", text: JSON.stringify({ value: 42 }) }] };
  }
  throw new Error("Unknown tool");
});

const transport = new StdioServerTransport();
await server.connect(transport);

MCP turns your integrations into reusable infrastructure — build once, connect to any compatible AI client.