Skip to content

Trace McpServer requests with OpenTelemetry MCP semantic conventions - #10304

Merged
tim-smart merged 17 commits into
mainfrom
agent/nelson-effect-bot/1382008f96f9
Nov 3, 2026
Merged

tim-smart merged 17 commits into
mainfrom
agent/nelson-effect-bot/1382008f96f9

Conversation

@effect-bot

@effect-bot effect-bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

McpServer now traces each request with a server span that follows the OpenTelemetry MCP semantic conventions. By default, tool arguments are no longer recorded.

Spans

RpcServer.make gets a new onRequest option. It wraps the effect that runs each request's middleware and handler, along with the response write when the handler returns its result directly, and receives the decoded request. McpServer passes disableTracing: true to its inner RpcServer and uses onRequest to run each request inside its own MCP span. The span therefore follows RpcServer's request lifecycle, and spans for other RPC users don't change.

  • Name: {method} {target}. The target is the tool name for tools/call and the prompt name for prompts/get. Every other method uses the bare method name; for example, resources/read never includes the URI.
  • Attributes:
    • mcp.method.name
    • jsonrpc.request.id (requests only)
    • mcp.protocol.version
    • mcp.session.id (HTTP sessions)
    • network.transport (tcp for HTTP, pipe for stdio)
    • gen_ai.operation.name: execute_tool, gen_ai.tool.name, gen_ai.prompt.name
    • mcp.resource.uri
  • Parent: a valid W3C traceparent in params._meta becomes the parent, and the transport span (for example the HTTP server span) is added as a link. Without it, the span nests under the transport span as before. Only traceparent is read from _meta. tracestate and baggage are not used, because Tracer.ExternalSpan cannot store them.
  • Errors:
    • A tool result with isError: true ends OK with error.type: tool_error.
    • JSON-RPC errors set rpc.status_code.
    • Errors caused by the request (-32700, -32600, -32601, -32602, -32002) keep the span OK.
    • Any other error code, including defects sent as -32603, fails the span and sets error.type to the code.
    • Cancellation, a client disconnect, server shutdown or a failed response write ends the span as interrupted. If an internal error (-32603) is sent in response, for example after a failed write, the span still records rpc.status_code and error.type.

A span ends after its response is written. Requests that McpServer rejects before they reach RpcServer get no span: these are unknown methods, payloads that fail to decode and JSON-RPC batches. Client notifications that McpServer handles itself, such as notifications/initialized and notifications/cancelled, get no span either.

Toolkit privacy

Toolkit.handle used to annotate the current span with tool and the raw parameters, which could include user data. It now records gen_ai.tool.name and, when a call id is present, gen_ai.tool.call.id. Argument capture is opt-in through a new Toolkit.RecordToolArguments reference, which defaults to false. When it is true, arguments are recorded as JSON in gen_ai.tool.call.arguments. McpServer reads the same reference for tools/call, so tools registered with McpServer.addTool, which don't go through Toolkit, also follow it.

Tests

  • McpServer.test.ts:
    • span names and attributes
    • _meta trace propagation
    • error classification
    • argument capture when opted in, for Toolkit tools and for tools registered with addTool
    • span lifecycle on transport disconnect, graceful EOF and a failed response write
  • LanguageModel.test.ts: tool-call annotations, with and without argument capture

Closes EFF-3295
Closes #10293

Covers OpenTelemetry MCP span names/attributes, _meta trace context extraction, error classification, and Toolkit no longer recording raw tool arguments.
McpServer now owns its server spans instead of RpcServer: spans are named after the MCP method and target, carry mcp.*/gen_ai.* attributes, continue a W3C traceparent from params._meta, and classify tool and JSON-RPC errors. Toolkit.handle stops recording raw tool arguments unless Toolkit.RecordToolArguments is enabled.
@changeset-bot

