44 */
55
66import { 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' ;
99import {
1010 MCP_TOOL_RESULT_CONTENT_ATTRIBUTE ,
1111 MCP_TOOL_RESULT_CONTENT_COUNT_ATTRIBUTE ,
1212 MCP_TOOL_RESULT_IS_ERROR_ATTRIBUTE ,
1313} from './attributes' ;
1414import { captureError } from './errorCapture' ;
1515import { filterMcpPiiFromSpanData } from './piiFiltering' ;
16- import type { RequestId , SessionId } from './types' ;
16+ import type { RequestId , RequestSpanMapValue , SessionId } from './types' ;
1717
1818// Simplified correlation system that works with or without sessionId
1919// 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 > ( ) ;
2821
2922/**
3023 * Stores span context for later correlation with handler execution
@@ -69,37 +62,28 @@ export function completeSpanWithResults(requestId: RequestId, result: unknown):
6962 if ( spanData ) {
7063 const { span, method } = spanData ;
7164
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' ) {
7966 // Add tool-specific attributes with PII filtering
8067 const rawToolAttributes = extractToolResultAttributes ( result ) ;
8168 const client = getClient ( ) ;
8269 const sendDefaultPii = Boolean ( client ?. getOptions ( ) . sendDefaultPii ) ;
8370 const toolAttributes = filterMcpPiiFromSpanData ( rawToolAttributes , sendDefaultPii ) ;
8471
85- spanWithMethods . setAttributes ( toolAttributes ) ;
72+ span . setAttributes ( toolAttributes ) ;
8673
8774 const isToolError = rawToolAttributes [ MCP_TOOL_RESULT_IS_ERROR_ATTRIBUTE ] === true ;
8875
8976 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 ' ,
9380 } ) ;
9481
9582 captureError ( new Error ( 'Tool returned error result' ) , 'tool_execution' ) ;
9683 }
9784 }
9885
99- if ( spanWithMethods . end ) {
100- spanWithMethods . end ( ) ;
101- }
102-
86+ span . end ( ) ;
10387 requestIdToSpanMap . delete ( requestId ) ;
10488 }
10589}
@@ -111,17 +95,11 @@ export function cleanupAllPendingSpans(): number {
11195 const pendingCount = requestIdToSpanMap . size ;
11296
11397 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 ( ) ;
125103 }
126104
127105 requestIdToSpanMap . clear ( ) ;
@@ -131,8 +109,8 @@ export function cleanupAllPendingSpans(): number {
131109/**
132110 * Simplified tool result attribute extraction
133111 */
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 > = { } ;
136114
137115 if ( typeof result === 'object' && result !== null ) {
138116 const resultObj = result as Record < string , unknown > ;
0 commit comments