LMStudio MCP
模型控制协议(MCP)服务器,允许Claude通过LM Studio与本地运行的LLM模型通信。
概述
LMStudio MCP在Claude(具有MCP功能)和本地运行的LM Studio实例之间创建了一座桥梁。这使克劳德能够:
- 检查LM Studio API的运行状况
- 列出可用型号
- 获取当前加载的模型
- 使用您的本地模型生成聊天和原始文本完成
- 生成用于语义搜索和RAG的向量嵌入
- 通过响应ID进行有状态的多回合对话
- 使用锁定的系统提示启动并继续持续对话
这使您能够通过Claude的界面利用自己的本地运行模型,将Claude的功能与您的私有模型相结合。
先决条件
- Python 3.7+
- LM 工作室 已在本地安装并运行,模型已加载
- Claude拥有MCP访问权限
- 必需的Python包(请参阅安装)
🚀 快速安装
单线安装(推荐)
curl -fsSL https://raw.githubusercontent.com/infinitimeless/LMStudio-MCP/main/install.sh | bash手动安装方法
1.本地Python安装
git clone https://github.com/infinitimeless/LMStudio-MCP.git
cd LMStudio-MCP
pip install requests "mcp[cli]" openai2.Docker安装
# Using pre-built image
docker run -it --network host ghcr.io/infinitimeless/lmstudio-mcp:latest
# Or build locally
git clone https://github.com/infinitimeless/LMStudio-MCP.git
cd LMStudio-MCP
docker build -t lmstudio-mcp .
docker run -it --network host lmstudio-mcp3.Docker编写
git clone https://github.com/infinitimeless/LMStudio-MCP.git
cd LMStudio-MCP
docker-compose up -d有关详细的部署说明,请参阅 医生.md.
⚙️ 配置
该网桥支持针对不同部署场景的灵活配置:
- 默认:连接到
http://localhost:1234/v1 - 自定义主机:设置
LMSTUDIO_HOST环境变量(例如。,192.168.1.100) - 自定义端口:设置
LMSTUDIO_PORT环境变量(例如。,5678)
例子:
export LMSTUDIO_HOST=192.168.1.100
export LMSTUDIO_PORT=5678
python lmstudio_bridge.py📖 有关详细的配置选项,请参阅 配置.md
MCP配置
快速设置
直接使用GitHub(最简单):
{
"lmstudio-mcp": {
"command": "uvx",
"args": [
"https://github.com/infinitimeless/LMStudio-MCP"
]
}
}使用本地安装:
{
"lmstudio-mcp": {
"command": "/bin/bash",
"args": [
"-c",
"cd /path/to/LMStudio-MCP && source venv/bin/activate && python lmstudio_bridge.py"
]
}
}使用Docker:
{
"lmstudio-mcp-docker": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"--network=host",
"ghcr.io/infinitimeless/lmstudio-mcp:latest"
]
}
}有关完整的MCP配置说明,请参阅 MCP_CONFIGURATION.md.
可选:MCP description 提示
您可以添加 description 字段到您的 .mcp.json 条目,帮助Claude了解何时使用此服务器以及预期结果。这对于提醒Claude版本要求特别有用:
{
"lmstudio-mcp": {
"command": "...",
"args": [...],
"description": "Local LLM bridge via LM Studio. Use for private/offline inference, embeddings, and multi-turn conversations. start_conversation and continue_conversation require LM Studio v0.3.29+."
}
}🧠 LM Studio系统提示(推荐)
直接在LM Studio中设置系统提示可以使本地模型在所有交互中具有一致的基线个性和行为,而无需将其传递给每个API调用。
如何设置
- 打开 LM 工作室
- 单击聊天面板顶部的型号名称
- 查找 系统提示 字段(可能位于⚙️ 齿轮图标或 高级设置)
- 粘贴系统提示并保存
此处设置的系统提示适用于通过API发送的所有完成,包括来自此MCP桥的完成。
示例系统提示
一般助理——干净直接:
You are a helpful, concise assistant. Answer directly without preamble like
"Sure!" or "Of course!". Never cut off mid-sentence — always finish your thought.休闲对话伙伴:
You are a regular person having a relaxed conversation with a friend.
Keep responses short and natural, like real chat. No bullet points or formal
language. You can invent fun details about your life and stay consistent with them.
Never cut off mid-sentence — always finish your thought.本地编码助理:
You are an expert software engineer. Be concise and precise. When writing code,
always include brief inline comments. Prefer simple, readable solutions over
clever ones. Never cut off mid-sentence or mid-code block.隐私第一文档分析师:
You are a careful document analyst. Summarise accurately and concisely.
Never invent information not present in the source material.
Always flag uncertainty explicitly.💡 提示: 始终以“永远不要在句子中间截断——始终完成你的想法”结束你的系统提示。这可以防止截断响应,无论如何 max_tokens 已配置。用法
- 启动LM工作室 并确保它在端口1234(默认)上运行
- 设置系统提示 在LM Studio中(见上文——推荐)
- 加载模型 LM工作室
- 配置Claude MCP 使用上述配置之一
- 连接到MCP服务器 在克劳德提示时
可用工具
该桥提供以下9个工具:
| 工具 | 说明 |
|---|---|
health_check() | 验证LM Studio API是否可访问 |
list_models() | 获取LM Studio中所有可用型号的列表 |
get_current_model() | 确定当前加载的模型 |
chat_completion(prompt, system_prompt, temperature, max_tokens) | 从您的本地模型生成聊天响应 |
text_completion(prompt, temperature, max_tokens, stop_sequences) | 生成原始文本/代码完成——更快,无聊天格式开销 |
generate_embeddings(text, model) | 为语义搜索和RAG工作流生成向量嵌入 |
create_response(input_text, previous_response_id, reasoning_effort, stream, model) | 通过响应ID进行有状态的对话——需要LM Studio v0.3.29+ |
start_conversation(system_prompt, first_message, temperature, max_tokens, model) | 使用持久系统提示启动多回合会话--返回 response_id |
continue_conversation(response_id, message, temperature, max_tokens, model) | 继续以开始的会话 start_conversation --自动保存上下文 |
多回合对话工作流程
与本地模型进行持久对话的推荐方法:
1. start_conversation(
system_prompt="You are a friend at a bar, keep it casual and fun.",
first_message="Hey! How's it going?"
)
→ { response_id: "resp_abc...", message: "Hey! Not bad, just unwinding..." }
2. continue_conversation(
response_id="resp_abc...",
message="Work's been insane this week."
)
→ { response_id: "resp_def...", message: "Ugh, tell me about it..." }
3. continue_conversation(
response_id="resp_def...",
message="If you could go anywhere tomorrow, where would you go?"
)
→ { response_id: "resp_ghi...", message: "Honestly? Northern Portugal..." }系统提示在整个会话中都是锁定的,不需要每次都重新发送。 需要LM Studio v0.3.29+版本。
部署选项
此项目支持多种部署方法:
| 方法 | 用例 | 优点 | 缺点 |
|---|---|---|---|
| 本地Python | 开发,简单设置 | 快速,直接控制 | 需要Python设置 |
| 码头工人 | 隔离环境 | 干净、便携 | 需要Docker |
| Docker Compose | 生产部署 | 易于管理 | 更复杂的设置 |
| Kubernetes | 企业/规模 | 高度可扩展 | 配置复杂 |
| GitHub Direct | 零安装 | 无需本地安装 | 需要互联网 |
已知限制
- 某些型号(例如phi-3.5-mini-instruct_uncered)可能存在兼容性问题
- 网桥当前仅使用LM Studio的与OpenAI兼容的API终结点
- 模型响应将受到本地加载模型功能的限制
create_response,start_conversation,以及continue_conversation需要LM Studio v0.3.29+generate_embeddings需要嵌入特定模型(例如。text-embedding-nomic-embed-text-v1.5)
故障排除
API连接问题
如果Claude在尝试连接到LM Studio时报告404错误:
- 确保LM Studio正在运行并已加载模型
- 检查LM Studio的服务器是否在端口1234上运行
- 验证您的防火墙没有阻止连接
- 如果问题仍然存在,请尝试在API URL中使用“127.0.0.1”而不是“localhost”
模型兼容性
如果某些模型不能正常工作:
- 某些模型可能不完全支持OpenAI聊天完成API格式
- 为有问题的模型尝试不同的参数值(温度、max_tokens)
- 如果问题仍然存在,考虑切换到更兼容的模型
有关详细的故障排除帮助,请参阅 故障排除.md.
🐳 Docker和容器化
该项目包括全面的Docker支持:
- 多架构图像 (AMD64、ARM64/苹果硅)
- 自动化构建 通过GitHub操作
- 预构建图像 可在GitHub容器注册表上找到
- Docker Compose 便于部署
- Kubernetes清单 用于生产部署
看 医生.md 获取完整的集装箱化文件。
贡献
欢迎投稿!请参阅 贡献.md 作为指导方针。
许可证
麻省理工学院
致谢
该项目最初开发为“Claude-LMS工作室-桥梁_V2” 更名为“LMStudio MCP”并开源。
社区项目
您在寻找更高级的功能吗?查看社区构建的增强版本:
通过 @艾哈迈德·易卜拉欣085 --构建了一个强大的扩展 在这个项目的基础上,增加了自主代理循环、37个工具、动态MCP发现, 多模型路由、视觉支持等等。
______________________________________________________________________
🌟 如果这个项目对你有帮助,请考虑给它一颗星!
