You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(mcp-server): Add defensive patches for Transport edge cases (#17291)
## 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:
1. **Transport initialization** - when transport constructors are
undefined/null
2. **Session establishment** - when `sessionId` is undefined during MCP
initialization phases
3. **Span correlation** - when transport objects cannot be used as
WeakMap keys
4. **Type validation** - when invalid transport objects are passed to
correlation functions
## Solution
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`)
```typescript
export function getTransportTypes(transport: MCPTransport): { mcpTransport: string; networkTransport: string } {
// Handle undefined transport gracefully while preserving type detection
if (\!transport || \!transport.constructor) {
return { mcpTransport: 'unknown', networkTransport: 'unknown' };
}
const transportName = transport.constructor.name?.toLowerCase() || '';
// ... rest of function
}
```
#### 2. Graceful sessionId Handling (`attributeExtraction.ts`)
```typescript
export function buildTransportAttributes(
transport: MCPTransport,
extra?: ExtraHandlerData,
): Record<string, string | number> {
// Gracefully handle undefined sessionId during MCP initialization
// Respects client-provided sessions and waits for proper session establishment
const sessionId = transport && 'sessionId' in transport ? transport.sessionId : undefined;
// ... rest of function
}
```
#### 3. WeakMap Correlation Fallback System (`correlation.ts`)
```typescript
const transportToSpanMap = new WeakMap<MCPTransport, Map<RequestId, RequestSpanMapValue>>();
/**
* Fallback span map for invalid transport objects
* @internal Used when transport objects cannot be used as WeakMap keys
*/
const fallbackSpanMap = new Map<RequestId, RequestSpanMapValue>();
function getOrCreateSpanMap(transport: MCPTransport): Map<RequestId, RequestSpanMapValue> {
// Handle invalid transport values for WeakMap while preserving correlation
if (\!transport || typeof transport \!== 'object') {
// Return persistent fallback Map to maintain correlation across calls
return fallbackSpanMap;
}
// ... rest of function
}
```
### Testing
Added comprehensive test coverage for all edge cases:
- **attributeExtraction.test.ts**: Tests undefined/null transports,
constructor edge cases, sessionId handling
- **correlation.test.ts**: Tests WeakMap fallback scenarios, invalid
transport objects, correlation isolation
All tests pass and verify that the patches handle edge cases without
breaking existing functionality.
### Compatibility
- ✅ **Backward compatible** - No breaking changes to existing APIs
- ✅ **Non-invasive** - Only adds defensive checks, doesn't modify core
logic
- ✅ **Performance neutral** - Minimal overhead with early returns for
invalid cases
- ✅ **Type safe** - Maintains existing TypeScript contracts
### Impact
This fix prevents runtime errors in production environments when:
- MCP transports are not fully initialized
- Session establishment is in progress
- Invalid transport objects are passed to instrumentation functions
- Edge case transport implementations are used
The patches ensure reliable instrumentation while maintaining full
compatibility with existing MCP server implementations.
## Testing Instructions
1. The existing test suite continues to pass
2. New edge case tests verify defensive behavior
3. Real-world StreamableHTTPServerTransport usage is now more robust
🤖 Generated with [Claude Code](https://claude.ai/code)
---------
Co-authored-by: Claude <[email protected]>
Co-authored-by: betegon <[email protected]>
0 commit comments