@brizz/sdk 0.1.29 → 0.1.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -2
- package/dist/chunk-3OLW4TOG.js +158 -0
- package/dist/chunk-6UYAVA5T.js +1970 -0
- package/dist/chunk-GCORRK6B.cjs +54 -0
- package/dist/chunk-JB6MA2RV.cjs +158 -0
- package/dist/chunk-LTXMCGVQ.cjs +90 -0
- package/dist/chunk-MWX3GIJW.js +90 -0
- package/dist/chunk-NUTJMVD4.cjs +1970 -0
- package/dist/chunk-RORIBZEV.js +54 -0
- package/dist/index.cjs +92 -3073
- package/dist/index.d.cts +7 -6
- package/dist/index.d.ts +7 -6
- package/dist/index.js +65 -2996
- package/dist/interrupt-HDNPDMYY.js +11 -0
- package/dist/interrupt-HOP3XPQF.cjs +11 -0
- package/dist/loader.mjs +11 -9
- package/dist/mcp-56TY4LZ5.js +712 -0
- package/dist/mcp-6BDG4SWY.cjs +712 -0
- package/dist/preload.cjs +26 -2508
- package/dist/preload.js +21 -2490
- package/package.json +39 -31
|
@@ -0,0 +1,712 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getSDKVersion,
|
|
3
|
+
logger
|
|
4
|
+
} from "./chunk-3OLW4TOG.js";
|
|
5
|
+
import {
|
|
6
|
+
BRIZZ,
|
|
7
|
+
PROPERTIES_CONTEXT_KEY,
|
|
8
|
+
SESSION_ID
|
|
9
|
+
} from "./chunk-RORIBZEV.js";
|
|
10
|
+
|
|
11
|
+
// src/internal/instrumentation/mcp/instrumentation.ts
|
|
12
|
+
import { MCPInstrumentation as BaseMCPInstrumentation } from "@arizeai/openinference-instrumentation-mcp";
|
|
13
|
+
import { trace as trace2 } from "@opentelemetry/api";
|
|
14
|
+
import { InstrumentationNodeModuleDefinition } from "@opentelemetry/instrumentation";
|
|
15
|
+
|
|
16
|
+
// src/internal/instrumentation/mcp/patches/protocol.ts
|
|
17
|
+
import { context as context2, propagation, SpanKind as SpanKind2, trace } from "@opentelemetry/api";
|
|
18
|
+
|
|
19
|
+
// src/internal/instrumentation/mcp/schemas.ts
|
|
20
|
+
import { SpanKind, SpanStatusCode } from "@opentelemetry/api";
|
|
21
|
+
|
|
22
|
+
// src/internal/instrumentation/mcp/semantic-conventions.ts
|
|
23
|
+
var MCP_TOOL_NAME = "mcp.tool.name";
|
|
24
|
+
var MCP_TOOL_ARGUMENTS = "mcp.tool.arguments";
|
|
25
|
+
var MCP_TOOL_RESULT = "mcp.tool.result";
|
|
26
|
+
var MCP_COMPONENT_TYPE = "mcp.component.type";
|
|
27
|
+
var MCP_COMPONENT_TOOL = "tool";
|
|
28
|
+
var MCP_COMPONENT_TOOL_SCHEMA = "tool_schema";
|
|
29
|
+
var MCP_TOOL_SCHEMA_PARAMETERS = "mcp.tool.schema.parameters";
|
|
30
|
+
var MCP_TOOL_SCHEMA_OUTPUT = "mcp.tool.schema.output";
|
|
31
|
+
var MCP_TOOL_DESCRIPTION = "mcp.tool.description";
|
|
32
|
+
var SPAN_NAME_TOOL_REGISTER = "mcp.tool.register";
|
|
33
|
+
var MCP_METHOD_NAME = "mcp.method.name";
|
|
34
|
+
var MCP_REQUEST_ID = "mcp.request.id";
|
|
35
|
+
var MCP_SESSION_ID = "mcp.session.id";
|
|
36
|
+
var MCP_PROTOCOL_VERSION = "mcp.protocol.version";
|
|
37
|
+
var MCP_RESOURCE_URI = "mcp.resource.uri";
|
|
38
|
+
var RPC_SYSTEM = "rpc.system";
|
|
39
|
+
var RPC_SYSTEM_MCP = "mcp";
|
|
40
|
+
var RPC_RESPONSE_STATUS_CODE = "rpc.response.status_code";
|
|
41
|
+
var GEN_AI_TOOL_NAME = "gen_ai.tool.name";
|
|
42
|
+
var GEN_AI_PROMPT_NAME = "gen_ai.prompt.name";
|
|
43
|
+
var GEN_AI_OPERATION_NAME = "gen_ai.operation.name";
|
|
44
|
+
var GEN_AI_OPERATION_EXECUTE_TOOL = "execute_tool";
|
|
45
|
+
var NETWORK_TRANSPORT = "network.transport";
|
|
46
|
+
var ERROR_TYPE = "error.type";
|
|
47
|
+
var ERROR_TYPE_TOOL = "tool_error";
|
|
48
|
+
var JSONRPC_REQUEST_ID = "jsonrpc.request.id";
|
|
49
|
+
var SPAN_NAME_TOOLS_CALL = "tools/call";
|
|
50
|
+
var MAX_ATTRIBUTE_LENGTH = 32 * 1024;
|
|
51
|
+
var TRUNCATION_SUFFIX = "\u2026(truncated)";
|
|
52
|
+
var METHOD_TOOLS_CALL = "tools/call";
|
|
53
|
+
var METHOD_TOOLS_LIST = "tools/list";
|
|
54
|
+
var METHOD_RESOURCES_READ = "resources/read";
|
|
55
|
+
var METHOD_PROMPTS_GET = "prompts/get";
|
|
56
|
+
var METHOD_INITIALIZE = "initialize";
|
|
57
|
+
|
|
58
|
+
// src/internal/instrumentation/mcp/schemas.ts
|
|
59
|
+
var _MAX_SCHEMA_ATTR_BYTES = 4e3;
|
|
60
|
+
function truncateSchemaAttr(value) {
|
|
61
|
+
if (value.length <= _MAX_SCHEMA_ATTR_BYTES) {
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
return `{"_truncated":true,"original_length":${value.length}}`;
|
|
65
|
+
}
|
|
66
|
+
function safeStringify(value) {
|
|
67
|
+
if (value === null || value === void 0) {
|
|
68
|
+
return "";
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
return JSON.stringify(value);
|
|
72
|
+
} catch {
|
|
73
|
+
return "";
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function emitSchemaSpansFromListResponse(result, transportSessionId, tracer) {
|
|
77
|
+
if (!transportSessionId) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const tools = extractTools(result);
|
|
81
|
+
if (tools === void 0) {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
for (const tool of tools) {
|
|
85
|
+
const name = typeof tool.name === "string" ? tool.name : void 0;
|
|
86
|
+
if (!name) {
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
const span = tracer.startSpan(`${SPAN_NAME_TOOL_REGISTER} ${name}`, {
|
|
90
|
+
kind: SpanKind.INTERNAL
|
|
91
|
+
});
|
|
92
|
+
try {
|
|
93
|
+
stampSchemaAttributes(span, name, transportSessionId, tool);
|
|
94
|
+
span.setStatus({ code: SpanStatusCode.OK });
|
|
95
|
+
} finally {
|
|
96
|
+
span.end();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function stampSchemaAttributes(span, toolName, transportSessionId, tool) {
|
|
101
|
+
if (!span.isRecording()) {
|
|
102
|
+
logger.warn(
|
|
103
|
+
`Brizz MCP: schema span is not recording; dropping attributes for ${toolName}`
|
|
104
|
+
);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const description = typeof tool.description === "string" ? tool.description : "";
|
|
108
|
+
const parameters = truncateSchemaAttr(safeStringify(tool.inputSchema));
|
|
109
|
+
const outputSchema = tool.outputSchema !== void 0 && tool.outputSchema !== null ? truncateSchemaAttr(safeStringify(tool.outputSchema)) : "";
|
|
110
|
+
span.setAttribute(RPC_SYSTEM, RPC_SYSTEM_MCP);
|
|
111
|
+
span.setAttribute(MCP_COMPONENT_TYPE, MCP_COMPONENT_TOOL_SCHEMA);
|
|
112
|
+
span.setAttribute(MCP_SESSION_ID, transportSessionId);
|
|
113
|
+
span.setAttribute(MCP_TOOL_NAME, toolName);
|
|
114
|
+
span.setAttribute(MCP_TOOL_SCHEMA_PARAMETERS, parameters);
|
|
115
|
+
span.setAttribute(MCP_TOOL_SCHEMA_OUTPUT, outputSchema);
|
|
116
|
+
span.setAttribute(MCP_TOOL_DESCRIPTION, description);
|
|
117
|
+
}
|
|
118
|
+
function extractTools(result) {
|
|
119
|
+
if (!result || typeof result !== "object") {
|
|
120
|
+
return void 0;
|
|
121
|
+
}
|
|
122
|
+
const tools = result.tools;
|
|
123
|
+
if (!Array.isArray(tools)) {
|
|
124
|
+
return void 0;
|
|
125
|
+
}
|
|
126
|
+
return tools.filter(
|
|
127
|
+
(t) => t !== null && typeof t === "object"
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/internal/instrumentation/mcp/session.ts
|
|
132
|
+
import { context } from "@opentelemetry/api";
|
|
133
|
+
function stampAndPropagateSession(span, sessionId, baseContext = context.active()) {
|
|
134
|
+
if (!sessionId) {
|
|
135
|
+
return { context: baseContext, sessionId: null };
|
|
136
|
+
}
|
|
137
|
+
if (span.isRecording()) {
|
|
138
|
+
try {
|
|
139
|
+
span.setAttribute(`${BRIZZ}.${SESSION_ID}`, sessionId);
|
|
140
|
+
} catch (error) {
|
|
141
|
+
logger.warn(
|
|
142
|
+
`Brizz MCP: failed to stamp session id on span: ${String(error)}`
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
try {
|
|
147
|
+
const prev = baseContext.getValue(PROPERTIES_CONTEXT_KEY);
|
|
148
|
+
const merged = prev ? { ...prev, [SESSION_ID]: sessionId } : { [SESSION_ID]: sessionId };
|
|
149
|
+
return {
|
|
150
|
+
context: baseContext.setValue(PROPERTIES_CONTEXT_KEY, merged),
|
|
151
|
+
sessionId
|
|
152
|
+
};
|
|
153
|
+
} catch (error) {
|
|
154
|
+
logger.warn(`Brizz MCP: failed to attach session context: ${String(error)}`);
|
|
155
|
+
return { context: baseContext, sessionId };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/internal/instrumentation/mcp/patches/attributes.ts
|
|
160
|
+
import { SpanStatusCode as SpanStatusCode2 } from "@opentelemetry/api";
|
|
161
|
+
function deriveSpanName(method, params) {
|
|
162
|
+
try {
|
|
163
|
+
if (method === METHOD_TOOLS_CALL) {
|
|
164
|
+
const name = typeof params?.["name"] === "string" ? params["name"] : "";
|
|
165
|
+
return name ? `${SPAN_NAME_TOOLS_CALL} ${name}` : SPAN_NAME_TOOLS_CALL;
|
|
166
|
+
}
|
|
167
|
+
if (method === METHOD_RESOURCES_READ) {
|
|
168
|
+
const uri = typeof params?.["uri"] === "string" ? params["uri"] : "";
|
|
169
|
+
return uri ? `${METHOD_RESOURCES_READ} ${uri}` : METHOD_RESOURCES_READ;
|
|
170
|
+
}
|
|
171
|
+
if (method === METHOD_PROMPTS_GET) {
|
|
172
|
+
const name = typeof params?.["name"] === "string" ? params["name"] : "";
|
|
173
|
+
return name ? `${METHOD_PROMPTS_GET} ${name}` : METHOD_PROMPTS_GET;
|
|
174
|
+
}
|
|
175
|
+
return method;
|
|
176
|
+
} catch {
|
|
177
|
+
return method || "mcp";
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function applyBaseAttributes(span, request) {
|
|
181
|
+
span.setAttribute(RPC_SYSTEM, RPC_SYSTEM_MCP);
|
|
182
|
+
if (request.method) {
|
|
183
|
+
span.setAttribute(MCP_METHOD_NAME, request.method);
|
|
184
|
+
}
|
|
185
|
+
if (request.id !== void 0 && request.id !== null) {
|
|
186
|
+
const id = String(request.id);
|
|
187
|
+
span.setAttribute(MCP_REQUEST_ID, id);
|
|
188
|
+
span.setAttribute(JSONRPC_REQUEST_ID, id);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function applyClientRequestAttributes(span, request, transportName) {
|
|
192
|
+
applyBaseAttributes(span, request);
|
|
193
|
+
applyMethodSpecificRequestAttributes(span, request);
|
|
194
|
+
if (transportName) {
|
|
195
|
+
const transport = normalizeTransport(transportName);
|
|
196
|
+
if (transport) {
|
|
197
|
+
span.setAttribute(NETWORK_TRANSPORT, transport);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function applyServerRequestAttributes(span, request, protocol) {
|
|
202
|
+
applyBaseAttributes(span, request);
|
|
203
|
+
applyMethodSpecificRequestAttributes(span, request);
|
|
204
|
+
if (request.method === METHOD_TOOLS_CALL) {
|
|
205
|
+
const args = request.params?.["arguments"];
|
|
206
|
+
if (args !== void 0) {
|
|
207
|
+
span.setAttribute(MCP_TOOL_ARGUMENTS, serializeForAttribute(args));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const sessionId = protocol.sessionId ?? protocol._transport?.sessionId;
|
|
211
|
+
if (sessionId) {
|
|
212
|
+
span.setAttribute(MCP_SESSION_ID, sessionId);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function applyMethodSpecificRequestAttributes(span, request) {
|
|
216
|
+
const params = request.params ?? {};
|
|
217
|
+
switch (request.method) {
|
|
218
|
+
case METHOD_TOOLS_CALL: {
|
|
219
|
+
const name = typeof params["name"] === "string" ? params["name"] : "";
|
|
220
|
+
if (name) {
|
|
221
|
+
span.setAttribute(MCP_TOOL_NAME, name);
|
|
222
|
+
span.setAttribute(GEN_AI_TOOL_NAME, name);
|
|
223
|
+
}
|
|
224
|
+
span.setAttribute(MCP_COMPONENT_TYPE, MCP_COMPONENT_TOOL);
|
|
225
|
+
span.setAttribute(GEN_AI_OPERATION_NAME, GEN_AI_OPERATION_EXECUTE_TOOL);
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
case METHOD_RESOURCES_READ: {
|
|
229
|
+
const uri = typeof params["uri"] === "string" ? params["uri"] : "";
|
|
230
|
+
if (uri) {
|
|
231
|
+
span.setAttribute(MCP_RESOURCE_URI, uri);
|
|
232
|
+
}
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
case METHOD_PROMPTS_GET: {
|
|
236
|
+
const name = typeof params["name"] === "string" ? params["name"] : "";
|
|
237
|
+
if (name) {
|
|
238
|
+
span.setAttribute(GEN_AI_PROMPT_NAME, name);
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
default:
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function applyResultAttributes(span, method, result) {
|
|
247
|
+
if (method === METHOD_INITIALIZE && result && typeof result === "object") {
|
|
248
|
+
const protocolVersion = result["protocolVersion"];
|
|
249
|
+
if (typeof protocolVersion === "string") {
|
|
250
|
+
span.setAttribute(MCP_PROTOCOL_VERSION, protocolVersion);
|
|
251
|
+
}
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (method !== METHOD_TOOLS_CALL) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const obj = result && typeof result === "object" ? result : null;
|
|
258
|
+
const content = obj?.["content"] ?? result;
|
|
259
|
+
span.setAttribute(MCP_TOOL_RESULT, serializeForAttribute(content));
|
|
260
|
+
if (obj && obj["isError"] === true) {
|
|
261
|
+
span.setAttribute(ERROR_TYPE, ERROR_TYPE_TOOL);
|
|
262
|
+
const message = extractToolErrorMessage(obj);
|
|
263
|
+
span.setStatus({ code: SpanStatusCode2.ERROR, message });
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function applyErrorAttributes(span, err) {
|
|
267
|
+
const error = err;
|
|
268
|
+
const code = error?.code;
|
|
269
|
+
if (typeof code === "number") {
|
|
270
|
+
span.setAttribute(RPC_RESPONSE_STATUS_CODE, code);
|
|
271
|
+
span.setAttribute(ERROR_TYPE, String(code));
|
|
272
|
+
} else if (error?.name === "AbortError") {
|
|
273
|
+
span.setAttribute(ERROR_TYPE, "cancelled");
|
|
274
|
+
} else if (error?.name === "TimeoutError") {
|
|
275
|
+
span.setAttribute(ERROR_TYPE, "timeout");
|
|
276
|
+
} else {
|
|
277
|
+
span.setAttribute(ERROR_TYPE, error?.constructor?.name || error?.name || "Error");
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
span.recordException(error);
|
|
281
|
+
} catch {
|
|
282
|
+
}
|
|
283
|
+
span.setStatus({
|
|
284
|
+
code: SpanStatusCode2.ERROR,
|
|
285
|
+
message: typeof error?.message === "string" ? error.message : void 0
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
function serializeForAttribute(value) {
|
|
289
|
+
const raw = rawSerialize(value);
|
|
290
|
+
if (raw.length <= MAX_ATTRIBUTE_LENGTH) {
|
|
291
|
+
return raw;
|
|
292
|
+
}
|
|
293
|
+
return raw.slice(0, MAX_ATTRIBUTE_LENGTH - TRUNCATION_SUFFIX.length) + TRUNCATION_SUFFIX;
|
|
294
|
+
}
|
|
295
|
+
function rawSerialize(value) {
|
|
296
|
+
if (value === null || value === void 0) {
|
|
297
|
+
return "";
|
|
298
|
+
}
|
|
299
|
+
if (typeof value === "string") {
|
|
300
|
+
return value;
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
return JSON.stringify(value);
|
|
304
|
+
} catch {
|
|
305
|
+
try {
|
|
306
|
+
if (value === null || value === void 0) {
|
|
307
|
+
return "";
|
|
308
|
+
}
|
|
309
|
+
const tag = Object.prototype.toString.call(value);
|
|
310
|
+
return typeof value.toString === "function" ? (
|
|
311
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
312
|
+
String(value)
|
|
313
|
+
) : tag;
|
|
314
|
+
} catch {
|
|
315
|
+
return "";
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
function extractToolErrorMessage(result) {
|
|
320
|
+
const content = result["content"];
|
|
321
|
+
if (Array.isArray(content)) {
|
|
322
|
+
for (const item of content) {
|
|
323
|
+
if (!(item && typeof item === "object")) {
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
const text = item["text"];
|
|
327
|
+
if (typeof text === "string") {
|
|
328
|
+
return text;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return void 0;
|
|
333
|
+
}
|
|
334
|
+
function normalizeTransport(ctorName) {
|
|
335
|
+
const name = ctorName.toLowerCase();
|
|
336
|
+
if (name.includes("stdio")) {
|
|
337
|
+
return "stdio";
|
|
338
|
+
}
|
|
339
|
+
if (name.includes("sse")) {
|
|
340
|
+
return "sse";
|
|
341
|
+
}
|
|
342
|
+
if (name.includes("streamablehttp") || name.includes("http")) {
|
|
343
|
+
return "http";
|
|
344
|
+
}
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// src/internal/instrumentation/mcp/patches/protocol.ts
|
|
349
|
+
var PATCHED_FLAG = /* @__PURE__ */ Symbol("brizz.mcp.protocol-patched");
|
|
350
|
+
function patchProtocolPrototype(prototype, tracer) {
|
|
351
|
+
if (!prototype || typeof prototype !== "object") {
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
const proto = prototype;
|
|
355
|
+
if (Object.hasOwn(proto, PATCHED_FLAG)) {
|
|
356
|
+
logger.debug("Brizz MCP: Protocol.prototype already patched, skipping");
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
const originalRequest = proto["request"];
|
|
360
|
+
const originalOnRequest = proto["_onrequest"];
|
|
361
|
+
if (typeof originalRequest === "function") {
|
|
362
|
+
proto["request"] = wrapRequest(originalRequest, tracer);
|
|
363
|
+
} else {
|
|
364
|
+
logger.debug(
|
|
365
|
+
"Brizz MCP: Protocol.prototype.request missing \u2014 skipping CLIENT patch"
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
if (typeof originalOnRequest === "function") {
|
|
369
|
+
proto["_onrequest"] = wrapOnRequest(
|
|
370
|
+
originalOnRequest,
|
|
371
|
+
tracer
|
|
372
|
+
);
|
|
373
|
+
} else {
|
|
374
|
+
logger.debug(
|
|
375
|
+
"Brizz MCP: Protocol.prototype._onrequest missing \u2014 skipping SERVER patch"
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
Object.defineProperty(proto, PATCHED_FLAG, {
|
|
379
|
+
value: true,
|
|
380
|
+
configurable: true,
|
|
381
|
+
writable: true
|
|
382
|
+
});
|
|
383
|
+
return true;
|
|
384
|
+
}
|
|
385
|
+
function wrapRequest(original, tracer) {
|
|
386
|
+
return function wrappedRequest(...args) {
|
|
387
|
+
const request = args[0];
|
|
388
|
+
if (!request || typeof request !== "object" || !request.method) {
|
|
389
|
+
return original.apply(this, args);
|
|
390
|
+
}
|
|
391
|
+
const span = safeStartClientSpan(tracer, request, this);
|
|
392
|
+
if (!span) {
|
|
393
|
+
return original.apply(this, args);
|
|
394
|
+
}
|
|
395
|
+
return executeAroundSpan(span, request.method, () => {
|
|
396
|
+
const ctx = trace.setSpan(context2.active(), span);
|
|
397
|
+
return context2.with(ctx, () => original.apply(this, args));
|
|
398
|
+
});
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
function wrapOnRequest(original, tracer) {
|
|
402
|
+
return function wrappedOnRequest(...args) {
|
|
403
|
+
const request = args[0];
|
|
404
|
+
if (!request || typeof request !== "object" || !request.method) {
|
|
405
|
+
return original.apply(this, args);
|
|
406
|
+
}
|
|
407
|
+
const handlers = this._requestHandlers;
|
|
408
|
+
if (!handlers || typeof handlers.get !== "function") {
|
|
409
|
+
return original.apply(this, args);
|
|
410
|
+
}
|
|
411
|
+
const method = request.method;
|
|
412
|
+
const handler = handlers.get(method) ?? this.fallbackRequestHandler;
|
|
413
|
+
if (!handler) {
|
|
414
|
+
return original.apply(this, args);
|
|
415
|
+
}
|
|
416
|
+
const started = safeStartServerSpan(tracer, request, this);
|
|
417
|
+
if (!started) {
|
|
418
|
+
return original.apply(this, args);
|
|
419
|
+
}
|
|
420
|
+
const { span, spanCtx } = started;
|
|
421
|
+
const transportSessionId = this.sessionId ?? this._transport?.sessionId;
|
|
422
|
+
const postResult = method === METHOD_TOOLS_LIST ? (result) => {
|
|
423
|
+
try {
|
|
424
|
+
emitSchemaSpansFromListResponse(result, transportSessionId, tracer);
|
|
425
|
+
} catch (error) {
|
|
426
|
+
logger.warn(
|
|
427
|
+
`Brizz MCP: failed to emit tools/list schema spans: ${String(error)}`
|
|
428
|
+
);
|
|
429
|
+
}
|
|
430
|
+
} : void 0;
|
|
431
|
+
const wrappedHandler = (req, extra) => context2.with(
|
|
432
|
+
spanCtx,
|
|
433
|
+
() => executeHandler(span, method, handler, req, extra, postResult)
|
|
434
|
+
);
|
|
435
|
+
const hadEntry = handlers.has(method);
|
|
436
|
+
const prev = handlers.get(method);
|
|
437
|
+
handlers.set(method, wrappedHandler);
|
|
438
|
+
const usedFallback = !hadEntry && this.fallbackRequestHandler === handler;
|
|
439
|
+
const fallbackPrev = usedFallback ? this.fallbackRequestHandler : void 0;
|
|
440
|
+
if (usedFallback) {
|
|
441
|
+
this.fallbackRequestHandler = wrappedHandler;
|
|
442
|
+
}
|
|
443
|
+
try {
|
|
444
|
+
return original.apply(this, args);
|
|
445
|
+
} finally {
|
|
446
|
+
if (hadEntry) {
|
|
447
|
+
handlers.set(method, prev);
|
|
448
|
+
} else {
|
|
449
|
+
handlers.delete(method);
|
|
450
|
+
}
|
|
451
|
+
if (usedFallback) {
|
|
452
|
+
this.fallbackRequestHandler = fallbackPrev;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
function executeAroundSpan(span, method, run, postResult) {
|
|
458
|
+
let result;
|
|
459
|
+
try {
|
|
460
|
+
result = run();
|
|
461
|
+
} catch (error) {
|
|
462
|
+
safeApplyErrorAttributes(span, error);
|
|
463
|
+
safeEnd(span);
|
|
464
|
+
throw error;
|
|
465
|
+
}
|
|
466
|
+
if (!isThenable(result)) {
|
|
467
|
+
safeApplyResultAttributes(span, method, result);
|
|
468
|
+
if (postResult) {
|
|
469
|
+
postResult(result);
|
|
470
|
+
}
|
|
471
|
+
safeEnd(span);
|
|
472
|
+
return result;
|
|
473
|
+
}
|
|
474
|
+
return result.then(
|
|
475
|
+
(value) => {
|
|
476
|
+
safeApplyResultAttributes(span, method, value);
|
|
477
|
+
if (postResult) {
|
|
478
|
+
postResult(value);
|
|
479
|
+
}
|
|
480
|
+
safeEnd(span);
|
|
481
|
+
return value;
|
|
482
|
+
},
|
|
483
|
+
(error) => {
|
|
484
|
+
safeApplyErrorAttributes(span, error);
|
|
485
|
+
safeEnd(span);
|
|
486
|
+
throw error;
|
|
487
|
+
}
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
function executeHandler(span, method, handler, req, extra, postResult) {
|
|
491
|
+
return executeAroundSpan(span, method, () => handler(req, extra), postResult);
|
|
492
|
+
}
|
|
493
|
+
function safeStartClientSpan(tracer, request, protocol) {
|
|
494
|
+
try {
|
|
495
|
+
const spanName = deriveSpanName(request.method, request.params);
|
|
496
|
+
const span = tracer.startSpan(spanName, { kind: SpanKind2.CLIENT });
|
|
497
|
+
applyClientRequestAttributes(
|
|
498
|
+
span,
|
|
499
|
+
request,
|
|
500
|
+
protocol._transport?.constructor?.name
|
|
501
|
+
);
|
|
502
|
+
return span;
|
|
503
|
+
} catch (error) {
|
|
504
|
+
logger.debug(`Brizz MCP: failed to open CLIENT span: ${String(error)}`);
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
function safeStartServerSpan(tracer, request, protocol) {
|
|
509
|
+
try {
|
|
510
|
+
const parentCtx = extractParentContext(request);
|
|
511
|
+
const spanName = deriveSpanName(request.method, request.params);
|
|
512
|
+
const span = tracer.startSpan(
|
|
513
|
+
spanName,
|
|
514
|
+
{ kind: SpanKind2.SERVER },
|
|
515
|
+
parentCtx
|
|
516
|
+
);
|
|
517
|
+
applyServerRequestAttributes(span, request, protocol);
|
|
518
|
+
const sessionId = protocol.sessionId ?? protocol._transport?.sessionId;
|
|
519
|
+
const { context: sessCtx } = stampAndPropagateSession(span, sessionId, parentCtx);
|
|
520
|
+
return { span, spanCtx: trace.setSpan(sessCtx, span) };
|
|
521
|
+
} catch (error) {
|
|
522
|
+
logger.debug(`Brizz MCP: failed to open SERVER span: ${String(error)}`);
|
|
523
|
+
return null;
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
function extractParentContext(request) {
|
|
527
|
+
try {
|
|
528
|
+
const meta = request.params?._meta;
|
|
529
|
+
if (meta && typeof meta === "object") {
|
|
530
|
+
return propagation.extract(context2.active(), meta);
|
|
531
|
+
}
|
|
532
|
+
} catch (error) {
|
|
533
|
+
logger.debug(
|
|
534
|
+
`Brizz MCP: failed to extract parent context from _meta: ${String(error)}`
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
return context2.active();
|
|
538
|
+
}
|
|
539
|
+
function isThenable(value) {
|
|
540
|
+
return !!value && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
|
|
541
|
+
}
|
|
542
|
+
function safeApplyResultAttributes(span, method, value) {
|
|
543
|
+
try {
|
|
544
|
+
applyResultAttributes(span, method, value);
|
|
545
|
+
} catch (error) {
|
|
546
|
+
logger.debug(
|
|
547
|
+
`Brizz MCP: failed to apply result attributes: ${String(error)}`
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
function safeApplyErrorAttributes(span, err) {
|
|
552
|
+
try {
|
|
553
|
+
applyErrorAttributes(span, err);
|
|
554
|
+
} catch (error) {
|
|
555
|
+
logger.debug(
|
|
556
|
+
`Brizz MCP: failed to apply error attributes: ${String(error)}`
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
function safeEnd(span) {
|
|
561
|
+
try {
|
|
562
|
+
span.end();
|
|
563
|
+
} catch (error) {
|
|
564
|
+
logger.debug(`Brizz MCP: failed to end span: ${String(error)}`);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// src/internal/instrumentation/mcp/version.ts
|
|
569
|
+
var INSTRUMENTATION_NAME = "@brizz/sdk/mcp";
|
|
570
|
+
var INSTRUMENTATION_VERSION = getSDKVersion();
|
|
571
|
+
|
|
572
|
+
// src/internal/instrumentation/mcp/instrumentation.ts
|
|
573
|
+
var PROTOCOL_MODULE_NAME = "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
574
|
+
var PROTOCOL_SUPPORTED_VERSIONS = [">=1.0.0 <2"];
|
|
575
|
+
var MCPInstrumentation = class extends BaseMCPInstrumentation {
|
|
576
|
+
/**
|
|
577
|
+
* Dedicated Brizz-named tracer for our SERVER + CLIENT spans.
|
|
578
|
+
*
|
|
579
|
+
* Why a separate tracer from the parent class's one: Arize's inherited
|
|
580
|
+
* transport patches don't emit spans (they only inject propagation
|
|
581
|
+
* headers), so using our own tracer here keeps Brizz spans attributed to
|
|
582
|
+
* `@brizz/sdk/mcp` in the dashboard. The parent's `tracer` is a
|
|
583
|
+
* `protected get` (no setter), so fighting it with assignment is the
|
|
584
|
+
* wrong tool.
|
|
585
|
+
*/
|
|
586
|
+
brizzTracer;
|
|
587
|
+
constructor(_config) {
|
|
588
|
+
super({ instrumentationConfig: _config?.instrumentationConfig });
|
|
589
|
+
this.brizzTracer = trace2.getTracer(INSTRUMENTATION_NAME, INSTRUMENTATION_VERSION);
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Apply the SERVER + CLIENT patch to a loaded `protocol.js` module. Shared
|
|
593
|
+
* by both the automatic module-load callback in `init()` and the manual
|
|
594
|
+
* `manuallyInstrument(...)` path below so there's exactly one place that
|
|
595
|
+
* calls `patchProtocolPrototype`.
|
|
596
|
+
*/
|
|
597
|
+
patchProtocolModule(module) {
|
|
598
|
+
const proto = module?.Protocol?.prototype;
|
|
599
|
+
if (!proto) {
|
|
600
|
+
logger.debug(
|
|
601
|
+
"Brizz MCP: module does not expose Protocol.prototype \u2014 skipping protocol patches"
|
|
602
|
+
);
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
const ok = patchProtocolPrototype(proto, this.brizzTracer);
|
|
606
|
+
if (ok) {
|
|
607
|
+
logger.debug("Brizz MCP: patched Protocol.prototype for SERVER+CLIENT spans");
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Unpatch is intentionally a no-op.
|
|
612
|
+
*
|
|
613
|
+
* `patchProtocolPrototype` wraps methods in place and sets a PATCHED_FLAG
|
|
614
|
+
* Symbol on the prototype to prevent double-patching. Restoring the
|
|
615
|
+
* originals would require us to hold references across module unloads,
|
|
616
|
+
* which isn't a pattern we need — instrumentation rarely gets torn down
|
|
617
|
+
* at runtime, and a process reload is the canonical way to detach.
|
|
618
|
+
*/
|
|
619
|
+
unpatchProtocolModule(_module) {
|
|
620
|
+
logger.debug(
|
|
621
|
+
"Brizz MCP: unpatch is a no-op \u2014 reload the process to cleanly detach"
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Extend `super.init()` with our protocol-layer module definition.
|
|
626
|
+
*
|
|
627
|
+
* Arize's `init()` returns definitions for the six transport modules
|
|
628
|
+
* (SSE client/server, stdio client/server, streamable-HTTP client/server)
|
|
629
|
+
* whose `send`/`start` methods get patched for W3C trace-context
|
|
630
|
+
* injection. We append one more: `@modelcontextprotocol/sdk/shared/protocol.js`
|
|
631
|
+
* where our patches open SERVER/CLIENT spans around the handler + outgoing
|
|
632
|
+
* request. If `super.init()` throws (unlikely but possible across Arize
|
|
633
|
+
* versions), we gracefully degrade to protocol-only instrumentation.
|
|
634
|
+
*
|
|
635
|
+
* The cast at the end satisfies the parent's strongly-typed generic
|
|
636
|
+
* without leaking the cast into call sites.
|
|
637
|
+
*/
|
|
638
|
+
init() {
|
|
639
|
+
let base;
|
|
640
|
+
try {
|
|
641
|
+
base = super.init() ?? [];
|
|
642
|
+
} catch (error) {
|
|
643
|
+
logger.warn(
|
|
644
|
+
`Brizz MCP: base Arize init() failed \u2014 transport context propagation disabled: ${String(error)}`
|
|
645
|
+
);
|
|
646
|
+
base = [];
|
|
647
|
+
}
|
|
648
|
+
const baseArr = Array.isArray(base) ? base : [base];
|
|
649
|
+
const brizzDef = new InstrumentationNodeModuleDefinition(
|
|
650
|
+
PROTOCOL_MODULE_NAME,
|
|
651
|
+
PROTOCOL_SUPPORTED_VERSIONS,
|
|
652
|
+
(module) => {
|
|
653
|
+
this.patchProtocolModule(module);
|
|
654
|
+
return module;
|
|
655
|
+
},
|
|
656
|
+
(module) => {
|
|
657
|
+
this.unpatchProtocolModule(module);
|
|
658
|
+
return module;
|
|
659
|
+
}
|
|
660
|
+
);
|
|
661
|
+
return [...baseArr, brizzDef];
|
|
662
|
+
}
|
|
663
|
+
/**
|
|
664
|
+
* Manually instrument MCP modules for Next.js/Webpack where the
|
|
665
|
+
* auto-instrumentation module-load hook doesn't fire.
|
|
666
|
+
*
|
|
667
|
+
* Forwards the six transport module fields to the inherited Arize
|
|
668
|
+
* `manuallyInstrument(...)` (context propagation) and applies our
|
|
669
|
+
* protocol patch if `protocolModule` is provided (SERVER/CLIENT spans).
|
|
670
|
+
* Both legs are try/catch-guarded — a failure in one shouldn't prevent
|
|
671
|
+
* the other from taking effect.
|
|
672
|
+
*/
|
|
673
|
+
manuallyInstrument(modules) {
|
|
674
|
+
const {
|
|
675
|
+
clientSSEModule,
|
|
676
|
+
serverSSEModule,
|
|
677
|
+
clientStdioModule,
|
|
678
|
+
serverStdioModule,
|
|
679
|
+
clientStreamableHTTPModule,
|
|
680
|
+
serverStreamableHTTPModule,
|
|
681
|
+
protocolModule
|
|
682
|
+
} = modules;
|
|
683
|
+
try {
|
|
684
|
+
super.manuallyInstrument({
|
|
685
|
+
clientSSEModule,
|
|
686
|
+
serverSSEModule,
|
|
687
|
+
clientStdioModule,
|
|
688
|
+
serverStdioModule,
|
|
689
|
+
clientStreamableHTTPModule,
|
|
690
|
+
serverStreamableHTTPModule
|
|
691
|
+
});
|
|
692
|
+
} catch (error) {
|
|
693
|
+
logger.warn(`Brizz MCP: Arize manuallyInstrument(...) failed: ${String(error)}`);
|
|
694
|
+
}
|
|
695
|
+
if (protocolModule) {
|
|
696
|
+
try {
|
|
697
|
+
this.patchProtocolModule(protocolModule);
|
|
698
|
+
} catch (error) {
|
|
699
|
+
logger.warn(
|
|
700
|
+
`Brizz MCP: failed to manually patch Protocol module: ${String(error)}`
|
|
701
|
+
);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
function isMCPInstrumentation(value) {
|
|
707
|
+
return !!value && typeof value === "object" && typeof value.instrumentationName === "string";
|
|
708
|
+
}
|
|
709
|
+
export {
|
|
710
|
+
MCPInstrumentation,
|
|
711
|
+
isMCPInstrumentation
|
|
712
|
+
};
|