98b7159cb8
Main branch checks / checks (push) Failing after 1s
Conformance Tests / server-conformance (push) Has been cancelled
Conformance Tests / client-conformance (push) Has been cancelled
81 lines
2.7 KiB
Markdown
81 lines
2.7 KiB
Markdown
# MCP Python SDK
|
|
|
|
!!! tip "You are viewing the v1.x maintenance-line documentation"
|
|
v2 is the current stable release: its documentation is at
|
|
<https://py.sdk.modelcontextprotocol.io/>. Staying on v1.x for now? Pin `mcp<2`
|
|
(for example `mcp>=1.28,<2`) so an unpinned install doesn't move you to 2.x.
|
|
|
|
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](https://modelcontextprotocol.io).
|
|
|
|
## Quick Example
|
|
|
|
Here's a simple MCP server that exposes a tool, resource, and prompt:
|
|
|
|
```python title="server.py"
|
|
from 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:
|
|
|
|
```bash
|
|
uv run --with "mcp<2" server.py
|
|
```
|
|
|
|
Then open the [MCP Inspector](https://github.com/modelcontextprotocol/inspector) and connect to `http://localhost:8000/mcp`:
|
|
|
|
```bash
|
|
npx -y @modelcontextprotocol/inspector
|
|
```
|
|
|
|
## Getting Started
|
|
|
|
<!-- TODO(Marcelo): automatically generate the follow references with a header on each of those files. -->
|
|
1. **[Install](installation.md)** the MCP SDK
|
|
2. **[Build servers](server.md)** - tools, resources, prompts, transports, ASGI mounting
|
|
3. **[Write clients](client.md)** - connect to servers, use tools/resources/prompts
|
|
4. **[Explore authorization](authorization.md)** - add security to your servers
|
|
5. **[Use low-level APIs](low-level-server.md)** - for advanced customization
|
|
6. **[Protocol features](protocol.md)** - MCP primitives, server capabilities
|
|
|
|
## API Reference
|
|
|
|
Full API documentation is available in the [API Reference](api.md).
|
|
|
|
## llms.txt
|
|
|
|
Reading with an LLM? This documentation is also published in the [llms.txt](https://llmstxt.org/) format:
|
|
[llms.txt](https://py.sdk.modelcontextprotocol.io/v1/llms.txt) is an index of the pages, and
|
|
[llms-full.txt](https://py.sdk.modelcontextprotocol.io/v1/llms-full.txt) contains every page in a single file.
|