使用说明
web获取mcp
一种模型上下文协议(MCP)服务器,提供 web_fetch 用于发出具有可配置安全约束的HTTP请求的工具。
特性
- 单
web_fetch工具 -近似 获取API 具有类似的界面。灵感来自Claude Code的内部 Web提取. - 可流式HTTP传输 -作为可通过网络访问的HTTP服务器运行
- 安全控制:
- MCP客户端到服务器身份验证的共享密钥
- URL列表
- HTTP方法的allowlist
- Headers的allowlist,通过工具传递
headers参数 - 从MCP请求的标头传递的Headers的allowlist
- 请求超时
- 响应大小限制
安装
npm install
npm run build
用法
启动服务器
# Start with default settings (auto-generated secret, localhost-only URLs)
node build/index.js
# Start with custom configuration
node build/index.js \
--secret=my-secret-key \
--port=3000 \
--allowed-url-regex="^https?://api\.example\.com/.*" \
--allowed-methods=GET,POST \
--allowed-tool-header-names=User-agent,Content-Type \
--allowed-passthrough-header-names=Authorization \
--fetch-timeout=30 \
--fetch-max-response-size=100
CLI选项
| 选项 | 默认值 | 描述 |
|---|---|---|
--secret=<string> |
自动生成 | 身份验证共享密钥 |
--port=<number> |
3000 |
HTTP服务器端口 |
--allowed-url-regex=<regex> |
^http://localhost(:[0-9]+)?(/.*)?$ |
允许的URL的正则表达式模式 |
--allowed-methods=<csv> |
GET |
允许的HTTP方法的逗号分隔列表 |
--allowed-tool-header-names=<csv> |
Content-Type,Accept |
工具允许的逗号分隔的标题列表 headers 参数(不区分大小写) |
--allowed-passthrough-header-names=<csv> |
无 | 允许从MCP SSE请求传递的逗号分隔的标头列表(不区分大小写) |
--fetch-timeout=<seconds> |
30 |
请求超时(秒) |
--fetch-max-response-size=<KB> |
100 |
最大响应大小(KB) |
MCP工具: web_fetch
认证
对MCP服务器的所有请求必须包括 X-WebFetchMcp-Secret 带有正确机密的标头:
curl -H "X-WebFetchMcp-Secret: your-secret-key" ...
参数
url(string,必填):要获取的URLmethod(字符串,可选):HTTP方法(默认:GET)headers(对象,可选):要包含的HTTP标头body(string,可选):POST/PUT请求的请求体
示例请求
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-H "X-WebFetchMcp-Secret: your-secret-key" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "web_fetch",
"arguments": {
"url": "http://localhost:8080/api/data",
"method": "GET",
"headers": {
"Authorization": "Bearer token123",
"Content-Type": "application/json"
}
}
}
}'
响应格式
成功的文本/JSON响应:
{
"content": [
{
"type": "text",
"text": "{\"result\": \"data\"}",
"mimeType": "application/json"
}
],
"structuredContent": {
"url": "http://localhost:8080/api/data",
"retrievedAt": "2025-11-01T12:00:00Z",
"statusCode": 200,
"headers": {
"content-type": "application/json",
"content-length": "1024"
}
}
}
成功的二进制/图像响应:
{
"content": [
{
"type": "image",
"data": "base64-encoded-data...",
"mimeType": "image/png"
}
],
"structuredContent": {
"url": "http://localhost:8080/image.png",
"retrievedAt": "2025-11-01T12:00:00Z",
"statusCode": 200
}
}
错误响应:
{
"content": [
{
"type": "text",
"text": "Failed to fetch URL: connection refused"
}
],
"structuredContent": {
"url": "http://localhost:8080/api/data",
"retrievedAt": "2025-11-01T12:00:00Z",
"statusCode": -1
},
"isError": true
}
安全
服务器实现了多个安全层:
- 安全认证码:所有MCP请求必须包括
X-WebFetchMcp-SecretHTTP标头与服务器的机密相匹配。没有此标头或具有不正确秘密的请求在到达MCP层之前会被拒绝,并出现401错误。 - 网页过滤:只允许与正则表达式模式匹配的URL
- 方法筛选:只允许使用指定的HTTP方法
- 标题筛选:只有白名单中的标题才能传递到目标URL(不区分大小写)
- 超时:请求在配置的超时后自动中止
- 大小限制:如果响应体超过最大大小,则会被截断
日志记录
所有请求都以通用日志格式记录到stderr:
- - - [2025-11-01T12:00:00.000Z] "GET api.example.com/data HTTP/1.1" 200 1024
格式: <client-ip> - - [<timestamp>] "<method> <url-host><url-path> HTTP/1.1" <status> <bytes>
发展
# Install dependencies
npm install
# Build
npm run build
# Run in development mode
npm run dev
许可证
阿帕奇-2.0
