This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch CAMEL-27404-mcp-enabled-property-sources
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 437575fa81e892ca10a044b5a1762d83d7b0aaa8
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 22 21:23:45 2026 +0100

    CAMEL-27404: camel-jbang - camel.server.mcp-enabled works from every 
property source
    
    camel.server.mcp-enabled (MCP over HTTP for ai-tool routes) failed with
    "The MCP server requires the Vert.x platform HTTP server" unless
    camel.server.enabled=true was set as well, from any property source. And
    the CLI read camel.server.mcp-enabled and mcp-transport only from the
    properties file, so --mcp-tags and the stdio setup (logging to stderr)
    were not applied when they came from --prop or -D.
    
    - KameletMain resolves camel.server.mcp-enabled and mcp-transport at
      runtime from every property source, and turns the HTTP server on for
      MCP over HTTP when camel.server.enabled is not configured.
      camel-mcp-server and camel-platform-http-main are downloaded on demand.
    - RunPipeline reads the MCP options from --prop, JVM system properties,
      environment variables and the profile properties, so --mcp-tags applies
      and MCP over stdio logs to stderr. --mcp-tags now also applies to MCP
      over stdio configured in the properties file.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../org/apache/camel/catalog/docs/mcp-server.adoc  | 14 +++-
 .../camel-mcp-server/src/main/docs/mcp-server.adoc | 14 +++-
 .../apache/camel/dsl/jbang/run/JBangRunner.java    |  2 +-
 .../apache/camel/dsl/jbang/run/RunPipeline.java    | 65 ++++++++++++++++-
 .../camel/dsl/jbang/run/RunPipelineTest.java       | 68 ++++++++++++++++++
 .../java/org/apache/camel/main/KameletMain.java    | 37 ++++++++++
 .../camel/main/KameletMainMcpHttpServerTest.java   | 81 ++++++++++++++++++++++
 7 files changed, 271 insertions(+), 10 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mcp-server.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mcp-server.adoc
index 225a8cc24870..4fc6643b8f0e 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mcp-server.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/mcp-server.adoc
@@ -218,7 +218,14 @@ Tag patterns support wildcards: use `+*+` to expose all 
tagged tools, or a
 prefix pattern like `+crm*+` to expose all tags starting with `crm`:
 
 The MCP endpoint is then served at `http://<host>:<port>/mcp` on the Camel
-main HTTP server. Any MCP client can connect over streamable HTTP, for
+main HTTP server.
+
+On Camel JBang (`camel run`), `camel.server.mcp-enabled = true` turns the HTTP 
server on by itself, so
+`camel.server.enabled = true` can be left out, and `camel-mcp-server` and 
`camel-platform-http-main` are downloaded on
+demand. As `camel.server.enabled`, the `camel.server.mcp-*` options work from 
every property source:
+`application.properties`, `--prop`, JVM system properties (`-D`) and 
environment variables, for example
+`camel run tools.yaml --prop camel.server.mcp-enabled=true --mcp-tags=crm`. 
Set `camel.server.enabled = false` to keep
+the HTTP server off. On Camel Main, set `camel.server.enabled = true` as shown 
above. Any MCP client can connect over streamable HTTP, for
 example another Camel integration using the
 xref:ROOT:openai-component.adoc[camel-openai] MCP client:
 
@@ -254,13 +261,14 @@ camel run tools.yaml --mcp-stdio --mcp-tags=crm,agent
 ----
 
 NOTE: `--mcp-tags` configures the ai-tool MCP server (`camel.server.mcp-tags`) 
and works with
-`--mcp-stdio` or HTTP transport when `camel.server.mcp-enabled` is set in 
properties. It is
+`--mcp-stdio` or HTTP transport when `camel.server.mcp-enabled` is set (in 
properties, with `--prop`, as a JVM system
+property or an environment variable). It is
 not used by `--mcp`, which exposes dev/diagnostics MCP tools on the management 
HTTP server.
 
 Logging and startup summaries are routed to stderr so stdout carries MCP
 JSON-RPC only. Camel JBang configures a stderr Log4j2 appender automatically
 when `--mcp-stdio` is used or when `camel.server.mcp-transport=stdio` is set
