Crawl4AI MCP包装器
定制模型上下文协议(MCP)服务器,该服务器将Crawl4AI Docker API与可靠的stdio传输打包,用于Claude Code集成。
概述
此MCP包装器提供了7个用于网络抓取、爬行和内容提取的工具:
- scrape_markdown -从网页中提取干净的标记
- 提取html -获取预处理的HTML结构
- capture_screenshot -拍摄全页PNG截图
- generate.pdf -从网页创建PDF文档
- execute_javascript -在浏览器上下文中运行JavaScript
- crawl_urls -使用配置选项抓取多个URL
- ask_crawl4ai -查询Crawl4AI文档和示例
先决条件
- Python 3.8或更高版本
- Crawl4AI Docker容器在11235端口上运行
- 已安装克劳德代码
安装
1.启动Crawl4AI Docker容器
docker run -d -p 11235:11235 --name crawl4ai --shm-size=2g unclecode/crawl4ai:latest验证它是否正在运行:
curl http://localhost:11235/health2.设置虚拟环境
cd /Volumes/4TB/Users/josephmcmyne/myProjects/mcp/crawl4ai-wrapper
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt3.测试包装材料
python test_wrapper.py您应该看到所有测试都通过了:
✅ All tests passed! MCP wrapper is ready to use.配置
克劳德代码(项目级)
添加到 /Volumes/4TB/Users/josephmcmyne/general/.mcp.json:
{
"mcpServers": {
"iterm-mcp": {
"args": ["-y", "iterm-mcp"],
"command": "npx"
},
"crawl4ai-custom": {
"command": "/Volumes/4TB/Users/josephmcmyne/myProjects/mcp/crawl4ai-wrapper/venv/bin/python",
"args": ["/Volumes/4TB/Users/josephmcmyne/myProjects/mcp/crawl4ai-wrapper/crawl4ai_mcp.py"],
"env": {
"CRAWL4AI_BASE_URL": "http://localhost:11235"
}
}
}
}克劳德代码(全球)
添加到 ~/.claude/claude_desktop_config.json (如果此文件存在):
{
"mcpServers": {
"crawl4ai-custom": {
"command": "/Volumes/4TB/Users/josephmcmyne/myProjects/mcp/crawl4ai-wrapper/venv/bin/python",
"args": ["/Volumes/4TB/Users/josephmcmyne/myProjects/mcp/crawl4ai-wrapper/crawl4ai_mcp.py"],
"env": {
"CRAWL4AI_BASE_URL": "http://localhost:11235"
}
}
}
}用法
重新启动Claude代码
配置后,重新启动Claude Code以加载新的MCP服务器。
验证连接
claude mcp list您应该看到:
crawl4ai-custom: ... - ✓ Connected在克劳德代码中使用
示例提示:
Scrape the content from https://example.com and summarize it.Take a screenshot of https://github.comCrawl these URLs and extract their main content: https://example.com, https://example.org工具参考
scrape_markdown
从网页中提取干净的标记。
参数:
url(必填):要抓取的URLfilter_type(可选):“fit”、“raw”、“bm25”或“llm”(默认值:“fit”)query(可选):BM25/LLM筛选器的查询字符串cache_bust(可选):缓存中断计数器(默认值:“0”)
例子:
result = await scrape_markdown("https://example.com")提取html
从网页获取预处理的HTML。
参数:
url(必填):从中提取HTML的URL
capture_screenshot
捕获一个完整的PNG页面截图。
参数:
url(必填):截图URLscreenshot_wait_for(可选):捕获前等待的秒数(默认值:2.0)output_path(可选):保存截图文件的路径
generate.pdf
从网页生成PDF文档。
参数:
url(必填):转换为PDF的URLoutput_path(可选):保存PDF文件的路径
execute_javascript
在网页上运行JavaScript代码。
参数:
url(必需):执行脚本的URLscripts(必填):要执行的JavaScript代码段列表
crawl_urls
使用配置抓取多个URL。
参数:
urls(必填):要抓取的1-100个URL列表browser_config(可选):浏览器配置覆盖crawler_config(可选):爬网程序配置覆盖hooks(可选):自定义挂钩功能
ask_crawl4ai
查询Crawl4AI文档。
参数:
query(必填):搜索查询context_type(可选):“code”、“doc”或“all”(默认值:“all”)score_ratio(可选):最低分数阈值(默认值:0.5)max_results(可选):最大结果(默认值:20)
故障排除
容器未运行
如果测试因连接错误而失败:
docker ps | grep crawl4ai如果没有运行,请启动它:
docker start crawl4aiMCP服务器未连接
检查克劳德代码日志:
tail -f ~/Library/Logs/Claude/mcp*.log验证您的Python路径 .mcp.json 正确:
which python
# Should match the venv path in your config端口冲突
如果端口11235正在使用中:
- 停止Crawl4AI容器:
docker stop crawl4ai - 找出冲突的过程:
lsof -i :11235 - 要么停止该进程,要么更改Docker和此包装器中的端口
发展
添加新工具
要添加新工具,请执行以下操作:
- 添加一个异步函数,用
@mcp.tool()在crawl4ai_mcp.py - 向相应的Crawl4AI端点发出HTTP请求
- 添加错误处理
- 在中添加测试
test_wrapper.py - 更新此自述文件
在调试模式下运行
# Enable debug logging
export CRAWL4AI_MCP_LOG=DEBUG
# Run the server
python crawl4ai_mcp.py建筑
┌─────────────────┐
│ Claude Code │
└────────┬────────┘
│ stdio (MCP protocol)
▼
┌─────────────────┐
│ FastMCP Server │ (this wrapper)
│ crawl4ai_mcp.py│
└────────┬────────┘
│ HTTP/REST API
▼
┌─────────────────┐
│ Crawl4AI Docker │
│ Container │
│ (port 11235) │
└─────────────────┘优于官方SSE服务器的优势
- 可靠的stdio传输:没有SSE连接问题
- 完全控制:易于扩展和定制
- 更好的错误处理:优雅的退化
- 简单调试:标准Python堆栈跟踪
- 无协议不匹配:直接HTTP到Crawl4AI API
许可证
MIT许可证-随意修改和分发。
