|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.antgroup.geaflow.mcp.server; |
| 21 | + |
| 22 | +import com.alibaba.fastjson.JSON; |
| 23 | +import com.alibaba.fastjson.JSONObject; |
| 24 | +import com.antgroup.geaflow.analytics.service.client.AnalyticsClient; |
| 25 | +import com.antgroup.geaflow.analytics.service.client.AnalyticsClientBuilder; |
| 26 | +import com.antgroup.geaflow.analytics.service.query.QueryResults; |
| 27 | +import com.antgroup.geaflow.common.config.Configuration; |
| 28 | +import com.antgroup.geaflow.mcp.util.YamlParser; |
| 29 | +import java.util.Map; |
| 30 | +import org.noear.solon.ai.annotation.ResourceMapping; |
| 31 | +import org.noear.solon.ai.annotation.ToolMapping; |
| 32 | +import org.noear.solon.ai.mcp.server.annotation.McpServerEndpoint; |
| 33 | +import org.noear.solon.annotation.Param; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +@McpServerEndpoint(name = "geaflow-mcp-server", sseEndpoint = "/geaflow/sse") |
| 38 | +public class GeaFlowMcpServerTools { |
| 39 | + private static final Logger LOGGER = LoggerFactory.getLogger(GeaFlowMcpServerTools.class); |
| 40 | + |
| 41 | + private static final String RETRY_TIMES = "analytics.retry.times"; |
| 42 | + private static final int DEFAULT_RETRY_TIMES = 3; |
| 43 | + private static final String ERROR = "error"; |
| 44 | + public static final String SERVER_HOST = "analytics.server.host"; |
| 45 | + public static final String SERVER_PORT = "analytics.server.port"; |
| 46 | + public static final String SERVER_USER = "analytics.query.user"; |
| 47 | + public static final String QUERY_TIMEOUT_MS = "analytics.query.timeout.ms"; |
| 48 | + public static final String INIT_CHANNEL_POOLS = "analytics.init.channel.pools"; |
| 49 | + public static final String CONFIG = "analytics.client.config"; |
| 50 | + public static final String CURRENT_VERSION = "v1.0.0"; |
| 51 | + |
| 52 | + /** |
| 53 | + * Resource that provides getting geaflow mcp server version. |
| 54 | + * @return version id. |
| 55 | + */ |
| 56 | + @ResourceMapping(uri = "config://mcp-server-version", description = "Get mcp server version") |
| 57 | + public String getServerVersion() { |
| 58 | + return CURRENT_VERSION; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * A tool that provides graph query capabilities. |
| 63 | + * @param query GQL query. |
| 64 | + * @return query result or error code. |
| 65 | + */ |
| 66 | + @ToolMapping(description = "execute query") |
| 67 | + public String executeQuery(@Param(name = "query", description = "query") String query) { |
| 68 | + AnalyticsClient analyticsClient = null; |
| 69 | + |
| 70 | + try { |
| 71 | + Map<String, Object> config = YamlParser.loadConfig(); |
| 72 | + int retryTimes = DEFAULT_RETRY_TIMES; |
| 73 | + if (config.containsKey(RETRY_TIMES)) { |
| 74 | + retryTimes = Integer.parseInt(config.get(RETRY_TIMES).toString()); |
| 75 | + } |
| 76 | + |
| 77 | + AnalyticsClientBuilder builder = AnalyticsClient |
| 78 | + .builder() |
| 79 | + .withHost(config.get(SERVER_HOST).toString()) |
| 80 | + .withPort((Integer) config.get(SERVER_PORT)) |
| 81 | + .withRetryNum(retryTimes); |
| 82 | + if (config.containsKey(CONFIG)) { |
| 83 | + Map<String, String> clientConfig = JSON.parseObject(config.get(CONFIG).toString(), Map.class); |
| 84 | + Configuration configuration = new Configuration(clientConfig); |
| 85 | + builder.withConfiguration(configuration); |
| 86 | + LOGGER.info("client config: {}", configuration); |
| 87 | + } |
| 88 | + if (config.containsKey(SERVER_USER)) { |
| 89 | + builder.withUser(config.get(SERVER_USER).toString()); |
| 90 | + } |
| 91 | + if (config.containsKey(QUERY_TIMEOUT_MS)) { |
| 92 | + builder.withTimeoutMs((Integer) config.get(QUERY_TIMEOUT_MS)); |
| 93 | + } |
| 94 | + if (config.containsKey(INIT_CHANNEL_POOLS)) { |
| 95 | + builder.withInitChannelPools((Boolean) config.get(INIT_CHANNEL_POOLS)); |
| 96 | + } |
| 97 | + analyticsClient = builder.build(); |
| 98 | + |
| 99 | + QueryResults queryResults = analyticsClient.executeQuery(query); |
| 100 | + if (queryResults.getError() != null) { |
| 101 | + final JSONObject error = new JSONObject(); |
| 102 | + error.put(ERROR, queryResults.getError()); |
| 103 | + return error.toJSONString(); |
| 104 | + } |
| 105 | + return queryResults.getFormattedData(); |
| 106 | + } catch (Exception e) { |
| 107 | + LOGGER.error(e.getMessage(), e); |
| 108 | + throw new RuntimeException(e); |
| 109 | + } finally { |
| 110 | + if (analyticsClient != null) { |
| 111 | + analyticsClient.shutdown(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments