Labs › local-genai

Build Your Own MCP Server Framework

Build an MCP server from JSON-RPC stdio tools to HTTP resources and prompts to sampling and progress notifications — understanding Anthropic's protocol for connecting LLMs to the real world.

python 3.12 advanced ~10h build · ~3h read ~800 LoC (v3)
v0: stdio Transport + Toolsv1: Resources + Prompts + HTTP Transportv2: Sampling + Progress + Middleware
☕ Java/Spring integration included service client

Why this lab exists

Every time Claude uses a tool — searches the web, reads a file, queries a database — it’s talking to an MCP server. The Model Context Protocol is Anthropic’s open standard for connecting LLMs to external systems, and it’s spreading fast: Claude Desktop, Claude.ai, Cursor, and dozens of third-party clients all speak MCP.

You’ve seen the diagrams: LLM calls tool, tool returns result, LLM generates answer. But until you’ve implemented the JSON-RPC handshake yourself, registered a tool with a schema, watched the tools/list response flow back over a Unix pipe, and then seen Claude actually call your function — you don’t understand what’s happening at the wire level.

This lab builds an MCP server from scratch: from the stdio transport loop (150 lines of Python) up through resources, prompts, SSE streaming, authentication middleware, and progress notifications. You will understand exactly what Claude Desktop does when it spawns an MCP server process, and what a remote MCP server must implement to work over HTTP.

What we’re building

An MCP server framework in Python, implemented across three stages. Each stage adds one layer of the MCP spec and tests it against the real protocol.

We build:

  • JSON-RPC 2.0 message framing over stdin/stdout
  • MCP initialize handshake with capability negotiation
  • Tool registration via @mcp_tool decorator with JSON Schema validation
  • Resource registration via @mcp_resource decorator for data sources
  • Prompt templates via @mcp_prompt with argument substitution
  • HTTP transport via FastAPI with SSE streaming for notifications
  • Progress notifications for long-running tool calls
  • Middleware pipeline (auth, logging) running around every request
  • Sampling support (sampling/createMessage — server asks client’s LLM)
  • A complete example server with 3 tools and 2 resources

We explicitly skip (and why):

  • Full OAuth 2.0 authorization (see “What the Toy Misses” — requires a token server)
  • URI template matching beyond prefix matching (full RFC 6570 templates are complex)
  • Persistent session state across reconnects (requires a session store)
  • Binary blob resources (base64 encoding of large files; straightforward but verbose)
  • Multi-server client routing (client-side concern, not server-side)

Mental model

MCP is a protocol, not a library. The spec defines three primitives the server exposes and one transport the server runs on.

Three server-side primitives:

Tools      — functions the LLM can call (side effects: read/write/compute)
             tools/list   → [{ name, description, inputSchema }]
             tools/call   → { content: [{type, text}], isError }

Resources  — data the LLM can read (no side effects: pure data access)
             resources/list → [{ uri, name, mimeType }]
             resources/read → { contents: [{uri, mimeType, text}] }

Prompts    — reusable prompt templates with named parameters
             prompts/list → [{ name, description, arguments }]
             prompts/get  → { messages: [{role, content}] }

Two transports:

stdio  — client spawns server as subprocess; stdin/stdout are the pipe
         one JSON object per line, framed by newline
         85,000 calls/sec — just readline + json.loads + dict lookup

HTTP   — client connects to server's HTTP endpoint
         POST /messages   → synchronous request/response
         GET  /sse        → server-sent events for notifications
         12,000 calls/sec — FastAPI + loopback overhead

The MCP handshake:

Client → Server:  initialize {protocolVersion, clientInfo}
Server → Client:  {protocolVersion, serverInfo, capabilities}
Client → Server:  notifications/initialized   (notification, no response)
--- server is now ready ---
Client → Server:  tools/list
Server → Client:  {tools: [...]}
Client → Server:  tools/call {name: "read_file", arguments: {path: "/tmp/x"}}
Server → Client:  {content: [{type: "text", text: "..."}], isError: false}

The entire protocol is this simple at the transport layer. The complexity is in the semantics: JSON Schema validation, URI template matching, progress notification timing, capability negotiation. This lab builds each of those layers explicitly.

Prerequisites

  • Python 3.12
  • Familiarity with JSON-RPC 2.0 (a request has jsonrpc, id, method, params; a response has id and either result or error)
  • Basic Python async/await (used in v1 SSE streaming and v2 progress notifications)
  • Optional: read the MCP specification at spec.modelcontextprotocol.io — the lab implements a subset of the 2024-11-05 version

Stage-by-stage build

Stage 0 stdio Transport + Tools ~200 LoC ›
  • Implement JsonRpcRequest / JsonRpcResponse dataclasses with from_dict / to_dict
  • Implement @mcp_tool decorator that registers functions in _TOOL_REGISTRY
  • Implement McpStdioServer.handle() routing initialize, tools/list, tools/call
  • Implement validate_input() checking JSON Schema required fields and types
  • Implement McpStdioServer.run() — the stdio loop (readline → dispatch → print)
  • Write sample tools: read_file, list_files, execute_python (restricted eval)
  • Write 6 tests: handshake, tools/list, tools/call dispatch, unknown tool, schema validation, stdio round-trip

The entire MCP stdio transport is a while True loop. Every line in is one JSON-RPC message. Every line out is one JSON-RPC response. The OS pipe handles buffering and flow control.

Why one message per line?

MCP uses newline-delimited JSON (NDJSON) for the stdio transport. This is the simplest possible framing: json.loads(line) on each line. No length prefix, no delimiter scanning. The tradeoff is that no message can contain a literal newline — but JSON strings encode newlines as \n, so this is always true for valid JSON.

while True:
    line = sys.stdin.readline()
    if not line:
        break          # EOF — client closed the pipe
    message = json.loads(line.strip())
    response = self.handle(message)
    if response is not None:   # notifications have no response
        sys.stdout.write(response.to_json() + "\n")
        sys.stdout.flush()

The flush() call is critical. Without it, Python’s stdout buffering holds the response in memory until the buffer fills. Claude Desktop would wait forever for the first response. Always flush after writing.

The tool registry:

The @mcp_tool decorator stores (ToolDefinition, callable) in a module-level dict keyed by tool name. This is a simple registry pattern — no framework magic.

_TOOL_REGISTRY: dict[str, tuple[ToolDefinition, Callable]] = {}

def mcp_tool(name, description, input_schema):
    def decorator(fn):
        _TOOL_REGISTRY[name] = (ToolDefinition(name, description, input_schema), fn)
        return fn
    return decorator

When tools/call arrives, the handler does a dict lookup by name, validates the arguments against the inputSchema, and calls the function with **arguments. The return value is serialized to JSON and wrapped in {content: [{type: "text", text: ...}], isError: false}.

JSON Schema validation:

MCP tools declare their inputs as JSON Schema. The validate_input() function does a minimal subset: checks that all required fields are present, and that field types match the type constraint. A production implementation would use the jsonschema library for full Draft 7 validation.

The execute_python tool:

The restricted eval pattern is instructive. Python’s eval() can execute arbitrary code, which is dangerous. We constrain it by passing a custom __builtins__ dict that only includes safe functions (math operations, type conversions). ast.literal_eval handles pure data literals (lists, dicts, numbers) safely before falling back to restricted eval.

This is a toy sandbox — a production implementation would use a subprocess with seccomp or a WebAssembly runtime. The “What the Toy Misses” section covers this.

v0 limitation:

v0 only supports tools. If a Claude Desktop session asks for resources (to see what files are available) or prompts (to use a code review template), the server returns METHOD_NOT_FOUND. v1 adds these.

Stage 1 Resources + Prompts + HTTP Transport ~500 LoC ›
  • Implement @mcp_resource decorator + _RESOURCE_REGISTRY
  • Implement resources/list and resources/read handlers (URI prefix matching)
  • Implement @mcp_prompt decorator + _PROMPT_REGISTRY with string.Template substitution
  • Implement prompts/list and prompts/get handlers
  • Implement create_app() — FastAPI app with POST /messages and GET /sse
  • Write sample resources: file:///current-directory, system://info
  • Write sample prompts: code_review(language, code), summarize(text, max_length)
  • Write 5 tests: resource read, URI matching, prompt substitution, HTTP POST, SSE stream

