Skip to content

feat: add MCP tool usage metrics system (token cost tracking)#48

Merged
vitorhugo-java merged 1 commit into
mainfrom
claude/mcp-tool-metrics-0TQDq
Jun 6, 2026
Merged

feat: add MCP tool usage metrics system (token cost tracking)#48
vitorhugo-java merged 1 commit into
mainfrom
claude/mcp-tool-metrics-0TQDq

Conversation

@vitorhugo-java
Copy link
Copy Markdown
Owner

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

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
@vitorhugo-java vitorhugo-java merged commit 4dbb93c into main Jun 6, 2026
2 of 3 checks passed
@vitorhugo-java vitorhugo-java deleted the claude/mcp-tool-metrics-0TQDq branch June 6, 2026 04:21
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 6, 2026

Qodana for JVM

2 new problems were found

Inspection name Severity Problems
Nullability and data flow problems 🔶 Warning 1
Mismatched query and update of 'StringBuilder' 🔶 Warning 1
View the detailed Qodana report

To be able to view the detailed Qodana report, you can either:

To get *.log files or any other Qodana artifacts, run the action with upload-result option set to true,
so that the action will upload the files as the job artifacts:

      - name: 'Qodana Scan'
        uses: JetBrains/qodana-action@v2025.3.2
        with:
          upload-result: true
Contact Qodana team

Contact us at qodana-support@jetbrains.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants