返回市场
爬虫4AI-MCP服务器

爬虫4AI-MCP服务器

作者:azure-architect2 星标更新:2025-08-04

项目介绍

Crawl4AI MCP Server

适用于Crawl4AI(开源且适合LLM的网络爬虫)的生产就绪型Model Context Protocol (MCP) 服务器集成。该项目提供了在Claude Desktop和Claude Code中无缝访问高级网络爬取和内容提取功能的能力。

🚀 快速开始

# 拉取并运行Crawl4AI
docker run -d -p 11235:11235 --name crawl4ai --shm-size=1g unclecode/crawl4ai:latest

# 添加到Claude Desktop配置
{
  "mcpServers": {
    "c4ai-sse": {
      "command": "sse",
      "args": ["http://localhost:11235/mcp/sse"]
    }
  }
}

就这样! 现在您可以在Claude中访问7种强大的网络爬取工具。

🛠 可用工具

工具描述最佳用途
md从网页中提取干净的Markdown博客文章、文章、文档
html提取原始HTML内容网络开发、DOM分析
screenshot捕获高分辨率截图视觉验证、归档
pdf从网页生成PDF文档归档、离线阅读
execute_js在页面上执行JavaScript动态内容、自定义提取
crawl批量处理多个URL高容量抓取、比较
ask查询Crawl4AI内置的知识库库文档、API参考

🎯 关键特性

  • 生产就绪:基于Docker的部署,带有健康检查和监控
  • 优化LLM:专门设计用于AI消费的内容提取
  • 高性能:并发处理与智能缓存
  • 全面性:截图捕获、PDF生成、JavaScript执行
  • 可靠性:强大的错误处理和超时管理
  • 可扩展性:支持Kubernetes和云平台

📊 性能亮点

从对7种工具的全面测试:

  • 成功率:所有核心功能的100%
  • 响应时间:平均每请求2-8秒
  • 内容质量:始终干净、格式良好的输出
  • 可靠性:复杂网站和边缘情况的强大处理能力
  • 并发性:高效批量处理多个URL

📚 文档

核心文档

快捷链接

🔧 系统要求

  • Docker:版本20.10+
  • 内存:最低2GB RAM(推荐4GB)
  • 存储:2GB空闲磁盘空间
  • 网络:拉取镜像所需的互联网连接
  • Claude:桌面或代码版,支持MCP

📦 安装方法

方法1:Docker(推荐)

# 基本安装
docker run -d \
  -p 11235:11235 \
  --name crawl4ai \
  --shm-size=1g \
  unclecode/crawl4ai:latest

方法2:Docker Compose

version: '3.8'
services:
  crawl4ai:
    image: unclecode/crawl4ai:latest
    container_name: crawl4ai
    ports:
      - "11235:11235"
    shm_size: '1gb'
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:11235/"]
      interval: 30s
      timeout: 10s
      retries: 3

方法3:Kubernetes

apiVersion: apps/v1
kind: Deployment
metadata:
  name: crawl4ai
spec:
  replicas: 1
  selector:
    matchLabels:
      app: crawl4ai
  template:
    metadata:
      labels:
        app: crawl4ai
    spec:
      containers:
      - name: crawl4ai
        image: unclecode/crawl4ai:latest
        ports:
        - containerPort: 11235
        resources:
          requests:
            memory: "1Gi"
            cpu: "500m"
          limits:
            memory: "2Gi"
            cpu: "1"

🎨 基本使用

提取文章内容

curl -X POST http://localhost:11235/md \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://en.wikipedia.org/wiki/Web_scraping",
    "f": "fit"
  }'

捕获截图

curl -X POST http://localhost:11235/screenshot \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://github.com",
    "screenshot_wait_for": 3
  }'

执行JavaScript

curl -X POST http://localhost:11235/execute_js \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://news.ycombinator.com",
    "scripts": [
      "Array.from(document.querySelectorAll(\".titleline a\")).slice(0, 5).map(a => ({title: a.textContent, url: a.href}))"
    ]
  }'

批量处理

curl -X POST http://localhost:11235/crawl \
  -H "Content-Type: application/json" \
  -d '{
    "urls": [
      "https://example.com",
      "https://httpbin.org/html",
      "https://jsonplaceholder.typicode.com"
    ]
  }'

🚀 高级功能

自定义浏览器配置

{
  "url": "https://example.com",
  "browser_config": {
    "viewport_width": 1920,
    "viewport_height": 1080,
    "user_agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36",
    "headless": true
  }
}

内容过滤选项

{
  "url": "https://example.com",
  "f": "fit",        // "raw", "fit", "bm25", "llm"
  "q": null,         // bm25/llm过滤查询
  "c": "0"           // 内容过滤级别
}

JavaScript执行模式

// 提取结构化数据
{
  title: document.title,
  headings: Array.from(document.querySelectorAll('h1,h2,h3')).map(h => h.textContent),
  links: Array.from(document.querySelectorAll('a')).slice(0, 10).map(a => ({
    text: a.textContent.trim(),
    url: a.href
  }))
}

🏭 生产部署

使用Docker Swarm实现高可用性

docker service create \
  --name crawl4ai \
  --replicas 3 \
  --publish 11235:11235 \
  --mount type=volume,source=crawl4ai-cache,target=/app/cache \
  unclecode/crawl4ai:latest

云平台部署