-in the profile. On Camel Main, configure logging to stderr yourself (for
+(in the profile, with `--prop`, as a JVM system property or an environment 
variable). On Camel Main, configure logging to stderr yourself (for
 example using the `log4j2-mcp-stdio.properties` resource shipped in
 `camel-mcp-server`). Anything else written to `System.out` while the MCP 
server runs, such as
 a `stream:out` endpoint or a `System.out.println` in a bean, goes to stderr as 
well.
diff --git a/components/camel-ai/camel-mcp-server/src/main/docs/mcp-server.adoc 
b/components/camel-ai/camel-mcp-server/src/main/docs/mcp-server.adoc
index 225a8cc24870..4fc6643b8f0e 100644
--- a/components/camel-ai/camel-mcp-server/src/main/docs/mcp-server.adoc
+++ b/components/camel-ai/camel-mcp-server/src/main/docs/mcp-server.adoc
@@ -218,7 +218,14 @@ Tag patterns support wildcards: use `+*+` to expose all 
tagged tools, or a
 prefix pattern like `+crm*+` to expose all tags starting with `crm`:
 
 The MCP endpoint is then served at `http://<host>:<port>/mcp` on the Camel
-main HTTP server. Any MCP client can connect over streamable HTTP, for
+main HTTP server.
+
+On Camel JBang (`camel run`), `camel.server.mcp-enabled = true` turns the HTTP 
server on by itself, so
+`camel.server.enabled = true` can be left out, and `camel-mcp-server` and 
`camel-platform-http-main` are downloaded on
+demand. As `camel.server.enabled`, the `camel.server.mcp-*` options work from 
every property source:
+`application.properties`, `--prop`, JVM system properties (`-D`) and 
environment variables, for example
+`camel run tools.yaml --prop camel.server.mcp-enabled=true --mcp-tags=crm`. 
Set `camel.server.enabled = false` to keep
+the HTTP server off. On Camel Main, set `camel.server.enabled = true` as shown 
above. Any MCP client can connect over streamable HTTP, for
 example another Camel integration using the
 xref:ROOT:openai-component.adoc[camel-openai] MCP client:
 
@@ -254,13 +261,14 @@ camel run tools.yaml --mcp-stdio --mcp-tags=crm,agent
 ----
 
 NOTE: `--mcp-tags` configures the ai-tool MCP server (`camel.server.mcp-tags`) 
and works with
-`--mcp-stdio` or HTTP transport when `camel.server.mcp-enabled` is set in 
properties. It is
+`--mcp-stdio` or HTTP transport when `camel.server.mcp-enabled` is set (in 
properties, with `--prop`, as a JVM system
+property or an environment variable). It is
 not used by `--mcp`, which exposes dev/diagnostics MCP tools on the management 
HTTP server.
 
 Logging and startup summaries are routed to stderr so stdout carries MCP
 JSON-RPC only. Camel JBang configures a stderr Log4j2 appender automatically
 when `--mcp-stdio` is used or when `camel.server.mcp-transport=stdio` is set
-in the profile. On Camel Main, configure logging to stderr yourself (for
+(in the profile, with `--prop`, as a JVM system property or an environment 
variable). On Camel Main, configure logging to stderr yourself (for
 example using the `log4j2-mcp-stdio.properties` resource shipped in
 `camel-mcp-server`). Anything else written to `System.out` while the MCP 
server runs, such as
 a `stream:out` endpoint or a `System.out.println` in a bean, goes to stderr as 
well.
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/JBangRunner.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/JBangRunner.java
index 2c0d64ef9ae7..c00b011f9624 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/JBangRunner.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/JBangRunner.java
@@ -66,7 +66,7 @@ final class JBangRunner {
             });
         }
         Properties profileProperties = !request.empty ? properties : null;
-        context.onPrepared(request, properties, 
RunPipeline.isMcpStdioEnabled(request.mcpStdio, profileProperties));
+        context.onPrepared(request, properties, 
RunPipeline.isMcpStdioEnabled(request, profileProperties));
 
         // the printer of the caller once it knows how the run logs (such as 
