个人随笔
目录
SpringAI2.0MCP的简单使用
2026-11-04 23:09:34

模型上下文协议(MCP)是一种标准化协议,使 AI 模型能够以结构化的方式与外部工具和资源进行交互。您可以将其视为连接您的 AI 模型与现实世界的桥梁——通过一致的接口,让它们访问数据库、API、文件系统及其他外部服务。它支持多种传输机制,可在不同环境中提供灵活性。

SpringAI2.0提供了MCP的服务端实现和客户端实现,这里要用两个项目

一、服务端

1、依赖pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>4.1.2</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.suibibk</groupId>
  12. <artifactId>spring-ai-test</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>spring-ai-test</name>
  15. <description>spring-ai-test</description>
  16. <properties>
  17. <java.version>17</java.version>
  18. <spring-ai.version>2.0.1</spring-ai.version>
  19. </properties>
  20. <dependencies>
  21. <!-- MCP Server 支持:`STREAMABLE` / `SSE` / `STATELESS`(http 网络模式,复用 8080 端口,访问 `/mcp`)-->
  22. <dependency>
  23. <groupId>org.springframework.ai</groupId>
  24. <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
  25. </dependency>
  26. </dependencies>
  27. <dependencyManagement>
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.ai</groupId>
  31. <artifactId>spring-ai-bom</artifactId>
  32. <version>${spring-ai.version}</version>
  33. <type>pom</type>
  34. <scope>import</scope>
  35. </dependency>
  36. </dependencies>
  37. </dependencyManagement>
  38. <build>
  39. <plugins>
  40. <plugin>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-maven-plugin</artifactId>
  43. </plugin>
  44. </plugins>
  45. </build>
  46. </project>

2、配置application.yaml

  1. spring:
  2. application:
  3. name: spring-ai-mcp-test
  4. ai:
  5. mcp:
  6. server:
  7. protocol: STREAMABLE
  8. logging:
  9. level:
  10. org.springframework.ai: debug
  11. server:
  12. port: 8081

3、mcp工具类

  1. @Component
  2. public class CalculatorTools {
  3. private final Map<String, String> configData = Map.of(
  4. "name", "demo",
  5. "version", "1.0.0"
  6. );
  7. @McpTool(name = "add", description = "Add two numbers together")
  8. public int add(
  9. @McpToolParam(description = "First number", required = true) int a,
  10. @McpToolParam(description = "Second number", required = true) int b) {
  11. System.out.println("有调用:"+a+";"+b);
  12. return a + b;
  13. }
  14. @McpResource(uri = "config://{key}", name = "Configuration", description = "Read config value by key")
  15. public String getConfig(String key) {
  16. return configData.getOrDefault(key, "not found");
  17. }
  18. }

4、启动类

  1. @SpringBootApplication
  2. public class SpringAiMCPTestApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(SpringAiMCPTestApplication.class, args);
  5. }
  6. }

然后启动就好了。

二、客户端

客户端就基于之前的例子,很简单

1、依赖

  1. <!-- MCP Client 依赖,服务端包不要引进来 -->
  2. <dependency>
  3. <groupId>org.springframework.ai</groupId>
  4. <artifactId>spring-ai-starter-mcp-client</artifactId>
  5. </dependency>

2、配置

  1. spring:
  2. application:
  3. name: spring-ai-test
  4. ai:
  5. openai:
  6. api-key: ${AI_BAILIAN_API_KEY}
  7. base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
  8. chat:
  9. model: qwen3.8-flash
  10. temperature: 0.7
  11. embedding:
  12. model: qwen3.7-text-embedding-flash
  13. dimensions: 2048
  14. mcp:
  15. client:
  16. streamable-http:
  17. connections:
  18. self-server:
  19. url: http://localhost:8081
  20. logging:
  21. level:
  22. org.springframework.ai: debug

3、调用

  1. @Autowired
  2. private ToolCallbackProvider mcpTools;
  3. @GetMapping("/mcp")
  4. public String mcp(String topic){
  5. String result = openaiChatClient
  6. .prompt()
  7. .user(u -> u.text("{topic}").param("topic",topic))
  8. .tools(mcpTools)
  9. .call()
  10. .content();
  11. return result;
  12. }

4、测试

访问http://localhost:8080/mcp?topic=1加上1等于多少
结果返回1 加上 1 等于 2。看MCPserver的日志也打印了

  1. 有调用:1;1
 1

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2