Skip to content

Commit 0e2b156

Browse files
committed
More fixups, including object property passthroughs
1 parent e929a4b commit 0e2b156

File tree

1 file changed

+65
-62
lines changed

1 file changed

+65
-62
lines changed

src/types.ts

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ export const RequestSchema = z.object({
1414
z
1515
.object({
1616
_meta: z.optional(
17-
z.object({
18-
/**
19-
* If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
20-
*/
21-
progressToken: z.optional(ProgressTokenSchema),
22-
}),
17+
z
18+
.object({
19+
/**
20+
* If specified, the caller is requesting out-of-band progress notifications for this request (as represented by notifications/progress). The value of this parameter is an opaque token that will be attached to any subsequent notifications. The receiver is not obligated to provide these notifications.
21+
*/
22+
progressToken: z.optional(ProgressTokenSchema),
23+
})
24+
.passthrough(),
2325
),
2426
})
25-
.catchall(z.unknown()),
27+
.passthrough(),
2628
),
2729
});
2830

@@ -34,9 +36,9 @@ export const NotificationSchema = z.object({
3436
/**
3537
* This parameter name is reserved by MCP to allow clients and servers to attach additional metadata to their notifications.
3638
*/
37-
_meta: z.optional(z.record(z.unknown())),
39+
_meta: z.optional(z.object({}).passthrough()),
3840
})
39-
.catchall(z.unknown()),
41+
.passthrough(),
4042
),
4143
});
4244

@@ -45,9 +47,9 @@ export const ResultSchema = z
4547
/**
4648
* This result property is reserved by the protocol to allow clients and servers to attach additional metadata to their responses.
4749
*/
48-
_meta: z.optional(z.record(z.unknown())),
50+
_meta: z.optional(z.object({}).passthrough()),
4951
})
50-
.catchall(z.unknown());
52+
.passthrough();
5153

5254
/**
5355
* A uniquely identifying ID for a request in JSON-RPC.
@@ -60,23 +62,25 @@ export const RequestIdSchema = z.union([z.string(), z.number().int()]);
6062
export const JSONRPCRequestSchema = RequestSchema.extend({
6163
jsonrpc: z.literal(JSONRPC_VERSION),
6264
id: RequestIdSchema,
63-
});
65+
}).strict();
6466

6567
/**
6668
* A notification which does not expect a response.
6769
*/
6870
export const JSONRPCNotificationSchema = NotificationSchema.extend({
6971
jsonrpc: z.literal(JSONRPC_VERSION),
70-
});
72+
}).strict();
7173

7274
/**
7375
* A successful (non-error) response to a request.
7476
*/
75-
export const JSONRPCResponseSchema = z.object({
76-
jsonrpc: z.literal(JSONRPC_VERSION),
77-
id: RequestIdSchema,
78-
result: ResultSchema,
79-
});
77+
export const JSONRPCResponseSchema = z
78+
.object({
79+
jsonrpc: z.literal(JSONRPC_VERSION),
80+
id: RequestIdSchema,
81+
result: ResultSchema,
82+
})
83+
.strict();
8084

8185
// Standard JSON-RPC error codes
8286
export const PARSE_ERROR = -32700;
@@ -88,24 +92,26 @@ export const INTERNAL_ERROR = -32603;
8892
/**
8993
* A response to a request that indicates an error occurred.
9094
*/
91-
export const JSONRPCErrorSchema = z.object({
92-
jsonrpc: z.literal(JSONRPC_VERSION),
93-
id: RequestIdSchema,
94-
error: z.object({
95-
/**
96-
* The error type that occurred.
97-
*/
98-
code: z.number().int(),
99-
/**
100-
* A short description of the error. The message SHOULD be limited to a concise single sentence.
101-
*/
102-
message: z.string(),
103-
/**
104-
* Additional information about the error. The value of this member is defined by the sender (e.g. detailed error information, nested errors etc.).
105-
*/
106-
data: z.optional(z.unknown()),
107-
}),
108-
});
95+
export const JSONRPCErrorSchema = z
96+
.object({
97+
jsonrpc: z.literal(JSONRPC_VERSION),
98+
id: RequestIdSchema,
99+
error: z.object({
100+
/**
101+
* The error type that occurred.
102+
*/
103+
code: z.number().int(),
104+
/**
105+
* A short description of the error. The message SHOULD be limited to a concise single sentence.
106+
*/
107+
message: z.string(),
108+
/**
109+
* Additional information about the error. The value of this member is defined by the sender (e.g. detailed error information, nested errors etc.).
110+
*/
111+
data: z.optional(z.unknown()),
112+
}),
113+
})
114+
.strict();
109115

