人工智能驱动的MCP客户端
这是一个增强的模型上下文协议(MCP)客户端,与Ollama集成,提供人工智能聊天功能,可以访问MCP工具和资源。
特性
- 🤖 人工智能聊天:Ollama提供的自然语言交互
- 🔧 自动MCP工具使用:自动检测意图并执行工具
- 🎯 智能意图识别:理解“创建笔记”或“显示所有笔记”等自然请求
- 🌐 多服务器支持:同时连接到多个MCP服务器
- 🧠 增强的上下文记忆:从对话历史记录中记住实体(笔记、文件夹)
- 🔗 上下文参考:使用“更新提及的笔记”或“删除该笔记”等短语
- 📚 资源访问:跨服务器自动读取和利用MCP资源
- 💬 提示模板:使用来自任何连接服务器的预定义提示
- 🔄 模型切换:在不同的Olama车型之间切换
- 📝 扩展对话历史记录:最多可保存500条消息,以获得更好的上下文
- 🚀 零命令界面:只需自然地说话,无需学习命令!
- 🔍 调试模式:切换AI推理和工具选择的详细日志记录
- 🎛️ 智能刀具路径:自动将工具调用路由到适当的服务器
先决条件
- Node.js (v16或更高版本)
- 没有 已安装并正在运行
- 下载地址:https://ollama.ai - 确保它正在运行 http://localhost:11434
- 兼容的MCP服务器 配置在
config.json
- 系统同时支持多个MCP服务器 - 每个服务器在配置中都应该具有唯一的名称 - 为简单起见,此客户端仅使用stdio传输(不使用HTTP) - 对于测试和调试,您可以使用简单但功能齐全的MCP服务器:https://github.com/Oleg-Imanilov/js-notes-mcp
安装
- 克隆此存储库
- 安装依赖项:
npm install- 在中配置您的设置
config.json - 确保Ollama使用您喜欢的型号运行:
ollama pull qwen2.5:latest配置
编辑 config.json 要自定义设置,请执行以下操作:
{
"ollama": {
"host": "http://localhost:11434",
"defaultModel": "qwen2.5:latest",
"systemPrompt": "Your custom system prompt..."
},
"mcp": {
"server-name-1": {
"command": "node",
"args": ["path/to/your/first-mcp-server.js"],
"cwd": "working/directory"
},
"server-name-2": {
"command": "node",
"args": ["path/to/your/second-mcp-server.js"],
"cwd": "working/directory"
}
}
}多服务器支持
客户端现在支持同时连接到多个MCP服务器。每个服务器在配置中都由一个唯一的名称标识。系统自动:
- 聚合来自所有连接服务器的工具、资源和提示
- 将工具调用路由到相应的服务器
- 在工具/资源列表中提供服务器信息
- 优雅地处理特定于服务器的故障
用法
启动人工智能聊天
npm start或
node index.js聊天指令
一旦进入AI聊天模式,您可以使用以下命令:
/tools-列出所有连接服务器中可用的MCP工具/resources-列出所有连接服务器的可用MCP资源/prompts-列出所有连接服务器的可用提示/model-切换Ollama型号/clear-清晰的对话历史和实体跟踪/context-显示跟踪的实体和对话上下文/debug-切换内部AI调用的调试日志记录/help-显示帮助消息/exit-退出聊天
语境特征
AI客户端现在具有增强的上下文记忆功能,可以跟踪对话中提到的实体:
实体跟踪
- 备注:在创建、更新或提及笔记时记住笔记名称
- 文件夹:讨论存储位置时跟踪文件夹路径
- 对话历史:维护多达150条消息,以深入理解上下文
上下文参考
现在,您可以使用依赖于对话上下文的自然引用:
You: Create a note called "project status" with initial content
AI: ✅ Successfully created note "project status" with the content: "initial content"
You: Update mentioned note with the latest progress
AI: 🤖 Auto-executing tool: update_note with AI-extracted args: {"name": "project status"}
✅ Successfully updated note "project status" with new content: "the latest progress"
You: Delete that note
AI: 🤖 Auto-executing tool: delete_note with AI-extracted args: {"name": "project status"}
✅ Successfully deleted note "project status"上下文命令
- 使用
/context查看系统正在跟踪哪些实体 - 系统会自动记住:
- 记录创建、更新和引用中的名称 - 配置更改后的文件夹路径 - 提及频率和最后提及时间
智能参数提取
AI使用完整的对话上下文来提取参数:
- “更新提及注释” → 查找最近讨论的笔记
- “删除该注释” → 根据上下文识别注释
- “显示项目注释” → 如果之前提到了“项目”,则使用上下文
- “将其更改为…” → 从对话中理解“it”指的是什么
交互示例
自动笔记管理:
You: Create a note called "meeting agenda" with today's discussion points
AI: 🤖 Auto-executing tool: create_note
✅ Successfully created note "meeting agenda" with the content: "today's discussion points"
You: Show me all my notes
AI: 🤖 Auto-executing tool: get_all_notes
📋 Here are all your notes (2 total):
1. **meeting agenda** (modified: 7/8/2025)
today's discussion points
2. **project ideas** (modified: 7/6/2025)
AI integration concepts...
You: Update mentioned note with the final decisions
AI: 🤖 Auto-executing tool: update_note with AI-extracted args: {"name": "meeting agenda"}
✅ Successfully updated note "meeting agenda" with new content: "the final decisions"上下文参考:
You: Create a project note with initial brainstorming ideas
AI: ✅ Successfully created note "project" with the content: "initial brainstorming ideas"
You: Also create a budget note for the project costs
AI: ✅ Successfully created note "budget" with the content: "for the project costs"
You: Update that budget note with $50,000 estimate
AI: 🤖 Using conversation context to identify "budget" note
✅ Successfully updated note "budget" with new content: "$50,000 estimate"
You: Delete the project note, we're going with a different approach
AI: 🤖 Found "project" in conversation context
✅ Successfully deleted note "project"上下文跟踪:
You: /context
AI: 📊 Current Context Information:
💬 Conversation: 12 messages stored (up to 500 messages retained)
📝 Recently mentioned notes (2):
1. budget - mentioned 2 time(s), last: 7/8/2025, 3:15:22 PM
2. project - mentioned 3 time(s), last: 7/8/2025, 3:14:45 PM
💡 You can now use contextual references like "update mentioned note" or "delete that note"
Multi-server context tracking ensures references work across all connected servers支持多服务器的自然对话:
You: What can you help me with?
AI: I can help you manage notes, access resources, and more! I have access to tools from multiple servers. Just ask naturally like "create a note" or "show my notes".
You: Do we have any resources available?
AI: 📚 Auto-reading resources from all connected servers...
📚 Found resources from notes-server: notes://all
📚 Found resources from files-server: files://documents
📚 Resource contains data from multiple servers...多服务器工具用法:
You: Create a note and also backup my files
AI: 🤖 Auto-executing tools from multiple servers:
- notes-server.create_note with content: "backup my files"
- files-server.backup_files with default settings
✅ Successfully created note and initiated backup across servers可用型号
客户支持任何Olama型号。热门选项包括:
qwen2.5:latest(示例来自配置)llama3.2llama3.1mistralcodellamallama2
使用以下方式切换型号 /model 在聊天中。
高级功能
调试模式
使用 /debug on 以实现内部AI推理和工具分析的详细记录。这有助于理解系统如何解释您的请求并做出工具执行决策。
多服务器工具路由
当多个服务器提供类似的工具时,系统会根据上下文和工具功能智能地将请求路由到最合适的服务器。
增强的上下文管理
该系统可维护多达500条对话消息(可配置),并通过复杂的基于LLM的分析来跟踪实体,以实现上下文理解。
LLM电动工具分析
该系统使用复杂的基于LLM的分析来理解用户意图并自动执行适当的工具。这是一个多步骤的过程:
意图检测
系统分析用户消息,以确定它们是否表示希望使用MCP工具:
- 自然语言处理:使用LLM从自然语言中理解用户意图
- 情境感知:考虑对话历史中的模糊引用
- 信心评分:仅在置信度高(≥70%)时执行工具
刀具选择和参数提取
当检测到工具使用时,系统:
- 确定合适的工具:将用户意图与所有服务器上的可用工具相匹配
- 提取参数:使用对话上下文填写工具参数
- 处理歧义:解决“该注释”或“提及的项目”等引用
执行流程
- 用户输入分析:LLM分析用户的消息以了解工具使用意图
- 工具执行:如果检测到,将自动调用相应的工具
- 响应生成:LLM根据工具输出创建用户友好的响应
- 上下文更新:更新对话历史和实体跟踪
示例分析过程
User: "Show me all my notes"
→ LLM detects: get_all_notes tool needed
→ System executes: get_all_notes()
→ LLM generates: "Here are all your notes: [formatted output]"这创造了一种无缝的体验,用户可以自然地说话,而不需要知道特定的命令或工具名称。
故障排除
Ollama连接问题
- 确保Ollama正在跑步:
ollama serve - 检查您的型号是否可用:
ollama list - 如果需要,拉动模型:
ollama pull qwen2.5:latest
MCP服务器问题
- 验证中的MCP服务器路径
config.json - 检查MCP服务器是否可执行且可访问
- 查看服务器日志中的连接错误
- 使用
/debug on查看详细的服务器交互日志
多服务器问题
- 如果一台服务器发生故障,系统将继续使用其他服务器
- 检查中的单个服务器配置
config.json - 验证每个服务器的命令路径和工作目录
- 如果遇到问题,请单独测试服务器
AI集成问题
- 确保Olama型号支持您正在使用的功能
- 尝试切换型号
/model - 使用
/debug on了解AI推理和工具选择过程 - 检查对话上下文
/context命令
常用命令
# Check Ollama status
ollama list
# Pull a new model
ollama pull qwen2.5:latest
# Start Ollama (if not running as service)
ollama serve建筑
客户端由几个关键模块组成:
核心文件
index.js-多服务器初始化的主要入口点config.json-Ollama和MCP服务器的配置
源模块(src/)
AIChatLoop.js-具有命令处理功能的AI聊天界面OllamaIntegration.js-Ollama与LLM电动工具分析的高级集成MultiServerClient.js-多服务器MCP客户端包装和工具路由client-helpers.js-用于测试和探索的MCP客户端实用程序功能utils.js-终端颜色实用程序和格式助手
数据目录(data/)
notes_storage.json-票据MCP服务器的数据存储示例
测试目录(tests/)
test-auto-execution.js-自动工具执行测试test-debug-command.js-调试命令功能测试test-debug.js-调试功能测试test-patterns.js-模式匹配测试test-simple.js-基本功能测试
关键架构特征
多服务器支持
MultiServerClient:管理与多个MCP服务器的连接- 工具聚合:将所有服务器的工具与服务器标识相结合
- 智能路由:自动将工具调用路由到适当的服务器
- 故障弱化:如果某些服务器发生故障,则继续工作
增强的AI集成
OllamaIntegration:精密的LLM驱动工具检测和执行- 意向分析:使用LLM分析用户消息以了解工具使用意图
- 上下文跟踪:维护对话历史和实体提及
- 自动执行:根据用户意图自动执行工具
- 响应生成:从工具输出中创建用户友好的响应
会话管理
- 实体跟踪:记住笔记、文件夹和其他提到的实体
- 上下文参考:解决“该说明”、“提及的项目”等问题。
- 扩展历史:可维护多达500条消息以提供深层上下文
- 调试日志记录:人工智能推理过程的可选详细日志记录
技术细节
ES模块支持
该项目使用现代ES模块(type: "module" 在package.json中)使用:
- 贯穿始终的导入/导出语法
- 正确的文件扩展名(
.js)进口 - 根据需要进行动态导入
依赖项
@modelcontextprotocol/sdk:MCP协议实施ollama:Ollama API集成- 内置Node.js模块:
fs,path,readline,url
错误处理
- 服务器连接失败
- 强大的工具执行错误处理
- AI分析失败的回退机制
- 详细的错误记录和用户反馈
贡献
欢迎通过以下方式做出贡献:
- 为多服务器架构添加新功能
- 改进人工智能集成和LLM驱动的工具分析
- 增加对更多Olama车型的支持
- 增强用户界面和聊天体验
- 改进错误处理和弹性
- 添加新功能测试
- 优化大型对话历史的性能
- 扩展上下文跟踪功能
许可证
该项目根据ISC许可证获得许可。
