Browse Source

feat: mcp支持远程调用

ageerle 1 tháng trước cách đây
mục cha
commit
788b372e32

+ 11 - 1
ruoyi-admin/src/main/resources/application.yml

@@ -319,5 +319,15 @@ wechat:
         secret: ''
         token:   ''
         aesKey: ''
-
+spring:
+  ai:
+    openai:
+      api-key: sk-xXe1WMPjhlVb1aiI1b4c6c8934D8463f9e4b67Ed8718B772
+      base-url: https://api.pandarobot.chat/
+    mcp:
+      client:
+        enabled: true
+        name: call-mcp-server
+        stdio:
+          servers-configuration: classpath:mcp-server.json
 

+ 12 - 0
ruoyi-admin/src/main/resources/mcp-server.json

@@ -0,0 +1,12 @@
+{
+  "mcpServers": {
+    "fileSystem": {
+      "command": "C:\\Program Files\\nodejs\\npx.cmd",
+      "args": [
+        "-y",
+        "@modelcontextprotocol/server-filesystem",
+        "D:\\software"
+      ]
+    }
+  }
+}

+ 24 - 5
ruoyi-modules-api/ruoyi-chat-api/pom.xml

@@ -32,6 +32,14 @@
 
     <!-- 对话基础模块 -->
     <dependencies>
+
+        <dependency>
+            <groupId>io.modelcontextprotocol.sdk</groupId>
+            <artifactId>mcp-spring-webflux</artifactId>
+            <version>0.8.0</version>
+        </dependency>
+
+
         <dependency>
             <groupId>org.ruoyi</groupId>
             <artifactId>ruoyi-common-chat</artifactId>
@@ -55,16 +63,27 @@
 
         <dependency>
             <groupId>org.springframework.ai</groupId>
-            <artifactId>spring-ai-mcp-client-webflux-spring-boot-starter</artifactId>
+            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
         </dependency>
 
         <dependency>
-            <groupId>io.modelcontextprotocol.sdk</groupId>
-            <artifactId>mcp-spring-webflux</artifactId>
-            <version>0.8.0</version>
-            <scope>compile</scope>
+            <groupId>org.springframework.ai</groupId>
+            <artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.ai</groupId>
+            <artifactId>spring-ai-mcp</artifactId>
+        </dependency>
+
+
+        <dependency>
+            <groupId>org.springframework.ai</groupId>
+            <artifactId>spring-ai-mcp-client-webflux-spring-boot-starter</artifactId>
+        </dependency>
+
+
+
 
     </dependencies>
 

+ 29 - 3
ruoyi-modules/ruoyi-chat/src/main/java/org/ruoyi/chat/service/chat/impl/OpenAIServiceImpl.java

@@ -2,6 +2,7 @@ package org.ruoyi.chat.service.chat.impl;
 
 import cn.hutool.json.JSONUtil;
 import io.modelcontextprotocol.client.McpClient;
+import io.modelcontextprotocol.client.McpSyncClient;
 import io.modelcontextprotocol.client.transport.WebFluxSseClientTransport;
 import io.modelcontextprotocol.spec.McpSchema;
 import lombok.RequiredArgsConstructor;
@@ -15,6 +16,12 @@ import org.ruoyi.common.chat.entity.chat.tool.ToolsFunction;
 import org.ruoyi.common.chat.openai.OpenAiStreamClient;
 import org.ruoyi.common.chat.request.ChatRequest;
 import org.ruoyi.common.core.exception.ServiceException;
+import org.springframework.ai.chat.client.ChatClient;
+import org.springframework.ai.chat.memory.ChatMemory;
+import org.springframework.ai.chat.memory.InMemoryChatMemory;
+import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
+import org.springframework.ai.tool.ToolCallbackProvider;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.web.reactive.function.client.WebClient;
 import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@@ -27,14 +34,33 @@ import java.util.Map;
 
 @Service
 @Slf4j
-@RequiredArgsConstructor
 public class OpenAIServiceImpl implements IChatService {
 
-    private final OpenAiStreamClient openAiStreamClient;
+    @Autowired
+    private OpenAiStreamClient openAiStreamClient;
+
+    private final ChatClient chatClient;
+
+    private final ChatMemory chatMemory = new InMemoryChatMemory();
+
+
+
+    public OpenAIServiceImpl(ChatClient.Builder chatClientBuilder, List<McpSyncClient> mcpSyncClients, ToolCallbackProvider tools) {
+        this.chatClient = chatClientBuilder
+                .defaultTools(new SyncMcpToolCallbackProvider(mcpSyncClients))
+                .build();
+    }
+
+
+    public String webMcpChat(String prompt){
+        return this.chatClient.prompt(prompt).call().content();
+
+    }
+
 
     @Override
     public SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter) {
-        String toolString = mcpChat(chatRequest);
+        String toolString = webMcpChat(chatRequest.getPrompt());
 
         Message userMessage = Message.builder().content("工具返回信息:"+toolString).role(Message.Role.ASSISTANT).build();
         List<Message> messages = chatRequest.getMessages();