mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-17 13:53:41 +00:00
feat: mcp支持远程调用
This commit is contained in:
@@ -319,5 +319,15 @@ wechat:
|
|||||||
secret: ''
|
secret: ''
|
||||||
token: ''
|
token: ''
|
||||||
aesKey: ''
|
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
ruoyi-admin/src/main/resources/mcp-server.json
Normal file
12
ruoyi-admin/src/main/resources/mcp-server.json
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"mcpServers": {
|
||||||
|
"fileSystem": {
|
||||||
|
"command": "C:\\Program Files\\nodejs\\npx.cmd",
|
||||||
|
"args": [
|
||||||
|
"-y",
|
||||||
|
"@modelcontextprotocol/server-filesystem",
|
||||||
|
"D:\\software"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,6 +32,14 @@
|
|||||||
|
|
||||||
<!-- 对话基础模块 -->
|
<!-- 对话基础模块 -->
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.modelcontextprotocol.sdk</groupId>
|
||||||
|
<artifactId>mcp-spring-webflux</artifactId>
|
||||||
|
<version>0.8.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.ruoyi</groupId>
|
<groupId>org.ruoyi</groupId>
|
||||||
<artifactId>ruoyi-common-chat</artifactId>
|
<artifactId>ruoyi-common-chat</artifactId>
|
||||||
@@ -55,16 +63,27 @@
|
|||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.ai</groupId>
|
<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>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.modelcontextprotocol.sdk</groupId>
|
<groupId>org.springframework.ai</groupId>
|
||||||
<artifactId>mcp-spring-webflux</artifactId>
|
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
|
||||||
<version>0.8.0</version>
|
|
||||||
<scope>compile</scope>
|
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.ruoyi.chat.service.chat.impl;
|
|||||||
|
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import io.modelcontextprotocol.client.McpClient;
|
import io.modelcontextprotocol.client.McpClient;
|
||||||
|
import io.modelcontextprotocol.client.McpSyncClient;
|
||||||
import io.modelcontextprotocol.client.transport.WebFluxSseClientTransport;
|
import io.modelcontextprotocol.client.transport.WebFluxSseClientTransport;
|
||||||
import io.modelcontextprotocol.spec.McpSchema;
|
import io.modelcontextprotocol.spec.McpSchema;
|
||||||
import lombok.RequiredArgsConstructor;
|
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.openai.OpenAiStreamClient;
|
||||||
import org.ruoyi.common.chat.request.ChatRequest;
|
import org.ruoyi.common.chat.request.ChatRequest;
|
||||||
import org.ruoyi.common.core.exception.ServiceException;
|
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.stereotype.Service;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
||||||
@@ -27,14 +34,33 @@ import java.util.Map;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class OpenAIServiceImpl implements IChatService {
|
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
|
@Override
|
||||||
public SseEmitter chat(ChatRequest chatRequest,SseEmitter emitter) {
|
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();
|
Message userMessage = Message.builder().content("工具返回信息:"+toolString).role(Message.Role.ASSISTANT).build();
|
||||||
List<Message> messages = chatRequest.getMessages();
|
List<Message> messages = chatRequest.getMessages();
|
||||||
|
|||||||
Reference in New Issue
Block a user