@brizz/sdk 0.1.39 → 0.1.41

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.
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/internal/version.ts
2
2
  function getSDKVersion() {
3
- return "0.1.39";
3
+ return "0.1.41";
4
4
  }
5
5
 
6
6
 
@@ -1,6 +1,6 @@
1
1
  // src/internal/version.ts
2
2
  function getSDKVersion() {
3
- return "0.1.39";
3
+ return "0.1.41";
4
4
  }
5
5
 
6
6
  export {
package/dist/index.cjs CHANGED
@@ -4,7 +4,7 @@
4
4
  var _chunkUNLNNKZ7cjs = require('./chunk-UNLNNKZ7.cjs');
5
5
 
6
6
 
7
- var _chunkNMDPWJMUcjs = require('./chunk-NMDPWJMU.cjs');
7
+ var _chunk3QYP2SDNcjs = require('./chunk-3QYP2SDN.cjs');
8
8
 
9
9
 
10
10
 
@@ -124,7 +124,7 @@ function loadGenAIInstrumentations() {
124
124
  function loadMCPInstrumentation() {
125
125
  void (async () => {
126
126
  try {
127
- const { MCPInstrumentation } = await Promise.resolve().then(() => _interopRequireWildcard(require("./mcp-3N3DAQGD.cjs")));
127
+ const { MCPInstrumentation } = await Promise.resolve().then(() => _interopRequireWildcard(require("./mcp-DU7AUWUH.cjs")));
128
128
  const mcp = new MCPInstrumentation({ exceptionLogger });
129
129
  _instrumentation.registerInstrumentations.call(void 0, { instrumentations: [mcp] });
130
130
  _chunkWC573FCIcjs.logger.debug("Auto-loaded MCP instrumentation");
@@ -507,7 +507,7 @@ var InstrumentationRegistry = (_class = class _InstrumentationRegistry {construc
507
507
  const mcpModules = this.manualModules.mcp;
508
508
  void (async () => {
509
509
  try {
510
- const { MCPInstrumentation } = await Promise.resolve().then(() => _interopRequireWildcard(require("./mcp-3N3DAQGD.cjs")));
510
+ const { MCPInstrumentation } = await Promise.resolve().then(() => _interopRequireWildcard(require("./mcp-DU7AUWUH.cjs")));
511
511
  const instrumentation = new MCPInstrumentation({ exceptionLogger: exceptionLogger2 });
512
512
  instrumentation.manuallyInstrument(mcpModules);
513
513
  _chunkWC573FCIcjs.logger.debug("Manual instrumentation enabled for MCP");
@@ -649,6 +649,11 @@ function setMessageId(id) {
649
649
  setCurrentSpanCustomProperties(properties);
650
650
  propagatePropertiesImperatively(properties);
651
651
  }
652
+ function setSessionId(id) {
653
+ const properties = { [_chunk77UGZGMPcjs.SESSION_ID]: id };
654
+ setCurrentSpanCustomProperties(properties);
655
+ propagatePropertiesImperatively(properties);
656
+ }
652
657
  function userProperties(user) {
653
658
  const properties = { [_chunk77UGZGMPcjs.USER_ID]: user.id };
654
659
  if (user.email !== void 0) {
@@ -1764,7 +1769,7 @@ var LoggingModule = (_class3 = class _LoggingModule {constructor() { _class3.pro
1764
1769
  const resourceAttributes = {
1765
1770
  ...config.resourceAttributes,
1766
1771
  "service.name": config.appName,
1767
- [BRIZZ_SDK_VERSION]: _chunkNMDPWJMUcjs.getSDKVersion.call(void 0, ),
1772
+ [BRIZZ_SDK_VERSION]: _chunk3QYP2SDNcjs.getSDKVersion.call(void 0, ),
1768
1773
  [BRIZZ_SDK_LANGUAGE]: SDK_LANGUAGE
1769
1774
  };
1770
1775
  if (config.environment) {
@@ -1989,7 +1994,7 @@ var BrizzSpanExporter = class {
1989
1994
  const resourceAttrs = {
1990
1995
  ...config.resourceAttributes,
1991
1996
  "service.name": config.appName,
1992
- [BRIZZ_SDK_VERSION]: _chunkNMDPWJMUcjs.getSDKVersion.call(void 0, ),
1997
+ [BRIZZ_SDK_VERSION]: _chunk3QYP2SDNcjs.getSDKVersion.call(void 0, ),
1993
1998
  [BRIZZ_SDK_LANGUAGE]: SDK_LANGUAGE
1994
1999
  };
1995
2000
  if (config.environment) {
@@ -2281,7 +2286,7 @@ var _Brizz = (_class6 = class __Brizz {constructor() { _class6.prototype.__init1
2281
2286
  const resourceAttributes = {
2282
2287
  ...resolvedConfig.resourceAttributes,
2283
2288
  "service.name": resolvedConfig.appName,
2284
- [BRIZZ_SDK_VERSION]: _chunkNMDPWJMUcjs.getSDKVersion.call(void 0, ),
2289
+ [BRIZZ_SDK_VERSION]: _chunk3QYP2SDNcjs.getSDKVersion.call(void 0, ),
2285
2290
  [BRIZZ_SDK_LANGUAGE]: SDK_LANGUAGE
2286
2291
  };
2287
2292
  if (resolvedConfig.environment) {
@@ -2492,4 +2497,5 @@ var Brizz = new _Brizz();
2492
2497
 
2493
2498
 
2494
2499
 
2495
- exports.Brizz = Brizz; exports.DEFAULT_PII_PATTERNS = DEFAULT_PII_PATTERNS; exports.LogLevel = _chunkWC573FCIcjs.LogLevel; exports.Session = Session; exports.SessionTitle = SessionTitle; exports.SeverityNumber = _apilogs.SeverityNumber; exports.addExternalLink = addExternalLink; exports.callWithMessageId = callWithMessageId; exports.callWithMute = callWithMute; exports.callWithOrganization = callWithOrganization; exports.callWithProperties = callWithProperties; exports.callWithSessionId = callWithSessionId; exports.callWithUser = callWithUser; exports.emitEvent = emitEvent; exports.emitEventWithSessionId = emitEventWithSessionId; exports.getActiveSession = getActiveSession; exports.getLogLevel = _chunkWC573FCIcjs.getLogLevel; exports.getMetricsExporter = getMetricsExporter; exports.getMetricsReader = getMetricsReader; exports.getSpanExporter = getSpanExporter; exports.getSpanProcessor = getSpanProcessor; exports.logger = _chunkWC573FCIcjs.logger; exports.maskAttributes = maskAttributes; exports.maskValue = maskValue; exports.recordFeedback = recordFeedback; exports.recordMetric = recordMetric; exports.setCurrentSpanCustomProperties = setCurrentSpanCustomProperties; exports.setLogLevel = _chunkWC573FCIcjs.setLogLevel; exports.setMessageId = setMessageId; exports.setOrganization = setOrganization; exports.setUser = setUser; exports.startSession = startSession; exports.startSessionTitle = startSessionTitle; exports.withMessageId = withMessageId; exports.withMute = withMute; exports.withOrganization = withOrganization; exports.withProperties = withProperties; exports.withSessionId = withSessionId; exports.withUser = withUser;
2500
+
2501
+ exports.Brizz = Brizz; exports.DEFAULT_PII_PATTERNS = DEFAULT_PII_PATTERNS; exports.LogLevel = _chunkWC573FCIcjs.LogLevel; exports.Session = Session; exports.SessionTitle = SessionTitle; exports.SeverityNumber = _apilogs.SeverityNumber; exports.addExternalLink = addExternalLink; exports.callWithMessageId = callWithMessageId; exports.callWithMute = callWithMute; exports.callWithOrganization = callWithOrganization; exports.callWithProperties = callWithProperties; exports.callWithSessionId = callWithSessionId; exports.callWithUser = callWithUser; exports.emitEvent = emitEvent; exports.emitEventWithSessionId = emitEventWithSessionId; exports.getActiveSession = getActiveSession; exports.getLogLevel = _chunkWC573FCIcjs.getLogLevel; exports.getMetricsExporter = getMetricsExporter; exports.getMetricsReader = getMetricsReader; exports.getSpanExporter = getSpanExporter; exports.getSpanProcessor = getSpanProcessor; exports.logger = _chunkWC573FCIcjs.logger; exports.maskAttributes = maskAttributes; exports.maskValue = maskValue; exports.recordFeedback = recordFeedback; exports.recordMetric = recordMetric; exports.setCurrentSpanCustomProperties = setCurrentSpanCustomProperties; exports.setLogLevel = _chunkWC573FCIcjs.setLogLevel; exports.setMessageId = setMessageId; exports.setOrganization = setOrganization; exports.setSessionId = setSessionId; exports.setUser = setUser; exports.startSession = startSession; exports.startSessionTitle = startSessionTitle; exports.withMessageId = withMessageId; exports.withMute = withMute; exports.withOrganization = withOrganization; exports.withProperties = withProperties; exports.withSessionId = withSessionId; exports.withUser = withUser;
package/dist/index.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  } from "./chunk-7SUF4VVI.js";
5
5
  import {
6
6
  getSDKVersion
7
- } from "./chunk-JBNWC4YN.js";
7
+ } from "./chunk-QRHMDRW6.js";
8
8
  import {
9
9
  DEFAULT_LOG_LEVEL,
10
10
  LogLevel,
@@ -124,7 +124,7 @@ function loadGenAIInstrumentations() {
124
124
  function loadMCPInstrumentation() {
125
125
  void (async () => {
126
126
  try {
127
- const { MCPInstrumentation } = await import("./mcp-EWOGAW4J.js");
127
+ const { MCPInstrumentation } = await import("./mcp-TI3IDGUJ.js");
128
128
  const mcp = new MCPInstrumentation({ exceptionLogger });
129
129
  registerInstrumentations({ instrumentations: [mcp] });
130
130
  logger.debug("Auto-loaded MCP instrumentation");
@@ -507,7 +507,7 @@ var InstrumentationRegistry = class _InstrumentationRegistry {
507
507
  const mcpModules = this.manualModules.mcp;
508
508
  void (async () => {
509
509
  try {
510
- const { MCPInstrumentation } = await import("./mcp-EWOGAW4J.js");
510
+ const { MCPInstrumentation } = await import("./mcp-TI3IDGUJ.js");
511
511
  const instrumentation = new MCPInstrumentation({ exceptionLogger: exceptionLogger2 });
512
512
  instrumentation.manuallyInstrument(mcpModules);
513
513
  logger.debug("Manual instrumentation enabled for MCP");
@@ -649,6 +649,11 @@ function setMessageId(id) {
649
649
  setCurrentSpanCustomProperties(properties);
650
650
  propagatePropertiesImperatively(properties);
651
651
  }
652
+ function setSessionId(id) {
653
+ const properties = { [SESSION_ID]: id };
654
+ setCurrentSpanCustomProperties(properties);
655
+ propagatePropertiesImperatively(properties);
656
+ }
652
657
  function userProperties(user) {
653
658
  const properties = { [USER_ID]: user.id };
654
659
  if (user.email !== void 0) {
@@ -2483,6 +2488,7 @@ export {
2483
2488
  setLogLevel,
2484
2489
  setMessageId,
2485
2490
  setOrganization,
2491
+ setSessionId,
2486
2492
  setUser,
2487
2493
  startSession,
2488
2494
  startSessionTitle,
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
- var _chunkNMDPWJMUcjs = require('./chunk-NMDPWJMU.cjs');
3
+ var _chunk3QYP2SDNcjs = require('./chunk-3QYP2SDN.cjs');
4
4
 
5
5
 
6
6
  var _chunkWC573FCIcjs = require('./chunk-WC573FCI.cjs');
@@ -77,9 +77,6 @@ function safeStringify(value) {
77
77
  }
78
78
  }
79
79
  function emitSchemaSpansFromListResponse(result, transportSessionId, tracer) {
80
- if (!transportSessionId) {
81
- return;
82
- }
83
80
  const tools = extractTools(result);
84
81
  if (tools === void 0) {
85
82
  return;
@@ -112,7 +109,9 @@ function stampSchemaAttributes(span, toolName, transportSessionId, tool) {
112
109
  const outputSchema = tool.outputSchema !== void 0 && tool.outputSchema !== null ? truncateSchemaAttr(safeStringify(tool.outputSchema)) : "";
113
110
  span.setAttribute(RPC_SYSTEM, RPC_SYSTEM_MCP);
114
111
  span.setAttribute(MCP_COMPONENT_TYPE, MCP_COMPONENT_TOOL_SCHEMA);
115
- span.setAttribute(MCP_SESSION_ID, transportSessionId);
112
+ if (transportSessionId) {
113
+ span.setAttribute(MCP_SESSION_ID, transportSessionId);
114
+ }
116
115
  span.setAttribute(MCP_TOOL_NAME, toolName);
117
116
  span.setAttribute(MCP_TOOL_SCHEMA_PARAMETERS, parameters);
118
117
  span.setAttribute(MCP_TOOL_SCHEMA_OUTPUT, outputSchema);
@@ -133,6 +132,42 @@ function extractTools(result) {
133
132
 
134
133
  // src/internal/instrumentation/mcp/session.ts
135
134
 
135
+ var FALLBACK_SESSION_ID = /* @__PURE__ */ Symbol("brizz.mcp.fallback-session-id");
136
+ var FALLBACK_SESSION_ID_BYTES = 16;
137
+ function generateSessionId() {
138
+ let id = "";
139
+ for (let i = 0; i < FALLBACK_SESSION_ID_BYTES; i++) {
140
+ id += Math.floor(Math.random() * 256).toString(16).padStart(2, "0");
141
+ }
142
+ return id;
143
+ }
144
+ function isSessionCarrier(value) {
145
+ return typeof value === "object" && value !== null;
146
+ }
147
+ function resolveTransportSessionId(protocol) {
148
+ if (!isSessionCarrier(protocol)) {
149
+ return void 0;
150
+ }
151
+ return _nullishCoalesce(protocol.sessionId, () => ( _optionalChain([protocol, 'access', _ => _._transport, 'optionalAccess', _2 => _2.sessionId])));
152
+ }
153
+ function resolveSessionId(protocol, baseContext) {
154
+ if (!isSessionCarrier(protocol)) {
155
+ return void 0;
156
+ }
157
+ const fromTransport = resolveTransportSessionId(protocol);
158
+ if (fromTransport) {
159
+ return fromTransport;
160
+ }
161
+ try {
162
+ const bound = baseContext.getValue(_chunk77UGZGMPcjs.PROPERTIES_CONTEXT_KEY);
163
+ if (_optionalChain([bound, 'optionalAccess', _3 => _3[_chunk77UGZGMPcjs.SESSION_ID]])) {
164
+ return void 0;
165
+ }
166
+ } catch (e2) {
167
+ }
168
+ protocol[FALLBACK_SESSION_ID] ??= generateSessionId();
169
+ return protocol[FALLBACK_SESSION_ID];
170
+ }
136
171
  function stampAndPropagateSession(span, sessionId, baseContext = _api.context.active()) {
137
172
  if (!sessionId) {
138
173
  return { context: baseContext, sessionId: null };
@@ -164,19 +199,19 @@ function stampAndPropagateSession(span, sessionId, baseContext = _api.context.ac
164
199
  function deriveSpanName(method, params) {
165
200
  try {
166
201
  if (method === METHOD_TOOLS_CALL) {
167
- const name = typeof _optionalChain([params, 'optionalAccess', _ => _["name"]]) === "string" ? params["name"] : "";
202
+ const name = typeof _optionalChain([params, 'optionalAccess', _4 => _4["name"]]) === "string" ? params["name"] : "";
168
203
  return name ? `${SPAN_NAME_TOOLS_CALL} ${name}` : SPAN_NAME_TOOLS_CALL;
169
204
  }
170
205
  if (method === METHOD_RESOURCES_READ) {
171
- const uri = typeof _optionalChain([params, 'optionalAccess', _2 => _2["uri"]]) === "string" ? params["uri"] : "";
206
+ const uri = typeof _optionalChain([params, 'optionalAccess', _5 => _5["uri"]]) === "string" ? params["uri"] : "";
172
207
  return uri ? `${METHOD_RESOURCES_READ} ${uri}` : METHOD_RESOURCES_READ;
173
208
  }
174
209
  if (method === METHOD_PROMPTS_GET) {
175
- const name = typeof _optionalChain([params, 'optionalAccess', _3 => _3["name"]]) === "string" ? params["name"] : "";
210
+ const name = typeof _optionalChain([params, 'optionalAccess', _6 => _6["name"]]) === "string" ? params["name"] : "";
176
211
  return name ? `${METHOD_PROMPTS_GET} ${name}` : METHOD_PROMPTS_GET;
177
212
  }
178
213
  return method;
179
- } catch (e2) {
214
+ } catch (e3) {
180
215
  return method || "mcp";
181
216
  }
182
217
  }
@@ -205,12 +240,12 @@ function applyServerRequestAttributes(span, request, protocol) {
205
240
  applyBaseAttributes(span, request);
206
241
  applyMethodSpecificRequestAttributes(span, request);
207
242
  if (request.method === METHOD_TOOLS_CALL) {
208
- const args = _optionalChain([request, 'access', _4 => _4.params, 'optionalAccess', _5 => _5["arguments"]]);
243
+ const args = _optionalChain([request, 'access', _7 => _7.params, 'optionalAccess', _8 => _8["arguments"]]);
209
244
  if (args !== void 0) {
210
245
  span.setAttribute(MCP_TOOL_ARGUMENTS, serializeForAttribute(args));
211
246
  }
212
247
  }
213
- const sessionId = _nullishCoalesce(protocol.sessionId, () => ( _optionalChain([protocol, 'access', _6 => _6._transport, 'optionalAccess', _7 => _7.sessionId])));
248
+ const sessionId = _nullishCoalesce(protocol.sessionId, () => ( _optionalChain([protocol, 'access', _9 => _9._transport, 'optionalAccess', _10 => _10.sessionId])));
214
249
  if (sessionId) {
215
250
  span.setAttribute(MCP_SESSION_ID, sessionId);
216
251
  }
@@ -258,7 +293,7 @@ function applyResultAttributes(span, method, result) {
258
293
  return;
259
294
  }
260
295
  const obj = result && typeof result === "object" ? result : null;
261
- const content = _nullishCoalesce(_optionalChain([obj, 'optionalAccess', _8 => _8["content"]]), () => ( result));
296
+ const content = _nullishCoalesce(_optionalChain([obj, 'optionalAccess', _11 => _11["content"]]), () => ( result));
262
297
  span.setAttribute(MCP_TOOL_RESULT, serializeForAttribute(content));
263
298
  if (obj && obj["isError"] === true) {
264
299
  span.setAttribute(ERROR_TYPE, ERROR_TYPE_TOOL);
@@ -268,24 +303,24 @@ function applyResultAttributes(span, method, result) {
268
303
  }
269
304
  function applyErrorAttributes(span, err) {
270
305
  const error = err;
271
- const code = _optionalChain([error, 'optionalAccess', _9 => _9.code]);
306
+ const code = _optionalChain([error, 'optionalAccess', _12 => _12.code]);
272
307
  if (typeof code === "number") {
273
308
  span.setAttribute(RPC_RESPONSE_STATUS_CODE, code);
274
309
  span.setAttribute(ERROR_TYPE, String(code));
275
- } else if (_optionalChain([error, 'optionalAccess', _10 => _10.name]) === "AbortError") {
310
+ } else if (_optionalChain([error, 'optionalAccess', _13 => _13.name]) === "AbortError") {
276
311
  span.setAttribute(ERROR_TYPE, "cancelled");
277
- } else if (_optionalChain([error, 'optionalAccess', _11 => _11.name]) === "TimeoutError") {
312
+ } else if (_optionalChain([error, 'optionalAccess', _14 => _14.name]) === "TimeoutError") {
278
313
  span.setAttribute(ERROR_TYPE, "timeout");
279
314
  } else {
280
- span.setAttribute(ERROR_TYPE, _optionalChain([error, 'optionalAccess', _12 => _12.constructor, 'optionalAccess', _13 => _13.name]) || _optionalChain([error, 'optionalAccess', _14 => _14.name]) || "Error");
315
+ span.setAttribute(ERROR_TYPE, _optionalChain([error, 'optionalAccess', _15 => _15.constructor, 'optionalAccess', _16 => _16.name]) || _optionalChain([error, 'optionalAccess', _17 => _17.name]) || "Error");
281
316
  }
282
317
  try {
283
318
  span.recordException(error);
284
- } catch (e3) {
319
+ } catch (e4) {
285
320
  }
286
321
  span.setStatus({
287
322
  code: _api.SpanStatusCode.ERROR,
288
- message: typeof _optionalChain([error, 'optionalAccess', _15 => _15.message]) === "string" ? error.message : void 0
323
+ message: typeof _optionalChain([error, 'optionalAccess', _18 => _18.message]) === "string" ? error.message : void 0
289
324
  });
290
325
  }
291
326
  function serializeForAttribute(value) {
@@ -304,7 +339,7 @@ function rawSerialize(value) {
304
339
  }
305
340
  try {
306
341
  return JSON.stringify(value);
307
- } catch (e4) {
342
+ } catch (e5) {
308
343
  try {
309
344
  if (value === null || value === void 0) {
310
345
  return "";
@@ -314,7 +349,7 @@ function rawSerialize(value) {
314
349
  // eslint-disable-next-line @typescript-eslint/no-base-to-string
315
350
  String(value)
316
351
  ) : tag;
317
- } catch (e5) {
352
+ } catch (e6) {
318
353
  return "";
319
354
  }
320
355
  }
@@ -421,19 +456,30 @@ function wrapOnRequest(original, tracer) {
421
456
  return original.apply(this, args);
422
457
  }
423
458
  const { span, spanCtx } = started;
424
- const transportSessionId = _nullishCoalesce(this.sessionId, () => ( _optionalChain([this, 'access', _16 => _16._transport, 'optionalAccess', _17 => _17.sessionId])));
425
- const postResult = method === METHOD_TOOLS_LIST ? (result) => {
426
- try {
427
- emitSchemaSpansFromListResponse(result, transportSessionId, tracer);
428
- } catch (error) {
429
- _chunkWC573FCIcjs.logger.warn(
430
- `Brizz MCP: failed to emit tools/list schema spans: ${String(error)}`
431
- );
432
- }
433
- } : void 0;
459
+ const hooks = {};
460
+ if (method === METHOD_TOOLS_LIST) {
461
+ hooks.onResult = (result) => {
462
+ try {
463
+ emitSchemaSpansFromListResponse(
464
+ result,
465
+ resolveTransportSessionId(this),
466
+ tracer
467
+ );
468
+ } catch (error) {
469
+ _chunkWC573FCIcjs.logger.warn(
470
+ `Brizz MCP: failed to emit tools/list schema spans: ${String(error)}`
471
+ );
472
+ }
473
+ };
474
+ }
475
+ if (method !== METHOD_TOOLS_CALL) {
476
+ hooks.onError = () => {
477
+ stampAndPropagateSession(span, resolveSessionId(this, spanCtx), spanCtx);
478
+ };
479
+ }
434
480
  const wrappedHandler = (req, extra) => _api.context.with(
435
481
  spanCtx,
436
- () => executeHandler(span, method, handler, req, extra, postResult)
482
+ () => executeHandler(span, method, handler, req, extra, hooks)
437
483
  );
438
484
  const hadEntry = handlers.has(method);
439
485
  const prev = handlers.get(method);
@@ -457,41 +503,39 @@ function wrapOnRequest(original, tracer) {
457
503
  }
458
504
  };
459
505
  }
460
- function executeAroundSpan(span, method, run, postResult) {
506
+ function executeAroundSpan(span, method, run, hooks) {
461
507
  let result;
462
508
  try {
463
509
  result = run();
464
510
  } catch (error) {
465
511
  safeApplyErrorAttributes(span, error);
512
+ _optionalChain([hooks, 'optionalAccess', _19 => _19.onError, 'optionalCall', _20 => _20()]);
466
513
  safeEnd(span);
467
514
  throw error;
468
515
  }
469
516
  if (!isThenable(result)) {
470
517
  safeApplyResultAttributes(span, method, result);
471
- if (postResult) {
472
- postResult(result);
473
- }
518
+ _optionalChain([hooks, 'optionalAccess', _21 => _21.onResult, 'optionalCall', _22 => _22(result)]);
474
519
  safeEnd(span);
475
520
  return result;
476
521
  }
477
522
  return result.then(
478
523
  (value) => {
479
524
  safeApplyResultAttributes(span, method, value);
480
- if (postResult) {
481
- postResult(value);
482
- }
525
+ _optionalChain([hooks, 'optionalAccess', _23 => _23.onResult, 'optionalCall', _24 => _24(value)]);
483
526
  safeEnd(span);
484
527
  return value;
485
528
  },
486
529
  (error) => {
487
530
  safeApplyErrorAttributes(span, error);
531
+ _optionalChain([hooks, 'optionalAccess', _25 => _25.onError, 'optionalCall', _26 => _26()]);
488
532
  safeEnd(span);
489
533
  throw error;
490
534
  }
491
535
  );
492
536
  }
493
- function executeHandler(span, method, handler, req, extra, postResult) {
494
- return executeAroundSpan(span, method, () => handler(req, extra), postResult);
537
+ function executeHandler(span, method, handler, req, extra, hooks) {
538
+ return executeAroundSpan(span, method, () => handler(req, extra), hooks);
495
539
  }
496
540
  function safeStartClientSpan(tracer, request, protocol) {
497
541
  try {
@@ -500,7 +544,7 @@ function safeStartClientSpan(tracer, request, protocol) {
500
544
  applyClientRequestAttributes(
501
545
  span,
502
546
  request,
503
- _optionalChain([protocol, 'access', _18 => _18._transport, 'optionalAccess', _19 => _19.constructor, 'optionalAccess', _20 => _20.name])
547
+ _optionalChain([protocol, 'access', _27 => _27._transport, 'optionalAccess', _28 => _28.constructor, 'optionalAccess', _29 => _29.name])
504
548
  );
505
549
  return span;
506
550
  } catch (error) {
@@ -518,8 +562,8 @@ function safeStartServerSpan(tracer, request, protocol) {
518
562
  parentCtx
519
563
  );
520
564
  applyServerRequestAttributes(span, request, protocol);
521
- const sessionId = _nullishCoalesce(protocol.sessionId, () => ( _optionalChain([protocol, 'access', _21 => _21._transport, 'optionalAccess', _22 => _22.sessionId])));
522
- const { context: sessCtx } = stampAndPropagateSession(span, sessionId, parentCtx);
565
+ const resolved = request.method === METHOD_TOOLS_CALL ? resolveSessionId(protocol, parentCtx) : void 0;
566
+ const { context: sessCtx } = stampAndPropagateSession(span, resolved, parentCtx);
523
567
  return { span, spanCtx: _api.trace.setSpan(sessCtx, span) };
524
568
  } catch (error) {
525
569
  _chunkWC573FCIcjs.logger.debug(`Brizz MCP: failed to open SERVER span: ${String(error)}`);
@@ -528,7 +572,7 @@ function safeStartServerSpan(tracer, request, protocol) {
528
572
  }
529
573
  function extractParentContext(request) {
530
574
  try {
531
- const meta = _optionalChain([request, 'access', _23 => _23.params, 'optionalAccess', _24 => _24._meta]);
575
+ const meta = _optionalChain([request, 'access', _30 => _30.params, 'optionalAccess', _31 => _31._meta]);
532
576
  if (meta && typeof meta === "object") {
533
577
  return _api.propagation.extract(_api.context.active(), meta);
534
578
  }
@@ -570,7 +614,7 @@ function safeEnd(span) {
570
614
 
571
615
  // src/internal/instrumentation/mcp/version.ts
572
616
  var INSTRUMENTATION_NAME = "@brizz/sdk/mcp";
573
- var INSTRUMENTATION_VERSION = _chunkNMDPWJMUcjs.getSDKVersion.call(void 0, );
617
+ var INSTRUMENTATION_VERSION = _chunk3QYP2SDNcjs.getSDKVersion.call(void 0, );
574
618
 
575
619
  // src/internal/instrumentation/mcp/instrumentation.ts
576
620
  var PROTOCOL_MODULE_NAME = "@modelcontextprotocol/sdk/shared/protocol.js";
@@ -588,7 +632,7 @@ var MCPInstrumentation = class extends _openinferenceinstrumentationmcp.MCPInstr
588
632
  */
589
633
 
590
634
  constructor(_config) {
591
- super({ instrumentationConfig: _optionalChain([_config, 'optionalAccess', _25 => _25.instrumentationConfig]) });
635
+ super({ instrumentationConfig: _optionalChain([_config, 'optionalAccess', _32 => _32.instrumentationConfig]) });
592
636
  this.brizzTracer = _api.trace.getTracer(INSTRUMENTATION_NAME, INSTRUMENTATION_VERSION);
593
637
  }
594
638
  /**
@@ -598,7 +642,7 @@ var MCPInstrumentation = class extends _openinferenceinstrumentationmcp.MCPInstr
598
642
  * calls `patchProtocolPrototype`.
599
643
  */
600
644
  patchProtocolModule(module) {
601
- const proto = _optionalChain([module, 'optionalAccess', _26 => _26.Protocol, 'optionalAccess', _27 => _27.prototype]);
645
+ const proto = _optionalChain([module, 'optionalAccess', _33 => _33.Protocol, 'optionalAccess', _34 => _34.prototype]);
602
646
  if (!proto) {
603
647
  _chunkWC573FCIcjs.logger.debug(
604
648
  "Brizz MCP: module does not expose Protocol.prototype \u2014 skipping protocol patches"
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  getSDKVersion
3
- } from "./chunk-JBNWC4YN.js";
3
+ } from "./chunk-QRHMDRW6.js";
4
4
  import {
5
5
  logger
6
6
  } from "./chunk-Y4HNUX3Q.js";
@@ -77,9 +77,6 @@ function safeStringify(value) {
77
77
  }
78
78
  }
79
79
  function emitSchemaSpansFromListResponse(result, transportSessionId, tracer) {
80
- if (!transportSessionId) {
81
- return;
82
- }
83
80
  const tools = extractTools(result);
84
81
  if (tools === void 0) {
85
82
  return;
@@ -112,7 +109,9 @@ function stampSchemaAttributes(span, toolName, transportSessionId, tool) {
112
109
  const outputSchema = tool.outputSchema !== void 0 && tool.outputSchema !== null ? truncateSchemaAttr(safeStringify(tool.outputSchema)) : "";
113
110
  span.setAttribute(RPC_SYSTEM, RPC_SYSTEM_MCP);
114
111
  span.setAttribute(MCP_COMPONENT_TYPE, MCP_COMPONENT_TOOL_SCHEMA);
115
- span.setAttribute(MCP_SESSION_ID, transportSessionId);
112
+ if (transportSessionId) {
113
+ span.setAttribute(MCP_SESSION_ID, transportSessionId);
114
+ }
116
115
  span.setAttribute(MCP_TOOL_NAME, toolName);
117
116
  span.setAttribute(MCP_TOOL_SCHEMA_PARAMETERS, parameters);
118
117
  span.setAttribute(MCP_TOOL_SCHEMA_OUTPUT, outputSchema);
@@ -133,6 +132,42 @@ function extractTools(result) {
133
132
 
134
133
  // src/internal/instrumentation/mcp/session.ts
135
134
  import { context } from "@opentelemetry/api";
135
+ var FALLBACK_SESSION_ID = /* @__PURE__ */ Symbol("brizz.mcp.fallback-session-id");
136
+ var FALLBACK_SESSION_ID_BYTES = 16;
137
+ function generateSessionId() {
138
+ let id = "";
139
+ for (let i = 0; i < FALLBACK_SESSION_ID_BYTES; i++) {
140
+ id += Math.floor(Math.random() * 256).toString(16).padStart(2, "0");
141
+ }
142
+ return id;
143
+ }
144
+ function isSessionCarrier(value) {
145
+ return typeof value === "object" && value !== null;
146
+ }
147
+ function resolveTransportSessionId(protocol) {
148
+ if (!isSessionCarrier(protocol)) {
149
+ return void 0;
150
+ }
151
+ return protocol.sessionId ?? protocol._transport?.sessionId;
152
+ }
153
+ function resolveSessionId(protocol, baseContext) {
154
+ if (!isSessionCarrier(protocol)) {
155
+ return void 0;
156
+ }
157
+ const fromTransport = resolveTransportSessionId(protocol);
158
+ if (fromTransport) {
159
+ return fromTransport;
160
+ }
161
+ try {
162
+ const bound = baseContext.getValue(PROPERTIES_CONTEXT_KEY);
163
+ if (bound?.[SESSION_ID]) {
164
+ return void 0;
165
+ }
166
+ } catch {
167
+ }
168
+ protocol[FALLBACK_SESSION_ID] ??= generateSessionId();
169
+ return protocol[FALLBACK_SESSION_ID];
170
+ }
136
171
  function stampAndPropagateSession(span, sessionId, baseContext = context.active()) {
137
172
  if (!sessionId) {
138
173
  return { context: baseContext, sessionId: null };
@@ -421,19 +456,30 @@ function wrapOnRequest(original, tracer) {
421
456
  return original.apply(this, args);
422
457
  }
423
458
  const { span, spanCtx } = started;
424
- const transportSessionId = this.sessionId ?? this._transport?.sessionId;
425
- const postResult = method === METHOD_TOOLS_LIST ? (result) => {
426
- try {
427
- emitSchemaSpansFromListResponse(result, transportSessionId, tracer);
428
- } catch (error) {
429
- logger.warn(
430
- `Brizz MCP: failed to emit tools/list schema spans: ${String(error)}`
431
- );
432
- }
433
- } : void 0;
459
+ const hooks = {};
460
+ if (method === METHOD_TOOLS_LIST) {
461
+ hooks.onResult = (result) => {
462
+ try {
463
+ emitSchemaSpansFromListResponse(
464
+ result,
465
+ resolveTransportSessionId(this),
466
+ tracer
467
+ );
468
+ } catch (error) {
469
+ logger.warn(
470
+ `Brizz MCP: failed to emit tools/list schema spans: ${String(error)}`
471
+ );
472
+ }
473
+ };
474
+ }
475
+ if (method !== METHOD_TOOLS_CALL) {
476
+ hooks.onError = () => {
477
+ stampAndPropagateSession(span, resolveSessionId(this, spanCtx), spanCtx);
478
+ };
479
+ }
434
480
  const wrappedHandler = (req, extra) => context2.with(
435
481
  spanCtx,
436
- () => executeHandler(span, method, handler, req, extra, postResult)
482
+ () => executeHandler(span, method, handler, req, extra, hooks)
437
483
  );
438
484
  const hadEntry = handlers.has(method);
439
485
  const prev = handlers.get(method);
@@ -457,41 +503,39 @@ function wrapOnRequest(original, tracer) {
457
503
  }
458
504
  };
459
505
  }
460
- function executeAroundSpan(span, method, run, postResult) {
506
+ function executeAroundSpan(span, method, run, hooks) {
461
507
  let result;
462
508
  try {
463
509
  result = run();
464
510
  } catch (error) {
465
511
  safeApplyErrorAttributes(span, error);
512
+ hooks?.onError?.();
466
513
  safeEnd(span);
467
514
  throw error;
468
515
  }
469
516
  if (!isThenable(result)) {
470
517
  safeApplyResultAttributes(span, method, result);
471
- if (postResult) {
472
- postResult(result);
473
- }
518
+ hooks?.onResult?.(result);
474
519
  safeEnd(span);
475
520
  return result;
476
521
  }
477
522
  return result.then(
478
523
  (value) => {
479
524
  safeApplyResultAttributes(span, method, value);
480
- if (postResult) {
481
- postResult(value);
482
- }
525
+ hooks?.onResult?.(value);
483
526
  safeEnd(span);
484
527
  return value;
485
528
  },
486
529
  (error) => {
487
530
  safeApplyErrorAttributes(span, error);
531
+ hooks?.onError?.();
488
532
  safeEnd(span);
489
533
  throw error;
490
534
  }
491
535
  );
492
536
  }
493
- function executeHandler(span, method, handler, req, extra, postResult) {
494
- return executeAroundSpan(span, method, () => handler(req, extra), postResult);
537
+ function executeHandler(span, method, handler, req, extra, hooks) {
538
+ return executeAroundSpan(span, method, () => handler(req, extra), hooks);
495
539
  }
496
540
  function safeStartClientSpan(tracer, request, protocol) {
497
541
  try {
@@ -518,8 +562,8 @@ function safeStartServerSpan(tracer, request, protocol) {
518
562
  parentCtx
519
563
  );
520
564
  applyServerRequestAttributes(span, request, protocol);
521
- const sessionId = protocol.sessionId ?? protocol._transport?.sessionId;
522
- const { context: sessCtx } = stampAndPropagateSession(span, sessionId, parentCtx);
565
+ const resolved = request.method === METHOD_TOOLS_CALL ? resolveSessionId(protocol, parentCtx) : void 0;
566
+ const { context: sessCtx } = stampAndPropagateSession(span, resolved, parentCtx);
523
567
  return { span, spanCtx: trace.setSpan(sessCtx, span) };
524
568
  } catch (error) {
525
569
  logger.debug(`Brizz MCP: failed to open SERVER span: ${String(error)}`);
@@ -2,7 +2,7 @@ import './internal/instrumentation/auto-init.js';
2
2
  export { Brizz, type IBrizzInitializeOptions } from './internal/sdk.js';
3
3
  export { type IInstrumentModules, type IMCPManualModules, } from './internal/instrumentation/index.js';
4
4
  export { emitEvent, emitEventWithSessionId } from './internal/log/index.js';
5
- export { getSpanExporter, getSpanProcessor, withSessionId, callWithSessionId, withProperties, callWithProperties, withMute, callWithMute, setCurrentSpanCustomProperties, setUser, setOrganization, setMessageId, withUser, callWithUser, withOrganization, callWithOrganization, withMessageId, callWithMessageId, Session, SessionTitle, startSession, startSessionTitle, getActiveSession, addExternalLink, recordFeedback, recordMetric, } from './internal/trace/index.js';
5
+ export { getSpanExporter, getSpanProcessor, withSessionId, callWithSessionId, withProperties, callWithProperties, withMute, callWithMute, setCurrentSpanCustomProperties, setUser, setOrganization, setMessageId, setSessionId, withUser, callWithUser, withOrganization, callWithOrganization, withMessageId, callWithMessageId, Session, SessionTitle, startSession, startSessionTitle, getActiveSession, addExternalLink, recordFeedback, recordMetric, } from './internal/trace/index.js';
6
6
  export type { IMuteOptions } from './internal/trace/index.js';
7
7
  export { getMetricsExporter, getMetricsReader } from './internal/metric/index.js';
8
8
  export type { IMaskingConfig, ISpanMaskingConfig, ILogMaskingConfig, IEventMaskingConfig, IEventMaskingRule, MaskingMode, IAttributesMaskingRule, } from './internal/masking/index.js';
@@ -3,4 +3,6 @@ export interface ISessionAttachResult {
3
3
  context: Context;
4
4
  sessionId: string | null;
5
5
  }
6
+ export declare function resolveTransportSessionId(protocol: unknown): string | undefined;
7
+ export declare function resolveSessionId(protocol: unknown, baseContext: Context): string | undefined;
6
8
  export declare function stampAndPropagateSession(span: Span, sessionId: string | null | undefined, baseContext?: Context): ISessionAttachResult;
@@ -1,4 +1,4 @@
1
1
  export { TracingModule, getSpanExporter, getSpanProcessor } from './tracing.js';
2
2
  export { BrizzBatchSpanProcessor, BrizzSimpleSpanProcessor } from './processors/span-processor.js';
3
- export { withSessionId, callWithSessionId, withProperties, callWithProperties, withMute, callWithMute, setCurrentSpanCustomProperties, setUser, setOrganization, setMessageId, withUser, callWithUser, withOrganization, callWithOrganization, withMessageId, callWithMessageId, Session, SessionTitle, startSession, startSessionTitle, getActiveSession, addExternalLink, recordFeedback, recordMetric, } from './session.js';
3
+ export { withSessionId, callWithSessionId, withProperties, callWithProperties, withMute, callWithMute, setCurrentSpanCustomProperties, setUser, setOrganization, setMessageId, setSessionId, withUser, callWithUser, withOrganization, callWithOrganization, withMessageId, callWithMessageId, Session, SessionTitle, startSession, startSessionTitle, getActiveSession, addExternalLink, recordFeedback, recordMetric, } from './session.js';
4
4
  export type { IMuteOptions } from './session.js';
@@ -22,6 +22,7 @@ export declare function setOrganization(org: {
22
22
  traits?: Record<string, string>;
23
23
  }): void;
24
24
  export declare function setMessageId(id: string): void;
25
+ export declare function setSessionId(id: string): void;
25
26
  export declare function callWithUser<A extends unknown[], F extends (...args: A) => ReturnType<F>>(user: {
26
27
  id: string;
27
28
  email?: string;
@@ -2,7 +2,7 @@ import './internal/instrumentation/auto-init.js';
2
2
  export { Brizz, type IBrizzInitializeOptions } from './internal/sdk.js';
3
3
  export { type IInstrumentModules, type IMCPManualModules, } from './internal/instrumentation/index.js';
4
4
  export { emitEvent, emitEventWithSessionId } from './internal/log/index.js';
5
- export { getSpanExporter, getSpanProcessor, withSessionId, callWithSessionId, withProperties, callWithProperties, withMute, callWithMute, setCurrentSpanCustomProperties, setUser, setOrganization, setMessageId, withUser, callWithUser, withOrganization, callWithOrganization, withMessageId, callWithMessageId, Session, SessionTitle, startSession, startSessionTitle, getActiveSession, addExternalLink, recordFeedback, recordMetric, } from './internal/trace/index.js';
5
+ export { getSpanExporter, getSpanProcessor, withSessionId, callWithSessionId, withProperties, callWithProperties, withMute, callWithMute, setCurrentSpanCustomProperties, setUser, setOrganization, setMessageId, setSessionId, withUser, callWithUser, withOrganization, callWithOrganization, withMessageId, callWithMessageId, Session, SessionTitle, startSession, startSessionTitle, getActiveSession, addExternalLink, recordFeedback, recordMetric, } from './internal/trace/index.js';
6
6
  export type { IMuteOptions } from './internal/trace/index.js';
7
7
  export { getMetricsExporter, getMetricsReader } from './internal/metric/index.js';
8
8
  export type { IMaskingConfig, ISpanMaskingConfig, ILogMaskingConfig, IEventMaskingConfig, IEventMaskingRule, MaskingMode, IAttributesMaskingRule, } from './internal/masking/index.js';
@@ -3,4 +3,6 @@ export interface ISessionAttachResult {
3
3
  context: Context;
4
4
  sessionId: string | null;
5
5
  }
6
+ export declare function resolveTransportSessionId(protocol: unknown): string | undefined;
7
+ export declare function resolveSessionId(protocol: unknown, baseContext: Context): string | undefined;
6
8
  export declare function stampAndPropagateSession(span: Span, sessionId: string | null | undefined, baseContext?: Context): ISessionAttachResult;
@@ -1,4 +1,4 @@
1
1
  export { TracingModule, getSpanExporter, getSpanProcessor } from './tracing.js';
2
2
  export { BrizzBatchSpanProcessor, BrizzSimpleSpanProcessor } from './processors/span-processor.js';
3
- export { withSessionId, callWithSessionId, withProperties, callWithProperties, withMute, callWithMute, setCurrentSpanCustomProperties, setUser, setOrganization, setMessageId, withUser, callWithUser, withOrganization, callWithOrganization, withMessageId, callWithMessageId, Session, SessionTitle, startSession, startSessionTitle, getActiveSession, addExternalLink, recordFeedback, recordMetric, } from './session.js';
3
+ export { withSessionId, callWithSessionId, withProperties, callWithProperties, withMute, callWithMute, setCurrentSpanCustomProperties, setUser, setOrganization, setMessageId, setSessionId, withUser, callWithUser, withOrganization, callWithOrganization, withMessageId, callWithMessageId, Session, SessionTitle, startSession, startSessionTitle, getActiveSession, addExternalLink, recordFeedback, recordMetric, } from './session.js';
4
4
  export type { IMuteOptions } from './session.js';
@@ -22,6 +22,7 @@ export declare function setOrganization(org: {
22
22
  traits?: Record<string, string>;
23
23
  }): void;
24
24
  export declare function setMessageId(id: string): void;
25
+ export declare function setSessionId(id: string): void;
25
26
  export declare function callWithUser<A extends unknown[], F extends (...args: A) => ReturnType<F>>(user: {
26
27
  id: string;
27
28
  email?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brizz/sdk",
3
- "version": "0.1.39",
3
+ "version": "0.1.41",
4
4
  "type": "module",
5
5
  "description": "OpenTelemetry-based observability SDK for AI applications",
6
6
  "keywords": [