Skip to content

Count code mode calls made through the optimizer's call_tool - #7286

Merged
rdimitrov merged 1 commit into
claude/vmcp-codemode-metrics-ed9fe3from
claude/codemode-calltool-count
Nov 24, 2026
Merged

rdimitrov merged 1 commit into
claude/vmcp-codemode-metrics-ed9fe3from
claude/codemode-calltool-count

Conversation

@rdimitrov

Copy link
Copy Markdown
Member

Stacked on #7275.

Summary

With the vMCP optimizer on, clients reach code mode scripts through the optimizer's call_tool meta-tool rather than calling execute_tool_script directly. The anonymous usage report counted only direct calls, so it could show code_mode_tool_calls with no code_mode_calls.

  • pkg/usagemetrics: NewMiddleware takes options. WithCodeModeCall(match) also counts a tools/call as a code mode call when match accepts it. The direct execute_tool_script check is unchanged.
  • pkg/vmcp/cli: vMCP passes that option only when the optimizer is on. Its matcher accepts a call_tool call whose arguments decode, through optimizer.CallToolInput, to tool_name: execute_tool_script. Names come from optimizerdec.CallToolName and script.ExecuteToolScriptName.
  • docs/middleware.md: code_mode_calls now describes both paths.

Type of change

  • Bug fix

Test plan

  • Unit tests: go test -race on pkg/usagemetrics and pkg/vmcp/cli
  • Linting: golangci-lint v2.15.0 (the CI pin) on both packages, 0 issues

The vMCP test sends real JSON-RPC bodies through mcp.ParsingMiddleware and the usage middleware, then reads code_mode_calls from the final report. Cases: optimizer on with call_tool targeting execute_tool_script (counted, including a differently cased Tool_Name and a tool_name nested in parameters); optimizer on with call_tool targeting another tool or with undecodable arguments (not counted); optimizer off with the same call_tool call (not counted); a direct execute_tool_script call (counted). The pkg/usagemetrics table covers the option itself and a middleware without it, as a plain proxy uses it.

Does this introduce a user-facing change?

Anonymous usage reports from vMCP with the optimizer on now include script calls made through call_tool in code_mode_calls. Before, those calls showed up only in count.

Special notes for reviewers

  • How vMCP is detected: the middleware does not detect it. vMCP builds its own middleware in pkg/vmcp/cli/usage_metrics.go and is the only caller that sets WithCodeModeCall, and only when the optimizer config is set. Plain proxies build theirs through CreateMiddleware, which sets no options, so a backend tool named call_tool there is never treated as code mode. The collector's component (vmcp) was the other candidate, but gating at the composition root also covers the optimizer being off.
  • The matcher decodes into optimizer.CallToolInput with schema.Translate, the same call the authz middleware and dispatch use, so it sees the target they execute. Indexing Arguments["tool_name"] would miss a nested or differently cased key. The decode runs only for call_tool requests.
  • vmcpFeatures' parameter optimizer became optimizerEnabled: usage_metrics.go now imports the optimizer package, and revive flags the shadowing.
  • TestUsageMetrics_CodeModeToolCallsReported now uses the shared reportAfter helper, with the same assertions.

🤖 Generated with Claude Code

With the vMCP optimizer on, clients reach code mode scripts through the
optimizer's call_tool meta-tool, so code_mode_calls missed them and a
report could show code_mode_tool_calls with no code_mode_calls.

The usage middleware gains a WithCodeModeCall option. vMCP sets it when
the optimizer is on, matching call_tool whose decoded target is
execute_tool_script. Plain proxies never set it, since a backend there
may have its own tool named call_tool.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rdimitrov
rdimitrov marked this pull request as ready for review November 24, 2026 21:29
@github-actions github-actions Bot added the size/S Small PR: 100-299 lines changed label Nov 24, 2026
@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.86%. Comparing base (86ab97a) to head (37455da).
⚠️ Report is 1 commits behind head on claude/vmcp-codemode-metrics-ed9fe3.

Files with missing lines Patch % Lines
pkg/vmcp/cli/serve.go 0.00% 1 Missing ⚠️
pkg/vmcp/cli/usage_metrics.go 88.88% 1 Missing ⚠️
Additional details and impacted files
@@                         Coverage Diff                          @@
##           claude/vmcp-codemode-metrics-ed9fe3    #7286   +/-   ##
====================================================================
  Coverage                                81.85%   81.86%           
====================================================================
  Files                                      916      916           
  Lines                                    92886    92901   +15     
====================================================================
+ Hits                                     76031    76050   +19     
+ Misses                                   16850    16846    -4     
  Partials                                     5        5           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rdimitrov
rdimitrov merged commit 5c4f74a into claude/vmcp-codemode-metrics-ed9fe3 Nov 24, 2026
42 checks passed
@rdimitrov
rdimitrov deleted the claude/codemode-calltool-count branch November 24, 2026 21:46
rdimitrov added a commit that referenced this pull request Nov 24, 2026
…and logs (#7275)

* Report code mode script tool calls in usage

Tool calls a code mode script makes run inside vMCP and never reach
the HTTP usage middleware, so the usage report does not see them.
Add an optional OnScript observer to codemode.Config that receives
the number of tool calls each script made, and have the vMCP CLI add
it to the report as code_mode_tool_calls, separate from count.

Part of #4744

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Report pending script tool calls at day end

Code mode script tool calls land when a script returns, so they can be
pending while the day's count is zero: a script that straddled midnight,
or the last script before shutdown. The midnight flush and the shutdown
flush now also fire on pending code_mode_tool_calls, without adding them
to count.

Also align the OnScript doc with ScriptStats ("started", including
failed and denied calls), note the accepted shutdown window for scripts
still running past the HTTP drain, and document that code_mode_calls
misses scripts reached through the optimizer's call_tool.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Add OTel metrics, spans and logs for code mode (#7278)

Operators running vMCP with code mode cannot see how scripts behave:
there is one debug line per script and no metric or span. Add
per-install observability for execute_tool_script.

- Metrics under the vMCP scope: toolhive_vmcp_script_executions,
  _duration, _tool_calls and _parallel_tasks, split by a status
  attribute (success, error, timeout, step_limit), plus a
  toolhive_vmcp_code_mode_enabled gauge on every server.
- A codemode.ExecuteToolScript span per script with a
  codemode.CallTool child per inner call, under parallel() too.
- Debug logs for script start and finish keyed by a script hash and
  session ID; the script body is never logged.

The engine marks step-limit failures with script.ErrStepLimitExceeded,
including inside parallel() children, and reports step count and
parallel() fan-out through an optional Config.OnStats. Script results,
limits and error messages are unchanged.

Part of #4743
Part of #4744

Co-authored-by: Claude Opus 5.5 <[email protected]>

* Count code mode calls made through the optimizer's call_tool (#7286)

Count code mode calls made through call_tool

With the vMCP optimizer on, clients reach code mode scripts through the
optimizer's call_tool meta-tool, so code_mode_calls missed them and a
report could show code_mode_tool_calls with no code_mode_calls.

The usage middleware gains a WithCodeModeCall option. vMCP sets it when
the optimizer is on, matching call_tool whose decoded target is
execute_tool_script. Plain proxies never set it, since a backend there
may have its own tool named call_tool.

Co-authored-by: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Small PR: 100-299 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant