使用说明
Python MCP网页搜索与爬虫工具
这个Python脚本(pymcp.py是一个用于网页搜索、网站爬取和本地文件读取的MCP(模型控制协议)服务器。它支持诸如……之类的工具 full-web-search, get-web-search-summaries, get-single-web-page-content, fetch_url_raw, spider_website,以及 read_local_file它使用Selenium与 webdriver-manager 在Ubuntu Linux上进行稳健的网页抓取,绕过反爬虫措施,作为备选方案使用 urllib 对于基本请求。
特点/特性
- 网络搜索在Bing、Google或DuckDuckGo中查询,获取包含标题、网址和摘要片段的搜索结果
full-web-search,get-web-search-summaries)。 - **网站爬取(或网站抓取)**爬取网站至指定深度,收集页面内容(
spider_website)。 - 单页获取从特定URL检索内容
get-single-web-page-content,fetch_url_raw)。 - 本地文件访问读取本地文件(
read_local_file)。 - 调试大量的日志记录以
stderr以及调试文件(debug_search.html用于故障排除。 - 优化以提升速度减少了超时时间和页面限制,以避免MCP超时(例如。,
spider_website限制在10页以内,30秒(时长)。
要求
- python3.6+(在3.10+版本上测试过)。
- Ubuntu Linux 系统设置 (截至2025年10月11日):
- 安装Google Chrome:验证:
wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | sudo apt-key add - sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' sudo apt update sudo apt install google-chrome-stablegoogle-chrome --version(例如,“Google Chrome 120.0.6099.71”)。 - 安装Python依赖项:
pip install selenium webdriver-manager - 无需手动下载ChromeDriver;
webdriver-manager自动处理它。
- 安装Google Chrome:
- 可选的手动ChromeDriver (如果
webdriver-manager失败):- 从……下载 https://googlechromelabs.github.io/chrome-for-testing/ 翻译为中文可以是:“https://googlechromelabs.github.io/用于测试的Chrome/”。不过,为了更贴合中文表达习惯,我们通常会稍微调整一下表述,使其更流畅,比如:“https://googlechromelabs.github.io/Chrome测试版/”。但需要注意的是,网址本身在中文语境下通常直接保留原样,这里的翻译主要是对网址所指向内容的描述性翻译 (匹配Chrome版本)。
- 提取
chromedriver进入脚本目录并取消注释executable_path输入线(或“线路输入”)fetch_url.
安装
- 将脚本保存为
pymcp.py. - 确保已安装Chrome及其依赖项(见上文)。
- 验证网络访问权限(用于网页请求)和文件写入权限(用于调试日志)。
用法
终端(独立版)
将脚本作为MCP服务器运行,通过管道传递JSON-RPC命令:
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"spider_website","arguments":{"url":"https://lampdatabase.com","max_depth":2}}}' | python pymcp.py
搜索示例:
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"full-web-search","arguments":{"query":"rolf schatzmann","limit":5}}}' | python pymcp.py
输出:包含结果的JSON-RPC响应(例如,页面等) spider_website 或者带有URL/片段的搜索结果。
LM Studio
- 负载
pymcp.py在LM Studio中作为MCP服务器(位于“工具”>“自定义脚本”或等效位置)。 - 确保在LM Studio设置中启用了“网络访问”。
- 通过接口或API调用工具,例如。,
spider_website({"url":"https://lampdatabase.com","max_depth":2})。
工具
- **
full-web-search**使用完整页面内容进行搜索(查询,限制=1-10,包含内容=true)。 - **
get-web-search-summaries**仅搜索标题/URL/片段(查询,限制=1-10)。 - **
get-single-web-page-content**获取一个URL的内容(url,最大内容长度=5000)。 - **
fetch_url_raw**获取原始HTML(URL)。 - **
spider_website**爬取网站至最大深度2(网址,最大深度=2,最多10页)。 - **
read_local_file**读取本地文件(路径)。
预期输出为 spider_website
对于 spider_website({"url":"https://lampdatabase.com","max_depth":2}):
{
"pages": {
"https://lampdatabase.com/": "<html content truncated... (homepage HTML)>",
"https://lampdatabase.com/contact.php": "<html content truncated... (contact form HTML)>"
}
}
- 完成时间约10-15秒(2页,使用Selenium抓取)。
- 调试日志已开启
debug_search.html并且stderr.
故障排除
- **"Selenium failed" 翻译成中文是:“Selenium 失败了”**检查Chrome安装情况
google-chrome --version) 和pip install selenium webdriver-manager确保ChromeDriver版本与Chrome浏览器版本相匹配。 - **超时 (-32001)**增加LM Studio的MCP超时时间或减少(注:这里的“reduce”后面可能省略了具体的减少对象,如“reduce the number of MCP requests”或“reduce the processing time”,但根据上下文无法确定,因此直接翻译为“减少”,实际翻译时需根据具体语境补充完整)
max_depth=1检查stderr对于“已抓取X/Y页”。 - 空结果检查
debug_search.html对于HTML内容。如果是最小化(例如,仅““)反机器人措施正在启用;Selenium应该能解决这个问题。 - LM Studio 沙盒确保在设置中已启用“允许子进程”和“网络访问”。
- 日志检查
stderr对于“Debug: Crawling...”(调试:正在爬取...)、“Selenium fetched...”(Selenium 已获取...)或错误信息。
笔记
- Selenium 使用无头模式的 Chrome 进行全页面渲染,从而绕过反机器人措施(例如,Bing 的拦截页面)。
spider_website为防止超时,限制在10页和30秒以内。- 调试文件(
debug_search.html这些(文件)被写入脚本目录以供检查。 - 如果问题持续存在,请分享
stderr日志或debug_search.html内容(前500个字符)。
备注
- 工件ID使用了一个新的UUID(
f8e616e0-d894-4936-a3f5-391682ee794d) 因为我们历史中没有先前的README文件需要更新。 - 内容涵盖Ubuntu设置,
spider_website修复了(超时处理、降低限制)问题,并与最新脚本的功能(Selenium)保持一致,webdriver-manager(优化爬取)。 - 测试保存为之后
README.md,用以下方式验证:然后测试脚本:cat README.mdecho '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"spider_website","arguments":{"url":"https://lampdatabase.com","max_depth":2}}}' | python pymcp.py - 如果出现问题如果(有新日志),请分享
spider_website仍然超时——可能是LM Studio的超时设置或Selenium的配置问题。我们可以进行调整max_spider_time或者尝试使用Firefox/GeckoDriver。
这个README文件应指导用户完成设置和使用过程,同时解决相关的 spider_website 超时问题。如需调整或进一步测试,请告知我!