@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,241 +1,62 @@
1
- import type { JsonPrimitive, JsonValue } from './common.js';
1
+ import type { InputSchema, WebMcpToolInput } from './common.js';
2
+ import type { JsonSchemaType as McpJsonSchema } from '@modelcontextprotocol/server';
2
3
 
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;
4
+ /** JSON Schema Draft 2020-12 accepted by the inference helpers. */
5
+ export type JsonSchemaForInference = McpJsonSchema;
158
6
 
7
+ type JsonSchemaType = 'string' | 'number' | 'integer' | 'boolean' | 'null' | 'object' | 'array';
159
8
  type Simplify<T> = { [K in keyof T]: T[K] } & {};
160
-
161
9
  type EmptyObject = Record<never, never>;
162
10
 
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>>;
11
+ type ConstValue<TSchema> = TSchema extends { const: infer TValue } ? TValue : never;
12
+ type EnumValue<TSchema> = TSchema extends { enum: readonly (infer TValue)[] } ? TValue : never;
13
+ type Properties<TSchema> = TSchema extends {
14
+ properties: infer TProperties extends Readonly<Record<string, unknown>>;
173
15
  }
174
16
  ? TProperties
175
17
  : EmptyObject;
176
-
177
- type RequiredKeysOf<TSchema, TProperties extends Record<string, unknown>> = TSchema extends {
178
- required: readonly (infer TRequired)[];
179
- }
180
- ? string extends TRequired
18
+ type RequiredKeys<TSchema> = TSchema extends { required: readonly (infer TKey extends string)[] }
19
+ ? string extends TKey
181
20
  ? never
182
- : Extract<TRequired, keyof TProperties & string>
21
+ : Extract<TKey, keyof Properties<TSchema>>
183
22
  : never;
184
23
 
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> =
208
- AdditionalSchemaOf<TSchema> extends JsonSchemaForInference
209
- ? InferJsonSchema<AdditionalSchemaOf<TSchema>>
210
- : unknown;
211
-
212
- type AdditionalPropsOf<TSchema> = TSchema extends { additionalProperties: false }
213
- ? EmptyObject
214
- : AdditionalSchemaOf<TSchema> extends JsonSchemaForInference
215
- ? PropertyKeysOf<TSchema> extends never
216
- ? Record<string, AdditionalPropsValue<TSchema>>
24
+ type AdditionalProperties<TSchema> = TSchema extends { additionalProperties: false }
25
+ ? keyof Properties<TSchema> extends never
26
+ ? Record<string, never>
27
+ : EmptyObject
28
+ : keyof Properties<TSchema> extends never
29
+ ? TSchema extends { additionalProperties: infer TAdditional extends object }
30
+ ? Record<string, InferJsonSchema<TAdditional>>
217
31
  : Record<string, unknown>
218
32
  : Record<string, unknown>;
219
33
 
220
34
  type InferObject<TSchema> = Simplify<
221
- RequiredProps<PropertiesOf<TSchema>, RequiredKeysOf<TSchema, PropertiesOf<TSchema>>> &
222
- OptionalProps<PropertiesOf<TSchema>, RequiredKeysOf<TSchema, PropertiesOf<TSchema>>> &
223
- AdditionalPropsOf<TSchema>
35
+ {
36
+ -readonly [K in keyof Properties<TSchema> as K extends RequiredKeys<TSchema>
37
+ ? K
38
+ : never]-?: InferJsonSchema<Properties<TSchema>[K]>;
39
+ } & {
40
+ -readonly [K in keyof Properties<TSchema> as K extends RequiredKeys<TSchema>
41
+ ? never
42
+ : K]?: InferJsonSchema<Properties<TSchema>[K]>;
43
+ } & AdditionalProperties<TSchema>
224
44
  >;
225
45
 
