使用说明
MCP多网关实验室
使用 MCP(模型上下文协议) 采用多服务器架构。该系统允许 克劳德桌面版 通过基于定义前缀路由请求的集中式网关与两个独立的业务域(销售和订单)进行交互。
目标
- 实施2个独立的MCP服务器
- 创建智能路由网关
- 向Claude Desktop展示4个功能工具
- 在PostgreSQL中持久化数据
- 适当处理错误和日志
系统架构
┌─────────────────────────────────┐
│ Claude Desktop (User) │
│ stdio interface │
└────────────┬────────────────────┘
│
▼
┌─────────────────────────────────┐
│ MCP Gateway (Node.js) │
│ Prefix-based routing: │
│ • sales_* → MCP Sales │
│ • orders_* → MCP Orders │
└──────┬──────────────────┬───────┘
│ │
▼ ▼
┌─────────────┐ ┌──────────────┐
│ MCP Sales │ │ MCP Orders │
│ Node/TypeScript │ Python/FastMCP│
│ 2 tools │ │ 2 tools │
└──────┬──────┘ └──────┬───────┘
│ │
└────────┬─────────┘
▼
┌──────────────────┐
│ PostgreSQL │
│ Port: 5434 │
│ • Table: sales │
│ • Table: orders │
└──────────────────┘
执行流程
- 用户 在Claude Desktop中键入查询
- 克劳德桌面版 确定所需的工具
- 网关 通过stdio接收请求
- 网关 基于前缀的路由(
sales_*或orders_*) - MCP服务器 对PostgreSQL执行查询
- 回应 沿着同一条路往回走
- 克劳德 向用户呈现结果
可用工具
域名:销售(前缀: sales_)
sales_total_previous_month
计算上一个日历月的汇总统计信息。
参数: 无
退货:
{
"success": true,
"month": "2025-09",
"total_transactions": 15,
"total_sales": 12450.50,
"average_sale": 830.03,
"min_sale": 45.00,
"max_sale": 2800.00
}
例子:
What were the total sales for last month?
sales_by_day
生成过去N天的每日销售时间序列。
参数:
n(数字,可选):要查询的天数(默认值:30)
退货:
{
"success": true,
"summary": {
"period_days": 30,
"total_days_with_sales": 28,
"total_sales_period": 25300.75,
"daily_average": 903.60
},
"daily_sales": [
{
"date": "2025-10-20",
"transactions": 3,
"total": 1275.50,
"average": 425.17,
"products": "Dell XPS Laptop, Logitech Mouse, HDMI Cable"
}
]
}
例子:
Give me the sales for the last 15 days
域名:订单(前缀: orders_)
orders_status_by_id
检索特定订单的状态和完整详细信息。
参数:
id(整数,必填):要查询的订单ID
退货:
{
"success": true,
"order": {
"id": 1,
"customer": "John Smith",
"amount": 1250.00,
"status": "completed",
"description": "Dell XPS 15 Laptop - Delivered",
"created_at": "2025-10-15T10:30:00"
}
}
例子:
Check the status of order ID 1
orders_create
在系统中创建初始状态为“待定”的新订单。
参数:
customer(字符串,必填):客户名称amount(数字,必填):订单总金额description(字符串,可选):订单描述
退货:
{
"success": true,
"message": "Order created successfully",
"order": {
"id": 11,
"customer": "Jane Doe",
"amount": 750.50,
"status": "pending",
"description": "Samsung Tablet + Case",
"created_at": "2025-10-20T18:45:30"
}
}
例子:
Create an order for Jane Doe with amount 750.50 and description "Samsung Tablet"
安装和设置
先决条件
步骤1:克隆存储库
git clone [REPOSITORY_URL]
cd mcp-multi-gateway
步骤2:设置数据库
# Start PostgreSQL with Docker
docker run --name postgres-mcp \
-e POSTGRES_PASSWORD=mcp_password \
-e POSTGRES_DB=mcp_lab \
-p 5434:5432 \
-d postgres:15
# Wait for it to start
sleep 5
# Run the SQL schema
docker exec -i postgres-mcp psql -U postgres -d mcp_lab < database/schema.sql
# Verify tables
docker exec -it postgres-mcp psql -U postgres -d mcp_lab -c "\dt"
步骤3:设置订单服务器(Python)
cd mcp-orders-py
# Create virtual environment
python -m venv venv
# Activate it
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Configure environment variables
cp .env.example .env
# Edit .env with your settings
# Test the server (optional)
python server.py
# Press Ctrl+C to stop
步骤4:设置销售服务器(Node/TypeScript)
cd ../mcp-sales-node
npm install
cp .env.example .env
# Edit .env with your settings
npm run build
# Test the server (optional)
npm start
# Press Ctrl+C to stop
步骤5:设置网关
cd ../mcp-gateway
npm install
# Test the gateway (optional)
node gateway.js
# Expected output:
# [GATEWAY-INFO] Connected successfully to Sales server
# [GATEWAY-INFO] Connected successfully to Orders server
# [GATEWAY-INFO] Gateway initialized with 2 backend(s)
# Press Ctrl+C to stop
步骤6:配置Claude桌面
-
找到配置文件:
- 窗户:
%APPDATA%\Claude\claude_desktop_config.json - 雨衣:
~/Library/Application Support/Claude/claude_desktop_config.json - Linux:
~/.config/Claude/claude_desktop_config.json
- 窗户:
-
编辑文件:
{
"mcpServers": {
"mcp-gateway": {
"command": "node",
"args": [
"[FULL_PATH_TO_PROJECT]/mcp-gateway/gateway.js"
]
}
}
}
Windows示例:
{
"mcpServers": {
"mcp-gateway": {
"command": "node",
"args": [
"C:\\Users\\youruser\\mcp-multi-gateway\\mcp-gateway\\gateway.js"
]
}
}
}
- 完全重新启动Claude Desktop
步骤7:验证安装
- 打开克劳德桌面
- 首选 设置 → 开发者 → MCP服务器
- 确认:
✅ mcp-gateway — Connected — 4 tools
测试
验证连接
首选 Settings → Developer → MCP Servers 并确认:
✅ mcp-gateway
Status: Connected
Tools: 4
销售额--上个月
What were the total sales for last month?
预期:回复总交易量、总销售额和平均值。
按日销售
Give me the sales for the last 20 days
预期:包含日期、交易计数和总计的每日条目数组。
订单--按ID查询
Check the status of order ID 1
预期:订单详细信息,包括客户、金额、状态和描述。
订单--创建
Create an order for "Test User" with amount 999.99 and description "System test"
预期: success: true,新订单ID,状态设置为 pending.
在数据库中验证:
docker exec -it postgres-mcp psql -U postgres -d mcp_lab \
-c "SELECT * FROM orders WHERE customer='Test User';"
项目结构
mcp-multi-gateway/
├── README.md
├── database/
│ └── schema.sql # DB schema and seed data
├── mcp-orders-py/ # Orders server (Python/FastMCP)
│ ├── server.py
│ ├── requirements.txt
│ └── .env.example
├── mcp-sales-node/ # Sales server (Node/TypeScript)
│ ├── src/
│ │ └── server.ts
│ ├── dist/
│ │ └── server.js
│ ├── package.json
│ ├── tsconfig.json
│ └── .env.example
└── mcp-gateway/ # MCP Gateway
├── gateway.js
└── package.json
许可证
该项目是为学术和学习目的而开发的。
致谢
- Claude Desktop的拟人化和MCP文档
- MCP开发人员社区提供示例和最佳实践