feat: mcp支持远程调用

This commit is contained in:
ageerle
2025-04-17 16:23:32 +08:00
parent 761d954ef1
commit 788b372e32
4 changed files with 76 additions and 9 deletions

View File

@@ -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

View File

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

View File

@@ -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>

View File

@@ -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();