Mcp Webmvc Server

Mcp Webmvc Server

🚀 Spring AI MCP 天气服务服务器示例

本项目是一个基于 Spring AI 框架构建的示例天气服务服务器,借助 Model Context Protocol (MCP) 协议,打造了一个基于 Spring Boot 的天气信息服务,具备基本的天气信息查询能力。

✨ 主要特性

  • 提供基本的天气信息查询能力。
  • 运用 Spring Boot、Spring AI 以及 MCP 协议搭建服务。
  • 演示 MCP 服务器端的实现,涵盖服务注册、协议处理和客户端交互。

📦 安装指南

项目依赖

以下是项目所需的 Maven 依赖:

<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-log4j2artifactId>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.datgengroupId>
<artifactId>jackson-dataformat-csvartifactId>
dependency>
<dependency>
<groupId>org.modelmappergroupId>
<artifactId>modelmapperartifactId>
dependency>
dependencies>

项目构建

执行以下命令下载所有依赖项并构建项目 JAR 文件:

mvn clean install

💻 使用示例

启动服务器

java -jar target/spring-ai-mcp-weather-server-0.0.1-SNAPSHOT.jar

启动后,服务器将在默认端口 8080 上运行,并提供 RESTful 接口。

访问接口

  • 基本信息接口:
    http://localhost:8080/api/health
    
  • 天气查询接口:
    http://localhost:8080/weather?city=New%20York
    

📚 详细文档

项目结构

spring-ai-mcp-weather-server/
├── src/main/java/
│   └── org.springframework.ai.mcp.sample.server/
│       ├── WeatherService.java          # 天气查询业务逻辑实现类
│       └── WeatherController.java       # 天气查询 RESTful 接口控制器
├── pom.xml                              # 项目依赖管理文件
└── README.md                            # 项目说明文档

配置文件

项目提供了一个默认的 application.properties 文件,包含以下配置:

server.port=8080
logging.level.root=INFO

如果需要自定义配置,可以创建一个 application.yamlapplication.properties 文件,并将其放入项目的 src/main/resources 目录。

🔧 技术细节

核心实现

1. WeatherService.java

package org.springframework.ai.mcp.sample.server;

import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class WeatherService {
public Map getWeather(String city) {
Map result = new HashMap<>();
try {
// 示例逻辑:返回假设计数据
if ("New York".equals(city)) {
result.put("temperature", 75);
result.put("condition", "晴朗");
result.put("humidity", 60);
result.put("windSpeed", 15);
} else {
result.put("error", "城市未找到");
}
} catch (Exception e) {
result.put("error", "服务异常");
}
return result;
}
}

2. WeatherController.java

package org.springframework.ai.mcp.sample.server;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WeatherController {
private final WeatherService weatherService;

public WeatherController(WeatherService weatherService) {
this.weatherService = weatherService;
}

@GetMapping("/weather")
public Map getWeather(@RequestParam String city) {
return weatherService.getWeather(city);
}
}

示例输出

当访问 /weather?city=New%20York 时,示例输出如下:

{
"temperature": 75,
"condition": "晴朗",
"humidity": 60,
"windSpeed": 15
}

扩展说明

服务扩展

  • 可以通过实现不同的 WeatherService 类来支持更多城市或天气数据源。
  • 支持多语言响应(如英文、中文等)。

协议扩展

  • MCP 协议支持多种数据格式,包括 JSON 和 CSV。
  • 可以根据需求自定义响应内容和结构。

项目目标

  • 演示 Spring AI 框架在 MCP 协议中的使用。
  • 提供一个可扩展的天气信息服务实现。
  • 展现微服务架构下的协议处理能力。
  • 0 关注
  • 0 收藏,8 浏览
  • system 提出于 2025-09-20 18:39

相似服务问题