MCP Python SDK - MCP Python SDK
https://py.sdk.modelcontextprotocol.io/ • 54 KB fetched Open original page
MCP Python SDK - MCP Python SDK
Skip to content
MCP Python SDK
MCP Python SDK
*
en - English
*
de - Deutsch
*
es - español
*
fr - français
*
hi - हिन्दी
*
ja - 日本語
*
ko - 한국어
*
pt - português (Brasil)
*
ru - русский язык
*
tr - Türkçe
*
uk - українська мова
*
zh - 简体中文
*
zh-hant - 繁體中文
Search
modelcontextprotocol/python-sdk
MCP Python SDK
modelcontextprotocol/python-sdk
*
MCP Python SDK
MCP Python SDK
On this page
*
Requirements
*
Installation
*
Example
*
Create it
*
Run it
*
Try it
*
Recap
*
Where to go next
*
What's new in v2
*
Get started
Get started
*
Installation
*
First steps
*
Connect to a real host
*
Testing
*
Servers
Servers
*
Tools
*
Structured Output
*
Resources
*
URI templates
*
Prompts
*
Completions
*
Images, audio & icons
*
Handling errors
*
Inside your handler
Inside your handler
*
The Context
*
Dependencies
*
Lifespan
*
Elicitation
*
Multi-round-trip requests
*
Sampling and roots
*
Progress
*
Logging
*
Subscriptions
*
Running your server
Running your server
*
Add to an existing app
*
Deploy & scale
*
Authorization
*
OpenTelemetry
*
Serving legacy clients
*
Clients
Clients
*
Callbacks
*
Transports
*
OAuth
*
Identity assertion
*
Multiple servers
*
Subscriptions
*
Caching
*
Protocol versions
*
Deprecated features
*
Advanced
Advanced
*
The low-level Server
*
Pagination
*
Middleware
*
Extensions
*
MCP Apps
*
Troubleshooting
*
Translations
*
Migration Guide
*
API Reference
API Reference
*
mcp
*
mcp_types
On this page
*
Requirements
*
Installation
*
Example
*
Create it
*
Run it
*
Try it
*
Recap
*
Where to go next
MCP Python SDK
This documents v2, the current stable release line
New to v2, or coming from v1? What's new in v2 is the five-minute tour of what changed, and the Migration Guide covers every breaking change.
Still on v1.x? Its documentation lives at the v1.x docs .
Something rough or confusing? Tell us .
The Model Context Protocol (MCP) lets applications provide context to LLMs in a standardized way, separating the concern of providing context from the LLM interaction itself.
This is the official Python SDK for it. With it you can:
* Build MCP servers that expose tools, resources, and prompts to any MCP host.
* Build MCP clients that connect to any MCP server.
* Speak every standard transport: stdio, Streamable HTTP, and SSE.
Requirements
Python 3.10+.
Installation
uv pip
uv add "mcp[cli]"
pip install "mcp[cli]"
The [cli] extra gives you the mcp command; you'll want it for development.
See Installation for what each dependency is for.
Example
Create it
Create a file server.py :
server.py from mcp.server import MCPServer
mcp = MCPServer ( "Demo" )
@mcp . tool ()
def add ( a : int , b : int ) -> int :
"""Add two numbers."""
return a + b
@mcp . resource ( "greeting:// {name} " )
def greeting ( name : str ) -> str :
"""Greet someone by name."""
return f "Hello, { name } !"
That's a complete MCP server.
It exposes one tool , add , and one templated resource , greeting://{name} .
Run it
uv run mcp dev server.py
This starts your server and opens the MCP Inspector , an interactive UI for poking at it. Open the URL it prints.
Note
The Inspector is a Node.js app, so mcp dev needs npx on your PATH .
Try it
In the Inspector, go to Tools and call add with a=1 , b=2 .
You get 3 back. ✨
The Inspector built that form (a required integer field for a , another for b ) from your type hints. So will Claude, and every other MCP host.
Now go to Resources and read greeting://World :
Hello, World!
Recap
Look again at what you did not write:
* No JSON Schema. a: int, b: int is the schema.
* No request parsing, no serialization, no validation code.
* No protocol handling at all.
You wrote two Python functions with type hints and a docstring. The SDK does the rest.
Where to go next
* Get started takes you from install to a working, tested server.
* Building an application that uses MCP servers? Start with Clients .
* Already have a FastAPI or Starlette app? Add to an existing app mounts an MCP server inside it.
* Hunting an exact error message? Troubleshooting is keyed by the verbatim text.
* Wondering what changed in v2? What's new in v2 is the five-minute tour.
* Migrating from v1? Start with the Migration Guide .
* Hunting for an exact signature? The API Reference is generated from the source.
* Reading with an LLM? This documentation is also published in the llms.txt format:
llms.txt is an index of the pages, and
llms-full.txt contains every page in a single file.
Back to top
Next
What's new in v2
Made with
Zensical
Links found on this page
- Skip to content [direct]
- de - Deutsch [direct]
- es - español [direct]
- fr - français [direct]
- hi - हिन्दी [direct]
- ja - 日本語 [direct]
- ko - 한국어 [direct]
- pt - português (Brasil) [direct]
- ru - русский язык [direct]
- tr - Türkçe [direct]
- uk - українська мова [direct]
- zh - 简体中文 [direct]
- zh-hant - 繁體中文 [direct]
- modelcontextprotocol/python-sdk [direct]
- What's new in v2 [direct]
- Get started [direct]
- Installation [direct]
- First steps [direct]
- Connect to a real host [direct]
- Testing [direct]
- Servers [direct]
- Tools [direct]
- Structured Output [direct]
- Resources [direct]
- URI templates [direct]
- Prompts [direct]
- Completions [direct]
- Images, audio & icons [direct]
- Handling errors [direct]
- Inside your handler [direct]
- The Context [direct]
- Dependencies [direct]
- Lifespan [direct]
- Elicitation [direct]
- Multi-round-trip requests [direct]
- Sampling and roots [direct]
- Progress [direct]
- Logging [direct]
- Subscriptions [direct]
- Running your server [direct]
- Add to an existing app [direct]
- Deploy & scale [direct]
- Authorization [direct]
- OpenTelemetry [direct]
- Serving legacy clients [direct]
- Clients [direct]
- Callbacks [direct]
- Transports [direct]
- OAuth [direct]
- Identity assertion [direct]
- Multiple servers [direct]
- Subscriptions [direct]
- Caching [direct]
- Protocol versions [direct]
- Deprecated features [direct]
- Advanced [direct]
- The low-level Server [direct]
- Pagination [direct]
- Middleware [direct]
- Extensions [direct]
- MCP Apps [direct]
- Troubleshooting [direct]
- Translations [direct]
- Migration Guide [direct]
- mcp [direct]
- mcp_types [direct]
- v1.x docs [direct]
- Tell us [direct]
- MCP Inspector [direct]
- llms.txt [direct]
- llms.txt [direct]
- llms-full.txt [direct]
- Zensical [direct]
|
|