changeset-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f3f4285

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 33 packages
Name Type
effect Patch
@effect/opentelemetry Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-cloudflare Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/ai-typesafe Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/graphql-generator Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@effect-janitor effect-janitor Bot added the 4.0 label Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 39.41 KB 39.41 KB 0.00 KB (0.00%)
basic.ts 7.23 KB 7.23 KB 0.00 KB (0.00%)
batching.ts 11.08 KB 11.08 KB 0.00 KB (0.00%)
brand.ts 6.87 KB 6.87 KB 0.00 KB (0.00%)
cache.ts 11.54 KB 11.54 KB 0.00 KB (0.00%)
config.ts 22.84 KB 22.84 KB 0.00 KB (0.00%)
differ.ts 22.00 KB 22.00 KB 0.00 KB (0.00%)
http-client.ts 26.93 KB 26.93 KB 0.00 KB (0.00%)
http-router.ts 37.00 KB 37.00 KB 0.00 KB (0.00%)
logger.ts 12.01 KB 12.01 KB 0.00 KB (0.00%)
metric.ts 9.47 KB 9.47 KB 0.00 KB (0.00%)
optic.ts 7.10 KB 7.10 KB 0.00 KB (0.00%)
pubsub.ts 16.02 KB 16.02 KB 0.00 KB (0.00%)
queue.ts 12.82 KB 12.82 KB 0.00 KB (0.00%)
schedule.ts 11.80 KB 11.80 KB 0.00 KB (0.00%)
schema-bigdecimal.ts 14.14 KB 14.14 KB 0.00 KB (0.00%)
schema-binary.ts 41.67 KB 41.67 KB 0.00 KB (0.00%)
schema-class.ts 21.73 KB 21.73 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 29.60 KB 29.60 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 27.99 KB 27.99 KB 0.00 KB (0.00%)
schema-string-transformation.ts 15.35 KB 15.35 KB 0.00 KB (0.00%)
schema-string.ts 12.34 KB 12.34 KB 0.00 KB (0.00%)
schema-template-literal.ts 16.27 KB 16.27 KB 0.00 KB (0.00%)
schema-toArbitrary.ts 38.95 KB 38.95 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 26.15 KB 26.15 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 20.90 KB 20.90 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 21.05 KB 21.05 KB 0.00 KB (0.00%)
schema-toFormatter.ts 21.14 KB 21.14 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 26.17 KB 26.17 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 21.23 KB 21.23 KB 0.00 KB (0.00%)
schema.ts 20.82 KB 20.82 KB 0.00 KB (0.00%)
stm.ts 14.20 KB 14.20 KB 0.00 KB (0.00%)
stream.ts 10.38 KB 10.38 KB 0.00 KB (0.00%)

…ment capture

