Crawl4AI网络爬虫MCP服务器

该项目提供了一个MCP(模型上下文协议)服务器,该服务器使用 crawl4ai 库执行网络抓取和智能内容提取任务。它允许AI代理(如Claude或用LangChain/LangGraph构建的代理)与网页交互,检索内容,搜索特定文本,并根据自然语言指令执行基于LLM的提取。
此服务器使用:
- FastMCP: 用于创建MCP服务器端点。
- crawl4ai: 用于核心网络爬行和提取逻辑。
- dotenv: 用于通过
.env文件。 - (可选)Docker: 对于容器化部署,绑定Python和依赖项。
特性
- 展示用于web交互的MCP工具:
- scrape_url:以Markdown格式获取网页的完整内容。 - extract_text_by_query:根据查询在页面上查找特定的文本片段。 - smart_extract:使用LLM(目前为Google Gemini)根据指令提取结构化信息。
- 可通过环境变量(API键)进行配置。
- 包括Docker配置(
Dockerfile)便于独立部署。 - 默认情况下,通过端口8002上的服务器发送事件(SSE)进行通信。
暴露的MCP工具
scrape_url
抓取网页并以Markdown格式返回其内容。
论据:
url(str, 必需的):要抓取的网页的URL。
退货:
- (str):Markdown格式的网页内容,或错误消息。
extract_text_by_query
从包含特定搜索查询的网页中提取相关文本片段。最多返回找到的前5个匹配项。
论据:
url(str, 必需的):要在其中搜索的网页的URL。query(str, 必需的):要搜索的文本查询(不区分大小写)。context_size(int, *可选的*):每个代码段中匹配的查询文本前后包含的字符数。默认为300.
退货:
- (str):一个包含已找到的文本片段的格式化字符串,或一条表示未找到匹配项的消息,或一则错误消息。
smart_extract
基于自然语言指令,使用配置的LLM(目前需要Google Gemini API密钥)从网页中智能地提取特定信息。
论据:
url(str, 必需的):要分析和提取的网页的URL。instruction(str, 必需的):自然语言教学,指定要提取的信息(例如,“列出本页提到的所有发言者”,“提取主要联系人电子邮件地址”,“总结主要发现”)。
退货:
- (str):提取的信息(通常格式化为JSON或基于指令的结构化文本)或指示未找到相关信息的消息,或错误消息(包括所需的API密钥缺失)。
设置和运行
您可以在本地或使用提供的Docker配置运行此服务器。
选项1:使用Docker运行(建议部署)
此方法捆绑了Python和所有必要的库。你只需要在主机上安装Docker。
- 安装Docker: 下载并安装 对于您的操作系统。启动Docker桌面。
- 克隆存储库:
git clone https://github.com/your-username/your-repo-name.git # Replace with your repo URL
cd your-repo-name- 创建
.env文件: 创建一个名为的文件.env在项目根目录中,并添加您的API密钥:
# Required for the smart_extract tool
GOOGLE_API_KEY=your_google_ai_api_key_here
# Optional, checked by server but not currently used by tools
# OPENAI_API_KEY=your_openai_key_here
# MISTRAL_API_KEY=your_mistral_key_here- 构建Docker镜像:
docker build -t crawl4ai-mcp-server .- 运行容器: 这将启动服务器,使您的主机上的端口8002可用。使用
--env-file从本地安全地传递API密钥.env将文件放入容器的环境中。
docker run -it --rm -p 8002:8002 --env-file .env crawl4ai-mcp-server- -it:以交互方式运行。 - --rm:退出时删除容器。 - -p 8002:8002:将主机端口8002映射到集装箱端口8002。 - --env-file .env:从本地加载环境变量 .env 将文件放入容器中。 对于API密钥至关重要。 - crawl4ai-mcp-server:您创建的图像的名称。
- 服务器正在运行: 将出现日志,表明服务器正在监听SSE(
http://0.0.0.0:8002). - 正在连接客户端: 配置您的MCP客户端(例如LangChain代理)以连接到
http://127.0.0.1:8002/sse随着transport: "sse".
选项2:本地运行
这需要Python和在主机上手动安装依赖项。
- 安装Python: 确保Python>=3.9(检查
crawl4ai如有需要,建议3.10+)。 - 克隆存储库:
git clone https://github.com/your-username/your-repo-name.git # Replace with your repo URL
cd your-repo-name- 创建虚拟环境(推荐):
python -m venv venv
source venv/bin/activate # Linux/macOS
# venv\Scripts\activate # Windows*(或使用Conda: conda create --name crawl4ai-env python=3.11 -y && conda activate crawl4ai-env)*
- 安装依赖关系:
pip install -r requirements.txt- 创建
.env文件: 创建一个名为的文件.env在项目根目录中,添加您的API密钥(与Docker设置步骤3中的内容相同)。 - 运行服务器:
python your_server_script_name.py # e.g., python webcrawl_mcp_server.py- 服务器正在运行: 它会继续听
http://127.0.0.1:8002/sse. - 正在连接客户端: 配置您的MCP客户端以连接到
http://127.0.0.1:8002/sse.
环境变量
服务器使用以下环境变量,通常从 .env 文件:
GOOGLE_API_KEY: 必需 为了smart_extract工具运行(使用谷歌双子座)。从以下位置获取一个 谷歌人工智能工作室.OPENAI_API_KEY:已检查是否存在,但 当前未使用 此版本中的任何工具。MISTRAL_API_KEY:已检查是否存在,但 当前未使用 此版本中的任何工具。
代理交互示例
# Example using the agent CLI from the previous setup
You: scrape_url https://example.com
Agent: Thinking...
[Agent calls scrape_url tool]
Agent: [Markdown content of example.com]
------------------------------
You: extract text from https://en.wikipedia.org/wiki/Web_scraping using the query "ethical considerations"
Agent: Thinking...
[Agent calls extract_text_by_query tool]
Agent: Found X matches for 'ethical considerations' on the page. Here are the relevant sections:
Match 1:
... text snippet ...
---
Match 2:
... text snippet ...
------------------------------
You: Use smart_extract on https://blog.google/technology/ai/google-gemini-ai/ to get the main points about Gemini models
Agent: Thinking...
[Agent calls smart_extract tool with Google API Key]
Agent: Successfully extracted information based on your instruction:
{
"main_points": [
"Gemini is Google's most capable AI model family (Ultra, Pro, Nano).",
"Designed to be multimodal, understanding text, code, audio, image, video.",
"Outperforms previous models on various benchmarks.",
"Being integrated into Google products like Bard and Pixel."
]
}
文件
your_server_script_name.pyMCP服务器的主Python脚本(例如。,webcrawl_mcp_server.py).Dockerfile:构建Docker容器镜像的说明。requirements.txt:Python依赖关系。.env.example:(推荐)显示所需密钥的示例环境文件。 不要承诺你的实际.env文件。.gitignore:指定Git有意未跟踪的文件(应包括.env).README.md:这个文件。
贡献
(如有需要,可添加捐款指南)
许可证
(指定您的许可证,例如MIT许可证)

