Skip to content

Commit f2128a7

Browse files
committed
docs: Update README with new structure and content
1 parent bed35f3 commit f2128a7

File tree

2 files changed

+52
-162
lines changed

2 files changed

+52
-162
lines changed

README.md

Lines changed: 48 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
# MCP Python SDK
2+
3+
<div align="center">
4+
5+
<strong>Python implementation of the Model Context Protocol (MCP)</strong>
6+
27
[![PyPI][pypi-badge]][pypi-url]
38
[![MIT licensed][mit-badge]][mit-url]
49
[![Python Version][python-badge]][python-url]
510
[![Documentation][docs-badge]][docs-url]
611
[![Specification][spec-badge]][spec-url]
712
[![GitHub Discussions][discussions-badge]][discussions-url]
813

14+
</div>
15+
16+
<!-- omit in toc -->
17+
## Table of Contents
18+
19+
- [Overview](#overview)
20+
- [Installation](#installation)
21+
- [Writing MCP Servers](#writing-mcp-servers)
22+
- [FastMCP (Recommended)](#fastmcp-recommended)
23+
- [Low-Level Server (Advanced)](#low-level-server-advanced)
24+
- [Writing MCP Clients](#writing-mcp-clients)
25+
- [MCP Primitives](#mcp-primitives)
26+
- [Server Capabilities](#server-capabilities)
27+
- [Documentation](#documentation)
28+
- [Contributing](#contributing)
29+
- [License](#license)
30+
931
[pypi-badge]: https://img.shields.io/pypi/v/mcp.svg
1032
[pypi-url]: https://pypi.org/project/mcp/
1133
[mit-badge]: https://img.shields.io/pypi/l/mcp.svg
@@ -19,7 +41,6 @@
1941
[discussions-badge]: https://img.shields.io/github/discussions/modelcontextprotocol/python-sdk
2042
[discussions-url]: https://github.com/modelcontextprotocol/python-sdk/discussions
2143

22-
Python implementation of the [Model Context Protocol](https://modelcontextprotocol.io) (MCP), providing both client and server capabilities for integrating with LLM surfaces.
2344

2445
## Overview
2546

@@ -45,35 +66,13 @@ pip install mcp
4566
pip install -r requirements.txt
4667
```
4768

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
59-
- [Resources](https://modelcontextprotocol.io/docs/concepts/resources): File-like attachments
60-
- [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
7170

72-
## Quick Start
71+
The MCP Python SDK provides two ways to implement servers:
7372

74-
### FastMCP
73+
### FastMCP (Recommended)
7574

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:
7776

7877
```python
7978
from mcp.server.fastmcp import FastMCP
@@ -91,13 +90,17 @@ def get_greeting(name: str) -> str:
9190
return f"Hello, {name}!"
9291
```
9392

94-
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
95100

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)
97102

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:
101104

102105
```python
103106
from mcp.server.lowlevel import Server, NotificationOptions
@@ -108,7 +111,6 @@ import mcp.types as types
108111
# Create a server instance
109112
server = Server("example-server")
110113

111-
# Add prompt capabilities
112114
@server.list_prompts()
113115
async def handle_list_prompts() -> list[types.Prompt]:
114116
return [
@@ -147,7 +149,6 @@ async def handle_get_prompt(
147149
)
148150

149151
async def run():
150-
# Run the server as STDIO
151152
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
152153
await server.run(
153154
read_stream,
@@ -167,9 +168,9 @@ if __name__ == "__main__":
167168
asyncio.run(run())
168169
```
169170

170-
### Creating a Client
171+
## Writing MCP Clients
171172

172-
**example_client.py**
173+
The SDK provides a high-level client interface for connecting to MCP servers:
173174

174175
```python
175176
from mcp import ClientSession, StdioServerParameters
@@ -188,17 +189,12 @@ async def run():
188189
# Initialize the connection
189190
await session.initialize()
190191

191-
# The example server only supports prompt primitives:
192-
193192
# List available prompts
194193
prompts = await session.list_prompts()
195194

196195
# Get a prompt
197196
prompt = await session.get_prompt("example-prompt", arguments={"arg1": "value"})
198197

199-
"""
200-
Other example calls include:
201-
202198
# List available resources
203199
resources = await session.list_resources()
204200

@@ -210,139 +206,33 @@ async def run():
210206

211207
# Call a tool
212208
result = await session.call_tool("tool-name", arguments={"arg1": "value"})
213-
"""
214209

215210
if __name__ == "__main__":
216211
import asyncio
217212
asyncio.run(run())
218213
```
219214

220-
## Primitives
215+
## MCP Primitives
221216

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:
223218

224219
| Primitive | Control | Description | Example Use |
225220
|-----------|-----------------------|-----------------------------------------------------|------------------------------|
226221
| Prompts | User-controlled | Interactive templates invoked by user choice | Slash commands, menu options |
227222
| Resources | Application-controlled| Contextual data managed by the client application | File contents, API responses |
228223
| Tools | Model-controlled | Functions exposed to the LLM to take actions | API calls, data updates |
229224

230-
### User-Controlled Primitives
231-
232-
**Prompts** are designed to be explicitly selected by users for their interactions with LLMs.
233-
234-
| Decorator | Description |
235-
|--------------------------|----------------------------------------|
236-
| `@server.list_prompts()` | List available prompt templates |
237-
| `@server.get_prompt()` | Get a specific prompt with arguments |
238-
239-
### Application-Controlled Primitives
240-
241-
**Resources** are controlled by the client application, which decides how and when they should be used based on its own logic.
242-
243-
| Decorator | Description |
244-
|--------------------------------|---------------------------------------|
245-
| `@server.list_resources()` | List available resources |
246-
| `@server.read_resource()` | Read a specific resource's content |
247-
| `@server.subscribe_resource()` | Subscribe to resource updates |
248-
249-
### Model-Controlled Primitives
250-
251-
**Tools** are exposed to LLMs to enable automated actions, with user approval.
252-
253-
| Decorator | Description |
254-
|------------------------|------------------------------------|
255-
| `@server.list_tools()` | List available tools |
256-
| `@server.call_tool()` | Execute a tool with arguments |
257-
258-
### Server Management
259-
260-
Additional decorators for server functionality:
261-
262-
| Decorator | Description |
263-
|-------------------------------|--------------------------------|
264-
| `@server.set_logging_level()` | Update server logging level |
265-
266-
### Capabilities
267-
268-
MCP servers declare capabilities during initialization. These map to specific decorators:
269-
270-
| Capability | Feature Flag | Decorators | Description |
271-
|-------------|------------------------------|-----------------------------------------------------------------|-------------------------------------|
272-
| `prompts` | `listChanged` | `@list_prompts`<br/>`@get_prompt` | Prompt template management |
273-
| `resources` | `subscribe`<br/>`listChanged`| `@list_resources`<br/>`@read_resource`<br/>`@subscribe_resource`| Resource exposure and updates |
274-
| `tools` | `listChanged` | `@list_tools`<br/>`@call_tool` | Tool discovery and execution |
275-
| `logging` | - | `@set_logging_level` | Server logging configuration |
276-
| `completion`| - | `@complete_argument` | Argument completion suggestions |
225+
### Server Capabilities
277226

278-
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:
287-
288-
- Sampling from the client's LLM
289-
- Sending progress updates
290-
- Logging messages
291-
- Accessing request metadata
292-
293-
Example using request context for LLM sampling:
294-
295-
```python
296-
@server.call_tool()
297-
async def handle_call_tool(name: str, arguments: dict) -> list[types.TextContent]:
298-
# Access the current request context
299-
context = server.request_context
300-
301-
# Use the session to sample from the client's LLM
302-
result = await context.session.create_message(
303-
messages=[
304-
types.SamplingMessage(
305-
role="user",
306-
content=types.TextContent(
307-
type="text",
308-
text="Analyze this data: " + json.dumps(arguments)
309-
)
310-
)
311-
],
312-
max_tokens=100
313-
)
314-
315-
return [types.TextContent(type="text", text=result.content.text)]
316-
```
317-
318-
Using request context for progress updates:
319-
320-
```python
321-
@server.call_tool()
322-
async def handle_call_tool(name: str, arguments: dict) -> list[types.TextContent]:
323-
context = server.request_context
324-
325-
if progress_token := context.meta.progressToken:
326-
# Send progress notifications
327-
await context.session.send_progress_notification(
328-
progress_token=progress_token,
329-
progress=0.5,
330-
total=1.0
331-
)
332-
333-
# Perform operation...
334-
335-
if progress_token:
336-
await context.session.send_progress_notification(
337-
progress_token=progress_token,
338-
progress=1.0,
339-
total=1.0
340-
)
341-
342-
return [types.TextContent(type="text", text="Operation complete")]
343-
```
227+
MCP servers declare capabilities during initialization:
344228

345-
The request context is automatically set for each request and provides a safe way to access the current client session and request metadata.
229+
| Capability | Feature Flag | Description |
230+
|-------------|------------------------------|------------------------------------|
231+
| `prompts` | `listChanged` | Prompt template management |
232+
| `resources` | `subscribe`<br/>`listChanged`| Resource exposure and updates |
233+
| `tools` | `listChanged` | Tool discovery and execution |
234+
| `logging` | - | Server logging configuration |
235+
| `completion`| - | Argument completion suggestions |
346236

347237
## Documentation
348238

uv.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)