Repository navigation
Add MCP metrics support with counters and histograms - #1837
Merged
Merged
Conversation
… 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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 collectionspecs/metrics-mcp.spec: Specification and test definitions for MCP metricsCore Metrics Implementation:
McpMetricGroup: Main metric group providing 14 MCP metrics (counters and histograms)McpMetricContext: Handles metric context and stream event processingMcpMethod: 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 extensionsMcpMetric: Individual metric definition wrapperMcpMetricGroupFactorySpi: Service provider for metric group factoryMetrics Exposed:
mcp.initialize/mcp.initialize.durationmcp.tools.list/mcp.tools.list.durationmcp.tools.call/mcp.tools.call.durationmcp.resources.list/mcp.resources.list.durationmcp.resources.read/mcp.resources.read.durationmcp.prompts.list/mcp.prompts.list.durationmcp.prompts.get/mcp.prompts.get.durationSupporting Changes:
McpOutcomeenum (OK/ERROR) andMcpEndExstructure for outcome reportingMcpClientFactoryandMcpProxyItemFactoryto support outcome tracking in stream end extensionsTest Coverage
McpMetricGroupTest: 425 lines of comprehensive unit tests covering metric resolution, context creation, stream event processing, and attribute extractionMcpMethodTest: Tests for MCP method type detection from stream extensionsMcpMetricGroupFactoryTest: Factory instantiation testsIntegration
Metrics are automatically discovered via
MetricGroupFactorySpiand 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