110116
export const JSONRPCMessageSchema = z.union([
111117
JSONRPCRequestSchema,
@@ -118,7 +124,7 @@ export const JSONRPCMessageSchema = z.union([
118124
/**
119125
* A response that indicates success but carries no data.
120126
*/
121-
export const EmptyResultSchema = ResultSchema;
127+
export const EmptyResultSchema = ResultSchema.strict();
122128

123129
/* Initialization */
124130
export const PROTOCOL_VERSION = 1;
@@ -141,10 +147,8 @@ export const ImageContentSchema = z.object({
141147
type: z.literal("image"),
142148
/**
143149
* The base64-encoded image data.
144-
*
145-
* @format byte
146150
*/
147-
data: z.string(),
151+
data: z.string().base64(),
148152
/**
149153
* The MIME type of the image. Different providers may support different image types.
150154
*/
@@ -174,11 +178,11 @@ export const ClientCapabilitiesSchema = z.object({
174178
/**
175179
* Experimental, non-standard capabilities that the client supports.
176180
*/
177-
experimental: z.optional(z.record(z.object({}))),
181+
experimental: z.optional(z.object({}).passthrough()),
178182
/**
179183
* Present if the client supports sampling from an LLM.
180184
*/
181-
sampling: z.optional(z.object({})),
185+
sampling: z.optional(z.object({}).passthrough()),
182186
});
183187

184188
/**
@@ -203,30 +207,32 @@ export const ServerCapabilitiesSchema = z.object({
203207
/**
204208
* Experimental, non-standard capabilities that the server supports.
205209
*/
206-
experimental: z.optional(z.record(z.object({}))),
210+
experimental: z.optional(z.object({}).passthrough()),
207211
/**
208212
* Present if the server supports sending log messages to the client.
209213
*/
210-
logging: z.optional(z.object({})),
214+
logging: z.optional(z.object({}).passthrough()),
211215
/**
212216
* Present if the server offers any prompt templates.
213217
*/
214-
prompts: z.optional(z.object({})),
218+
prompts: z.optional(z.object({}).passthrough()),
215219
/**
216220
* Present if the server offers any resources to read.
217221
*/
218222
resources: z.optional(
219-
z.object({
220-
/**
221-
* Whether this server supports subscribing to resource updates.
222-
*/
223-
subscribe: z.optional(z.boolean()),
224-
}),
223+
z
224+
.object({
225+
/**
226+
* Whether this server supports subscribing to resource updates.
227+
*/
228+
subscribe: z.optional(z.boolean()),
229+
})
230+
.passthrough(),
225231
),
226232
/**
227233
* Present if the server offers any tools to call.
228234
*/
229-
tools: z.optional(z.object({})),
235+
tools: z.optional(z.object({}).passthrough()),
230236
});
231237

232238
/**
@@ -303,10 +309,8 @@ export const TextResourceContentsSchema = ResourceContentsSchema.extend({
303309
export const BlobResourceContentsSchema = ResourceContentsSchema.extend({
304310
/**
305311
* A base64-encoded string representing the binary data of the item.
306-
*
307-
* @format byte
308312
*/
309-
blob: z.string(),
313+
blob: z.string().base64(),
310314
});
311315

312316
/**
@@ -344,8 +348,6 @@ export const ResourceSchema = z.object({
344348
export const ResourceTemplateSchema = z.object({
345349
/**
346350
* A URI template (according to RFC 6570) that can be used to construct resource URIs.
347-
*
348-
* @format uri-template
349351
*/
350352
uriTemplate: z.string(),
351353

@@ -549,7 +551,7 @@ export const ToolSchema = z.object({
549551
*/
550552
inputSchema: z.object({
551553
type: z.literal("object"),
552-
properties: z.optional(z.record(z.object({}))),
554+
properties: z.optional(z.object({}).passthrough()),
553555
}),
554556
});
555557

@@ -657,7 +659,7 @@ export const CreateMessageRequestSchema = RequestSchema.extend({
657659
/**
658660
* Optional metadata to pass through to the LLM provider. The format of this metadata is provider-specific.
659661
*/
660-
metadata: z.optional(z.object({})),
662+
metadata: z.optional(z.object({}).passthrough()),
661663
}),
662664
});
663665

@@ -674,7 +676,10 @@ export const CreateMessageResultSchema = ResultSchema.extend({
674676
*/
675677
stopReason: z.enum(["endTurn", "stopSequence", "maxTokens"]),
676678
role: z.enum(["user", "assistant"]),
677-
content: z.union([TextContentSchema, ImageContentSchema]),
679+
content: z.discriminatedUnion("type", [
680+
TextContentSchema,
681+
ImageContentSchema,
682+
]),
678683
});
679684

680685
/* Autocomplete */
@@ -685,8 +690,6 @@ export const ResourceReferenceSchema = z.object({
685690
type: z.literal("ref/resource"),
686691
/**
687692
* The URI or URI template of the resource.
688-
*
689-
* @format uri-template
690693
*/
691694
uri: z.string(),
692695
});

0 commit comments

Comments
 (0)