|
13 | 13 | package com.alibaba.higress.sdk.service.mcp.detail; |
14 | 14 |
|
15 | 15 | import java.util.ArrayList; |
16 | | -import java.util.HashMap; |
| 16 | +import java.util.Collections; |
17 | 17 | import java.util.List; |
18 | | -import java.util.Map; |
19 | 18 | import java.util.Optional; |
20 | 19 |
|
21 | 20 | import org.apache.commons.lang3.StringUtils; |
|
31 | 30 | import com.alibaba.higress.sdk.service.mcp.McpServerConfigMapHelper; |
32 | 31 |
|
33 | 32 | import io.kubernetes.client.openapi.models.V1ConfigMap; |
| 33 | +import lombok.Builder; |
| 34 | +import lombok.Data; |
34 | 35 | import lombok.extern.slf4j.Slf4j; |
35 | 36 |
|
36 | 37 | /** |
@@ -79,38 +80,105 @@ private void completeConfigFields(String name, McpServer result) { |
79 | 80 | * @return db tools config YAML |
80 | 81 | */ |
81 | 82 | private String buildDatabaseToolsConfig(String serverName, String dbType) { |
| 83 | + SchemaConfig schemaConfig = buildSchemaConfig(serverName, dbType); |
| 84 | + String result = ""; |
| 85 | + try { |
| 86 | + result = YAML.writeValueAsString(schemaConfig); |
| 87 | + } catch (Exception e) { |
| 88 | + log.error("Failed to build db tools config", e); |
| 89 | + } |
| 90 | + return result; |
| 91 | + } |
82 | 92 |
|
83 | | - Map<String, Object> sqlArg = new HashMap<>(); |
84 | | - sqlArg.put("name", "sql"); |
85 | | - sqlArg.put("type", "string"); |
86 | | - sqlArg.put("description", "The sql query to execute"); |
87 | | - sqlArg.put("required", true); |
88 | | - sqlArg.put("position", "body"); |
| 93 | + @Data |
| 94 | + @Builder |
| 95 | + static class ToolArgsSchemaConfig { |
| 96 | + private String name; |
| 97 | + private String type; |
| 98 | + private String description; |
| 99 | + private boolean required; |
| 100 | + private String position; |
| 101 | + } |
| 102 | + @Data |
| 103 | + @Builder |
| 104 | + static class ToolsSchemaConfig { |
| 105 | + private String name; |
| 106 | + private String description; |
| 107 | + private List<ToolArgsSchemaConfig> args; |
| 108 | + } |
| 109 | + @Data |
| 110 | + @Builder |
| 111 | + static class SchemaConfig { |
| 112 | + private String server; |
| 113 | + private List<ToolsSchemaConfig> tools; |
| 114 | + } |
89 | 115 |
|
90 | | - List<Map<String, Object>> args = new ArrayList<>(); |
91 | | - args.add(sqlArg); |
| 116 | + private SchemaConfig buildSchemaConfig(String server, String dbType) { |
| 117 | + |
| 118 | + String descriptionSuffix = String.format("in database %s.", dbType); |
| 119 | + |
| 120 | + List<ToolsSchemaConfig> tools = new ArrayList<>(); |
| 121 | + tools.add(buildToolsConfig("query", |
| 122 | + String.format("Run a read-only SQL query %s", descriptionSuffix), |
| 123 | + buildQueryToolsConfig())); |
| 124 | + tools.add(buildToolsConfig("execute", |
| 125 | + String.format("Execute an insert, update, or delete SQL %s", descriptionSuffix), |
| 126 | + buildExecuteToolsConfig())); |
| 127 | + tools.add(buildToolsConfig("list tables", |
| 128 | + String.format("List all tables %s", descriptionSuffix), |
| 129 | + Collections.emptyList())); |
| 130 | + tools.add(buildToolsConfig("describe table", |
| 131 | + String.format("Get the structure of a specific table %s", descriptionSuffix), |
| 132 | + buildDescribeTableToolsConfig())); |
| 133 | + |
| 134 | + return SchemaConfig.builder() |
| 135 | + .server(server) |
| 136 | + .tools(tools) |
| 137 | + .build(); |
| 138 | + } |
92 | 139 |
|
93 | | - Map<String, Object> queryTool = new HashMap<>(); |
94 | | - queryTool.put("name", "query"); |
95 | | - queryTool.put("description", String.format("Run a read-only SQL query in database %s.", dbType)); |
96 | | - queryTool.put("args", args); |
| 140 | + private List<ToolArgsSchemaConfig> buildQueryToolsConfig() { |
| 141 | + ToolArgsSchemaConfig build = ToolArgsSchemaConfig.builder() |
| 142 | + .name("sql") |
| 143 | + .type("string") |
| 144 | + .description("The sql query to execute") |
| 145 | + .required(true) |
| 146 | + .position("body") |
| 147 | + .build(); |
97 | 148 |
|
98 | | - List<Map<String, Object>> tools = new ArrayList<>(); |
99 | | - tools.add(queryTool); |
| 149 | + return Collections.singletonList(build); |
| 150 | + } |
100 | 151 |
|
101 | | - Map<String, Object> server = new HashMap<>(); |
102 | | - server.put("name", serverName); |
| 152 | + private List<ToolArgsSchemaConfig> buildExecuteToolsConfig() { |
| 153 | + ToolArgsSchemaConfig build = ToolArgsSchemaConfig.builder() |
| 154 | + .name("sql") |
| 155 | + .type("string") |
| 156 | + .description("The sql to execute") |
| 157 | + .required(true) |
| 158 | + .position("body") |
| 159 | + .build(); |
103 | 160 |
|
104 | | - Map<String, Object> config = new HashMap<>(); |
105 | | - config.put("tools", tools); |
106 | | - config.put("server", server); |
| 161 | + return Collections.singletonList(build); |
| 162 | + } |
107 | 163 |
|
108 | | - String result = ""; |
109 | | - try { |
110 | | - result = YAML.writeValueAsString(config); |
111 | | - } catch (Exception e) { |
112 | | - log.error("Failed to build db tools config", e); |
113 | | - } |
114 | | - return result; |
| 164 | + private List<ToolArgsSchemaConfig> buildDescribeTableToolsConfig() { |
| 165 | + ToolArgsSchemaConfig build = ToolArgsSchemaConfig.builder() |
| 166 | + .name("table") |
| 167 | + .type("string") |
| 168 | + .description("table name") |
| 169 | + .required(true) |
| 170 | + .position("body") |
| 171 | + .build(); |
| 172 | + |
| 173 | + return Collections.singletonList(build); |
115 | 174 | } |
| 175 | + |
| 176 | + private ToolsSchemaConfig buildToolsConfig(String name, String desc, List<ToolArgsSchemaConfig> args) { |
| 177 | + return ToolsSchemaConfig.builder() |
| 178 | + .name(name) |
| 179 | + .description(desc) |
| 180 | + .args(args) |
| 181 | + .build(); |
| 182 | + } |
| 183 | + |
116 | 184 | } |
0 commit comments