无服务器网页抓取工具

一个基于Model Context Protocol (MCP)的无服务器网页抓取工具,为LLMs(如Claude、Gemini或自定义机器人)提供实时网页数据访问。

  • by Kishoraditya
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Kishoraditya/mcp_tutorial

使用说明

无服务器MCP刮刀

强大的无服务器 模型上下文协议(MCP) 用于网络抓取的服务器,旨在让LLM(如Claude、Gemini或自定义机器人)访问实时网络数据。

特性

  • 无服务器:在AWS Lambda上运行(或一般通过Docker运行)。扩展到零成本。
  • 无状态架构:使用HTTP JSON-RPC(POST /mcp)以实现强大的Lambda兼容性。
  • 安全:受API密钥验证和SSRF验证(TODO)保护。
  • 工具:
    • scrape_url:获取页面,去除杂乱(广告/导航/脚本),并返回干净的Markdown。
    • get_page_metadata:提取标题、描述和作者。

快速入门(本地)

  1. 再进行

    python -m venv venv
    source venv/bin/activate  # or venv\Scripts\activate on Windows
    pip install -r requirements.txt
    
  2. 运行服务器

    uvicorn app.main:app --port 8000 --reload
    
  3. 测试IT

    Linux/Mac:

    curl -X POST http://localhost:8000/mcp \
      -H "Content-Type: application/json" \
      -H "X-API-Key: dev-key" \
      -d '{"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "scrape_url", "arguments": {"url": "https://example.com"}}, "id": 1}'
    

    Windows PowerShell:

    Invoke-RestMethod -Uri "http://localhost:8000/mcp" `
      -Method Post `
      -Headers @{ "X-API-Key" = "dev-key" } `
      -ContentType "application/json" `
      -Body '{"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "scrape_url", "arguments": {"url": "https://example.com"}}, "id": 1}' `
      | Select-Object -ExpandProperty result | Select-Object -ExpandProperty content | Select-Object -ExpandProperty text
    

连接到LLM

选项A:本地客户端(例如,Claude Desktop)

由于Claude Desktop在本地运行,您可以将其指向本地服务器或部署的URL。 修改您的 claude_desktop_config.json:

{
  "mcpServers": {
    "scraper": {
      "command": "uv",
      "args": ["run", "mcp-scraper"],
      "env": {"MCP_API_KEY": "dev-key"},
       "url": "http://localhost:8000/mcp" 
    }
  }
}

选项B:自定义Bot(LangChain/neneneba API)

查看我们的 聊天机器人教程.

你的机器人需要执行一个基本的“工具调用”流程:

  1. LLM输出JSON: {"tool": "scrape_url", "url": "..."}.
  2. Bot向以下对象发出POST请求 https://<lambda-url>/mcp.
  3. Bot将响应反馈给LLM。

部署(AWS Lambda)

此项目配置为 AWS-SAM.

  1. 构建

    sam build
    
  2. 部署

    sam deploy --guided
    

    提示时输入所需的API密钥。

  3. 获取URL 输出将提供您的公共函数URL。在您的 mcp_server_config.json.