Tech With Galvan
Back to Articles
AIMar 15, 2026 • 6 min read

Build Your First MCP Server in Python (Model Context Protocol)

Build a working Model Context Protocol server in Python from scratch — expose tools and resources to Claude, Cursor, and any MCP client, with real code and tests.

Galvan
Galvan

Founder & Creator

Introduction

Model Context Protocol (MCP) is the open standard that lets AI assistants like Claude, Cursor, and Windsurf talk to your code. Instead of hard-coding tool calls into every app, you build one MCP server that exposes tools and data — and any MCP-compatible client can use them.

Think of MCP as “USB-C for AI”: one connector, many devices. In this tutorial you’ll build a real MCP server in Python that exposes a weather tool and a notes resource, then connect it to Claude Desktop.

What you’ll build

  • An MCP server using the official mcp Python SDK
  • One tool (get_weather) the model can call
  • One resource (notes://today) the model can read
  • A working connection to Claude Desktop

Prerequisites

  • Python 3.10+
  • uv (recommended) or pip
  • Claude Desktop (free) for testing

Setup

mkdir mcp-weather && cd mcp-weather
uv init
uv add "mcp[cli]"

Step 1: Create the server

Create server.py:

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("weather-notes")

@mcp.tool()
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    # In real life, call an API here. We'll fake it.
    fake = {"London": "12°C, cloudy", "Tokyo": "22°C, sunny", "Delhi": "34°C, hazy"}
    return fake.get(city, f"No data for {city}")

@mcp.resource("notes://today")
def today_notes() -> str:
    """Today's scratchpad notes."""
    return "Ship the MCP post. Review PR #42. Buy coffee."

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

That’s it. FastMCP handles the JSON-RPC protocol, tool discovery, and argument validation from your type hints.

Step 2: Test it locally

uv run mcp dev server.py

This opens the MCP Inspector in your browser. You can list tools, call get_weather("Tokyo"), and read notes://today — all without an LLM. This is the fastest way to debug an MCP server.

Step 3: Connect to Claude Desktop

Find your Claude Desktop config file:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • Windows: %APPDATA%\Claude\claude_desktop_config.json

Add your server:

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

Restart Claude Desktop. You’ll see a 🔌 icon — click it to confirm weather-notes is connected. Now ask: “What’s the weather in Tokyo, and what are my notes for today?” Claude will call both tools.

How it works under the hood

  1. Discovery: the client asks the server “what tools do you have?” via tools/list. The server replies with names, descriptions, and JSON schemas derived from your type hints.
  2. Invocation: when the model decides to use a tool, the client sends tools/call with arguments. The server runs your function and returns text.
  3. Resources: resources/list + resources/read let the model pull context (files, DB rows, notes) without a tool call.

This is the same loop as the AI agent tutorial — LLM + tools + loop — but standardized so any client works.

Adding a real API (weather example)

Replace the fake dict with a real call:

import httpx

@mcp.tool()
def get_weather(city: str) -> str:
    """Get the current weather for a city."""
    r = httpx.get(f"https://wttr.in/{city}?format=3", timeout=10)
    return r.text

That’s a 3-line upgrade from mock to real data. The interface never changes — clients just see a better tool.

Common mistakes

  • Relative paths in the config — Claude Desktop runs from a different working directory. Always use absolute paths.
  • Forgetting if __name__ == "__main__" — the mcp run entrypoint needs it.
  • Blocking I/O in tools — use httpx (async-friendly) or wrap sync calls; a slow tool freezes the whole conversation.
  • No descriptions — the docstring becomes the tool description the model reads. Write them like you’re explaining to a junior dev.

Key Concepts

  • MCP = USB-C for AI — one server, many clients
  • Tools = actions the model can take (functions with type hints)
  • Resources = read-only context the model can pull
  • FastMCP = the Python SDK that hides the protocol
  • Inspector = local test harness before connecting a real client

What to Try Next

  • Add a prompt (@mcp.prompt()) — reusable prompt templates clients can invoke.
  • Wrap an existing Streamlit app’s logic as MCP tools so Claude can drive it.
  • Deploy the server over HTTP with mcp.run(transport="streamable-http") and host it on Cloudflare Pages.
  • Combine with Pydantic AI to build an agent that uses your MCP server.

FAQ

MCP vs LangChain tools — what’s the difference?

LangChain tools live inside your Python app. MCP tools are exposed over a protocol, so any client (Claude, Cursor, your own agent) can discover and call them. Use MCP when you want reuse across tools; use LangChain when you’re building one app.

Can I use MCP with local Ollama models?

Yes — clients like Continue.dev and Cline support MCP with local models. The server doesn’t care which model calls it.

Is MCP secure?

The server runs on your machine with your permissions. Only expose tools you’d be comfortable an LLM calling. Never expose shell execution or file deletion without confirmation flows.