使用说明
MCP聊天客户端
一个智能聊天客户端,将MCP(模型上下文协议)工具与LLM功能集成在一起,具有LangChain支持的高级意图识别功能。
特性
- MCP工具集成:连接和使用MCP服务器及其工具
- 智能路由:使用LangChain根据用户查询自动选择相关工具
- LangChain意图识别:先进的文本处理和语义分析,实现精确的工具匹配
- LLM集成:与OpenAI兼容的API支持聊天和文本处理
- 会话管理:具有上下文保留功能的多回合对话
- 工具元数据管理:使用关键字映射对MCP工具进行自动索引
建筑
核心组件
- 增强型智能路由器:使用LangChain支持的意图识别智能路由查询
- LangChain文本处理器:高级文本分析、标记化和语义理解
- MCP经理:管理MCP服务器连接和工具发现
- 工具元数据服务:使用关键字映射存储和管理工具信息
- 数据库服务:用于工具元数据存储的PostgreSQL连接管理
技术栈
- 前端Next.js 15、React 19、TypeScript
- 后端:Next.js API路线
- 数据库:PostgreSQL用于工具元数据和配置存储
- 意图识别:用于高级文本处理和语义分析的LangChain
- LLM集成:与OpenAI兼容的API,用于聊天和文本处理
- 主控程序:用于工具集成的模型上下文协议
意图识别
当前架构(基于LangChain):
- 用于语义分析的LangChain文本处理器
- 基于规则的语义上下文意图识别
- 高级标记化和实体识别
先决条件
- Node.js 18+
- PostgreSQL 12+
- 与OpenAI兼容的LLM API终结点
安装
- 克隆存储库:
git clone <repository-url>
cd mcpchat
- 安装依赖项:
npm install
- 安装PostgreSQL:
# macOS
brew install postgresql
# Ubuntu/Debian
sudo apt-get install postgresql postgresql-contrib
- 配置LLM设置:
cp config/llm.json.example config/llm.json
编辑 config/llm.json:
{
"url": "https://your-llm-api.com/v1/chat/completions",
"apiKey": "your-api-key",
"timeout": 30000,
"maxTokens": 2000,
"temperature": 0.7,
"headers": {
"Content-Type": "application/json"
}
}
- 配置数据库:
cp config/database.json.example config/database.json
编辑 config/database.json:
{
"postgresql": {
"host": "localhost",
"port": 5432,
"database": "mcp_tools",
"user": "your-username",
"password": "your-password",
"ssl": false,
"pool": {
"min": 2,
"max": 10
}
}
}
- 在中配置MCP服务器
config/mcp.json:
{
"mcpServers": {
"your-server": {
"command": "node",
"args": ["path/to/server.js"]
}
}
}
数据库设置
应用程序将自动:
- 创建
mcp_tools如果数据库不存在 - 创建必要的表和索引
- 启动时索引MCP工具
手动设置(可选):
CREATE DATABASE mcp_tools;
运行应用程序
开发模式(自动初始化):
npm run dev
应用程序将自动:
- 如果需要,检查并创建数据库
- 初始化所有必需的表
- 将配置文件同步到数据库
- 验证系统完整性
对于手动数据库初始化:
npm run db:init
生产部署:
# Option 1: Full production setup (recommended)
npx tsc --noEmit #check error
npm run start:prod
# Option 2: Manual steps
npx tsc --noEmit #check error
npm run db:init # Initialize database
npm run build # Build the application
npm start # Start production server
# Option 3: With initialization
npm run start:init # Initialize database and start
打开 http://localhost:3000 在您的浏览器中。
配置
数据库配置(config/database.json)
- PostgreSQL:PostgreSQL连接设置
host:数据库主机port:数据库端口(默认值:5432)database:数据库名称(默认值:mcp_tools)user:数据库用户password:数据库密码ssl:启用SSL连接pool:连接池设置
LLM配置(config/llm.json)
配置LLM服务设置:
url:LLM API终结点(与OpenAI兼容)apiKey:API身份验证密钥timeout:请求超时(毫秒)(默认值:30000)maxTokens:每个响应的最大令牌数(默认值:2000)temperature:响应随机性0-1(默认值:0.7)headers:其他HTTP标头(可选)
备注:环境变量 LLM_URL 和 LLM_API_KEY 如果配置文件不可用,则仍支持作为回退。
MCP配置(config/mcp.json)
定义MCP服务器及其连接详细信息。支持stdio和HTTP传输。
配置示例:
{
"mcpServers": {
"gurddy-stdio": {
"name": "gurddy-stdio",
"transport": "stdio",
"command": "gurddy-mcp",
"args": [],
"env": {},
"disabled": false,
"autoApprove": ["solve_n_queens", "solve_sudoku"]
},
"example-http": {
"name": "example-http",
"transport": "http",
"url": "http://localhost:3001",
"timeout": 60000,
"retryAttempts": 3,
"retryDelay": 1000,
"disabled": false,
"autoApprove": []
}
}
}
运输类型:
stdio:基于流程的通信(默认)command:可执行命令args:命令参数env:环境变量
http:基于HTTP的通信url:MCP服务器URLtimeout:请求超时(毫秒)retryAttempts:重试尝试次数retryDelay:重试之间的延迟(毫秒)
看 MCP文件 了解详情。
运作原理
- 工具发现:MCP工具从配置的服务器加载
- 查询处理:使用LangChain文本处理来分析用户查询
- 工具选择:大多数相关工具都是基于语义分析选择的
- 执行:执行选定的工具并将结果返回给LLM
- 响应生成:LLM生成自然语言响应
API终点
POST /api/chat-发送聊天信息GET /api/tools-列出可用工具POST /api/tools/search-按查询搜索工具POST /api/tools/index-触发工具索引GET /api/config-获取系统配置POST /api/test-mcp-测试MCP连接(stdio/http)
测试MCP连接
访问 http://localhost:3000/test-mcp 以测试MCP服务器连接。
测试页面支持:
- stdio传输:基于测试过程的MCP服务器
- http传输:测试基于HTTP的MCP服务器
- 多种方法:初始化、工具/列表、工具/调用、ping
- 自定义参数:配置命令、参数、环境、URL等。
API使用示例:
# Test stdio MCP server
curl -X POST http://localhost:3000/api/test-mcp \
-H "Content-Type: application/json" \
-d '{
"transport": "stdio",
"command": "gurddy-mcp",
"args": [],
"env": {},
"method": "initialize",
"params": {}
}'
# Test HTTP MCP server
curl -X POST http://localhost:3000/api/test-mcp \
-H "Content-Type: application/json" \
-d '{
"transport": "http",
"url": "http://localhost:3001",
"method": "initialize",
"params": {}
}'
发展
运行PostgreSQL
docker组成-up-d
#docker-compose.yaml
version: '3'
services:
postgres:
image: postgres:16
container_name: postgres
restart: always
environment:
TZ: Asia/Shanghai
POSTGRES_USER: root
POSTGRES_PASSWORD: 123456
POSTGRES_DB: postgres
ports:
- "5432:5432"
volumes:
- /etc/localtime:/etc/localtime:ro
- pgdata:/var/lib/postgresql/data:rw
volumes:
pgdata:
driver: local
运行测试:
npm test
类型检查:
npm run type-check
Linting:
npm run lint
故障排除
数据库连接问题
- 确保PostgreSQL正在运行
- 验证中的凭据
config/database.json
MCP工具未加载
- 验证中的MCP服务器配置
config/mcp.json - 检查MCP服务器日志
- 确保服务器命令可执行
许可证
麻省理工学院
