stacklok / stacklok/toolhive

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

Open
#6,169 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage
Dominant language
Go
Stars
2.2k
Forks
300
Avg merge
1d 15h
Merged PRs (30d)
184

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read pkg/telemetry/middleware.go, especially recordOperationDuration, and trace how parsedMCP.ResourceID reaches metric attributes. Then inspect pkg/vmcp/server/sessionmanager/factory.go for the optimizer's tool_name path and review the documented cardinality policy. Done means every per-request metric uses a bounded value for unresolvable tool or prompt names while raw values remain available on spans.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
observability
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.