paulserban.eu

Writing Edition

Paul Serban

Writing, snippets & book notes

Cheat sheet - Post

Architecting Production-Grade MCP Servers

← Back to AI Engineering: Architecting an MCP Server the Right Way

Building a Model Context Protocol (MCP) server correctly means applying rigorous API design discipline to a consumer that reasons at runtime rather than reading documentation once.

Overview

Key Concepts

Tools vs. Resources vs. Prompts

Transports: stdio vs. Streamable HTTP

Tool Granularity Trade-off

Schema Validation and Structured Error Handling

// Structured failure the model can act on, instead of an unhandled throw
return {
  content: [{ type: "text", text: `Order lookup failed: ${(err as Error).message}` }],
  isError: true,
};

Security as a First-Class Concern

Observability and Versioning

Trade-offs / Caveats

Example in Practice

A get_order_status tool (shown in both TypeScript with the official @modelcontextprotocol/sdk + Zod, and Python with FastMCP + Pydantic) illustrates the pattern end to end: a single narrow tool, a regex-constrained orderId argument validated before it reaches the backend, a description stating exactly when the tool should be used, and failures returned as isError: true text rather than thrown exceptions. Both implementations deliberately avoid a generic "call the orders API" passthrough - narrow, tightly-schemed tools trade some flexibility for validation, auditability, and a much smaller blast radius when something goes wrong.

Related topics