@brizz/sdk 0.1.20 → 0.1.22
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/dist/index.cjs +919 -191
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +13 -1
- package/dist/index.d.ts +13 -1
- package/dist/index.js +892 -161
- package/dist/index.js.map +1 -1
- package/dist/preload.cjs +626 -18
- package/dist/preload.cjs.map +1 -1
- package/dist/preload.js +626 -18
- package/dist/preload.js.map +1 -1
- package/package.json +6 -5
package/dist/preload.cjs
CHANGED
|
@@ -258,6 +258,618 @@ var import_instrumentation_pinecone = require("@traceloop/instrumentation-pineco
|
|
|
258
258
|
var import_instrumentation_qdrant = require("@traceloop/instrumentation-qdrant");
|
|
259
259
|
var import_instrumentation_together = require("@traceloop/instrumentation-together");
|
|
260
260
|
var import_instrumentation_vertexai = require("@traceloop/instrumentation-vertexai");
|
|
261
|
+
|
|
262
|
+
// src/internal/instrumentation/mcp/instrumentation.ts
|
|
263
|
+
var import_openinference_instrumentation_mcp = require("@arizeai/openinference-instrumentation-mcp");
|
|
264
|
+
var import_api6 = require("@opentelemetry/api");
|
|
265
|
+
var import_instrumentation = require("@opentelemetry/instrumentation");
|
|
266
|
+
|
|
267
|
+
// src/internal/instrumentation/mcp/patches/protocol.ts
|
|
268
|
+
var import_api5 = require("@opentelemetry/api");
|
|
269
|
+
|
|
270
|
+
// src/internal/instrumentation/mcp/session.ts
|
|
271
|
+
var import_api3 = require("@opentelemetry/api");
|
|
272
|
+
|
|
273
|
+
// src/internal/semantic-conventions.ts
|
|
274
|
+
var import_api2 = require("@opentelemetry/api");
|
|
275
|
+
var BRIZZ = "brizz";
|
|
276
|
+
var PROPERTIES = "properties";
|
|
277
|
+
var SESSION_ID = "session.id";
|
|
278
|
+
var PROPERTIES_CONTEXT_KEY = (0, import_api2.createContextKey)(PROPERTIES);
|
|
279
|
+
var SESSION_OBJECT_CONTEXT_KEY = (0, import_api2.createContextKey)("brizz.session.object");
|
|
280
|
+
|
|
281
|
+
// src/internal/instrumentation/mcp/session.ts
|
|
282
|
+
function stampAndPropagateSession(span, sessionId, baseContext = import_api3.context.active()) {
|
|
283
|
+
if (!sessionId) {
|
|
284
|
+
return { context: baseContext, sessionId: null };
|
|
285
|
+
}
|
|
286
|
+
if (span.isRecording()) {
|
|
287
|
+
try {
|
|
288
|
+
span.setAttribute(`${BRIZZ}.${SESSION_ID}`, sessionId);
|
|
289
|
+
} catch (error) {
|
|
290
|
+
logger.warn(
|
|
291
|
+
`Brizz MCP: failed to stamp session id on span: ${String(error)}`
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
try {
|
|
296
|
+
const prev = baseContext.getValue(PROPERTIES_CONTEXT_KEY);
|
|
297
|
+
const merged = prev ? { ...prev, [SESSION_ID]: sessionId } : { [SESSION_ID]: sessionId };
|
|
298
|
+
return {
|
|
299
|
+
context: baseContext.setValue(PROPERTIES_CONTEXT_KEY, merged),
|
|
300
|
+
sessionId
|
|
301
|
+
};
|
|
302
|
+
} catch (error) {
|
|
303
|
+
logger.warn(`Brizz MCP: failed to attach session context: ${String(error)}`);
|
|
304
|
+
return { context: baseContext, sessionId };
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// src/internal/instrumentation/mcp/patches/attributes.ts
|
|
309
|
+
var import_api4 = require("@opentelemetry/api");
|
|
310
|
+
|
|
311
|
+
// src/internal/instrumentation/mcp/semantic-conventions.ts
|
|
312
|
+
var MCP_TOOL_NAME = "mcp.tool.name";
|
|
313
|
+
var MCP_TOOL_ARGUMENTS = "mcp.tool.arguments";
|
|
314
|
+
var MCP_TOOL_RESULT = "mcp.tool.result";
|
|
315
|
+
var MCP_COMPONENT_TYPE = "mcp.component.type";
|
|
316
|
+
var MCP_COMPONENT_TOOL = "tool";
|
|
317
|
+
var MCP_METHOD_NAME = "mcp.method.name";
|
|
318
|
+
var MCP_REQUEST_ID = "mcp.request.id";
|
|
319
|
+
var MCP_SESSION_ID = "mcp.session.id";
|
|
320
|
+
var MCP_PROTOCOL_VERSION = "mcp.protocol.version";
|
|
321
|
+
var MCP_RESOURCE_URI = "mcp.resource.uri";
|
|
322
|
+
var RPC_SYSTEM = "rpc.system";
|
|
323
|
+
var RPC_SYSTEM_MCP = "mcp";
|
|
324
|
+
var RPC_RESPONSE_STATUS_CODE = "rpc.response.status_code";
|
|
325
|
+
var GEN_AI_TOOL_NAME = "gen_ai.tool.name";
|
|
326
|
+
var GEN_AI_PROMPT_NAME = "gen_ai.prompt.name";
|
|
327
|
+
var GEN_AI_OPERATION_NAME = "gen_ai.operation.name";
|
|
328
|
+
var GEN_AI_OPERATION_EXECUTE_TOOL = "execute_tool";
|
|
329
|
+
var NETWORK_TRANSPORT = "network.transport";
|
|
330
|
+
var ERROR_TYPE = "error.type";
|
|
331
|
+
var ERROR_TYPE_TOOL = "tool_error";
|
|
332
|
+
var JSONRPC_REQUEST_ID = "jsonrpc.request.id";
|
|
333
|
+
var SPAN_NAME_TOOLS_CALL = "tools/call";
|
|
334
|
+
var MAX_ATTRIBUTE_LENGTH = 32 * 1024;
|
|
335
|
+
var TRUNCATION_SUFFIX = "\u2026(truncated)";
|
|
336
|
+
var METHOD_TOOLS_CALL = "tools/call";
|
|
337
|
+
var METHOD_RESOURCES_READ = "resources/read";
|
|
338
|
+
var METHOD_PROMPTS_GET = "prompts/get";
|
|
339
|
+
var METHOD_INITIALIZE = "initialize";
|
|
340
|
+
|
|
341
|
+
// src/internal/instrumentation/mcp/patches/attributes.ts
|
|
342
|
+
function deriveSpanName(method, params) {
|
|
343
|
+
try {
|
|
344
|
+
if (method === METHOD_TOOLS_CALL) {
|
|
345
|
+
const name = typeof params?.["name"] === "string" ? params["name"] : "";
|
|
346
|
+
return name ? `${SPAN_NAME_TOOLS_CALL} ${name}` : SPAN_NAME_TOOLS_CALL;
|
|
347
|
+
}
|
|
348
|
+
if (method === METHOD_RESOURCES_READ) {
|
|
349
|
+
const uri = typeof params?.["uri"] === "string" ? params["uri"] : "";
|
|
350
|
+
return uri ? `${METHOD_RESOURCES_READ} ${uri}` : METHOD_RESOURCES_READ;
|
|
351
|
+
}
|
|
352
|
+
if (method === METHOD_PROMPTS_GET) {
|
|
353
|
+
const name = typeof params?.["name"] === "string" ? params["name"] : "";
|
|
354
|
+
return name ? `${METHOD_PROMPTS_GET} ${name}` : METHOD_PROMPTS_GET;
|
|
355
|
+
}
|
|
356
|
+
return method;
|
|
357
|
+
} catch {
|
|
358
|
+
return method || "mcp";
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
function applyBaseAttributes(span, request) {
|
|
362
|
+
span.setAttribute(RPC_SYSTEM, RPC_SYSTEM_MCP);
|
|
363
|
+
if (request.method) {
|
|
364
|
+
span.setAttribute(MCP_METHOD_NAME, request.method);
|
|
365
|
+
}
|
|
366
|
+
if (request.id !== void 0 && request.id !== null) {
|
|
367
|
+
const id = String(request.id);
|
|
368
|
+
span.setAttribute(MCP_REQUEST_ID, id);
|
|
369
|
+
span.setAttribute(JSONRPC_REQUEST_ID, id);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
function applyClientRequestAttributes(span, request, transportName) {
|
|
373
|
+
applyBaseAttributes(span, request);
|
|
374
|
+
applyMethodSpecificRequestAttributes(span, request);
|
|
375
|
+
if (transportName) {
|
|
376
|
+
const transport = normalizeTransport(transportName);
|
|
377
|
+
if (transport) {
|
|
378
|
+
span.setAttribute(NETWORK_TRANSPORT, transport);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function applyServerRequestAttributes(span, request, protocol) {
|
|
383
|
+
applyBaseAttributes(span, request);
|
|
384
|
+
applyMethodSpecificRequestAttributes(span, request);
|
|
385
|
+
if (request.method === METHOD_TOOLS_CALL) {
|
|
386
|
+
const args = request.params?.["arguments"];
|
|
387
|
+
if (args !== void 0) {
|
|
388
|
+
span.setAttribute(MCP_TOOL_ARGUMENTS, serializeForAttribute(args));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
const sessionId = protocol.sessionId ?? protocol._transport?.sessionId;
|
|
392
|
+
if (sessionId) {
|
|
393
|
+
span.setAttribute(MCP_SESSION_ID, String(sessionId));
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
function applyMethodSpecificRequestAttributes(span, request) {
|
|
397
|
+
const params = request.params ?? {};
|
|
398
|
+
switch (request.method) {
|
|
399
|
+
case METHOD_TOOLS_CALL: {
|
|
400
|
+
const name = typeof params["name"] === "string" ? params["name"] : "";
|
|
401
|
+
if (name) {
|
|
402
|
+
span.setAttribute(MCP_TOOL_NAME, name);
|
|
403
|
+
span.setAttribute(GEN_AI_TOOL_NAME, name);
|
|
404
|
+
}
|
|
405
|
+
span.setAttribute(MCP_COMPONENT_TYPE, MCP_COMPONENT_TOOL);
|
|
406
|
+
span.setAttribute(GEN_AI_OPERATION_NAME, GEN_AI_OPERATION_EXECUTE_TOOL);
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
case METHOD_RESOURCES_READ: {
|
|
410
|
+
const uri = typeof params["uri"] === "string" ? params["uri"] : "";
|
|
411
|
+
if (uri) {
|
|
412
|
+
span.setAttribute(MCP_RESOURCE_URI, uri);
|
|
413
|
+
}
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
case METHOD_PROMPTS_GET: {
|
|
417
|
+
const name = typeof params["name"] === "string" ? params["name"] : "";
|
|
418
|
+
if (name) {
|
|
419
|
+
span.setAttribute(GEN_AI_PROMPT_NAME, name);
|
|
420
|
+
}
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
default:
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
function applyResultAttributes(span, method, result) {
|
|
428
|
+
if (method === METHOD_INITIALIZE && result && typeof result === "object") {
|
|
429
|
+
const protocolVersion = result["protocolVersion"];
|
|
430
|
+
if (typeof protocolVersion === "string") {
|
|
431
|
+
span.setAttribute(MCP_PROTOCOL_VERSION, protocolVersion);
|
|
432
|
+
}
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (method !== METHOD_TOOLS_CALL) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
const obj = result && typeof result === "object" ? result : null;
|
|
439
|
+
const content = obj?.["content"] ?? result;
|
|
440
|
+
span.setAttribute(MCP_TOOL_RESULT, serializeForAttribute(content));
|
|
441
|
+
if (obj && obj["isError"] === true) {
|
|
442
|
+
span.setAttribute(ERROR_TYPE, ERROR_TYPE_TOOL);
|
|
443
|
+
const message = extractToolErrorMessage(obj);
|
|
444
|
+
span.setStatus({ code: import_api4.SpanStatusCode.ERROR, message });
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function applyErrorAttributes(span, err) {
|
|
448
|
+
const error = err;
|
|
449
|
+
const code = error?.code;
|
|
450
|
+
if (typeof code === "number") {
|
|
451
|
+
span.setAttribute(RPC_RESPONSE_STATUS_CODE, code);
|
|
452
|
+
span.setAttribute(ERROR_TYPE, String(code));
|
|
453
|
+
} else if (error?.name === "AbortError") {
|
|
454
|
+
span.setAttribute(ERROR_TYPE, "cancelled");
|
|
455
|
+
} else if (error?.name === "TimeoutError") {
|
|
456
|
+
span.setAttribute(ERROR_TYPE, "timeout");
|
|
457
|
+
} else {
|
|
458
|
+
span.setAttribute(
|
|
459
|
+
ERROR_TYPE,
|
|
460
|
+
error?.constructor?.name || error?.name || "Error"
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
try {
|
|
464
|
+
span.recordException(error);
|
|
465
|
+
} catch {
|
|
466
|
+
}
|
|
467
|
+
span.setStatus({
|
|
468
|
+
code: import_api4.SpanStatusCode.ERROR,
|
|
469
|
+
message: typeof error?.message === "string" ? error.message : void 0
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
function serializeForAttribute(value) {
|
|
473
|
+
const raw = rawSerialize(value);
|
|
474
|
+
if (raw.length <= MAX_ATTRIBUTE_LENGTH) {
|
|
475
|
+
return raw;
|
|
476
|
+
}
|
|
477
|
+
return raw.slice(0, MAX_ATTRIBUTE_LENGTH - TRUNCATION_SUFFIX.length) + TRUNCATION_SUFFIX;
|
|
478
|
+
}
|
|
479
|
+
function rawSerialize(value) {
|
|
480
|
+
if (value === null || value === void 0) {
|
|
481
|
+
return "";
|
|
482
|
+
}
|
|
483
|
+
if (typeof value === "string") {
|
|
484
|
+
return value;
|
|
485
|
+
}
|
|
486
|
+
try {
|
|
487
|
+
return JSON.stringify(value);
|
|
488
|
+
} catch {
|
|
489
|
+
try {
|
|
490
|
+
if (value === null || value === void 0) {
|
|
491
|
+
return "";
|
|
492
|
+
}
|
|
493
|
+
const tag = Object.prototype.toString.call(value);
|
|
494
|
+
return typeof value.toString === "function" ? (
|
|
495
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
496
|
+
String(value)
|
|
497
|
+
) : tag;
|
|
498
|
+
} catch {
|
|
499
|
+
return "";
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
function extractToolErrorMessage(result) {
|
|
504
|
+
const content = result["content"];
|
|
505
|
+
if (Array.isArray(content)) {
|
|
506
|
+
for (const item of content) {
|
|
507
|
+
if (item && typeof item === "object") {
|
|
508
|
+
const text = item["text"];
|
|
509
|
+
if (typeof text === "string") {
|
|
510
|
+
return text;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return void 0;
|
|
516
|
+
}
|
|
517
|
+
function normalizeTransport(ctorName) {
|
|
518
|
+
const name = ctorName.toLowerCase();
|
|
519
|
+
if (name.includes("stdio")) {
|
|
520
|
+
return "stdio";
|
|
521
|
+
}
|
|
522
|
+
if (name.includes("sse")) {
|
|
523
|
+
return "sse";
|
|
524
|
+
}
|
|
525
|
+
if (name.includes("streamablehttp") || name.includes("http")) {
|
|
526
|
+
return "http";
|
|
527
|
+
}
|
|
528
|
+
return null;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// src/internal/instrumentation/mcp/patches/protocol.ts
|
|
532
|
+
var PATCHED_FLAG = /* @__PURE__ */ Symbol("brizz.mcp.protocol-patched");
|
|
533
|
+
function patchProtocolPrototype(prototype, tracer) {
|
|
534
|
+
if (!prototype || typeof prototype !== "object") {
|
|
535
|
+
return false;
|
|
536
|
+
}
|
|
537
|
+
const proto = prototype;
|
|
538
|
+
if (proto[PATCHED_FLAG]) {
|
|
539
|
+
logger.debug("Brizz MCP: Protocol.prototype already patched, skipping");
|
|
540
|
+
return false;
|
|
541
|
+
}
|
|
542
|
+
const originalRequest = proto["request"];
|
|
543
|
+
const originalOnRequest = proto["_onrequest"];
|
|
544
|
+
if (typeof originalRequest === "function") {
|
|
545
|
+
proto["request"] = wrapRequest(originalRequest, tracer);
|
|
546
|
+
} else {
|
|
547
|
+
logger.debug(
|
|
548
|
+
"Brizz MCP: Protocol.prototype.request missing \u2014 skipping CLIENT patch"
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
if (typeof originalOnRequest === "function") {
|
|
552
|
+
proto["_onrequest"] = wrapOnRequest(
|
|
553
|
+
originalOnRequest,
|
|
554
|
+
tracer
|
|
555
|
+
);
|
|
556
|
+
} else {
|
|
557
|
+
logger.debug(
|
|
558
|
+
"Brizz MCP: Protocol.prototype._onrequest missing \u2014 skipping SERVER patch"
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
proto[PATCHED_FLAG] = true;
|
|
562
|
+
return true;
|
|
563
|
+
}
|
|
564
|
+
function wrapRequest(original, tracer) {
|
|
565
|
+
return function wrappedRequest(...args) {
|
|
566
|
+
const request = args[0];
|
|
567
|
+
if (!request || typeof request !== "object" || !request.method) {
|
|
568
|
+
return original.apply(this, args);
|
|
569
|
+
}
|
|
570
|
+
const span = safeStartClientSpan(tracer, request, this);
|
|
571
|
+
if (!span) {
|
|
572
|
+
return original.apply(this, args);
|
|
573
|
+
}
|
|
574
|
+
return executeAroundSpan(span, request.method, () => {
|
|
575
|
+
const ctx = import_api5.trace.setSpan(import_api5.context.active(), span);
|
|
576
|
+
return import_api5.context.with(ctx, () => original.apply(this, args));
|
|
577
|
+
});
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
function wrapOnRequest(original, tracer) {
|
|
581
|
+
return function wrappedOnRequest(...args) {
|
|
582
|
+
const request = args[0];
|
|
583
|
+
if (!request || typeof request !== "object" || !request.method) {
|
|
584
|
+
return original.apply(this, args);
|
|
585
|
+
}
|
|
586
|
+
const handlers = this._requestHandlers;
|
|
587
|
+
if (!handlers || typeof handlers.get !== "function") {
|
|
588
|
+
return original.apply(this, args);
|
|
589
|
+
}
|
|
590
|
+
const method = request.method;
|
|
591
|
+
const handler = handlers.get(method) ?? this.fallbackRequestHandler;
|
|
592
|
+
if (!handler) {
|
|
593
|
+
return original.apply(this, args);
|
|
594
|
+
}
|
|
595
|
+
const started = safeStartServerSpan(tracer, request, this);
|
|
596
|
+
if (!started) {
|
|
597
|
+
return original.apply(this, args);
|
|
598
|
+
}
|
|
599
|
+
const { span, spanCtx } = started;
|
|
600
|
+
const wrappedHandler = (req, extra) => import_api5.context.with(spanCtx, () => executeHandler(span, method, handler, req, extra));
|
|
601
|
+
const hadEntry = handlers.has(method);
|
|
602
|
+
const prev = handlers.get(method);
|
|
603
|
+
handlers.set(method, wrappedHandler);
|
|
604
|
+
const usedFallback = !hadEntry && this.fallbackRequestHandler === handler;
|
|
605
|
+
const fallbackPrev = usedFallback ? this.fallbackRequestHandler : void 0;
|
|
606
|
+
if (usedFallback) {
|
|
607
|
+
this.fallbackRequestHandler = wrappedHandler;
|
|
608
|
+
}
|
|
609
|
+
try {
|
|
610
|
+
return original.apply(this, args);
|
|
611
|
+
} finally {
|
|
612
|
+
if (hadEntry) {
|
|
613
|
+
handlers.set(method, prev);
|
|
614
|
+
} else {
|
|
615
|
+
handlers.delete(method);
|
|
616
|
+
}
|
|
617
|
+
if (usedFallback) {
|
|
618
|
+
this.fallbackRequestHandler = fallbackPrev;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
function executeAroundSpan(span, method, run) {
|
|
624
|
+
let result;
|
|
625
|
+
try {
|
|
626
|
+
result = run();
|
|
627
|
+
} catch (error) {
|
|
628
|
+
safeApplyErrorAttributes(span, error);
|
|
629
|
+
safeEnd(span);
|
|
630
|
+
throw error;
|
|
631
|
+
}
|
|
632
|
+
if (!isThenable(result)) {
|
|
633
|
+
safeApplyResultAttributes(span, method, result);
|
|
634
|
+
safeEnd(span);
|
|
635
|
+
return result;
|
|
636
|
+
}
|
|
637
|
+
return result.then(
|
|
638
|
+
(value) => {
|
|
639
|
+
safeApplyResultAttributes(span, method, value);
|
|
640
|
+
safeEnd(span);
|
|
641
|
+
return value;
|
|
642
|
+
},
|
|
643
|
+
(error) => {
|
|
644
|
+
safeApplyErrorAttributes(span, error);
|
|
645
|
+
safeEnd(span);
|
|
646
|
+
throw error;
|
|
647
|
+
}
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
function executeHandler(span, method, handler, req, extra) {
|
|
651
|
+
return executeAroundSpan(span, method, () => handler(req, extra));
|
|
652
|
+
}
|
|
653
|
+
function safeStartClientSpan(tracer, request, protocol) {
|
|
654
|
+
try {
|
|
655
|
+
const spanName = deriveSpanName(request.method, request.params);
|
|
656
|
+
const span = tracer.startSpan(spanName, { kind: import_api5.SpanKind.CLIENT });
|
|
657
|
+
applyClientRequestAttributes(
|
|
658
|
+
span,
|
|
659
|
+
request,
|
|
660
|
+
protocol._transport?.constructor?.name
|
|
661
|
+
);
|
|
662
|
+
return span;
|
|
663
|
+
} catch (error) {
|
|
664
|
+
logger.debug(`Brizz MCP: failed to open CLIENT span: ${String(error)}`);
|
|
665
|
+
return null;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
function safeStartServerSpan(tracer, request, protocol) {
|
|
669
|
+
try {
|
|
670
|
+
const parentCtx = extractParentContext(request);
|
|
671
|
+
const spanName = deriveSpanName(request.method, request.params);
|
|
672
|
+
const span = tracer.startSpan(
|
|
673
|
+
spanName,
|
|
674
|
+
{ kind: import_api5.SpanKind.SERVER },
|
|
675
|
+
parentCtx
|
|
676
|
+
);
|
|
677
|
+
applyServerRequestAttributes(span, request, protocol);
|
|
678
|
+
const sessionId = protocol.sessionId ?? protocol._transport?.sessionId;
|
|
679
|
+
const { context: sessCtx } = stampAndPropagateSession(span, sessionId, parentCtx);
|
|
680
|
+
return { span, spanCtx: import_api5.trace.setSpan(sessCtx, span) };
|
|
681
|
+
} catch (error) {
|
|
682
|
+
logger.debug(`Brizz MCP: failed to open SERVER span: ${String(error)}`);
|
|
683
|
+
return null;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
function extractParentContext(request) {
|
|
687
|
+
try {
|
|
688
|
+
const meta = request.params?._meta;
|
|
689
|
+
if (meta && typeof meta === "object") {
|
|
690
|
+
return import_api5.propagation.extract(import_api5.context.active(), meta);
|
|
691
|
+
}
|
|
692
|
+
} catch (error) {
|
|
693
|
+
logger.debug(
|
|
694
|
+
`Brizz MCP: failed to extract parent context from _meta: ${String(error)}`
|
|
695
|
+
);
|
|
696
|
+
}
|
|
697
|
+
return import_api5.context.active();
|
|
698
|
+
}
|
|
699
|
+
function isThenable(value) {
|
|
700
|
+
return !!value && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
|
|
701
|
+
}
|
|
702
|
+
function safeApplyResultAttributes(span, method, value) {
|
|
703
|
+
try {
|
|
704
|
+
applyResultAttributes(span, method, value);
|
|
705
|
+
} catch (error) {
|
|
706
|
+
logger.debug(
|
|
707
|
+
`Brizz MCP: failed to apply result attributes: ${String(error)}`
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
function safeApplyErrorAttributes(span, err) {
|
|
712
|
+
try {
|
|
713
|
+
applyErrorAttributes(span, err);
|
|
714
|
+
} catch (error) {
|
|
715
|
+
logger.debug(
|
|
716
|
+
`Brizz MCP: failed to apply error attributes: ${String(error)}`
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
function safeEnd(span) {
|
|
721
|
+
try {
|
|
722
|
+
span.end();
|
|
723
|
+
} catch (error) {
|
|
724
|
+
logger.debug(`Brizz MCP: failed to end span: ${String(error)}`);
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
// src/internal/version.ts
|
|
729
|
+
function getSDKVersion() {
|
|
730
|
+
return "0.1.22";
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
// src/internal/instrumentation/mcp/version.ts
|
|
734
|
+
var INSTRUMENTATION_NAME = "@brizz/sdk/mcp";
|
|
735
|
+
var INSTRUMENTATION_VERSION = getSDKVersion();
|
|
736
|
+
|
|
737
|
+
// src/internal/instrumentation/mcp/instrumentation.ts
|
|
738
|
+
var PROTOCOL_MODULE_NAME = "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
739
|
+
var PROTOCOL_SUPPORTED_VERSIONS = [">=1.0.0 <2"];
|
|
740
|
+
var MCPInstrumentation = class extends import_openinference_instrumentation_mcp.MCPInstrumentation {
|
|
741
|
+
/**
|
|
742
|
+
* Dedicated Brizz-named tracer for our SERVER + CLIENT spans.
|
|
743
|
+
*
|
|
744
|
+
* Why a separate tracer from the parent class's one: Arize's inherited
|
|
745
|
+
* transport patches don't emit spans (they only inject propagation
|
|
746
|
+
* headers), so using our own tracer here keeps Brizz spans attributed to
|
|
747
|
+
* `@brizz/sdk/mcp` in the dashboard. The parent's `tracer` is a
|
|
748
|
+
* `protected get` (no setter), so fighting it with assignment is the
|
|
749
|
+
* wrong tool.
|
|
750
|
+
*/
|
|
751
|
+
brizzTracer;
|
|
752
|
+
constructor(_config) {
|
|
753
|
+
super({ instrumentationConfig: _config?.instrumentationConfig });
|
|
754
|
+
this.brizzTracer = import_api6.trace.getTracer(INSTRUMENTATION_NAME, INSTRUMENTATION_VERSION);
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Extend `super.init()` with our protocol-layer module definition.
|
|
758
|
+
*
|
|
759
|
+
* Arize's `init()` returns definitions for the six transport modules
|
|
760
|
+
* (SSE client/server, stdio client/server, streamable-HTTP client/server)
|
|
761
|
+
* whose `send`/`start` methods get patched for W3C trace-context
|
|
762
|
+
* injection. We append one more: `@modelcontextprotocol/sdk/shared/protocol.js`
|
|
763
|
+
* where our patches open SERVER/CLIENT spans around the handler + outgoing
|
|
764
|
+
* request. If `super.init()` throws (unlikely but possible across Arize
|
|
765
|
+
* versions), we gracefully degrade to protocol-only instrumentation.
|
|
766
|
+
*
|
|
767
|
+
* The cast at the end satisfies the parent's strongly-typed generic
|
|
768
|
+
* without leaking the cast into call sites.
|
|
769
|
+
*/
|
|
770
|
+
init() {
|
|
771
|
+
let base;
|
|
772
|
+
try {
|
|
773
|
+
base = super.init() ?? [];
|
|
774
|
+
} catch (error) {
|
|
775
|
+
logger.warn(
|
|
776
|
+
`Brizz MCP: base Arize init() failed \u2014 transport context propagation disabled: ${String(error)}`
|
|
777
|
+
);
|
|
778
|
+
base = [];
|
|
779
|
+
}
|
|
780
|
+
const baseArr = Array.isArray(base) ? base : [base];
|
|
781
|
+
const brizzDef = new import_instrumentation.InstrumentationNodeModuleDefinition(
|
|
782
|
+
PROTOCOL_MODULE_NAME,
|
|
783
|
+
PROTOCOL_SUPPORTED_VERSIONS,
|
|
784
|
+
(module3) => {
|
|
785
|
+
this.patchProtocolModule(module3);
|
|
786
|
+
return module3;
|
|
787
|
+
},
|
|
788
|
+
(module3) => {
|
|
789
|
+
this.unpatchProtocolModule(module3);
|
|
790
|
+
return module3;
|
|
791
|
+
}
|
|
792
|
+
);
|
|
793
|
+
return [...baseArr, brizzDef];
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Manually instrument MCP modules for Next.js/Webpack where the
|
|
797
|
+
* auto-instrumentation module-load hook doesn't fire.
|
|
798
|
+
*
|
|
799
|
+
* Forwards the six transport module fields to the inherited Arize
|
|
800
|
+
* `manuallyInstrument(...)` (context propagation) and applies our
|
|
801
|
+
* protocol patch if `protocolModule` is provided (SERVER/CLIENT spans).
|
|
802
|
+
* Both legs are try/catch-guarded — a failure in one shouldn't prevent
|
|
803
|
+
* the other from taking effect.
|
|
804
|
+
*/
|
|
805
|
+
manuallyInstrument(modules) {
|
|
806
|
+
const {
|
|
807
|
+
clientSSEModule,
|
|
808
|
+
serverSSEModule,
|
|
809
|
+
clientStdioModule,
|
|
810
|
+
serverStdioModule,
|
|
811
|
+
clientStreamableHTTPModule,
|
|
812
|
+
serverStreamableHTTPModule,
|
|
813
|
+
protocolModule
|
|
814
|
+
} = modules;
|
|
815
|
+
try {
|
|
816
|
+
super.manuallyInstrument({
|
|
817
|
+
clientSSEModule,
|
|
818
|
+
serverSSEModule,
|
|
819
|
+
clientStdioModule,
|
|
820
|
+
serverStdioModule,
|
|
821
|
+
clientStreamableHTTPModule,
|
|
822
|
+
serverStreamableHTTPModule
|
|
823
|
+
});
|
|
824
|
+
} catch (error) {
|
|
825
|
+
logger.warn(`Brizz MCP: Arize manuallyInstrument(...) failed: ${String(error)}`);
|
|
826
|
+
}
|
|
827
|
+
if (protocolModule) {
|
|
828
|
+
try {
|
|
829
|
+
this.patchProtocolModule(protocolModule);
|
|
830
|
+
} catch (error) {
|
|
831
|
+
logger.warn(
|
|
832
|
+
`Brizz MCP: failed to manually patch Protocol module: ${String(error)}`
|
|
833
|
+
);
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* Apply the SERVER + CLIENT patch to a loaded `protocol.js` module. Shared
|
|
839
|
+
* by both the automatic module-load callback in `init()` and the manual
|
|
840
|
+
* `manuallyInstrument(...)` path above so there's exactly one place that
|
|
841
|
+
* calls `patchProtocolPrototype`.
|
|
842
|
+
*/
|
|
843
|
+
patchProtocolModule(module3) {
|
|
844
|
+
const proto = module3?.Protocol?.prototype;
|
|
845
|
+
if (!proto) {
|
|
846
|
+
logger.debug(
|
|
847
|
+
"Brizz MCP: module does not expose Protocol.prototype \u2014 skipping protocol patches"
|
|
848
|
+
);
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
const ok = patchProtocolPrototype(proto, this.brizzTracer);
|
|
852
|
+
if (ok) {
|
|
853
|
+
logger.debug("Brizz MCP: patched Protocol.prototype for SERVER+CLIENT spans");
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Unpatch is intentionally a no-op.
|
|
858
|
+
*
|
|
859
|
+
* `patchProtocolPrototype` wraps methods in place and sets a PATCHED_FLAG
|
|
860
|
+
* Symbol on the prototype to prevent double-patching. Restoring the
|
|
861
|
+
* originals would require us to hold references across module unloads,
|
|
862
|
+
* which isn't a pattern we need — instrumentation rarely gets torn down
|
|
863
|
+
* at runtime, and a process reload is the canonical way to detach.
|
|
864
|
+
*/
|
|
865
|
+
unpatchProtocolModule(_module) {
|
|
866
|
+
logger.debug(
|
|
867
|
+
"Brizz MCP: unpatch is a no-op \u2014 reload the process to cleanly detach"
|
|
868
|
+
);
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
|
|
872
|
+
// src/internal/instrumentation/registry.ts
|
|
261
873
|
var InstrumentationRegistry = class _InstrumentationRegistry {
|
|
262
874
|
static instance;
|
|
263
875
|
manualModules = null;
|
|
@@ -362,6 +974,14 @@ var InstrumentationRegistry = class _InstrumentationRegistry {
|
|
|
362
974
|
}
|
|
363
975
|
})();
|
|
364
976
|
}
|
|
977
|
+
if (this.manualModules?.mcp) {
|
|
978
|
+
try {
|
|
979
|
+
new MCPInstrumentation({ exceptionLogger }).manuallyInstrument(this.manualModules.mcp);
|
|
980
|
+
logger.debug("Manual instrumentation enabled for MCP");
|
|
981
|
+
} catch (error) {
|
|
982
|
+
logger.error(`Failed to apply MCP instrumentation: ${String(error)}`);
|
|
983
|
+
}
|
|
984
|
+
}
|
|
365
985
|
}
|
|
366
986
|
};
|
|
367
987
|
|
|
@@ -371,13 +991,8 @@ var import_exporter_logs_otlp_http = require("@opentelemetry/exporter-logs-otlp-
|
|
|
371
991
|
var import_resources = require("@opentelemetry/resources");
|
|
372
992
|
var import_sdk_logs2 = require("@opentelemetry/sdk-logs");
|
|
373
993
|
|
|
374
|
-
// src/internal/version.ts
|
|
375
|
-
function getSDKVersion() {
|
|
376
|
-
return "0.1.20";
|
|
377
|
-
}
|
|
378
|
-
|
|
379
994
|
// src/internal/log/processors/log-processor.ts
|
|
380
|
-
var
|
|
995
|
+
var import_api7 = require("@opentelemetry/api");
|
|
381
996
|
var import_sdk_logs = require("@opentelemetry/sdk-logs");
|
|
382
997
|
|
|
383
998
|
// src/internal/masking/patterns.ts
|
|
@@ -1004,13 +1619,6 @@ function maskAttributes(attributes, rules, outputOriginalValue = false) {
|
|
|
1004
1619
|
return maskedAttributes;
|
|
1005
1620
|
}
|
|
1006
1621
|
|
|
1007
|
-
// src/internal/semantic-conventions.ts
|
|
1008
|
-
var import_api2 = require("@opentelemetry/api");
|
|
1009
|
-
var BRIZZ = "brizz";
|
|
1010
|
-
var PROPERTIES = "properties";
|
|
1011
|
-
var PROPERTIES_CONTEXT_KEY = (0, import_api2.createContextKey)(PROPERTIES);
|
|
1012
|
-
var SESSION_OBJECT_CONTEXT_KEY = (0, import_api2.createContextKey)("brizz.session.object");
|
|
1013
|
-
|
|
1014
1622
|
// src/internal/log/processors/log-processor.ts
|
|
1015
1623
|
var DEFAULT_LOG_MASKING_RULES = [
|
|
1016
1624
|
{
|
|
@@ -1030,7 +1638,7 @@ var BrizzSimpleLogRecordProcessor = class extends import_sdk_logs.SimpleLogRecor
|
|
|
1030
1638
|
if (maskingConfig) {
|
|
1031
1639
|
maskLog(logRecord, maskingConfig);
|
|
1032
1640
|
}
|
|
1033
|
-
const associationProperties =
|
|
1641
|
+
const associationProperties = import_api7.context.active().getValue(PROPERTIES_CONTEXT_KEY);
|
|
1034
1642
|
if (associationProperties) {
|
|
1035
1643
|
for (const [key, value] of Object.entries(associationProperties)) {
|
|
1036
1644
|
logRecord.setAttribute(`${BRIZZ}.${key}`, value);
|
|
@@ -1050,7 +1658,7 @@ var BrizzBatchLogRecordProcessor = class extends import_sdk_logs.BatchLogRecordP
|
|
|
1050
1658
|
if (maskingConfig) {
|
|
1051
1659
|
maskLog(logRecord, maskingConfig);
|
|
1052
1660
|
}
|
|
1053
|
-
const associationProperties =
|
|
1661
|
+
const associationProperties = import_api7.context.active().getValue(PROPERTIES_CONTEXT_KEY);
|
|
1054
1662
|
if (associationProperties) {
|
|
1055
1663
|
for (const [key, value] of Object.entries(associationProperties)) {
|
|
1056
1664
|
logRecord.setAttribute(`${BRIZZ}.${key}`, value);
|
|
@@ -1437,10 +2045,10 @@ var BrizzSpanExporter = class {
|
|
|
1437
2045
|
};
|
|
1438
2046
|
|
|
1439
2047
|
// src/internal/trace/processors/span-processor.ts
|
|
1440
|
-
var
|
|
2048
|
+
var import_api8 = require("@opentelemetry/api");
|
|
1441
2049
|
var import_sdk_trace_base = require("@opentelemetry/sdk-trace-base");
|
|
1442
2050
|
function applyContextAttributes(span) {
|
|
1443
|
-
const sessionProperties =
|
|
2051
|
+
const sessionProperties = import_api8.context.active().getValue(PROPERTIES_CONTEXT_KEY);
|
|
1444
2052
|
if (sessionProperties) {
|
|
1445
2053
|
for (const [key, value] of Object.entries(sessionProperties)) {
|
|
1446
2054
|
span.setAttribute(`${BRIZZ}.${key}`, value);
|
|
@@ -1637,7 +2245,7 @@ function getSpanProcessor() {
|
|
|
1637
2245
|
}
|
|
1638
2246
|
|
|
1639
2247
|
// src/internal/trace/session.ts
|
|
1640
|
-
var
|
|
2248
|
+
var import_api9 = require("@opentelemetry/api");
|
|
1641
2249
|
|
|
1642
2250
|
// src/internal/sdk.ts
|
|
1643
2251
|
var _Brizz = class __Brizz {
|