获取JSONPath MCP
](https://pypi.org/project/fetch-jsonpath-mcp/) 
一种模型上下文协议(MCP)服务器,提供从URL获取JSON数据和web内容的工具。具有智能内容提取、多种HTTP方法和类似浏览器的标头,可实现可靠的web抓取。
🎯 为什么使用这个?
减少LLM代币的使用和幻觉 -与其获取整个JSON响应并浪费令牌,不如只提取所需的数据。
传统提取与JSONPath提取
❌ 传统获取(浪费):
// API returns 2000+ tokens
{
"data": [
{
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"avatar": "https://...",
"profile": {
"bio": "Long bio text...",
"settings": {...},
"preferences": {...},
"metadata": {...}
},
"posts": [...],
"followers": [...],
"created_at": "2023-01-01",
"updated_at": "2024-01-01"
},
// ... 50 more users
],
"pagination": {...},
"meta": {...}
}✅ JSONPath提取(高效):
// Only 10 tokens - exactly what you need!
["Alice", "Bob", "Charlie"]使用模式: data[*].name 保存 99%代币 并从无关数据中消除模型幻觉。
安装
对于大多数IDE,使用 uvx 运行服务器的工具。
{
"mcpServers": {
"fetch-jsonpath-mcp": {
"command": "uvx",
"args": [
"fetch-jsonpath-mcp"
]
}
}
}Install in Claude Code
claude mcp add fetch-jsonpath-mcp -- uvx fetch-jsonpath-mcpInstall in Cursor
{
"mcpServers": {
"fetch-jsonpath-mcp": {
"command": "uvx",
"args": ["fetch-jsonpath-mcp"]
}
}
}Install in Windsurf
将此添加到您的Windsurf MCP配置文件中。看 Windsurf MCP文件 了解更多信息。
Windsurf本地服务器连接
{
"mcpServers": {
"fetch-jsonpath-mcp": {
"command": "uvx",
"args": ["fetch-jsonpath-mcp"]
}
}
}Install in VS Code
"mcp": {
"servers": {
"fetch-jsonpath-mcp": {
"type": "stdio",
"command": "uvx",
"args": ["fetch-jsonpath-mcp"]
}
}
}开发设置
1.安装依赖项
uv sync2.启动演示服务器(可选)
# Install demo server dependencies
uv add fastapi uvicorn
# Start demo server on port 8080
uv run demo-server3.运行MCP服务器
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
使用支持所有HTTP方法的JSONPath模式提取JSON数据。
{
"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": "# Demo Server Data\n\n..."}, {"url": "http://localhost:8080", "success": true, "content": "{\"foo\": [{\"baz\": 1, \"qux\": \"a\"}, {\"baz\": 2, \"qux\": \"b\"}]..."}]
支持:
- 简单URL字符串
- 具有自定义方法和标头的完整请求对象
- 同一批次中的混合输入类型
JSONPath示例
此项目使用 Jsonpath螺母 用于JSONPath实现。
| 模式 | 结果 | 描述 |
|---|---|---|
foo[*].baz | [1, 2] | 获取所有baz值 |
bar.items[*] | [10, 20, 30] | 获取所有项目 |
metadata.version | ["1.0.0"] | 获取版本 |
有关完整的JSONPath语法参考,请参阅 jsonpath ng文档.
🚀 性能优势
- 代币效率:仅提取所需数据,而不是整个JSON响应
- 更快的处理速度:更小的有效载荷=更快的LLM响应
- 减少幻觉:更少的无关数据=更准确的输出
- 成本节约:更少的代币=更低的API成本
- 更专注:干净的数据有助于模型保持任务
- 智能标头:默认浏览器标头可防止阻止并改善访问
- Markdown转换:保持结构清晰可读的格式
配置
设置环境变量以自定义行为:
# Request timeout in seconds (default: 10.0)
export JSONRPC_MCP_TIMEOUT=30
# SSL verification (default: true)
export JSONRPC_MCP_VERIFY=false
# Follow redirects (default: true)
export JSONRPC_MCP_FOLLOW_REDIRECTS=true
# Custom headers (will be merged with default browser headers)
export JSONRPC_MCP_HEADERS='{"Authorization": "Bearer token"}'
# HTTP proxy configuration
export JSONRPC_MCP_PROXY="http://proxy.example.com:8080"默认浏览器标题:服务器会自动包含逼真的浏览器标头以防止阻塞:
- 用户代理:Chrome浏览器模拟
- 接受:标准浏览器内容类型
- 接受语言,接受编码:浏览器默认值
- 安全标头:适用于现代浏览器的Sec Fetch-\*标头
自定义标题 JSONRPC_MCP_HEADERS 当发生冲突时,将覆盖默认值。
发展
# Run tests
pytest
# Check code quality
ruff check --fix
# Build and test locally
uv buildv1.1.0的新增功能
- ✨ 多方法HTTP支持:获取、发布、放置、删除、补丁、头部、选项
- 🔄 工具重命名:
get-json→fetch-json,get-text→fetch-text - 📄 Markdown转换:默认HTML到Markdown转换
markdownify - 🌐 智能浏览器标题:自动浏览器模拟标题
- 🎛️ 格式控制:文本内容的三种输出格式(markdown、clean_text、raw_html)
- 🚀 增强的批处理:支持批量操作中的不同方法
