Skip to main content
The official MCP Python SDK provides high-level and low-level APIs for building MCP servers and clients in Python. Its FastMCP API can turn typed Python functions into tools, resources, and prompts with generated schemas.
The Python SDK has multiple major API generations. The examples below use the stable v1 interface from the mcp package. Check the official repository and migration guide for the version you install.

What the Python SDK provides

You can use it to:
  • Create MCP servers with FastMCP
  • Build lower-level servers for custom protocol behavior
  • Define typed tools, resources, and prompts
  • Build MCP clients with ClientSession
  • Use stdio and Streamable HTTP
  • Access logging, progress, sampling, and elicitation through request context
  • Implement authentication for remote servers

Install the SDK

The official repository recommends uv for project management:
With pip:
Pin a compatible major version in production so an SDK upgrade does not unexpectedly change your API.

Build a minimal MCP server

Create server.py:
Python type hints define the input and output types. The docstring helps the model understand when to call the tool. Run it with:

Add a resource

FastMCP uses URI templates for dynamic resources:
The client can read greeting://Ada to receive Hello, Ada!.

Add a prompt

Prompts are intended for explicit user selection. Keep their arguments and expected output clear.

Build a minimal MCP client

Create client.py:
The client starts the server, initializes the session, discovers tools, and calls add.

Use context in a tool

A handler can request a FastMCP context object to access request-scoped features.
Keep model-visible progress and logs free of secrets.

Organize a production server

A maintainable project can separate MCP definitions from business services:
Handlers should be thin adapters. Put API calls, database access, and business rules in testable service modules.

Choose a transport

For a remote server, follow the SDK version’s current Streamable HTTP and authentication guidance. Do not use legacy SSE for a new deployment unless compatibility requires it. Read MCP transports and Remote MCP servers.

Production safeguards

  • Validate model-provided and client-provided input.
  • Authenticate and authorize each protected operation.
  • Use timeouts for HTTP and database calls.
  • Apply concurrency and rate limits.
  • Avoid blocking the event loop in async handlers.
  • Return safe, actionable errors.
  • Keep credentials out of prompts, results, and logs.
  • Add health checks, metrics, tracing, and audit events.
  • Pin and regularly update dependencies.

Test with MCP Inspector

Run:
Test tool discovery, invalid input, errors, resources, prompts, cancellations, and the chosen transport.

When to use 0mcp instead

If your server primarily exposes an existing API, 0mcp can remove the need to write and operate the MCP translation layer yourself. 0mcp supports:
  • OpenAPI 3.x and Swagger 2.0 import
  • Direct REST API-to-MCP conversion without an OpenAPI document
  • GraphQL API-to-MCP conversion
  • Hosted MCP endpoints, tools, resources, and prompts
  • Playground testing, versioning, rollback, analytics, and logs
Use the Python SDK when you need custom Python workflows or protocol behavior. Use 0mcp when your main goal is to expose an existing API through a managed MCP server.

When to use Python

Python is a strong choice when the server needs:
  • Data processing
  • Machine-learning libraries
  • Existing Python business logic
  • Rapid service development
  • Async HTTP or database integrations
Compare language tradeoffs in MCP SDK comparison.

Key takeaway

The Python SDK and FastMCP make it concise to define MCP capabilities, but production reliability still depends on validation, authorization, testing, and operational design.