Skip to content

Commit 6eed214

Browse files
committed
Update sampling.mdx
1 parent 2cace9a commit 6eed214

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

docs/concepts/sampling.mdx

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ description: "Let your servers request completions from LLMs"
55

66
Sampling is a powerful MCP feature that allows servers to request LLM completions through the client, enabling sophisticated agentic behaviors while maintaining security and privacy.
77

8+
<Info>
9+
This feature of MCP is not yet supported in the Claude Desktop client.
10+
</Info>
11+
812
## How sampling works
913

1014
The sampling flow follows these steps:
1115

1216
1. Server sends a `sampling/createMessage` request to the client
1317
2. Client reviews the request and can modify it
1418
3. Client samples from an LLM
15-
4. Client reviews the completion
19+
4. Client reviews the completion
1620
5. Client returns the result to the server
1721

1822
This human-in-the-loop design ensures users maintain control over what the LLM sees and generates.
@@ -28,14 +32,24 @@ Sampling requests use a standardized message format:
2832
role: "user" | "assistant",
2933
content: {
3034
type: "text" | "image",
35+
3136
// For text:
3237
text?: string,
38+
3339
// For images:
34-
data?: string, // base64 encoded
40+
data?: string, // base64 encoded
3541
mimeType?: string
3642
}
3743
}
3844
],
45+
modelPreferences?: {
46+
hints?: [{
47+
name?: string // Suggested model name/family
48+
}],
49+
costPriority?: number, // 0-1, importance of minimizing cost
50+
speedPriority?: number, // 0-1, importance of low latency
51+
intelligencePriority?: number // 0-1, importance of capabilities
52+
},
3953
systemPrompt?: string,
4054
includeContext?: "none" | "thisServer" | "allServers",
4155
temperature?: number,
@@ -51,11 +65,27 @@ Sampling requests use a standardized message format:
5165

5266
The `messages` array contains the conversation history to send to the LLM. Each message has:
5367

54-
- `role`: Either "user" or "assistant"
68+
- `role`: Either "user" or "assistant"
5569
- `content`: The message content, which can be:
5670
- Text content with a `text` field
5771
- Image content with `data` (base64) and `mimeType` fields
5872

73+
### Model preferences
74+
75+
The `modelPreferences` object allows servers to specify their model selection preferences:
76+
77+
- `hints`: Array of model name suggestions that clients can use to select an appropriate model:
78+
- `name`: String that can match full or partial model names (e.g. "claude-3", "sonnet")
79+
- Clients may map hints to equivalent models from different providers
80+
- Multiple hints are evaluated in preference order
81+
82+
- Priority values (0-1 normalized):
83+
- `costPriority`: Importance of minimizing costs
84+
- `speedPriority`: Importance of low latency response
85+
- `intelligencePriority`: Importance of advanced model capabilities
86+
87+
Clients make the final model selection based on these preferences and their available models.
88+
5989
### System prompt
6090

6191
An optional `systemPrompt` field allows servers to request a specific system prompt. The client may modify or ignore this.
@@ -85,8 +115,8 @@ The client returns a completion result:
85115

86116
```typescript
87117
{
88-
model: string, // Name of the model used
89-
stopReason: "endTurn" | "stopSequence" | "maxTokens",
118+
model: string, // Name of the model used
119+
stopReason?: "endTurn" | "stopSequence" | "maxTokens" | string,
90120
role: "user" | "assistant",
91121
content: {
92122
type: "text" | "image",
@@ -97,33 +127,27 @@ The client returns a completion result:
97127
}
98128
```
99129

100-
## Example implementation
130+
## Example request
101131

102132
Here's an example of requesting sampling from a client:
103-
104-
```typescript
105-
// Server-side code
106-
const response = await server.request({
107-
method: "sampling/createMessage",
108-
params: {
109-
messages: [
133+
```json
134+
{
135+
"method": "sampling/createMessage",
136+
"params": {
137+
"messages": [
110138
{
111-
role: "user",
112-
content: {
113-
type: "text",
114-
text: "What files are in the current directory?"
139+
"role": "user",
140+
"content": {
141+
"type": "text",
142+
"text": "What files are in the current directory?"
115143
}
116144
}
117145
],
118-
systemPrompt: "You are a helpful file system assistant.",
119-
includeContext: "thisServer",
120-
maxTokens: 100,
121-
temperature: 0.7
146+
"systemPrompt": "You are a helpful file system assistant.",
147+
"includeContext": "thisServer",
148+
"maxTokens": 100
122149
}
123-
}, CreateMessageResultSchema);
124-
125-
// Handle the completion
126-
console.log(response.content.text);
150+
}
127151
```
128152

129153
## Best practices
@@ -218,4 +242,4 @@ Be aware of these limitations:
218242
- Costs should be considered
219243
- Model availability varies
220244
- Response times vary
221-
- Not all content types supported
245+
- Not all content types supported

0 commit comments

Comments
 (0)