Tools vs Resources — the key distinction:

Tools are for actions: read_file reads a file (but could also write one). Resources are for data access with no side effects. The MCP spec distinguishes them because clients can cache resources (they’re idempotent reads) but must not cache tool calls (they may have side effects).

In practice, this means:

  • read_file(path) as a tool: Claude calls it when it decides to read a file. It can also be used to write.
  • file:///path as a resource: The client can enumerate it, pre-fetch it, show it in a UI. It’s declared upfront, not called on demand.

The resource URI scheme is flexible. Our two sample resources use file:// (filesystem convention) and system:// (custom scheme). A database resource might use db://customers/42. The client treats the URI as an opaque string — the server defines what it means.

String.Template for prompt substitution:

Python’s string.Template uses $variable substitution, which is safe for user-supplied templates because it doesn’t execute code. The alternative, f-strings, would require eval. We use safe_substitute which leaves unmapped variables as $variable rather than raising KeyError for missing optional arguments.

from string import Template
template = "Review this $language code:\n\n$code"
rendered = Template(template).safe_substitute({"language": "Python", "code": "..."})

SSE transport:

Server-Sent Events (SSE) is the MCP spec’s choice for HTTP server-to-client streaming. The client opens a long-lived GET /sse connection; the server sends events as data: {...}\n\n chunks whenever it has something to say (progress updates, list-changed notifications).

async def sse_generator():
    queue = asyncio.Queue()
    self._sse_queues.append(queue)
    try:
        yield f"data: {json.dumps({'type': 'connected'})}\n\n"
        while True:
            message = await asyncio.wait_for(queue.get(), timeout=30.0)
            yield f"data: {message}\n\n"
    finally:
        self._sse_queues.remove(queue)

Each SSE client gets its own asyncio.Queue. Broadcasting a notification means putting it on every queue. The generator yields from its queue, streaming events to that client. When the client disconnects, the finally block removes the queue.

The 30-second timeout sends a keepalive comment (: keepalive\n\n) to prevent the connection from being closed by proxies and load balancers that kill idle connections.

HTTP vs stdio dispatch:

The protocol layer is identical for HTTP and stdio. The same McpHttpServer.handle(message: dict) method processes the message. The transport layer is what differs:

  • stdio: json.loads(sys.stdin.readline()) → handle() → sys.stdout.write(response.to_json())
  • HTTP: await request.json() → handle() → JSONResponse(response.to_dict())

This separation is deliberate. You could add a Unix socket transport or a WebSocket transport without touching the protocol layer.

Stage 2 Sampling + Progress + Middleware ~800 LoC ›
  • Implement McpMiddleware ABC with pre() and post() hooks
  • Implement AuthMiddleware(token) that checks Authorization: Bearer in pre()
  • Implement LoggingMiddleware that times every request and appends to self.log
  • Implement ProgressReporter.report(progress, total) that sends notifications/progress
  • Implement McpServer._run_middleware_pre/_run_middleware_post chain
  • Implement roots/list handler returning configured filesystem paths
  • Implement sampling/createMessage handler with MockSamplingHandler
  • Write 4 tests: progress notifications sent, roots returns paths, auth rejects, logging captures

Sampling — the bidirectional trick:

Every other MCP primitive flows client-to-server: the client calls a tool, reads a resource, gets a prompt. Sampling flows server-to-client: the MCP server asks the client’s LLM to generate text.

Why would a tool need to call an LLM? Agentic tools. Imagine an MCP tool that summarizes a large codebase. It might:

  1. Read all Python files (tool calls to read_file)
  2. For each batch of files, ask the LLM to summarize them (sampling)
  3. Ask the LLM to synthesize the summaries into a final report (sampling)
  4. Return the final report to the client

The LLM is both the orchestrator (calling tools) and a service (being called by tools). Sampling makes MCP truly bidirectional.

Our MockSamplingHandler echoes the last user message as the “generated” response. This lets the test suite verify that the sampling code path runs correctly without a real LLM endpoint.

Progress notifications — the async flow:

When a tool call starts, the client can include _meta.progressToken in the request params. If the server sees this token, it should send notifications/progress messages while the tool is running:

