Skip to content

Commit 1cd1048

Browse files
committed
ref: improve jsdoc and refactor attributes to be in a single file
1 parent 612fc8c commit 1cd1048

File tree

11 files changed

+341
-247
lines changed

11 files changed

+341
-247
lines changed

packages/core/src/integrations/mcp-server/attributeExtraction.ts

Lines changed: 80 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { isURLObjectRelative, parseStringToURLObject } from '../../utils/url';
66
import {
77
CLIENT_ADDRESS_ATTRIBUTE,
88
CLIENT_PORT_ATTRIBUTE,
9+
MCP_LOGGING_DATA_TYPE_ATTRIBUTE,
10+
MCP_LOGGING_LEVEL_ATTRIBUTE,
11+
MCP_LOGGING_LOGGER_ATTRIBUTE,
12+
MCP_LOGGING_MESSAGE_ATTRIBUTE,
913
MCP_PROMPT_NAME_ATTRIBUTE,
14+
MCP_REQUEST_ARGUMENT,
1015
MCP_REQUEST_ID_ATTRIBUTE,
1116
MCP_RESOURCE_URI_ATTRIBUTE,
1217
MCP_SESSION_ID_ATTRIBUTE,
@@ -26,7 +31,10 @@ import type {
2631
MethodConfig,
2732
} from './types';
2833

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+
*/
3038
const METHOD_CONFIGS: Record<string, MethodConfig> = {
3139
'tools/call': {
3240
targetField: 'name',
@@ -56,7 +64,12 @@ const METHOD_CONFIGS: Record<string, MethodConfig> = {
5664
},
5765
};
5866

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+
*/
6073
export function extractTargetInfo(
6174
method: string,
6275
params: Record<string, unknown>,
@@ -80,7 +93,12 @@ export function extractTargetInfo(
8093
};
8194
}
8295

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+
*/
84102
export function getRequestArguments(method: string, params: Record<string, unknown>): Record<string, string> {
85103
const args: Record<string, string> = {};
86104
const config = METHOD_CONFIGS[method as keyof typeof METHOD_CONFIGS];
@@ -89,52 +107,55 @@ export function getRequestArguments(method: string, params: Record<string, unkno
89107
return args;
90108
}
91109

92-
// Capture arguments from the configured field
93110
if (config.captureArguments && config.argumentsField && params?.[config.argumentsField]) {
94111
const argumentsObj = params[config.argumentsField];
95112
if (typeof argumentsObj === 'object' && argumentsObj !== null) {
96113
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);
98115
}
99116
}
100117
}
101118

102-
// Capture specific fields as arguments
103119
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);
105121
}
106122

107123
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);
109125
}
110126

111127
return args;
112128
}
113129

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+
*/
115135
export function getTransportTypes(transport: MCPTransport): { mcpTransport: string; networkTransport: string } {
116136
const transportName = transport.constructor?.name?.toLowerCase() || '';
117137

118-
// Standard MCP transports per specification
119138
if (transportName.includes('stdio')) {
120139
return { mcpTransport: 'stdio', networkTransport: 'pipe' };
121140
}
122141

123-
// Streamable HTTP is the standard HTTP-based transport
124142
if (transportName.includes('streamablehttp') || transportName.includes('streamable')) {
125143
return { mcpTransport: 'http', networkTransport: 'tcp' };
126144
}
127145

128-
// SSE is deprecated (backwards compatibility)
129146
if (transportName.includes('sse')) {
130147
return { mcpTransport: 'sse', networkTransport: 'tcp' };
131148
}
132149

133-
// For custom transports, mark as unknown
134150
return { mcpTransport: 'unknown', networkTransport: 'unknown' };
135151
}
136152

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+
*/
138159
export function getNotificationAttributes(
139160
method: string,
140161
params: Record<string, unknown>,
@@ -153,18 +174,17 @@ export function getNotificationAttributes(
153174

154175
case 'notifications/message':
155176
if (params?.level) {
156-
attributes['mcp.logging.level'] = String(params.level);
177+
attributes[MCP_LOGGING_LEVEL_ATTRIBUTE] = String(params.level);
157178
}
158179
if (params?.logger) {
159-
attributes['mcp.logging.logger'] = String(params.logger);
180+
attributes[MCP_LOGGING_LOGGER_ATTRIBUTE] = String(params.logger);
160181
}
161182
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;
164184
if (typeof params.data === 'string') {
165-
attributes['mcp.logging.message'] = params.data;
185+
attributes[MCP_LOGGING_MESSAGE_ATTRIBUTE] = params.data;
166186
} else {
167-
attributes['mcp.logging.message'] = JSON.stringify(params.data);
187+
attributes[MCP_LOGGING_MESSAGE_ATTRIBUTE] = JSON.stringify(params.data);
168188
}
169189
}
170190
break;
@@ -189,8 +209,7 @@ export function getNotificationAttributes(
189209

190210
case 'notifications/resources/updated':
191211
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);
194213
const urlObject = parseStringToURLObject(String(params.uri));
195214
if (urlObject && !isURLObjectRelative(urlObject)) {
196215
attributes['mcp.resource.protocol'] = urlObject.protocol.replace(':', '');
@@ -207,7 +226,11 @@ export function getNotificationAttributes(
207226
return attributes;
208227
}
209228

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+
*/
211234
export function extractClientInfo(extra: ExtraHandlerData): {
212235
address?: string;
213236
port?: number;
@@ -222,7 +245,12 @@ export function extractClientInfo(extra: ExtraHandlerData): {
222245
};
223246
}
224247

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+
*/
226254
export function buildTransportAttributes(
227255
transport: MCPTransport,
228256
extra?: ExtraHandlerData,
@@ -241,7 +269,13 @@ export function buildTransportAttributes(
241269
};
242270
}
243271

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+
*/
245279
export function buildTypeSpecificAttributes(
246280
type: McpSpanType,
247281
message: JsonRpcRequest | JsonRpcNotification,
@@ -258,18 +292,23 @@ export function buildTypeSpecificAttributes(
258292
};
259293
}
260294

261-
// For notifications, only include notification-specific attributes
262295
return getNotificationAttributes(message.method, params || {});
263296
}
264297

265-
/** Get metadata about tool result content array */
298+
/**
299+
* Get metadata about tool result content array
300+
* @internal
301+
*/
266302
function getContentMetadata(content: unknown[]): Record<string, string | number> {
267303
return {
268304
[MCP_TOOL_RESULT_CONTENT_COUNT_ATTRIBUTE]: content.length,
269305
};
270306
}
271307

272-
/** Build attributes from a single content item */
308+
/**
309+
* Build attributes from a single content item
310+
* @internal
311+
*/
273312
function buildContentItemAttributes(
274313
contentItem: Record<string, unknown>,
275314
prefix: string,
@@ -304,7 +343,10 @@ function buildContentItemAttributes(
304343
return attributes;
305344
}
306345

307-
/** Build attributes from embedded resource object */
346+
/**
347+
* Build attributes from embedded resource object
348+
* @internal
349+
*/
308350
function buildEmbeddedResourceAttributes(resource: Record<string, unknown>, prefix: string): Record<string, string> {
309351
const attributes: Record<string, string> = {};
310352

@@ -319,7 +361,10 @@ function buildEmbeddedResourceAttributes(resource: Record<string, unknown>, pref
319361
return attributes;
320362
}
321363

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+
*/
323368
function buildAllContentItemAttributes(content: unknown[]): Record<string, string | number> {
324369
const attributes: Record<string, string | number> = {};
325370

@@ -341,7 +386,11 @@ function buildAllContentItemAttributes(content: unknown[]): Record<string, strin
341386
return attributes;
342387
}
343388

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+
*/
345394
export function extractToolResultAttributes(result: unknown): Record<string, string | number | boolean> {
346395
let attributes: Record<string, string | number | boolean> = {};
347396

0 commit comments

Comments
 (0)