使用说明
天气预报工具
描述
这个Python代码提供了一个工具,可以使用 MET挪威API。它利用 FastMCP 工具注册和使用框架 httpx 用于异步HTTP请求。
特性
-
天气预报: 这
get_weather函数检索指定纬度和经度的天气预报数据。 -
每日总结: 这
summarize_daily_timeseries_most_occurrences函数处理原始预测数据,以提供简洁的每日摘要,包括:- 最常见的天气符号是晚上、早上、下午和晚上。
- 当天的最低和最高温度。
- 预报将在当地时间返回。
-
HTTP请求: 这
make_net_request函数处理对MET挪威API的异步HTTP请求。 -
天气符号映射: 这
map_weather_symbol函数将API中的天气符号代码转换为可供人阅读的描述。
依赖项
- Python 3.13+
collectionsdatetimetypingasynciojsonhttpxmcp.server.fastmcp
设置
-
安装依赖关系:
根据需要安装MCP和其他依赖项(请参阅 MCP文件)
-
获取坐标: 你需要你想要天气预报的位置的纬度和经度。
用法
-
导入: 导入必要的模块并初始化FastMCP服务器。
from collections import Counter from datetime import datetime, timezone, timedelta from mcp.server.fastmcp import FastMCP from typing import Any import asyncio import json import httpx mcp = FastMCP("weather") -
使用
get_weather工具:async def main(): weather_data = await get_weather(55.6761, 12.5683) # Example: Copenhagen, Denmark print(weather_data) if __name__ == "__main__": asyncio.run(main())替换
55.6761和12.5683具有所需的纬度和经度。
工具定义
* get_weather(latitude: float, longitude: float) -> str
*检索并格式化给定位置的天气预报。
*论点:
* latitude (float):位置的纬度。
* longitude (float):位置的经度。
*退货:
* str:包含每日天气预报的格式化字符串。
主控程序
该代码使用FastMCP框架。有关如何设置和运行服务器的详细信息,请参阅FastMCP文档。