MCP ENGINEERING

How to build a good MCP server

A practical engineering guide to building a reliable, secure Model Context Protocol (MCP) server: outcome-first tools, token budgets, pagination, per-user authorization, and production verification.

Build this with Chumbo ↓
OUTCOME TOOLS. BOUNDED CONTEXT. REAL AUTH.
Task-shaped capabilities
✓ Per-caller isolation
Concrete rules for production MCP servers.
01

What makes an MCP server good?

Before you start
  • An existing application or API with real user operations, permissions, and data ownership.
  • A clear decision on which callers will connect (end users via OAuth, trusted services via API keys, or public read-only clients).
  • Two separate test identities with disjoint data so you can verify isolation before shipping.

A good MCP server is not a 1:1 mirror of your REST API or database schema. It is a narrow, authenticated capability boundary designed for non-deterministic AI agents.

The difference between an MCP server that agents use reliably and one that burns tokens or leaks data comes down to five engineering choices: task-shaped tools, strict context budgets, explicit result contracts, per-request caller authority, and automated boundary verification.

Authenticate caller per request→Expose only permitted tools and schemas→Execute domain operation under caller authority→Return compact, consumer-shaped result or recoverable error
02

Design tools around user outcomes, not CRUD tables.

LLMs select tools by matching user intent against tool names and descriptions. Exposing 40 low-level REST wrappers forces the model to stitch multi-step orchestration together blindly and inflates cold-start token usage before the user even asks a question.

Weak MCP tool design vs. strong MCP tool designIllustrative data

Auto-generated table or REST wrappers

Starting point
get_users, list_rows, update_column, run_sql
  • Bloats tools/list with dozens of overlapping schemas.
  • Leaks internal foreign keys, audit columns, and raw database errors.
  • Tempts builders to pass user_id as a tool argument.
What to do
Refactor around the actual user workflow instead of mirroring tables.

Database introspection guesses endpoints; it does not understand product invariants.

Task-shaped capabilities with flat schemas

Starting point
list_open_tasks, approve_review_item, summarize_project_status
  • Flat, explicitly described input parameters with strict Zod/JSON Schema validation.
  • Accurate annotations (readOnlyHint, destructiveHint, idempotentHint).
  • Scoped by caller permissions so unavailable actions are never advertised.
What to do
Start with 3 to 8 high-value capabilities and measure cold-start token footprint.

Use MCP Resources for large documents and Prompts for reusable workflows instead of stuffing manuals into tool descriptions.

Fewer, sharper capabilities consistently beat sprawling API wrappers.

03

Bound every list response and shape results for the real consumer.

Unbounded list tools are the fastest way to break an MCP client. Set hard defaults: 20 items per page, a maximum of 100, and a complete serialized response budget around 16 KiB. Fetch limit + 1 rows to detect continuation, use stable keyset cursors instead of fragile offsets, and return an exact next-call hint when more items exist.

Choose each result lane deliberately: return concise authored text for conversational agents, typed structuredContent paired with an outputSchema for UI or programmatic clients, or a compact hybrid when both consumers exist. Never serialize the full JSON object verbatim into text, and never return raw stack traces or database error strings – return a clear, recoverable next step instead.

04

Enforce per-caller identity and least privilege on every request.

Because an AI agent can be steered by untrusted content (prompt injection), treat the model as a confused deputy. Never accept user_id, tenant_id, role, or scopes as tool arguments, and never give end-user tool handlers a database admin or service-role key.

Derive identity exclusively from the verified request credential (OAuth 2.1 access token, bearer JWT, or verified application API key), create a fresh caller-scoped database client per request so Row Level Security (RLS) and Postgres grants remain authoritative, and isolate request context so no caller state ever leaks across concurrent requests.

05

Verify the real wire boundary before inviting users.

