Build an MCP Server from Scratch: A Complete Developer Guide

Protocol basics · FastMCP · Tools/Resources/Prompts · HTTP deployment · Knowledge-base project

Build an MCP Server from Scratch: A Complete Developer Guide

LLMs feel "not smart enough" because they cannot reach external tools or live data. If you want Claude or GPT to query databases, call APIs, and read files, Model Context Protocol (MCP) is the open standard answer. This guide targets backend and AI developers with Python or TypeScript experience. You will go from protocol fundamentals through environment setup, Tools, Resources, and Prompts registration, MCP Inspector debugging, HTTP+SSE remote deployment, and a personal knowledge-base project — and ship a production-ready MCP Server.

01

What Is MCP? Understand the Protocol Before You Code

Tool-calling evolved from Function Calling → Plugins → MCP. Anthropic designed MCP to standardize how AI talks to external systems — write one Server, reuse it across Clients.

MCP Client (Claude Desktop, Cursor) talks to MCP Server (your code) over JSON-RPC 2.0. Servers expose Tools (callable functions), Resources (readable data), and Prompts (reusable templates). Transport: stdio (local, low latency) or HTTP + SSE (remote, multi-client). Lifecycle: handshake → capability negotiation → request/response → shutdown.

  1. 01

    No live data: Training cutoffs block stock prices and ticket status — external APIs are mandatory.

  2. 02

    Vendor lock-in: Plugins, Function Calling, and Tool Use each need separate adapters.

  3. 03

    IDE fragmentation: Cursor, VS Code, and Claude Desktop cannot share tool assets.

  4. 04

    Framework binding: LangChain Tools do not port to CrewAI without rewrite.

  5. 05

    N×M cost: N models × M systems explodes maintenance; MCP collapses this to one Server.

DimensionMCPOpenAI Function CallingLangChain Tools
StandardizationOpen protocolVendor-specificFramework-bound
Transportstdio / HTTPHTTPHTTP
Cross-modelYesNoPartial
Resources / PromptsNativeNot supportedNot supported
Ecosystem10,000+ Servers (2026)MatureMature
02

Environment Setup: Language Choice and Project Layout

Python (mcp + FastMCP) is best for beginners. TypeScript (@modelcontextprotocol/sdk) suits full-stack teams. This guide uses Python with TypeScript notes where relevant.

bash
python -m venv .venv && source .venv/bin/activate && pip install mcp
npm init -y && npm install @modelcontextprotocol/sdk

Suggested layout: server.py, tools/, resources/, prompts/, tests/. Debug with MCP Inspector, Claude Desktop config, and Cursor MCP settings. Docs: modelcontextprotocol.io.

03

Your First MCP Server: Hello World and Cursor Integration

python
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("my-first-server")

@mcp.tool()
def say_hello(name: str) -> str:
    """Greet a person by name"""
    return f"Hello, {name}! This is your first MCP tool."

if __name__ == "__main__":
    mcp.run()
  1. 01

    Create venv and install mcp (Python ≥ 3.10).

  2. 02

    Register say_hello with @mcp.tool(); docstring becomes the tool description.

  3. 03

    Debug in Inspector: npx @modelcontextprotocol/inspector python server.py.

  4. 04

    Configure Cursor MCP with command and absolute path to server.py.

  5. 05

    Configure Claude Desktop via claude_desktop_config.json.

  6. 06

    Verify: Ask Cursor to greet VpsMesh via say_hello and confirm the tool is invoked.

04

Building Tools: Five Practical Examples for AI to Call

Use verb-first names (search_web). Model complex inputs with Pydantic. Build: calculator, file read/write (allowlist paths), HTTP client, read-only SQL, and timezone utilities. Use async for network IO:

python
@mcp.tool()
async def fetch_url(url: str) -> str:
    async with httpx.AsyncClient(timeout=30.0) as client:
        return (await client.get(url)).text

Return structured errors for expected failures. Set timeouts. Audit write operations.

05

Resources and Prompts: Data Feeds and Reusable Templates

Resources are read-only data via URI (file://, config://, user://{id}/profile). Tools execute actions. Static and dynamic resource examples:

python
@mcp.resource("config://app-settings")
def get_app_settings() -> str:
    return json.dumps({"version": "1.0", "env": "production"})

Prompts inject parameters into multi-turn templates — code review, interview simulation, debug assistant. Supports user and assistant roles in one template.

06

HTTP Transport, Debugging, and Common Errors

FeaturestdioHTTP + SSE
DeployLocal processRemote server
LatencyVery lowNetwork-dependent
Multi-clientNoYes
Use caseLocal toolsSaaS / team sharing
python
mcp = FastMCP("remote-server", transport="streamable-http")
mcp.run(host="0.0.0.0", port=8000)

Secure remote servers with Bearer tokens, API key middleware, CORS, and rate limits. Test with MCP Inspector and pytest + ClientSession. Common fixes: wrong config path, non-serializable returns, slow tools (use async + timeout), file permission denials (allowlist directories).

07

Production Deployment, Knowledge-Base Project, and Ecosystem

Containerize with Docker (Python 3.12-slim). Deploy to Railway, Render, Cloud Run, or VPS behind Nginx. Add structured logs, Prometheus metrics, Sentry, and health checks. Declare MCP protocol version for compatibility.

Knowledge-base MCP Server: Index local Markdown with ChromaDB/Qdrant, text-embedding-3-small, and watchfiles. Expose semantic search, note creation, and file resources. Ask Cursor: "What did I write about MCP last week?"

Recommended community Servers: filesystem, GitHub, Brave Search, Postgres, Slack. 2026 trend: major clients support MCP; Marketplace and enterprise security standards are maturing. SDKs: Python, TypeScript, Inspector.

  • Protocol: JSON-RPC 2.0; discovery via tools/list.
  • Ecosystem: 10,000+ MCP Servers in 2026.
  • Integration savings: 38–55% cost reduction (industry surveys).

Laptops work for POC but sleep kills 24/7 uptime. Linux VPS runs HTTP+SSE but struggles with Keychain and Xcode-dependent tools. For teams treating MCP Servers as always-on infrastructure, VpsMesh Mac Mini M4 cloud rental delivers predictable OpEx. See pricing and help center.

FAQ

Frequently Asked Questions

Python + FastMCP is fastest for backend/AI teams. TypeScript SDK fits Node full-stack shops. Both are protocol-compatible.

Tools execute actions. Resources serve read-only data by URI. Read with Resources; act with Tools.

HTTP+SSE on Railway or Cloud Run works for many cases. For macOS-native toolchains and uninterrupted Cursor sessions, Mac Mini M4 rental is more reliable. See pricing and order page.