采样 Sampling
让您的服务器请求来自 LLM 的补全
采样的工作原理
1.
sampling/createMessage
请求2.
3.
4.
5.
消息格式
请求参数
消息
messages
数组包含要发送到 LLM 的对话历史记录。 每条消息都有:role
: “user” 或 “assistant”content
text
字段的文本内容data
(base64 编码)和 mimeType
字段的图像内容模型偏好
modelPreferences
对象允许服务器指定其模型选择偏好:hints
: 客户端可用于选择合适模型的模型名称建议数组:name
: 可以匹配完整或部分模型名称的字符串(例如,“claude-3”,“sonnet”)costPriority
: 最小化成本的重要性speedPriority
: 低延迟响应的重要性intelligencePriority
: 高级模型能力的重要性系统提示
systemPrompt
字段允许服务器请求特定的系统提示(system prompt)。 客户端可以修改或忽略此提示。上下文包含
includeContext
参数指定要包含的 MCP 上下文:"none"
:无其他上下文"thisServer"
:包含来自请求服务器的上下文"allServers"
:包含来自所有连接的 MCP 服务器的上下文采样参数
temperature
: 控制随机性(0.0 到 1.0)maxTokens
: 要生成的最大令牌(token)数stopSequences
: 停止生成的序列数组metadata
: 其他特定于提供程序的参数响应格式
示例请求
{
"method": "sampling/createMessage",
"params": {
"messages": [
{
"role": "user",
"content": {
"type": "text",
"text": "What files are in the current directory?"
}
}
],
"systemPrompt": "You are a helpful file system assistant.",
"includeContext": "thisServer",
"maxTokens": 100
}
}
最佳实践
1.
2.
3.
4.
includeContext
包含相关上下文5.
6.
7.
8.
9.
10.