Commit 87b4e30
authored
feat(core): MCP server instrumentation without breaking Miniflare (#16817)
Closes #16826, #16654, #16666 #16978
Different approach from #16807 .
Using `Proxy` was causing issues in cloudflare #16182.
Now using `fill` we shouldn't have those problems as `fill` doesn't
create a new wrapper object with a different identity, so now:
1. `fill` just replaces the method on the existing object
2. The transport object keeps its original identity
3. When `transport.start()` runs and accesses private fields, this is
still the original transport object
4. The `WeakMap` recognizes it as the same object that owns the private
fields
## What's inside
- Support for new MCP SDK methods (`mcpServerInstance.tool()`,
`mcpServerInstance.resource()`, etc.)
- Tracing instrumentation
- Error handling
## Tracing
It follows [OTEL semantic conventions for
MCP](https://github.com/open-telemetry/semantic-conventions/blob/3097fb0af5b9492b0e3f55dc5f6c21a3dc2be8df/docs/gen-ai/mcp.md)
and adds more attributes we thought are useful.
It also handles PII based on user setting of `sendDefaultPii`.
### Tracing flow
1. **Transport receives tools/call request (`id: 2`)**
2. **Create INACTIVE `mcp.server` span**
3. **Store span in**`requestIdToSpanMap[2] = { span, method:
"tools/call", startTime }`
4. **Execute handler with span context** *(handler gets requestId: 2)*
5. **Handler finds span using** `requestId: 2`
6. **Tool execution happens within span context**
7. **Response sent with tool results**
8. `completeSpanWithToolResults(2, result)` **enriches and completes
span**
## Error handling
1. **error capture** - `errorCapture.ts`
- Non-invasive error reporting that never interferes with MCP service
operation
- Error context with MCP-specific metadata
- PII filtering respects `sendDefaultPii` settings
- Resilient to Sentry failures (wrapped in try-catch)
2. **Tool execution error capturing** - `handlers.ts`
- Captures exceptions thrown during tool execution
- Preserves normal MCP behaviour (errors converted to `isError: true`)
- Includes tool name, arguments, and request context
- Handles both sync and async tool handlers
3. **Transport error instrumentation** - `transport.ts`
- Captures connection errors and network failures
- Intercepts JSON-RPC error responses
- Includes transport type and session information
- Handles error responses being sent back to clients1 parent 37bfcdc commit 87b4e30
File tree
27 files changed
+3337
-580
lines changed- dev-packages/e2e-tests/test-applications
- node-express-v5/tests
- node-express
- src
- tests
- packages/core
- src
- integrations/mcp-server
- test/lib
- integrations/mcp-server
27 files changed
+3337
-580
lines changedLines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
79 | | - | |
| 79 | + | |
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
| 58 | + | |
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| |||
Lines changed: 7 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
53 | 53 | | |
Lines changed: 12 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
45 | | - | |
| 46 | + | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
| |||
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
54 | | - | |
| 56 | + | |
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
| |||
64 | 66 | | |
65 | 67 | | |
66 | 68 | | |
| 69 | + | |
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
70 | | - | |
| 73 | + | |
| 74 | + | |
71 | 75 | | |
72 | 76 | | |
73 | 77 | | |
| |||
76 | 80 | | |
77 | 81 | | |
78 | 82 | | |
79 | | - | |
| 83 | + | |
80 | 84 | | |
81 | 85 | | |
82 | 86 | | |
| |||
100 | 104 | | |
101 | 105 | | |
102 | 106 | | |
| 107 | + | |
103 | 108 | | |
104 | 109 | | |
105 | 110 | | |
106 | | - | |
| 111 | + | |
| 112 | + | |
107 | 113 | | |
108 | 114 | | |
109 | 115 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
| 115 | + | |
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
| |||
0 commit comments