AWS ECSGoogle Cloud RunAzure Container Instances - 请参阅部署指南获取完整说明。

监控与可观测性

# docker-compose.monitoring.yml
services:
  crawl4ai:
    image: unclecode/crawl4ai:latest
    environment:
      - METRICS_ENABLED=true
      - LOG_LEVEL=INFO
  
  prometheus:
    image: prom/prometheus
    ports:
      - "9090:9090"

🔍 健康检查与监控

# 基本健康检查
curl http://localhost:11235/

# 架构验证
curl http://localhost:11235/mcp/schema | jq '.tools | length'

# 性能监控
docker stats crawl4ai

🛡 安全特性

  • 网络隔离 使用自定义Docker网络
  • 资源限制 防止资源耗尽
  • 只读容器 提升安全性
  • 非root执行 使用适当的用户权限

📈 性能调优

内存优化

docker run -d \
  --memory=4g \
  --memory-swap=4g \
  --shm-size=2g \
  --name crawl4ai \
  unclecode/crawl4ai:latest

CPU优化

docker run -d \
  --cpus="2.0" \
  --cpuset-cpus="0,1" \
  --name crawl4ai \
  unclecode/crawl4ai:latest

浏览器性能

{
  "browser_config": {
    "text_mode": true,
    "light_mode": true,
    "extra_args": [
      "--disable-images",
      "--disable-javascript",
      "--disable-plugins"
    ]
  }
}

🧪 测试与验证

自动化测试套件

# 运行全面测试
./test-crawl4ai.sh

# 预期结果:
# 1. 健康检查:✓ 通过
# 2. 架构检查:✓ 通过
# 3. MD工具检查:✓ 通过
# 4. 截图工具检查:✓ 通过
# 5. JavaScript工具检查:✓ 通过

手动验证

# 分别测试每个工具
curl -X POST http://localhost:11235/md -H "Content-Type: application/json" -d '{"url": "https://example.com", "f": "fit"}'
curl -X POST http://localhost:11235/screenshot -H "Content-Type: application/json" -d '{"url": "https://example.com"}'
curl -X POST http://localhost:11235/execute_js -H "Content-Type: application/json" -d '{"url": "https://example.com", "scripts": ["document.title"]}'

🔧 配置

环境变量

# 日志
LOG_LEVEL=INFO              # DEBUG, INFO, WARNING, ERROR
MAX_WORKERS=4               # 最大并发工作者数
TIMEOUT=30                  # 默认请求超时

# 特性
CACHE_ENABLED=true          # 启用缓存
SCREENSHOT_ENABLED=true     # 启用截图
PDF_ENABLED=true            # 启用PDF生成

# 安全
RATE_LIMIT=100              # 每分钟请求数
MAX_URL_LENGTH=2048         # 最大URL长度

Claude Desktop集成

{
  "mcpServers": {
    "c4ai-sse": {
      "command": "sse",
      "args": ["http://localhost:11235/mcp/sse"],
      "env": {
        "LOG_LEVEL": "INFO",
        "TIMEOUT": "30"
      }
    }
  }
}

📊 使用案例

内容分析流水线

def comprehensive_analysis(url):
    # 步骤1:提取内容
    md_result = call_tool("md", {"url": url, "f": "fit"})
    
    # 步骤2:分析结构
    js_result = call_tool("execute_js", {
        "url": url,
        "scripts": ["({wordCount: document.body.textContent.split(' ').length})"]
    })
    
    # 步骤3:视觉验证
    screenshot_result = call_tool("screenshot", {"url": url})
    
    return combine_results(md_result, js_result, screenshot_result)

竞争对手监控

def monitor_competitors(urls):
    # 批量处理所有竞争对手
    batch_result = call_tool("crawl", {"urls": urls})
    
    # 提取定价和产品信息
    for result in batch_result["results"]:
        if result["success"]:
            analyze_competitor_data(result)

🚨 故障排除

常见问题

问题解决方案
端口已被占用使用-p 8080:11235替代端口
容器无法启动查看docker logs crawl4ai中的错误
MCP连接失败使用curl http://localhost:11235/mcp/sse验证端点
内存使用过高添加--memory=4g --shm-size=2g限制
请求超时在工具参数中增加超时

诊断命令

# 容器状态
docker ps | grep crawl4ai
docker inspect crawl4ai --format='{{.State.Health.Status}}'

# 网络连通性
curl -I http://localhost:11235/
telnet localhost 11235

# 资源使用
docker stats crawl4ai

🤝 贡献

这是一个针对开源Crawl4AI库的部署和集成项目。对于Crawl4AI核心开发:

对于MCP集成问题或部署改进,请在此仓库中打开一个issue。

📄 许可证

此项目遵循底层Crawl4AI项目的许可证。请参阅官方Crawl4AI仓库以获取许可信息。

🙏 致谢

  • Crawl4AI团队 - 创建了优秀的开源网络爬虫库
  • Anthropic - 提供了Model Context Protocol规范和Claude集成
  • 社区贡献者 - 测试、反馈和改进

📞 支持

  • 文档:在/docs目录中的综合指南
  • 问题报告:通过GitHub Issues报告问题
  • 社区:加入Crawl4AI社区讨论
  • 商业支持:联系企业支持和定制部署

🎉 准备开始爬取了吗? 请按照上面的快速开始指南操作,或深入阅读安装及设置指南以获取详细说明。


最后更新日期:2025年8月4日
版本:1.0.0