关于 Proyecto Cliente MCP con Gemini y Servidor de Herramientas Meteorológicas
这是一个 Python 示例项目,将 Gemini 客户端与天气工具服务端配合使用,展示 AI 如何调用天气工具并将结果整理为面向用户的回答。
主要功能
- 01Gemini 客户端与天气服务端
- 02天气预警和预报工具
- 03工具发现与调用
- 04客户端和服务端合并日志
适用场景
- 01回答本地天气问题
- 02检查 MCP 与 Gemini 的请求流程
MCP 相关
客户端连接天气 MCP 服务,发现并调用 get_alerts 和 get_forecast 等工具。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
简而言之 / 概述
- 安装依赖项:
pip install -r requirements.txt - 创建
.env,并添加GEMINI_API_KEY=TU_CLAVE_API_AQUI。 - 运行:
python mcp_gemini_client.py weather_tool_server.py
使用 Gemini 的 MCP 客户端与气象工具服务
此代码库演示了如何使用 MCP(Model-Client-Protocol)协议,将 AI 客户端(使用 Google Gemini)连接到提供专用工具的服务(此处为气象工具)。
工作原理
该系统充当 Gemini AI 与专用工具之间的桥梁。下面以简单的方式说明其流程:
组件角色
-
mcp_gemini_client.py- 这是一个“中间层”,用于:- 与 Gemini AI 通信。
- 连接到工具服务(
weather_tool_server.py)。 - 处理用户输入并显示回复。
-
weather_tool_server.py- 这是一个“工具提供方”,用于:- 提供与天气相关的专用工具(
get_alerts、get_forecast)。 - 处理工具请求并返回结果。
- 提供与天气相关的专用工具(
-
MCP(Model-Client-Protocol) - 这是一个“通信系统”,用于:
- 为客户端发现和调用工具提供标准化方式。
- 处理客户端与服务之间的消息传递。
天气查询流程
- 用户输入:在客户端中输入类似“纽约的天气如何?”的问题。
- 客户端处理:
mcp_gemini_client.py将此问题发送给 Gemini AI。- 它还会通过查询
weather_tool_server.py,告知 Gemini 有哪些可用工具。
- Gemini AI 的决策:
- Gemini 会识别出需要使用气象工具(例如
get_forecast)。 - 它会格式化回复,指出要使用哪个工具以及要传入哪些参数(例如纽约的纬度和经度)。
- Gemini 会识别出需要使用气象工具(例如
- 工具执行:
- 客户端解析 Gemini 发出的工具调用请求。
- 将工具请求发送到
weather_tool_server.py。 - 服务执行天气查询并返回结果。
- 最终回复:
- 工具结果返回给客户端,客户端随后将其发送回 Gemini。
- Gemini 将这些结果整理成用户易于理解的回复。
- 客户端显示最终回复。
配置与运行
- 安装依赖项:
pip install google-generativeai python-dotenv mcp-protocols # 确保 mcp-protocols 是正确的软件包 - API 密钥:
- 从 Google AI Studio 获取 API 密钥。
- 在项目根目录中创建
.env文件。 - 将密钥添加到
.env文件中:GEMINI_API_KEY=TU_CLAVE_API_AQUI
- 运行:
- 确保
mcp_gemini_client.py和weather_tool_server.py位于同一目录中。 - 运行客户端,并告知它服务器脚本的位置:
python mcp_gemini_client.py weather_tool_server.py - 按提示在终端中输入查询。
- 确保
日志文件(mcp_log.txt)
- 集中且详细: 此文件现在包含客户端(
mcp_gemini_client.py)和服务端(weather_tool_server.py)的合并日志。 - 流程可见性: 日志有意记录得十分详细,以展示完整的数据流:
- 发送到 Gemini API 和从 Gemini API 接收的消息(包括提示词)。
- 客户端与服务端之间通过 MCP 协议发送和接收的工具请求/响应。
- 标识: 每条日志都包含模块名称(
mcp_gemini_client或weather_tool_server),方便你识别生成该消息的组件。 - 调试: 这是调试问题并了解各组件之间交互的主要来源。
日志流示例(mcp_log.txt)
以下是完整交互日志的简化示例,展示了关键通信内容:
-
客户端向 Gemini 发送初始提示词(包含工具描述):
2025-04-21 12:31:54,519 - __main__ - INFO - === Prompt Sent to Gemini (Initial) === You are an assistant that helps users with weather information. You have access to the following tools: ... Tool: get_forecast Description: Get weather forecast for a location... Schema: {...} ... When you need to use a tool, format your response like this: TOOL_NAME: {the tool name} TOOL_ARGS: {JSON formatted arguments for the tool} ... The user message is: cual es el tiempo en nyc ? ====================================== -
Gemini 回复并请求使用工具:
2025-04-21 12:31:56,049 - __main__ - INFO - === Raw Response Received from Gemini (Initial) === TOOL_NAME: get_forecast TOOL_ARGS: {"latitude": 40.7128, "longitude": -74.0060} =============================================== -
客户端解析回复,并向 MCP 服务发送工具请求:
2025-04-21 12:31:56,050 - __main__ - INFO - Executing MCP tool: get_forecast with args: {'latitude': 40.7128, 'longitude': -74.006} -
服务接收 MCP 工具请求:
2025-04-21 12:31:56,052 - INFO - mcp.server.lowlevel.server - Processing request of type CallToolRequest 2025-04-21 12:31:56,052 - INFO - __main__ - MCP Request Received: get_forecast with latitude: 40.7128, longitude: -74.006 -
服务执行工具,并向客户端发送 MCP 回复:
2025-04-21 12:31:57,180 - INFO - __main__ - === MCP Response Sent (get_forecast) === Today: Temperature: 56F ... Forecast: Cloudy. High near 56... ---... ======================================= -
客户端接收 MCP 工具回复:
2025-04-21 12:31:57,182 - __main__ - INFO - Raw MCP Tool Result Object Received: meta=None content=[TextContent(type='text', text='\n Today:\n...)] isError=False 2025-04-21 12:31:57,182 - __main__ - INFO - MCP tool get_forecast executed. Extracted Result Content: [TextContent(type='text', text='\n Today:\n...)]... -
客户端向 Gemini 发送最终提示词(包含工具结果):
2025-04-21 12:31:57,182 - __main__ - INFO - === Prompt Sent to Gemini (Final) === I asked for information about the weather in response to: "cual es el tiempo en nyc ?" I used the get_forecast tool with these arguments: {"latitude": 40.7128, "longitude": -74.006} The tool returned this result: [TextContent(type='text', text='\n Today:\n...')] Please provide a helpful, user-friendly response based on this information. ======================================= -
Gemini 回复并生成面向用户的格式化最终答复:
2025-04-21 12:31:59,000 - __main__ - INFO - === Raw Response Received from Gemini (Final) === The weather in NYC looks like this: **Today:** Cloudy with a high of 56F... ... ===============================================
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。