mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-24 08:03:53 +08:00
Merge pull request #1899 from xinnan-tech/manager-api-mcp
优化获取请求mcp工具方法
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package xiaozhi.common.utils;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
/**
|
||||||
|
* JSON-RPC2.0 格式规范对象
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class JsonRpcTwo {
|
||||||
|
private String jsonrpc = "2.0";
|
||||||
|
private String method;
|
||||||
|
private Object params;
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
public JsonRpcTwo(String method, Object params, Integer id) {
|
||||||
|
this.method = method;
|
||||||
|
this.params = params;
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package xiaozhi.modules.agent.Enums;
|
||||||
|
|
||||||
|
import xiaozhi.common.utils.JsonUtils;
|
||||||
|
import xiaozhi.common.utils.JsonRpcTwo;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小智MCP JSON-RPC 请求json
|
||||||
|
*/
|
||||||
|
public class XiaoZhiMcpJsonRpcJson {
|
||||||
|
//小智初始化mcp请求json
|
||||||
|
private static final String INITIALIZE_JSON;
|
||||||
|
//小智mcp初始化成功,返回通知请求json
|
||||||
|
private static final String NOTIFICATIONS_INITIALIZED_JSON;
|
||||||
|
//小智mcp获取mcp工具集合请求json
|
||||||
|
private static final String TOOLS_LIST_REQUEST;
|
||||||
|
// 延迟加载
|
||||||
|
static {
|
||||||
|
INITIALIZE_JSON = JsonUtils.toJsonString(new JsonRpcTwo("initialize",
|
||||||
|
Map.of(
|
||||||
|
"protocolVersion", "2024-11-05",
|
||||||
|
"capabilities", Map.of(
|
||||||
|
"roots", Map.of("listChanged", false),
|
||||||
|
"sampling", Map.of()),
|
||||||
|
"clientInfo", Map.of(
|
||||||
|
"name", "xz-mcp-broker",
|
||||||
|
"version", "0.0.1")),
|
||||||
|
1));
|
||||||
|
NOTIFICATIONS_INITIALIZED_JSON = "{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}";
|
||||||
|
TOOLS_LIST_REQUEST = JsonUtils.toJsonString(new JsonRpcTwo("tools/list", null, 2));
|
||||||
|
}
|
||||||
|
public static String getInitializeJson(){
|
||||||
|
return INITIALIZE_JSON;
|
||||||
|
}
|
||||||
|
public static String getNotificationsInitializedJson(){
|
||||||
|
return NOTIFICATIONS_INITIALIZED_JSON;
|
||||||
|
}
|
||||||
|
public static String getToolsListJson(){
|
||||||
|
return TOOLS_LIST_REQUEST;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package xiaozhi.modules.agent.dto;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MCP JSON-RPC 请求 DTO
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class McpJsonRpcRequest {
|
|
||||||
private String jsonrpc = "2.0";
|
|
||||||
private String method;
|
|
||||||
private Object params;
|
|
||||||
private Integer id;
|
|
||||||
|
|
||||||
public McpJsonRpcRequest() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public McpJsonRpcRequest(String method) {
|
|
||||||
this.method = method;
|
|
||||||
}
|
|
||||||
|
|
||||||
public McpJsonRpcRequest(String method, Object params, Integer id) {
|
|
||||||
this.method = method;
|
|
||||||
this.params = params;
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public McpJsonRpcRequest(String method, Object params) {
|
|
||||||
this.method = method;
|
|
||||||
this.params = params;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+4
-17
@@ -18,7 +18,7 @@ import xiaozhi.common.constant.Constant;
|
|||||||
import xiaozhi.common.utils.AESUtils;
|
import xiaozhi.common.utils.AESUtils;
|
||||||
import xiaozhi.common.utils.HashEncryptionUtil;
|
import xiaozhi.common.utils.HashEncryptionUtil;
|
||||||
import xiaozhi.common.utils.JsonUtils;
|
import xiaozhi.common.utils.JsonUtils;
|
||||||
import xiaozhi.modules.agent.dto.McpJsonRpcRequest;
|
import xiaozhi.modules.agent.Enums.XiaoZhiMcpJsonRpcJson;
|
||||||
import xiaozhi.modules.agent.service.AgentMcpAccessPointService;
|
import xiaozhi.modules.agent.service.AgentMcpAccessPointService;
|
||||||
import xiaozhi.modules.sys.service.SysParamsService;
|
import xiaozhi.modules.sys.service.SysParamsService;
|
||||||
import xiaozhi.modules.sys.utils.WebSocketClientManager;
|
import xiaozhi.modules.sys.utils.WebSocketClientManager;
|
||||||
@@ -71,17 +71,7 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
|||||||
|
|
||||||
// 步骤1: 发送初始化消息并等待响应
|
// 步骤1: 发送初始化消息并等待响应
|
||||||
log.info("发送MCP初始化消息,智能体ID: {}", id);
|
log.info("发送MCP初始化消息,智能体ID: {}", id);
|
||||||
McpJsonRpcRequest initializeRequest = new McpJsonRpcRequest("initialize",
|
client.sendText(XiaoZhiMcpJsonRpcJson.getInitializeJson());
|
||||||
Map.of(
|
|
||||||
"protocolVersion", "2024-11-05",
|
|
||||||
"capabilities", Map.of(
|
|
||||||
"roots", Map.of("listChanged", false),
|
|
||||||
"sampling", Map.of()),
|
|
||||||
"clientInfo", Map.of(
|
|
||||||
"name", "xz-mcp-broker",
|
|
||||||
"version", "0.0.1")),
|
|
||||||
1);
|
|
||||||
client.sendJson(initializeRequest);
|
|
||||||
|
|
||||||
// 等待初始化响应 (id=1) - 移除固定延迟,改为响应驱动
|
// 等待初始化响应 (id=1) - 移除固定延迟,改为响应驱动
|
||||||
List<String> initResponses = client.listenerWithoutClose(response -> {
|
List<String> initResponses = client.listenerWithoutClose(response -> {
|
||||||
@@ -125,13 +115,10 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
|||||||
|
|
||||||
// 步骤2: 发送初始化完成通知 - 只有在收到initialize响应后才发送
|
// 步骤2: 发送初始化完成通知 - 只有在收到initialize响应后才发送
|
||||||
log.info("发送MCP初始化完成通知,智能体ID: {}", id);
|
log.info("发送MCP初始化完成通知,智能体ID: {}", id);
|
||||||
String notificationJson = "{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}";
|
client.sendText(XiaoZhiMcpJsonRpcJson.getNotificationsInitializedJson());
|
||||||
client.sendText(notificationJson);
|
|
||||||
|
|
||||||
// 步骤3: 发送工具列表请求 - 立即发送,无需额外延迟
|
// 步骤3: 发送工具列表请求 - 立即发送,无需额外延迟
|
||||||
log.info("发送MCP工具列表请求,智能体ID: {}", id);
|
log.info("发送MCP工具列表请求,智能体ID: {}", id);
|
||||||
McpJsonRpcRequest toolsRequest = new McpJsonRpcRequest("tools/list", null, 2);
|
client.sendText(XiaoZhiMcpJsonRpcJson.getToolsListJson());
|
||||||
client.sendJson(toolsRequest);
|
|
||||||
|
|
||||||
// 等待工具列表响应 (id=2)
|
// 等待工具列表响应 (id=2)
|
||||||
List<String> toolsResponses = client.listener(response -> {
|
List<String> toolsResponses = client.listener(response -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user