@mcp-b/webmcp-ts-sdk 2.3.2 → 3.0.0

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.js CHANGED
@@ -5053,6 +5053,9 @@ function toStructuredContent(value) {
5053
5053
  if (!isPlainObject$1(value) || !isJsonValue(value)) return;
5054
5054
  return value;
5055
5055
  }
5056
+ function isPromiseLike(value) {
5057
+ return Boolean(value) && (typeof value === "object" || typeof value === "function") && typeof value.then === "function";
5058
+ }
5056
5059
  function serializeTextContent(value) {
5057
5060
  if (typeof value === "string") return value;
5058
5061
  try {
@@ -5087,8 +5090,8 @@ function withDefaultTimeout(options) {
5087
5090
  * (resources, prompts, elicitation, sampling) via BaseMcpServer.
5088
5091
  *
5089
5092
  * Deprecated compatibility (kept for Chrome Beta 147 and existing wrappers,
5090
- * removed in the next major): `unregisterTool(name)`, `provideContext()`,
5091
- * `clearContext()`, and the `{ unregister }` handle returned from `registerTool`.
5093
+ * removed in the next major): `unregisterTool(name)` and the `{ unregister }`
5094
+ * handle returned from `registerTool`.
5092
5095
  *
5093
5096
  * When `native` is provided, tool operations are mirrored so that
5094
5097
  * navigator.modelContextTesting stays in sync.
@@ -5099,8 +5102,6 @@ var BrowserMcpServer = class extends McpServer {
5099
5102
  _promptSchemas = /* @__PURE__ */ new Map();
5100
5103
  _jsonValidator;
5101
5104
  _publicMethodsBound = false;
5102
- _provideContextDeprecationWarned = false;
5103
- _clearContextDeprecationWarned = false;
5104
5105
  _unregisterToolDeprecationWarned = false;
5105
5106
  _nativeToolCleanups = /* @__PURE__ */ new Map();
5106
5107
  _producerEventTarget = new EventTarget();
@@ -5128,8 +5129,6 @@ var BrowserMcpServer = class extends McpServer {
5128
5129
  if (this._publicMethodsBound) return;
5129
5130
  this.registerTool = this.registerTool.bind(this);
5130
5131
  this.unregisterTool = this.unregisterTool.bind(this);
5131
- this.provideContext = this.provideContext.bind(this);
5132
- this.clearContext = this.clearContext.bind(this);
5133
5132
  this.listTools = this.listTools.bind(this);
5134
5133
  this.getTools = this.getTools.bind(this);
5135
5134
  this.callTool = this.callTool.bind(this);
@@ -5229,9 +5228,10 @@ var BrowserMcpServer = class extends McpServer {
5229
5228
  const nativeRegister = this.native.registerTool;
5230
5229
  const nativeUnregisterTool = this.getNativeUnregisterTool();
5231
5230
  const cleanup = Boolean(signal) || !nativeUnregisterTool ? this.createNativeToolCleanup(signal) : void 0;
5231
+ let nativeRegisterResult;
5232
5232
  try {
5233
- if (cleanup) nativeRegister.call(this.native, tool, cleanup.options);
5234
- else nativeRegister.call(this.native, tool);
5233
+ if (cleanup) nativeRegisterResult = nativeRegister.call(this.native, tool, cleanup.options);
5234
+ else nativeRegisterResult = nativeRegister.call(this.native, tool);
5235
5235
  } catch (error) {
5236
5236
  cleanup?.abort();
5237
5237
  if (isPermissionsPolicySecurityError(error)) {
@@ -5246,6 +5246,15 @@ var BrowserMcpServer = class extends McpServer {
5246
5246
  nativeSignalAccepted: nativeRegister.length >= 2 || !nativeUnregisterTool
5247
5247
  };
5248
5248
  this._nativeToolCleanups.set(tool.name, nativeToolCleanup);
5249
+ if (isPromiseLike(nativeRegisterResult)) nativeRegisterResult.then(void 0, (error) => {
5250
+ cleanup.abort();
5251
+ if (this._nativeToolCleanups.get(tool.name) === nativeToolCleanup) this._nativeToolCleanups.delete(tool.name);
5252
+ if (isPermissionsPolicySecurityError(error)) {
5253
+ console.warn("[BrowserMcpServer] Native WebMCP tool mirror is blocked by permissions policy; continuing with WebMCP transport registration only.");
5254
+ return;
5255
+ }
5256
+ console.warn("[BrowserMcpServer] Native WebMCP tool mirror registration rejected; continuing with WebMCP transport registration only.", error);
5257
+ });
5249
5258
  return nativeToolCleanup;
5250
5259
  }
5251
5260
  unregisterNativeToolMirror(name, options) {
@@ -5368,19 +5377,6 @@ var BrowserMcpServer = class extends McpServer {
5368
5377
  if (this.native) this.unregisterNativeToolMirror(name);
5369
5378
  if (existing) this.notifyProducerToolsChanged();
5370
5379
  }
5371
- clearRegisteredTools() {
5372
- let changed = false;
5373
- for (const name of Object.keys(this._parentTools)) {
5374
- this._parentTools[name]?.remove();
5375
- changed = true;
5376
- if (this.native) try {
5377
- this.unregisterNativeToolMirror(name);
5378
- } catch (error) {
5379
- console.warn("[BrowserMcpServer] Native unregister during clear failed:", error);
5380
- }
5381
- }
5382
- if (changed) this.notifyProducerToolsChanged();
5383
- }
5384
5380
  registerResource(descriptor) {
5385
5381
  const registered = super.registerResource(descriptor.name, descriptor.uri, {
5386
5382
  ...descriptor.description !== void 0 && { description: descriptor.description },
@@ -5396,30 +5392,11 @@ var BrowserMcpServer = class extends McpServer {
5396
5392
  registered.remove();
5397
5393
  } };
5398
5394
  }
5399
- provideContext(options) {
5400
- this.warnProvideContextDeprecationOnce();
5401
- this.clearRegisteredTools();
5402
- for (const tool of options?.tools ?? []) this.registerTool(tool);
5403
- }
5404
- clearContext() {
5405
- this.warnClearContextDeprecationOnce();
5406
- this.clearRegisteredTools();
5407
- }
5408
5395
  resolveToolNameForUnregister(nameOrTool) {
5409
5396
  if (typeof nameOrTool === "string") return nameOrTool;
5410
5397
  if (isPlainObject$1(nameOrTool) && typeof nameOrTool.name === "string") return nameOrTool.name;
5411
5398
  throw new TypeError("Failed to execute 'unregisterTool' on 'ModelContext': parameter 1 must be a string or an object with a string name.");
5412
5399
  }
5413
- warnProvideContextDeprecationOnce() {
5414
- if (this._provideContextDeprecationWarned) return;
5415
- this._provideContextDeprecationWarned = true;
5416
- console.warn("[BrowserMcpServer] navigator.modelContext.provideContext() is deprecated and will be removed in the next major version. Register tools individually with registerTool() instead.");
5417
- }
5418
- warnClearContextDeprecationOnce() {
5419
- if (this._clearContextDeprecationWarned) return;
5420
- this._clearContextDeprecationWarned = true;
5421
- console.warn("[BrowserMcpServer] navigator.modelContext.clearContext() is deprecated and will be removed in the next major version. Unregister individual tools instead.");
5422
- }
5423
5400
  warnUnregisterToolDeprecationOnce() {
5424
5401
  if (this._unregisterToolDeprecationWarned) return;
5425
5402
  this._unregisterToolDeprecationWarned = true;
@@ -5473,7 +5450,9 @@ var BrowserMcpServer = class extends McpServer {
5473
5450
  return item;
5474
5451
  });
5475
5452
  }
5476
- getTools() {
5453
+ async getTools() {
5454
+ const origin = typeof globalThis.location === "object" && typeof globalThis.location?.origin === "string" ? globalThis.location.origin : "";
5455
+ const currentWindow = typeof globalThis.window === "object" ? globalThis.window : void 0;
5477
5456
  return this.listTools().map((tool) => {
5478
5457
  let inputSchema;
5479
5458
  try {
@@ -5483,8 +5462,11 @@ var BrowserMcpServer = class extends McpServer {
5483
5462
  }
5484
5463
  return {
5485
5464
  name: tool.name,
5465
+ title: typeof tool.annotations?.title === "string" ? tool.annotations.title : tool.description ?? "",
5486
5466
  description: tool.description ?? "",
5487
- inputSchema
5467
+ inputSchema,
5468
+ origin,
5469
+ window: currentWindow
5488
5470
  };
5489
5471
  });
5490
5472
  }
@@ -5523,11 +5505,17 @@ var BrowserMcpServer = class extends McpServer {
5523
5505
  if (!tool) throw new Error(`Tool not found: ${params.name}`);
5524
5506
  return tool.handler(params.arguments ?? {}, {});
5525
5507
  }
5526
- async executeTool(name, args = {}) {
5527
- return this.callTool({
5528
- name,
5529
- arguments: args
5508
+ async executeTool(toolOrName, inputArgsJsonOrArgs = {}) {
5509
+ if (typeof toolOrName === "string") return this.callTool({
5510
+ name: toolOrName,
5511
+ arguments: typeof inputArgsJsonOrArgs === "string" ? JSON.parse(inputArgsJsonOrArgs) : inputArgsJsonOrArgs
5512
+ });
5513
+ const result = await this.callTool({
5514
+ name: toolOrName.name,
5515
+ arguments: typeof inputArgsJsonOrArgs === "string" ? JSON.parse(inputArgsJsonOrArgs) : inputArgsJsonOrArgs
5530
5516
  });
5517
+ const serialized = JSON.stringify(result);
5518
+ return serialized === void 0 ? null : serialized;
5531
5519
  }
5532
5520
  /**
5533
5521
  * Override connect to initialize request handlers BEFORE the transport connection.