内容
# 服务器端MCP服务器
MCP(模型上下文协议)服务器的最小化、生产就绪示例,在AWS Lambda上运行,使用**Streamable HTTP**传输协议,通过**API Gateway HTTP API v2**部署。
使用官方的[`@modelcontextprotocol/sdk`](https://github.com/modelcontextprotocol/typescript-sdk)构建 - 没有stdio适配器,没有代理。
## 架构
```
AI 客户端 (Cursor / Windsurf / Claude Code / VS Code)
│
│ POST /mcp x-api-key: <密钥>
▼
API Gateway HTTP API v2
│
├─ Lambda 授权器 ← 验证 x-api-key 对 Secrets Manager
│
▼
Lambda (Docker 镜像, Node.js 22)
│
│ Lambda Web 适配器转换 Lambda 事件 → HTTP
▼
Node.js HTTP 服务器 (MCP SDK StreamableHTTPServerTransport)
```
**设计无状态** - 每个请求都会创建一个新的MCP服务器 + 传输实例。无会话状态,无DynamoDB,无冷启动协调需求。
### 堆栈
| 层 | 技术 |
|---|---|
| 基础设施 | AWS CDK v2 (TypeScript) |
| API | API Gateway HTTP API v2 |
| 运行时 | Lambda - Docker 镜像 (Node.js 22) |
| 授权器 | Lambda - `NodejsFunction` esbuild 包 (Node.js 22) |
| 认证 | API 密钥存储在 Secrets Manager 中,按请求验证 |
| MCP 传输 | `StreamableHTTPServerTransport` (无状态,JSON 响应模式) |
| Lambda ↔ HTTP 桥接 | [AWS Lambda Web Adapter](https://github.com/aws/aws-lambda-web-adapter) |
### 包含的示例工具
| 工具 | 描述 |
|---|---|
| `get_weather` | 返回城市的模拟天气 |
| `calculate` | 基本算术(加 / 减 / 乘 / 除) |
| `get_server_status` | 服务器状态,时间戳,AWS 区域 |
| `lookup_user` | 按 ID 模拟用户查找 |
用 [`app/src/server.ts`](app/src/server.ts) 中的实际工具实现替换这些工具。
## 先决条件
- Node.js 22+
- Docker(运行中)
- 配置了 AWS CLI - `aws sts get-caller-identity` 应该成功
- 在目标区域中已引导 CDK:
```bash
npx cdk bootstrap aws://YOUR_ACCOUNT_ID/YOUR_REGION
```
## 设置
**1. 安装依赖项**
```bash
npm install
cd app && npm install && cd ..
cd lib/authorizer && npm install && cd ../..
```
**2. 配置 API 密钥**
```bash
cp .env.example .env
```
生成一个强密钥并将其粘贴到 `.env` 中:
```bash
openssl rand -hex 32
# → 将输出粘贴为 API_KEY=... 在 .env 中
```
**3. 部署**
```bash
npx cdk deploy
```
CDK 构建 Docker 镜像,将其推送到 ECR,创建 Lambda 函数,连接 API Gateway,并将您的密钥存储在 Secrets Manager 中。端点 URL 将作为输出打印:
```
输出:
McpServerStack.McpEndpoint = https://<id>.execute-api.<region>.amazonaws.com/mcp
```
## 本地测试
```bash
cd app && npm run build && node dist/server.js
```
```bash
# 初始化
curl -s -X POST http://localhost:8080/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","method":"initialize","params":{"clientInfo":{"name":"test","version":"1.0"},"protocolVersion":"2025-03-26","capabilities":{}},"id":1}' | jq .
# 列出工具
curl -s -X POST http://localhost:8080/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","method":"tools/list","id":2}' | jq .
# 调用工具
curl -s -X POST http://localhost:8080/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"get_weather","arguments":{"city":"Kyiv"}},"id":3}' | jq .
```
## 测试已部署的端点
```bash
MCP_URL="https://<id>.execute-api.<region>.amazonaws.com/mcp"
API_KEY="<您的 .env 中的密钥>"
curl -s -X POST "$MCP_URL" \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-H "x-api-key: $API_KEY" \
-d '{"jsonrpc":"2.0","method":"tools/list","id":1}' | jq .
# 应返回 403
curl -s -X POST "$MCP_URL" \
-H "Content-Type: application/json" \
-H "x-api-key: wrong-key" \
-d '{"jsonrpc":"2.0","method":"tools/list","id":2}'
```
对于交互式调试,请使用 [MCP Inspector](https://github.com/modelcontextprotocol/inspector):
```bash
npx @modelcontextprotocol/inspector
```
选择 **Streamable HTTP**,输入端点 URL,添加 `x-api-key` 头。
## 连接客户端
### Cursor
`.cursor/mcp.json`:
```json
{
"mcpServers": {
"my-server": {
"url": "https://<id>.execute-api.<region>.amazonaws.com/mcp",
"headers": { "x-api-key": "<API_KEY>" }
}
}
}
```
### Windsurf
`~/.windsurf/mcp.json`(或工作区 `.windsurf/mcp.json`):
```json
{
"mcpServers": {
"my-server": {
"url": "https://<id>.execute-api.<region>.amazonaws.com/mcp",
"headers": { "x-api-key": "<API_KEY>" }
}
}
}
```
### VS Code
`.vscode/mcp.json`:
```json
{
"servers": {
"my-server": {
"type": "sse",
"url": "https://<id>.execute-api.<region>.amazonaws.com/mcp",
"headers": { "x-api-key": "<API_KEY>" }
}
}
}
```
> VS Code 使用 `"type": "sse"` - 它尚不支持 `"type": "http"`(Streamable HTTP)。
### Claude Code
```bash
claude mcp add my-mcp-server --transport sse "$MCP_URL" \
--header "x-api-key: $API_KEY"
```
## 添加您自己的工具
编辑 [`app/src/server.ts`](app/src/server.ts) 并在 `createMcpServer()` 中添加工具:
```typescript
server.tool(
"my_tool",
"此工具的作用",
{ param: z.string().describe("参数") },
async ({ param }) => ({
content: [{ type: "text", text: `结果为 ${param}` }],
})
);
```
然后使用 `npx cdk deploy` 重新部署。
## 清理
```bash
npx cdk destroy
```
## 为什么使用 Lambda Web Adapter?
`StreamableHTTPServerTransport` 期望标准的 Node.js `IncomingMessage`/`ServerResponse` 对象,而不是 Lambda 的 JSON 事件格式。[Lambda Web Adapter](https://github.com/aws/aws-lambda-web-adapter) 作为 Lambda 扩展运行,透明地将 Lambda 调用转换为本地服务器的 HTTP 请求 - 无需更改应用程序代码。
## 为什么是无状态?
Lambda 实例是短暂的,可能不会在请求之间重用。状态ful MCP 会话(带有会话 ID)需要外部持久化传输状态(例如 DynamoDB),这会增加复杂性。无状态模式(`sessionIdGenerator: undefined`)使每个请求完全自包含 - 是 Lambda 的自然适配。
> **注意:** API Gateway HTTP API v2 有严格的 30 秒集成超时。所有工具调用必须在此窗口内完成。
MCP 配置
以下是此 MCP Server 的配置信息,可直接复制到 Cursor 或其他 MCP 客户端中使用。
mcp.json
连接信息
猜你喜欢
rea
用于编码代理的逆向工程:一个 CLI 和 MCP 服务器,用于检查任何应用程序,了解其功能,并按照您的方式构建它们。
everything-claude-code
完整的 Claude Code 配置集合 - agents、skills、hooks、commands、rules、MCPs。来自 Anthropic...
Firecrawl
Firecrawl MCP Server 实现了网页抓取、爬虫和内容提取功能。
MarkItDown MCP
MarkItDown-MCP是一个轻量级服务器,用于将URI转换为Markdown。
cc-switch
跨平台的Claude Code、Codex和Gemini CLI一体化助手。
Agent-Reach
赋予你的AI Agent观察整个互联网的眼睛。读取并搜索...