使用说明
rest-api-mcp
一个轻量级的MCP(模型上下文协议)服务器,提供结构化的 http_request AI代理的工具。消除了对脆弱的需求 curl 由于引用/转义问题而在Windows上中断的命令。
为什么?
人工智能代理(如ClaudeCode)在API开发过程中经常需要发出HTTP请求。使用 curl 通过Bash在Windows上是不可靠的——引用JSON正文、转义特殊字符和处理标头在shell之间都有不同的中断。此MCP服务器提供了一个单一的、结构化的 http_request 该工具在所有平台上工作方式相同,并格式化响应以尽可能少地燃烧上下文标记。
快速开始
在克劳德代码中注册
这 register 子命令会自动将服务器添加到Claude Code的配置中,无需手动编辑JSON。
# Register for current project (creates .mcp.json)
rest-api-mcp register project
# Register with a base URL for API development
rest-api-mcp register project . -- --base-url http://localhost:8080
# Register globally for all projects (writes to ~/.claude.json)
rest-api-mcp register user
争论之后 -- 每次启动时都会转发到MCP服务器。
更多示例
# Authenticated API with default headers
rest-api-mcp register project . -- \
--base-url https://api.example.com \
--default-header "Authorization: Bearer YOUR_TOKEN_HERE" \
--default-header "Content-Type: application/json"
# Session-based API (login + cookie flows)
rest-api-mcp register project . -- \
--base-url http://localhost:8080 \
--cookie-jar
# Corporate proxy / self-signed certs
rest-api-mcp register project . -- \
--base-url https://internal-api.corp.local \
--proxy http://proxy.corp.local:8080 \
--insecure
手动配置
您还可以直接编辑配置文件。这 register 命令在中生成这样的条目 .mcp.json 或 ~/.claude.json:
{
"mcpServers": {
"rest-api": {
"command": "/path/to/rest-api-mcp",
"args": ["--base-url", "http://localhost:8080"]
}
}
}
Full configuration example with all options
{
"mcpServers": {
"rest-api": {
"command": "/path/to/rest-api-mcp",
"args": [
"--base-url", "http://localhost:3000",
"--default-header", "Authorization: Bearer YOUR_TOKEN_HERE",
"--default-header", "Content-Type: application/json",
"--default-header", "Accept: application/json",
"--timeout", "15s",
"--max-response-size", "102400",
"--retry", "2",
"--retry-delay", "500ms",
"--cookie-jar"
]
}
}
}
安装
下载二进制文件
Windows、macOS和Linux的预构建二进制文件可在 发布 页面。
从源代码构建
需要Go 1.25+。
git clone https://github.com/lexandro/rest-api-mcp.git
cd rest-api-mcp
go build -o rest-api-mcp.exe .
CLI标志
| 标志 | 默认值 | 描述 |
|---|---|---|
--base-url |
(无) | 相对路径前添加了基本URL(有或没有前导斜线) |
--default-header |
(无) | 默认标头(可重复),格式: Key: Value |
--timeout |
30s |
默认请求超时 |
--max-response-size |
51200 |
最大响应体大小(以字节为单位)(默认50KB) |
--proxy |
(无) | HTTP/HTTPS代理URL |
--retry |
0 |
失败请求的重试次数 |
--retry-delay |
1s |
重试之间的延迟 |
--insecure |
false |
跳过TLS证书验证 |
--cookie-jar |
false |
内存cookie jar——在会话/登录流的请求中持久保存cookie |
工具: http_request
一个用于发出HTTP请求的单一、多功能工具。
参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
method |
string | yes | HTTP方法:GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS |
url |
string | yes | 完整URL或相对路径(如果 --base-url 已设置) |
headers |
object | no | 请求头作为键值对 |
body |
string | no | 请求正文(通常为JSON) |
queryParams |
object | no | 将参数作为键值对进行查询 |
timeout |
string | no | 每次请求超时覆盖(例如。, 10s, 500ms) |
followRedirects |
boolean | 否 | 遵循HTTP重定向(默认值:true) |
includeResponseHeaders |
boolean | no | 在输出中包含响应标头(默认值:false) |
jsonFilter |
string | 否 | GJSON路径 从JSON响应中提取字段,例如。 name, items.#.id, {name,id} |
saveTo |
string | no | 将响应正文写入此文件路径,而不是内联返回 |
maxResponseBytes |
number | no | 每个请求的响应大小限制(覆盖 --max-response-size) |
files |
对象 | 否 | 多部分/表单数据上传:表单字段名称→ 本地文件路径(与互斥 body) |
formFields |
object | no | 多部分/表单数据的文本字段 |
响应格式
紧凑、令牌高效的输出,旨在最大限度地减少上下文窗口的使用:
200 OK
{"id":1,"name":"example"}
打印精美的JSON响应 自动缩小 (在缩进API上节省20-40%的令牌)。随着 includeResponseHeaders: true:
200 OK
Content-Type: application/json
X-Request-Id: abc123
{"id":1,"name":"example"}
随着 jsonFilter 只返回请求的字段:
200 OK
{"name":"example","price":42}
二进制响应被总结而不是转储到上下文中:
200 OK
[binary: image/png, 245891 bytes — pass saveTo to write it to a file]
随着 saveTo 正文被流式传输到磁盘(没有大小限制),只返回摘要——然后代理可以读取或grep该文件:
200 OK
[saved to C:\temp\response.json: 245891 bytes, application/json]
大型内联响应会自动截断:
200 OK
{"data": [...first 50KB...]}
[truncated: 51200/245891 bytes — pass saveTo to fetch the full body to a file]
代币效率
- 自动JSON压缩 -预先打印的API响应在输入上下文之前被压缩
jsonFilter野外提取 --仅返回代理从大型有效载荷中需要的字段(GJSON路径语法)- 二进制检测 --二进制体变成单行摘要,而不是上下文中的原始字节
saveTo文件卸载 --大/二进制响应进入磁盘;全身无需燃烧代币即可使用- 默认情况下没有响应标头 --每次请求可节省约200-500个令牌
- 50KB响应限制 --防止将大量有效负载转储到上下文中(通过
maxResponseBytes) - 最小状态行 —
200 OK而不是冗长的curl输出,没有持续时间开销 - 无请求回显 --代理已经知道它发送了什么
- 文本错误 —
Request failed: connection refused不是堆栈跟踪
例子
简单GET
{ "method": "GET", "url": "/api/users" }
带有JSON正文的POST
{
"method": "POST",
"url": "/api/users",
"headers": { "Content-Type": "application/json" },
"body": "{\"name\": \"John\", \"email\": \"john@example.com\"}"
}
带查询参数的GET
{
"method": "GET",
"url": "/api/search",
"queryParams": { "q": "hello", "limit": "10" }
}
从大型响应中仅提取所需字段
{
"method": "GET",
"url": "https://api.github.com/repos/lexandro/rest-api-mcp",
"jsonFilter": "{name,stargazers_count,topics}"
}
从每个数组项中获取一个字段
{
"method": "GET",
"url": "/api/users",
"jsonFilter": "#.email"
}
下载文件的大型或二进制响应
{
"method": "GET",
"url": "/api/export/report.pdf",
"saveTo": "C:\\temp\\report.pdf"
}
上传文件(多部分/表单数据)
{
"method": "POST",
"url": "/api/upload",
"files": { "document": "C:\\temp\\invoice.pdf" },
"formFields": { "category": "invoices" }
}
带超时覆盖的PUT
{
"method": "PUT",
"url": "/api/users/123",
"body": "{\"name\": \"Updated\"}",
"timeout": "5s"
}
不遵循重定向的GET
{
"method": "GET",
"url": "/api/short-link",
"followRedirects": false,
"includeResponseHeaders": true
}
发展
构建
go build -o rest-api-mcp.exe .
测试
go test ./...
兽医
go vet ./...
许可证
麻省理工学院
