← 返回首页
MCP Server
Skip to content
MCP Server
Introduction
modelcontextprotocol/python-sdk

MCP Python SDK

Looking for the upcoming v2?

See the v2 development documentation.

The Model Context Protocol (MCP) allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction.

This Python SDK implements the full MCP specification, making it easy to:

  • Build MCP servers that expose resources, prompts, and tools
  • Create MCP clients that can connect to any MCP server
  • Use standard transports like stdio, SSE, and Streamable HTTP

If you want to read more about the specification, please visit the MCP documentation.

Quick Example

Here's a simple MCP server that exposes a tool, resource, and prompt:

server.pyfrom mcp.server.fastmcp import FastMCP mcp = FastMCP("Test Server", json_response=True) @mcp.tool() def add(a: int, b: int) -> int: """Add two numbers""" return a + b @mcp.resource("greeting://{name}") def get_greeting(name: str) -> str: """Get a personalized greeting""" return f"Hello, {name}!" @mcp.prompt() def greet_user(name: str, style: str = "friendly") -> str: """Generate a greeting prompt""" return f"Write a {style} greeting for someone named {name}." if __name__ == "__main__": mcp.run(transport="streamable-http")

Run the server:

uv run --with mcp server.py

Then open the MCP Inspector and connect to http://localhost:8000/mcp:

npx -y @modelcontextprotocol/inspector

Getting Started

  1. Install the MCP SDK
  2. Build servers - tools, resources, prompts, transports, ASGI mounting
  3. Write clients - connect to servers, use tools/resources/prompts
  4. Explore authorization - add security to your servers
  5. Use low-level APIs - for advanced customization
  6. Protocol features - MCP primitives, server capabilities

API Reference

Full API documentation is available in the API Reference.