Skip to content

Commit 519eb32

Browse files
committed
Update sampling.md
1 parent 924cce9 commit 519eb32

File tree

1 file changed

+28
-42
lines changed

1 file changed

+28
-42
lines changed

docs/specification/client/sampling.md

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,26 @@ weight: 40
88
**Protocol Revision**: {{< param protocolRevision >}}
99
{{< /callout >}}
1010

11-
The Model Context Protocol (MCP) provides a standardized way for servers to request generations from language models via clients. This unique reverse flow allows clients to maintain control over model access, selection, and permissions while enabling servers to leverage AI capabilities. Servers can request text or image-based interactions and optionally include context from MCP servers in their prompts.
11+
The Model Context Protocol (MCP) provides a standardized way for servers to request LLM sampling ("completions" or "generations") from language models via clients. This flow allows clients to maintain control over model access, selection, and permissions while enabling servers to leverage AI capabilities&mdash;with no server API keys necessary. Servers can request text or image-based interactions and optionally include context from MCP servers in their prompts.
1212

1313
## User Interaction Model
1414

15-
Sampling in MCP is designed around a human-in-the-loop approval pattern. A recommended implementation exposes sampling requests through an approval UI that allows users to:
15+
Sampling in MCP allows servers to implement agentic behaviors, by enabling LLM calls to occur _nested_ inside other MCP server features.
1616

17-
1. Review incoming sampling requests
18-
2. Inspect model selections and parameters
19-
3. View and edit prompts before sending
20-
4. Review generated responses before delivery
17+
Implementations are free to expose sampling through any interface pattern that suits their needs&mdash;the protocol itself does not mandate any specific user interaction model.
2118

22-
However, implementations are free to expose sampling through any interface pattern that suits their needs - the protocol itself does not mandate any specific user interaction model.
19+
{{< callout type="warning" >}}
20+
For trust & safety and security, there **SHOULD** always be a human in the loop with the ability to deny sampling requests.
21+
22+
Applications **SHOULD**:
23+
* Provide UI that makes it easy and intuitive to review sampling requests
24+
* Allow users to view and edit prompts before sending
25+
* Present generated responses for review before delivery
26+
{{< /callout >}}
2327

2428
## Capabilities
2529

26-
Clients that support sampling MUST include a `sampling` capability in their `ClientCapabilities` during initialization:
30+
Clients that support sampling **MUST** declare the `sampling` capability during [initialization]({{< ref "/specification/basic/lifecycle#initialization" >}}):
2731

2832
```json
2933
{
@@ -33,8 +37,6 @@ Clients that support sampling MUST include a `sampling` capability in their `Cli
3337
}
3438
```
3539

36-
The empty object indicates basic sampling support with no additional features.
37-
3840
## Protocol Messages
3941

4042
### Creating Messages
@@ -67,8 +69,7 @@ To request a language model generation, servers send a `sampling/createMessage`
6769
"speedPriority": 0.5
6870
},
6971
"systemPrompt": "You are a helpful assistant.",
70-
"maxTokens": 100,
71-
"temperature": 0.7
72+
"maxTokens": 100
7273
}
7374
}
7475
```
@@ -151,18 +152,18 @@ To solve this, MCP implements a preference system that combines abstract capabil
151152

152153
Servers express their needs through three normalized priority values (0-1):
153154

154-
- `costPriority`: How important is minimizing costs? Higher values prefer cheaper models
155-
- `speedPriority`: How important is low latency? Higher values prefer faster models
156-
- `intelligencePriority`: How important are advanced capabilities? Higher values prefer more capable models
155+
- `costPriority`: How important is minimizing costs? Higher values prefer cheaper models.
156+
- `speedPriority`: How important is low latency? Higher values prefer faster models.
157+
- `intelligencePriority`: How important are advanced capabilities? Higher values prefer more capable models.
157158

158159
#### Model Hints
159160

160161
While priorities help select models based on characteristics, `hints` allow servers to suggest specific models or model families:
161162

162163
- Hints are treated as substrings that can match model names flexibly
163164
- Multiple hints are evaluated in order of preference
164-
- Clients MAY map hints to equivalent models from different providers
165-
- Hints are advisory - clients make final model selection
165+
- Clients **MAY** map hints to equivalent models from different providers
166+
- Hints are advisory&mdash;clients make final model selection
166167

167168
For example:
168169
```json
@@ -171,8 +172,8 @@ For example:
171172
{"name": "claude-3-sonnet"}, // Prefer Sonnet-class models
172173
{"name": "claude"} // Fall back to any Claude model
173174
],
174-
"costPriority": 0.3, // Cost is less important
175-
"speedPriority": 0.8, // Speed is very important
175+
"costPriority": 0.3, // Cost is less important
176+
"speedPriority": 0.8, // Speed is very important
176177
"intelligencePriority": 0.5 // Moderate capability needs
177178
}
178179
```
@@ -181,39 +182,24 @@ The client processes these preferences to select an appropriate model from its a
181182

182183
## Error Handling
183184

184-
Clients SHOULD return standard JSON-RPC errors for common failure cases:
185-
186-
- User rejection: `-32001`
187-
- Model unavailable: `-32002`
188-
- Invalid parameters: `-32602`
189-
- Context length exceeded: `-32003`
185+
Clients **SHOULD** return errors for common failure cases:
190186

191187
Example error:
192188
```json
193189
{
194190
"jsonrpc": "2.0",
195191
"id": 1,
196192
"error": {
197-
"code": -32001,
198-
"message": "User rejected sampling request",
199-
"data": {
200-
"reason": "Content policy violation"
201-
}
193+
"code": -1,
194+
"message": "User rejected sampling request"
202195
}
203196
}
204197
```
205198

206199
## Security Considerations
207200

208-
1. Clients MUST implement user approval controls
209-
2. Both parties SHOULD validate message content
210-
3. Servers SHOULD respect model preference hints
211-
4. Clients SHOULD implement rate limiting
212-
5. Both parties MUST handle sensitive data appropriately
213-
214-
## See Also
215-
216-
{{< cards >}}
217-
{{< card link="/utilities/progress" title="Progress Tracking" icon="arrow-circle-right" >}}
218-
{{< card link="/utilities/cancellation" title="Request Cancellation" icon="x-circle" >}}
219-
{{< /cards >}}
201+
1. Clients **SHOULD** implement user approval controls
202+
2. Both parties **SHOULD** validate message content
203+
3. Clients **SHOULD** respect model preference hints
204+
4. Clients **SHOULD** implement rate limiting
205+
5. Both parties **MUST** handle sensitive data appropriately

0 commit comments

Comments
 (0)