gnodet commented on code in PR #25052:
URL: https://github.com/apache/camel/pull/25052#discussion_r3638620124


##########
dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/RouteCostEstimateTools.java:
##########
@@ -0,0 +1,342 @@
+/*
+ * 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.dsl.jbang.core.commands.mcp;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import jakarta.enterprise.context.ApplicationScoped;
+
+import io.quarkiverse.mcp.server.Tool;
+import io.quarkiverse.mcp.server.ToolArg;
+import io.quarkiverse.mcp.server.ToolCallException;
+
+/**
+ * MCP Tool for estimating API costs of a Camel route based on its component 
usage.
+ * <p>
+ * Particularly useful for AI pipelines where Bedrock, Textract, and S3 calls 
have per-unit pricing. Provides
+ * per-execution cost estimates and projections at a given throughput.
+ * <p>
+ * Cost data is approximate and based on published AWS pricing as of 2025. 
Actual costs depend on model, region, and
+ * volume tiers.
+ */
+@ApplicationScoped
+public class RouteCostEstimateTools {
+
+    private static final Pattern YAML_SCHEME_PATTERN = Pattern.compile(
+            
"(?:uri:\\s*[\"']?|from:\\s+[\"']?|to:\\s+[\"']?|toD:\\s+[\"']?)([a-zA-Z][a-zA-Z0-9+.-]*):(?://)?",
+            Pattern.MULTILINE);

Review Comment:
   **YAML regex captures `uri` as a false scheme in multi-line routes.** The 
`\s+` in `from:\s+` matches newlines, so `from:\n  uri: "aws-bedrock:model"` 
captures `uri` instead of `aws-bedrock`. This silently omits the most expensive 
component from the cost estimate.
   
   The `uri:\s*` alternative already handles the multi-line case correctly — 
the `from:/to:/toD:` alternatives only need to match inline same-line syntax, 
so `[ \t]+` (horizontal whitespace) is sufficient:
   
   ```suggestion
       private static final Pattern YAML_SCHEME_PATTERN = Pattern.compile(
               "(?:uri:\\s*[\"']?|from:[ \\t]+[\"']?|to:[ \\t]+[\"']?|toD:[ 
\\t]+[\"']?)([a-zA-Z][a-zA-Z0-9+.-]*):(?://)?",
               Pattern.MULTILINE);
   ```



##########
dsl/camel-jbang/camel-jbang-mcp/src/test/java/org/apache/camel/dsl/jbang/core/commands/mcp/RouteCostEstimateToolsTest.java:
##########
@@ -0,0 +1,179 @@
+/*
+ * 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.dsl.jbang.core.commands.mcp;
+
+import java.util.List;
+
+import io.quarkiverse.mcp.server.ToolCallException;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+class RouteCostEstimateToolsTest {
+
+    private RouteCostEstimateTools tools;
+
+    @BeforeEach
+    void setUp() {
+        tools = new RouteCostEstimateTools();
+    }
+
+    private static final String AI_PIPELINE_ROUTE = """
+            - route:
+                id: ai-summarization
+                from:
+                  uri: "file:documents"
+                steps:
+                  - to: "docling:convert"
+                  - to: "aws-bedrock:label"
+                  - to: "aws2-s3:output-bucket"
+            """;
+
+    private static final String TEXTRACT_ROUTE = """
+            - route:
+                from:
+                  uri: "aws2-s3:input-bucket"
+                steps:
+                  - to: "aws2-textract:detect"
+                  - to: "aws-bedrock:label"
+            """;
+
+    private static final String SIMPLE_ROUTE = """
+            - route:
+                from:
+                  uri: "timer:tick"
+                steps:
+                  - to: "log:out"
+            """;
+
+    @Test
+    void shouldRequireRouteContent() {
+        assertThatThrownBy(() -> tools.camel_route_cost_estimate(null, null, 
null, null, null))
+                .isInstanceOf(ToolCallException.class)
+                .hasMessageContaining("Route content is required");
+    }
+
+    @Test
+    void shouldEstimateCostForAiPipeline() {
+        RouteCostEstimateTools.CostEstimateResult result
+                = tools.camel_route_cost_estimate(AI_PIPELINE_ROUTE, 100, 5, 
1000, 500);
+
+        assertThat(result).isNotNull();
+        assertThat(result.costBreakdown()).isNotNull();
+        assertThat(result.costBreakdown()).hasSizeGreaterThanOrEqualTo(2);
+        
assertThat(result.summary().mostExpensiveComponent()).isEqualTo("aws-bedrock");
+        assertThat(result.projection().messagesPerHour()).isEqualTo(100);
+        assertThat(result.disclaimer()).isNotBlank();
+    }
+
+    @Test
+    void shouldIdentifyBedrockAsMostExpensive() {
+        RouteCostEstimateTools.CostEstimateResult result
+                = tools.camel_route_cost_estimate(TEXTRACT_ROUTE, null, null, 
null, null);
+
+        assertThat(result.costBreakdown()).isNotNull();
+        
assertThat(result.costBreakdown().get(0).scheme()).isEqualTo("aws-bedrock");
+    }
+
+    @Test
+    void shouldReportNoPricedComponentsForSimpleRoute() {
+        RouteCostEstimateTools.CostEstimateResult result
+                = tools.camel_route_cost_estimate(SIMPLE_ROUTE, null, null, 
null, null);
+
+        assertThat(result.costBreakdown()).isNull();
+        assertThat(result.summary().note()).contains("No pay-per-use 
components");
+    }
+
+    @Test
+    void shouldIncludeDoclingAsFree() {
+        RouteCostEstimateTools.CostEstimateResult result
+                = tools.camel_route_cost_estimate(AI_PIPELINE_ROUTE, null, 
null, null, null);
+
+        assertThat(result.costBreakdown())
+                .anyMatch(c -> "docling".equals(c.scheme()) && 
c.estimatedCostPerExecution() == 0);
+    }
+
+    @Test
+    void shouldProjectMonthlyCosts() {
+        RouteCostEstimateTools.CostEstimateResult result
+                = tools.camel_route_cost_estimate(TEXTRACT_ROUTE, 1000, 10, 
2000, 1000);
+
+        assertThat(result.projection()).isNotNull();
+        assertThat(result.projection().monthly()).startsWith("$");
+        assertThat(result.projection().messagesPerHour()).isEqualTo(1000);
+    }
+
+    @Test
+    void shouldProvideOptimizationTips() {
+        RouteCostEstimateTools.CostEstimateResult result
+                = tools.camel_route_cost_estimate(AI_PIPELINE_ROUTE, null, 
null, null, null);
+
+        assertThat(result.optimizationTips()).isNotNull();
+        assertThat(result.optimizationTips()).isNotEmpty();
+    }
+
+    @Test

Review Comment:
   Minor: `contains("docling", "aws-bedrock", "aws2-s3")` won't detect the 
false `uri` entry caused by the regex bug. Using 
`containsExactlyInAnyOrder("file", "docling", "aws-bedrock", "aws2-s3")` would 
catch both the false positive and the missing `file` scheme.



-- 
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]

Reply via email to