使用说明
MCP演示
此存储库包含模型上下文协议(MCP)服务器的演示实现。该服务器旨在管理组织和设备,计算设备与其关联组织之间的距离,并提供一组与数据交互的工具。
特性
- 数据库集成:连接到PostgreSQL数据库以管理组织和设备。
- 距离计算:使用Haversine公式计算设备与其关联组织之间的距离。
- MCP工具:提供获取组织、设备和计算距离的工具。
先决条件
- Python 3.12.5
- PostgreSQL数据库
设置
-
克隆存储库:
git clone <repository-url> cd mcp-demo -
设置虚拟环境:
uv venv source .venv/bin/activate -
在中配置环境变量
.env文件:DB_NAME=mcp DB_USER=mcp_user DB_PASSWORD=pa$$word DB_HOST=localhost DB_PORT=5432 -
设置数据库:
psql -U <username> -d <database> -f tables.sql
用法
-
运行MCP服务器:
uv run main.py -
使用提供的工具与服务器交互:
get_organizations:从数据库中获取所有组织。get_devices:从数据库中获取所有设备。get_devices_by_organization:获取特定组织的设备。list_devices_with_distance:列出设备及其与相关组织的距离。
项目结构
main.py:MCP服务器的主要入口点。tables.sql:用于设置数据库模式和种子数据的SQL脚本。.env:数据库配置的环境变量。pyproject.toml:项目元数据和依赖关系。
依赖项
httpx:Python的HTTP客户端。mcp[cli]:用于构建服务器的MCP框架。psycopg2:Python的PostgreSQL数据库适配器。
Claude桌面MCP服务器配置
要为此MCP服务器项目配置Claude Desktop,请执行以下步骤:
-
从官方网站在您的计算机上安装Claude Desktop。
-
打开Claude Desktop并导航到设置菜单。
-
为MCP服务器添加新的项目配置:
code ~/Library/Application\ Support/Claude/claude_desktop_config.json -
添加mcp服务器
{
"mcpServers": {
"devices": {
"command": "uv",
"args": [
"--directory",
"/path/to/mcp-demo",
"run",
"main.py"
],
"env": {
"DB_NAME": "mcp",
"DB_USER": "mcp_user",
"DB_PASSWORD": "pa$$word",
"DB_HOST": "localhost",
"DB_PORT": "5432"
}
}
}
}
- 运行查询,例如:
List the devices in DB and show me how far are they from their organization address. Also, add get the mac addresses of the devices.