Crawlab-MCP模型上下文
一种模型上下文协议服务器,允许人工智能应用程序通过自然语言与Crawlab的功能进行交互,从而实现蜘蛛管理、任务执行和文件操作。
服务介绍
Crawlab MCP 服务器
这是一个用于Crawlab的模型上下文协议(MCP)服务器,允许AI应用程序与Crawlab的功能进行交互。
概览
MCP服务器为AI应用程序提供了一种标准化的方式来访问Crawlab的功能,包括:
- 爬虫管理(创建、读取、更新、删除)
- 任务管理(运行、取消、重启)
- 文件管理(读取、写入)
- 资源访问(爬虫、任务)
架构
MCP 服务器/客户端架构促进了AI应用程序与Crawlab之间的通信:
graph TB
User[User] --> Client[MCP Client]
Client --> LLM[LLM Provider]
Client <--> Server[MCP Server]
Server <--> Crawlab[Crawlab API]
subgraph "MCP System"
Client
Server
end
subgraph "Crawlab System"
Crawlab
DB[(Database)]
Crawlab <--> DB
end
class User,LLM,Crawlab,DB external;
class Client,Server internal;
%% Flow annotations
LLM -.-> |Tool calls| Client
Client -.-> |Executes tool calls| Server
Server -.-> |API requests| Crawlab
Crawlab -.-> |API responses| Server
Server -.-> |Tool results| Client
Client -.-> |Human-readable response| User
classDef external fill:#f9f9f9,stroke:#333,stroke-width:1px;
classDef internal fill:#d9edf7,stroke:#31708f,stroke-width:1px;
通信流程
- 用户查询:用户向MCP客户端发送自然语言查询
- LLM处理:客户端将查询转发给LLM提供商(例如,Claude, OpenAI)
- 工具选择:LLM识别必要的工具并生成工具调用
- 工具执行:客户端将工具调用发送到MCP服务器
- API交互:服务器执行相应的Crawlab API请求
- 响应生成:结果通过服务器返回到客户端再到LLM
- 用户响应:客户端将最终的人类可读响应传递给用户
安装和使用
选项1:作为Python包安装
您可以将MCP服务器安装为Python包,这提供了方便的CLI:
# Install from source
pip install -e .
# Or install from GitHub (when available)
# pip install git+https://github.com/crawlab-team/crawlab-mcp-server.git
安装后,您可以使用CLI:
# Start the MCP server
crawlab_mcp-mcp server [--spec PATH_TO_SPEC] [--host HOST] [--port PORT]
# Start the MCP client
crawlab_mcp-mcp client SERVER_URL
选项2:本地运行
前提条件
- Python 3.8+
- 运行中且可访问的Crawlab实例
- 来自Crawlab的API令牌
配置
-
将
.env.example文件复制为.env:cp .env.example .env -
使用您的Crawlab API详细信息编辑
.env文件:CRAWLAB_API_BASE_URL=http://your-crawlab-instance:8080/api CRAWLAB_API_TOKEN=your_api_token_here
本地运行
-
安装依赖项:
pip install -r requirements.txt -
运行服务器:
python server.py
使用Docker运行
-
构建Docker镜像:
docker build -t crawlab-mcp-server . -
运行容器:
docker run -p 8000:8000 --env-file .env crawlab-mcp-server
与Docker Compose集成
要将MCP服务器添加到您现有的Crawlab Docker Compose设置中,请在您的docker-compose.yml中添加以下服务:
services:
# ... existing Crawlab services
mcp-server:
build: ./backend/mcp-server
ports:
- "8000:8000"
environment:
- CRAWLAB_API_BASE_URL=http://backend:8000/api
- CRAWLAB_API_TOKEN=your_api_token_here
depends_on:
- backend
与AI应用程序一起使用
MCP服务器使AI应用程序能够通过自然语言与Crawlab进行交互。根据上面的架构图,以下是使用MCP系统的方法:
设置连接
- 启动MCP服务器:确保您的MCP服务器正在运行且可访问
- 配置AI客户端:将您的AI应用程序连接到MCP服务器
示例:与Claude桌面版一起使用
- 打开 Claude 桌面版
- 进入设置 > MCP 服务器
- 添加一个新的服务器,并输入你的 MCP 服务器的 URL(例如,
http://localhost:8000) - 在与 Claude 的对话中,现在你可以通过自然语言描述你想要执行的操作来使用 Crawlab 功能
示例交互
基于我们的架构,这里是与系统进行的一些示例交互:
创建爬虫:
User: "Create a new spider named 'Product Scraper' for the e-commerce project"
↓
LLM identifies intent and calls the create_spider tool
↓
MCP Server executes the API call to Crawlab
↓
Spider is created and details are returned to the user
运行任务:
User: "Run the 'Product Scraper' spider on all available nodes"
↓
LLM calls the run_spider tool with appropriate parameters
↓
MCP Server sends the command to Crawlab API
↓
Task is started and confirmation is returned to the user
可用命令
你可以使用以下自然语言命令与系统交互:
- “列出我所有的爬虫”
- “根据这些规格创建一个新的爬虫...”
- “显示名为 X 的爬虫的代码”
- “使用这段代码更新爬虫 X 中的 main.py 文件...”
- “运行爬虫 X 并在我完成后通知我”
- “显示爬虫 X 上次运行的结果”
可用资源和工具
这些是支持自然语言交互的基础工具:
资源
spiders: 列出所有爬虫tasks: 列出所有任务
工具
爬虫管理
get_spider: 获取特定爬虫的详情create_spider: 创建新的爬虫update_spider: 更新现有的爬虫delete_spider: 删除一个爬虫
任务管理
get_task: 获取特定任务的详情run_spider: 运行一个爬虫cancel_task: 取消正在运行的任务restart_task: 重启一个任务get_task_logs: 获取任务的日志
文件管理
get_spider_files: 列出爬虫的所有文件get_spider_file: 获取特定文件的内容save_spider_file: 将内容保存到文件中