添加mcp工具列表接口

This commit is contained in:
hrz
2025-06-26 18:29:07 +08:00
parent 1d293244d3
commit 69ee8ab438
3 changed files with 39 additions and 36 deletions
@@ -1,9 +1,16 @@
package xiaozhi.modules.agent.controller; 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.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import xiaozhi.common.utils.Result; import xiaozhi.common.utils.Result;
import xiaozhi.modules.agent.service.AgentMcpAccessPointService; import xiaozhi.modules.agent.service.AgentMcpAccessPointService;
@@ -16,16 +23,26 @@ public class AgentMcpAccessPointController {
/** /**
* 获取智能体的Mcp接入点地址 * 获取智能体的Mcp接入点地址
*
* @param audioId 智能体id * @param audioId 智能体id
* @return 返回错误提醒或者Mcp接入点地址 * @return 返回错误提醒或者Mcp接入点地址
*/ */
@Operation(summary = "获取智能体的Mcp接入点地址") @Operation(summary = "获取智能体的Mcp接入点地址")
@GetMapping("/address/{audioId}") @GetMapping("/address/{agentId}")
public Result<String> getAgentMcpAccessAddress(@PathVariable("audioId") String audioId) { @RequiresPermissions("sys:role:normal")
String agentMcpAccessAddress = agentMcpAccessPointService.getAgentMcpAccessAddress(audioId); public Result<String> getAgentMcpAccessAddress(@PathVariable("agentId") String agentId) {
String agentMcpAccessAddress = agentMcpAccessPointService.getAgentMcpAccessAddress(agentId);
if (agentMcpAccessAddress == null) { if (agentMcpAccessAddress == null) {
return new Result<String>().error("请进入参数管理配置mcp接入点地址"); return new Result<String>().ok("请进入参数管理配置mcp接入点地址");
} }
return new Result<String>().ok(agentMcpAccessAddress); 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);
}
} }
@@ -1,20 +1,20 @@
package xiaozhi.modules.agent.service.impl; 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.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; 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.constant.Constant;
import xiaozhi.common.utils.AESUtils; import xiaozhi.common.utils.AESUtils;
import xiaozhi.common.utils.HashEncryptionUtil; import xiaozhi.common.utils.HashEncryptionUtil;
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 java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
@AllArgsConstructor @AllArgsConstructor
@Service @Service
@Slf4j @Slf4j
@@ -25,7 +25,7 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
public String getAgentMcpAccessAddress(String id) { public String getAgentMcpAccessAddress(String id) {
// 获取到mcp的地址 // 获取到mcp的地址
String url = sysParamsService.getValue(Constant.SERVER_MCP_ENDPOINT, true); String url = sysParamsService.getValue(Constant.SERVER_MCP_ENDPOINT, true);
if (StringUtils.isBlank(url)) { if (StringUtils.isBlank(url) || "null".equals(url)) {
return null; return null;
} }
URI uri = getURI(url); URI uri = getURI(url);
@@ -36,35 +36,22 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
// 获取加密的token // 获取加密的token
String encryptToken = encryptToken(id, key); String encryptToken = encryptToken(id, key);
// 返回智能体Mcp路径的格式 // 返回智能体Mcp路径的格式
String AgentMcpUrl = "%s/mcp?token=%s"; agentMcpUrl = "%s/mcp?token=%s".formatted(agentMcpUrl, encryptToken);
return AgentMcpUrl.formatted(agentMcpUrl, encryptToken); return agentMcpUrl;
} }
@Override @Override
public List<String> getAgentMcpToolsList(String id) { public List<String> getAgentMcpToolsList(String id) {
List<String> list = List.of(); String wsUrl = getAgentMcpAccessAddress(id);
String url = sysParamsService.getValue(Constant.SERVER_MCP_ENDPOINT, true); if (StringUtils.isBlank(wsUrl)) {
if (StringUtils.isBlank(url)) { return List.of();
return list;
} }
URI uri = getURI(url); return List.of();
// 获取智能体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;
} }
/** /**
* 获取URI对象 * 获取URI对象
*
* @param url 路径 * @param url 路径
* @return URI对象 * @return URI对象
*/ */
@@ -107,7 +94,6 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
return wsScheme + ":" + path; return wsScheme + ":" + path;
} }
/** /**
* 获取对智能体id加密的token * 获取对智能体id加密的token
* *
@@ -119,7 +105,7 @@ public class AgentMcpAccessPointServiceImpl implements AgentMcpAccessPointServic
// 使用md5对智能体id进行加密 // 使用md5对智能体id进行加密
String md5 = HashEncryptionUtil.Md5hexDigest(agentId); String md5 = HashEncryptionUtil.Md5hexDigest(agentId);
// aes需要加密文本 // aes需要加密文本
String json = "{\"agentId\": %s}".formatted(md5); String json = "{\"agentId\": \"%s\"}".formatted(md5);
// 加密后成token值 // 加密后成token值
return AESUtils.encrypt(key, json); return AESUtils.encrypt(key, json);
} }
+1 -1
View File
@@ -30,7 +30,7 @@ baidu-aip==4.16.13
chardet==5.2.0 chardet==5.2.0
aioconsole==0.8.1 aioconsole==0.8.1
markitdown==0.1.1 markitdown==0.1.1
mcp-proxy==0.6.0 mcp-proxy==0.8.0
PyJWT==2.8.0 PyJWT==2.8.0
psutil==7.0.0 psutil==7.0.0
portalocker==2.10.1 portalocker==2.10.1