Server → Client: {"method": "notifications/progress",
                   "params": {"progressToken": "abc", "progress": 1, "total": 3}}
Server → Client: {"method": "notifications/progress",
                   "params": {"progressToken": "abc", "progress": 2, "total": 3}}
...
Server → Client: {"method": "tools/call", "result": {...}}

The client matches progressToken to the pending tool call and updates a progress bar. The server sends progress via the same SSE channel (for HTTP transport) or on stdout (for stdio transport).

In our implementation, call_tool_with_progress is async because it needs to await the SSE queue puts between progress steps. The synchronous handle() method can’t do this — it’s for the request/response cycle. Progress requires a separate async code path.

Middleware chain:

The middleware pattern wraps the handler in pre/post hooks. The chain runs pre-hooks in order and post-hooks in reverse (like a stack of decorators):

pre:  auth → logging → handler → logging → auth
      ─────── (inward) ──────────── (outward) ───

AuthMiddleware.pre() returns a JsonRpcResponse immediately if the token is invalid — this short-circuits the chain without calling the handler. LoggingMiddleware.pre() records the start time and returns None (continue). LoggingMiddleware.post() computes elapsed time and appends to self.log.

Roots — declared scope, enforced on tool calls:

roots/list returns a list of filesystem paths the server has declared access to. The protocol itself doesn’t enforce anything here: the client uses the list to build UI hints (“this server can access /home/user/projects”) and to avoid asking for files the server obviously can’t read. This lab’s server goes one step further and checks tools/call arguments against its roots. Any argument the tool’s input schema marks as a path ("format": "path" or "x-mcp-path": true, on a string or on the items of an array), plus any argument with a common path name (path, file, filename, filepath, directory, dir, …), is resolved (so ../ and symlinks can’t escape) and rejected if it falls outside every root. An earlier version checked only path and directory, so a tool taking filename slipped past.

The correct mental model: roots are like a server’s “business card” — it declares its scope, but the client still enforces what it asks for based on user consent.

Run it yourself

Run it yourself labs/mcp-server ↗
Prereqs: Python 3.12+pip or uv
  1. 1
    Clone and install dependencies
    git clone https://github.com/pushkarkumar-dev/system-design-labs
    cd system-design-labs/labs/mcp-server
    pip install -e '.[test]'
  2. 2
    Run tests (all 15 tests, no LLM needed)
    python -m pytest tests/test_mcp.py -v
  3. 3
    Try the stdio server manually
    echo '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","clientInfo":{"name":"test","version":"0.0.1"}}}' | python -m src.example_server
  4. 4
    Start the HTTP server
    python -m src.example_server --http --port 8080 --debug
  5. 5
    Initialize and list tools via HTTP
    curl -s -X POST http://localhost:8080/messages \
      -H 'Content-Type: application/json' \
      -d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","clientInfo":{"name":"curl","version":"0.0.1"}}}' | python -m json.tool
    
    curl -s -X POST http://localhost:8080/messages \
      -H 'Content-Type: application/json' \
      -d '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' | python -m json.tool
  6. 6
    Call a tool via HTTP
    curl -s -X POST http://localhost:8080/messages \
      -H 'Content-Type: application/json' \
      -d '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"execute_python","arguments":{"code":"sum(range(100))"}}}' | python -m json.tool
Expected output
{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {
      "content": [
          {
              "type": "text",
              "text": "4950"
          }
      ],
      "isError": false
  }
}

Benchmarks

Benchmark results on M2 MacBook Pro, 16GB RAM
Metric Result Notes
Tool dispatch (stdio, JSON parse + call) 85,000 calls/sec estimated — stdin readline + json.loads + dispatch
Tool dispatch (HTTP POST, loopback) 12,000 calls/sec estimated — FastAPI HTTP overhead on loopback
JSON-RPC parse (1KB message) 1,800,000 parses/sec estimated — json.loads on 1KB string
Resource read (1MB file) 1,200 reads/sec estimated — file I/O + text content assembly

The 7x throughput gap between stdio and HTTP is the defining architectural fact of MCP deployment. Claude Desktop runs every MCP server as a local subprocess using stdio transport — not because HTTP doesn’t work, but because stdio is faster, requires no port management, and the subprocess lifecycle is tied to the client process. Remote MCP servers (shared team tools, third-party integrations) use HTTP because they need to be accessible from multiple clients — and accept the latency cost.

