sse mcp客户端
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -62,8 +62,13 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- Spring AI MCP 客户端 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.ai</groupId>
|
||||||
|
<artifactId>spring-ai-starter-mcp-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Spring AI openai-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.ai</groupId>
|
<groupId>org.springframework.ai</groupId>
|
||||||
<artifactId>spring-ai-starter-model-openai</artifactId>
|
<artifactId>spring-ai-starter-model-openai</artifactId>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.peanut.modules.springai;
|
|||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.ai.chat.client.ChatClient;
|
import org.springframework.ai.chat.client.ChatClient;
|
||||||
|
import org.springframework.ai.tool.ToolCallbackProvider;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -15,11 +16,11 @@ public class ChatController {
|
|||||||
|
|
||||||
private final ChatClient chatClient;
|
private final ChatClient chatClient;
|
||||||
|
|
||||||
public ChatController(ChatClient.Builder chatClientBuilder) {
|
public ChatController(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools) {
|
||||||
this.chatClient = chatClientBuilder.build();
|
this.chatClient = chatClientBuilder.defaultToolCallbacks(tools).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/chat")
|
@RequestMapping(value = "/chat")
|
||||||
public Flux<String> chat(@RequestBody Map<String,Object> parmas) {
|
public Flux<String> chat(@RequestBody Map<String,Object> parmas) {
|
||||||
return chatClient.prompt()
|
return chatClient.prompt()
|
||||||
.user(parmas.get("message").toString())
|
.user(parmas.get("message").toString())
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package com.peanut.modules.springai;
|
||||||
|
|
||||||
|
import io.modelcontextprotocol.client.McpSyncClient;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class McpHeartbeatService {
|
||||||
|
|
||||||
|
private final List<McpSyncClient> mcpClients;
|
||||||
|
|
||||||
|
public McpHeartbeatService(List<McpSyncClient> mcpClients) {
|
||||||
|
this.mcpClients = mcpClients;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(fixedRate = 120000) // 每2分钟1次发送心跳
|
||||||
|
public void sendHeartbeat() {
|
||||||
|
try {
|
||||||
|
this.mcpClients.parallelStream().forEach(client -> {client.ping();});
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -40,6 +40,26 @@ spring:
|
|||||||
main:
|
main:
|
||||||
allow-circular-references: true
|
allow-circular-references: true
|
||||||
ai:
|
ai:
|
||||||
|
mcp:
|
||||||
|
client:
|
||||||
|
toolcallback:
|
||||||
|
enabled: true
|
||||||
|
name: spring-ai-mcp-client
|
||||||
|
type: async
|
||||||
|
sse:
|
||||||
|
connections:
|
||||||
|
server1:
|
||||||
|
url: http://localhost:9090
|
||||||
|
# stdio:
|
||||||
|
# connections:
|
||||||
|
# server2:
|
||||||
|
# command: java
|
||||||
|
# args:
|
||||||
|
# -Dspring.ai.mcp.server.stdio=true
|
||||||
|
# -Dspring.main.web-application-type=none
|
||||||
|
# -Dspring.main.banner-mode=off
|
||||||
|
# -jar
|
||||||
|
# F:\ideaProject\stdioServer\target\stdioServer-0.0.1-SNAPSHOT.jar
|
||||||
openai:
|
openai:
|
||||||
api-key: sk-9b9babcc85414e999faac58411eb40d0 # 设置 DeepSeek API 的访问密钥,永远不要将密钥提交到代码仓库,建议通过环境变量注入。-收费
|
api-key: sk-9b9babcc85414e999faac58411eb40d0 # 设置 DeepSeek API 的访问密钥,永远不要将密钥提交到代码仓库,建议通过环境变量注入。-收费
|
||||||
base-url: https://api.deepseek.com # 指定 DeepSeek API 的基础地址,格式与OpenAI相同。
|
base-url: https://api.deepseek.com # 指定 DeepSeek API 的基础地址,格式与OpenAI相同。
|
||||||
|
|||||||
Reference in New Issue
Block a user