Skip to content

Tool calls produce no span and the kubectl subprocess drops trace context #102

Open

Description

@braghettos

Summary

With tracing enabled, an MCP tool call to kagent-tools doesn't show up in the caller's trace. The server extracts the incoming W3C traceparent, but it never starts a span for the request or the tool call. The k8s tools then run kubectl as a subprocess, which receives no trace context, so the Kubernetes API requests behind the call can't join the trace either.

We see this as an agent turn whose trace ends at the gateway's MCP hop: the tool call itself, and everything it did against the API server, is invisible.

Where (v0.3.1)

  • cmd/main.go: the HTTP handler is wrapped only in telemetry.HTTPMiddleware. There's no otelhttp handler and no other server span.
  • internal/telemetry/middleware.go HTTPMiddleware: it calls propagator.Extract(...) and stores the trace and span IDs in the context, but starts no span. Its own comment says ExtractTraceInfo has no non-test callers since the Go SDK migration (Migrate to GO SDK #66).
  • pkg/k8s/k8s.go runKubectlCommand / runKubectlCommandWithTimeout: these run kubectl through commands.NewCommandBuilder("kubectl"). The child process gets no traceparent, and kubectl has no way to send one.

The only spans in a tool call's context are the cache.* spans.

Expected

  • A server span per MCP request (or per tools/call), a child of the incoming traceparent, named for the tool (for example tools/call k8s_get_resources), with the tool name and an error status on failure.
  • Ideally, the Kubernetes API requests the tool makes are children of that span, so API-server tracing (APIServerTracing) links up with the agent's trace.

Possible fixes

  1. Wrap the MCP handler in otelhttp.NewHandler (or start a span in HTTPMiddleware), and start a child span per tool call in the handlers. That alone makes tool calls visible in the caller's trace.
  2. For the Kubernetes requests: use client-go in-process for the read verbs, with an otelhttp transport, instead of a kubectl subprocess. Then traceparent reaches the API server. A subprocess can't carry it, because kubectl doesn't read trace context from its environment or flags.

Happy to help test a change.

Activity

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