Skip to content

Bound client-controlled tool/prompt name cardinality on per-request metrics #6169

Open

Description

@glageju

mcp.server.operation.duration sets gen_ai.tool.name and gen_ai.prompt.name from parsedMCP.ResourceID, which is params.name verbatim from the client's JSON-RPC body (pkg/telemetry/middleware.go, recordOperationDuration). Neither is validated against the server's resolved tool or prompt set, so a client calling arbitrary names grows this metric's series count without bound.

The same pattern exists in vMCP's optimizer: stacklok.vmcp.optimizer.call_tool.{requests,duration} carry tool_name from input.ToolName (pkg/vmcp/server/sessionmanager/factory.go). The not_found outcome is direct evidence that unresolvable names reach that path and are still recorded.

mcp.method.name and http.request.method were bounded to the semconv _OTHER sentinel in #5956, so this is the remaining unbounded surface on per-request metrics. It matters more after #5956 because mcp.server.operation.duration becomes the sole per-method metric once the legacy twins are retired — there is no unaffected alternative to fall back on.

RFC §3.3 cardinality policy: never use an unbounded value as a label on a per-request metric.

Why it wasn't fixed in #5956

Bounding these requires the resolved tool/prompt set, which the telemetry middleware has no access to — it sees a parsed request, not the backend's capability list. Fixing it means either threading capability state into telemetry or resolving the name before the metric is recorded. That's a design change, not a one-line guard.

Interim mitigation (documented)

Drop the attributes with a Prometheus metric_relabel_config if untrusted clients can reach the proxy. See docs/observability.md → Cardinality warning.

Acceptance

  • No per-request metric carries a client-controlled tool/prompt name unbounded.
  • Unresolvable names record a bounded sentinel, with the raw value available on spans.

Activity

  1. added
    needs-triage
    on Aug 1, 2026
  2. suantea commented on Oct 11, 2026

    @suantea

    I'd like to pick this up. There are two independent surfaces, and one of them can ship without any cross-package wiring, so here's a proposal for the split.

    Surface B — vMCP optimizer (self-contained, no new wiring)

    pkg/vmcp/server/sessionmanager/factory.go:438 sets toolAttr := attribute.String("tool_name", input.ToolName) on toolhive_vmcp_optimizer_call_tool_{requests,errors,not_found,duration}. Unlike the proxy path, the optimizer already owns the resolved/aggregated tool set, so it can bound locally: emit tool_name only when the name is in the optimizer's index, otherwise the semconv _OTHER sentinel, keeping the raw name on the existing span/log. This one has no design question — I can do it on its own, with the _OTHER sentinel matching #5956.

    Surface A — mcp.server.operation.duration (needs a decision)

    HTTPMiddleware.recordOperationDuration (pkg/telemetry/middleware.go:829/833) emits gen_ai.tool.name / gen_ai.prompt.name from parsedMCP.ResourceID (client params.name, verbatim). The middleware sees a parsed request, not the backend's capability list, so bounding needs one of:

    1. Inject a resolver via a new option (e.g. WithResourceNameResolver(interface{ HasResource(method, name string) bool })), wired at the proxyrunner / vMCP composition roots from the same capability source the optimizer uses. Unknown → _OTHER on the metric, raw value still on the span.
    2. Self-contained observation: have the middleware cache the last tools/list / prompts/list name set it observes flowing through (per session) and bound against that. No new wiring, but there is a cold-start window before the first list where every name bounds to _OTHER, and cache lifetime/session-keying becomes its own thing to get right.

    I lean toward option 1 (explicit resolver) because it makes the trust source explicit and avoids a heuristic cache, but it does mean wiring the capability source into the telemetry config at both composition roots.

    Suggested plan

    Ship surface B first (acceptance already satisfied there, no design fork), then agree the shape for surface A. Both keep the raw value on the span, per the acceptance criteria.

    Does that split work for you? If so I'll start with the optimizer counters; and tell me whether you prefer option 1 or 2 (or a name other than _OTHER) for the proxy middleware.

  3. jhrozek commented on Nov 24, 2026

    @jhrozek
    Contributor

    Thanks for the detailed write-up. I checked current main and this is still the case. mcp.server.operation.duration takes gen_ai.tool.name and gen_ai.prompt.name straight from the request's params.name, and the vMCP optimizer does the same with tool_name on its call_tool metrics. The only protection today is a length cap on the label, plus the SDK's 2000 series per instrument limit. That limit stops unbounded growth, but a burst of junk names would use up the budget and merge the real tools into the overflow series.

    Two corrections to the background. #5956 was closed without merging, so the _OTHER bounding for method names never landed and mcp.method.name is still only truncated. Also, #6271 tracks the wider hardening work and treats this issue as its tool/prompt part, so I'm keeping this one open as the tracker.

    @suantea, your split sounds right to me. The optimizer side is self-contained, so go ahead with a PR for that one. For the proxy middleware, injecting a resolver is preferable to caching names from tools/list, since the cache has a cold-start gap. Please hold off on writing that until a maintainer confirms the design.

  4. removed
    needs-triage
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions