mcpField
P
Proyecto Cliente MCP con Gemini y Servidor de Herramientas Meteorológicas

Proyecto Cliente MCP con Gemini y Servidor de Herramientas Meteorológicas

本地服务
github.com/KarlheinzniebuhrAI 与智能体077 周前更新

关于 Proyecto Cliente MCP con Gemini y Servidor de Herramientas Meteorológicas

这是一个 Python 示例项目,将 Gemini 客户端与天气工具服务端配合使用,展示 AI 如何调用天气工具并将结果整理为面向用户的回答。

主要功能

  1. 01Gemini 客户端与天气服务端
  2. 02天气预警和预报工具
  3. 03工具发现与调用
  4. 04客户端和服务端合并日志

适用场景

  1. 01回答本地天气问题
  2. 02检查 MCP 与 Gemini 的请求流程

MCP 相关

客户端连接天气 MCP 服务,发现并调用 get_alerts 和 get_forecast 等工具。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

简而言之 / 概述

  1. 安装依赖项:pip install -r requirements.txt
  2. 创建 .env,并添加 GEMINI_API_KEY=TU_CLAVE_API_AQUI。
  3. 运行:python mcp_gemini_client.py weather_tool_server.py

使用 Gemini 的 MCP 客户端与气象工具服务

此代码库演示了如何使用 MCP(Model-Client-Protocol)协议,将 AI 客户端(使用 Google Gemini)连接到提供专用工具的服务(此处为气象工具)。

工作原理

该系统充当 Gemini AI 与专用工具之间的桥梁。下面以简单的方式说明其流程:

组件角色
  1. mcp_gemini_client.py - 这是一个“中间层”,用于:

    • 与 Gemini AI 通信。
    • 连接到工具服务(weather_tool_server.py)。
    • 处理用户输入并显示回复。
  2. weather_tool_server.py - 这是一个“工具提供方”,用于:

    • 提供与天气相关的专用工具(get_alerts、get_forecast)。
    • 处理工具请求并返回结果。
  3. MCP(Model-Client-Protocol) - 这是一个“通信系统”,用于:

    • 为客户端发现和调用工具提供标准化方式。
    • 处理客户端与服务之间的消息传递。
天气查询流程
  1. 用户输入:在客户端中输入类似“纽约的天气如何?”的问题。
  2. 客户端处理:
    • mcp_gemini_client.py 将此问题发送给 Gemini AI。
    • 它还会通过查询 weather_tool_server.py,告知 Gemini 有哪些可用工具。
  3. Gemini AI 的决策:
    • Gemini 会识别出需要使用气象工具(例如 get_forecast)。
    • 它会格式化回复,指出要使用哪个工具以及要传入哪些参数(例如纽约的纬度和经度)。
  4. 工具执行:
    • 客户端解析 Gemini 发出的工具调用请求。
    • 将工具请求发送到 weather_tool_server.py。
    • 服务执行天气查询并返回结果。
  5. 最终回复:
    • 工具结果返回给客户端,客户端随后将其发送回 Gemini。
    • Gemini 将这些结果整理成用户易于理解的回复。
    • 客户端显示最终回复。

配置与运行

  1. 安装依赖项:
    pip install google-generativeai python-dotenv mcp-protocols # 确保 mcp-protocols 是正确的软件包
    
  2. API 密钥:
    • 从 Google AI Studio 获取 API 密钥。
    • 在项目根目录中创建 .env 文件。
    • 将密钥添加到 .env 文件中:
      GEMINI_API_KEY=TU_CLAVE_API_AQUI
      
  3. 运行:
    • 确保 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)

以下是完整交互日志的简化示例,展示了关键通信内容:

  1. 客户端向 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 ?
    ======================================
    
  2. 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}
    ===============================================
    
  3. 客户端解析回复,并向 MCP 服务发送工具请求:

    2025-04-21 12:31:56,050 - __main__ - INFO - Executing MCP tool: get_forecast with args: {'latitude': 40.7128, 'longitude': -74.006}
    
  4. 服务接收 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
    
  5. 服务执行工具,并向客户端发送 MCP 回复:

    2025-04-21 12:31:57,180 - INFO - __main__ - === MCP Response Sent (get_forecast) ===
    
                        Today:
                        Temperature: 56F
                        ...
                        Forecast: Cloudy. High near 56...
                        
    ---...
    =======================================
    
  6. 客户端接收 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...)]...
    
  7. 客户端向 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.
    =======================================
    
  8. 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...
    ...
    ===============================================
    

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。