mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
添加mcp工具列表接口
This commit is contained in:
+22
-5
@@ -1,9 +1,16 @@
|
||||
package xiaozhi.modules.agent.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.shiro.authz.annotation.RequiresPermissions;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import xiaozhi.common.utils.Result;
|
||||
import xiaozhi.modules.agent.service.AgentMcpAccessPointService;
|
||||
|
||||
@@ -16,16 +23,26 @@ public class AgentMcpAccessPointController {
|
||||
|
||||
/**
|
||||
* 获取智能体的Mcp接入点地址
|
||||
*
|
||||
* @param audioId 智能体id
|
||||
* @return 返回错误提醒或者Mcp接入点地址
|
||||
*/
|
||||
@Operation(summary = "获取智能体的Mcp接入点地址")
|
||||
@GetMapping("/address/{audioId}")
|
||||
public Result<String> getAgentMcpAccessAddress(@PathVariable("audioId") String audioId) {
|
||||
String agentMcpAccessAddress = agentMcpAccessPointService.getAgentMcpAccessAddress(audioId);
|
||||
@GetMapping("/address/{agentId}")
|
||||
@RequiresPermissions("sys:role:normal")
|
||||
public Result<String> getAgentMcpAccessAddress(@PathVariable("agentId") String agentId) {
|
||||
String agentMcpAccessAddress = agentMcpAccessPointService.getAgentMcpAccessAddress(agentId);
|
||||
if (agentMcpAccessAddress == null) {
|
||||
return new Result<String>().error("请进入参数管理配置mcp接入点地址");
|
||||
return new Result<String>().ok("请进入参数管理配置mcp接入点地址");
|
||||
}
|
||||
return new Result<String>().ok(agentMcpAccessAddress);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取智能体的Mcp工具列表")
|
||||
@GetMapping("/tools/{agentId}")
|
||||
@RequiresPermissions("sys:role:normal")
|
||||
public Result<List<String>> getAgentMcpToolsList(@PathVariable("agentId") String agentId) {
|
||||
List<String> agentMcpToolsList = agentMcpAccessPointService.getAgentMcpToolsList(agentId);
|
||||
return new Result<List<String>>().ok(agentMcpToolsList);
|
||||
}
|
||||
}
|
||||
|
||||
+16
-30
@@ -1,20 +1,20 @@
|
||||
package xiaozhi.modules.agent.service.impl;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.stereotype.Service;
|
||||
import xiaozhi.common.constant.Constant;
|
||||
import xiaozhi.common.utils.AESUtils;
|
||||
import xiaozhi.common.utils.HashEncryptionUtil;
|
||||
import xiaozhi.modules.agent.service.AgentMcpAccessPointService;
|
||||
import xiaozhi.modules.sys.service.SysParamsService;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Service
|
||||
@Slf4j
|
||||
@@ -25,7 +25,7 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
||||
public String getAgentMcpAccessAddress(String id) {
|
||||
// 获取到mcp的地址
|
||||
String url = sysParamsService.getValue(Constant.SERVER_MCP_ENDPOINT, true);
|
||||
if (StringUtils.isBlank(url)) {
|
||||
if (StringUtils.isBlank(url) || "null".equals(url)) {
|
||||
return null;
|
||||
}
|
||||
URI uri = getURI(url);
|
||||
@@ -36,35 +36,22 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
||||
// 获取加密的token
|
||||
String encryptToken = encryptToken(id, key);
|
||||
// 返回智能体Mcp路径的格式
|
||||
String AgentMcpUrl = "%s/mcp?token=%s";
|
||||
return AgentMcpUrl.formatted(agentMcpUrl, encryptToken);
|
||||
|
||||
agentMcpUrl = "%s/mcp?token=%s".formatted(agentMcpUrl, encryptToken);
|
||||
return agentMcpUrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAgentMcpToolsList(String id) {
|
||||
List<String> list = List.of();
|
||||
String url = sysParamsService.getValue(Constant.SERVER_MCP_ENDPOINT, true);
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return list;
|
||||
String wsUrl = getAgentMcpAccessAddress(id);
|
||||
if (StringUtils.isBlank(wsUrl)) {
|
||||
return List.of();
|
||||
}
|
||||
URI uri = getURI(url);
|
||||
// 获取智能体mcp的url前缀
|
||||
String agentMcpUrl = getAgentMcpUrl(uri);
|
||||
// 获取密钥
|
||||
String key = getSecretKey(uri);
|
||||
// 获取加密的token
|
||||
String encryptToken = encryptToken(id, key);
|
||||
// 返回智能体Mcp路径的格式
|
||||
String AgentMcpUrl = "%s/call?token=%s";
|
||||
String formatted = AgentMcpUrl.formatted(agentMcpUrl, encryptToken);
|
||||
|
||||
|
||||
return list;
|
||||
return List.of();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取URI对象
|
||||
*
|
||||
* @param url 路径
|
||||
* @return URI对象
|
||||
*/
|
||||
@@ -107,7 +94,6 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
||||
return wsScheme + ":" + path;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取对智能体id加密的token
|
||||
*
|
||||
@@ -119,7 +105,7 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
|
||||
// 使用md5对智能体id进行加密
|
||||
String md5 = HashEncryptionUtil.Md5hexDigest(agentId);
|
||||
// aes需要加密文本
|
||||
String json = "{\"agentId\": %s}".formatted(md5);
|
||||
String json = "{\"agentId\": \"%s\"}".formatted(md5);
|
||||
// 加密后成token值
|
||||
return AESUtils.encrypt(key, json);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ baidu-aip==4.16.13
|
||||
chardet==5.2.0
|
||||
aioconsole==0.8.1
|
||||
markitdown==0.1.1
|
||||
mcp-proxy==0.6.0
|
||||
mcp-proxy==0.8.0
|
||||
PyJWT==2.8.0
|
||||
psutil==7.0.0
|
||||
portalocker==2.10.1
|
||||
Reference in New Issue
Block a user