You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python implementation of the [Model Context Protocol](https://modelcontextprotocol.io) (MCP), providing both client and server capabilities for integrating with LLM surfaces.
23
44
24
45
## Overview
25
46
@@ -45,35 +66,13 @@ pip install mcp
45
66
pip install -r requirements.txt
46
67
```
47
68
48
-
## Overview
49
-
MCP servers provide focused functionality like resources, tools, prompts, and other capabilities that can be reused across many client applications. These servers are designed to be easy to build, highly composable, and modular.
50
-
51
-
### Key design principles
52
-
- Servers are extremely easy to build with clear, simple interfaces
53
-
- Multiple servers can be composed seamlessly through a shared protocol
54
-
- Each server operates in isolation and cannot access conversation context
55
-
- Features can be added progressively through capability negotiation
56
-
57
-
### Server provided primitives
58
-
-[Prompts](https://modelcontextprotocol.io/docs/concepts/prompts): Templatable text
-[Tools](https://modelcontextprotocol.io/docs/concepts/tools): Functions that models can call
61
-
- Utilities:
62
-
- Completion: Auto-completion provider for prompt arguments or resource URI templates
63
-
- Logging: Logging to the client
64
-
- Pagination*: Pagination for long results
65
-
66
-
### Client provided primitives
67
-
-[Sampling](https://modelcontextprotocol.io/docs/concepts/sampling): Allow servers to sample using client models
68
-
- Roots: Information about locations to operate on (e.g., directories)
69
-
70
-
Connections between clients and servers are established through transports like **stdio** or **SSE** (Note that most clients support stdio, but not SSE at the moment). The transport layer handles message framing, delivery, and error handling.
69
+
## Writing MCP Servers
71
70
72
-
## Quick Start
71
+
The MCP Python SDK provides two ways to implement servers:
73
72
74
-
### FastMCP
73
+
### FastMCP (Recommended)
75
74
76
-
The fastest way to build MCP servers is with FastMCP, which provides a high-level, Pythonic interface:
75
+
FastMCP provides a high-level, Pythonic interface for building MCP servers quickly and easily. It handles all the complex protocol details so you can focus on building great tools:
FastMCP handles all the complex protocol details and server management, so you can focus on building great tools. It's designed to be high-level and Pythonic - in most cases, decorating a function is all you need.
93
+
FastMCP features:
94
+
- Simple, decorator-based API
95
+
- Automatic type handling and validation
96
+
- Built-in support for async functions
97
+
- Progress reporting and logging
98
+
- Resource templating
99
+
- Image handling
95
100
96
-
FastMCP was originally developed by Jeremiah Lowin at [jlowin/fastmcp](https://github.com/jlowin/fastmcp). We are grateful for his contribution in developing this excellent framework that has now been integrated into MCP.
101
+
### Low-Level Server (Advanced)
97
102
98
-
### Low-Level Implementation
99
-
100
-
For more control, you can use the low-level MCP implementation directly. This gives you full access to the protocol and allows you to customize every aspect of your server. We recommend using `mcp.server.lowlevel` for all low-level server implementations, as the direct `mcp.server` imports will be deprecated and removed in SDK 2.0.0.
103
+
For more control, you can use the low-level server implementation directly. This gives you full access to the protocol and allows you to customize every aspect of your server:
101
104
102
105
```python
103
106
from mcp.server.lowlevel import Server, NotificationOptions
result =await session.call_tool("tool-name", arguments={"arg1": "value"})
213
-
"""
214
209
215
210
if__name__=="__main__":
216
211
import asyncio
217
212
asyncio.run(run())
218
213
```
219
214
220
-
## Primitives
215
+
## MCP Primitives
221
216
222
-
The MCP Python SDK provides decorators that map to the core protocol primitives. Each primitive follows a different interaction pattern based on how it is controlled and used:
217
+
The MCP protocol defines three core primitives that servers can implement:
223
218
224
219
| Primitive | Control | Description | Example Use |
Capabilities are negotiated during connection initialization. Servers only need to implement the decorators for capabilities they support.
279
-
280
-
## Client Interaction
281
-
282
-
The MCP Python SDK enables servers to interact with clients through request context and session management. This allows servers to perform operations like LLM sampling and progress tracking.
283
-
284
-
### Request Context
285
-
286
-
The Request Context provides access to the current request and client session. It can be accessed through `server.request_context` and enables:
0 commit comments