Skip to content

Comments

fix(core): use sessionId for MCP transport correlation#19172

Merged
betegon merged 16 commits intodevelopfrom
bete/fix/mcp-wrapper-transport-correlation
Feb 10, 2026
Merged

fix(core): use sessionId for MCP transport correlation#19172
betegon merged 16 commits intodevelopfrom
bete/fix/mcp-wrapper-transport-correlation

Conversation

@betegon
Copy link
Member

@betegon betegon commented Feb 4, 2026

Summary

Fixes MCP server instrumentation not recording events for wrapper transport patterns (like NodeStreamableHTTPServerTransport which wraps WebStandardStreamableHTTPServerTransport).

The root cause: wrapper transports proxy onmessage/send via getters/setters, causing different this values. The previous WeakMap<transport, ...> correlation couldn't match requests to responses. This changes to Map<sessionId, ...> correlation which works regardless of transport object reference.

Changes

  • Changed correlation.ts to use sessionId-based Maps instead of transport-based WeakMaps
  • Changed sessionManagement.ts to use sessionId-based session lookup
  • Added unit tests with mock wrapper transport utility
  • Added E2E tests for StreamableHTTP transport across node-express, node-express-v5, and tsx-express

Test Plan

  • Unit tests: cd packages/core && yarn test (2010 tests pass)
  • E2E tests: All pass
    • node-express: 14/14
    • node-express-v5: 12/12
    • tsx-express: 10/10

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).
  • Link an issue if there is one related to your pull request. If no issue is linked, one will be auto-generated and linked.

Closes #19233

Thanks @gotenxds for reporting and debugging this issue!

…n spans

This introduces `recordInputs` and `recordOutputs` options to the MCP server wrapper and related functions, allowing for more granular control over the data captured in spans.
Wrapper transports (like NodeStreamableHTTPServerTransport wrapping
WebStandardStreamableHTTPServerTransport) proxy onmessage/send via
getters/setters, causing different 'this' values in each callback.

This changes correlation from WeakMap<transport> to Map<sessionId>
to correctly correlate requests with responses regardless of which
transport object reference is used.

Reported-by: @gotenxds
Add mock wrapper transport utility and tests verifying that
instrumentation correctly handles transports that proxy onmessage/send
via getters/setters (common pattern with StreamableHTTP transports).
Add E2E tests for MCP StreamableHTTP transport across node-express,
node-express-v5, and tsx-express test applications. Tests verify that
transactions are recorded correctly for the wrapper transport pattern
used by NodeStreamableHTTPServerTransport.
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

Codecov Results 📊


Generated by Codecov Action

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

Codecov Results 📊

23 passed | ⏭️ 7 skipped | Total: 30 | Pass Rate: 76.67% | Execution Time: 10.04s

All tests are passing successfully.


Generated by Codecov Action

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 9,142 - 8,968 +2%
GET With Sentry 1,743 19% 1,736 +0%
GET With Sentry (error only) 6,178 68% 6,029 +2%
POST Baseline 1,201 - 1,202 -0%
POST With Sentry 581 48% 585 -1%
POST With Sentry (error only) 1,035 86% 1,062 -3%
MYSQL Baseline 3,308 - 3,277 +1%
MYSQL With Sentry 462 14% 500 -8%
MYSQL With Sentry (error only) 2,707 82% 2,659 +2%

View base workflow run

@betegon betegon marked this pull request as ready for review February 9, 2026 13:00
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

* different transport objects share the same logical session
*/
const transportToSpanMap = new WeakMap<MCPTransport, Map<RequestId, RequestSpanMapValue>>();
const sessionToSpanMap = new Map<string, Map<RequestId, RequestSpanMapValue>>();
Copy link

Choose a reason for hiding this comment

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

Module-level Maps leak memory without transport close

Medium Severity

sessionToSpanMap and sessionToSessionData are module-level Map<string, ...> instances whose entries are only removed via explicit delete calls in cleanupPendingSpansForTransport / cleanupSessionDataForTransport, which are solely triggered by the transport's onclose handler. If a transport is abandoned without onclose firing (e.g., unclean disconnect), entries accumulate indefinitely. The previous WeakMap<MCPTransport, ...> approach provided automatic GC-based cleanup as a safety net, which this change loses for stateful transports.

Additional Locations (1)

Fix in Cursor Fix in Web

Copy link
Member Author

Choose a reason for hiding this comment

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

acknowledged, intentional.

So this would happen if somehow the mcp server skips onClose when closing a connection, and it shouldn't happen.

  1. the only way to skip onclose is a process crash (where memory is freed anyway) or a genuine bug in user code where they abandon a transport without closing it
  2. the MCP server.close() calls close() on all connected transports, which triggers onclose
  3. HTTP connection drops trigger onclose in the transport implementation
  4. SSE disconnects trigger onclose

This is a normal flow as per MCP docs:

1. Transport created (sessionId assigned)
2. server.connect(transport)  →  we wrap onmessage/send/onclose
3. Messages flow:
   - onmessage: stores span in sessionToSpanMap[sessionId][requestId]
   - send:      looks up span in sessionToSpanMap[sessionId][requestId], ends it, deletes entry
4. Transport closes:
   - onclose fires
   - cleanupPendingSpansForTransport():  ends any orphan spans, calls sessionToSpanMap.delete(sessionId)
   - cleanupSessionDataForTransport():   calls sessionToSessionData.delete(sessionId)
5. Maps are now clean for that sessionId

So everything is cleaned up.

@betegon betegon requested a review from JPeer264 February 9, 2026 13:21
@betegon betegon self-assigned this Feb 9, 2026
Copy link
Member

@JPeer264 JPeer264 left a comment

Choose a reason for hiding this comment

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

LGTM.

@betegon betegon merged commit 389ab1f into develop Feb 10, 2026
219 checks passed
@betegon betegon deleted the bete/fix/mcp-wrapper-transport-correlation branch February 10, 2026 12:36
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.

MCP server aint recording events - req/res transport issue

2 participants