使用说明
项目简介
本项目 'mcp_example' 提供了一组示例,展示了如何构建和使用 MCP (Model Context Protocol) 服务器。它包含两个独立的MCP服务器示例:
- 天气服务器 (weather_server.py): 提供查询美国国家气象局 (NWS) 天气预报和警报的工具。
- 智能灯光服务器 (keylight_server.py): 控制 Elgato Key Light 智能灯光的工具。
同时,项目也包含了MCP客户端示例,可以连接到这些服务器并利用提供的工具与LLM进行交互。
主要功能点
- 资源 (Resources) 管理 (示例中未显式展示): 虽然示例没有直接展示资源管理,但MCP服务器的设计理念支持资源托管和管理。
- 工具 (Tools) 注册和执行:
- 天气服务器注册了 'get_alerts' (获取州天气警报) 和 'get_forecast' (获取经纬度天气预报) 工具。
- 智能灯光服务器注册了 'get_keylight_status' (获取灯光状态) 和 'set_keylight_status' (设置灯光状态) 工具。
- Prompt 模板 (Prompts) (示例中未显式展示): 示例中没有定义Prompt模板,但MCP服务器支持Prompt模板的定义和渲染,以定制LLM交互模式。
- JSON-RPC 通信: 服务器和客户端通过 JSON-RPC 协议进行通信。
- SSE (Server-Sent Events) 传输协议: 示例服务器默认使用 SSE 作为传输协议。
- Stdio 传输协议: 天气服务器 ('weather_server.py') 注释掉了一行代码 'mcp.run(transport='stdio')',表明也支持 Stdio 传输协议。
- 客户端示例: 提供了 Python 客户端代码 ('client/single/main.py' 和 'client/multi/main.py'),演示如何连接和使用 MCP 服务器。
安装步骤
-
克隆仓库:
git clone https://github.com/moniquelive/mcp_example cd mcp_example -
安装依赖: 确保已安装 Python 3.7 或更高版本,然后使用 pip 安装项目依赖:
pip install -r requirements.txt -
配置环境变量:
- 智能灯光服务器 (keylight_server.py): 需要设置 'KEYLIGHT_API_BASE' 环境变量,指向你的 Elgato Key Light 的 API Base URL。你需要在你的 Key Light 设备设置中找到这个 URL。例如:
export KEYLIGHT_API_BASE="http://your_keylight_ip:9123" - Anthropic API Key (客户端): 客户端 ('client.py') 使用 Anthropic Claude 模型。你需要设置 'ANTHROPIC_API_KEY' 环境变量,替换为你的 Anthropic API 密钥。例如:
export ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY"
- 智能灯光服务器 (keylight_server.py): 需要设置 'KEYLIGHT_API_BASE' 环境变量,指向你的 Elgato Key Light 的 API Base URL。你需要在你的 Key Light 设备设置中找到这个 URL。例如:
服务器配置
MCP客户端配置 (以 'client/single/main.py' 为例):
MCP客户端需要配置连接到MCP服务器的信息。对于本示例,你需要配置两个服务器:天气服务器和智能灯光服务器。
1. 天气服务器配置 (weather_server.py):
- 服务器名称 (server name): 'weather' (在 'weather_server.py' 中定义 'mcp = FastMCP("weather")')
- 启动命令 (command): 'python'
- 命令参数 (args): 'server/weather_server.py'
- 环境变量 (env) (可选): 无特殊环境变量
{ "server_name": "weather", "command": "python", "args": ["server/weather_server.py"], "env": {} }
参数注释:
- '"server_name"': MCP服务器的名称,用于客户端识别和调用工具。
- '"command"': 启动服务器的可执行命令,这里是 'python'。
- '"args"': 传递给启动命令的参数列表,这里指定了天气服务器脚本的路径 'server/weather_server.py'。
- '"env"': 可选的环境变量字典,如果服务器需要特定的环境变量才能运行,可以在这里配置。天气服务器示例不需要额外的环境变量。
2. 智能灯光服务器配置 (keylight_server.py):
- 服务器名称 (server name): 'keylight' (在 'keylight_server.py' 中定义 'mcp = FastMCP("keylight")')
- 启动命令 (command): 'python'
- 命令参数 (args): 'server/keylight_server.py'
- 环境变量 (env): 需要设置 'KEYLIGHT_API_BASE' 环境变量。
{ "server_name": "keylight", "command": "python", "args": ["server/keylight_server.py"], "env": { "KEYLIGHT_API_BASE": "http://your_keylight_ip:9123" // 请替换为你的 Key Light API Base URL } }
参数注释:
- '"server_name"': MCP服务器的名称,用于客户端识别。
- '"command"': 启动服务器的命令,'python'。
- '"args"': 服务器脚本路径 'server/keylight_server.py'。
- '"env"': 环境变量字典,这里配置了 'KEYLIGHT_API_BASE',请务必替换 '"http://your_keylight_ip:9123"' 为你实际的 Key Light API Base URL。
MCP客户端如何使用这些配置:
MCP客户端 (例如 'client/single/main.py' 或 'client/multi/main.py') 需要根据这些配置信息来启动和连接 MCP 服务器。 'client/single/main.py' 示例中,如果运行 'python client/single/main.py server/weather_server.py',它会使用 Stdio 传输协议启动并连接到 'weather_server.py'。如果直接运行 'python client/single/main.py',它会默认连接到 SSE 服务器 'http://localhost:8000/sse' (需要服务器先以 SSE 模式运行)。 'client/multi/main.py' 则预设了连接到两个 SSE 服务器的配置 ('http://localhost:8000/sse' - Key Light, 'http://localhost:8001/sse' - Weather)。
基本使用方法
-
启动服务器:
-
天气服务器 (SSE 模式): 在终端中,导航到 'mcp_example' 目录,并运行:
FASTMCP_PORT=8001 python server/weather_server.py这将启动天气服务器,监听 8001 端口的 SSE 连接。
-
智能灯光服务器 (SSE 模式): 在另一个终端中,导航到 'mcp_example' 目录,设置 'KEYLIGHT_API_BASE' 环境变量,并运行:
export KEYLIGHT_API_BASE="http://your_keylight_ip:9123" # 替换为你的 Key Light API Base URL FASTMCP_PORT=8000 python server/keylight_server.py这将启动智能灯光服务器,监听 8000 端口的 SSE 连接。
-
-
运行客户端:
-
单服务器客户端 (client/single/main.py, 连接到 SSE 服务器):
ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY" python client/single/main.py这将启动客户端,默认连接到 'http://localhost:8000/sse' (假设 Key Light 服务器运行在 8000 端口)。 你可以在客户端中输入查询,例如 "What's the weather in San Francisco?" 或 "Turn on the key light"。
-
单服务器客户端 (client/single/main.py, 连接到 Stdio 服务器):
ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY" python client/single/main.py server/weather_server.py这将启动客户端,并使用 Stdio 传输协议直接连接到 'server/weather_server.py' 脚本。
-
多服务器客户端 (client/multi/main.py, 连接到多个 SSE 服务器):
ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY" python client/multi/main.py这将启动客户端,同时连接到 Key Light 服务器 (8000 端口) 和天气服务器 (8001 端口)。
-
-
与客户端交互: 客户端启动后,你可以在命令行中输入自然语言查询,客户端会调用相应的 MCP 工具,并结合 LLM (Claude) 的能力给出回复。 例如:
- 查询天气: 'What are the weather alerts for California?'
- 查询预报: 'What's the forecast for latitude 37.7749 and longitude -122.4194?' (旧金山)
- 获取灯光状态: 'What's the key light status?'
- 设置灯光: 'Turn on the key light and set brightness to 50'
-
退出: 在客户端输入 'quit' 或 'exit' 可以安全退出。
注意:
- 确保你的 Elgato Key Light 设备已连接到网络,并且你已获取到其 API Base URL。
- 确保你已设置有效的 Anthropic API Key。
- 示例客户端代码使用了 'claude-3-5-sonnet-20241022' 模型,你可能需要根据你的 Anthropic API 访问权限调整模型名称。
- 如果你遇到连接问题,请检查服务器和客户端的网络配置,以及端口是否被占用。
信息
分类
网页与API