LM Studio MCP服务器
一个MCP(模型上下文协议)服务器,为AI助手提供对LM Studio模型的控制。该服务器支持远程模型管理,包括通过LM Studio API列出、加载和卸载模型。
特性
- 健康检查:验证与LM Studio的连接
- 列出已下载的型号:查看LM Studio库中可用的所有LLM模型
- 列出已加载的型号:查看内存中当前加载了哪些模型
- 负荷模型:使用可配置参数将模型加载到内存中
- 卸载模型:从内存中删除特定模型实例
- 获取型号信息:检索有关已加载模型的详细信息
先决条件
- Node.js 18.0.0或更高版本
- LM Studio在启用本地服务器的情况下运行
安装```bash
Clone the repository
git clone
cd lm-studio-mcp-server
Install dependencies
npm install
## 配置
服务器使用环境变量连接到LM Studio:
|变量|默认值|描述|
| ------------------- | --------------------- | ----------------------------------------- |
| `LMSTUDIO_BASE_URL` |(派生)|LM Studio的完整WebSocket URL|
| `LMSTUDIO_HOST` | `127.0.0.1` |LM Studio主机(如果未设置BASE_URL,则使用)|
| `LMSTUDIO_PORT` | `1234` |LM Studio端口(如果未设置BASE_URL,则使用)|
## 用法
### 运行模式
**发展** (使用 `tsx` 用于TypeScript执行):```bash
npm start
# or with file watching
npm run dev
生产 (使用编译的JavaScript):```bash
npm run build
npm run start:prod
**码头工人**:```bash
# Pull the published image
docker pull portertech/lm-studio-mcp-server:latest
# Run (connects to LM Studio on host machine)
docker run -i --rm portertech/lm-studio-mcp-server:latest
# Run with custom LM Studio host
docker run -i --rm \
-e LMSTUDIO_HOST=192.168.1.100 \
-e LMSTUDIO_PORT=1234 \
portertech/lm-studio-mcp-server:latest
MCP客户端配置
克劳德
添加到您的 claude_desktop_config.json:
使用npx(建议用于已安装的软件包):```json
{
"mcpServers": {
"lmstudio": {
"command": "npx",
"args": ["@portertech/lm-studio-mcp-server"],
"env": {
"LMSTUDIO_HOST": "127.0.0.1",
"LMSTUDIO_PORT": "1234"
}
}
}
}
**利用当地发展:**```json
{
"mcpServers": {
"lmstudio": {
"command": "npx",
"args": ["tsx", "/path/to/lm-studio-mcp-server/src/index.ts"],
"env": {
"LMSTUDIO_HOST": "127.0.0.1",
"LMSTUDIO_PORT": "1234"
}
}
}
}
使用Docker:```json
{
"mcpServers": {
"lmstudio": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"-e",
"LMSTUDIO_HOST=127.0.0.1",
"-e",
"LMSTUDIO_PORT=1234",
"portertech/lm-studio-mcp-server:latest"
]
}
}
}
> **注:** 对于macOS/Windows上的Docker连接到主机上的LM Studio,请使用 `LMSTUDIO_HOST=host.docker.internal`.
## 可用工具
所有工具都返回一致的响应信封:```typescript
{
success: boolean;
message: string;
data?: T; // Present on success
error?: { // Present on failure
code: string;
message: string;
};
}
错误代码
| 代码 | 描述 |
|---|---|
MODEL_NOT_FOUND |
请求的模型不存在 |
MODEL_NOT_LOADED |
当前未加载模型 |
CONNECTION_FAILED |
无法连接到LM Studio |
INVALID_INPUT |
提供的参数无效 |
LOAD_FAILED |
加载模型失败 |
UNLOAD_FAILED |
卸载模型失败 |
UNKNOWN |
意外错误 |
health_check
检查与LM Studio服务器的连接。
参数:无
退货:连接状态和基本URL
list_models
列出LM Studio中可用的所有下载的LLM模型。
参数:无
退货:模型信息对象数组,包含:
modelKey:用于加载的型号标识符path:模型的相对路径displayName:人类可读的模型名称sizeBytes:大小(字节)architecture:模型架构(如果可用)quantization:量化类型(如果可用)
list_loaded_models
列出内存中当前加载的所有模型。
参数:无
退货:加载的模型信息数组,包含:
identifier:实例标识符modelKey:型号密钥path:模型路径displayName:人类可读名称sizeBytes:大小(字节)vision:模型是否支持愿景trainedForToolUse:模型是否经过工具使用培训
load_model
将模型加载到内存中。
参数:
model(必填):要加载的模型密钥(例如。,llama-3.2-3b-instruct)identifier(可选):加载实例的自定义标识符contextLength(可选):以令牌为单位的上下文窗口大小(最小值:1)evalBatchSize(可选):令牌处理的批量大小(最小值:1)
退货:加载模型详细信息(标识符、modelKey、路径)的成功状态
unload_model
从内存中卸载模型。
参数:
identifier(必填):要卸载的加载模型的标识符
退货:成功状态
get_model_info
获取加载模型的详细信息。
参数:
identifier(必填):加载模型的标识符
退货:模型详细信息,包括标识符、modelKey、路径、displayName、sizeBytes、contextLength
发展```bash
Build the project
npm run build
Run in development mode with auto-reload
npm run dev
Type check without emitting
npm run typecheck
Run tests
npm test
Lint
npm run lint
Format
npm run format:check
npm run format
### 发布过程
该项目包括 `make release` 自动发布命令:```bash
# Create a new release (runs CI, sets version, commits, tags, publishes to npm and Docker Hub)
make release VERSION=
# Example:
make release VERSION=1.0.5
这将运行完整的发布管道:
- CI检查(棉绒、类型检查、测试)
- 在中设置版本
package.json - 提交版本升级
- 创建带注释的git标签(
v) - 发布到npm
- 构建Docker镜像并将其推送到Docker Hub
项目结构```
src/
├── index.ts # MCP server entry point
├── client.ts # LM Studio client wrapper
├── types.ts # Shared types and result helpers
└── tools/
├── index.ts # Tool exports
├── health-check.ts # Health check tool
├── list-models.ts # List downloaded models
├── list-loaded-models.ts
├── load-model.ts
├── unload-model.ts
└── get-model-info.ts
```
建筑
- 一致的结果:所有工具返回相同的结果
ToolResult信封 - 安全包装:工具处理程序被包装以捕获异常并返回错误有效载荷
- 延迟配置:环境变量在运行时读取,而不是在模块加载时读取
- Singleton客户端:重用单个LM Studio客户端实例
许可证
国际协调委员会
