-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(mcp-server): Add defensive patches for Transport edge cases #17291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
…ort edge cases This patch adds defensive handling for edge cases in MCP server instrumentation when working with StreamableHTTPServerTransport and similar transport implementations. Changes: - Add transport constructor null checks in getTransportTypes() - Graceful sessionId undefined handling in buildTransportAttributes() - WeakMap correlation fallback system for invalid transport objects - Type validation for WeakMap operations in correlation.ts The patches prevent runtime errors during MCP initialization and session establishment while maintaining backward compatibility and proper instrumentation functionality. Includes comprehensive test coverage for all edge cases and transport scenarios. Fixes edge cases where: - Transport objects have undefined/null constructors - sessionId is undefined during initialization phases - Transport objects cannot be used as WeakMap keys - Invalid transport objects are passed to correlation functions 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
…patterns - Streamline sessionId handling with cleaner undefined checks - Use optional chaining for transport constructor validation - Maintain WeakMap fallback system for invalid transport objects - Align TypeScript patterns with working JavaScript build output These changes ensure compatibility with StreamableHTTPServerTransport while following current Sentry v10.0.0 defensive programming patterns. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
…ation The defensive programming patterns are now the correct baseline implementation, not patches to be applied. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
I've changed a couple of thing, mostly agreed with @codyde . The description in his PR lines 5 items:
These two first items are the main addition in this PR. So I haven't touch them much, just did a small refactor so it was easier to follow. Now about these:
i've remove them as there aren't any code path that could put us in those situation as we have 1.) and 2.) in place. I also added better transport name handling following @codyde addition of using const transportName = transport.constructor.name?.toLowerCase() || 'unknown'; So now we get the name of the transport object used, which is handy when there could be more than one |
packages/core/src/integrations/mcp-server/attributeExtraction.ts
Outdated
Show resolved
Hide resolved
@@ -61,7 +61,7 @@ describe('MCP Server Semantic Conventions', () => { | |||
'mcp.session.id': 'test-session-123', | |||
'client.address': '192.168.1.100', | |||
'client.port': 54321, | |||
'mcp.transport': 'http', | |||
'mcp.transport': 'streamablehttpservertransport', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, should we maybe no lower-case the transport? I suppose this would be easier to read in camel case? 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approved, LGTM - only one idea (but feel free to ignore) left, which is maybe we should not lowercase the transport name? but will happily defer to your perspective on this! nice work!
Problem
The Sentry MCP server instrumentation has several edge cases that can cause runtime errors when working with
StreamableHTTPServerTransport
and similar transport implementations. These issues occur during:sessionId
is undefined during MCP initialization phasesSolution
This PR adds defensive patches to handle these edge cases gracefully while preserving existing functionality and maintaining backward compatibility.
Changes Made
1. Transport Constructor Null Checks (
attributeExtraction.ts
)2. Graceful sessionId Handling (
attributeExtraction.ts
)3. WeakMap Correlation Fallback System (
correlation.ts
)Testing
Added comprehensive test coverage for all edge cases:
All tests pass and verify that the patches handle edge cases without breaking existing functionality.
Compatibility
Impact
This fix prevents runtime errors in production environments when:
The patches ensure reliable instrumentation while maintaining full compatibility with existing MCP server implementations.
Testing Instructions
🤖 Generated with Claude Code