oscerd commented on code in PR #25052: URL: https://github.com/apache/camel/pull/25052#discussion_r3656177710
########## dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/RouteCostEstimateTools.java: ########## @@ -0,0 +1,317 @@ +/* + * 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 SCHEME_PATTERN = Pattern.compile( + "(?:uri:\\s*[\"']?|from:\\s+[\"']?|to:\\s+[\"']?|toD:\\s+[\"']?)([a-zA-Z][a-zA-Z0-9+.-]*):(?://)?", + Pattern.MULTILINE); + + private static final Map<String, ComponentCostProfile> COST_PROFILES = buildCostProfiles(); + + @Tool(annotations = @Tool.Annotations(readOnlyHint = true, destructiveHint = false, openWorldHint = false), + description = "Estimate API costs for a Camel route based on its component usage. " + + "Analyzes a YAML or XML route definition and identifies components with " + + "pay-per-use pricing (Bedrock LLM, Textract, S3, SQS, SNS, etc.). " + + "Returns per-execution cost estimate and monthly projection at a given throughput. " + + "Cost data is approximate based on published AWS pricing.") + public CostEstimateResult camel_route_cost_estimate( + @ToolArg(description = "The Camel route definition (YAML or XML)") String route, + @ToolArg(description = "Expected messages per hour for cost projection (default: 100)") Integer messagesPerHour, + @ToolArg(description = "Average document pages per message for Textract/Docling (default: 5)") Integer avgPages, + @ToolArg(description = "Average LLM input tokens per request (default: 1000)") Integer avgInputTokens, + @ToolArg(description = "Average LLM output tokens per request (default: 500)") Integer avgOutputTokens) { + + if (route == null || route.isBlank()) { + throw new ToolCallException("Route content is required", null); + } + + int throughput = messagesPerHour != null && messagesPerHour > 0 ? messagesPerHour : 100; + int pages = avgPages != null && avgPages > 0 ? avgPages : 5; + int inTokens = avgInputTokens != null && avgInputTokens > 0 ? avgInputTokens : 1000; + int outTokens = avgOutputTokens != null && avgOutputTokens > 0 ? avgOutputTokens : 500; + + List<String> detectedSchemes = extractSchemes(route); + + List<ComponentCostBreakdown> breakdown = new ArrayList<>(); + double totalPerExecution = 0; + + for (String scheme : detectedSchemes) { + ComponentCostProfile profile = COST_PROFILES.get(scheme); + if (profile == null) { + continue; + } + + double costPerExec = profile.estimateCostPerExecution(pages, inTokens, outTokens); + totalPerExecution += costPerExec; + breakdown.add(new ComponentCostBreakdown( + scheme, profile.displayName(), profile.pricingModel(), + costPerExec, profile.pricingNote())); + } + + breakdown.sort(Comparator.comparingDouble(ComponentCostBreakdown::estimatedCostPerExecution).reversed()); + + double hourly = totalPerExecution * throughput; + double daily = hourly * 24; + double monthly = daily * 30; + + String mostExpensive = breakdown.isEmpty() ? null : breakdown.get(0).scheme(); + + List<String> optimizationTips = buildOptimizationTips(detectedSchemes, breakdown); + + CostProjection projection = new CostProjection( + throughput, + formatCost(hourly), formatCost(daily), formatCost(monthly)); + + CostSummary summary = new CostSummary( + formatCost(totalPerExecution), + breakdown.size(), + detectedSchemes.size(), + mostExpensive, + breakdown.isEmpty() ? "No pay-per-use components detected in this route" : null); + + return new CostEstimateResult( + breakdown.isEmpty() ? null : breakdown, + projection, summary, + optimizationTips.isEmpty() ? null : optimizationTips, + "Cost estimates are approximate based on published AWS pricing (us-east-1). " + + "Actual costs vary by region, volume tier, and model."); + } + + List<String> extractSchemes(String route) { + List<String> schemes = new ArrayList<>(); + Matcher m = SCHEME_PATTERN.matcher(route); + while (m.find()) { + String scheme = m.group(1); + if (!schemes.contains(scheme)) { + schemes.add(scheme); + } + } + return schemes; + } + + private List<String> buildOptimizationTips(List<String> schemes, List<ComponentCostBreakdown> breakdown) { + List<String> tips = new ArrayList<>(); + + if (schemes.contains("aws-bedrock")) { + tips.add("Consider using a smaller/cheaper Bedrock model for simpler tasks " + + "(e.g., Nova Lite instead of Claude for classification)"); + tips.add("Use streaming to reduce perceived latency without affecting token costs"); + } + if (schemes.contains("aws2-textract") && schemes.contains("docling")) { + tips.add("Using both Textract and Docling — consider using only Docling (free, open-source) " + + "for text extraction and reserving Textract for table/form extraction"); + } + if (schemes.contains("aws2-s3")) { + tips.add("S3 GET costs are minimal but add up at scale — consider caching frequently accessed documents"); + } + if (breakdown.size() > 1) { + tips.add("Most expensive component: " + breakdown.get(0).displayName() + + " — focus optimization here for maximum savings"); + } + return tips; + } + + private static String formatCost(double cost) { + if (cost < 0.01) { + return String.format("$%.6f", cost); + } + return String.format("$%.4f", cost); + } + + private static Map<String, ComponentCostProfile> buildCostProfiles() { + Map<String, ComponentCostProfile> profiles = new LinkedHashMap<>(); + + profiles.put("aws-bedrock", new ComponentCostProfile( + "AWS Bedrock (Claude Sonnet 4)", "per-token", + "Input: ~$3/MTok, Output: ~$15/MTok (Claude Sonnet 4 pricing)") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return (inputTokens * 3.0 / 1_000_000) + (outputTokens * 15.0 / 1_000_000); + } Review Comment: Addressed the 'at minimum' ask: the Bedrock display name is now "AWS Bedrock (model-dependent, estimated as Claude Sonnet 4)", its pricingNote spells out Nova Lite ~$0.06/$0.24, Haiku ~$0.25/$1.25, Opus ~$15/$75, and the top-level disclaimer (8cc9bbd) now states these are specific-model estimates, not a Bedrock-wide figure, and that cheaper/local models cost far less. The optional model parameter (the 'ideally') I'd add as a follow-up if you want per-model scaling — happy to. Leaving that as your call.\n\n_Claude Code on behalf of Andrea Cosentino (@oscerd)._ ########## dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/RouteCostEstimateTools.java: ########## @@ -0,0 +1,317 @@ +/* + * 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 SCHEME_PATTERN = Pattern.compile( + "(?:uri:\\s*[\"']?|from:\\s+[\"']?|to:\\s+[\"']?|toD:\\s+[\"']?)([a-zA-Z][a-zA-Z0-9+.-]*):(?://)?", + Pattern.MULTILINE); + + private static final Map<String, ComponentCostProfile> COST_PROFILES = buildCostProfiles(); + + @Tool(annotations = @Tool.Annotations(readOnlyHint = true, destructiveHint = false, openWorldHint = false), + description = "Estimate API costs for a Camel route based on its component usage. " + + "Analyzes a YAML or XML route definition and identifies components with " + + "pay-per-use pricing (Bedrock LLM, Textract, S3, SQS, SNS, etc.). " + + "Returns per-execution cost estimate and monthly projection at a given throughput. " + + "Cost data is approximate based on published AWS pricing.") + public CostEstimateResult camel_route_cost_estimate( + @ToolArg(description = "The Camel route definition (YAML or XML)") String route, + @ToolArg(description = "Expected messages per hour for cost projection (default: 100)") Integer messagesPerHour, + @ToolArg(description = "Average document pages per message for Textract/Docling (default: 5)") Integer avgPages, + @ToolArg(description = "Average LLM input tokens per request (default: 1000)") Integer avgInputTokens, + @ToolArg(description = "Average LLM output tokens per request (default: 500)") Integer avgOutputTokens) { + + if (route == null || route.isBlank()) { + throw new ToolCallException("Route content is required", null); + } + + int throughput = messagesPerHour != null && messagesPerHour > 0 ? messagesPerHour : 100; + int pages = avgPages != null && avgPages > 0 ? avgPages : 5; + int inTokens = avgInputTokens != null && avgInputTokens > 0 ? avgInputTokens : 1000; + int outTokens = avgOutputTokens != null && avgOutputTokens > 0 ? avgOutputTokens : 500; + + List<String> detectedSchemes = extractSchemes(route); + + List<ComponentCostBreakdown> breakdown = new ArrayList<>(); + double totalPerExecution = 0; + + for (String scheme : detectedSchemes) { + ComponentCostProfile profile = COST_PROFILES.get(scheme); + if (profile == null) { + continue; + } + + double costPerExec = profile.estimateCostPerExecution(pages, inTokens, outTokens); + totalPerExecution += costPerExec; + breakdown.add(new ComponentCostBreakdown( + scheme, profile.displayName(), profile.pricingModel(), + costPerExec, profile.pricingNote())); + } + + breakdown.sort(Comparator.comparingDouble(ComponentCostBreakdown::estimatedCostPerExecution).reversed()); + + double hourly = totalPerExecution * throughput; + double daily = hourly * 24; + double monthly = daily * 30; + + String mostExpensive = breakdown.isEmpty() ? null : breakdown.get(0).scheme(); + + List<String> optimizationTips = buildOptimizationTips(detectedSchemes, breakdown); + + CostProjection projection = new CostProjection( + throughput, + formatCost(hourly), formatCost(daily), formatCost(monthly)); + + CostSummary summary = new CostSummary( + formatCost(totalPerExecution), + breakdown.size(), + detectedSchemes.size(), + mostExpensive, + breakdown.isEmpty() ? "No pay-per-use components detected in this route" : null); + + return new CostEstimateResult( + breakdown.isEmpty() ? null : breakdown, + projection, summary, + optimizationTips.isEmpty() ? null : optimizationTips, + "Cost estimates are approximate based on published AWS pricing (us-east-1). " + + "Actual costs vary by region, volume tier, and model."); + } + + List<String> extractSchemes(String route) { + List<String> schemes = new ArrayList<>(); + Matcher m = SCHEME_PATTERN.matcher(route); + while (m.find()) { + String scheme = m.group(1); + if (!schemes.contains(scheme)) { + schemes.add(scheme); + } + } + return schemes; + } + + private List<String> buildOptimizationTips(List<String> schemes, List<ComponentCostBreakdown> breakdown) { + List<String> tips = new ArrayList<>(); + + if (schemes.contains("aws-bedrock")) { + tips.add("Consider using a smaller/cheaper Bedrock model for simpler tasks " + + "(e.g., Nova Lite instead of Claude for classification)"); + tips.add("Use streaming to reduce perceived latency without affecting token costs"); + } + if (schemes.contains("aws2-textract") && schemes.contains("docling")) { + tips.add("Using both Textract and Docling — consider using only Docling (free, open-source) " + + "for text extraction and reserving Textract for table/form extraction"); + } + if (schemes.contains("aws2-s3")) { + tips.add("S3 GET costs are minimal but add up at scale — consider caching frequently accessed documents"); + } + if (breakdown.size() > 1) { + tips.add("Most expensive component: " + breakdown.get(0).displayName() + + " — focus optimization here for maximum savings"); + } + return tips; + } + + private static String formatCost(double cost) { + if (cost < 0.01) { + return String.format("$%.6f", cost); + } + return String.format("$%.4f", cost); + } + + private static Map<String, ComponentCostProfile> buildCostProfiles() { + Map<String, ComponentCostProfile> profiles = new LinkedHashMap<>(); + + profiles.put("aws-bedrock", new ComponentCostProfile( + "AWS Bedrock (Claude Sonnet 4)", "per-token", + "Input: ~$3/MTok, Output: ~$15/MTok (Claude Sonnet 4 pricing)") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return (inputTokens * 3.0 / 1_000_000) + (outputTokens * 15.0 / 1_000_000); + } + }); + + profiles.put("aws2-textract", new ComponentCostProfile( + "AWS Textract", "per-page", + "DetectDocumentText: ~$1.50/1000 pages, AnalyzeDocument: ~$15/1000 pages") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return pages * 1.50 / 1000; + } + }); + + profiles.put("docling", new ComponentCostProfile( + "Docling (self-hosted)", "free", + "Open-source, self-hosted. Cost is only infrastructure (compute/memory)") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return 0; + } + }); + + profiles.put("aws2-s3", new ComponentCostProfile( + "AWS S3", "per-request", + "GET: ~$0.40/1M requests, PUT: ~$5/1M requests") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return 0.40 / 1_000_000; + } + }); + + profiles.put("aws2-sqs", new ComponentCostProfile( + "AWS SQS", "per-request", + "~$0.40 per 1M requests (Standard)") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return 0.40 / 1_000_000; + } + }); + + profiles.put("aws2-sns", new ComponentCostProfile( + "AWS SNS", "per-publish", + "~$0.50 per 1M publishes") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return 0.50 / 1_000_000; + } + }); + + profiles.put("aws2-kinesis", new ComponentCostProfile( + "AWS Kinesis", "per-shard-hour + per-PUT", + "~$0.015/shard/hour + $0.014/1M PUT units") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return 0.014 / 1_000_000; + } + }); Review Comment: Same treatment for langchain4j-chat: display name says "(model-dependent)", the pricingNote says "Estimated as Claude Sonnet 4; free with Ollama/local models", and the disclaimer now explicitly calls out that a route on a local/self-hosted model (Ollama) will cost ~zero, so the estimate is not read as a real figure for those. Provider-agnostic phantom cost is now clearly flagged rather than silent.\n\n_Claude Code on behalf of Andrea Cosentino (@oscerd)._ ########## dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/RouteCostEstimateTools.java: ########## @@ -0,0 +1,317 @@ +/* + * 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 SCHEME_PATTERN = Pattern.compile( + "(?:uri:\\s*[\"']?|from:\\s+[\"']?|to:\\s+[\"']?|toD:\\s+[\"']?)([a-zA-Z][a-zA-Z0-9+.-]*):(?://)?", + Pattern.MULTILINE); + + private static final Map<String, ComponentCostProfile> COST_PROFILES = buildCostProfiles(); + + @Tool(annotations = @Tool.Annotations(readOnlyHint = true, destructiveHint = false, openWorldHint = false), + description = "Estimate API costs for a Camel route based on its component usage. " + + "Analyzes a YAML or XML route definition and identifies components with " + + "pay-per-use pricing (Bedrock LLM, Textract, S3, SQS, SNS, etc.). " + + "Returns per-execution cost estimate and monthly projection at a given throughput. " + + "Cost data is approximate based on published AWS pricing.") + public CostEstimateResult camel_route_cost_estimate( + @ToolArg(description = "The Camel route definition (YAML or XML)") String route, + @ToolArg(description = "Expected messages per hour for cost projection (default: 100)") Integer messagesPerHour, + @ToolArg(description = "Average document pages per message for Textract/Docling (default: 5)") Integer avgPages, + @ToolArg(description = "Average LLM input tokens per request (default: 1000)") Integer avgInputTokens, + @ToolArg(description = "Average LLM output tokens per request (default: 500)") Integer avgOutputTokens) { + + if (route == null || route.isBlank()) { + throw new ToolCallException("Route content is required", null); + } + + int throughput = messagesPerHour != null && messagesPerHour > 0 ? messagesPerHour : 100; + int pages = avgPages != null && avgPages > 0 ? avgPages : 5; + int inTokens = avgInputTokens != null && avgInputTokens > 0 ? avgInputTokens : 1000; + int outTokens = avgOutputTokens != null && avgOutputTokens > 0 ? avgOutputTokens : 500; + + List<String> detectedSchemes = extractSchemes(route); + + List<ComponentCostBreakdown> breakdown = new ArrayList<>(); + double totalPerExecution = 0; + + for (String scheme : detectedSchemes) { + ComponentCostProfile profile = COST_PROFILES.get(scheme); + if (profile == null) { + continue; + } + + double costPerExec = profile.estimateCostPerExecution(pages, inTokens, outTokens); + totalPerExecution += costPerExec; + breakdown.add(new ComponentCostBreakdown( + scheme, profile.displayName(), profile.pricingModel(), + costPerExec, profile.pricingNote())); + } + + breakdown.sort(Comparator.comparingDouble(ComponentCostBreakdown::estimatedCostPerExecution).reversed()); + + double hourly = totalPerExecution * throughput; + double daily = hourly * 24; + double monthly = daily * 30; + + String mostExpensive = breakdown.isEmpty() ? null : breakdown.get(0).scheme(); + + List<String> optimizationTips = buildOptimizationTips(detectedSchemes, breakdown); + + CostProjection projection = new CostProjection( + throughput, + formatCost(hourly), formatCost(daily), formatCost(monthly)); + + CostSummary summary = new CostSummary( + formatCost(totalPerExecution), + breakdown.size(), + detectedSchemes.size(), + mostExpensive, + breakdown.isEmpty() ? "No pay-per-use components detected in this route" : null); + + return new CostEstimateResult( + breakdown.isEmpty() ? null : breakdown, + projection, summary, + optimizationTips.isEmpty() ? null : optimizationTips, + "Cost estimates are approximate based on published AWS pricing (us-east-1). " + + "Actual costs vary by region, volume tier, and model."); + } + + List<String> extractSchemes(String route) { + List<String> schemes = new ArrayList<>(); + Matcher m = SCHEME_PATTERN.matcher(route); + while (m.find()) { + String scheme = m.group(1); + if (!schemes.contains(scheme)) { + schemes.add(scheme); + } + } + return schemes; + } + + private List<String> buildOptimizationTips(List<String> schemes, List<ComponentCostBreakdown> breakdown) { + List<String> tips = new ArrayList<>(); + + if (schemes.contains("aws-bedrock")) { + tips.add("Consider using a smaller/cheaper Bedrock model for simpler tasks " + + "(e.g., Nova Lite instead of Claude for classification)"); + tips.add("Use streaming to reduce perceived latency without affecting token costs"); + } + if (schemes.contains("aws2-textract") && schemes.contains("docling")) { + tips.add("Using both Textract and Docling — consider using only Docling (free, open-source) " + + "for text extraction and reserving Textract for table/form extraction"); + } + if (schemes.contains("aws2-s3")) { + tips.add("S3 GET costs are minimal but add up at scale — consider caching frequently accessed documents"); + } + if (breakdown.size() > 1) { + tips.add("Most expensive component: " + breakdown.get(0).displayName() + + " — focus optimization here for maximum savings"); + } + return tips; + } + + private static String formatCost(double cost) { + if (cost < 0.01) { + return String.format("$%.6f", cost); + } + return String.format("$%.4f", cost); + } + + private static Map<String, ComponentCostProfile> buildCostProfiles() { + Map<String, ComponentCostProfile> profiles = new LinkedHashMap<>(); + + profiles.put("aws-bedrock", new ComponentCostProfile( + "AWS Bedrock (Claude Sonnet 4)", "per-token", Review Comment: Fair point on staleness. For now the data carries a pricingDataAsOf field (2025-Q2) and the disclaimer marks it approximate, and the immediate accuracy concern (model-specific labeling) is handled in-code. Externalizing to a classpath cost-profiles.json so it can be refreshed without a rebuild is a reasonable follow-up; I'd rather do that as its own change than fold a resource-loading mechanism into this PR. Noting it here.\n\n_Claude Code on behalf of Andrea Cosentino (@oscerd)._ ########## dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/RouteCostEstimateTools.java: ########## @@ -0,0 +1,317 @@ +/* + * 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 SCHEME_PATTERN = Pattern.compile( + "(?:uri:\\s*[\"']?|from:\\s+[\"']?|to:\\s+[\"']?|toD:\\s+[\"']?)([a-zA-Z][a-zA-Z0-9+.-]*):(?://)?", + Pattern.MULTILINE); + + private static final Map<String, ComponentCostProfile> COST_PROFILES = buildCostProfiles(); + + @Tool(annotations = @Tool.Annotations(readOnlyHint = true, destructiveHint = false, openWorldHint = false), + description = "Estimate API costs for a Camel route based on its component usage. " + + "Analyzes a YAML or XML route definition and identifies components with " + + "pay-per-use pricing (Bedrock LLM, Textract, S3, SQS, SNS, etc.). " + + "Returns per-execution cost estimate and monthly projection at a given throughput. " + + "Cost data is approximate based on published AWS pricing.") + public CostEstimateResult camel_route_cost_estimate( + @ToolArg(description = "The Camel route definition (YAML or XML)") String route, + @ToolArg(description = "Expected messages per hour for cost projection (default: 100)") Integer messagesPerHour, + @ToolArg(description = "Average document pages per message for Textract/Docling (default: 5)") Integer avgPages, + @ToolArg(description = "Average LLM input tokens per request (default: 1000)") Integer avgInputTokens, + @ToolArg(description = "Average LLM output tokens per request (default: 500)") Integer avgOutputTokens) { + + if (route == null || route.isBlank()) { + throw new ToolCallException("Route content is required", null); + } + + int throughput = messagesPerHour != null && messagesPerHour > 0 ? messagesPerHour : 100; + int pages = avgPages != null && avgPages > 0 ? avgPages : 5; + int inTokens = avgInputTokens != null && avgInputTokens > 0 ? avgInputTokens : 1000; + int outTokens = avgOutputTokens != null && avgOutputTokens > 0 ? avgOutputTokens : 500; + + List<String> detectedSchemes = extractSchemes(route); + + List<ComponentCostBreakdown> breakdown = new ArrayList<>(); + double totalPerExecution = 0; + + for (String scheme : detectedSchemes) { + ComponentCostProfile profile = COST_PROFILES.get(scheme); + if (profile == null) { + continue; + } + + double costPerExec = profile.estimateCostPerExecution(pages, inTokens, outTokens); + totalPerExecution += costPerExec; + breakdown.add(new ComponentCostBreakdown( + scheme, profile.displayName(), profile.pricingModel(), + costPerExec, profile.pricingNote())); + } + + breakdown.sort(Comparator.comparingDouble(ComponentCostBreakdown::estimatedCostPerExecution).reversed()); + + double hourly = totalPerExecution * throughput; + double daily = hourly * 24; + double monthly = daily * 30; + + String mostExpensive = breakdown.isEmpty() ? null : breakdown.get(0).scheme(); + + List<String> optimizationTips = buildOptimizationTips(detectedSchemes, breakdown); + + CostProjection projection = new CostProjection( + throughput, + formatCost(hourly), formatCost(daily), formatCost(monthly)); + + CostSummary summary = new CostSummary( + formatCost(totalPerExecution), + breakdown.size(), + detectedSchemes.size(), + mostExpensive, + breakdown.isEmpty() ? "No pay-per-use components detected in this route" : null); + + return new CostEstimateResult( + breakdown.isEmpty() ? null : breakdown, + projection, summary, + optimizationTips.isEmpty() ? null : optimizationTips, + "Cost estimates are approximate based on published AWS pricing (us-east-1). " + + "Actual costs vary by region, volume tier, and model."); + } + + List<String> extractSchemes(String route) { + List<String> schemes = new ArrayList<>(); + Matcher m = SCHEME_PATTERN.matcher(route); + while (m.find()) { + String scheme = m.group(1); + if (!schemes.contains(scheme)) { + schemes.add(scheme); + } + } + return schemes; + } + + private List<String> buildOptimizationTips(List<String> schemes, List<ComponentCostBreakdown> breakdown) { + List<String> tips = new ArrayList<>(); + + if (schemes.contains("aws-bedrock")) { + tips.add("Consider using a smaller/cheaper Bedrock model for simpler tasks " + + "(e.g., Nova Lite instead of Claude for classification)"); + tips.add("Use streaming to reduce perceived latency without affecting token costs"); + } + if (schemes.contains("aws2-textract") && schemes.contains("docling")) { + tips.add("Using both Textract and Docling — consider using only Docling (free, open-source) " + + "for text extraction and reserving Textract for table/form extraction"); + } + if (schemes.contains("aws2-s3")) { + tips.add("S3 GET costs are minimal but add up at scale — consider caching frequently accessed documents"); + } + if (breakdown.size() > 1) { + tips.add("Most expensive component: " + breakdown.get(0).displayName() + + " — focus optimization here for maximum savings"); + } + return tips; + } + + private static String formatCost(double cost) { + if (cost < 0.01) { + return String.format("$%.6f", cost); + } + return String.format("$%.4f", cost); + } + + private static Map<String, ComponentCostProfile> buildCostProfiles() { + Map<String, ComponentCostProfile> profiles = new LinkedHashMap<>(); + + profiles.put("aws-bedrock", new ComponentCostProfile( + "AWS Bedrock (Claude Sonnet 4)", "per-token", + "Input: ~$3/MTok, Output: ~$15/MTok (Claude Sonnet 4 pricing)") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return (inputTokens * 3.0 / 1_000_000) + (outputTokens * 15.0 / 1_000_000); + } + }); + + profiles.put("aws2-textract", new ComponentCostProfile( + "AWS Textract", "per-page", + "DetectDocumentText: ~$1.50/1000 pages, AnalyzeDocument: ~$15/1000 pages") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return pages * 1.50 / 1000; + } + }); + + profiles.put("docling", new ComponentCostProfile( + "Docling (self-hosted)", "free", + "Open-source, self-hosted. Cost is only infrastructure (compute/memory)") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return 0; + } + }); + + profiles.put("aws2-s3", new ComponentCostProfile( + "AWS S3", "per-request", + "GET: ~$0.40/1M requests, PUT: ~$5/1M requests") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return 0.40 / 1_000_000; + } + }); + + profiles.put("aws2-sqs", new ComponentCostProfile( + "AWS SQS", "per-request", + "~$0.40 per 1M requests (Standard)") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return 0.40 / 1_000_000; + } + }); + + profiles.put("aws2-sns", new ComponentCostProfile( + "AWS SNS", "per-publish", + "~$0.50 per 1M publishes") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return 0.50 / 1_000_000; + } + }); + + profiles.put("aws2-kinesis", new ComponentCostProfile( + "AWS Kinesis", "per-shard-hour + per-PUT", + "~$0.015/shard/hour + $0.014/1M PUT units") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return 0.014 / 1_000_000; + } + }); + + profiles.put("langchain4j-chat", new ComponentCostProfile( + "LangChain4j Chat (model-dependent)", "per-token", + "Cost depends on the underlying model provider") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return (inputTokens * 3.0 / 1_000_000) + (outputTokens * 15.0 / 1_000_000); + } + }); + + profiles.put("langchain4j-embeddings", new ComponentCostProfile( + "LangChain4j Embeddings (model-dependent)", "per-token", + "Embedding: ~$0.02/1M tokens (Titan Embed)") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return inputTokens * 0.02 / 1_000_000; + } + }); + + profiles.put("openai", new ComponentCostProfile( + "OpenAI API", "per-token", + "GPT-4o: ~$2.50/MTok input, ~$10/MTok output") { + @Override + double estimateCostPerExecution(int pages, int inputTokens, int outputTokens) { + return (inputTokens * 2.50 / 1_000_000) + (outputTokens * 10.0 / 1_000_000); + } + }); + + return profiles; + } + + // ---- Cost profile base ---- + + abstract static class ComponentCostProfile { + private final String displayName; + private final String pricingModel; + private final String pricingNote; + + ComponentCostProfile(String displayName, String pricingModel, String pricingNote) { + this.displayName = displayName; + this.pricingModel = pricingModel; + this.pricingNote = pricingNote; + } + + String displayName() { + return displayName; + } + + String pricingModel() { + return pricingModel; + } + + String pricingNote() { + return pricingNote; + } + + abstract double estimateCostPerExecution(int pages, int inputTokens, int outputTokens); + } + + // ---- Result records ---- Review Comment: Done — all four result records (CostEstimateResult, ComponentCostBreakdown, CostProjection, CostSummary) are public record, so Jackson/MCP can serialize them across package boundaries.\n\n_Claude Code on behalf of Andrea Cosentino (@oscerd)._ ########## dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/RouteCostEstimateTools.java: ########## @@ -0,0 +1,317 @@ +/* + * 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 SCHEME_PATTERN = Pattern.compile( + "(?:uri:\\s*[\"']?|from:\\s+[\"']?|to:\\s+[\"']?|toD:\\s+[\"']?)([a-zA-Z][a-zA-Z0-9+.-]*):(?://)?", + Pattern.MULTILINE); + + private static final Map<String, ComponentCostProfile> COST_PROFILES = buildCostProfiles(); + + @Tool(annotations = @Tool.Annotations(readOnlyHint = true, destructiveHint = false, openWorldHint = false), + description = "Estimate API costs for a Camel route based on its component usage. " + + "Analyzes a YAML or XML route definition and identifies components with " + + "pay-per-use pricing (Bedrock LLM, Textract, S3, SQS, SNS, etc.). " + + "Returns per-execution cost estimate and monthly projection at a given throughput. " + + "Cost data is approximate based on published AWS pricing.") + public CostEstimateResult camel_route_cost_estimate( + @ToolArg(description = "The Camel route definition (YAML or XML)") String route, + @ToolArg(description = "Expected messages per hour for cost projection (default: 100)") Integer messagesPerHour, + @ToolArg(description = "Average document pages per message for Textract/Docling (default: 5)") Integer avgPages, + @ToolArg(description = "Average LLM input tokens per request (default: 1000)") Integer avgInputTokens, + @ToolArg(description = "Average LLM output tokens per request (default: 500)") Integer avgOutputTokens) { + Review Comment: Done — camel_route_cost_estimate now wraps the logic in try { ... } catch (ToolCallException e) { throw e; } catch (Throwable e) { throw new ToolCallException(...); }, matching the package convention.\n\n_Claude Code on behalf of Andrea Cosentino (@oscerd)._ ########## dsl/camel-jbang/camel-jbang-mcp/src/main/java/org/apache/camel/dsl/jbang/core/commands/mcp/RouteCostEstimateTools.java: ########## @@ -0,0 +1,317 @@ +/* + * 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 SCHEME_PATTERN = Pattern.compile( + "(?:uri:\\s*[\"']?|from:\\s+[\"']?|to:\\s+[\"']?|toD:\\s+[\"']?)([a-zA-Z][a-zA-Z0-9+.-]*):(?://)?", + Pattern.MULTILINE); + Review Comment: Done — there are now separate YAML and XML scheme patterns. XML_SCHEME_PATTERN matches <from|<to|<toD uri="scheme:", and the YAML pattern's from:/to:/toD: alternatives use [ \t]+ (horizontal whitespace) so they no longer span a newline into uri:, while the uri:\s* ["']? alternative captures the real scheme. shouldExtractSchemesFromXmlRoutes covers XML.\n\n_Claude Code on behalf of Andrea Cosentino (@oscerd)._ -- 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]
