Skip to content

Commit f59b51d

Browse files
committed
fix division by 0 error
1 parent aa1f208 commit f59b51d

File tree

1 file changed

+67
-55
lines changed

1 file changed

+67
-55
lines changed

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

Lines changed: 67 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -72,69 +72,81 @@ export function getNotificationAttributes(
7272
method: string,
7373
params: Record<string, unknown>,
7474
): Record<string, string | number> {
75-
const attributes: Record<string, string | number> = {};
76-
7775
switch (method) {
7876
case 'notifications/cancelled':
79-
if (params?.requestId) {
80-
attributes['mcp.cancelled.request_id'] = String(params.requestId);
81-
}
82-
if (params?.reason) {
83-
attributes['mcp.cancelled.reason'] = String(params.reason);
84-
}
85-
break;
86-
77+
return getCancelledAttributes(params);
8778
case 'notifications/message':
88-
if (params?.level) {
89-
attributes[MCP_LOGGING_LEVEL_ATTRIBUTE] = String(params.level);
90-
}
91-
if (params?.logger) {
92-
attributes[MCP_LOGGING_LOGGER_ATTRIBUTE] = String(params.logger);
93-
}
94-
if (params?.data !== undefined) {
95-
attributes[MCP_LOGGING_DATA_TYPE_ATTRIBUTE] = typeof params.data;
96-
if (typeof params.data === 'string') {
97-
attributes[MCP_LOGGING_MESSAGE_ATTRIBUTE] = params.data;
98-
} else {
99-
attributes[MCP_LOGGING_MESSAGE_ATTRIBUTE] = JSON.stringify(params.data);
100-
}
101-
}
102-
break;
103-
79+
return getMessageAttributes(params);
10480
case 'notifications/progress':
105-
if (params?.progressToken) {
106-
attributes['mcp.progress.token'] = String(params.progressToken);
107-
}
108-
if (typeof params?.progress === 'number') {
109-
attributes['mcp.progress.current'] = params.progress;
110-
}
111-
if (typeof params?.total === 'number') {
112-
attributes['mcp.progress.total'] = params.total;
113-
if (typeof params?.progress === 'number') {
114-
attributes['mcp.progress.percentage'] = (params.progress / params.total) * 100;
115-
}
116-
}
117-
if (params?.message) {
118-
attributes['mcp.progress.message'] = String(params.message);
119-
}
120-
break;
121-
81+
return getProgressAttributes(params);
12282
case 'notifications/resources/updated':
123-
if (params?.uri) {
124-
attributes[MCP_RESOURCE_URI_ATTRIBUTE] = String(params.uri);
125-
const urlObject = parseStringToURLObject(String(params.uri));
126-
if (urlObject && !isURLObjectRelative(urlObject)) {
127-
attributes['mcp.resource.protocol'] = urlObject.protocol.replace(':', '');
128-
}
129-
}
130-
break;
131-
83+
return getResourcesUpdatedAttributes(params);
13284
case 'notifications/initialized':
133-
attributes['mcp.lifecycle.phase'] = 'initialization_complete';
134-
attributes['mcp.protocol.ready'] = 1;
135-
break;
85+
return { 'mcp.lifecycle.phase': 'initialization_complete', 'mcp.protocol.ready': 1 };
86+
default:
87+
return {};
13688
}
89+
}
13790

91+
function getCancelledAttributes(params: Record<string, unknown>): Record<string, string | number> {
92+
const attributes: Record<string, string | number> = {};
93+
if (params?.requestId) {
94+
attributes['mcp.cancelled.request_id'] = String(params.requestId);
95+
}
96+
if (params?.reason) {
97+
attributes['mcp.cancelled.reason'] = String(params.reason);
98+
}
99+
return attributes;
100+
}
101+
102+
function getMessageAttributes(params: Record<string, unknown>): Record<string, string | number> {
103+
const attributes: Record<string, string | number> = {};
104+
if (params?.level) {
105+
attributes[MCP_LOGGING_LEVEL_ATTRIBUTE] = String(params.level);
106+
}
107+
if (params?.logger) {
108+
attributes[MCP_LOGGING_LOGGER_ATTRIBUTE] = String(params.logger);
109+
}
110+
if (params?.data !== undefined) {
111+
attributes[MCP_LOGGING_DATA_TYPE_ATTRIBUTE] = typeof params.data;
112+
if (typeof params.data === 'string') {
113+
attributes[MCP_LOGGING_MESSAGE_ATTRIBUTE] = params.data;
114+
} else {
115+
attributes[MCP_LOGGING_MESSAGE_ATTRIBUTE] = JSON.stringify(params.data);
116+
}
117+
}
118+
return attributes;
119+
}
120+
121+
function getProgressAttributes(params: Record<string, unknown>): Record<string, string | number> {
122+
const attributes: Record<string, string | number> = {};
123+
if (params?.progressToken) {
124+
attributes['mcp.progress.token'] = String(params.progressToken);
125+
}
126+
if (typeof params?.progress === 'number') {
127+
attributes['mcp.progress.current'] = params.progress;
128+
}
129+
if (typeof params?.total === 'number') {
130+
attributes['mcp.progress.total'] = params.total;
131+
if (typeof params?.progress === 'number') {
132+
attributes['mcp.progress.percentage'] = params.total ? (params.progress / params.total) * 100 : 0;
133+
}
134+
}
135+
if (params?.message) {
136+
attributes['mcp.progress.message'] = String(params.message);
137+
}
138+
return attributes;
139+
}
140+
141+
function getResourcesUpdatedAttributes(params: Record<string, unknown>): Record<string, string | number> {
142+
const attributes: Record<string, string | number> = {};
143+
if (params?.uri) {
144+
attributes[MCP_RESOURCE_URI_ATTRIBUTE] = String(params.uri);
145+
const urlObject = parseStringToURLObject(String(params.uri));
146+
if (urlObject && !isURLObjectRelative(urlObject)) {
147+
attributes['mcp.resource.protocol'] = urlObject.protocol.replace(':', '');
148+
}
149+
}
138150
return attributes;
139151
}
140152

0 commit comments

Comments
 (0)