文档
Anthropic 兼容端点
Anthropic 兼容端点
LM Studio 通过 API 支持模型上下文协议(MCP)的使用。MCP 允许模型通过标准化的服务器与外部工具和服务进行交互。
MCP 服务器提供的工具可供模型在聊天请求期间调用。你可以通过两种方式启用 MCP 服务器:作为按请求定义的临时服务器,或作为 mcp.json 文件中预配置的服务器。
| 功能 | 临时 (Ephemeral) | mcp.json |
|---|---|---|
| 如何在请求中指定 | integrations → "type": "ephemeral_mcp" | integrations → "type": "plugin" |
| 配置 | 仅按请求定义 | 在 mcp.json 中预配置 |
| 使用场景 | 一次性请求、远程 MCP 工具执行 | 需要 command 的 MCP 服务器、常用服务器 |
| 服务器 ID | 通过集成中的 server_label 指定 | 通过集成中的 id(例如 mcp/playwright)指定 |
| 自定义标头 | 通过 headers 字段支持 | 在 mcp.json 中配置 |
临时 MCP 服务器在每个请求中动态定义。这对于测试或不想预配置服务器时非常有用。
临时 MCP 服务器要求在 服务器设置 中启用“允许按请求 MCP (Allow per-request MCPs)”设置。
curl https://:1234/api/v1/chat \
-H "Authorization: Bearer $LM_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "ibm/granite-4-micro",
"input": "What is the top trending model on hugging face?",
"integrations": [
{
"type": "ephemeral_mcp",
"server_label": "huggingface",
"server_url": "https://hugging-face.cn/mcp",
"allowed_tools": ["model_search"]
}
],
"context_length": 8000
}'
模型现在可以调用指定 MCP 服务器中的工具
{
"model_instance_id": "ibm/granite-4-micro",
"output": [
{
"type": "reasoning",
"content": "..."
},
{
"type": "message",
"content": "..."
},
{
"type": "tool_call",
"tool": "model_search",
"arguments": {
"sort": "trendingScore",
"limit": 1
},
"output": "...",
"provider_info": {
"server_label": "huggingface",
"type": "ephemeral_mcp"
}
},
{
"type": "reasoning",
"content": "\n"
},
{
"type": "message",
"content": "The top trending model is ..."
}
],
"stats": {
"input_tokens": 419,
"total_output_tokens": 362,
"reasoning_output_tokens": 195,
"tokens_per_second": 27.620159487314744,
"time_to_first_token_seconds": 1.437
},
"response_id": "resp_7c1a08e3d6e279efcfecb02df9de7cbd316e93422d0bb5cb"
}
MCP 服务器可以在你的 mcp.json 文件中预配置。对于需要对计算机执行操作的 MCP 服务器(如 microsoft/playwright-mcp)以及你经常使用的服务器,推荐使用这种方式。
来自 mcp.json 的 MCP 服务器要求在 服务器设置 中启用“允许调用来自 mcp.json 的服务器 (Allow calling servers from mcp.json)”设置。

在 LM Studio 中编辑 mcp.json
curl https://:1234/api/v1/chat \
-H "Authorization: Bearer $LM_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "ibm/granite-4-micro",
"input": "Open lmstudio.ai",
"integrations": ["mcp/playwright"],
"context_length": 8000,
"temperature": 0
}'
响应中包含来自配置的 MCP 服务器的工具调用
{
"model_instance_id": "ibm/granite-4-micro",
"output": [
{
"type": "reasoning",
"content": "..."
},
{
"type": "message",
"content": "..."
},
{
"type": "tool_call",
"tool": "browser_navigate",
"arguments": {
"url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
},
"output": "...",
"provider_info": {
"plugin_id": "mcp/playwright",
"type": "plugin"
}
},
{
"type": "reasoning",
"content": "..."
},
{
"type": "message",
"content": "The YouTube video page for ..."
}
],
"stats": {
"input_tokens": 2614,
"total_output_tokens": 594,
"reasoning_output_tokens": 389,
"tokens_per_second": 26.293245822877495,
"time_to_first_token_seconds": 0.154
},
"response_id": "resp_cdac6a9b5e2a40027112e441ce6189db18c9040f96736407"
}
对于临时服务器和 mcp.json 服务器,你都可以使用 allowed_tools 字段限制模型可以调用的工具。这在你不想使用 MCP 服务器中的某些工具,或者为了减少模型接收的工具定义数量以加快提示词处理速度时非常有用。
curl https://:1234/api/v1/chat \
-H "Authorization: Bearer $LM_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "ibm/granite-4-micro",
"input": "What is the top trending model on hugging face?",
"integrations": [
{
"type": "ephemeral_mcp",
"server_label": "huggingface",
"server_url": "https://hugging-face.cn/mcp",
"allowed_tools": ["model_search"]
}
],
"context_length": 8000
}'
如果没有提供 allowed_tools,则来自服务器的所有工具均可供模型使用。
当使用需要身份验证的临时 MCP 服务器时,你可以传递自定义标头
curl https://:1234/api/v1/chat \
-H "Authorization: Bearer $LM_API_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "ibm/granite-4-micro",
"input": "Give me details about my SUPER-SECRET-PRIVATE Hugging face model",
"integrations": [
{
"type": "ephemeral_mcp",
"server_label": "huggingface",
"server_url": "https://hugging-face.cn/mcp",
"allowed_tools": ["model_search"],
"headers": {
"Authorization": "Bearer <YOUR_HF_TOKEN>"
}
}
],
"context_length": 8000
}'
本页源码可在 GitHub 上找到