使用说明
HTTP MCP服务器
一种模型上下文协议(MCP)服务器,为AI驱动的web抓取工作流提供HTTP调试和测试功能。
🚀 特性
- 🌐 HTTP请求:发出GET、POST、PUT、DELETE请求,并完全控制标头、数据和超时
- 📊 响应分析:对响应标头、状态代码、内容类型和性能指标进行深入分析
- ⚡ 性能测试:通过多次迭代和统计分析配置文件请求性能
- 🔍 调试工作流:比较响应、验证端点和调试HTTP交互
- 🔗 集成就绪:设计用于与Debug MCP和Playwright MCP服务器无缝协作
📦 安装
来自GitHub(推荐)
pip install git+https://github.com/alexwilliamson/http-mcp-server.git
为了发展
git clone https://github.com/alexwilliamson/http-mcp-server.git
cd http-mcp-server
pip install -e .
🏃 快速开始
1.启动服务器
# HTTP transport (recommended for AI agents)
http-mcp http --port 8933
# Or stdio transport (for direct MCP clients)
http-mcp stdio
2.从AI代理连接
from mcp.client.sse import sse_client
from mcp import ClientSession
from langchain_mcp_adapters.tools import load_mcp_tools
async with sse_client("http://localhost:8933/sse") as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
http_tools = await load_mcp_tools(session)
# Now you have 8 powerful HTTP debugging tools available!
for tool in http_tools:
print(f"Available: {tool.name}")
3.在刮板测试中的使用
此服务器使AI代理能够测试和调试web抓取的HTTP方面:
- 直接测试API端点
- 比较浏览器与直接HTTP响应
- 分析标题和响应结构
- 分析性能并识别瓶颈
- 调试身份验证和Cookie
🛠️ 可用工具
核心HTTP操作
| 工具 | 说明 | 示例使用 |
|---|---|---|
make_request |
使用完全控制进行HTTP请求 | 测试API端点、下载页面 |
analyze_response |
深度响应分析 | 了解内容类型、编码、结构 |
extract_headers |
对标头进行分类和分析 | 检查安全标头、缓存规则 |
validate_status |
验证响应状态代码 | 确保请求按预期成功 |
高级调试工具
| 工具 | 说明 | 示例使用 |
|---|---|---|
debug_request |
带有会话日志记录的请求 | 调试带有工件的失败请求 |
compare_responses |
比较两个HTTP响应 | 浏览器与API的响应差异 |
profile_performance |
多迭代性能测试 | 找到最快的端点,识别速度减慢 |
实用工具
| 工具 | 说明 | 示例使用 |
|---|---|---|
close_http_client |
彻底关闭HTTP客户端 | 在工作流中进行适当的清理 |
🏗️ 体系结构集成
此服务器是完整的AI scraper调试堆栈的一部分:
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Playwright MCP │ │ Debug MCP │ │ HTTP MCP │
│ Browser Auto │ │ File/Terminal │ │ This Server │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
└───────────────────────┼───────────────────────┘
│
┌─────────────────────┐
│ AI Agent │
│ │
│ 1. Plan Scraping │
│ 2. Test HTTP First │
│ 3. Generate Code │
│ 4. Debug & Fix │
└─────────────────────┘
🔧 使用示例
基本HTTP请求
# Make a simple GET request
response = await make_request("GET", "https://api.example.com/data")
# Analyze the response
analysis = await analyze_response(response)
print(f"Content type: {analysis.content_type}")
print(f"Response time: {analysis.performance_metrics['response_time_ms']}ms")
调试API终结点
# Debug request with session logging
debug_result = await debug_request({
"method": "POST",
"url": "https://api.example.com/search",
"headers": {"Authorization": "Bearer token"},
"data": {"query": "test"}
}, session_id="debug-123")
# Check if request succeeded
if debug_result["success"]:
response = debug_result["response"]
analysis = debug_result["analysis"]
print(f"API returned {len(response['content'])} bytes")
else:
print(f"Request failed: {debug_result['error']}")
比较浏览器与API
# Get response from browser (via Playwright MCP)
browser_response = await playwright_get_page_content(url)
# Get same content via direct HTTP
api_response = await make_request("GET", url)
# Compare responses
comparison = await compare_responses(browser_response, api_response)
print(f"Content identical: {comparison['content_similarity']['identical']}")
print(f"Header differences: {len(comparison['header_differences']['value_differences'])}")
性能分析
# Profile endpoint performance
profile = await profile_performance("https://api.example.com/data", iterations=5)
stats = profile["statistics"]
print(f"Average response time: {stats['avg_response_time_ms']:.2f}ms")
print(f"Success rate: {stats['success_rate']:.1f}%")
📊 响应分析特征
内容分析
- 内容类型检测(JSON、HTML、XML等)
- 编码检测和验证
- 大小度量和压缩分析
标题分析
- 安全标头审核(CSP、HSTS、X-Frame-Options)
- 缓存头分析(缓存控制,ETag)
- 服务器信息提取
- 自定义标题分类
性能指标
- 响应时间测量
- 内容大小分析
- 传输速度计算
- 跨多个请求的统计分析
🧪 测试
# Test the server directly
python -m http_mcp.server http --port 8933
# In another terminal, test basic functionality:
curl -X POST http://localhost:8933/mcp \
-H "Content-Type: application/json" \
-d '{"method": "tools/list"}'
🔧 配置
请求默认值
服务器使用合理的默认值:
- 超时:30秒
- 最大重定向: 10
- 用户代理:标准HTTP客户端
- SSL验证:已启用
调试工件
使用时 debug_request 使用会话管理:
debug_artifacts/sessions/{session_id}/responses/
├── http_response_1234567890.json # Full response data
├── http_content_1234567890.html # Response content (if HTML)
└── http_analysis_1234567890.json # Analysis results
🐛 故障排除
常见问题
- SSL错误:对于开发,请考虑SSL验证设置
- 超时问题:调整慢速端点的超时
- 内存使用:通过流媒体高效处理大量响应
- 速率限制:性能测试迭代之间的内置延迟
调试模式
启用详细日志记录:
http-mcp http --port 8933 --log-level DEBUG
🤝 集成示例
使用调试MCP
# Combined HTTP testing and file operations
http_response = await make_request("GET", target_url)
await write_file("debug_response.html", http_response["content"])
await search_file("debug_response.html", "error")
使用LangGraph工作流
# HTTP testing in scraper debug workflow
if strategy == "api_direct":
# Test API endpoint first
api_response = await make_request("GET", api_url)
api_analysis = await analyze_response(api_response)
if api_analysis.is_json:
# Generate API scraper
scraper_code = generate_api_scraper(api_response)
else:
# Fall back to HTML scraping
scraper_code = generate_html_scraper(api_response)
🤝 贡献
- 分叉存储库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
📜 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
🙏 致谢
- 专为人工智能驱动的开发工作流程而构建
- 与集成 模型上下文协议(MCP)
- 设计用于 LangGraph 代理工作流
- HTTP客户端由 httpx
- 自动化刮刀开发流水线的一部分