Stress tests

Schema validation throughput: Run tools/call with intentionally wrong argument types at high frequency. Each call must validate against the JSON Schema before invoking the function. Our validate_input() is O(fields) per call — at 85,000 calls/sec, schema validation adds less than 1 microsecond per call. The bottleneck is json.loads, not validation.

SSE fan-out: Connect 100 clients to GET /sse. Broadcast one notification. Each client’s queue receives the notification; each SSE generator yields it. At 100 clients, the fan-out is 100 queue puts and 100 async yields. This is fast (microseconds) but blocks the asyncio event loop for each yield. A production server would use a pub/sub broker (Redis, NATS) for fan-out beyond 1,000 clients.

Middleware chain depth: Add 10 logging middleware instances to the chain. Each request runs 10 pre-hooks and 10 post-hooks in addition to the handler. At 85,000 stdio calls/sec, 20 extra function calls add approximately 2 microseconds per request. Middleware overhead is negligible — the bottleneck is always json.loads and json.dumps.

Large tool output: A tool that returns 1MB of text (e.g., reading a large log file). The response JSON is over 1MB. json.dumps of 1MB takes about 10ms. sys.stdout.write of 1MB blocks until the pipe buffer drains. For large outputs, MCP has no built-in streaming — the entire result arrives as one JSON object. Production servers either truncate large outputs or expose them as resources (which can be streamed separately).

What the toy misses

What the toy misses Honest gaps vs. production
  • MCP authorization (OAuth 2.0) The MCP spec defines OAuth 2.0 authorization for HTTP transport — the MCP server is the resource server, the client obtains a bearer token from a separate authorization server. Our auth middleware only validates pre-shared tokens. Implementing OAuth 2.0 properly requires a token server, PKCE flow for public clients, and token refresh handling.
  • Capability negotiation versioning The MCP spec is versioned (currently 2024-11-05). Clients and servers negotiate which optional capabilities are supported. Our implementation advertises all capabilities regardless of what the client requested. A correct implementation checks the client's capability list and only enables what both sides support.
  • Transport reconnection HTTP/SSE connections drop due to network issues, proxy timeouts, and server restarts. Production MCP clients implement exponential backoff reconnection with session state preservation — the server must maintain in-flight tool call state across reconnects using a session ID.
  • Binary resource content Our resource read returns text. MCP also supports binary blob resources (images, audio files, PDFs) encoded as base64 in the `blob` field. Large binary resources require chunked streaming to avoid loading the entire file into memory before encoding.
  • Multi-server routing A production MCP client connects to multiple servers simultaneously (filesystem server, database server, web search server). The client must route tool calls to the correct server by matching tool names and merge capability lists without name collisions. Our server is always standalone.
  • Schema evolution When you add a new optional parameter to a tool's JSON Schema, existing clients must not break. MCP relies on additionalProperties for forward compatibility, but schema evolution in practice requires maintaining backward compatibility across versions and communicating schema changes to clients.

Wiring it into a Java/Spring service

☕

Wiring it into a Java/Spring service

service client spring-ai
Maven dependencies
org.springframework.ai:spring-ai-mcp:1.0.0-M6
org.springframework.boot:spring-boot-starter-web:3.3.0

Part A — McpAutoConfiguration: spawning the Python server

McpAutoConfiguration creates a McpSyncClient bean configured with Spring AI’s StdioClientTransport. When the Spring context starts, this bean spawns our Python MCP server as a subprocess and runs the MCP initialize handshake automatically.

@Bean(destroyMethod = "close")
public McpSyncClient mcpSyncClient(McpProperties props) {
    ServerParameters serverParams = ServerParameters.builder(props.getServerCommand())
            .args(props.getServerArgs().split("\\s+"))
            .build();

    StdioClientTransport transport = new StdioClientTransport(serverParams);

    McpSyncClient client = McpSyncClient.builder()
            .transport(transport)
            .build();

    client.initialize();  // runs MCP handshake synchronously
    return client;
}

The destroyMethod = "close" annotation ensures the subprocess is killed cleanly when the Spring context closes. Without it, the Python process would become a zombie.

