使用说明
使用天气MCP的代理AI
任务2:使用MCP工具构建代理AI
目标
设计并实施 代理人工智能系统 哪里:
- 一 代理 接受自然语言查询
- 代理人执行 意图检测与实体提取
- 代理人将执行委托给 一个MCP工具
- MCP工具检索 天气数据来自https://api.weather.gov/
- 代理格式化并返回 清晰、人类可读的响应
该项目展示了 使用模型上下文协议(MCP)模式的基于工具的代理架构.
系统架构
User Query
↓
Agent Client (Intent Detection + Entity Extraction)
↓
MCP Server (Tool Invocation Layer)
↓
Tool Registry
↓
Weather MCP Tool
↓
api.weather.gov
↓
Structured Weather Data
↓
Formatted Agent Response
组件说明
1.代理客户(test/client.js)
这 代理客户端 模拟能够执行以下操作的AI代理:
- 接受自然语言输入
- 检测用户意图
- 提取实体(城市名称)
- 选择合适的工具
- 调用MCP服务器
代理人职责
- 自然语言理解
- 意图检测
- 实体抽取
- 工具委派
- 响应格式
用户查询示例:
What is the weather in New York?
代理推理:
Intent: Weather Query
Entity: New York
Tool Selected: get_weather
2.MCP服务器(server.js)
这 MCP服务器 充当中央工具执行层。
责任:
- 接收工具调用请求
- 验证工具输入
- 将执行路由到正确的工具
- 将结构化结果返回给代理
请求示例:
POST /mcp
主体:
{
"tool": "get_weather",
"params": {
"city": "New York"
}
}
3.工具注册表(tools/toolRegistry.js)
这 工具注册表 管理所有可用的MCP工具。
责任:
- 注册可用工具
- 提供工具查找功能
- 启用动态工具选择
例子:
get_weather → weatherTool.js
这种设计允许将来轻松添加新工具。
4.天气MCP工具(tools/weatherTool.js)
这 天气工具 负责检索天气数据。
职责
- 接受城市输入
- 执行地理位置查找
- 致电美国国家气象局API
- 解析并组织响应
- 返回标准化天气数据
天气API来源
https://api.weather.gov/
返回给代理的示例响应:
{
"city": "New York, United States",
"temperature": "65°F",
"wind": "9 to 14 mph",
"condition": "Areas Of Fog then Mostly Cloudy"
}
使用的技术
- Node.js v22
- Express.js
- 本机获取API
- 美国国家气象局API(API.weither.gov)
安装
步骤1–克隆存储库
git clone https://github.com/Bhojrockzzz/weather-mcp-assignment.git
cd weather-mcp-assignment
步骤2–安装依赖项
npm install
运行项目
启动MCP服务器
node server.js
预期产量:
Weather MCP Server running on port 3000
运行代理客户端
打开另一个终端并运行:
node test/client.js
交互示例:
Ask the Weather Agent: What is the weather in New York?
输出示例:
Agent Reasoning Steps
--------------------------
Intent detected: weather
Selected Tool: get_weather
Extracted City: New York
Weather Report
--------------------------
City: New York, United States
Temperature: 65°F
Wind: 9 to 14 mph
Condition: Areas Of Fog then Mostly Cloudy
API测试(可选)
MCP服务器也可以使用以下工具进行测试 邮差或卷发.
端点
POST http://localhost:3000/mcp
请求体
{
"tool": "get_weather",
"params": {
"city": "New York"
}
}
示例响应
{
"success": true,
"tool": "get_weather",
"result": {
"city": "New York, United States",
"temperature": "65°F",
"wind": "9 to 14 mph",
"condition": "Areas Of Fog then Mostly Cloudy"
}
}
代理工作流摘要
- 用户输入自然语言查询
- Agent分析查询
- 代理检测到 天气意图
- 药剂提取物 城市实体
- 代理选择 get_weather工具
- MCP服务器调用天气工具
- 天气工具调用 api.weather.gov
- 响应返回给代理
- 代理为用户格式化响应
主要特点
- 基于代理的体系结构
- 意图检测
- 工具委派
- MCP风格的工具调用
- 外部API集成
- 模块化工具系统
- 可扩展架构
结论
该项目展示了 使用基于工具的架构构建的基本代理AI系统. 代理执行推理并将执行委托给MCP工具,该工具从美国国家气象局API检索实时天气数据。
模块化设计允许使用其他工具轻松扩展,从而创建更高级的 多工具人工智能代理.
