feat: add MCP tool usage metrics system (token cost tracking)#48
Merged
Conversation
Adds infrastructure to automatically measure every MCP tool execution and
store token estimates, byte sizes, and wall-clock timings for observability
and cost-ranking dashboards.
Components added:
- ToolExecutionMetric entity + V26 Flyway migration (tool_execution_metrics table)
- TokenEstimatorService — CL100K_BASE estimation via JTokkit (cl100k_base)
- ToolMetricsCollector — generic measure(toolName, request, Supplier<T>) wrapper;
metrics failures never break business execution (logged only)
- ToolExecutionMetricRepository — JPQL + native queries for top-expensive, usage-by-day,
most-expensive-executions, and avg-execution-time-per-tool
- ToolMetricsController — GET /api/v1/internal/metrics/tools/{top-expensive,usage-by-day,
most-expensive,avg-execution-time}
- Interface projections + MostExpensiveExecutionResponse record in dto/metrics/
- McpApplicationTools fully integrated with ToolMetricsCollector on all 9 methods
Alerts: WARN log emitted when responseTokens > 5 000 (expensive threshold).
https://claude.ai/code/session_01EFuRv2jTHxB7PLXwQTAPbz
Qodana for JVM2 new problems were found
View the detailed Qodana reportTo be able to view the detailed Qodana report, you can either:
To get - name: 'Qodana Scan'
uses: JetBrains/qodana-action@v2025.3.2
with:
upload-result: trueContact Qodana teamContact us at qodana-support@jetbrains.com
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds infrastructure to automatically measure every MCP tool execution and
store token estimates, byte sizes, and wall-clock timings for observability
and cost-ranking dashboards.
Components added:
metrics failures never break business execution (logged only)
most-expensive-executions, and avg-execution-time-per-tool
most-expensive,avg-execution-time}
Alerts: WARN log emitted when responseTokens > 5 000 (expensive threshold).
https://claude.ai/code/session_01EFuRv2jTHxB7PLXwQTAPbz