First FastMCP Server

A complete server with a tool, resource, and prompt in ~30 lines.

Setup

# New project
mkdir my-mcp-server && cd my-mcp-server
uv init
uv add fastmcp

Server Code

# server.py
from fastmcp import FastMCP, Context

mcp = FastMCP("MyServer")


# --- Tools ---

@mcp.tool
def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b


@mcp.tool
async def search(query: str, limit: int = 5, context: Context = None) -> list[dict]:
    """Search for items. Uses context for logging."""
    if context:
        await context.send_message("info", f"Searching for: {query}")
    # Your search logic here
    return [{"title": f"Result {i}", "query": query} for i in range(limit)]


# --- Resources ---

@mcp.resource("config://settings")
def get_settings() -> dict:
    """Current server settings."""
    return {"version": "1.0", "debug": True}


@mcp.resource("data://items/{item_id}")
def get_item(item_id: str) -> dict:
    """Get an item by ID."""
    return {"id": item_id, "name": f"Item {item_id}"}


# --- Prompts ---

@mcp.prompt()
def analyze(data: str, focus: str = "general") -> str:
    """Generate an analysis prompt."""
    return f"Analyze the following data with focus on {focus}:\n\n{data}"


if __name__ == "__main__":
    mcp.run()  # defaults to stdio transport

Run It

# stdio (for testing with MCP clients)
uv run python server.py

# HTTP with SSE (for remote access)
uv run python -c "
from server import mcp
mcp.run(transport='sse', host='0.0.0.0', port=8000)
"

# Or use the CLI
uv run fastmcp run server.py --transport sse --port 8000

Connect to Claude Desktop

Add to Claude Desktop’s MCP config (~/Library/Application Support/Claude/claude_desktop_config.json):

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

Connect to Claude Code

Add to .mcp.json in your project root:

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

PyPI Distribution

For distributing as a package (like largefile, diffchunk):

# pyproject.toml
[project.scripts]
my-server = "my_mcp_server.main:cli_main"
# main.py
from fastmcp import FastMCP

mcp = FastMCP("MyServer")

# ... register tools ...

def cli_main():
    mcp.run()

Then users install with uvx --from my-server my-server or pip install my-server.