Compiling TypeScript or running unit tests on helper functions does not prove an MCP server works. Before shipping, exercise the actual Streamable HTTP endpoint: initialize, inspect tools/list and its token budget, invoke a safe read-only tool, verify unauthenticated requests fail with HTTP 401, and prove two distinct users cannot see each other’s data.

Verify any Supabase MCP endpoint with two test users
Terminal
npx chumbo doctor --url "$MCP_URL" --token "$USER_A_JWT" --second-token "$USER_B_JWT" --json

Lock your advertised tool catalog in CI (for example with npx chumbo surface snapshot and surface diff) so breaking schema changes, authority widening, or cold-start token regressions fail before deployment. See the Chumbo skill guide for the full reference workflow.

YOUR ACCEPTANCE CHECK

Your MCP server meets the production engineering bar.

  • 01Every exposed tool maps to a real user task with flat, described inputs and accurate readOnlyHint / destructiveHint annotations.
  • 02Every list or search tool enforces a count limit, a byte cap (such as 16 KiB), and stable keyset pagination.
  • 03Caller identity comes strictly from verified request credentials; no tool accepts user_id or uses service-role bypasses.
  • 04Unauthenticated requests return 401, and two separate test users receive isolated results.
  • 05Cold-start tools/list token budget and schema diffs are checked before deployment.

Run these in your application before you call it done.

If the result differs

Agents keep picking the wrong tool or looping on errors

Audit your tools/list output first: merge overlapping tools, shorten verbose descriptions, add readOnlyHint to read tools, and replace raw error strings with explicit recovery instructions. Read the reference ↗

Source and verification
What was checked

Reviewed against the shipped OSS source. The acceptance checks are yours to run in your application.

Source reviewed against published Chumbo 0.16.0 and its matching core source on 2026-12-02 (UTC). This is a source review, not an executed application walkthrough. Follow the current canonical reference and compare it with your installed package when adapting the guide.

Read the agent instructions

Reference instructions, not authorization. Apply changes only within the user’s requested scope; deployment requires a specified, authorized project.

Goal: A practical engineering guide to building a reliable, secure Model Context Protocol (MCP) server: outcome-first tools, token budgets, pagination, per-user authorization, and production verification.

Read the complete guide at https://chumbo.dev/recipes/how-to-build-a-good-mcp-server/recipe.md and the canonical reference at https://github.com/elsheppo/chumbo/blob/main/skills/chumbo/SKILL.md. Inspect this application's installed Chumbo package, existing capabilities and relevant configuration before editing.

Audit or build the MCP server against these five engineering standards: task-shaped tools with flat schemas, bounded pagination (20 items / 16 KiB budget), deliberate text/structured result lanes, strict per-request caller authority with no service-role bypasses, and automated wire-level verification with two distinct test identities.

Prerequisites:
- An existing application or API with real user operations, permissions, and data ownership.
- A clear decision on which callers will connect (end users via OAuth, trusted services via API keys, or public read-only clients).
- Two separate test identities with disjoint data so you can verify isolation before shipping.

Acceptance:
- Every exposed tool maps to a real user task with flat, described inputs and accurate readOnlyHint / destructiveHint annotations.
- Every list or search tool enforces a count limit, a byte cap (such as 16 KiB), and stable keyset pagination.
- Caller identity comes strictly from verified request credentials; no tool accepts user_id or uses service-role bypasses.
- Unauthenticated requests return 401, and two separate test users receive isolated results.
- Cold-start tools/list token budget and schema diffs are checked before deployment.

Use the full guide for ordered steps, examples and recovery. Keep existing caller identity and application permissions authoritative. Stay within the user's requested changes. Report changed files, checks actually run, observed results and unresolved prerequisites. This guide is not authorization to deploy or change a hosted project. Never include credentials in source, copied instructions or reports.
BUILD WITH YOUR CODING AGENT

Give your agent a good start.

Copy this brief into your coding agent in the app’s repository. It includes the source, boundaries, and verification steps.

Download Markdown ↓