quiet with MCP over stdio)
         Printer printer = context.getPrinter();
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/RunPipeline.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/RunPipeline.java
index 8fb7b9e91b15..61a75cf05274 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/RunPipeline.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/RunPipeline.java
@@ -1005,7 +1005,7 @@ public class RunPipeline {
     }
 
     private boolean isMcpStdioEnabled(Properties profileProperties) {
-        return isMcpStdioEnabled(request.mcpStdio, profileProperties);
+        return isMcpStdioEnabled(request, profileProperties);
     }
 
     /**
@@ -1020,12 +1020,70 @@ public class RunPipeline {
                 && 
"stdio".equalsIgnoreCase(profileProperties.getProperty("camel.server.mcp-transport",
 "").trim());
     }
 
+    /**
+     * Whether the ai-tool routes are served over MCP stdio: with the option, 
or with camel.server.mcp-transport=stdio
+     * from any property source that the run reads up front: --prop, a JVM 
system property, an environment variable or
+     * the profile properties (CAMEL-27404). The run decides it before 
starting, as it logs to stderr then.
+     */
+    public static boolean isMcpStdioEnabled(RunRequest request, Properties 
profileProperties) {
+        if (request.mcpStdio) {
+            return true;
+        }
+        String transport = serverProperty(request, profileProperties, 
"mcp-transport", "mcpTransport");
+        return transport != null && "stdio".equalsIgnoreCase(transport.trim());
+    }
+
+    /**
+     * Whether the ai-tool routes are served over MCP, with 
camel.server.mcp-enabled from --prop, a JVM system property,
+     * an environment variable or the profile properties. Over HTTP, the run 
does not depend on it: camel-main resolves
+     * it at runtime, turns the HTTP server on and downloads camel-mcp-server 
and camel-platform-http-main on demand.
+     */
     boolean isAiToolMcpServerEnabled(Properties profileProperties) {
         if (isMcpStdioEnabled(profileProperties)) {
             return true;
         }
-        return profileProperties != null
-                && 
"true".equalsIgnoreCase(profileProperties.getProperty("camel.server.mcp-enabled",
 "").trim());
+        return isTrue(serverProperty(request, profileProperties, 
"mcp-enabled", "mcpEnabled"));
+    }
+
+    /**
+     * A camel.server option (by its dashed or camelCase name) from the 
sources that camel-main reads, in the order that
+     * wins there: --prop, JVM system property, environment variable, profile 
properties.
+     */
+    static String serverProperty(RunRequest request, Properties 
profileProperties, String dashed, String camelCase) {
+        String answer = null;
+        for (String option : new String[] { dashed, camelCase }) {
+            answer = propertyOfAnySource(request, profileProperties, 
"camel.server." + option);
+            if (answer != null) {
+                break;
+            }
+        }
+        return answer;
+    }
+
+    private static String propertyOfAnySource(RunRequest request, Properties 
profileProperties, String key) {
+        String answer = argumentProperty(request, key);
+        if (answer == null) {
+            answer = System.getProperty(key);
+        }
+        if (answer == null) {
+            answer = System.getenv(key.toUpperCase(Locale.ROOT).replace('.', 
'_').replace('-', '_'));
+        }
+        if (answer == null && profileProperties != null) {
+            answer = profileProperties.getProperty(key);
+        }
+        return answer;
+    }
+
+    private static String argumentProperty(RunRequest request, String key) {
+        if (request.applicationProperties != null) {
+            for (String p : request.applicationProperties) {
+                String k = StringHelper.before(p, "=");
+                if (k != null && key.equals(k.trim())) {
+                    return StringHelper.after(p, "=");
+                }
+            }
+        }
+        return null;
     }
 
     void applyMcpTagsOverride(KameletMain main) {
@@ -1107,6 +1165,7 @@ public class RunPipeline {
         } else {
             writeSetting(main, profileProperties, "camel.server.mcp-enabled", 
"true");
             writeSetting(main, profileProperties, 
"camel.server.mcp-transport", "stdio");
+            applyMcpTagsOverride(main);
         }
         main.addOverrideProperty("camel.main.startupSummaryLevel", "Off");
         main.setSilent(true);
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/RunPipelineTest.java
 
b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/RunPipelineTest.java
index 4858420d68ea..e1f53b236ce2 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/RunPipelineTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/RunPipelineTest.java
@@ -243,6 +243,74 @@ class RunPipelineTest {
         
assertThat(main.getOverrideProperties().getProperty("camel.server.mcp-tags")).isEqualTo("crm,notify");
     }
 
+    @Test
+    void mcpEnabledFromTheCommandLineAppliesTheMcpTags() {
+        // camel run --prop camel.server.mcp-enabled=true --mcp-tags=crm 
(CAMEL-27404)
+        RunRequest request = request();
+        request.applicationProperties = new String[] { 
"camel.server.mcp-enabled=true" };
+        RunPipeline pipeline = new RunPipeline(request);
+
+        assertThat(pipeline.isAiToolMcpServerEnabled(new 
Properties())).isTrue();
+        assertThat(pipeline.isAiToolMcpServerEnabled(null)).isTrue();
+
+        request.applicationProperties = new String[] { 
"camel.server.mcpEnabled = true" };
+        assertThat(pipeline.isAiToolMcpServerEnabled(null)).isTrue();
+    }
+
+    @Test
+    void mcpEnabledFromAJvmSystemProperty() {
+        RunRequest request = request();
+        RunPipeline pipeline = new RunPipeline(request);
+        assertThat(pipeline.isAiToolMcpServerEnabled(null)).isFalse();
+
+        System.setProperty("camel.server.mcp-enabled", "true");
+        try {
+            assertThat(pipeline.isAiToolMcpServerEnabled(null)).isTrue();
+        } finally {
+            System.clearProperty("camel.server.mcp-enabled");
+        }
+    }
+
+    @Test
+    void theCommandLineWinsOverTheProfileForMcpEnabled() {
+        RunRequest request = request();
+        request.applicationProperties = new String[] { 
"camel.server.mcp-enabled=false" };
+        Properties profile = new Properties();
+        profile.setProperty("camel.server.mcp-enabled", "true");
+
+        assertThat(new 
RunPipeline(request).isAiToolMcpServerEnabled(profile)).isFalse();
+    }
+
+    @Test
+    void mcpStdioFromTheCommandLine() {
+        RunRequest request = request();
+        request.applicationProperties = new String[] { 
"camel.server.mcp-transport=stdio" };
+
+        assertThat(RunPipeline.isMcpStdioEnabled(request, null)).isTrue();
+
+        request.applicationProperties = null;
+        assertThat(RunPipeline.isMcpStdioEnabled(request, null)).isFalse();
+        System.setProperty("camel.server.mcp-transport", "stdio");
+        try {
+            assertThat(RunPipeline.isMcpStdioEnabled(request, null)).isTrue();
+        } finally {
+            System.clearProperty("camel.server.mcp-transport");
+        }
+    }
+
+    @Test
+    void applyMcpTagsOverrideForStdioOfTheProfile() {
+        RunRequest request = request();
+        request.mcpTags = "agent";
+        KameletMain main = new KameletMain("jbang");
+        Properties profile = new Properties();
+        profile.setProperty("camel.server.mcp-transport", "stdio");
+
+        new RunPipeline(request).applyMcpStdioRuntimeOptions(main, profile);
+
+        
assertThat(main.getOverrideProperties().getProperty("camel.server.mcp-tags")).isEqualTo("agent");
+    }
+
     @Test
     void mcpStdioOfTheProfile() {
         Properties profile = new Properties();
diff --git 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
index 849151eefc2a..24e44a9b721c 100644
--- 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
+++ 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
@@ -905,6 +905,10 @@ public class KameletMain extends MainCommandLineSupport {
         if ("true".equals(getInitialProperties().get(getInstanceType() + 
".mcp"))) {
             applyMcpManagementHost(camelContext);
         }
+        // not while exporting, transforming or resolving dependencies, which 
run no HTTP server
+        if (!silent && 
!"true".equals(getInitialProperties().get(getInstanceType() + ".export"))) {
+            applyMcpHttpServer(camelContext);
+        }
         // auto configure camel afterwards
         super.autoconfigure(camelContext);
     }
@@ -1142,6 +1146,39 @@ public class KameletMain extends MainCommandLineSupport {
         configure().httpManagementServer().withHost("127.0.0.1");
     }
 
+    /**
+     * camel.server.mcp-enabled serves the ai-tool, ai-resource and ai-prompt 
routes over MCP on the HTTP server, so it
+     * turns the HTTP server on when camel.server.enabled is not configured. 
Decided before the auto configuration, when
+     * every property source (application.properties, --prop, JVM system 
properties, environment variables) can be
+     * resolved, so the CLI does not have to decide it up front: 
camel-platform-http-main and camel-mcp-server are then
+     * downloaded on demand (CAMEL-27404). Not for MCP over stdio, which needs 
no HTTP server, and not when
+     * camel.server.enabled is configured, such as camel.server.enabled=false.
+     */
+    void applyMcpHttpServer(CamelContext camelContext) {
+        HttpServerConfigurationProperties server = configure().httpServer();
+        boolean mcpEnabled = resolveServerProperty(camelContext, 
"mcp-enabled", "mcpEnabled")
+                .map(v -> "true".equalsIgnoreCase(v.trim()))
+                .orElse(server.isMcpEnabled());
+        if (!mcpEnabled) {
+            return;
+        }
+        String transport
+                = resolveServerProperty(camelContext, "mcp-transport", 
"mcpTransport").orElse(server.getMcpTransport());
+        if (transport != null && "stdio".equalsIgnoreCase(transport.trim())) {
+            return;
+        }
+        if 
(camelContext.getPropertiesComponent().resolveProperty("camel.server.enabled").isPresent())
 {
+            return;
+        }
+        server.setEnabled(true);
+    }
+
+    private static Optional<String> resolveServerProperty(CamelContext 
camelContext, String dashed, String camelCase) {
+        var pc = camelContext.getPropertiesComponent();
+        Optional<String> answer = pc.resolveProperty("camel.server." + dashed);
+        return answer.isPresent() ? answer : 
pc.resolveProperty("camel.server." + camelCase);
+    }
+
     private static String getPid() {
         return String.valueOf(ProcessHandle.current().pid());
     }
diff --git 
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainMcpHttpServerTest.java
 
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainMcpHttpServerTest.java
new file mode 100644
index 000000000000..55b01059c656
--- /dev/null
+++ 
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainMcpHttpServerTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.main;
+
+import java.util.Properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * camel.server.mcp-enabled turns the HTTP server on at runtime, from every 
property source, as camel.server.enabled
+ * does (CAMEL-27404).
+ */
+class KameletMainMcpHttpServerTest {
+
+    @Test
+    void mcpEnabledTurnsTheHttpServerOn() throws Exception {
+        assertTrue(httpServerEnabled("camel.server.mcp-enabled", "true"));
+        assertTrue(httpServerEnabled("camel.server.mcpEnabled", "true"));
+        assertTrue(httpServerEnabled("camel.server.mcp-enabled", "true", 
"camel.server.mcp-transport", "http"));
+    }
+
+    @Test
+    void mcpEnabledAsJvmSystemPropertyTurnsTheHttpServerOn() throws Exception {
+        System.setProperty("camel.server.mcp-enabled", "true");
+        try {
+            assertTrue(httpServerEnabled());
+        } finally {
+            System.clearProperty("camel.server.mcp-enabled");
+        }
+    }
+
+    @Test
+    void mcpDisabledLeavesTheHttpServerOff() throws Exception {
+        assertFalse(httpServerEnabled());
+        assertFalse(httpServerEnabled("camel.server.mcp-enabled", "false"));
+    }
+
+    @Test
+    void mcpOverStdioNeedsNoHttpServer() throws Exception {
+        assertFalse(httpServerEnabled("camel.server.mcp-enabled", "true", 
"camel.server.mcp-transport", "stdio"));
+        assertFalse(httpServerEnabled("camel.server.mcp-enabled", "true", 
"camel.server.mcpTransport", " STDIO "));
+    }
+
+    @Test
+    void aConfiguredHttpServerIsLeftToCamelMain() throws Exception {
+        // camel-main binds camel.server.enabled later on
+        assertFalse(httpServerEnabled("camel.server.mcp-enabled", "true", 
"camel.server.enabled", "false"));
+    }
+
+    private static boolean httpServerEnabled(String... properties) throws 
Exception {
+        KameletMain main = new KameletMain("camel.jbang");
+        try (CamelContext context = new DefaultCamelContext()) {
+            Properties initial = new Properties();
+            for (int i = 0; i < properties.length; i += 2) {
+                initial.setProperty(properties[i], properties[i + 1]);
+            }
+            context.getPropertiesComponent().setInitialProperties(initial);
+            main.applyMcpHttpServer(context);
+        }
+        return main.configure().httpServer().isEnabled();
+    }
+}

Reply via email to