@@ -6,7 +6,12 @@ import { isURLObjectRelative, parseStringToURLObject } from '../../utils/url';
6
6
import {
7
7
CLIENT_ADDRESS_ATTRIBUTE ,
8
8
CLIENT_PORT_ATTRIBUTE ,
9
+ MCP_LOGGING_DATA_TYPE_ATTRIBUTE ,
10
+ MCP_LOGGING_LEVEL_ATTRIBUTE ,
11
+ MCP_LOGGING_LOGGER_ATTRIBUTE ,
12
+ MCP_LOGGING_MESSAGE_ATTRIBUTE ,
9
13
MCP_PROMPT_NAME_ATTRIBUTE ,
14
+ MCP_REQUEST_ARGUMENT ,
10
15
MCP_REQUEST_ID_ATTRIBUTE ,
11
16
MCP_RESOURCE_URI_ATTRIBUTE ,
12
17
MCP_SESSION_ID_ATTRIBUTE ,
@@ -26,7 +31,10 @@ import type {
26
31
MethodConfig ,
27
32
} from './types' ;
28
33
29
- /** Configuration for MCP methods to extract targets and arguments */
34
+ /**
35
+ * Configuration for MCP methods to extract targets and arguments
36
+ * @internal Maps method names to their extraction configuration
37
+ */
30
38
const METHOD_CONFIGS : Record < string , MethodConfig > = {
31
39
'tools/call' : {
32
40
targetField : 'name' ,
@@ -56,7 +64,12 @@ const METHOD_CONFIGS: Record<string, MethodConfig> = {
56
64
} ,
57
65
} ;
58
66
59
- /** Extracts target info from method and params based on method type */
67
+ /**
68
+ * Extracts target info from method and params based on method type
69
+ * @param method - MCP method name
70
+ * @param params - Method parameters
71
+ * @returns Target name and attributes for span instrumentation
72
+ */
60
73
export function extractTargetInfo (
61
74
method : string ,
62
75
params : Record < string , unknown > ,
@@ -80,7 +93,12 @@ export function extractTargetInfo(
80
93
} ;
81
94
}
82
95
83
- /** Extracts request arguments based on method type */
96
+ /**
97
+ * Extracts request arguments based on method type
98
+ * @param method - MCP method name
99
+ * @param params - Method parameters
100
+ * @returns Arguments as span attributes with mcp.request.argument prefix
101
+ */
84
102
export function getRequestArguments ( method : string , params : Record < string , unknown > ) : Record < string , string > {
85
103
const args : Record < string , string > = { } ;
86
104
const config = METHOD_CONFIGS [ method as keyof typeof METHOD_CONFIGS ] ;
@@ -89,52 +107,55 @@ export function getRequestArguments(method: string, params: Record<string, unkno
89
107
return args ;
90
108
}
91
109
92
- // Capture arguments from the configured field
93
110
if ( config . captureArguments && config . argumentsField && params ?. [ config . argumentsField ] ) {
94
111
const argumentsObj = params [ config . argumentsField ] ;
95
112
if ( typeof argumentsObj === 'object' && argumentsObj !== null ) {
96
113
for ( const [ key , value ] of Object . entries ( argumentsObj as Record < string , unknown > ) ) {
97
- args [ `mcp.request.argument .${ key . toLowerCase ( ) } ` ] = JSON . stringify ( value ) ;
114
+ args [ `${ MCP_REQUEST_ARGUMENT } .${ key . toLowerCase ( ) } ` ] = JSON . stringify ( value ) ;
98
115
}
99
116
}
100
117
}
101
118
102
- // Capture specific fields as arguments
103
119
if ( config . captureUri && params ?. uri ) {
104
- args [ 'mcp.request.argument. uri' ] = JSON . stringify ( params . uri ) ;
120
+ args [ ` ${ MCP_REQUEST_ARGUMENT } . uri` ] = JSON . stringify ( params . uri ) ;
105
121
}
106
122
107
123
if ( config . captureName && params ?. name ) {
108
- args [ 'mcp.request.argument. name' ] = JSON . stringify ( params . name ) ;
124
+ args [ ` ${ MCP_REQUEST_ARGUMENT } . name` ] = JSON . stringify ( params . name ) ;
109
125
}
110
126
111
127
return args ;
112
128
}
113
129
114
- /** Extracts transport types based on transport constructor name */
130
+ /**
131
+ * Extracts transport types based on transport constructor name
132
+ * @param transport - MCP transport instance
133
+ * @returns Transport type mapping for span attributes
134
+ */
115
135
export function getTransportTypes ( transport : MCPTransport ) : { mcpTransport : string ; networkTransport : string } {
116
136
const transportName = transport . constructor ?. name ?. toLowerCase ( ) || '' ;
117
137
118
- // Standard MCP transports per specification
119
138
if ( transportName . includes ( 'stdio' ) ) {
120
139
return { mcpTransport : 'stdio' , networkTransport : 'pipe' } ;
121
140
}
122
141
123
- // Streamable HTTP is the standard HTTP-based transport
124
142
if ( transportName . includes ( 'streamablehttp' ) || transportName . includes ( 'streamable' ) ) {
125
143
return { mcpTransport : 'http' , networkTransport : 'tcp' } ;
126
144
}
127
145
128
- // SSE is deprecated (backwards compatibility)
129
146
if ( transportName . includes ( 'sse' ) ) {
130
147
return { mcpTransport : 'sse' , networkTransport : 'tcp' } ;
131
148
}
132
149
133
- // For custom transports, mark as unknown
134
150
return { mcpTransport : 'unknown' , networkTransport : 'unknown' } ;
135
151
}
136
152
137
- /** Extracts additional attributes for specific notification types */
153
+ /**
154
+ * Extracts additional attributes for specific notification types
155
+ * @param method - Notification method name
156
+ * @param params - Notification parameters
157
+ * @returns Method-specific attributes for span instrumentation
158
+ */
138
159
export function getNotificationAttributes (
139
160
method : string ,
140
161
params : Record < string , unknown > ,
@@ -153,18 +174,17 @@ export function getNotificationAttributes(
153
174
154
175
case 'notifications/message' :
155
176
if ( params ?. level ) {
156
- attributes [ 'mcp.logging.level' ] = String ( params . level ) ;
177
+ attributes [ MCP_LOGGING_LEVEL_ATTRIBUTE ] = String ( params . level ) ;
157
178
}
158
179
if ( params ?. logger ) {
159
- attributes [ 'mcp.logging.logger' ] = String ( params . logger ) ;
180
+ attributes [ MCP_LOGGING_LOGGER_ATTRIBUTE ] = String ( params . logger ) ;
160
181
}
161
182
if ( params ?. data !== undefined ) {
162
- attributes [ 'mcp.logging.data_type' ] = typeof params . data ;
163
- // Store the actual message content
183
+ attributes [ MCP_LOGGING_DATA_TYPE_ATTRIBUTE ] = typeof params . data ;
164
184
if ( typeof params . data === 'string' ) {
165
- attributes [ 'mcp.logging.message' ] = params . data ;
185
+ attributes [ MCP_LOGGING_MESSAGE_ATTRIBUTE ] = params . data ;
166
186
} else {
167
- attributes [ 'mcp.logging.message' ] = JSON . stringify ( params . data ) ;
187
+ attributes [ MCP_LOGGING_MESSAGE_ATTRIBUTE ] = JSON . stringify ( params . data ) ;
168
188
}
169
189
}
170
190
break ;
@@ -189,8 +209,7 @@ export function getNotificationAttributes(
189
209
190
210
case 'notifications/resources/updated' :
191
211
if ( params ?. uri ) {
192
- attributes [ 'mcp.resource.uri' ] = String ( params . uri ) ;
193
- // Extract protocol from URI
212
+ attributes [ MCP_RESOURCE_URI_ATTRIBUTE ] = String ( params . uri ) ;
194
213
const urlObject = parseStringToURLObject ( String ( params . uri ) ) ;
195
214
if ( urlObject && ! isURLObjectRelative ( urlObject ) ) {
196
215
attributes [ 'mcp.resource.protocol' ] = urlObject . protocol . replace ( ':' , '' ) ;
@@ -207,7 +226,11 @@ export function getNotificationAttributes(
207
226
return attributes ;
208
227
}
209
228
210
- /** Extracts client connection info from extra handler data */
229
+ /**
230
+ * Extracts client connection info from extra handler data
231
+ * @param extra - Extra handler data containing connection info
232
+ * @returns Client address and port information
233
+ */
211
234
export function extractClientInfo ( extra : ExtraHandlerData ) : {
212
235
address ?: string ;
213
236
port ?: number ;
@@ -222,7 +245,12 @@ export function extractClientInfo(extra: ExtraHandlerData): {
222
245
} ;
223
246
}
224
247
225
- /** Build transport and network attributes */
248
+ /**
249
+ * Build transport and network attributes
250
+ * @param transport - MCP transport instance
251
+ * @param extra - Optional extra handler data
252
+ * @returns Transport attributes for span instrumentation
253
+ */
226
254
export function buildTransportAttributes (
227
255
transport : MCPTransport ,
228
256
extra ?: ExtraHandlerData ,
@@ -241,7 +269,13 @@ export function buildTransportAttributes(
241
269
} ;
242
270
}
243
271
244
- /** Build type-specific attributes based on message type */
272
+ /**
273
+ * Build type-specific attributes based on message type
274
+ * @param type - Span type (request or notification)
275
+ * @param message - JSON-RPC message
276
+ * @param params - Optional parameters for attribute extraction
277
+ * @returns Type-specific attributes for span instrumentation
278
+ */
245
279
export function buildTypeSpecificAttributes (
246
280
type : McpSpanType ,
247
281
message : JsonRpcRequest | JsonRpcNotification ,
@@ -258,18 +292,23 @@ export function buildTypeSpecificAttributes(
258
292
} ;
259
293
}
260
294
261
- // For notifications, only include notification-specific attributes
262
295
return getNotificationAttributes ( message . method , params || { } ) ;
263
296
}
264
297
265
- /** Get metadata about tool result content array */
298
+ /**
299
+ * Get metadata about tool result content array
300
+ * @internal
301
+ */
266
302
function getContentMetadata ( content : unknown [ ] ) : Record < string , string | number > {
267
303
return {
268
304
[ MCP_TOOL_RESULT_CONTENT_COUNT_ATTRIBUTE ] : content . length ,
269
305
} ;
270
306
}
271
307
272
- /** Build attributes from a single content item */
308
+ /**
309
+ * Build attributes from a single content item
310
+ * @internal
311
+ */
273
312
function buildContentItemAttributes (
274
313
contentItem : Record < string , unknown > ,
275
314
prefix : string ,
@@ -304,7 +343,10 @@ function buildContentItemAttributes(
304
343
return attributes ;
305
344
}
306
345
307
- /** Build attributes from embedded resource object */
346
+ /**
347
+ * Build attributes from embedded resource object
348
+ * @internal
349
+ */
308
350
function buildEmbeddedResourceAttributes ( resource : Record < string , unknown > , prefix : string ) : Record < string , string > {
309
351
const attributes : Record < string , string > = { } ;
310
352
@@ -319,7 +361,10 @@ function buildEmbeddedResourceAttributes(resource: Record<string, unknown>, pref
319
361
return attributes ;
320
362
}
321
363
322
- /** Build attributes for all content items in the result */
364
+ /**
365
+ * Build attributes for all content items in the tool result
366
+ * @internal
367
+ */
323
368
function buildAllContentItemAttributes ( content : unknown [ ] ) : Record < string , string | number > {
324
369
const attributes : Record < string , string | number > = { } ;
325
370
@@ -341,7 +386,11 @@ function buildAllContentItemAttributes(content: unknown[]): Record<string, strin
341
386
return attributes ;
342
387
}
343
388
344
- /** Extract tool result attributes for span instrumentation */
389
+ /**
390
+ * Extract tool result attributes for span instrumentation
391
+ * @param result - Tool execution result
392
+ * @returns Attributes extracted from tool result content
393
+ */
345
394
export function extractToolResultAttributes ( result : unknown ) : Record < string , string | number | boolean > {
346
395
let attributes : Record < string , string | number | boolean > = { } ;
347
396
0 commit comments