Failing regressions for spans left open on transport disconnect, ended early on graceful EOF, and ended before a failed response write is recovered as -32603.
Spans now end after their response is written, so a failed write is recorded as the -32603 sent in its place. A graceful EOF no longer interrupts pending spans; transport disconnects end them as they are forwarded to RpcServer, and server shutdown ends any still open.
Share the tool argument formatter between Toolkit and McpServer, hoist the
interrupted exit used for abandoned request spans, and drop the non-null
assertions in finishRequestSpan.
RpcServer.make now accepts onRequest, which receives the decoded request. McpServer uses it to wrap each request's effect in its MCP span, so the span follows RpcServer's request lifecycle (response write, cancellation, disconnect and shutdown) instead of a separate span table, disconnect forwarding and shutdown finalizer.
The -32603 sent for a defect, a self-interruption or a failed response write is now recorded as rpc.status_code and error.type on the request's MCP span, found through a private reference so the HTTP span is never annotated. Narrow RpcServer.OnRequest docs: a Deferred reply is sent outside the wrapped effect.
When McpServer answers a request with an internal error after its original response write fails, the span keeps the recorded -32603 attributes and ends with the handler fiber's failure instead of being reclassified from the handler's own error.
Comment thread packages/effect/src/rpc/RpcServer.ts Outdated
* @category models
* @since 4.1.3
*/
export type OnRequest<A extends Rpc.Any> = <X, E, R>(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Keep the types inline

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.

Inlined in ef3f6cb: the OnRequest alias is gone, and its lifecycle notes now live in the RpcServer.make docs.

@tim-smart
tim-smart enabled auto-merge (squash) November 3, 2026 00:35
@tim-smart
tim-smart merged commit 0d0c824 into main Nov 3, 2026
17 checks passed
@tim-smart
tim-smart deleted the agent/nelson-effect-bot/1382008f96f9 branch November 3, 2026 00:43
@effect-bot effect-bot mentioned this pull request Nov 3, 2026
laithalsaadoon added a commit to memhtml/memhtml that referenced this pull request Nov 22, 2026
## What moves

- The Effect catalog set goes from 4.1.2 to 4.1.4 as one version: `effect`, `@effect/platform-node`, `@effect/platform-node-shared`, `@effect/vitest`.
- The lockfile follows: the four Effect packages, plus `undici` 8.12.3 -> 8.12.4 under `@effect/platform-node`, which asks for `^8.12.4`. Nothing else in the lockfile changes; `ws` stays at 8.22.1, and the docs site's own `undici` 8.12.3 (under `unifont`) stays where it is.
- No source change was needed. The only source edits are comments and prose that name the pin.

## Behavior changes memhtml reaches

Effect 4.1.3 changed behavior that memhtml reaches, and 4.1.4 keeps all but one of those changes. A user or operator moving from 4.1.2 to 4.1.4 can observe five things:

1. **`memhtml-mcp` exits with code 0 instead of 130 when its client closes stdin.** Anything that spawns the server and reads its exit code sees the new value. ([#10475](Effect-TS/effect#10475))
2. **A list request with a cursor the server never issued is refused.** `tools/list`, `resources/list`, `resources/templates/list` and `prompts/list` now answer `-32602 "Unknown cursor"`; before, they ignored the cursor and returned the full list. memhtml never issues a `nextCursor`, so a conforming client never sends one. ([#10447](Effect-TS/effect#10447))
3. **MCP request spans are renamed, only when `OTEL_EXPORTER_OTLP_ENDPOINT` is set.** A span such as `McpServer.@effect/mcp/2025-06-18/tools/call` becomes `tools/call memory_search`, and its attributes move from `rpc.*`, `tool` and `parameters` to `mcp.*`, `gen_ai.*`, `jsonrpc.request.id` and `network.transport`. Tool arguments are no longer exported on the span. Dashboards or queries keyed on the old names need updating. ([#10304](Effect-TS/effect#10304))
4. **Successful spans carry status Unset instead of Ok, under the same condition.** This applies to every span the MCP server and the CLI export. ([#10440](Effect-TS/effect#10440))
5. **`memhtml head serve` stops at once after SIGTERM when a local client had sent an HTTP `Upgrade` request.** Before, it waited the full two-second shutdown grace. memhtml's own client never sends `Upgrade`, so this needs a foreign client on the socket. Replies are unchanged. ([#10360](Effect-TS/effect#10360))

One 4.1.3 change does not carry over: 4.1.3 answered three malformed Skills requests with `-32601` instead of `-32602`, and 4.1.4 restores `-32602` ([#10540](Effect-TS/effect#10540)). Those replies are identical on 4.1.2 and 4.1.4.

None of the other 4.1.4 entries is reached: they cover `HttpApi`, `effect/cli`, the AI provider clients, `SchemaRepresentation` and `TestClock` internals.

## Proofs

- **Typecheck:** `turbo run typecheck` passes, 35 of 35 tasks, 0 errors (tsc 7.0.2).
- **Emit comparison:** every project was rebuilt with `tsc -b --force` and compared with a build of `main`: 418 `.js` and `.d.ts` files, 414 byte-identical. Three of the other four (`apps/mcp/dist/server.js`, `tools.js`, `server.d.ts`) differ only in the restated comment lines. The fourth, `apps/mcp/dist/tools.d.ts`, gains a third type argument (`Tool.Execution`) on the 15 tool types; that file is not part of the published bundle. No executable line differs.
- **Tests:** the `apps/mcp` suite passes 163 of 163. `apps/cli` `head-server.test.ts` passes 26 of 27 in a run with a long temp directory; the one failure is the socket-path-limit case, whose own precondition failed by one byte before any server code ran, and the file passes in the full gate. `catalog.test.ts` passes 4 of 4, `packages/eval` `corpus.test.ts` 26 of 26, `packages/llm` `structured.test.ts` 45 of 45. The full local gate (`mise run check`) passes 76 of 76 tasks on this head.
- **MCP stdio harness**, the `main` build against this branch's build, same fixture store:
  - 50 requests: 46 replies identical, 4 differ (the four cursor requests in item 2).
  - `tools/list` is identical: 41,197 bytes, same sha256.
  - Requests sent before `initialize` (9) and requests with bad header fields (8) are all identical.
  - All seven Skills requests are identical.
  - Exit code at stdin close: 130 before, 0 after.
- **Telemetry probe** against a local collector: 49 MCP spans and 22 CLI spans on both builds, with the name, attribute and status differences in items 3 and 4. Batch timing is unchanged.
- **Head server probe:** 17 replies equal on both builds apart from a duration, a pid and the store path. Stop time after an `Upgrade` request: 2,027 ms before, 27 ms after.
- **Advisories:** none affects Effect 4.1.2, 4.1.3 or 4.1.4, or undici 8.12.3 or 8.12.4. 4.1.4 is the newest release of all four packages.

## Restated sentences

- `apps/mcp/src/server.ts`: two comments now name effect 4.1.4 (the `instructions` option, the five protocol adapters).
- `apps/mcp/src/tools.ts`: one comment now names effect 4.1.4.
- `docs/backlog.md`: the `instructions` entry names the 4.1.4 pin and adds the 2026-11-16 probe.
- `CLAUDE.md`: the adapters sentence names 4.1.4. The "differs from recall" paragraph is reworded, not just renumbered: `Schema.d.ts` changed in 4.1.3 (it adds `Rational` and `RationalFromString` and changes the `isAnyOf` and `Iso` types), so the paragraph now says the claims it names were checked again against the installed 4.1.4, where `Effect.d.ts` is byte-identical to 4.1.2's.

## Merge

This PR is left open for the maintainer. It compiles and the emitted code is unchanged, but it changes an exit code, one MCP error path and the exported telemetry, and those are a maintainer's call.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

McpServer: follow the OpenTelemetry MCP semantic conventions for spans

2 participants