Skip to content

Add MCP metrics support with counters and histograms - #1837

Merged
jfallows merged 3 commits into
developfrom
claude/busy-goldberg-0fcji
Jun 5, 2026
Merged

jfallows merged 3 commits into
developfrom
claude/busy-goldberg-0fcji

Conversation

@jfallows

@jfallows jfallows commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR adds comprehensive metrics support for the MCP (Model Context Protocol) binding, enabling observability of MCP operations through counters and duration histograms.

Changes

New Modules:

  • runtime/metrics-mcp: Runtime implementation of MCP metrics collection
  • specs/metrics-mcp.spec: Specification and test definitions for MCP metrics

Core Metrics Implementation:

  • McpMetricGroup: Main metric group providing 14 MCP metrics (counters and histograms)
  • McpMetricContext: Handles metric context and stream event processing
  • McpMethod: Enum mapping MCP request types (initialize, tools.list, tools.call, resources.list, resources.read, prompts.list, prompts.get)
  • McpMeasure: Enum for metric measures (COUNT, DURATION)
  • McpAttributeHelper: Extracts metric attributes from MCP stream extensions
  • McpMetric: Individual metric definition wrapper
  • McpMetricGroupFactorySpi: Service provider for metric group factory

Metrics Exposed:

  • mcp.initialize / mcp.initialize.duration
  • mcp.tools.list / mcp.tools.list.duration
  • mcp.tools.call / mcp.tools.call.duration
  • mcp.resources.list / mcp.resources.list.duration
  • mcp.resources.read / mcp.resources.read.duration
  • mcp.prompts.list / mcp.prompts.list.duration
  • mcp.prompts.get / mcp.prompts.get.duration

Supporting Changes:

  • Extended MCP binding spec with McpOutcome enum (OK/ERROR) and McpEndEx structure for outcome reporting
  • Updated McpClientFactory and McpProxyItemFactory to support outcome tracking in stream end extensions
  • Added test specifications for error scenarios (tools.call.is.error)
  • Updated parent pom.xml files to include new modules

Test Coverage

  • McpMetricGroupTest: 425 lines of comprehensive unit tests covering metric resolution, context creation, stream event processing, and attribute extraction
  • McpMethodTest: Tests for MCP method type detection from stream extensions
  • McpMetricGroupFactoryTest: Factory instantiation tests
  • Integration tests in binding specs for error handling scenarios

Integration

Metrics are automatically discovered via MetricGroupFactorySpi and integrated into the Zilla metrics framework. The implementation tracks both success and error outcomes for each MCP operation type.

Fixes #1825

https://claude.ai/code/session_01L1p2VkKKCAZd7zxvGBFueS

claude added 3 commits June 4, 2026 23:10
… duration histograms

Adds a new `mcp` metrics group (specs/metrics-mcp.spec + runtime/metrics-mcp)
that instruments every MCP method observed on an MCP-compatible binding:

  mcp.initialize / .duration
  mcp.tools.list / .duration
  mcp.tools.call / .duration
  mcp.resources.list / .duration
  mcp.resources.read / .duration
  mcp.prompts.list / .duration
  mcp.prompts.get / .duration

Each method has a counter and a latency histogram, so a cache that serves a
*/list method shows up directly as lower list-method latency (no separate
hit/miss metric). The method is discriminated from the McpBeginEx union kind;
tool / resource / prompt / session names are read verbatim from the begin
extension and exposed as opt-in, config-driven attributes (${mcp.tool},
${mcp.resource}, ${mcp.prompt}, ${mcp.session}, ${mcp.method}, ${mcp.outcome}),
following the same attribute model as metrics-http. The group attaches under
any MCP binding's telemetry.metrics and records identically on each (no
binding-specific coupling), so the same metric at two bindings is
distinguishable for diffing.

`outcome` (ok|error) is read from the terminal frame, not a body parse: a
normal END is ok, an ABORT/RESET is error. This keeps a failed tools/call
visible to operators even though it returns HTTP 200.

Deferred to follow-up (per scoping, after #1810 lands McpEndEx):
- refine END outcome via McpEndEx.outcome so a tool-domain error (isError)
  on a protocol-successful call is distinguished from a clean result
- token-claim-valued attributes (needs guard claims exposed to metric handlers)
- engine-level integration tests across OTLP/Prometheus export

https://claude.ai/code/session_01L1p2VkKKCAZd7zxvGBFueS
…p metrics

Adds a frame-native MCP outcome convention so the `mcp` metrics group can read
tool-domain success/failure from the terminal frame instead of a body parse.

specs/binding-mcp.spec:
- mcp.idl: add `McpOutcome { OK, ERROR }` enum and minimal
  `McpEndEx { outcome = OK }` extension (struct extends Extension, following
  the HttpEndEx/WsEndEx convention)
- McpFunctions: add `endEx()` / `matchEndEx()` builders + matchers
- new `tools.call.is.error` scenarios (network + application) asserting the
  reply END carries `McpEndEx.outcome=ERROR`, with NetworkIT/ApplicationIT
  peer tests

runtime/binding-mcp (McpClientFactory):
- detect `isError: true` at the top level of a tools/call result during the
  JSON-RPC result decode (depth-1 key inspection within the existing skip),
  and emit `McpEndEx.outcome=ERROR` on the reply END; the ok path emits a plain
  END (outcome defaults to ok when omitted). JSON-RPC protocol errors continue
  to terminate with ABORT.
- McpClientIT: shouldCallToolIsError

runtime/metrics-mcp:
- McpMetricContext reads `McpEndEx.outcome` from the END frame: END with
  outcome=error -> error, END omitted/ok -> ok, ABORT/RESET -> error. This
  makes a failed tools/call (HTTP 200, isError) record outcome=error.

Only tools/call carries isError; resources/read and prompts/get failures are
JSON-RPC protocol errors (ABORT). Forwarding McpEndEx across proxy/server hops
for multi-binding metric placement is a follow-up.

https://claude.ai/code/session_01L1p2VkKKCAZd7zxvGBFueS
The mcp proxy relayed the upstream reply END without its extension, dropping
any McpEndEx.outcome set by the producing client. McpProxyItemFactory (shared
base for tools/call, resources/read, prompts/get) now reads the incoming END
extension in onClientEnd and forwards it verbatim on the reply END toward its
initiator, via a new extension-bearing doEnd overload.

This lets the `mcp` metrics group observe outcome=error at the proxy and server
bindings (which read the exit-emitted app reply END), not only at the client
that decodes the tool result. The server binding needs no change: its
downstream is HTTP and it consumes the app reply END produced by its exit.

McpProxyIT: shouldCallToolIsError (reuses the app/tools.call.is.error scripts,
which already assert end.ext outcome=ERROR on both sides).

https://claude.ai/code/session_01L1p2VkKKCAZd7zxvGBFueS

@jfallows jfallows left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jfallows
jfallows merged commit f601d56 into develop Jun 5, 2026
38 checks passed
jfallows added a commit that referenced this pull request Jun 6, 2026
…1840)

PR #1837 added the MCP metrics group as a docker-image maven dependency but
omitted it from the zpm image manifest, so the packaged runtime image's jlink
module set did not include io.aklivity.zilla.runtime.metrics.mcp. As a result
mcp.* metric names failed engine schema validation ("must be one of
[grpc/http/stream...]") and MCP metrics could not be enabled in a deployed Zilla.

Add io.aklivity.zilla:metrics-mcp to zpm.json.template (the Dockerfile
regenerates zpm.json from it) so the image links the metrics.mcp module.
Verified: the rebuilt image's `java --list-modules` now includes
io.aklivity.zilla.runtime.metrics.mcp and an mcp.* metrics config validates.

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
@jfallows
jfallows deleted the claude/busy-goldberg-0fcji branch July 1, 2026 23:04
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.

metrics-mcp: instrument all MCP methods initialize, tools/*, resources/*, prompts/*

2 participants