Update sampling.mdx

This commit is contained in:
Justin Spahr-Summers 2024-11-21 15:46:20 +00:00
commit 6eed214799
No known key found for this signature in database

View file

@ -5,6 +5,10 @@ description: "Let your servers request completions from LLMs"
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. 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.
<Info>
This feature of MCP is not yet supported in the Claude Desktop client.
</Info>
## How sampling works ## How sampling works
The sampling flow follows these steps: The sampling flow follows these steps:
@ -28,14 +32,24 @@ Sampling requests use a standardized message format:
role: "user" | "assistant", role: "user" | "assistant",
content: { content: {
type: "text" | "image", type: "text" | "image",
// For text: // For text:
text?: string, text?: string,
// For images: // For images:
data?: string, // base64 encoded data?: string, // base64 encoded
mimeType?: string mimeType?: string
} }
} }
], ],
modelPreferences?: {
hints?: [{
name?: string // Suggested model name/family
}],
costPriority?: number, // 0-1, importance of minimizing cost
speedPriority?: number, // 0-1, importance of low latency
intelligencePriority?: number // 0-1, importance of capabilities
},
systemPrompt?: string, systemPrompt?: string,
includeContext?: "none" | "thisServer" | "allServers", includeContext?: "none" | "thisServer" | "allServers",
temperature?: number, temperature?: number,
@ -56,6 +70,22 @@ The `messages` array contains the conversation history to send to the LLM. Each
- Text content with a `text` field - Text content with a `text` field
- Image content with `data` (base64) and `mimeType` fields - Image content with `data` (base64) and `mimeType` fields
### Model preferences
The `modelPreferences` object allows servers to specify their model selection preferences:
- `hints`: Array of model name suggestions that clients can use to select an appropriate model:
- `name`: String that can match full or partial model names (e.g. "claude-3", "sonnet")
- Clients may map hints to equivalent models from different providers
- Multiple hints are evaluated in preference order
- Priority values (0-1 normalized):
- `costPriority`: Importance of minimizing costs
- `speedPriority`: Importance of low latency response
- `intelligencePriority`: Importance of advanced model capabilities
Clients make the final model selection based on these preferences and their available models.
### System prompt ### System prompt
An optional `systemPrompt` field allows servers to request a specific system prompt. The client may modify or ignore this. An optional `systemPrompt` field allows servers to request a specific system prompt. The client may modify or ignore this.
@ -86,7 +116,7 @@ The client returns a completion result:
```typescript ```typescript
{ {
model: string, // Name of the model used model: string, // Name of the model used
stopReason: "endTurn" | "stopSequence" | "maxTokens", stopReason?: "endTurn" | "stopSequence" | "maxTokens" | string,
role: "user" | "assistant", role: "user" | "assistant",
content: { content: {
type: "text" | "image", type: "text" | "image",
@ -97,33 +127,27 @@ The client returns a completion result:
} }
``` ```
## Example implementation ## Example request
Here's an example of requesting sampling from a client: Here's an example of requesting sampling from a client:
```json
```typescript
// Server-side code
const response = await server.request({
method: "sampling/createMessage",
params: {
messages: [
{ {
role: "user", "method": "sampling/createMessage",
content: { "params": {
type: "text", "messages": [
text: "What files are in the current directory?" {
"role": "user",
"content": {
"type": "text",
"text": "What files are in the current directory?"
} }
} }
], ],
systemPrompt: "You are a helpful file system assistant.", "systemPrompt": "You are a helpful file system assistant.",
includeContext: "thisServer", "includeContext": "thisServer",
maxTokens: 100, "maxTokens": 100
temperature: 0.7 }
} }
}, CreateMessageResultSchema);
// Handle the completion
console.log(response.content.text);
``` ```
## Best practices ## Best practices