davsclaus commented on code in PR #25203: URL: https://github.com/apache/camel/pull/25203#discussion_r3723091065
########## components/camel-ai/camel-mcp-server/src/main/java/org/apache/camel/component/mcp/server/jbang/JbangDevMcpServer.java: ########## @@ -0,0 +1,250 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.mcp.server.jbang; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.camel.CamelContext; +import org.apache.camel.CamelContextAware; +import org.apache.camel.component.ai.tool.AiToolParameterHelper.ParameterDef; +import org.apache.camel.component.mcp.server.McpServerInfo; +import org.apache.camel.component.mcp.server.McpServerTool; +import org.apache.camel.component.mcp.server.McpToolCallHandler; +import org.apache.camel.component.mcp.server.McpToolCallResult; +import org.apache.camel.component.mcp.server.vertx.VertxMcpServerEngine; +import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpRouter; +import org.apache.camel.support.service.ServiceSupport; +import org.apache.camel.util.json.JsonArray; +import org.apache.camel.util.json.JsonObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Dev/diagnostics MCP server on the management HTTP port, exposing shared JBang {@code ToolRegistry} tools through + * {@link VertxMcpServerEngine}. JBang classes are resolved reflectively so {@code camel-jbang-core} is not required at + * compile time. + */ +public class JbangDevMcpServer extends ServiceSupport implements CamelContextAware { + + private static final Logger LOG = LoggerFactory.getLogger(JbangDevMcpServer.class); + private static final String SERVER_NAME = "camel-jbang-dev-tools"; + private static final String TOOL_REGISTRY = "org.apache.camel.dsl.jbang.core.commands.ai.ToolRegistry"; + private static final String TOOL_CONTEXT = "org.apache.camel.dsl.jbang.core.commands.ai.ToolContext"; + + private CamelContext camelContext; + private String path = "/mcp"; + private VertxMcpServerEngine engine; + private Object toolContext; + + @Override + public CamelContext getCamelContext() { + return camelContext; + } + + @Override + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + @Override + protected void doStart() throws Exception { + toolContext = createToolContext(); + + engine = new VertxMcpServerEngine(); + engine.setCamelContext(camelContext); + engine.setTargetServerType(resolveTargetServerType()); + String version = camelContext.getVersion(); + if (version == null || version.isBlank()) { + version = "unknown"; + } + engine.initialize(new McpServerInfo(SERVER_NAME, version, path)); + engine.start(); + + for (Object descriptor : allToolDescriptors()) { + engine.toolAdded(toMcpTool(descriptor)); + } + } + + @Override + protected void doStop() throws Exception { + if (engine != null) { + engine.stop(); + engine = null; + } + toolContext = null; + } + + private McpServerTool toMcpTool(Object descriptor) { + String toolName = invokeString(descriptor, "name"); + McpToolCallHandler handler = arguments -> { + try { + Object result = executeTool(toolName, stringArguments(arguments)); + return new McpToolCallResult(result != null ? result.toString() : "", false); + } catch (Exception e) { + Throwable failure = e; + if (e instanceof InvocationTargetException ite && ite.getCause() != null) { + failure = ite.getCause(); + } + String message = failure.getMessage(); + if (message == null || message.isBlank()) { + message = failure.getClass().getSimpleName(); + } + return new McpToolCallResult(message, true); + } + }; + return new McpServerTool() { + @Override + public String name() { + return toolName; + } Review Comment: Minor: `parameters()` returns `Map.of()` while `inputSchemaJson()` is populated from the descriptor's params. This works today because `VertxMcpServerEngine.toolAdded()` only uses `inputSchemaJson()`, but could be a gap for forward compatibility if a future engine implementation relies on `parameters()` instead. ########## dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java: ########## @@ -594,6 +594,19 @@ protected CamelContext createCamelContext() { configure().httpManagementServer().withEnabled(true); configure().httpManagementServer().withInfoEnabled(true); } + boolean mcp = "true".equals(getInitialProperties().get(getInstanceType() + ".mcp")); + if (mcp) { + configure().httpManagementServer().withEnabled(true); + configure().httpManagementServer().withMcpEnabled(true); + configure().httpManagementServer().withHost("127.0.0.1"); Review Comment: This unconditionally overrides the management server host to `127.0.0.1` when MCP is enabled. If a user also uses `--observe` for health/metrics, external probes (e.g. Kubernetes liveness checks) won't be able to reach the management endpoints. The CLI help documents this side effect, which is good. But is there a way for the user to override this back (e.g. via `camel.management.host` in properties)? If property-file values are applied after this code, it would work — worth confirming the ordering. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
