使用说明
简单MCP工具服务器
一个简单的MCP服务器,它公开了一个使用SSE传输的网站获取工具。
需求
- Python 3.10或更高版本(在Python 3.13上测试)
安装
# Create a virtual environment
python3 -m venv venv
# Activate the virtual environment
source venv/bin/activate
# Install the package and dependencies
pip install -r requirements.txt
MCP Python SDK文档
MCP Python SDK文档已拆分为较小的文件,并组织在 docs/ 目录。这种结构使AI代理更容易导航和理解SDK。文件包括:
- 核心概念(服务器、资源、工具等)
- 以不同模式运行MCP服务器
- 示例和高级用法
- 还有更多!
用法
该软件包提供了一个命令行界面(CLI),其中包含几个命令来管理MCP服务器:
启动服务器
在默认端口(7000)上启动服务器或指定自定义端口:
# Using default port (7000)
python -m mcp_simple_tool start
# Using custom port
python -m mcp_simple_tool start --port 8000
管理服务器
# Check if server is running
python -m mcp_simple_tool check [--port PORT]
# Stop the server
python -m mcp_simple_tool stop [--port PORT]
# Restart the server (stop and start)
python -m mcp_simple_tool restart [--port PORT]
重启命令将:
- 停止指定端口上的任何现有服务器
- 在后台启动新服务器
- 等待服务器响应
- 日志输出到server.Log
CLI快速参考
| 命令 | 目的 |
|---|---|
start |
启动服务器 |
stop |
停止服务器 |
check |
健康检查 |
restart |
停止和启动 |
服务器工具
服务器公开了以下工具:
-
获取: 远程 HTTP提取器——给出一个绝对URL;返回页面文本。
url:要获取的网站的URL(必填)
-
search_docs:跨SDK文档的语义搜索;返回前k名摘录。
query:搜索短语或问题(必填)k:要返回的顶级匹配数(可选,默认值=3)
-
get_content:获取 完整本地文件 对于返回的任何比赛
search_docs.file:相对于文档的路径(必填)
开发设置
对于开发,请安装其他工具:
pip install -e .
pip install -r requirements.txt
使用Makefile执行常见任务:
# Format code
make fmt
# Run linters
make lint
# Run tests
make test
该测试套件为所有测试提供了一个内置的20秒超时,以防止挂起,特别是在SSE端点的情况下。对于单个测试,可以使用以下命令指定更严格的超时 @pytest.mark.timeout(seconds) 装饰师。
语义搜索索引
对于search_docs工具,您可以手动构建或重建向量索引:
# Build or rebuild the semantic search index
python scripts/build_doc_index.py
如果索引不存在,则会在首次使用工具时自动构建。
项目架构
mcp_simple_tool/
__init__.py # Package initialization
__main__.py # Entry point when run as module
cli.py # Command-line interface
server/ # Server implementation
__init__.py # Server package initialization
app.py # ASGI application setup
config.py # Configuration settings
handlers.py # Tool implementations
http.py # HTTP utilities
semantic_search/ # Semantic search functionality
__init__.py # Package initialization
indexing.py # Build and persist vector store
search.py # Load index and query helpers
与光标一起使用
此MCP服务器可以与Cursor一起作为客户端使用。对于设置:
- 在终端中运行服务器:
source venv/bin/activate
python -m mcp_simple_tool start
# or use the restart command
python -m mcp_simple_tool restart
- 通过创建一个
.cursor/mcp.json文件:
{
"mcpServers": {
"website-fetcher-sse": {
"url": "http://localhost:7000/sse"
}
}
}
- 使用Cursor时,在提示中提及服务器
