How to Intermediate · 3 min read

MCP protocol specification overview

Quick answer
The MCP (Model Context Protocol) is a standardized protocol by Anthropic for connecting AI agents to external tools and resources via a structured message exchange. It uses JSON-based messages over stdio or SSE transport, enabling seamless integration of AI models with APIs, databases, or custom tools.

PREREQUISITES

  • Python 3.8+
  • pip install mcp
  • Anthropic API key (free tier works)
  • Basic knowledge of JSON and Python

Setup

Install the official mcp Python package and set your ANTHROPIC_API_KEY environment variable to authenticate with the MCP server.

bash
pip install mcp

Step by step

This example demonstrates a simple MCP server using the mcp Python SDK that listens on stdio and responds to a basic request.

python
import os
from mcp.server import Server
from mcp.server.stdio import stdio_server

# Define a simple handler function
async def handler(request):
    # Echo back the received request content
    return {"response": f"Received: {request.get('content', '')}"}

# Create MCP server instance with the handler
server = Server(handler=handler)

# Run the server using stdio transport
stdio_server(server)

Common variations

  • Use SSE transport instead of stdio for web-based integration.
  • Implement multiple handlers to connect different tools or APIs.
  • Integrate with Anthropic models by forwarding requests to claude-3-5-sonnet-20241022.
python
import os
import asyncio
from mcp.server import Server
from mcp.server.sse import sse_server

async def handler(request):
    # Example: call Anthropic Claude model here
    from openai import OpenAI
    client = OpenAI(api_key=os.environ["ANTHROPIC_API_KEY"])
    response = client.chat.completions.create(
        model="claude-3-5-sonnet-20241022",
        system="You are an MCP tool responder.",
        messages=[{"role": "user", "content": request.get('content', '')}]
    )
    return {"response": response.choices[0].message.content}

server = Server(handler=handler)

# Run MCP server with SSE transport
asyncio.run(sse_server(server, host="0.0.0.0", port=8080))

Troubleshooting

  • If the server does not start, verify your Python version is 3.8 or higher.
  • Check that ANTHROPIC_API_KEY is set correctly in your environment.
  • For stdio transport, ensure your environment supports asynchronous stdio operations.
  • Use logging to debug message flow and handler responses.

Key Takeaways

  • Use the official mcp Python SDK to implement MCP servers easily with async handlers.
  • MCP supports stdio and SSE transports for flexible AI agent integration.
  • Forward MCP requests to Anthropic models like claude-3-5-sonnet-20241022 for powerful tool responses.
Verified 2026-04 · claude-3-5-sonnet-20241022
Verify ↗