Part B — McpClient: the high-level wrapper

McpClient wraps McpSyncClient with convenience methods. The raw Spring AI API uses McpSchema.* types for everything; our wrapper extracts the text content and returns plain strings — the 80% case for most Spring services.

public String callTool(String toolName, Map<String, Object> arguments) {
    McpSchema.CallToolResult result = delegate.callTool(
            new McpSchema.CallToolRequest(toolName, arguments)
    );
    return result.content().stream()
            .filter(c -> c instanceof McpSchema.TextContent)
            .map(c -> ((McpSchema.TextContent) c).text())
            .findFirst().orElse("");
}

Part C — McpDemoApplication: the full lifecycle

McpDemoApplication is a CommandLineRunner that demonstrates the full MCP session:

// 1. List available tools (Python server responds)
List<McpSchema.Tool> tools = client.listTools();

// 2. Call a tool
String result = client.callTool("execute_python", Map.of("code", "sum(range(100))"));
// result: "4950"

// 3. Read a resource
String sysInfo = client.readResource("system://info");
// sysInfo: JSON with platform, python_version, cwd, timestamp

// 4. Get a rendered prompt
String prompt = client.getPrompt("code_review", Map.of(
        "language", "Java",
        "code", "public int add(int a, int b) { return a + b; }"
));
// prompt: "Please review the following Java code for bugs..."

application.yml

mcp:
  transport: stdio
  server-command: python3
  server-args: "-m src.example_server"
  working-dir: "labs/mcp-server"

Run it

# Terminal 1: no separate server needed — Spring AI spawns it
cd labs/mcp-server/java-integration
mvn spring-boot:run

Expected output:

=== MCP Server Demo ===

Available tools (3):
  - read_file: Read the text contents of a file.
  - list_files: List files in a directory.
  - execute_python: Execute a Python expression and return the result.

Calling execute_python(code="len('hello world')"):
  Result: 11

Calling list_files(directory="."):
  file: pyproject.toml
  dir: src
  dir: tests
  ... (more entries)

Reading resource: system://info
  "platform": "macOS-15.x-arm64-...",
  "python_version": "3.12.x",

Rendered prompt 'code_review':
  Please review the following Python code for bugs...

Done. MCP client closed.

The key architectural insight: the Python subprocess lifecycle is tied to the Spring application lifecycle. When mvn spring-boot:run exits, the Python server is killed. No separate process management needed.

What surprised me

Where to go deeper

  • MCP specification: The authoritative source for the 2024-11-05 protocol — spec.modelcontextprotocol.io
  • MCP Python SDK: Anthropic’s official Python MCP SDK with full spec compliance — github.com/modelcontextprotocol/python-sdk
  • MCP TypeScript SDK: The reference implementation, used in Claude Desktop — github.com/modelcontextprotocol/typescript-sdk
  • Spring AI MCP: Spring AI’s MCP client and server support — docs.spring.io/spring-ai/reference/api/mcp
  • JSON-RPC 2.0 spec: The base protocol that MCP is built on — jsonrpc.org/specification
  • Claude Desktop MCP config: How to add your server to Claude Desktop — modelcontextprotocol.io/quickstart/user
  • NDJSON: Newline-delimited JSON — the stdio framing format — ndjson.org
  • Previous in Local GenAI: Agent Framework (lab agent-framework) — builds a ReAct agent loop that uses tools; MCP is the standardized protocol for those tools
  • Previous in Local GenAI: RAG System (lab rag) — MCP resources are the protocol-level abstraction for exposing retrieval results to the LLM
  • Next in Local GenAI: LLM Inference (lab llm-inference) — running the LLM that calls your MCP server tools locally
  • System design companion: How would you design an MCP server that handles 10,000 concurrent HTTP clients? The SSE fan-out bottleneck is the first thing to solve — a pub/sub broker (Redis Streams, NATS) replaces the in-process queue list.
  • System design companion: How should an MCP server handle tool calls that take more than 30 seconds? Progress notifications are the protocol answer; but the HTTP/SSE connection keepalive timeout means you need proxy configuration too (nginx proxy_read_timeout, CloudFront idle timeout).