AI代理与MCP集成

一个简单、模块化的AI代理,可以通过MCP服务器进行聊天和扩展功能。

  • by MuhammadMuazzain
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 muhammadmuazzain/mcp-ai-agent

使用说明

集成MCP的AI代理

一个简单的模块化AI代理,可以聊天并使用MCP(模型上下文协议)服务器实现各种功能。

特性

✅ 对话式AI -与客服自然交谈 ✅ MCP集成 -使用MCP服务器扩展功能
✅ 回声服务器 -用于调试MCP连接的测试服务器 ✅ 文件系统访问 -通过MCP读/写本地文件 🔜 航班API -准备好进行Amadeus/Dafel集成(需要API密钥)

快速开始

  1. 安装依赖项:
npm install
  1. 启动聊天界面:
npm run chat
  1. 运行测试:
npm run test

项目结构

ai-agent-mcp/
├── .claude/
│   └── claude_desktop_config.json  # Claude Desktop MCP config
├── config/
│   └── mcp-servers.json           # MCP server configurations
├── src/
│   ├── Agent.js                   # Main agent logic
│   ├── MCPClient.js              # MCP client wrapper
│   ├── chat.js                   # Interactive chat interface
│   └── test.js                   # Test script
├── workspace/                     # Filesystem MCP working directory
└── package.json

可用命令

与客服聊天时:

  • help -显示可用命令
  • show tools -列出MCP功能
  • echo [message] -测试回声服务器
  • read file "filename" -读取文件
  • write file "filename" with "content" -写入文件
  • list files -在工作区中显示文件
  • exit -退出聊天

添加新的MCP服务器

编辑 config/mcp-servers.json 要添加新服务器,请执行以下操作:

{
  "servers": {
    "your-server": {
      "enabled": true,
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-name"],
      "env": {
        "API_KEY": "your-key-here"
      },
      "description": "Your server description"
    }
  }
}

聊天会话示例

You > help
Agent > [Shows help menu with available commands]

You > echo Hello World!
Agent > 🔊 Echo server response: Hello World!

You > write file "notes.txt" with "My first note"
Agent > ✅ Successfully wrote to notes.txt

You > read file "notes.txt"
Agent > 📄 File contents of notes.txt:
My first note

You > list files
Agent > 📁 Files in workspace:
notes.txt
README.md

扩展代理

该项目设计为模块化:

  1. 添加新的MCP服务器 在……里面 config/mcp-servers.json
  2. 扩展代理.js 处理新的对话模式
  3. 更新MCPClient.js 用于自定义工具处理

故障排除

  • MCP服务器连接失败:确保npx可以下载并运行MCP服务器
  • 文件操作不起作用:检查 workspace 目录存在
  • 工具未显示:验证配置中启用了MCP服务器

未来的增强功能

  • []添加持久对话历史记录
  • []与OpenAI/Anthropic API集成
  • []添加更多MCP服务器(天气、数据库等)
  • []实施对话上下文管理
  • []添加web UI界面