4
4
*/
5
5
6
6
import { getClient } from '../../currentScopes' ;
7
- import { withActiveSpan } from '../../tracing' ;
8
- import type { Span } from '../../types-hoist/span' ;
7
+ import { SPAN_STATUS_ERROR , withActiveSpan } from '../../tracing' ;
8
+ import type { Span , SpanAttributeValue } from '../../types-hoist/span' ;
9
9
import {
10
10
MCP_TOOL_RESULT_CONTENT_ATTRIBUTE ,
11
11
MCP_TOOL_RESULT_CONTENT_COUNT_ATTRIBUTE ,
12
12
MCP_TOOL_RESULT_IS_ERROR_ATTRIBUTE ,
13
13
} from './attributes' ;
14
14
import { captureError } from './errorCapture' ;
15
15
import { filterMcpPiiFromSpanData } from './piiFiltering' ;
16
- import type { RequestId , SessionId } from './types' ;
16
+ import type { RequestId , RequestSpanMapValue , SessionId } from './types' ;
17
17
18
18
// Simplified correlation system that works with or without sessionId
19
19
// Maps requestId directly to span data for stateless operation
20
- const requestIdToSpanMap = new Map <
21
- RequestId ,
22
- {
23
- span : Span ;
24
- method : string ;
25
- startTime : number ;
26
- }
27
- > ( ) ;
20
+ const requestIdToSpanMap = new Map < RequestId , RequestSpanMapValue > ( ) ;
28
21
29
22
/**
30
23
* Stores span context for later correlation with handler execution
@@ -69,37 +62,28 @@ export function completeSpanWithResults(requestId: RequestId, result: unknown):
69
62
if ( spanData ) {
70
63
const { span, method } = spanData ;
71
64
72
- const spanWithMethods = span as Span & {
73
- setAttributes : ( attrs : Record < string , unknown > ) => void ;
74
- setStatus : ( status : { code : number ; message : string } ) => void ;
75
- end : ( ) => void ;
76
- } ;
77
-
78
- if ( spanWithMethods . setAttributes && method === 'tools/call' ) {
65
+ if ( method === 'tools/call' ) {
79
66
// Add tool-specific attributes with PII filtering
80
67
const rawToolAttributes = extractToolResultAttributes ( result ) ;
81
68
const client = getClient ( ) ;
82
69
const sendDefaultPii = Boolean ( client ?. getOptions ( ) . sendDefaultPii ) ;
83
70
const toolAttributes = filterMcpPiiFromSpanData ( rawToolAttributes , sendDefaultPii ) ;
84
71
85
- spanWithMethods . setAttributes ( toolAttributes ) ;
72
+ span . setAttributes ( toolAttributes ) ;
86
73
87
74
const isToolError = rawToolAttributes [ MCP_TOOL_RESULT_IS_ERROR_ATTRIBUTE ] === true ;
88
75
89
76
if ( isToolError ) {
90
- spanWithMethods . setStatus ( {
91
- code : 2 , // ERROR
92
- message : 'Tool execution failed ' ,
77
+ span . setStatus ( {
78
+ code : SPAN_STATUS_ERROR ,
79
+ message : 'Tool returned error result ' ,
93
80
} ) ;
94
81
95
82
captureError ( new Error ( 'Tool returned error result' ) , 'tool_execution' ) ;
96
83
}
97
84
}
98
85
99
- if ( spanWithMethods . end ) {
100
- spanWithMethods . end ( ) ;
101
- }
102
-
86
+ span . end ( ) ;
103
87
requestIdToSpanMap . delete ( requestId ) ;
104
88
}
105
89
}
@@ -111,17 +95,11 @@ export function cleanupAllPendingSpans(): number {
111
95
const pendingCount = requestIdToSpanMap . size ;
112
96
113
97
for ( const [ , spanData ] of requestIdToSpanMap ) {
114
- const spanWithEnd = spanData . span as Span & {
115
- end : ( ) => void ;
116
- setStatus : ( status : { code : number ; message : string } ) => void ;
117
- } ;
118
- if ( spanWithEnd . setStatus && spanWithEnd . end ) {
119
- spanWithEnd . setStatus ( {
120
- code : 2 , // ERROR
121
- message : 'Transport closed before request completion' ,
122
- } ) ;
123
- spanWithEnd . end ( ) ;
124
- }
98
+ spanData . span . setStatus ( {
99
+ code : SPAN_STATUS_ERROR ,
100
+ message : 'Transport closed before request completion' ,
101
+ } ) ;
102
+ spanData . span . end ( ) ;
125
103
}
126
104
127
105
requestIdToSpanMap . clear ( ) ;
@@ -131,8 +109,8 @@ export function cleanupAllPendingSpans(): number {
131
109
/**
132
110
* Simplified tool result attribute extraction
133
111
*/
134
- function extractToolResultAttributes ( result : unknown ) : Record < string , string | number | boolean > {
135
- const attributes : Record < string , string | number | boolean > = { } ;
112
+ function extractToolResultAttributes ( result : unknown ) : Record < string , SpanAttributeValue | undefined > {
113
+ const attributes : Record < string , SpanAttributeValue | undefined > = { } ;
136
114
137
115
if ( typeof result === 'object' && result !== null ) {
138
116
const resultObj = result as Record < string , unknown > ;
0 commit comments