基于老友的实现,简化了实现 MCP

Java来实现一个获取实时天气信息的MCP服务端 - 开发调优 - LINUX DO

spring:
  application:
    name: mcp-server-weather
  ai:
    openai:
      api-key:
      base-url: 


       <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
            <version>1.0.0-M6</version>
        </dependency>
        <!--模块依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.36</version>
        </dependency>
@RestController
public class TestController {


    @Resource
    private OpenAiChatModel openAiChatModel;

    @Resource
    private WeatherService weatherService;



    @GetMapping("/test")
    public WeatherFunctionResponse test(@RequestParam("prompt") String prompt) {
        return  ChatClient.create(openAiChatModel)
                .prompt(prompt)
                .tools(weatherService)
                .call()
                .entity(WeatherFunctionResponse.class);
    }
}

GET http://xxxxx/test?
prompt=纽约今天的天气如何?


{
  "obsTime": "2025-03-11T02:53-04:00",
  "temp": "12",
  "feelsLike": "9",
  "icon": "150",
  "text": "晴",
  "wind360": "225",
  "windDir": "西南风",
  "windScale": "2",
  "windSpeed": "11",
  "humidity": "34",
  "precip": "0.0",
  "pressure": "1010",
  "vis": "16",
  "cloud": "10",
  "dew": "-3"
}
8 个赞

占前排,这个怎么调用呢?

直接接口调用呀

太强了!

帅锅才强呀 :tieba_022:

太强了佬

此话题已在最后回复的 30 天后被自动关闭。不再允许新回复。