WebMCP🌐
基于Python的模型上下文协议(MCP)服务器,为AI助手提供网络搜索和URL分析工具。采用FastMCP框架构建,可与Claude Desktop和其他兼容MCP的AI工具无缝集成。
🚀 特性
- 🔍 网页搜索:DuckDuckGo支持内容提取的搜索
- 📊 URL分析:元数据提取和内容预览
- ⚡ FastMCP集成:基于装饰器的工具创建
- 🔧 不需要API密钥:使用免费网络搜索服务
- 🛡️ 错误处理:优雅的错误响应和超时
- 📝 类型安全:完整的类型提示和验证
📋 需求
- Python 3.8+
- MCP Python SDK
- FastMCP框架
- aiohttp&BeautifulSoup4
🛠️ 安装
1.克隆和安装依赖项
git clone https://github.com/yourusername/WebMCP.git
cd WebMCP
pip install -r requirements.txt2.为Claude Desktop安装
# Install server for Claude Desktop
mcp install server.py
# Install with custom name
mcp install server.py --name "WebMCP Server"
# Install with environment variables
mcp install server.py -v API_KEY=abc123 -v DB_URL=postgres://...3.发展模式
# Run with MCP Inspector for testing
mcp dev server.py
# Run with additional dependencies
mcp dev server.py --with pandas --with numpy
# Mount local code for live updates
mcp dev server.py --with-editable .🎯 可用工具
web_search
使用带有可选内容提取功能的DuckDuckGo搜索网络。
参数:
query(字符串,必填):搜索查询limit(整数,可选):最大结果(默认值:5)include_content(布尔值,可选):获取页面内容(默认值:true)
例子:
result = await session.call_tool(
"web_search",
arguments={
"query": "latest AI developments",
"limit": 3,
"include_content": True
}
)url_info
获取任何URL的元数据和内容预览。
参数:
url(字符串,必填):要分析的URL
例子:
result = await session.call_tool(
"url_info",
arguments={"url": "https://example.com"}
)📁 资源
web://search/{query}:搜索结果作为文本资源
💬 提示词
search_prompt:创建全面的搜索和分析提示
🧪 测试
运行测试套件以验证功能:
python mcpTest.py这将:
- 列出所有可用的工具、资源和提示
- 测试网络搜索功能
- 测试URL分析功能
🔧 发展
项目结构
WebMCP/
├── server.py # Main FastMCP server
├── mcpTest.py # Test client
├── requirements.txt # Python dependencies
├── README.md # This file
├── .gitignore # Git ignore rules
└── CLAUDE.md # Claude Code instructionsFastMCP模式
服务器遵循FastMCP最佳实践:
from mcp.server.fastmcp import FastMCP
# Create server
mcp = FastMCP("WebMCP")
# Define tools
@mcp.tool()
async def my_tool(param: str) -> str:
"""Tool description"""
return f"Result: {param}"
# Define resources
@mcp.resource("data://{id}")
def get_data(id: str) -> str:
"""Get data resource"""
return f"Data for {id}"
# Define prompts
@mcp.prompt()
def my_prompt(topic: str) -> str:
"""Create prompt"""
return f"Analyze: {topic}"
if __name__ == "__main__":
mcp.run()🌟 用法示例
Claude桌面集成
- 安装服务器:
mcp install server.py - 重新启动克劳德桌面
- 在对话中使用网络搜索:
- “探索量子计算的最新发展” - “分析此URL:https://example.com" - “查找有关可再生能源趋势的信息”
程序化使用
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
import asyncio
async def search_web():
server_params = StdioServerParameters(
command="python",
args=["server.py"]
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
result = await session.call_tool(
"web_search",
arguments={"query": "Python programming", "limit": 5}
)
return result
# Run the search
results = asyncio.run(search_web())🔒 安全与隐私
- 没有API密钥:使用DuckDuckGo(以隐私为中心的搜索)
- 本地处理:所有内容提取都在本地进行
- 可配置超时:防止挂在速度较慢的网站上
- 错误处理:巧妙的故障不会使服务器崩溃
🤝 贡献
- 分叉存储库
- 创建要素分支:
git checkout -b feature-name - 进行更改并测试:
python mcpTest.py - 提交您的更改:
git commit -m "Add feature" - 推到分支:
git push origin feature-name - 提交拉取请求
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
🙏 致谢
- FastMCP -MCP框架
- MCP Python SDK -协议实施
- DuckDuckGo -搜索功能
- 美丽汤 -HTML解析
📞 支持
- 🐛 错误报告:
- 💡 功能请求:
- 📧 问题:打开讨论或问题
______________________________________________________________________
WebMCP -将网络搜索的力量带给人工智能助手,一次一个MCP工具! 🚀
