I Built My First MCP Server in Python (SDK v2) and Connected It to Claude

2026-11-30来源 dev.toEnglish
I Built My First MCP Server in Python (SDK v2) and Connected It to Claude

I kept hearing about MCP (Model Context Protocol), so today I stopped reading and built one. In one evening I went from "what is MCP?" to Claude reading and saving my notes through a server I wrote in Python.

Here's what I built, what broke, and what I learned.

What is MCP?

MCP is an open standard for connecting AI apps to tools and data. Think of it as USB-C for AI apps: you write a server once, and any MCP-compatible app (Claude Desktop, Cursor, VS Code) can use it.

An MCP server can expose three things:

  • Tools: functions the AI can call
  • Resources: read-only data the app can load
  • Prompts: reusable prompt templates

What I built

A small notes server backed by SQLite with:

  • 6 tools: add_note, update_note, search_notes, list_notes, delete_note, count_notes_by_tag
  • 2 resources: notes://all and notes://{title}
  • 2 prompts: summarize_notes and article_outline

Setup

uv init mcp-notes cd mcp-notes uv add "mcp[cli]" 

Note: the Python SDK is now on v2. Most tutorials still show FastMCP, but v2 uses MCPServer.

A tool in a few lines

from mcp.server import MCPServer mcp = MCPServer("Notes") @mcp.tool() def count_notes_by_tag() -> dict[str, int]: """Count notes per tag. Each note has exactly one tag, so the counts add up to the total number of notes.""" with get_db() as conn: rows = conn.execute( "SELECT tag, COUNT(*) AS total FROM notes GROUP BY tag" ).fetchall() return {r["tag"]: r["total"] for r in rows} 

If you know FastAPI, this feels familiar: type hints become the input schema, and the docstring becomes the description the AI reads.

Resources are just as simple:

@mcp.resource("notes://{title}") def get_note(title: str) -> str: """Read a single note by its title.""" ... 

Testing with MCP Inspector

uv run mcp dev server.py 

This is where I hit my first bug. The connection kept failing. The console said:

error: unexpected argument '--directory C:...\mcp-notes run server.py'

I had typed all the arguments into one field, so uv received them as one long string. The fix was to add them as 4 separate arguments: --directory, the path, run, server.py. After that, it connected instantly.

MCP Inspector showing the notes server connected

Connecting to Claude Desktop

In Claude Desktop: Settings → Desktop app → Edit config (under Local MCP servers), then add:

{ "mcpServers": { "notes": { "command": "uv", "args": ["--directory", "C:\\path\\to\\mcp-notes", "run", "server.py"] } } } 

Then fully quit Claude from the system tray and reopen it. Closing the window isn't enough, and that cost me a few minutes.

Claude Desktop settings showing the notes MCP server running

Then I asked Claude: "How many notes do I have per tag?" and it called my count_notes_by_tag tool on its own. 🤯

Claude calling the count_notes_by_tag tool and answering

How it works under the hood

  1. Claude Desktop starts server.py and does a handshake.
  2. It asks tools/list, and the server returns each tool's name, docstring and schema.
  3. Claude decides which tool to call and sends tools/call with the arguments.
  4. My Python function runs, and the result goes back to Claude.

All of this is plain JSON-RPC 2.0 over stdin/stdout. You can watch these messages live in the Inspector's Protocol panel.

The lesson I didn't expect: docstrings matter

My first docstring just said "Count how many notes exist for each tag." Claude guessed that a note could have multiple tags, so it said it couldn't tell me the total.

I added one line: "Each note has exactly one tag." I asked again, and it answered "3 notes in total" straight away.

In MCP, your docstrings are the AI's only instructions. Write them for the AI that will read them.

What I learned

  • MCP is simpler than it sounds: decorators + type hints + docstrings
  • Use the Inspector to test before connecting to an AI app
  • Arguments must be separate items, not one string
  • Fully restart Claude Desktop after editing the config
  • Clear docstrings lead to correct tool use

What's next

I'm planning to switch from SQLite to PostgreSQL and wrap a RAG search as an MCP tool.

📂 Full code: https://github.com/kajaljainkj/mcp-notes

Have you built an MCP server yet? What tool would you give your AI first? Let me know in the comments 👇

原文:https://www.a8q9.cn/en/article/i-built-my-first-mcp-server-in-python-sdk-v2-and-connected-it-to-claude-163921.html

相关文章