内容
# MCP HTTP通信设置
本仓库演示了如何使用模型上下文协议 (MCP) 通过HTTP通信在客户端和服务器组件之间进行通信。
## 项目结构
- `weather/`: 提供天气工具的MCP Server
- `mcp-client-typescript/`: 连接到服务器并使用Claude API的MCP客户端
## 设置说明
### 1. 安装依赖
首先,构建服务器和客户端:
```bash
# 构建天气服务器
cd weather
npm install
npm run build
# 构建客户端
cd ../mcp-client-typescript
npm install
npm run build
```
### 2. 配置环境变量
确保在客户端的 `.env` 文件中设置您的API密钥:
```
# mcp-client-typescript/.env
ANTHROPIC_API_KEY=your_anthropic_api_key_here
MCP_SERVER_URL=http://localhost:3001
```
### 3. 运行HTTP服务器
使用HTTP传输启动天气MCP Server:
```bash
cd weather
npm run start:http
```
服务器将默认在 http://localhost:3001 上启动。
### 4. 运行HTTP客户端
在单独的终端中,启动MCP客户端:
```bash
cd mcp-client-typescript
npm run start:http
```
客户端将通过HTTP连接到服务器,并允许您通过Claude与天气工具进行交互。
## 测试
服务器和客户端都运行后,您可以在客户端终端中询问有关天气的问题:
- "What's the weather forecast for San Francisco?" (latitude 37.7749, longitude -122.4194)
- "Are there any weather alerts in CA?"
## 部署
这种基于HTTP的设置可以很容易地适应AWS Lambda和API Gateway:
1. 将服务器作为带有API Gateway的Lambda函数部署
2. 更新客户端以指向API Gateway URL
3. 将客户端部署为另一个Lambda函数或从任何环境中使用它