@mcp-b/webmcp-types 4.0.0 → 5.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.
@@ -1,441 +1,164 @@
1
- import type { InputSchema, ToolResponse } from './common.js';
1
+ import type { InputSchema, WebMcpToolInput } from './common.js';
2
2
  import type { JsonSchemaForInference } from './json-schema.js';
3
3
  import type {
4
+ ModelContextTool,
5
+ ModelContextToolFromSchema,
4
6
  ToolDescriptor,
5
7
  ToolDescriptorFromSchema,
6
8
  ToolListItem,
7
- ToolRawResult,
9
+ WebMcpToolAnnotations,
8
10
  } from './tool.js';
9
11
 
10
- // ============================================================================
11
- // Model Context Testing
12
- // ============================================================================
13
-
14
- /**
15
- * Tool info returned by ModelContextTesting.listTools().
16
- */
17
- export interface ModelContextTestingToolInfo {
18
- name: string;
19
- description: string;
20
- inputSchema?: string;
12
+ /** Options for tools exposed by descendant documents. */
13
+ export interface ModelContextGetToolOptions {
14
+ fromOrigins?: string[];
21
15
  }
22
16
 
23
- /**
24
- * Tool info returned by Chromium's producer-facing ModelContext.getTools().
25
- */
26
- export interface ModelContextToolInfo extends ModelContextTestingToolInfo {
27
- title: string;
28
- origin: string;
29
- window: Window;
30
- }
31
-
32
- /**
33
- * Options supported by ModelContextTesting.executeTool().
34
- */
35
- export interface ModelContextTestingExecuteToolOptions {
36
- signal?: AbortSignal;
37
- }
38
-
39
- /**
40
- * Chromium testing API on navigator.modelContextTesting.
41
- *
42
- * The native runtime extends EventTarget and fires `toolchange` events.
43
- */
44
- export interface ModelContextTesting extends EventTarget {
45
- listTools(): ModelContextTestingToolInfo[];
46
- executeTool(
47
- toolName: string,
48
- inputArgsJson: string,
49
- options?: ModelContextTestingExecuteToolOptions
50
- ): Promise<string | null>;
51
- getCrossDocumentScriptToolResult?(): Promise<string>;
52
- ontoolchange: ((this: ModelContextTesting, ev: Event) => unknown) | null;
17
+ /** Tool metadata returned by `document.modelContext.getTools()`. */
18
+ export interface RegisteredTool {
19
+ name: string;
53
20
  /**
54
- * @deprecated Use `addEventListener('toolchange', ...)` instead.
21
+ * Present on every tool Chrome and `@mcp-b/webmcp-polyfill` return today --
22
+ * the spec defaults it to the empty string when a tool registers no title.
23
+ * Kept optional deliberately: webmcp#224 proposes dropping that default and
24
+ * omitting the member instead. Read it as `tool.title || tool.name`: today the
25
+ * spec default makes it an empty string, which `??` does not fall through.
55
26
  */
56
- registerToolsChangedCallback?(callback: () => void): void;
57
- }
58
-
59
- /**
60
- * Polyfill-only testing extensions layered on top of ModelContextTesting.
61
- */
62
- export interface ModelContextTestingPolyfillExtensions {
27
+ title?: string;
28
+ description: string;
63
29
  /**
64
- * Registers a callback invoked when the tool list changes.
65
- * @deprecated Use `addEventListener('toolchange', ...)` on native runtimes.
66
- * Kept for polyfill backward compatibility.
30
+ * A JSON Schema object since webmcp#241, rolling out from Chrome 154.0.8013
31
+ * (cross-document tools first). Chrome 149-153 -- most of the Origin Trial
32
+ * population -- and 154's same-document tools still return the serialized
33
+ * JSON string the change replaced. Consumers that support both generations
34
+ * must branch on `typeof` and guard the parse of the string arm.
67
35
  */
68
- registerToolsChangedCallback(callback: () => void): void;
69
- getToolCalls(): Array<{
70
- toolName: string;
71
- arguments: Record<string, unknown>;
72
- timestamp: number;
73
- }>;
74
- clearToolCalls(): void;
75
- setMockToolResponse(toolName: string, response: ToolResponse): void;
76
- clearMockToolResponse(toolName: string): void;
77
- clearAllMockToolResponses(): void;
78
- getRegisteredTools(): ReturnType<ModelContextExtensions['listTools']>;
79
- reset(): void;
80
- }
81
-
82
- // ============================================================================
83
- // Type Inference Helpers
84
- // ============================================================================
85
-
86
- /**
87
- * Any supported tool descriptor.
88
- *
89
- * Uses `never` for args so descriptors with stricter argument objects remain assignable.
90
- */
91
- export type AnyToolDescriptor = ToolDescriptor<never, unknown, string>;
92
-
93
- /**
94
- * Infers argument shape from a tool descriptor.
95
- */
96
- export type InferToolArgs<TTool> =
97
- TTool extends ToolDescriptor<infer TArgs, infer _TResult, infer _TName> ? TArgs : never;
98
-
99
- /**
100
- * Infers result shape from a tool descriptor.
101
- */
102
- export type InferToolResult<TTool> =
103
- TTool extends ToolDescriptor<infer _TArgs, infer TResult, infer _TName> ? TResult : never;
104
-
105
- /**
106
- * Union of tool names from a tuple/array of descriptors.
107
- */
108
- export type ToolName<TTools extends readonly { name: string }[]> = TTools[number]['name'];
109
-
110
- /**
111
- * Extracts a tool descriptor by name from a tuple/array.
112
- */
113
- export type ToolByName<
114
- TTools extends readonly { name: string }[],
115
- TName extends ToolName<TTools>,
116
- > = Extract<TTools[number], { name: TName }>;
117
-
118
- /**
119
- * Tool argument type by tool name from a tuple/array.
120
- */
121
- export type ToolArgsByName<
122
- TTools extends readonly { name: string }[],
123
- TName extends ToolName<TTools>,
124
- > = InferToolArgs<ToolByName<TTools, TName>>;
125
-
126
- /**
127
- * Tool result type by tool name from a tuple/array.
128
- */
129
- export type ToolResultByName<
130
- TTools extends readonly { name: string }[],
131
- TName extends ToolName<TTools>,
132
- > = InferToolResult<ToolByName<TTools, TName>>;
133
-
134
- /**
135
- * Typed parameters for MCP-B compatibility `callTool`.
136
- *
137
- * When a tool has no args (`Record<string, never>`), `arguments` becomes optional.
138
- */
139
- export type ToolCallParams<
140
- TName extends string = string,
141
- TArgs extends Record<string, unknown> = Record<string, never>,
142
- > = { name: TName } & (TArgs extends Record<string, never>
143
- ? { arguments?: TArgs }
144
- : { arguments: TArgs });
145
-
146
- // ============================================================================
147
- // Tool Call Event
148
- // ============================================================================
149
-
150
- /**
151
- * Event dispatched when a tool is called.
152
- */
153
- export interface ToolCallEvent extends Event {
36
+ inputSchema?: InputSchema | string;
37
+ window: Window;
38
+ origin: string;
154
39
  /**
155
- * Tool name being invoked.
40
+ * Absent when the tool registered no annotations -- both the spec and the
41
+ * polyfill omit the member rather than emitting an empty object. Each hint
42
+ * inside it is always populated, but the object itself is not guaranteed.
156
43
  */
157
- name: string;
44
+ annotations?: WebMcpToolAnnotations;
45
+ }
158
46
 
159
- /**
160
- * Tool arguments supplied by the caller.
161
- */
162
- arguments: Record<string, unknown>;
47
+ export interface ChromeModelContextExecuteToolOptions {
48
+ signal?: AbortSignal;
49
+ }
163
50
 
164
- /**
165
- * Intercepts execution with a custom tool response.
166
- */
167
- respondWith: (response: ToolResponse) => void;
51
+ /** Experimental Chromium methods; feature-detect them before use. */
52
+ export interface ChromeModelContextExtensions {
53
+ executeTool?(
54
+ tool: RegisteredTool,
55
+ inputArguments: string,
56
+ options?: ChromeModelContextExecuteToolOptions
57
+ ): Promise<string | null>;
168
58
  }
169
59
 
170
- /**
171
- * Tool identity accepted by compatibility unregister flows.
172
- *
173
- * Current Chromium exposes string-name unregistration.
174
- * MCP-B compatibility runtimes may also accept the originally registered tool object.
175
- */
176
- export interface ModelContextToolReference {
60
+ /** @deprecated Metadata returned by `navigator.modelContextTesting`. */
61
+ export interface ModelContextTestingToolInfo {
177
62
  name: string;
63
+ description: string;
64
+ inputSchema?: string;
178
65
  }
179
66
 
180
- /**
181
- * Non-standard compatibility handle returned by some runtimes after registerTool().
182
- *
183
- * @deprecated Use `options.signal` on `registerTool(tool, options)`. Will be removed in the next major.
184
- */
185
- export interface ModelContextToolRegistrationHandle {
186
- unregister(): void;
67
+ /** @deprecated Compatibility surface for Chromium's removed testing API. */
68
+ export interface ModelContextTesting extends EventTarget {
69
+ listTools(): ModelContextTestingToolInfo[];
70
+ executeTool(
71
+ toolName: string,
72
+ inputArgsJson: string,
73
+ options?: ChromeModelContextExecuteToolOptions
74
+ ): Promise<string | null>;
75
+ ontoolchange: ((this: ModelContextTesting, event: Event) => unknown) | null;
187
76
  }
188
77
 
189
- /**
190
- * @see {@link https://webmachinelearning.github.io/webmcp/#dictdef-modelcontextregistertooloptions}
191
- */
78
+ /** Options accepted by `ModelContext.registerTool()`. */
192
79
  export interface ModelContextRegisterToolOptions {
193
- /**
194
- * An `AbortSignal` whose abortion unregisters the tool. A pre-aborted signal short-circuits registration.
195
- */
196
80
  signal?: AbortSignal;
197
-
198
- /**
199
- * Origins that can observe this tool from other documents in the same tree.
200
- */
201
81
  exposedTo?: string[];
202
82
  }
203
83
 
204
- // ============================================================================
205
- // Model Context
206
- // ============================================================================
84
+ type WidenedSchema<TSchema extends InputSchema> = string extends TSchema['type'] ? unknown : never;
207
85
 
208
86
  /**
209
- * Strict WebMCP core interface on document.modelContext.
87
+ * Standard `document.modelContext` API.
88
+ * @see https://webmachinelearning.github.io/webmcp/#modelcontext
210
89
  */
211
- export interface ModelContextCore {
212
- // ==================== TOOLS ====================
213
-
214
- /**
215
- * Registers a dynamic tool with JSON Schema-driven inference.
216
- *
217
- * `execute(args)` is inferred from `inputSchema`, and when a literal object
218
- * `outputSchema` is provided, `execute(...).structuredContent` is inferred too.
219
- */
90
+ export interface ModelContext extends EventTarget {
220
91
  registerTool<
221
- TInputSchema extends JsonSchemaForInference,
222
- TOutputSchema extends JsonSchemaForInference | undefined = undefined,
92
+ const TInputSchema extends JsonSchemaForInference,
93
+ TResult = unknown,
223
94
  TName extends string = string,
224
95
  >(
225
- tool: ToolDescriptorFromSchema<TInputSchema, TOutputSchema, TName>,
96
+ tool: ModelContextToolFromSchema<TInputSchema, TResult, TName>,
226
97
  options?: ModelContextRegisterToolOptions
227
98
  ): Promise<void>;
228
99
 
229
- /**
230
- * Registers a dynamic tool with explicitly typed args/result.
231
- */
232
100
  registerTool<
233
101
  TInputSchema extends InputSchema,
234
- TArgs extends Record<string, unknown> = Record<string, unknown>,
102
+ TArgs extends WebMcpToolInput = WebMcpToolInput,
103
+ TResult = unknown,
235
104
  TName extends string = string,
236
105
  >(
237
- tool: ToolDescriptor<TArgs, ToolRawResult, TName> & {
106
+ tool: ModelContextTool<TArgs, TResult, TName> & {
238
107
  inputSchema: TInputSchema;
239
- } & (TInputSchema extends InputSchema
240
- ? string extends TInputSchema['type']
241
- ? unknown
242
- : never
243
- : unknown),
108
+ } & WidenedSchema<TInputSchema>,
244
109
  options?: ModelContextRegisterToolOptions
245
110
  ): Promise<void>;
246
111
 
247
- /**
248
- * Registers a dynamic tool without an explicit inputSchema.
249
- * Runtime defaults this to an empty object schema.
250
- */
251
112
  registerTool<
252
- TArgs extends Record<string, unknown> = Record<string, unknown>,
113
+ TArgs extends WebMcpToolInput = Record<string, unknown>,
114
+ TResult = unknown,
253
115
  TName extends string = string,
254
116
  >(
255
- tool: Omit<ToolDescriptor<TArgs, ToolRawResult, TName>, 'inputSchema'> & {
117
+ tool: Omit<ModelContextTool<TArgs, TResult, TName>, 'inputSchema'> & {
256
118
  inputSchema?: undefined;
257
119
  },
258
120
  options?: ModelContextRegisterToolOptions
259
121
  ): Promise<void>;
260
122
 
261
- /**
262
- * Lists currently registered producer tools using the native Chromium preview
263
- * shape. Current Chromium returns JSON-stringified schemas here, matching
264
- * ModelContextTesting.listTools().
265
- */
266
- getTools(): Promise<ModelContextToolInfo[]>;
267
-
268
- /**
269
- * Executes a registered tool object returned from getTools().
270
- *
271
- * This mirrors Chromium's current producer-facing preview API. The testing API
272
- * still executes by `(toolName, inputArgsJson)`.
273
- */
274
- executeTool(
275
- tool: ModelContextToolInfo,
276
- inputArgsJson: string,
277
- options?: ModelContextTestingExecuteToolOptions
278
- ): Promise<string | null>;
279
-
280
- /**
281
- * Handler invoked when the producer tool list changes.
282
- */
283
- ontoolchange: ((this: ModelContextCore, ev: Event) => unknown) | null;
284
-
285
- addEventListener(
286
- type: 'toolchange',
287
- listener: () => void,
288
- options?: boolean | AddEventListenerOptions
289
- ): void;
290
-
291
- removeEventListener(
292
- type: 'toolchange',
293
- listener: () => void,
294
- options?: boolean | EventListenerOptions
295
- ): void;
296
-
297
- dispatchEvent(event: Event): boolean;
123
+ getTools(options?: ModelContextGetToolOptions): Promise<RegisteredTool[]>;
124
+ ontoolchange: ((this: ModelContext, event: Event) => unknown) | null;
298
125
  }
299
126
 
300
- /**
301
- * MCPB extension surface layered on top of strict WebMCP core.
302
- * These members are intentionally non-standard.
303
- */
127
+ /** Non-standard methods exposed by MCP-B runtimes. */
304
128
  export interface ModelContextExtensions {
305
- /**
306
- * Unregisters a dynamic tool by name or tool reference.
307
- *
308
- * @deprecated Removed from the WebMCP spec on April 23, 2026. Use `registerTool(tool, { signal })`. Will be removed in the next major.
309
- */
310
- unregisterTool(nameOrTool: string | ModelContextToolReference): void;
311
-
312
- /**
313
- * Lists currently registered tools.
314
- */
315
- listTools(): ToolListItem[];
316
-
317
- /**
318
- * Executes a registered tool by name.
319
- *
320
- * @deprecated Prefer the WebMCP standard producer path: get a descriptor from
321
- * `getTools()` and pass it to `executeTool(tool, inputArgsJson)`. This method
322
- * remains as an MCP-B compatibility convenience.
323
- */
324
- callTool<
129
+ registerTool<
130
+ const TInputSchema extends JsonSchemaForInference,
131
+ const TOutputSchema extends JsonSchemaForInference | undefined = undefined,
325
132
  TName extends string = string,
326
- TArgs extends Record<string, unknown> = Record<string, unknown>,
327
- >(params: {
328
- name: TName;
329
- arguments?: TArgs;
330
- }): Promise<ToolResponse>;
331
-
332
- // ==================== EVENTS ====================
333
-
334
- /**
335
- * Adds a listener for tool invocation events.
336
- */
337
- addEventListener(
338
- type: 'toolcall',
339
- listener: (event: ToolCallEvent) => void | Promise<void>,
340
- options?: boolean | AddEventListenerOptions
341
- ): void;
342
-
343
- /**
344
- * Adds a listener for tool list changes.
345
- */
346
- addEventListener(
347
- type: 'toolchange',
348
- listener: () => void,
349
- options?: boolean | AddEventListenerOptions
350
- ): void;
351
-
352
- /**
353
- * @deprecated Use `'toolchange'` instead. Will be removed in the next major.
354
- */
355
- addEventListener(
356
- type: 'toolschanged',
357
- listener: () => void,
358
- options?: boolean | AddEventListenerOptions
359
- ): void;
360
-
361
- /**
362
- * Removes a listener for tool invocation events.
363
- */
364
- removeEventListener(
365
- type: 'toolcall',
366
- listener: (event: ToolCallEvent) => void | Promise<void>,
367
- options?: boolean | EventListenerOptions
368
- ): void;
133
+ >(
134
+ tool: ToolDescriptorFromSchema<TInputSchema, TOutputSchema, TName>,
135
+ options?: ModelContextRegisterToolOptions
136
+ ): Promise<void>;
369
137
 
370
- /**
371
- * Removes a listener for tool list changes.
372
- */
373
- removeEventListener(
374
- type: 'toolchange',
375
- listener: () => void,
376
- options?: boolean | EventListenerOptions
377
- ): void;
138
+ registerTool<
139
+ TInputSchema extends InputSchema,
140
+ TArgs extends WebMcpToolInput = WebMcpToolInput,
141
+ TName extends string = string,
142
+ >(
143
+ tool: ToolDescriptor<TArgs, unknown, TName> & {
144
+ inputSchema: TInputSchema;
145
+ } & WidenedSchema<TInputSchema>,
146
+ options?: ModelContextRegisterToolOptions
147
+ ): Promise<void>;
378
148
 
379
- /**
380
- * @deprecated Use `'toolchange'` instead. Will be removed in the next major.
381
- */
382
- removeEventListener(
383
- type: 'toolschanged',
384
- listener: () => void,
385
- options?: boolean | EventListenerOptions
386
- ): void;
149
+ registerTool<
150
+ TArgs extends WebMcpToolInput = Record<string, unknown>,
151
+ TName extends string = string,
152
+ >(
153
+ tool: Omit<ToolDescriptor<TArgs, unknown, TName>, 'inputSchema'> & {
154
+ inputSchema?: undefined;
155
+ },
156
+ options?: ModelContextRegisterToolOptions
157
+ ): Promise<void>;
387
158
 
388
- /**
389
- * Dispatches an event.
390
- */
391
- dispatchEvent(event: Event): boolean;
159
+ listTools(): ToolListItem[];
392
160
  }
393
161
 
394
- /**
395
- * Public document.modelContext type (strict core only).
396
- */
397
- export type ModelContext = ModelContextCore;
398
-
399
- /**
400
- * Full runtime shape including MCPB extensions.
401
- */
402
- export type ModelContextWithExtensions = ModelContextCore & ModelContextExtensions;
403
-
404
- // ============================================================================
405
- // Typed Model Context
406
- // ============================================================================
407
-
408
- /**
409
- * Strongly-typed `ModelContext` view derived from known tool descriptors.
410
- *
411
- * This is useful when your project has a static tool registry and you want
412
- * name-aware inference for MCP-B compatibility `callTool`.
413
- */
414
- export type TypedModelContext<
415
- TTools extends readonly { name: string }[] = readonly ToolDescriptor[],
416
- > = ModelContextWithExtensions & {
417
- /**
418
- * Executes a known tool with name-aware argument and response inference.
419
- *
420
- * @deprecated Prefer the WebMCP standard producer path: get a descriptor from
421
- * `getTools()` and pass it to `executeTool(tool, inputArgsJson)`. This typed
422
- * convenience remains available for MCP-B compatibility.
423
- */
424
- callTool<TName extends ToolName<TTools>>(
425
- params: ToolCallParams<TName, ToolArgsByName<TTools, TName>>
426
- ): Promise<ToolResultByName<TTools, TName>>;
427
-
428
- /**
429
- * Fallback call signature for unknown or dynamically-discovered tools.
430
- *
431
- * @deprecated Prefer the WebMCP standard producer path: get a descriptor from
432
- * `getTools()` and pass it to `executeTool(tool, inputArgsJson)`. This typed
433
- * convenience remains available for MCP-B compatibility.
434
- */
435
- callTool(params: { name: string; arguments?: Record<string, unknown> }): Promise<ToolResponse>;
436
-
437
- /**
438
- * Lists tools with a narrowed name union.
439
- */
440
- listTools(): Array<ToolListItem<ToolName<TTools>>>;
441
- };
162
+ export type ChromeModelContext = ModelContext & ChromeModelContextExtensions;
163
+ export type ModelContextWithExtensions = Omit<ModelContext, 'registerTool'> &
164
+ ModelContextExtensions;