Python MCP server quickstart: build and connect one locally

Build a small Python MCP server with the mcp SDK, run it over stdio with uv, and connect it to an MCP client configuration file.

Updated · Markdown version

A Python MCP server is a script that declares tools with the mcp SDK and serves them to a client over stdio. You can build a working one in a single file, run it with uv, and register it in a client's MCP configuration as of 2026-11-21.

Steps

  1. Install uv using the installer from the quickstart. On macOS or Linux, run curl -LsSf https://astral.sh/uv/install.sh | sh. On Windows, run powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex". Restart the terminal so uv is on the path.
  2. Create the project and install the SDK. Run uv init weather, then cd weather, uv venv, and uv add "mcp[cli]". The quoted extra pulls in the mcp command-line tool. Install plain mcp if you do not need that tool.
  3. Create weather.py in the project folder and import the server class with from mcp.server import MCPServer. Create the instance with mcp = MCPServer("weather").
  4. Declare each tool as an async def decorated with @mcp.tool(). Give every parameter a type hint, and write a docstring. The SDK turns the hints and the docstring into the tool's input schema and description, so the docstring is what the model reads when it decides whether to call the tool.
  5. Raise an error for failures you want the client to see. The quickstart raises ToolError from mcp.server.mcpserver.exceptions when an upstream request fails, instead of returning an empty string that looks like a valid answer.
  6. End the file with the entry point guard: if __name__ == "__main__": followed by mcp.run(transport="stdio").
  7. Open the client configuration file. For Claude's local app, the quickstart names claude_desktop_config.json. Add an entry under mcpServers, using the absolute path to the project folder:
{
  "mcpServers": {
    "weather": {
      "command": "uv",
      "args": ["--directory", "C:\\ABSOLUTE\\PATH\\TO\\weather", "run", "weather.py"]
    }
  }
}

On Windows, use double backslashes or forward slashes in the path. If the client cannot find uv, put the full output of where uv (Windows) or which uv (macOS or Linux) in the command field.

  1. Restart the client so it reads the configuration again.

Check that it works

Run uv run mcp dev weather.py from the project folder. The mcp CLI starts a development session for the file, which is a quicker check than waiting for a full client to launch the process. Then restart the client and confirm the server appears in its tool list. Call one tool with a known input. If the server appears but no tools show, the decorator is missing on the function, or the function is defined after the if __name__ block.

Pitfalls

  • Logging to stdout breaks the stdio stream. Over stdio, the protocol messages travel on stdout. A stray print() writes text the client cannot parse. Use the logging module, which the quickstart writes to stderr, and remove debug prints before connecting a client.
  • Relative paths in the config. The client starts the process from its own working folder, not yours. Always use the absolute project path with --directory, and test with uv run weather.py from another folder to confirm the path resolves.
  • Missing type hints produce weak tool schemas. A parameter without a hint gives the model little to work with. Add str, int, float, or dict[str, Any] hints, and check the schema the dev session shows.
  • Mismatched SDK versions. The quickstart requires SDK 2.0.0 or higher, and the README's import paths can differ from older tutorials you find online. If from mcp.server import MCPServer fails, check the installed version with uv pip show mcp.
  • Ambiguous Python version. The quickstart says 3.10 or higher, while the README says 3.11+. Pin the interpreter with uv and use 3.11 or later if you want to match the README.

When to use a skill instead

A one-file server is a good first exercise, but a production server needs decisions about tool granularity, output size, error shape, and whether a tool or a resource fits the data. The mcp-server-builder skill covers those design choices, including JSON Schema inputs, bounded outputs, and stdio versus HTTP transport. The python-practices skill reviews the Python code itself, so typing, structure, and modern idioms get the same check as the MCP design. Use the skills once the quickstart works and you are deciding what the server should expose. Ask a general prompt only for the first working file.

FAQ

How do I create an MCP server in Python? Install the SDK with uv add "mcp[cli]", create an MCPServer instance, decorate functions with @mcp.tool(), and end the file with mcp.run(transport="stdio"). Then register the file under mcpServers in your client's configuration.

What is an MCP server? An MCP server is a process that exposes capabilities to a client through the Model Context Protocol. The quickstart describes three kinds: resources, which are readable data; tools, which are functions the model can call with user approval; and prompts, which are reusable templates.

Do I need the cli extra? No. The [cli] extra adds the mcp command, which provides the development session used in the check step. The server runs without it, but you lose that quick test path.

Platform Score is our assessment under a published method. It is not a safety guarantee.

Next steps

  1. Debug an MCP Server with MCP Inspector Read next.
  2. MCP Server Examples: Learning From a Reference Server Read next.
  3. All guides by topic Browse everything.

Sources

This page is not offered to search engines yet: The content file does not mark this page indexable yet.