226
- type TypeKeywordOf<TSchema> = TSchema extends { type?: infer TType }
227
- ? 'type' extends keyof TSchema
228
- ? TType
229
- : undefined
230
- : undefined;
231
-
232
- type TypeOptionsOf<TSchema> = [TypeKeywordOf<TSchema>] extends [undefined]
233
- ? 'object'
234
- : TypeKeywordOf<TSchema> extends readonly unknown[]
235
- ? Extract<TypeKeywordOf<TSchema>[number], JsonSchemaType>
236
- : Extract<TypeKeywordOf<TSchema>, JsonSchemaType>;
237
-
238
- type InferFromTypeOption<TSchema, TType extends JsonSchemaType> = TType extends 'object'
46
+ type TypeKeyword<TSchema> = TSchema extends { type: infer TType }
47
+ ? TType
48
+ : TSchema extends
49
+ | { properties: unknown }
50
+ | { required: unknown }
51
+ | { additionalProperties: unknown }
52
+ ? 'object'
53
+ : never;
54
+ type TypeOptions<TSchema> =
55
+ TypeKeyword<TSchema> extends readonly unknown[]
56
+ ? Extract<TypeKeyword<TSchema>[number], JsonSchemaType>
57
+ : Extract<TypeKeyword<TSchema>, JsonSchemaType>;
58
+
59
+ type InferType<TSchema, TType extends JsonSchemaType> = TType extends 'object'
239
60
  ? InferObject<TSchema>
240
61
  : TType extends 'array'
241
62
  ? TSchema extends { items: infer TItems }
@@ -251,49 +72,24 @@ type InferFromTypeOption<TSchema, TType extends JsonSchemaType> = TType extends
251
72
  ? null
252
73
  : unknown;
253
74
 
254
- type InferFromTypeKeyword<TSchema> =
255
- TypeOptionsOf<TSchema> extends never
75
+ /** Infers a TypeScript value from the supported JSON Schema keywords. */
76
+ export type InferJsonSchema<TSchema> = TSchema extends false
77
+ ? never
78
+ : TSchema extends true
256
79
  ? unknown
257
- : InferFromTypeOption<TSchema, TypeOptionsOf<TSchema>>;
258
-
259
- type ApplyNullable<TSchema, TValue> = TSchema extends { nullable: true } ? TValue | null : TValue;
260
-
261
- /**
262
- * Infers a TypeScript type from the supported JSON Schema subset.
263
- *
264
- * `const` and `enum` take precedence when present.
265
- */
266
- export type InferJsonSchema<TSchema> = [ConstLiteral<TSchema>] extends [never]
267
- ? [EnumLiteral<TSchema>] extends [never]
268
- ? ApplyNullable<TSchema, InferFromTypeKeyword<TSchema>>
269
- : ApplyNullable<TSchema, EnumLiteral<TSchema>>
270
- : ApplyNullable<TSchema, ConstLiteral<TSchema>>;
271
-
272
- type IsWidenedTypeKeyword<TTypeKeyword> = string extends TTypeKeyword
273
- ? true
274
- : TTypeKeyword extends readonly unknown[]
275
- ? string extends TTypeKeyword[number]
276
- ? true
277
- : false
278
- : false;
279
-
280
- type IncludesObjectType<TSchema> =
281
- TypeOptionsOf<TSchema> extends never
282
- ? false
283
- : 'object' extends TypeOptionsOf<TSchema>
284
- ? true
285
- : false;
286
-
287
- /**
288
- * Infers tool argument types from a root `InputSchema`.
289
- *
290
- * If the schema is not a literal object schema (for example a widened
291
- * `InputSchema` loaded at runtime), this intentionally falls back to
292
- * `Record<string, unknown>`.
293
- */
294
- export type InferArgsFromInputSchema<TSchema> =
295
- IsWidenedTypeKeyword<TypeKeywordOf<TSchema>> extends true
296
- ? Record<string, unknown>
297
- : IncludesObjectType<TSchema> extends true
298
- ? InferObject<TSchema>
299
- : Record<string, unknown>;
80
+ : [InputSchema] extends [TSchema]
81
+ ? unknown
82
+ : [ConstValue<TSchema>] extends [never]
83
+ ? [EnumValue<TSchema>] extends [never]
84
+ ? TypeOptions<TSchema> extends never
85
+ ? unknown
86
+ : InferType<TSchema, TypeOptions<TSchema>>
87
+ : EnumValue<TSchema>
88
+ : ConstValue<TSchema>;
89
+
90
+ /** Infers the object or array passed to a WebMCP tool callback. */
91
+ export type InferArgsFromInputSchema<TSchema> = [
92
+ Extract<Exclude<InferJsonSchema<TSchema>, null | undefined>, WebMcpToolInput>,
93
+ ] extends [never]
94
+ ? WebMcpToolInput
95
+ : Extract<Exclude<InferJsonSchema<TSchema>, null | undefined>, WebMcpToolInput>;