Skip to content

Log backend tool errors at Debug and count them - #7300

Merged
ChrisJBurns merged 1 commit into
stacklok:mainfrom
alex-feel:vmcp-tool-error-text-debug
Nov 25, 2026
Merged

ChrisJBurns merged 1 commit into
stacklok:mainfrom
alex-feel:vmcp-tool-error-text-debug

Conversation

@alex-feel

Copy link
Copy Markdown
Contributor

Summary

A tool's error text often repeats the call's input: a query the backend could not parse, an ID it could not find, a file name. When a backend answers tools/call with isError: true, the vMCP backend client logs that text at WARN, so a failed call can write the caller's input into the vMCP logs at the default level, and operators cannot drop it because logLevel accepts only debug (pkg/vmcp/config/config.go:726). A caller can trigger the line on every call, so docs/logging.md:22 puts it at Debug with a counter where operators need a signal, as code mode does for a failed script with toolhive_vmcp_script_executions{status} (#7275).

  • toolResultFromMCP logs tool returned IsError=true at Debug, with the same message and fields.
  • The backend telemetry decorator counts these results in a new counter, toolhive_vmcp_backend_tool_errors.
  • The result the client receives is unchanged.
  • docs/operator/virtualmcpserver-observability.md documents the counter and the log line, and says that composite tool workflows still log the text. It and docs/telemetry-migration-guide.md say that mcp.client.operation.duration gets no error.type for an isError result.

Closes #7297

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

TestToolResultFromMCP_LogsIsErrorOnlyAtDebug covers a result with and without _meta and fails with main's slog.Warn put back. TestTelemetryBackendClient_CallToolRecordsToolErrors covers one and two isError results, a successful result, and a Go error, and checks each case's mcp.client.operation.duration point, where only the Go error sets error.type. It fails when the counter is skipped, doubled, or carries other attributes than the request counter, and when the duration point of an isError result gains error.type or loses its attributes.

Does this introduce a user-facing change?

Yes, for operators.

  • Before: an isError result logs WARN tool returned IsError=true with tool, backend, error (the first content block's text), and meta when the backend sent _meta. Outside the optimizer, no vMCP metric reads the flag.
  • After: the same line at DEBUG, so without logLevel: debug (--debug) the backend client logs nothing for them, and a deployment without telemetry no longer sees these failures at the default level. With telemetry configured, toolhive_vmcp_backend_tool_errors (_total in Prometheus) counts them per backend and tool. toolhive_vmcp_backend_errors is unchanged.

Special notes for reviewers

  • Metric design: a new counter in backendtelemetry, recorded with the attributes toolhive_vmcp_backend_requests gets for the same call, as the errors counter and duration histogram are (pkg/vmcp/internal/backendtelemetry/backendtelemetry.go:238, :242, :254), so it adds no new label values. The decorator wraps the backend client once, in core.New (pkg/vmcp/core/core_vmcp.go:122), above both the Legacy and the Modern path, so each result counts once, composite workflow steps and code mode's inner calls included. It is a separate counter, not an attribute on toolhive_vmcp_backend_errors, so existing series and alerts keep their meaning; the optimizer counts result.IsError the same way (pkg/vmcp/server/sessionmanager/factory.go:362-365, :484).
  • error.type: the OTel MCP semantic conventions put error.type=tool_error on mcp.client.operation.duration and the client span for an isError result. This PR sets neither, and the decorator's span status stays unset where code mode marks its inner span failed (pkg/vmcp/codemode/decorator.go:407). Setting them would change what an existing metric and existing traces report for these calls, which is a different change; OTEL: Add error.type and rpc.response.status_code for JSON-RPC errors #3765 tracks the same gap for pkg/telemetry/middleware.go. As recorded here, the histogram has no backend or tool attribute (backendtelemetry.go:232-235), so it could not say which tool failed. The docs now say that error.type is not set for an isError result.
  • record also returns the call's metric attributes: the revision label is read before the call, and a call can reclassify it.
  • The session backend's CallTool (pkg/vmcp/session/internal/backend/mcp_session.go:364) is left alone because no tools/call reaches it: server.New routes every call through the core (pkg/vmcp/server/server.go:590-593), and the one handler that calls a session's CallTool is installed only in a branch sessionmanager documents as unreachable (pkg/vmcp/server/sessionmanager/factory.go:182-186).
  • Left out: composite tool workflows. A failed step logs the tool's error text at WARN on each attempt (pkg/vmcp/composer/workflow_engine.go:518-519) and wraps it into the step error (:520). The executor, the engine, and the workflow's caller each log that error at ERROR (pkg/vmcp/composer/dag_executor.go:138, workflow_engine.go:203, and for example pkg/vmcp/core/core_calls.go:297), and the client gets it as the workflow's failure message. Taking the text out of those lines means separating the logged error from the returned one, which is a different change.
  • toolResultFromMCP takes the logger as a parameter. The test passes its own logger and never swaps the process-wide default, for the reason cedarAdmission gives for its injected logger (pkg/vmcp/core/admission.go:121-126).

When a backend answers tools/call with isError, the vMCP backend client logged a WARN line carrying the text of the result's first content block.
A caller can trigger that line on every call, and a tool's error text often repeats the call's arguments, such as a query the backend could not parse, an identifier, or a file name, so a failed call could write the caller's input into the vMCP logs at the default level.
The vMCP log level setting can only switch debug on, so operators had no way to keep that text out.
The line now logs at Debug with the same message and fields, as code mode does for a failed script.
The backend telemetry decorator counts these results in a new toolhive_vmcp_backend_tool_errors counter with the attributes of toolhive_vmcp_backend_requests, once per call on both the Legacy and the Modern path, because toolhive_vmcp_backend_errors counts only calls that return a Go error.
The client still receives the full result.
toolResultFromMCP takes its logger as a parameter, so the test asserts on the line without swapping the process-wide slog default.
Composite tool workflows still log a failed step's error text in their own lines, and this change leaves them as they are.

Signed-off-by: Aleksandr Filippov <[email protected]>
@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.00%. Comparing base (1082992) to head (6d1579e).

Files with missing lines Patch % Lines
...vmcp/internal/backendtelemetry/backendtelemetry.go 68.75% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7300      +/-   ##
==========================================
+ Coverage   81.95%   82.00%   +0.04%     
==========================================
  Files         922      922              
  Lines       93367    93376       +9     
==========================================
+ Hits        76521    76575      +54     
+ Misses      16841    16796      -45     
  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.

@ChrisJBurns
ChrisJBurns merged commit 8f29572 into stacklok:main Nov 25, 2026
43 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 25, 2026
2 tasks
@alex-feel
alex-feel deleted the vmcp-tool-error-text-debug branch November 27, 2026 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vMCP logs a backend tool's error text at WARN

2 participants