Skip to content

Commit 8524991

Browse files
committed
refactor types
1 parent 790f2b3 commit 8524991

File tree

3 files changed

+31
-43
lines changed

3 files changed

+31
-43
lines changed

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

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,20 @@
44
*/
55

66
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';
99
import {
1010
MCP_TOOL_RESULT_CONTENT_ATTRIBUTE,
1111
MCP_TOOL_RESULT_CONTENT_COUNT_ATTRIBUTE,
1212
MCP_TOOL_RESULT_IS_ERROR_ATTRIBUTE,
1313
} from './attributes';
1414
import { captureError } from './errorCapture';
1515
import { 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>;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* PII filtering for MCP server spans
33
* Removes sensitive data when sendDefaultPii is false
44
*/
5-
5+
import type { SpanAttributeValue } from '../../types-hoist/span';
66
import { MCP_TOOL_RESULT_CONTENT_ATTRIBUTE } from './attributes';
77

88
/** PII attributes that should be removed when sendDefaultPii is false */
@@ -20,9 +20,9 @@ const PII_ATTRIBUTES = new Set([
2020
export function filterMcpPiiFromSpanData(
2121
spanData: Record<string, unknown>,
2222
sendDefaultPii: boolean,
23-
): Record<string, unknown> {
23+
): Record<string, SpanAttributeValue | undefined> {
2424
if (sendDefaultPii) {
25-
return spanData; // Send everything when PII is allowed
25+
return spanData as Record<string, SpanAttributeValue | undefined>; // Cast for type safety
2626
}
2727

2828
// Use Object.fromEntries with filter for a more functional approach
@@ -31,5 +31,5 @@ export function filterMcpPiiFromSpanData(
3131
const isPiiAttribute = PII_ATTRIBUTES.has(key) || key.startsWith('mcp.request.argument.');
3232
return !isPiiAttribute;
3333
}),
34-
);
34+
) as Record<string, SpanAttributeValue | undefined>;
3535
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Span } from '../../types-hoist/span';
2+
13
/**
24
* types for MCP server instrumentation
35
*/
@@ -143,6 +145,14 @@ export interface McpSpanConfig {
143145

144146
export type SessionId = string;
145147
export type RequestId = string | number;
148+
149+
// Type for the value stored in requestIdToSpanMap
150+
export type RequestSpanMapValue = {
151+
span: Span;
152+
method: string;
153+
startTime: number;
154+
};
155+
146156
export type MCPHandler = (...args: unknown[]) => unknown;
147157
export interface HandlerExtraData {
148158
sessionId?: SessionId;

0 commit comments

Comments
 (0)