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
- Install
uvusing the installer from the quickstart. On macOS or Linux, runcurl -LsSf https://astral.sh/uv/install.sh | sh. On Windows, runpowershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex". Restart the terminal souvis on the path. - Create the project and install the SDK. Run
uv init weather, thencd weather,uv venv, anduv add "mcp[cli]". The quoted extra pulls in themcpcommand-line tool. Install plainmcpif you do not need that tool. - Create
weather.pyin the project folder and import the server class withfrom mcp.server import MCPServer. Create the instance withmcp = MCPServer("weather"). - Declare each tool as an
async defdecorated 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. - Raise an error for failures you want the client to see. The quickstart raises
ToolErrorfrommcp.server.mcpserver.exceptionswhen an upstream request fails, instead of returning an empty string that looks like a valid answer. - End the file with the entry point guard:
if __name__ == "__main__":followed bymcp.run(transport="stdio"). - Open the client configuration file. For Claude's local app, the quickstart names
claude_desktop_config.json. Add an entry undermcpServers, 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.
- 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 theloggingmodule, 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 withuv run weather.pyfrom 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, ordict[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 MCPServerfails, check the installed version withuv pip show mcp. - Ambiguous Python version. The quickstart says 3.10 or higher, while the README says 3.11+. Pin the interpreter with
uvand 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.
Related skills in this catalog
Platform Score is our assessment under a published method. It is not a safety guarantee.
Related guides
Sources
- https://modelcontextprotocol.io/docs/develop/build-server (accessed 2026-11-21)
- https://github.com/modelcontextprotocol/python-sdk (accessed 2026-11-21)
This page is not offered to search engines yet: The content file does not mark this page indexable yet.