|
48 | 48 | - [Logging and Notifications](#logging-and-notifications) |
49 | 49 | - [Authentication](#authentication) |
50 | 50 | - [MCPServer Properties](#mcpserver-properties) |
| 51 | + - [Server Instructions](#server-instructions) |
51 | 52 | - [Session Properties and Methods](#session-properties-and-methods) |
52 | 53 | - [Request Context Properties](#request-context-properties) |
53 | 54 | - [Running Your Server](#running-your-server) |
@@ -1088,6 +1089,42 @@ def server_info(ctx: Context) -> dict: |
1088 | 1089 | } |
1089 | 1090 | ``` |
1090 | 1091 |
|
| 1092 | +### Server Instructions |
| 1093 | + |
| 1094 | +Server instructions are sent to clients during initialization and are useful for describing how related tools should be used together. They can provide workflow hints, domain context, or guardrails that apply across the server's tools without repeating the same guidance in every tool description. |
| 1095 | + |
| 1096 | +For example, instructions can describe a preferred order for tools in a workflow: |
| 1097 | + |
| 1098 | +<!-- snippet-source examples/snippets/servers/instructions.py --> |
| 1099 | +```python |
| 1100 | +from mcp.server.mcpserver import MCPServer |
| 1101 | + |
| 1102 | +mcp = MCPServer( |
| 1103 | + name="Workflow Assistant", |
| 1104 | + instructions=( |
| 1105 | + "Use the project tools together: call get_project_status first, " |
| 1106 | + "then use create_task only for missing or blocked work." |
| 1107 | + ), |
| 1108 | +) |
| 1109 | + |
| 1110 | + |
| 1111 | +@mcp.tool() |
| 1112 | +def get_project_status(project_id: str) -> str: |
| 1113 | + """Summarize current project status.""" |
| 1114 | + return f"Project {project_id} is on track." |
| 1115 | + |
| 1116 | + |
| 1117 | +@mcp.tool() |
| 1118 | +def create_task(project_id: str, title: str) -> str: |
| 1119 | + """Create a follow-up task for the project.""" |
| 1120 | + return f"Created task '{title}' for project {project_id}." |
| 1121 | +``` |
| 1122 | + |
| 1123 | +_Full example: [examples/snippets/servers/instructions.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/instructions.py)_ |
| 1124 | +<!-- /snippet-source --> |
| 1125 | + |
| 1126 | +Clients can read the same value from `InitializeResult.instructions` after connecting. Inside tools, the server can access it as `ctx.mcp_server.instructions`. |
| 1127 | + |
1091 | 1128 | ### Session Properties and Methods |
1092 | 1129 |
|
1093 | 1130 | The session object accessible via `ctx.session` provides advanced control over client communication: |
|
0 commit comments