in spring-ai-alibaba-jmanus/src/main/java/com/alibaba/cloud/ai/example/manus/dynamic/mcp/service/McpService.java [189:253]
public List<McpConfigEntity> insertOrupdateMcpRepo(McpConfigRequestVO mcpConfigVO) throws IOException {
List<McpConfigEntity> entityList = new ArrayList<>();
try (JsonParser jsonParser = new ObjectMapper().createParser(mcpConfigVO.getConfigJson())) {
McpServersConfig mcpServerConfig = jsonParser.readValueAs(McpServersConfig.class);
String type = mcpConfigVO.getConnectionType();
McpConfigType mcpConfigType = McpConfigType.valueOf(type);
if (McpConfigType.STUDIO.equals(mcpConfigType)) {
// STUDIO类型的连接需要特殊处理
mcpServerConfig.getMcpServers().forEach((name, config) -> {
if (config.getCommand() == null || config.getCommand().isEmpty()) {
throw new IllegalArgumentException(
"Missing required 'command' field in server configuration for " + name);
}
if (config.getUrl() != null && !config.getUrl().isEmpty()) {
throw new IllegalArgumentException(
"STUDIO type should not have 'url' field in server configuration for " + name);
}
});
}
else if (McpConfigType.SSE.equals(mcpConfigType)) {
// SSE类型的连接需要特殊处理
mcpServerConfig.getMcpServers().forEach((name, config) -> {
if (config.getUrl() == null || config.getUrl().isEmpty()) {
throw new IllegalArgumentException(
"Missing required 'url' field in server configuration for " + name);
}
if (config.getCommand() != null && !config.getCommand().isEmpty()) {
throw new IllegalArgumentException(
"SSE type should not have 'command' field in server configuration for " + name);
}
});
}
else if (McpConfigType.STREAMING.equals(mcpConfigType)) {
throw new UnsupportedOperationException("STREAMING connection type is not supported yet");
}
// 迭代处理每个MCP服务器配置
for (Map.Entry<String, McpServerConfig> entry : mcpServerConfig.getMcpServers().entrySet()) {
String serverName = entry.getKey();
McpServerConfig serverConfig = entry.getValue();
// 使用ServerConfig的toJson方法将配置转换为JSON字符串
String configJson = serverConfig.toJson();
// 查找对应的MCP配置实体
McpConfigEntity mcpConfigEntity = mcpConfigRepository.findByMcpServerName(serverName);
if (mcpConfigEntity == null) {
mcpConfigEntity = new McpConfigEntity();
mcpConfigEntity.setConnectionConfig(configJson);
mcpConfigEntity.setMcpServerName(serverName);
mcpConfigEntity.setConnectionType(mcpConfigType);
}
else {
mcpConfigEntity.setConnectionConfig(configJson);
mcpConfigEntity.setConnectionType(mcpConfigType);
}
McpConfigEntity entity = mcpConfigRepository.save(mcpConfigEntity);
entityList.add(entity);
logger.info("MCP server '{}' has been saved to database.", serverName);
}
}
return entityList;
}