Issue 00 · Autumn 2026日本語 · RSS
AgentKoji

How to Build Your Own MCP Server

How to build an MCP server with the official Python SDK: a minimal example, tested by hand over JSON-RPC, then registered in Claude Code and confirmed connected.

TL;DR

  • With the official Python SDK (mcp 2.x), a function decorated with @mcp.tool() on an MCPServer plus mcp.run() is a working stdio MCP server.
  • In 2.x FastMCP was renamed MCPServer; raise ToolError for failures the model should read, and never write to stdout.
  • Register it in Claude Code with claude mcp add using full paths, and confirm Connected with claude mcp get.

We build a minimal MCP server with the official Python SDK, talk to it by hand, and register it in Claude Code until it shows “Connected.” Local setup: Python 3.13.5, the mcp SDK 2.3.0, Claude Code 2.1.301 (2026-10-16; re-checked 2026-11-02: 2.3.0 is still the latest on PyPI and the server below behaves the same). How MCP works and design principles (with a TypeScript SDK v2 example) are in MCP Servers 101 and Five Design Rules; for choosing existing servers, see How to Choose MCP Servers.

Check the SDK version first

pip install mcp gave 2.3.0, where the common from mcp.server.fastmcp import FastMCP fails:

No module named 'mcp.server.fastmcp'. This is mcp 2.x, where FastMCP was renamed to MCPServer (from mcp.server.mcpserver import MCPServer) ...

The migration guide confirms the rename and suggests pinning <2 to keep v1 code (https://py.sdk.modelcontextprotocol.io/v2/migration/, checked 2026-10-16).

A minimal server

from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.exceptions import ToolError

mcp = MCPServer("dummy-notes", version="0.1.0")

NOTES = {"hello": "This is a dummy note."}

@mcp.tool()
def read_note(name: str) -> str:
    """Read one dummy note by name. Use when the user asks for a saved note."""
    if name not in NOTES:
        raise ToolError(f"unknown note: {name}")
    return NOTES[name]

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

Type hints become the input schema, the docstring the description; run() defaults to stdio. Set version explicitly: unversioned 2.x servers report an empty string (same source).

Talk to it by hand

We piped JSON-RPC messages into stdin: initialize, list tools, call with an existing and a missing note.

1 {"protocolVersion": "2025-11-25", "serverInfo": {"name": "dummy-notes", "version": "0.1.0"}, ...}
2 {"tools": [{"description": "Read one dummy note by name. ...", "inputSchema": {"properties": {"name": {"title": "Name", "type": "string"}}, "required": ["name"], ...
3 {"content": [{"text": "This is a dummy note.", "type": "text"}], "isError": false, ...}
4 {"content": [{"text": "Error executing tool read_note: unknown note: nope", "type": "text"}], "isError": true}

Response 1 uses 2025-11-25 because our client asked for it; per the migration guide the newest revision the 2.x SDK speaks is 2026-07-28 (checked 2026-11-02).

Return errors the model can read

We first raised a plain ValueError; the response was just Error executing tool read_note, with the reason missing. With ToolError the reason comes through. The guide says to raise ToolError or return is_error=True for failures the calling LLM should see (same source).

Don’t write to stdout

The official build guide says stdio servers must never write to stdout; use logging, which writes to stderr (https://modelcontextprotocol.io/docs/develop/build-server, checked 2026-10-16). In our 2.3.0 test a print() didn’t break responses (2.x keeps protocol streams on private descriptors, per the migration guide), but follow the guide anyway.

Register and confirm

$ claude mcp add dummy-notes -- /tmp/mymcp/.venv/bin/python /tmp/mymcp/server.py
Added stdio MCP server dummy-notes with command: /tmp/mymcp/.venv/bin/python /tmp/mymcp/server.py to local config

The docs say to check with claude mcp get <name> (https://code.claude.com/docs/en/mcp, checked 2026-10-16):

dummy-notes:
  Scope: Local config (private to you in this project)
  Status: ✔ Connected
  Type: stdio

No login was needed to get this far.

Use full paths

Registered as python server.py instead:

bad-path: python server.py - ✘ Failed to connect — ENOENT: Executable not found in $PATH: "python"

A command that works in your shell may not resolve where Claude Code launches it. Use full paths to the venv Python and the server file.

Summary

FAQ

Why does from mcp.server.fastmcp import FastMCP fail?
In mcp 2.x FastMCP was renamed MCPServer. Import it from mcp.server.mcpserver, or pin mcp<2 to keep v1 code.
How do I get a tool error's reason to the model?
Raise ToolError from mcp.server.mcpserver.exceptions. In our 2.3.0 test a plain ValueError dropped the reason.
What about TypeScript?
MCP Servers 101 and Five Design Rules has a TypeScript SDK v2 (@modelcontextprotocol/server) example.