Skip to main content

MCP-Client-For-Weather-Example

MCP-Client-For-Weather-Example 是一个基于 MCP(Modular Communication Protocol)的客户端和服务器项目,支持通过 LLM 的大语言模型与工具交互,并提供天气查询功能。

功能特性

  • MCP 客户端:支持与 MCP 服务器通信,调用工具并处理查询。
  • 天气查询服务:通过 OpenWeather API 查询指定城市的天气信息。
  • 环境变量支持:通过 .env 文件配置 API 密钥和其他参数。
  • 异步支持:使用 asyncio 和 httpx 实现高效的异步通信。

文件结构

├── .env # 环境变量配置文件
├── .gitignore # Git 忽略文件
├── client.py # MCP 客户端实现
├── mcp_weather_server.py # 天气查询服务实现
├── pyproject.toml # 项目依赖配置文件
└── README.md # 项目说明文件

环境配置

1. 安装依赖

确保已安装 Python 3 或更高版本。然后运行以下命令安装依赖:

# 使用 uv
pip install uv
uv add mcp httpx openai
# 创建虚拟环境
uv venv
# 激活虚拟环境
source .venv/bin/activate

2. 配置环境变量

在项目根目录下创建 .env 文件,并添加以下内容:

这里使用的硅基智能的 API,天气API去 https://openweathermap.org 注册

OPENAI_API_KEY=你的OpenAI API密钥
BASE_URL=https://api.siliconflow.cn/v1 # <你的OpenAI API基础URL>
MODEL=使用的模型名称
WEATHER_API_KEY=你的OpenWeather API密钥

使用方法

  1. 运行以下命令启动 MCP 客户端并连接到服务器:
python client.py mcp_weather_server.py
  1. 启动后,输入城市名称(英文)即可查询天气信息。例如:

回答效果取决于模型本身,你的输出可能有些出入。

你: 今天北京天气怎样?
🤖 LLM: 北京的天气情况如下:
- 温度:10.94°C
- 湿度:15%
- 风速:4 m/s
- 天气:晴,少云
今天的北京天气晴朗,温度适宜,适合外出活动。请注意保暖,因为早晨和晚上的气温可能会有所下降
  1. 退出客户端 输入 quit 退出 MCP 客户端。

项目依赖

Python 3+
httpx - 异步 HTTP 客户端
mcp - Modular Communication Protocol
openai - OpenAI API 客户端
python-dotenv - 环境变量加载工具

开发指南

代码格式化

使用 pycharm 快捷键,进行格式化

单元测试

目前项目未包含单元测试模块,建议后续添加测试以提高代码质量。

许可证

本项目未指定许可证,默认保留所有权利。如需使用,请联系项目作者。

贡献

欢迎提交 Issue 和 Pull Request 来改进本项目。

联系方式

如有问题,请通过 GitHub Issue 提交反馈。

感谢

学习资源

MCP Servers

Metadata

Release files for iflow-mcp_a-persimmons-mcp-ds-client 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iflow-mcp_a-persimmons-mcp-ds-client 0.1.0
File Size Uploaded
iflow_mcp_a_persimmons_mcp_ds_client-0.1.0.tar.gz 5.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iflow-mcp_a-persimmons-mcp-ds-client 0.1.0
File Interpreter ABI Platform
iflow_mcp_a_persimmons_mcp_ds_client-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / iflow_mcp_a_persimmons_mcp_ds_client-0.1.0.tar.gz

Download URL iflow_mcp_a_persimmons_mcp_ds_client-0.1.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
086f61d74a9aa4b52c05e043848bc921bc922ee43fd767fde154dfd285ff18ca
BLAKE2b-256 checksum
How to use checksums
fe72f161b21188a52c54df9a9170c844c1f1ba12085649f3b038e727fba9dd84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / iflow_mcp_a_persimmons_mcp_ds_client-0.1.0-py3-none-any.whl

Download URL iflow_mcp_a_persimmons_mcp_ds_client-0.1.0-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bdf5db2d4b0a33387472c533f8d7944e77fba667ad1c6db9792416f7c07d4ed4
BLAKE2b-256 checksum
How to use checksums
dc5e4a845b3d1cac890452b5d2475c0b53005f77fb1ad01eb52855416fcd2b81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page