Commit 4abfe01
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 <noreply@anthropic.com>
Co-authored-by: betegon <miguelbetegongarcia@gmail.com>1 parent 377c7fc commit 4abfe01
File tree
4 files changed
+120
-40
lines changed- packages/core
- src/integrations/mcp-server
- test/lib/integrations/mcp-server
4 files changed
+120
-40
lines changedLines changed: 10 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
86 | 86 | | |
87 | 87 | | |
88 | 88 | | |
89 | | - | |
90 | 89 | | |
91 | | - | |
| 90 | + | |
92 | 91 | | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
105 | 101 | | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | 102 | | |
Lines changed: 16 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
| 155 | + | |
| 156 | + | |
163 | 157 | | |
164 | | - | |
165 | | - | |
166 | | - | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
167 | 166 | | |
168 | 167 | | |
169 | | - | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
170 | 172 | | |
171 | 173 | | |
172 | 174 | | |
173 | 175 | | |
174 | 176 | | |
175 | 177 | | |
176 | 178 | | |
| 179 | + | |
177 | 180 | | |
178 | 181 | | |
179 | 182 | | |
180 | 183 | | |
181 | 184 | | |
182 | | - | |
| 185 | + | |
183 | 186 | | |
184 | 187 | | |
185 | 188 | | |
| |||
Lines changed: 6 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
64 | | - | |
| 64 | + | |
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| |||
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
96 | | - | |
| 96 | + | |
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
| 128 | + | |
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
| 157 | + | |
158 | 158 | | |
159 | 159 | | |
160 | 160 | | |
| |||
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
196 | | - | |
| 196 | + | |
197 | 197 | | |
198 | 198 | | |
199 | 199 | | |
| |||
227 | 227 | | |
228 | 228 | | |
229 | 229 | | |
230 | | - | |
| 230 | + | |
231 | 231 | | |
232 | 232 | | |
233 | 233 | | |
| |||
Lines changed: 88 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
214 | 216 | | |
215 | 217 | | |
216 | 218 | | |
217 | | - | |
| 219 | + | |
218 | 220 | | |
219 | 221 | | |
220 | 222 | | |
| |||
245 | 247 | | |
246 | 248 | | |
247 | 249 | | |
248 | | - | |
| 250 | + | |
249 | 251 | | |
250 | 252 | | |
251 | 253 | | |
| |||
286 | 288 | | |
287 | 289 | | |
288 | 290 | | |
289 | | - | |
| 291 | + | |
290 | 292 | | |
291 | 293 | | |
292 | 294 | | |
| |||
361 | 363 | | |
362 | 364 | | |
363 | 365 | | |
364 | | - | |
| 366 | + | |
365 | 367 | | |
366 | 368 | | |
367 | 369 | | |
| |||
500 | 502 | | |
501 | 503 | | |
502 | 504 | | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
503 | 587 | | |
0 commit comments