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"
}