llama mcp服务器
MCP服务器将Claude Code桥接到本地llama.cpp。与Claude一起运行本地LLM进行实验、测试和成本效益推理。
需求
- Node.js 18+
- llama.cpp 和
llama-server建造 - GGUF模型文件
安装
npm install llama-mcp-server或者克隆并从源代码构建:
git clone https://github.com/ahays248/llama-mcp-server
cd llama-mcp-server
npm install
npm run build配置
通过环境变量进行配置:
| 变量 | 描述 | 默认值 |
|---|---|---|
LLAMA_SERVER_URL | llama服务器的URL | http://localhost:8080 |
LLAMA_SERVER_TIMEOUT | 请求超时(毫秒) | 30000 |
LLAMA_MODEL_PATH | GGUF模型文件的路径 | (无) |
LLAMA_SERVER_PATH | llama服务器二进制文件的路径 | llama-server |
使用Claude代码
选项1:插件安装(推荐)
由于a 已知Bug 在Claude Code中,非插件MCP服务器可以连接但不公开其工具。解决方法是通过本地市场将llama mcp服务器作为插件安装。
步骤1:创建市场结构
llama-marketplace/
├── .claude-plugin/
│ └── marketplace.json
└── plugins/
└── llama/
├── .claude-plugin/
│ └── plugin.json
└── .mcp.json步骤2:创建marketplace.json
// llama-marketplace/.claude-plugin/marketplace.json
{
"name": "llama-marketplace",
"description": "Local marketplace for llama.cpp MCP plugin",
"owner": {
"name": "Your Name"
},
"plugins": [
{
"name": "llama",
"description": "llama.cpp MCP server for local LLM inference",
"source": "./plugins/llama"
}
]
}步骤3:创建plugin.json
// llama-marketplace/plugins/llama/.claude-plugin/plugin.json
{
"name": "llama",
"version": "0.1.0",
"description": "llama.cpp MCP server for local LLM inference"
}步骤4:创建.mcp.json
// llama-marketplace/plugins/llama/.mcp.json
{
"mcpServers": {
"llama": {
"command": "npx",
"args": ["-y", "llama-mcp-server"],
"env": {
"LLAMA_SERVER_URL": "http://localhost:8080",
"LLAMA_MODEL_PATH": "/path/to/your/model.gguf",
"LLAMA_SERVER_PATH": "/path/to/llama-server"
}
}
}
}步骤5:安装插件
# Add the local marketplace
claude plugin marketplace add /path/to/llama-marketplace
# Install the plugin
claude plugin install llama@llama-marketplace
# Restart Claude Code重新启动后,工具将显示为 mcp__plugin_llama_llama__*.
选项2:直接MCP配置
注: 由于上述错误,此方法可能不起作用。如果添加服务器后没有出现工具,请使用选项1。
添加到您的Claude Code MCP配置中:
claude mcp add llama -e LLAMA_SERVER_URL=http://localhost:8080 -e LLAMA_MODEL_PATH=/path/to/model.gguf -e LLAMA_SERVER_PATH=/path/to/llama-server -- npx -y llama-mcp-server或手动添加到 ~/.claude.json:
{
"mcpServers": {
"llama": {
"command": "npx",
"args": ["-y", "llama-mcp-server"],
"env": {
"LLAMA_SERVER_URL": "http://localhost:8080",
"LLAMA_MODEL_PATH": "/path/to/your/model.gguf",
"LLAMA_SERVER_PATH": "/path/to/llama-server"
}
}
}
}工具
服务器工具
| 工具 | 说明 |
|---|---|
llama_health | 检查llama服务器是否正在运行并获取状态 |
llama_props | 获取或设置服务器属性 |
llama_models | 列出可用/已加载的型号 |
llama_slots | 查看当前插槽处理状态 |
llama_metrics | 获取与Prometheus兼容的指标 |
令牌工具
| 工具 | 说明 |
|---|---|
llama_tokenize | 将文本转换为令牌ID |
llama_detokenize | 将令牌ID转换回文本 |
llama_apply_template | 使用模特的模板格式化聊天消息 |
推理工具
| 工具 | 说明 |
|---|---|
llama_complete | 从提示生成文本完成 |
llama_chat | 聊天完成(兼容OpenAI) |
llama_embed | 为文本生成嵌入 |
llama_infill | 带前缀和后缀上下文的代码补全 |
llama_rerank | 根据与查询的相关性对文档进行重新排序 |
模型管理工具
| 工具 | 说明 |
|---|---|
llama_load_model | 加载模型(仅路由器模式) |
llama_unload_model | 卸载当前型号(仅路由器模式) |
LoRA工具
| 工具 | 说明 |
|---|---|
llama_lora_list | 列出已加载的LoRA适配器 |
llama_lora_set | 设置LoRA适配器刻度 |
工艺控制工具
| 工具 | 说明 |
|---|---|
llama_start | 将llama服务器作为子进程启动 |
llama_stop | 停止llama服务器进程 |
示例:启动llama服务器并运行推理
User: Start llama-server with my local model
Claude: I'll start llama-server for you.
[Uses llama_start tool with model path]
User: Generate a haiku about coding
Claude: Let me use the local model for that.
[Uses llama_complete tool]
Result:
Lines of code cascade
Through the silent morning hours
Bugs flee from the light发展
# Run tests
npm test
# Type check
npm run typecheck
# Build
npm run build
# Watch mode for development
npm run dev故障排除
工具未出现在Claude代码中
症状: 服务器在中显示“已连接” claude mcp list 但没有 llama_* 工具可用。
原因: Claude代码中的已知错误,非插件MCP服务器不公开工具(#12164).
解决方案: 使用插件安装方法(上面的选项1)。
某些工具的HTTP 501错误
某些工具需要特定的服务器配置:
| 工具 | 要求 |
|---|---|
llama_metrics | 使用启动llama服务器 --metrics 旗帜 |
llama_embed | 使用启动llama服务器 --embedding 标记或使用嵌入模型 |
llama_infill | 使用具有填充中间支持的模型(例如CodeLlama、DeepSeek Coder) |
llama_rerank | 使用重新分级模型 |
llama_load_model / llama_unload_model | llama服务器必须处于路由器模式 |
连接被拒绝错误
症状: Cannot connect to llama-server at http://localhost:8080
解决:
- 使用
llama_start启动服务器,或 - 手动启动llama服务器:
llama-server -m /path/to/model.gguf - 检查
LLAMA_SERVER_URL匹配llama服务器运行的位置
WSL/Windows路径问题
在WSL中运行时,确保路径使用Linux格式:
- ✓
/home/user/models/model.gguf - ✗
C:\Users\user\models\model.gguf
许可证
麻省理工学院
