Skip to content

Commit c0d662e

Browse files
committed
docs(instructions): contributes to some of the questions in issue #1464, main contribution is starting the ball rolling with some simple docs and use cases for instructions that are inline with existing documentation
1 parent 6c26d08 commit c0d662e

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

docs/concepts.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,99 @@
1111
- Context and sessions
1212
- Lifecycle and state
1313
-->
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).
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Example showing how to use server instructions to guide tool usage.
3+
4+
The instructions field helps clients understand how to use your tools
5+
together, effectively providing tool grouping/bundling/namespacing.
6+
7+
cd to the `examples/snippets` directory and run:
8+
uv run server server_instructions stdio
9+
"""
10+
11+
from typing import Any
12+
13+
from mcp.server.fastmcp import FastMCP
14+
15+
# Create server with instructions
16+
mcp = FastMCP(
17+
name="Multi-Domain Server",
18+
instructions="""This server provides tools across multiple domains:
19+
20+
## Weather Tools
21+
- get_weather: Get current weather for a location
22+
- get_forecast: Get weather forecast
23+
24+
These tools work together - use get_weather for current conditions,
25+
then get_forecast for future planning.
26+
27+
## Calendar Tools
28+
- create_event: Schedule a new calendar event
29+
- list_events: View upcoming events
30+
31+
Use list_events first to check availability before create_event.
32+
33+
## Best Practices
34+
- Always check weather before scheduling outdoor events
35+
- Use get_forecast to plan events 2-7 days ahead
36+
""",
37+
)
38+
39+
40+
# Define the tools mentioned in instructions
41+
@mcp.tool()
42+
def get_weather(location: str) -> dict[str, Any]:
43+
"""Get current weather for a location"""
44+
return {"location": location, "temperature": 72, "condition": "sunny", "humidity": 45}
45+
46+
47+
@mcp.tool()
48+
def get_forecast(location: str, days: int = 5) -> list[dict[str, Any]]:
49+
"""Get weather forecast for upcoming days"""
50+
return [{"day": i, "high": 70 + i, "low": 50 + i, "condition": "partly cloudy"} for i in range(days)]
51+
52+
53+
@mcp.tool()
54+
def create_event(title: str, date: str, time: str) -> dict[str, Any]:
55+
"""Schedule a new calendar event"""
56+
return {"id": "evt_123", "title": title, "date": date, "time": time, "status": "created"}
57+
58+
59+
@mcp.tool()
60+
def list_events(start_date: str, end_date: str) -> list[dict[str, Any]]:
61+
"""View upcoming events in date range"""
62+
return [
63+
{"title": "Team Meeting", "date": start_date, "time": "10:00"},
64+
{"title": "Lunch with Client", "date": start_date, "time": "12:00"},
65+
]

0 commit comments

Comments
 (0)