Browse Source

feat:完成MCP Client代码编写;
MCP功能调试成功;

yangyi 7 tháng trước cách đây
mục cha
commit
482e7aedfb

+ 12 - 0
pom.xml

@@ -29,6 +29,13 @@
                 <artifactId>spring-boot-starter-web</artifactId>
                 <version>${spring.boot.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.springframework.ai</groupId>
+                <artifactId>spring-ai-bom</artifactId>
+                <version>${spring.ai.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
             <dependency>
                 <groupId>org.springframework.ai</groupId>
                 <artifactId>spring-ai-starter-model-openai</artifactId>
@@ -50,6 +57,11 @@
                 <artifactId>spring-ai-starter-mcp-server-webflux</artifactId>
                 <version>${spring.ai.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.springframework.ai</groupId>
+                <artifactId>spring-ai-starter-mcp-client-webflux</artifactId>
+                <version>${spring.ai.version}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 </project>

+ 4 - 0
spring-ai-learn/pom.xml

@@ -38,6 +38,10 @@
             <groupId>org.springframework.ai</groupId>
             <artifactId>spring-ai-rag</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.ai</groupId>
+            <artifactId>spring-ai-starter-mcp-client-webflux</artifactId>
+        </dependency>
     </dependencies>
 
 </project>

+ 32 - 0
spring-ai-learn/src/main/java/space/anyi/springAiAlibabaLearn/controller/MCPController.java

@@ -0,0 +1,32 @@
+package space.anyi.springAiAlibabaLearn.controller;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.ai.chat.client.ChatClient;
+import org.springframework.ai.chat.model.ChatModel;
+import org.springframework.ai.tool.ToolCallbackProvider;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/mcp")
+public class MCPController {
+    private static final Logger log = LoggerFactory.getLogger(MCPController.class);
+    private final ChatClient chatClient;
+
+    public MCPController(ChatModel chatModel, ToolCallbackProvider toolCallbackProvider) {
+        //构建chatClient,配置使用MCP server提供的tool
+        this.chatClient = ChatClient.builder(chatModel)
+                .defaultToolCallbacks(toolCallbackProvider)
+                .build();
+    }
+    @GetMapping("/test")
+    public String MCPTest(@RequestParam("message")String message){
+        log.debug("message:{}",message);
+        return chatClient.prompt(message)
+                .call()
+                .content();
+    }
+}

+ 8 - 0
spring-ai-learn/src/main/resources/application.yaml

@@ -48,6 +48,14 @@ spring:
         table-name: vector_store
 #        文档批量处理的最大数量
         max-document-batch-size: 1000
+#        mcp相关的配置
+    mcp:
+      client:
+        sse:
+          connections:
+#            配置mcp的连接消息,名称可以自定义
+            yangyi-mcp-server:
+              url: http://localhost:10001
 logging:
   level:
     root: DEBUG