@mcp-b/webmcp-types 0.0.0-beta-20260216200013

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.
@@ -0,0 +1,290 @@
1
+ import type { JsonPrimitive, JsonValue } from './common.js';
2
+
3
+ /**
4
+ * Primitive JSON Schema `type` values supported by the MVP inference layer.
5
+ */
6
+ export type JsonSchemaPrimitiveType = 'string' | 'number' | 'integer' | 'boolean' | 'null';
7
+
8
+ /**
9
+ * JSON Schema `type` values supported by the MVP inference layer.
10
+ */
11
+ export type JsonSchemaType = JsonSchemaPrimitiveType | 'object' | 'array';
12
+
13
+ /**
14
+ * JSON Schema multi-type tuple (for example `["string", "null"]`).
15
+ */
16
+ export type JsonSchemaTypeArray = readonly [JsonSchemaType, ...JsonSchemaType[]];
17
+
18
+ /**
19
+ * Literal values supported in JSON Schema `enum`/`const`.
20
+ */
21
+ export type JsonSchemaEnumValue = JsonPrimitive | JsonValue;
22
+
23
+ /**
24
+ * Extra JSON Schema keywords tolerated by the inference layer.
25
+ *
26
+ * These keys are intentionally accepted as opaque metadata. Inference only uses
27
+ * the core MVP keywords and ignores these fields.
28
+ */
29
+ interface SupplementalJsonSchemaKeywords {
30
+ $defs?: unknown;
31
+ $ref?: unknown;
32
+ additionalItems?: unknown;
33
+ allOf?: unknown;
34
+ anyOf?: unknown;
35
+ contains?: unknown;
36
+ definitions?: unknown;
37
+ dependentRequired?: unknown;
38
+ dependentSchemas?: unknown;
39
+ format?: unknown;
40
+ if?: unknown;
41
+ maxContains?: unknown;
42
+ minContains?: unknown;
43
+ not?: unknown;
44
+ oneOf?: unknown;
45
+ patternProperties?: unknown;
46
+ prefixItems?: unknown;
47
+ propertyNames?: unknown;
48
+ then?: unknown;
49
+ unevaluatedItems?: unknown;
50
+ unevaluatedProperties?: unknown;
51
+ }
52
+
53
+ /**
54
+ * Non-validation metadata accepted by the MVP inference subset.
55
+ */
56
+ interface JsonSchemaMetadata extends SupplementalJsonSchemaKeywords {
57
+ default?: JsonValue;
58
+ description?: string;
59
+ examples?: readonly JsonValue[];
60
+ /**
61
+ * OpenAPI-compatible nullability marker.
62
+ */
63
+ nullable?: boolean;
64
+ title?: string;
65
+ }
66
+
67
+ /**
68
+ * JSON Schema for `type: "string"`.
69
+ */
70
+ export interface JsonSchemaString extends JsonSchemaMetadata {
71
+ const?: string;
72
+ enum?: readonly string[];
73
+ maxLength?: number;
74
+ minLength?: number;
75
+ pattern?: string;
76
+ type: 'string';
77
+ }
78
+
79
+ /**
80
+ * JSON Schema for `type: "number"` and `type: "integer"`.
81
+ */
82
+ export interface JsonSchemaNumber extends JsonSchemaMetadata {
83
+ const?: number;
84
+ enum?: readonly number[];
85
+ exclusiveMaximum?: number;
86
+ exclusiveMinimum?: number;
87
+ maximum?: number;
88
+ minimum?: number;
89
+ multipleOf?: number;
90
+ type: 'number' | 'integer';
91
+ }
92
+
93
+ /**
94
+ * JSON Schema for `type: "boolean"`.
95
+ */
96
+ export interface JsonSchemaBoolean extends JsonSchemaMetadata {
97
+ const?: boolean;
98
+ enum?: readonly boolean[];
99
+ type: 'boolean';
100
+ }
101
+
102
+ /**
103
+ * JSON Schema for `type: "null"`.
104
+ */
105
+ export interface JsonSchemaNull extends JsonSchemaMetadata {
106
+ const?: null;
107
+ enum?: readonly null[];
108
+ type: 'null';
109
+ }
110
+
111
+ /**
112
+ * JSON Schema for `type: "array"`.
113
+ */
114
+ export interface JsonSchemaArray extends JsonSchemaMetadata {
115
+ items: JsonSchemaForInference;
116
+ maxItems?: number;
117
+ minItems?: number;
118
+ type: 'array';
119
+ uniqueItems?: boolean;
120
+ }
121
+
122
+ /**
123
+ * JSON Schema for `type: "object"`.
124
+ */
125
+ export interface JsonSchemaObject extends JsonSchemaMetadata {
126
+ additionalProperties?: boolean | JsonSchemaForInference;
127
+ maxProperties?: number;
128
+ minProperties?: number;
129
+ properties?: Readonly<Record<string, JsonSchemaForInference>>;
130
+ required?: readonly string[];
131
+ type: 'object';
132
+ }
133
+
134
+ /**
135
+ * JSON Schema for multi-type unions via `type: [...]`.
136
+ */
137
+ export interface JsonSchemaMultiType extends JsonSchemaMetadata {
138
+ additionalProperties?: boolean | JsonSchemaForInference;
139
+ const?: JsonSchemaEnumValue;
140
+ enum?: readonly JsonSchemaEnumValue[];
141
+ items?: JsonSchemaForInference;
142
+ properties?: Readonly<Record<string, JsonSchemaForInference>>;
143
+ required?: readonly string[];
144
+ type: JsonSchemaTypeArray;
145
+ }
146
+
147
+ /**
148
+ * JSON Schema subset supported by the MVP type inference layer.
149
+ */
150
+ export type JsonSchemaForInference =
151
+ | JsonSchemaArray
152
+ | JsonSchemaBoolean
153
+ | JsonSchemaMultiType
154
+ | JsonSchemaNull
155
+ | JsonSchemaNumber
156
+ | JsonSchemaObject
157
+ | JsonSchemaString;
158
+
159
+ type Simplify<T> = { [K in keyof T]: T[K] } & {};
160
+
161
+ type EmptyObject = Record<never, never>;
162
+
163
+ type EnumLiteral<TSchema> = TSchema extends { enum: infer TEnum extends readonly unknown[] }
164
+ ? Extract<TEnum[number], JsonSchemaEnumValue>
165
+ : never;
166
+
167
+ type ConstLiteral<TSchema> = TSchema extends { const: infer TConst }
168
+ ? Extract<TConst, JsonSchemaEnumValue>
169
+ : never;
170
+
171
+ type PropertiesOf<TSchema> = TSchema extends {
172
+ properties: infer TProperties extends Readonly<Record<string, JsonSchemaForInference>>;
173
+ }
174
+ ? TProperties
175
+ : EmptyObject;
176
+
177
+ type RequiredKeysOf<TSchema, TProperties extends Record<string, unknown>> = TSchema extends {
178
+ required: readonly (infer TRequired)[];
179
+ }
180
+ ? string extends TRequired
181
+ ? never
182
+ : Extract<TRequired, keyof TProperties & string>
183
+ : never;
184
+
185
+ type RequiredProps<
186
+ TProperties extends Record<string, JsonSchemaForInference>,
187
+ TRequiredKeys extends string,
188
+ > = {
189
+ [K in keyof TProperties as K extends TRequiredKeys ? K : never]-?: InferJsonSchema<
190
+ TProperties[K]
191
+ >;
192
+ };
193
+
194
+ type OptionalProps<
195
+ TProperties extends Record<string, JsonSchemaForInference>,
196
+ TRequiredKeys extends string,
197
+ > = {
198
+ [K in keyof TProperties as K extends TRequiredKeys ? never : K]?: InferJsonSchema<TProperties[K]>;
199
+ };
200
+
201
+ type PropertyKeysOf<TSchema> = keyof PropertiesOf<TSchema> & string;
202
+
203
+ type AdditionalSchemaOf<TSchema> = TSchema extends { additionalProperties: infer TAdditional }
204
+ ? TAdditional
205
+ : undefined;
206
+
207
+ type AdditionalPropsValue<TSchema> = AdditionalSchemaOf<TSchema> extends JsonSchemaForInference
208
+ ? InferJsonSchema<AdditionalSchemaOf<TSchema>>
209
+ : unknown;
210
+
211
+ type AdditionalPropsOf<TSchema> = TSchema extends { additionalProperties: false }
212
+ ? EmptyObject
213
+ : AdditionalSchemaOf<TSchema> extends JsonSchemaForInference
214
+ ? PropertyKeysOf<TSchema> extends never
215
+ ? Record<string, AdditionalPropsValue<TSchema>>
216
+ : Record<string, unknown>
217
+ : Record<string, unknown>;
218
+
219
+ type InferObject<TSchema> = Simplify<
220
+ RequiredProps<PropertiesOf<TSchema>, RequiredKeysOf<TSchema, PropertiesOf<TSchema>>> &
221
+ OptionalProps<PropertiesOf<TSchema>, RequiredKeysOf<TSchema, PropertiesOf<TSchema>>> &
222
+ AdditionalPropsOf<TSchema>
223
+ >;
224
+
225
+ type TypeKeywordOf<TSchema> = TSchema extends { type: infer TType } ? TType : never;
226
+
227
+ type TypeOptionsOf<TSchema> = TypeKeywordOf<TSchema> extends readonly unknown[]
228
+ ? Extract<TypeKeywordOf<TSchema>[number], JsonSchemaType>
229
+ : Extract<TypeKeywordOf<TSchema>, JsonSchemaType>;
230
+
231
+ type InferFromTypeOption<TSchema, TType extends JsonSchemaType> = TType extends 'object'
232
+ ? InferObject<TSchema>
233
+ : TType extends 'array'
234
+ ? TSchema extends { items: infer TItems }
235
+ ? InferJsonSchema<TItems>[]
236
+ : unknown[]
237
+ : TType extends 'string'
238
+ ? string
239
+ : TType extends 'number' | 'integer'
240
+ ? number
241
+ : TType extends 'boolean'
242
+ ? boolean
243
+ : TType extends 'null'
244
+ ? null
245
+ : unknown;
246
+
247
+ type InferFromTypeKeyword<TSchema> = TypeOptionsOf<TSchema> extends never
248
+ ? unknown
249
+ : InferFromTypeOption<TSchema, TypeOptionsOf<TSchema>>;
250
+
251
+ type ApplyNullable<TSchema, TValue> = TSchema extends { nullable: true } ? TValue | null : TValue;
252
+
253
+ /**
254
+ * Infers a TypeScript type from the supported JSON Schema subset.
255
+ *
256
+ * `const` and `enum` take precedence when present.
257
+ */
258
+ export type InferJsonSchema<TSchema> = [ConstLiteral<TSchema>] extends [never]
259
+ ? [EnumLiteral<TSchema>] extends [never]
260
+ ? ApplyNullable<TSchema, InferFromTypeKeyword<TSchema>>
261
+ : ApplyNullable<TSchema, EnumLiteral<TSchema>>
262
+ : ApplyNullable<TSchema, ConstLiteral<TSchema>>;
263
+
264
+ type IsWidenedTypeKeyword<TTypeKeyword> = string extends TTypeKeyword
265
+ ? true
266
+ : TTypeKeyword extends readonly unknown[]
267
+ ? string extends TTypeKeyword[number]
268
+ ? true
269
+ : false
270
+ : false;
271
+
272
+ type IncludesObjectType<TSchema> = TypeOptionsOf<TSchema> extends never
273
+ ? false
274
+ : 'object' extends TypeOptionsOf<TSchema>
275
+ ? true
276
+ : false;
277
+
278
+ /**
279
+ * Infers tool argument types from a root `InputSchema`.
280
+ *
281
+ * If the schema is not a literal object schema (for example a widened
282
+ * `InputSchema` loaded at runtime), this intentionally falls back to
283
+ * `Record<string, unknown>`.
284
+ */
285
+ export type InferArgsFromInputSchema<TSchema extends { type: string | readonly string[] }> =
286
+ IsWidenedTypeKeyword<TSchema['type']> extends true
287
+ ? Record<string, unknown>
288
+ : IncludesObjectType<TSchema> extends true
289
+ ? InferObject<TSchema>
290
+ : Record<string, unknown>;
@@ -0,0 +1,346 @@
1
+ import type { CallToolResult, InputSchema, ToolResponse } from './common.js';
2
+ import type { JsonSchemaForInference, JsonSchemaObject } from './json-schema.js';
3
+ import type { ToolDescriptor, ToolDescriptorFromSchema, ToolListItem } from './tool.js';
4
+
5
+ // ============================================================================
6
+ // Model Context Input
7
+ // ============================================================================
8
+
9
+ /**
10
+ * Context provided to models via provideContext().
11
+ */
12
+ export interface ModelContextInput<TTools extends readonly ToolDescriptor[] = ToolDescriptor[]> {
13
+ /**
14
+ * Base tool descriptors to expose.
15
+ */
16
+ tools?: TTools;
17
+ }
18
+
19
+ /**
20
+ * Public options accepted by provideContext().
21
+ */
22
+ export type ModelContextOptions = ModelContextInput;
23
+
24
+ // ============================================================================
25
+ // Model Context Testing
26
+ // ============================================================================
27
+
28
+ /**
29
+ * Tool info returned by ModelContextTesting.listTools().
30
+ */
31
+ export interface ModelContextTestingToolInfo {
32
+ name: string;
33
+ description: string;
34
+ inputSchema?: string;
35
+ }
36
+
37
+ /**
38
+ * Options supported by ModelContextTesting.executeTool().
39
+ */
40
+ export interface ModelContextTestingExecuteToolOptions {
41
+ signal?: AbortSignal;
42
+ }
43
+
44
+ /**
45
+ * Chromium early-preview testing API on navigator.modelContextTesting.
46
+ */
47
+ export interface ModelContextTesting {
48
+ listTools(): ModelContextTestingToolInfo[];
49
+ executeTool(
50
+ toolName: string,
51
+ inputArgsJson: string,
52
+ options?: ModelContextTestingExecuteToolOptions
53
+ ): Promise<string | null>;
54
+ registerToolsChangedCallback(callback: () => void): void;
55
+ getCrossDocumentScriptToolResult(): Promise<string>;
56
+ }
57
+
58
+ /**
59
+ * Polyfill-only testing extensions layered on top of ModelContextTesting.
60
+ */
61
+ export interface ModelContextTestingPolyfillExtensions {
62
+ getToolCalls(): Array<{
63
+ toolName: string;
64
+ arguments: Record<string, unknown>;
65
+ timestamp: number;
66
+ }>;
67
+ clearToolCalls(): void;
68
+ setMockToolResponse(toolName: string, response: ToolResponse): void;
69
+ clearMockToolResponse(toolName: string): void;
70
+ clearAllMockToolResponses(): void;
71
+ getRegisteredTools(): ReturnType<ModelContextExtensions['listTools']>;
72
+ reset(): void;
73
+ }
74
+
75
+ // ============================================================================
76
+ // Type Inference Helpers
77
+ // ============================================================================
78
+
79
+ /**
80
+ * Any supported tool descriptor.
81
+ *
82
+ * Uses `never` for args so descriptors with stricter argument objects remain assignable.
83
+ */
84
+ export type AnyToolDescriptor = ToolDescriptor<never, CallToolResult, string>;
85
+
86
+ /**
87
+ * Infers argument shape from a tool descriptor.
88
+ */
89
+ export type InferToolArgs<TTool> = TTool extends ToolDescriptor<
90
+ infer TArgs,
91
+ infer _TResult,
92
+ infer _TName
93
+ >
94
+ ? TArgs
95
+ : never;
96
+
97
+ /**
98
+ * Infers result shape from a tool descriptor.
99
+ */
100
+ export type InferToolResult<TTool> = TTool extends ToolDescriptor<
101
+ infer _TArgs,
102
+ infer TResult,
103
+ infer _TName
104
+ >
105
+ ? TResult
106
+ : never;
107
+
108
+ /**
109
+ * Union of tool names from a tuple/array of descriptors.
110
+ */
111
+ export type ToolName<TTools extends readonly { name: string }[]> = TTools[number]['name'];
112
+
113
+ /**
114
+ * Extracts a tool descriptor by name from a tuple/array.
115
+ */
116
+ export type ToolByName<
117
+ TTools extends readonly { name: string }[],
118
+ TName extends ToolName<TTools>,
119
+ > = Extract<TTools[number], { name: TName }>;
120
+
121
+ /**
122
+ * Tool argument type by tool name from a tuple/array.
123
+ */
124
+ export type ToolArgsByName<
125
+ TTools extends readonly { name: string }[],
126
+ TName extends ToolName<TTools>,
127
+ > = InferToolArgs<ToolByName<TTools, TName>>;
128
+
129
+ /**
130
+ * Tool result type by tool name from a tuple/array.
131
+ */
132
+ export type ToolResultByName<
133
+ TTools extends readonly { name: string }[],
134
+ TName extends ToolName<TTools>,
135
+ > = InferToolResult<ToolByName<TTools, TName>>;
136
+
137
+ /**
138
+ * Typed parameters for `callTool`.
139
+ *
140
+ * When a tool has no args (`Record<string, never>`), `arguments` becomes optional.
141
+ */
142
+ export type ToolCallParams<
143
+ TName extends string = string,
144
+ TArgs extends Record<string, unknown> = Record<string, never>,
145
+ > = { name: TName } & (TArgs extends Record<string, never>
146
+ ? { arguments?: TArgs }
147
+ : { arguments: TArgs });
148
+
149
+ // ============================================================================
150
+ // Tool Call Event
151
+ // ============================================================================
152
+
153
+ /**
154
+ * Event dispatched when a tool is called.
155
+ */
156
+ export interface ToolCallEvent extends Event {
157
+ /**
158
+ * Tool name being invoked.
159
+ */
160
+ name: string;
161
+
162
+ /**
163
+ * Tool arguments supplied by the caller.
164
+ */
165
+ arguments: Record<string, unknown>;
166
+
167
+ /**
168
+ * Intercepts execution with a custom tool response.
169
+ */
170
+ respondWith: (response: ToolResponse) => void;
171
+ }
172
+
173
+ // ============================================================================
174
+ // Model Context
175
+ // ============================================================================
176
+
177
+ /**
178
+ * Strict WebMCP core interface on navigator.modelContext.
179
+ */
180
+ export interface ModelContextCore {
181
+ // ==================== CONTEXT ====================
182
+
183
+ /**
184
+ * Replaces base context with provided tools.
185
+ */
186
+ provideContext(options?: ModelContextOptions): void;
187
+
188
+ // ==================== TOOLS ====================
189
+
190
+ /**
191
+ * Registers a dynamic tool with JSON Schema-driven inference.
192
+ *
193
+ * `execute(args)` is inferred from `inputSchema`, and when a literal object
194
+ * `outputSchema` is provided, `execute(...).structuredContent` is inferred too.
195
+ */
196
+ registerTool<
197
+ TInputSchema extends JsonSchemaForInference,
198
+ TOutputSchema extends JsonSchemaObject | undefined = undefined,
199
+ TName extends string = string,
200
+ >(tool: ToolDescriptorFromSchema<TInputSchema, TOutputSchema, TName>): void;
201
+
202
+ /**
203
+ * Registers a dynamic tool with explicitly typed args/result.
204
+ */
205
+ registerTool<
206
+ TInputSchema extends InputSchema,
207
+ TArgs extends Record<string, unknown> = Record<string, unknown>,
208
+ TResult extends CallToolResult = CallToolResult,
209
+ TName extends string = string,
210
+ >(
211
+ tool: ToolDescriptor<TArgs, TResult, TName> & {
212
+ inputSchema: TInputSchema;
213
+ } & (TInputSchema extends InputSchema
214
+ ? string extends TInputSchema['type']
215
+ ? unknown
216
+ : never
217
+ : unknown)
218
+ ): void;
219
+
220
+ /**
221
+ * Registers a dynamic tool without an explicit inputSchema.
222
+ * Runtime defaults this to an empty object schema.
223
+ */
224
+ registerTool<
225
+ TArgs extends Record<string, unknown> = Record<string, unknown>,
226
+ TResult extends CallToolResult = CallToolResult,
227
+ TName extends string = string,
228
+ >(
229
+ tool: Omit<ToolDescriptor<TArgs, TResult, TName>, 'inputSchema'> & {
230
+ inputSchema?: undefined;
231
+ }
232
+ ): void;
233
+
234
+ /**
235
+ * Unregisters a dynamic tool by name.
236
+ */
237
+ unregisterTool(name: string): void;
238
+
239
+ /**
240
+ * Clears all context (base + dynamic registrations).
241
+ */
242
+ clearContext(): void;
243
+ }
244
+
245
+ /**
246
+ * MCPB extension surface layered on top of strict WebMCP core.
247
+ * These members are intentionally non-standard.
248
+ */
249
+ export interface ModelContextExtensions {
250
+ /**
251
+ * Lists currently registered tools.
252
+ */
253
+ listTools(): ToolListItem[];
254
+
255
+ /**
256
+ * Executes a registered tool.
257
+ */
258
+ callTool<
259
+ TName extends string = string,
260
+ TArgs extends Record<string, unknown> = Record<string, unknown>,
261
+ >(params: { name: TName; arguments?: TArgs }): Promise<ToolResponse>;
262
+
263
+ // ==================== EVENTS ====================
264
+
265
+ /**
266
+ * Adds a listener for tool invocation events.
267
+ */
268
+ addEventListener(
269
+ type: 'toolcall',
270
+ listener: (event: ToolCallEvent) => void | Promise<void>,
271
+ options?: boolean | AddEventListenerOptions
272
+ ): void;
273
+
274
+ /**
275
+ * Adds a listener for tool list changes.
276
+ */
277
+ addEventListener(
278
+ type: 'toolschanged',
279
+ listener: () => void,
280
+ options?: boolean | AddEventListenerOptions
281
+ ): void;
282
+
283
+ /**
284
+ * Removes a listener for tool invocation events.
285
+ */
286
+ removeEventListener(
287
+ type: 'toolcall',
288
+ listener: (event: ToolCallEvent) => void | Promise<void>,
289
+ options?: boolean | EventListenerOptions
290
+ ): void;
291
+
292
+ /**
293
+ * Removes a listener for tool list changes.
294
+ */
295
+ removeEventListener(
296
+ type: 'toolschanged',
297
+ listener: () => void,
298
+ options?: boolean | EventListenerOptions
299
+ ): void;
300
+
301
+ /**
302
+ * Dispatches an event.
303
+ */
304
+ dispatchEvent(event: Event): boolean;
305
+ }
306
+
307
+ /**
308
+ * Public navigator.modelContext type (strict core only).
309
+ */
310
+ export type ModelContext = ModelContextCore;
311
+
312
+ /**
313
+ * Full runtime shape including MCPB extensions.
314
+ */
315
+ export type ModelContextWithExtensions = ModelContextCore & ModelContextExtensions;
316
+
317
+ // ============================================================================
318
+ // Typed Model Context
319
+ // ============================================================================
320
+
321
+ /**
322
+ * Strongly-typed `ModelContext` view derived from known tool descriptors.
323
+ *
324
+ * This is useful when your project has a static tool registry and you want
325
+ * name-aware inference for `callTool`.
326
+ */
327
+ export type TypedModelContext<
328
+ TTools extends readonly { name: string }[] = readonly ToolDescriptor[],
329
+ > = ModelContextWithExtensions & {
330
+ /**
331
+ * Executes a known tool with name-aware argument and response inference.
332
+ */
333
+ callTool<TName extends ToolName<TTools>>(
334
+ params: ToolCallParams<TName, ToolArgsByName<TTools, TName>>
335
+ ): Promise<ToolResultByName<TTools, TName>>;
336
+
337
+ /**
338
+ * Fallback call signature for unknown or dynamically-discovered tools.
339
+ */
340
+ callTool(params: { name: string; arguments?: Record<string, unknown> }): Promise<ToolResponse>;
341
+
342
+ /**
343
+ * Lists tools with a narrowed name union.
344
+ */
345
+ listTools(): Array<ToolListItem<ToolName<TTools>>>;
346
+ };