官方参考文档:模型上下文协议 (MCP) :: Spring AI 参考 - Spring 框架
下面实现一个简单实例:
MCP Server 服务
**主要步骤:**将 Spring Boot 项目改造成一个 MCP Server,通过引入 Spring AI MCP Server 相关依赖,将业务能力以 MCP Tool / Resource 的形式标准化地暴露给大模型,并通过 SSE(Server-Sent Events)与模型侧建立长连接通信。
引入依赖
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
注解开发
在 com.example.mcp.tool 包下,定义了一个自定义计算类,使用 @McpTool 注解将方法声明成可通过 MCP 协议暴露给大模型调用的工具(Tool):
@Component
{
{
* (a + b) / ;
}
{
a * a - a + ;
}
}

