Skip to content
Starfield Labs

Articles

How to build an MCP server in Python: a 20-minute tutorial

One file, two tools, connected to your AI assistant in about 20 minutes.

By Starfield LabsPublished

Cover art with two connectors plugging together beside the headline How to build an MCP server in Python.

An MCP server is a small program that gives an AI assistant new abilities, such as reading your notes or calling an API. You can build one in Python in about 20 minutes with the official MCP Python SDK: write a function, add a decorator, test it in the MCP Inspector, then connect it to an assistant like Claude. This tutorial builds a server that lets your assistant search a folder of notes.

What is MCP, in plain words?

MCP, the Model Context Protocol, is a standard way to plug tools and data into AI apps. Think of it as a universal adapter. You build a tool once as an MCP server, and any app that speaks MCP (Claude, Claude Code, Cursor, VS Code and others) can use it.

An MCP server can offer three kinds of things:

Kind What it is Example
Tool An action the AI can take Search notes, create a task, send a message
Resource Data the AI can read A file, a record, a page
Prompt A reusable message template "Summarise this note for a manager"

What do you need?

  • Python 3.11 or newer
  • uv, a fast Python package manager (or pip)
  • Node.js, so the MCP Inspector can run
  • An MCP client to connect to at the end, such as Claude Desktop or Claude Code

Step 1: Set up the project

uv init notes-server
cd notes-server
uv add "mcp[cli]"
mkdir notes
echo "Buy GPUs for the demo. Ask Sam about the venue." > notes/todo.txt
echo "Pitch: problem, live demo, one clever detail, next steps." > notes/pitch.txt

The [cli] extra adds the mcp command you'll use to test and install the server.

Step 2: Write the server

Create server.py:

from pathlib import Path

from mcp.server import MCPServer

NOTES = Path(__file__).parent / "notes"

mcp = MCPServer("Notes")


@mcp.tool()
def list_notes() -> list[str]:
    """List the names of all notes."""
    return sorted(p.name for p in NOTES.glob("*.txt"))


@mcp.tool()
def search_notes(query: str) -> list[str]:
    """Return the names of notes that contain the query text."""
    q = query.lower()
    return [p.name for p in NOTES.glob("*.txt") if q in p.read_text().lower()]


@mcp.resource("note://{name}")
def read_note(name: str) -> str:
    """Read one note by its file name."""
    return (NOTES / name).read_text()


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

Three things to notice:

  1. The decorator makes the tool. @mcp.tool() turns a normal Python function into a tool the AI can call.
  2. The docstring and type hints matter. The SDK uses the function's name, docstring and types to tell the AI what the tool does and what to pass in. Write them as if for a new colleague.
  3. mcp.run() starts the server. With no arguments, it talks to the client over standard input and output ("stdio"), which is what desktop apps expect.

Step 3: Test it in the MCP Inspector

uv run mcp dev server.py

This opens the MCP Inspector, a small web app for testing servers. Open the link it prints, then:

  1. Go to Tools, run list_notes, and check you see pitch.txt and todo.txt.
  2. Run search_notes with the query demo.
  3. Go to Resources and read note://todo.txt.

If a tool fails here, it will fail in your assistant too. Fix it before moving on.

Step 4: Connect it to an AI assistant

Claude Desktop: run Claude Desktop once, then:

uv run mcp install server.py

This adds the server to Claude Desktop's config file. Quit Claude Desktop completely and reopen it, then ask: "Which of my notes mention the demo?"

Claude Code:

claude mcp add notes -- uv run --with "mcp[cli]" mcp run /absolute/path/to/server.py

Cursor or VS Code: add the same command to .cursor/mcp.json (under mcpServers) or .vscode/mcp.json (under servers, with "type": "stdio").

Always use the absolute path to server.py. Clients start your server from their own folder, so relative paths break.

What are the common mistakes?

  • Printing to the console. A stdio server must not print() anything; stray output breaks the connection. Log to a file instead.
  • Old tutorials. Many guides written for version 1 of the SDK use FastMCP. The current version (v2) uses MCPServer, imported from mcp.server.
  • Vague docstrings. If the AI picks the wrong tool, improve the tool names and descriptions first.
  • Tools that do too much. Small tools with clear inputs work better than one big tool.
  • No guardrails. If a tool writes, deletes or sends something, ask the user to confirm first.

What can you build next?

  • A server that reads your calendar or task list
  • A server that wraps a public API, such as weather or transport times
  • A server that runs safe, read-only queries on a database
  • A remote server other people can use, run with the streamable-http transport (uv run mcp run server.py --transport streamable-http)

MCP servers are a great hackathon project: small, useful and easy to demo. Starfield Labs runs one too: connect your coding agent to Starfield Labs over MCP and it can find events and enroll you.

Where to go next

Turn your server into a full agent with how to build an AI agent, or see AI agent examples for ideas. Sign up at starfieldlabs.ai to hear first about our events. Before you trust a server, test it with a client.

Sources (opened 20 November 2026)

Keep reading

All articles