Build an MCP Server from Scratch: A Complete Developer Guide (2026)
If you want Claude, GPT, or Cursor to query your database, call APIs, or search local notes—but every model needs a different adapter—you are hitting the same N×M integration wall MCP was built to fix. This hands-on guide is for backend and AI developers with Python or TypeScript basics. By the end you will build, debug, and deploy a production-ready MCP Server with Tools, Resources, Prompts, HTTP transport, a knowledge-base project, and a five-step Mac cloud Runbook.
Table of Contents
- 1. Introduction: Why AI Needs External Tools
- 2. What Is MCP? Protocol Before Code
- 3. Environment Setup
- 4. Hello World: Your First MCP Server
- 5. Tools: Five Practical Examples
- 6. Resources: Dynamic Content for AI
- 7. Prompts: Reusable Template Library
- 8. HTTP Transport: Remote MCP Servers
- 9. Debug and Test
- 10. Production Deployment
- 11. Project: Personal Knowledge Base MCP Server
- 12. MCP Ecosystem Outlook
- 13. Hard Facts You Can Cite
- 14. Conclusion
1. Introduction: Why AI Needs External Tools
LLMs are powerful text engines but blind to your live systems: training cutoffs block real-time data, and models cannot execute side effects without Tool Use. You want an assistant that reads your Markdown vault, runs SQL, or hits internal APIs—without rewriting integrations every time you switch from Claude to GPT or from Cursor to another IDE.
Three core pain points before MCP
- Fragmented tool wiring. OpenAI Function Calling, Claude Tool Use, and LangChain Tools each define schemas differently—N models × M tools = N×M adapters.
- No runtime discovery. REST endpoints sit in static docs; AI cannot call
tools/listto learn what is available mid-session. - Local Host dependency. Cursor and Claude Desktop spawn STDIO subprocesses on macOS; a Linux VPS or closed laptop breaks the chain.
Model Context Protocol (MCP) standardizes how Hosts discover and invoke Tools, read Resources, and load Prompt templates over JSON-RPC 2.0. This tutorial walks from zero to a deployable Server—not theory alone. For the protocol-level "why MCP is the HTTP of AI," see our MCP protocol deep dive.
2. What Is MCP? Protocol Before Code
Anthropic open-sourced MCP in November 2024. It sits between AI Clients (Claude Desktop, Cursor, custom apps) and the Server you build—exposing three capability types:
- Tools — callable functions (search, calculate, SQL)
- Resources — addressable read-only data via URI (
file://,config://) - Prompts — parameterized prompt templates the Host can inject
Lifecycle: initialize handshake → capability negotiation → request/response → graceful shutdown. Transport options: STDIO (local subprocess) or HTTP + SSE / streamable HTTP (remote).
MCP vs Function Calling vs LangChain Tools
| Dimension | MCP | OpenAI Function Calling | LangChain Tools |
|---|---|---|---|
| Standardization | Open protocol | Vendor-specific | Framework-bound |
| Transport | STDIO / HTTP | HTTP only | HTTP only |
| Cross-model | Yes | No | Partial |
| Resources / Prompts | Native | Not supported | Not supported |
| Runtime discovery | tools/list | Static schema | Static schema |
3. Environment Setup
Choose your language
Python (recommended for this guide): official SDK mcp with FastMCP decorators. TypeScript: @modelcontextprotocol/sdk for Node-native teams.
Project layout
Debug tooling
- MCP Inspector —
npx @modelcontextprotocol/inspector - Claude Desktop —
claude_desktop_config.json - Cursor —
.cursor/mcp.json
4. Hello World: Your First MCP Server
Run and inspect:
Wire into Cursor (.cursor/mcp.json):
Restart Cursor, open MCP settings, and confirm say_hello appears in the tool list.
5. Tools: Five Practical Examples
Tools are actions. Function signatures plus docstrings become JSON Schema for the model. Use Pydantic models for complex inputs.
Tool 1 — Calculator
Tool 2 — File read/write
Tool 3 — HTTP request (async)
Tool 4 — Database query
Tool 5 — Time and timezone
Best practices: return JSON-serializable types; use structured error strings instead of bare stack traces; set timeouts on network and DB calls; validate paths and SQL to prevent injection.
6. Resources: Dynamic Content for AI
Resources supply data; Tools perform actions. MCP addresses them by URI.
Mime types: text/plain, application/json, or binary for PDFs/images. For a filesystem Server, expose directory listings and file contents as Resources so the model reads context without mutating state through Tools.
7. Prompts: Reusable Template Library
Prompts package multi-turn templates with parameters—useful for code review, incident triage, or interview prep.
Hosts call prompts/list and prompts/get to inject consistent instructions—reducing prompt drift across sessions.
8. HTTP Transport: Remote MCP Servers
| Feature | STDIO | HTTP + SSE / streamable HTTP |
|---|---|---|
| Deployment | Local subprocess | Remote server |
| Latency | Very low | Network-dependent |
| Multi-client | One Host process | Many clients |
| Best for | Cursor, Claude Desktop | Team SaaS, shared tools |
Place Nginx or a cloud load balancer in front for TLS termination. Never expose an unauthenticated MCP HTTP endpoint to the public internet.
9. Debug and Test
MCP Inspector workflow
- Start:
npx @modelcontextprotocol/inspector python server.py - Call
tools/listand verify schemas - Invoke each Tool with edge-case inputs
- Inspect JSON-RPC request/response pairs in the UI
Automated test example
Common errors
| Error | Cause | Fix |
|---|---|---|
| Tool missing in Host | Wrong path in mcp.json | Use absolute paths; restart Host |
| JSON serialize failure | Non-serializable return | Return str or dict |
| Timeout disconnect | Slow sync Tool | Make async; add timeout |
| Permission denied | Path outside allowlist | Configure allowed roots |
10. Production Deployment
Five-step production Runbook
Step 1 — Containerize
Step 2 — Transport and authentication
Enable streamable HTTP; require Bearer Token or API Key on every request; restrict CORS to known Host origins.
Step 3 — Test before ship
Run MCP Inspector plus pytest suite; record tools/call p95 latency baselines.
Step 4 — Observability
Structured JSON logs, Prometheus counters per tool name, Sentry for unhandled exceptions, and a /health endpoint for orchestrators.
Step 5 — Host on durable infrastructure
For STDIO workflows with Cursor, use launchd on a always-on Mac node with CPU and memory limits. For HTTP mode, deploy to Railway, Render, Cloud Run, or behind Nginx on a VPS—with TLS and auth enforced.
Declare MCP protocol version in initialize responses and version Tools carefully to avoid breaking existing Clients.
11. Project: Personal Knowledge Base MCP Server
Goal: Let Cursor answer "What did I write about MCP last week?" by searching local Markdown notes.
Stack
- Vector store: ChromaDB or Qdrant
- Embeddings:
text-embedding-3-smallor local model - File watch:
watchfilesto re-index on save
Core Tools
Expose vault files as Resources (note://{slug}) for read-only preview. In Cursor, ask natural-language questions; the model calls semantic_search and cites snippets from your vault.
12. MCP Ecosystem Outlook
By mid-2026, MCP is supported across Claude Desktop, Cursor, VS Code Copilot extensions, OpenAI Responses API tooling, and a growing marketplace. Reference Servers worth studying:
mcp-server-filesystem— sandboxed file opsmcp-server-github— repos, PRs, issuesmcp-server-brave-search— web searchmcp-server-postgres— SQL over MCPmcp-server-slack— team messaging
Enterprise adoption is driving auth standards (OAuth 2.1, scoped API keys) and audited Server registries. Next steps: read the spec at modelcontextprotocol.io, publish your Server to GitHub, and combine MCP with Agent orchestration frameworks.
13. Hard Facts You Can Cite (2026-06-16)
- Protocol origin: MCP open-sourced by Anthropic in November 2024; built on JSON-RPC 2.0 with STDIO and HTTP transports.
- Official SDKs: Python (
mcp) and TypeScript (@modelcontextprotocol/sdk) maintained atgithub.com/modelcontextprotocol. - Three primitives: Every Server exposes Tools (actions), Resources (read URIs), and Prompts (templates)—discoverable at runtime via
tools/list,resources/list,prompts/list. - Inspector: Official debug UI via
npx @modelcontextprotocol/inspector; supports live JSON-RPC tracing without a Host.
14. Conclusion: From Tutorial to Production-Grade MCP
You now have the full path: protocol mental model, Python SDK setup, five Tools, Resources, Prompts, HTTP mode, Inspector testing, Docker packaging, and a knowledge-base reference project. Running this entirely on a local laptop works for demos—but lid-close sleep kills STDIO sessions, path and permission quirks multiply across teammates, and Linux VPS or Docker-only stacks cannot host native Cursor/Claude Desktop subprocesses or Apple toolchain sidecars without painful workarounds.
For stable, auditable, 7×24 MCP Servers that Cursor and Claude Desktop can reach reliably, deploy on a dedicated VPSMAC M4 Mac cloud node: bare-metal macOS, launchd KeepAlive, SSH in minutes, pin-able Python environments, and room to run HTTP gateways plus STDIO side-by-side. That is the production-grade 2026 path—more controllable than betting on a primary machine staying awake, and far simpler than forcing macOS Host workflows onto Linux VPS.