MCP HubMCP Hub
yestarz

mcp-server-weather

by: yestarz

mcp server weather

19created 28/02/2025
Visit
weather

📌Overview

Purpose: The framework aims to provide a real-time weather retrieval service for MCP servers, utilizing the He Feng Weather API and implemented in Java.

Overview: MCP Server Weather is a Java-based server application that integrates with the He Feng Weather API to fetch current weather data. It facilitates easy access to weather information, helping developers build applications that require real-time weather updates.

Key Features:

  • Real-Time Weather Retrieval: The getWeather tool enables users to obtain up-to-date weather information for specific locations, enhancing application responsiveness and user engagement.

  • Easy Integration: Simplified setup process and straightforward dependency management allow for quick implementation within Java applications, ensuring efficient development workflows.


MCP Server Weather

基于和风天气API的一个获取实时天气的MCP服务端,使用Java实现。
和风天气API文档:https://dev.qweather.com/docs/api/weather/weather-now/

可用工具列表

  • 获取实时天气 getWeather

使用方法

  1. 下载项目到本地
  2. 打包项目,生成jar包:
mvn clean package -Dmaven.test.skip=true

Claude使用方法

{
  "mcpServers": {
    "mcp-server-weather": {
      "command": "java",
      "args": [
        "-Dspring.ai.mcp.server.stdio=true",
        "-jar",
        "你的jar包路径",
        "--weather.api.api-key=YOUR API KEY"
      ]
    }
  }
}

LangChain4J使用方法

引入依赖:

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j</artifactId>
</dependency>
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-open-ai</artifactId>
</dependency>
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-mcp</artifactId>
</dependency>

示例代码:

@Bean
public ChatLanguageModel chatLanguageModel() {
    return OpenAiChatModel.builder()
            .apiKey(System.getenv("AI_DASHSCOPE_API_KEY"))
            .modelName("qwen-turbo")
            .logRequests(true)
            .logResponses(true)
            .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
            .build();
}

@Bean
public McpClient mcpClientWeather() {
    return new DefaultMcpClient.Builder()
            .transport(new StdioMcpTransport.Builder()
                    .command(List.of(
                            "java",
                            "-Dspring.ai.mcp.server.stdio=true",
                            "-jar",
                            "mcp-server-weather-0.0.1-SNAPSHOT.jar",
                            "--weather.api.api-key=%s".formatted(System.getenv("HEFENG_WEATHER_API_KEY"))))
                    .logEvents(true)
                    .build())
            .build();
}

@Bean
public AiAssistant aiAssistant(McpClient mcpClientWeather) {
    ToolProvider toolProvider = McpToolProvider.builder()
            .mcpClients(List.of(mcpClientWeather))
            .build();
    return AiServices.builder(AiAssistant.class)
            .chatLanguageModel(chatLanguageModel())
            .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
            .toolProvider(toolProvider)
            .build();
}

@Test
public void testWeather1(){
    System.out.println(aiAssistant.chat("今天重庆的天气怎么样?"));
    /**
     * AI回复示例:
     * 今天重庆的天气情况如下:
     * - 天气状况:阴
     * - 气温:18℃
     * - 体感温度:16℃
     * - 风向:东风
     * - 风力:2级
     * - 湿度:47%
     * - 降水量:0.0mm
     * - 空气压力:980hPa
     * - 能见度:9km
     * - 云量:91% 
     */
}