mcp本地llm
MCP服务器,允许Claude Code将机械任务委托给本地LLM。克劳德负责思考;您的本地模型处理繁重的工作——摘要、分类、提取、起草。
这是 不 克劳德的替代者。这是一个成本优化层。Claude保持控制,决定委派什么,并审查输出。局部模型只做体积工作,不需要边界推理。
建筑
Claude Code作为编排者坐在最上面。它通过mcp调用mcp本地llm工具,mcp将请求转发到本地运行的Ollama(或任何与OpenAI兼容的后端)。克劳德决定委派什么,并审查返回的内容。
需求
- 奥拉玛 已安装并正在运行
- Node.js 18+
- Claude Code(或任何兼容MCP的客户端)
设置
1.安装Ollama并拉动模型
# Install Ollama (macOS)
brew install ollama
# Start the Ollama service
ollama serve
# Pull the default model
ollama pull qwen2.5-coder:7b2.克隆和构建
git clone https://github.com/aplaceforallmystuff/mcp-local-llm.git
cd mcp-local-llm
npm install
npm run build3.添加到克劳德代码
claude mcp add local-llm -s user -- node /path/to/mcp-local-llm/dist/index.js或手动添加到 ~/.claude.json:
{
"mcpServers": {
"local-llm": {
"command": "node",
"args": ["/path/to/mcp-local-llm/dist/index.js"]
}
}
}4.验证
在克劳德法典中 local_status 该工具应显示您的Ollama连接和可用型号。
可用工具
local_summarize
使用本地LLM总结文本。
参数:
text(必填):总结文本style:"brief"|"detailed"|"bullet_points"|"executive"max_length:大约最大字数(默认值:150)focus:需要强调的具体方面
用途: 大量总结、浓缩研究、会议记录
local_draft
生成初稿以供改进。
参数:
task(必填):起草什么context(必填):背景和要求format:输出格式(markdown、纯文本等)tone:期望的语气(专业、休闲、技术)
用途: 样板、初始内容、基于模板的生成
local_classify
将文本分类。
参数:
text(必填):要分类的文本categories(必填):可能类别的数组allow_multiple:允许多个类别(默认值:false)explain:包括解释(默认值:false)
用途: 排序、标记、组织内容
local_extract
从文本中提取结构化信息。
参数:
text(必填):要提取的文本fields(必填):要提取的字段数组output_format:"json"|"yaml"|"markdown_table"
用途: 解析文档、数据提取
local_transform
根据说明转换文本。
参数:
text(必填):要转换的文本instruction(必填):转换说明
用途: 格式化、样式转换、简单重写
local_complete
原始完成,实现最大灵活性。
参数:
prompt(必填):提示system:系统消息max_tokens:最大令牌数(默认值:2048)temperature:温度(默认值:0.7)
用途: 不适合其他工具的自定义任务
local_status
检查本地LLM连接状态和可用型号。
配置
环境变量(所有可选--默认值适用于标准Ollama安装):
| 变量 | 默认值 | 描述 |
|---|---|---|
LOCAL_LLM_BASE_URL | http://localhost:11434/v1 | API终点 |
LOCAL_LLM_MODEL | qwen2.5-coder:7b | 要使用的模型 |
LOCAL_LLM_MAX_TOKENS | 2048 | 默认最大令牌数 |
LOCAL_LLM_TEMPERATURE | 0.7 | 默认温度 |
替代方案:Docker模型运行器
如果你更喜欢Docker Model Runner而不是Ollama:
# Enable Model Runner with TCP access
docker desktop enable model-runner --tcp=12434
# Pull a model
docker model pull ai/gemma3:latest然后设置环境变量:
export LOCAL_LLM_BASE_URL="http://localhost:12434/engines/v1"
export LOCAL_LLM_MODEL="ai/gemma3:latest"任何公开与OpenAI兼容的API的后端都可以工作。
代表团理念
| 克劳德做 | 本地模型做 |
|---|---|
| 复杂推理 | 批量总结 |
| 架构决策 | 锅炉板生成 |
| 质量审查 | 文本提取/格式化 |
| 解决新问题 | 简单分类 |
| 最终编辑 | 初始草稿生成 |
克劳德评论,本地模式产生。局部模型处理体积;克劳德负责质量控制。
教克劳德委派
一旦MCP服务器运行,Claude Code可以自动调用这些工具。但克劳德不会知道 *当* 除非你告诉它,否则不要使用它们。以下是设置方法。
选项1:将路由指令添加到CLAUDE.md
最可靠的方法。将委派规则添加到您的项目或全局 CLAUDE.md 文件,这样克劳德每次会话都会应用它们:
## Local LLM Routing
Route mechanical tasks to local models via `mcp__local-llm__*` tools to save API costs.
| Task Type | Tool | Example |
|-----------|------|---------|
| Summarizing content | `local_summarize` | Condensing research notes |
| Initial drafts | `local_draft` | Boilerplate emails, docstrings |
| Classification | `local_classify` | Sorting items, tagging content |
| Data extraction | `local_extract` | Parsing structured data from text |
| Text transformation | `local_transform` | Formatting changes, style conversions |
| Custom simple tasks | `local_complete` | Any mechanical task with clear instructions |
### When NOT to Route Locally
- Complex reasoning or analysis
- Security-sensitive operations
- Tasks requiring multi-step tool chains
- Content requiring voice/style judgment
- Anything you specifically want Claude's opinion on选项2:直接问克劳德
你可以告诉克劳德在对话中委派:
"Summarize these 20 files using the local model"
"Use local_classify to sort these inbox items into categories: work, personal, spam"
"Draft a README section using local_draft, then review and clean it up yourself"Claude将调用MCP工具并在向您展示之前查看输出。
选项3:将其构建为自定义代理
如果您使用Claude Code的代理系统,请在代理定义中添加委托说明:
## Agent: inbox-processor
When classifying inbox items, use `local_classify` to categorize each item
before deciding where to file it. Use `local_summarize` to create brief
descriptions for items that need them.实际例子
批量汇总研究文件:
"Read all the markdown files in ./research/ and use local_summarize to create
a bullet-point summary of each one. Compile the summaries into a single document."对内容进行分类和排序:
"I have 30 notes in my inbox folder. Use local_classify with categories
[project, reference, action-item, archive] to sort them, then move each
file to the appropriate folder."起草并完善工作流程:
"Use local_draft to generate initial docstrings for all exported functions
in src/. Then review each one and fix anything that's inaccurate or unclear."提取结构化数据:
"Extract the name, email, company, and role from each of these email signatures
using local_extract. Output as a JSON array."模式总是一样的:本地模型产生,克劳德评论。你可以节省7B型号的批量工作成本,克劳德的判断是质量门。
故障排除
“连接被拒绝”或状态显示错误
- 检查Ollama是否正在运行:
ollama list - 如果需要,启动它:
ollama serve - 验证端口:
curl http://localhost:11434/v1/models
“找不到模型”
- 拉动模型:
ollama pull qwen2.5-coder:7b - 或者通过以下方式设置不同的模型
LOCAL_LLM_MODEL
使用不同的后端
- 集
LOCAL_LLM_BASE_URL到后端的OpenAI兼容端点 - 集
LOCAL_LLM_MODEL到后端支持的模型
工具未出现在Claude代码中
- 验证MCP服务器是否已配置:
claude mcp list - 检查路径
dist/index.js是正确的 - 如有需要,进行重建:
npm run build
许可证
麻省理工学院
