|
| 1 | +--- |
| 2 | +title: Server |
| 3 | +type: docs |
| 4 | +weight: 1 |
| 5 | +cascade: |
| 6 | + type: docs |
| 7 | +--- |
| 8 | + |
| 9 | +This guide walks you through setting up and implementing a basic MCP server using Python. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Install the MCP Python SDK using pip: |
| 14 | + |
| 15 | +```bash |
| 16 | +pip install mcp-python |
| 17 | +``` |
| 18 | +or add it to your [pyproject.toml](https://packaging.python.org/en/latest/guides/writing-pyproject-toml/) using [uv]( {{< param "uv_docs" >}} ). |
| 19 | +```bash |
| 20 | +uv add mcp-python |
| 21 | +``` |
| 22 | + |
| 23 | +## Server API Overview |
| 24 | + |
| 25 | +The Python SDK provides a decorator-based API for implementing MCP servers. The main `Server` class offers decorators for handling different types of requests: |
| 26 | + |
| 27 | +### Resource Handling |
| 28 | +- `@server.list_resources()`: Lists available resources |
| 29 | +- `@server.read_resource()`: Reads content of a specific resource |
| 30 | +- `@server.subscribe_resource()`: Handles resource subscription requests |
| 31 | +- `@server.unsubscribe_resource()`: Handles unsubscribe requests |
| 32 | + |
| 33 | +### Tool Support |
| 34 | +- `@server.list_tools()`: Lists available tools |
| 35 | +- `@server.call_tool()`: Handles tool execution requests |
| 36 | + |
| 37 | +### Prompt Management |
| 38 | +- `@server.list_prompts()`: Lists available prompts |
| 39 | +- `@server.get_prompt()`: Retrieves specific prompt templates |
| 40 | + |
| 41 | +### Progress & Logging |
| 42 | +- `@server.progress_notification()`: Handles progress updates |
| 43 | +- `@server.set_logging_level()`: Controls server logging |
| 44 | + |
| 45 | +The decorators automatically handle request/response formatting and protocol compliance, allowing you to focus on implementing the core functionality. |
| 46 | + |
| 47 | +## Basic Server Implementation |
| 48 | + |
| 49 | +Here's a minimal example of implementing an MCP server: |
| 50 | + |
| 51 | +```python |
| 52 | +from mcp_python.server import Server |
| 53 | +import anyio |
| 54 | + |
| 55 | +# Create a new server instance |
| 56 | +server = Server("example-server") |
| 57 | + |
| 58 | +# Implement resource handling |
| 59 | +@server.list_resources() |
| 60 | +async def list_resources(): |
| 61 | + # Return list of available resources |
| 62 | + return [ |
| 63 | + { |
| 64 | + "uri": "example://resource1", |
| 65 | + "name": "Example Resource 1", |
| 66 | + "description": "An example resource" |
| 67 | + } |
| 68 | + ] |
| 69 | + |
| 70 | +@server.read_resource() |
| 71 | +async def read_resource(uri): |
| 72 | + # Return content for the requested resource |
| 73 | + if uri == "example://resource1": |
| 74 | + return "Example resource content" |
| 75 | + raise ValueError(f"Unknown resource: {uri}") |
| 76 | + |
| 77 | +# Main server loop |
| 78 | +async def main(): |
| 79 | + # Use stdio transport for this example |
| 80 | + async with stdio_server() as (read_stream, write_stream): |
| 81 | + await server.run( |
| 82 | + read_stream, |
| 83 | + write_stream, |
| 84 | + server.create_initialization_options() |
| 85 | + ) |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + anyio.run(main) |
| 89 | +``` |
| 90 | + |
| 91 | +## Advanced Server Features |
| 92 | + |
| 93 | +### Adding Tool Support |
| 94 | + |
| 95 | +```python |
| 96 | +@server.list_tools() |
| 97 | +async def list_tools(): |
| 98 | + return [ |
| 99 | + { |
| 100 | + "name": "example-tool", |
| 101 | + "description": "An example tool", |
| 102 | + "parameters": { |
| 103 | + "type": "object", |
| 104 | + "properties": { |
| 105 | + "param1": {"type": "string"} |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + ] |
| 110 | + |
| 111 | +@server.call_tool() |
| 112 | +async def call_tool(name, **kwargs): |
| 113 | + if name == "example-tool": |
| 114 | + return f"Tool executed with params: {kwargs}" |
| 115 | + raise ValueError(f"Unknown tool: {name}") |
| 116 | +``` |
| 117 | + |
| 118 | +### Adding Prompt Support |
| 119 | + |
| 120 | +```python |
| 121 | +@server.list_prompts() |
| 122 | +async def list_prompts(): |
| 123 | + return [ |
| 124 | + { |
| 125 | + "name": "example-prompt", |
| 126 | + "description": "An example prompt", |
| 127 | + "parameters": { |
| 128 | + "type": "object", |
| 129 | + "properties": { |
| 130 | + "param1": {"type": "string"} |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + ] |
| 135 | + |
| 136 | +@server.get_prompt() |
| 137 | +async def get_prompt(name, arguments): |
| 138 | + if name == "example-prompt": |
| 139 | + return PromptResponse( |
| 140 | + messages=[ |
| 141 | + Message( |
| 142 | + role="user", |
| 143 | + content=f"Example prompt with args: {arguments}" |
| 144 | + ) |
| 145 | + ], |
| 146 | + desc="Example prompt response" |
| 147 | + ) |
| 148 | + raise ValueError(f"Unknown prompt: {name}") |
| 149 | +``` |
| 150 | + |
| 151 | +## Running the Server |
| 152 | + |
| 153 | +Start the server from the command line: |
| 154 | + |
| 155 | +```bash |
| 156 | +python your_server.py |
| 157 | +``` |
| 158 | + |
| 159 | +For SSE or WebSocket transport, you'll need to use an ASGI server like Hypercorn: |
| 160 | + |
| 161 | +```bash |
| 162 | +hypercorn your_server:app --bind 0.0.0.0:8000 |
| 163 | +``` |
| 164 | + |
| 165 | +## Best Practices |
| 166 | + |
| 167 | +1. Always implement proper error handling for all server methods |
| 168 | +2. Use typing hints for better code clarity and IDE support |
| 169 | +3. Document your server's capabilities and resources |
| 170 | +4. Implement logging for debugging purposes |
| 171 | +5. Follow the MCP specification for consistent behavior |
| 172 | + |
| 173 | +## Common Patterns |
| 174 | + |
| 175 | +### Resource Updates |
| 176 | + |
| 177 | +```python |
| 178 | +async def notify_resource_update(uri, session): |
| 179 | + await session.send_resource_updated(uri) |
| 180 | +``` |
| 181 | + |
| 182 | +### Progress Notifications |
| 183 | + |
| 184 | +```python |
| 185 | +async def long_running_operation(session): |
| 186 | + for i in range(100): |
| 187 | + await session.send_progress_notification( |
| 188 | + "operation-1", |
| 189 | + progress=i, |
| 190 | + total=100 |
| 191 | + ) |
| 192 | +``` |
| 193 | + |
| 194 | +### Logging |
| 195 | + |
| 196 | +```python |
| 197 | +async def log_operation(session): |
| 198 | + await session.send_log_message( |
| 199 | + level="info", |
| 200 | + data="Operation completed", |
| 201 | + logger="example-server" |
| 202 | + ) |
| 203 | +``` |
0 commit comments