|
11 | 11 | - Context and sessions |
12 | 12 | - Lifecycle and state |
13 | 13 | --> |
| 14 | + |
| 15 | +## Server Instructions |
| 16 | + |
| 17 | +When a server initializes, it can send instructions tot he client explaining how the tools can be used as a collective group, this can be thought of like an instructions manual for the consumer of a given server. |
| 18 | + |
| 19 | +### Basic Usage |
| 20 | + |
| 21 | +Here's how you add instructions to your server: |
| 22 | + |
| 23 | +```python title="server.py" |
| 24 | +from mcp.server.fastmcp import FastMCP |
| 25 | + |
| 26 | +mcp = FastMCP( |
| 27 | + name="Weather & Calendar", |
| 28 | + instructions=""" |
| 29 | +# How to use this server |
| 30 | +
|
| 31 | +## Weather Tools |
| 32 | +- get_weather: Current conditions |
| 33 | +- get_forecast: Future predictions |
| 34 | +
|
| 35 | +## Calendar Tools |
| 36 | +- list_events: View your calendar |
| 37 | +- create_event: Schedule something |
| 38 | +
|
| 39 | +tip: Check the weather forecast before scheduling outdoor events |
| 40 | +""" |
| 41 | +) |
| 42 | + |
| 43 | +@mcp.tool() |
| 44 | +def get_weather(location: str) -> dict: |
| 45 | + """Get current weather""" |
| 46 | + return {"temp": 72, "condition": "sunny"} |
| 47 | + |
| 48 | +@mcp.tool() |
| 49 | +def create_event(title: str, date: str) -> dict: |
| 50 | + """Schedule an event""" |
| 51 | + return {"id": "evt_123", "title": title, "date": date} |
| 52 | +``` |
| 53 | + |
| 54 | +1. Instructions support Markdown formatting for better readability. |
| 55 | + |
| 56 | +!!! info |
| 57 | + The `instructions` field is part of the `InitializeResult` that clients receive during the connection handshake. It's optional, but super helpful when you have multiple related tools. |
| 58 | + |
| 59 | +### When to Use Instructions |
| 60 | + |
| 61 | +Instructions are shown to both humans (in client UIs like MCP Inspector) and LLMs (as context for tool selection). They work best when you have multiple related tools and need to explain how they work together. |
| 62 | + |
| 63 | +Use instructions when: |
| 64 | + |
| 65 | +- Your server has tools from different domains (like weather + calendar) |
| 66 | +- Tools should be used in a specific order or sequence |
| 67 | +- You need to share constraints or best practices |
| 68 | + |
| 69 | +They're **not** for documenting individual tool parameters - use docstrings for that. |
| 70 | + |
| 71 | +### Writing Good Instructions |
| 72 | + |
| 73 | +Focus on tool relationships and workflows, not individual tool details: |
| 74 | + |
| 75 | +```python title="good_instructions.py" |
| 76 | +instructions = """ |
| 77 | +## File Operations |
| 78 | +- read_file: Load file contents |
| 79 | +- write_file: Save to disk |
| 80 | +
|
| 81 | +Always use read_file before write_file to avoid overwriting data. |
| 82 | +
|
| 83 | +## Rate Limits |
| 84 | +- API calls: 100/hour |
| 85 | +- File operations: No limit |
| 86 | +""" |
| 87 | +``` |
| 88 | + |
| 89 | +Keep them concise (10-30 lines) and use Markdown headers to group related tools. |
| 90 | + |
| 91 | +!!! info |
| 92 | + Access instructions from tools using `ctx.fastmcp.instructions` to expose them programmatically. |
| 93 | + |
| 94 | +### Low-Level Server |
| 95 | + |
| 96 | +If you're using the low-level server API, you set instructions the same way: |
| 97 | + |
| 98 | +```python |
| 99 | +from mcp.server.lowlevel import Server |
| 100 | + |
| 101 | +server = Server( |
| 102 | + name="My Server", |
| 103 | + instructions="Your usage guide here..." |
| 104 | +) |
| 105 | +``` |
| 106 | + |
| 107 | +### Complete Example |
| 108 | + |
| 109 | +For a full working example showing instructions with a multi-domain server, check out [examples/snippets/servers/server_instructions.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/server_instructions.py). |
0 commit comments