一个提供从URL获取JSON数据和网页内容工具的Model Context Protocol (MCP)服务器。具有智能内容提取、多种HTTP方法以及类似浏览器的头部信息,以实现可靠的网络抓取。
减少LLM令牌使用及幻觉 - 不需要获取整个JSON响应并浪费令牌,只需提取所需的数据。
❌ 传统获取(浪费):
// API返回2000多个令牌
{
"data": [
{
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"avatar": "https://...",
"profile": {
"bio": "长简介文本...",
"settings": {...},
"preferences": {...},
"metadata": {...}
},
"posts": [...],
"followers": [...],
"created_at": "2023-01-01",
"updated_at": "2024-01-01"
},
// ... 另外50个用户
],
"pagination": {...},
"meta": {...}
}
✅ JSONPath提取(高效):
// 只有10个令牌 - 正是你需要的!
["Alice", "Bob", "Charlie"]
使用模式:data[*].name节省了99%的令牌,并且消除了因无关数据引起的模型幻觉。
对于大多数IDE,使用uvx工具运行服务器。
{
"mcpServers": {
"fetch-jsonpath-mcp": {
"command": "uvx",
"args": [
"fetch-jsonpath-mcp"
]
}
}
}
<details>
<summary><b>在Claude Code中安装</b></summary>
claude mcp add fetch-jsonpath-mcp -- uvx fetch-jsonpath-mcp
</details>
<details>
<summary><b>在Cursor中安装</b></summary>
{
"mcpServers": {
"fetch-jsonpath-mcp": {
"command": "uvx",
"args": ["fetch-jsonpath-mcp"]
}
}
}
</details>
<details>
<summary><b>在Windsurf中安装</b></summary>
添加到你的Windsurf MCP配置文件中。更多信息参见Windsurf MCP文档。
{
"mcpServers": {
"fetch-jsonpath-mcp": {
"command": "uvx",
"args": ["fetch-jsonpath-mcp"]
}
}
}
</details>
<details>
<summary><b>在VS Code中安装</b></summary>
"mcp": {
"servers": {
"fetch-jsonpath-mcp": {
"type": "stdio",
"command": "uvx",
"args": ["fetch-jsonpath-mcp"]
}
}
}
</details>
uv sync
# 安装示例服务器依赖
uv add fastapi uvicorn
# 在端口8080启动示例服务器
uv run demo-server
uv run fetch-jsonpath-mcp
位于http://localhost:8080的示例服务器返回:
{
"foo": [{"baz": 1, "qux": "a"}, {"baz": 2, "qux": "b"}],
"bar": {
"items": [10, 20, 30],
"config": {"enabled": true, "name": "example"}
},
"metadata": {"version": "1.0.0"}
}
fetch-json使用JSONPath模式提取JSON数据,并支持所有HTTP方法。
{
"name": "fetch-json",
"arguments": {
"url": "http://localhost:8080",
"pattern": "foo[*].baz",
"method": "GET"
}
}
返回:[1, 2]
参数:
url(必需):目标URLpattern(可选):用于数据提取的JSONPath模式method(可选):HTTP方法(GET, POST, PUT, DELETE等) - 默认:"GET"data(可选):POST/PUT请求的请求体headers(可选):额外的HTTP头部fetch-text获取网页内容并进行智能文本提取。默认为Markdown格式,以便于阅读。
{
"name": "fetch-text",
"arguments": {
"url": "http://localhost:8080",
"output_format": "clean_text"
}
}
返回:JSON数据的干净文本表示
输出格式:
"markdown"(默认):将HTML转换为干净的Markdown格式"clean_text":去除HTML标签的纯文本"raw_html":原始HTML内容参数:
url(必需):目标URLmethod(可选):HTTP方法 - 默认:"GET"data(可选):POST/PUT请求的请求体headers(可选):额外的HTTP头部output_format(可选):输出格式 - 默认:"markdown"batch-fetch-json并发处理多个带有不同JSONPath模式的URL。
{
"name": "batch-fetch-json",
"arguments": {
"requests": [
{"url": "http://localhost:8080", "pattern": "foo[*].baz"},
{"url": "http://localhost:8080", "pattern": "bar.items[*]"}
]
}
}
返回:[{"url": "http://localhost:8080", "pattern": "foo[*].baz", "success": true, "content": [1, 2]}, {"url": "http://localhost:8080", "pattern": "bar.items[*]", "success": true, "content": [10, 20, 30]}]
请求对象参数:
url(必需):目标URLpattern(可选):JSONPath模式method(可选):HTTP方法 - 默认:"GET"data(可选):请求体headers(可选):额外的HTTP头部batch-fetch-text从多个URL获取内容并进行智能文本提取。
{
"name": "batch-fetch-text",
"arguments": {
"requests": [
"http://localhost:8080",
{"url": "http://localhost:8080", "output_format": "raw_html"}
],
"output_format": "markdown"
}
}
返回:[{"url": "http://localhost:8080", "success": true, "content": "# 示例服务器数据\n\n..."}, {"url": "http://localhost:8080", "success": true, "content": "{\"foo\": [{\"baz\": 1, \"qux\": \"a\"}, {\"baz\": 2, \"qux\": \"b\"}]..."}]
支持:
此项目使用jsonpath-ng实现JSONPath。
| 模式 | 结果 | 描述 |
|---|---|---|
foo[*].baz | [1, 2] | 获取所有baz值 |
bar.items[*] | [10, 20, 30] | 获取所有items |
metadata.version | ["1.0.0"] | 获取版本 |
完整的JSONPath语法参考,请参阅jsonpath-ng文档。
设置环境变量来自定义行为:
# 请求超时时间(秒,默认:10.0)
export JSONRPC_MCP_TIMEOUT=30
# SSL验证(默认:true)
export JSONRPC_MCP_VERIFY=false
# 跟随重定向(默认:true)
export JSONRPC_MCP_FOLLOW_REDIRECTS=true
# 自定义头部(将与默认浏览器头部合并)
export JSONRPC_MCP_HEADERS='{"Authorization": "Bearer token"}'
# HTTP代理配置
export JSONRPC_MCP_PROXY="http://proxy.example.com:8080"
默认浏览器头部:服务器自动包含模拟真实浏览器的头部,以防止被阻止:
JSONRPC_MCP_HEADERS中的自定义头部在冲突时会覆盖默认值。
# 运行测试
pytest
# 检查代码质量
ruff check --fix
# 本地构建和测试
uv build
get-json → fetch-json, get-text → fetch-textmarkdownify