MCP获取流式HTTP服务器
MCP(模型上下文协议)服务器的原生Python实现,通过StreamableHTTP传输提供安全的URL获取功能。
概述
此服务器使用本机StreamableHTTP传输实现MCP协议,提供 fetch 该工具允许AI助手从具有内置安全保护的URL中检索内容。
主要特点
- 本地MCP实现:无需stdio桥接即可直接实现协议
- 流式HTTP传输:用于web本机部署的基于HTTP的传输
- 安全提取:SSRF保护、大小限制和robots.txt合规性
- 无状态操作:每个请求都是独立的,支持横向扩展
- 生产就绪:包括健康检查、正确的错误处理和安全措施
建筑
此服务作为MCP OAuth网关三层架构的一部分运行:
- 特拉菲克 (第1层):处理路由和身份验证
- 身份验证服务 (第2层):管理OAuth流和令牌验证
- MCP服务 (第3层):此服务-纯协议实现
该服务没有身份验证代码,所有身份验证都由Traefik的ForwardAuth中间件处理。
安装
使用pip
pip install mcp-fetch-streamablehttp-server使用 pixi
pixi add --pypi mcp-fetch-streamablehttp-serverDocker部署
FROM python:3.11-slim
# Install the package
RUN pip install mcp-fetch-streamablehttp-server
# Set environment variables
ENV MCP_SERVER_NAME=mcp-fetch
ENV MCP_SERVER_VERSION=1.0.0
ENV MCP_PROTOCOL_VERSION=2025-06-18
ENV HOST=0.0.0.0
ENV PORT=3000
# Expose the port
EXPOSE 3000
# Run the server
CMD ["python", "-m", "mcp_fetch_streamablehttp_server"]快速开始
环境变量
创建一个 .env 具有所需配置的文件:
# Required
MCP_SERVER_NAME=mcp-fetch
MCP_SERVER_VERSION=1.0.0
MCP_PROTOCOL_VERSION=2025-06-18
# Optional
MCP_FETCH_ALLOWED_SCHEMES=["http","https"]
MCP_FETCH_MAX_REDIRECTS=5
MCP_FETCH_DEFAULT_USER_AGENT=ModelContextProtocol/1.0 (Fetch Server)
HOST=0.0.0.0
PORT=3000使用Docker Compose运行
services:
mcp-fetch:
build: ./mcp-fetch-streamablehttp-server
env_file: .env
ports:
- "3000:3000"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/mcp", "-X", "POST",
"-H", "Content-Type: application/json",
"-d", '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"healthcheck","version":"1.0"}},"id":1}']
interval: 30s
timeout: 5s
retries: 3本地运行
# Using pip/pixi installation
mcp-fetch-server
# Or using Python module
python -m mcp_fetch_streamablehttp_server
# Using just and pixi (recommended for development)
just run mcp-fetch
# Or directly with pixi
pixi run -e mcp-fetch python -m mcp_fetch_streamablehttp_serverapi参考
StreamableHTTP 端点
- POST/mcp:处理JSON-RPC请求
- GET/mcp:SSE端点(基础设施就绪,尚未实施)
- 删除/mcp:会话终止(基础设施就绪)
MCP方法
初始化
协议握手和能力协商。
{
"jsonrpc": "2.0",
"method": "initialize",
"params": {
"protocolVersion": "2025-06-18",
"capabilities": {},
"clientInfo": {
"name": "example-client",
"version": "1.0"
}
},
"id": 1
}工具/列表
返回可用工具(目前仅 fetch).
{
"jsonrpc": "2.0",
"method": "tools/list",
"params": {},
"id": 2
}工具/调用
执行提取工具。
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "fetch",
"arguments": {
"url": "https://example.com",
"method": "GET",
"headers": {
"User-Agent": "Custom-Agent/1.0"
}
}
},
"id": 3
}提取工具
这 fetch 该工具使用以下参数从URL检索内容:
- 网址 (必填):要获取的URL
- 方法:HTTP方法(GET或POST,默认:GET)
- 标头:可选HTTP标头对象
- 身体:POST请求的可选请求体
- 最大长度:最大响应长度(字节)(默认值:100000)
- 用户代理:要使用的用户代理字符串(默认值:“ModelContextProtocol/1.0”)
安全功能
- SSRF保护:阻止对本地主机、专用网络和云元数据服务的请求
- 大小限制:默认情况下响应限制为100KB
- 方案限制:默认情况下只允许http/https
- 重定向限制:默认情况下最多5个重定向
响应格式
文本内容:
{
"content": [
{
"type": "text",
"text": "Response content here"
}
]
}图像内容:
{
"content": [
{
"type": "image",
"data": "",
"mimeType": "image/png"
}
]
}错误响应:
{
"content": [
{
"type": "text",
"text": "Error: Connection timeout"
}
],
"isError": true
}测试
使用项目的测试基础架构运行测试:
# Run all tests
just test
# Run specific tests
just test test_mcp_fetch
# Run with coverage
just test-sidecar-coverage测试应该验证没有模拟的真实协议交互:
- 通过初始化进行协议握手
- 工具列表和执行
- 安全边界验证
- 错误处理场景
部署
生产配置
- 在Traefik后面部署适当的布线标签
- 配置ForwardAuth中间件进行身份验证
- 通过.env文件设置所有环境变量
- 启用健康检查以进行监控
- 使用docker compose进行编排
健康监测
可以通过发送初始化请求来监视服务:
curl -X POST http://localhost:3000/mcp \
-H 'Content-Type: application/json' \
-d '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"monitoring","version":"1.0"}},"id":1}'具有匹配协议版本的成功响应表示运行状况良好。
发展
项目结构
mcp-fetch-streamablehttp-server/
├── src/
│ └── mcp_fetch_streamablehttp_server/
│ ├── __init__.py # Package initialization
│ ├── __main__.py # Entry point
│ ├── server.py # FastAPI application
│ ├── transport.py # StreamableHTTP transport
│ └── fetch_handler.py # Fetch tool implementation
├── pyproject.toml # Package configuration
├── CLAUDE.md # Divine implementation guide
└── README.md # This file添加功能
扩展此服务时:
- 保持无状态运行
- 通过MCP工具界面添加新工具
- 通过环境变量进行配置
- 包括安全验证
- 遵循现有的错误处理模式
安全考虑
- 从不添加身份验证码(由Traefik处理)
- 根据SSRF模式验证所有URL
- 限制响应大小以防止DoS
- 对所有HTTP操作使用超时控制
- 遵循最小特权原则
许可证
MCP OAuth网关项目的一部分。
