Skip to content

Commit 0bda0c0

Browse files
committed
Simplify Zod declaration and supported modes logic
1 parent eb9301d commit 0bda0c0

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/client/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ export function getSupportedElicitationModes(capabilities: ClientCapabilities['e
103103
return { supportsFormMode: false, supportsUrlMode: false };
104104
}
105105

106-
const hasFormCapability = Object.prototype.hasOwnProperty.call(capabilities, 'form');
107-
const hasUrlCapability = Object.prototype.hasOwnProperty.call(capabilities, 'url');
106+
const hasFormCapability = capabilities.form !== undefined;
107+
const hasUrlCapability = capabilities.url !== undefined;
108108

109109
// If neither form nor url are explicitly declared, form mode is supported (backwards compatibility)
110110
const supportsFormMode = hasFormCapability || (!hasFormCapability && !hasUrlCapability);

src/types.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -274,27 +274,27 @@ export const ImplementationSchema = BaseMetadataSchema.extend({
274274
websiteUrl: z.string().optional()
275275
}).merge(IconsSchema);
276276

277-
const ElicitationCapabilitySchema = z
278-
.preprocess(
279-
value => {
280-
if (value && typeof value === 'object' && !Array.isArray(value)) {
281-
const hasForm = Object.prototype.hasOwnProperty.call(value, 'form');
282-
const hasUrl = Object.prototype.hasOwnProperty.call(value, 'url');
283-
if (!hasForm && !hasUrl) {
284-
return { ...(value as Record<string, unknown>), form: {} };
285-
}
277+
const ElicitationCapabilitySchema = z.preprocess(
278+
value => {
279+
if (value && typeof value === 'object' && !Array.isArray(value)) {
280+
const recordValue = value as Record<string, unknown>;
281+
const hasForm = Object.prototype.hasOwnProperty.call(recordValue, 'form');
282+
const hasUrl = Object.prototype.hasOwnProperty.call(recordValue, 'url');
283+
if (!hasForm && !hasUrl) {
284+
return { ...recordValue, form: {} };
286285
}
287-
return value;
288-
},
289-
z
290-
.object({
291-
applyDefaults: z.boolean().optional(),
292-
form: z.object({}).passthrough().optional(),
293-
url: z.object({}).passthrough().optional()
294-
})
295-
.passthrough()
286+
}
287+
return value;
288+
},
289+
z.intersection(
290+
z.object({
291+
applyDefaults: z.boolean().optional(),
292+
form: AssertObjectSchema.optional(),
293+
url: AssertObjectSchema.optional()
294+
}),
295+
z.record(z.string(), z.unknown())
296296
)
297-
.optional();
297+
);
298298

299299
/**
300300
* Capabilities a client may support. Known capabilities are defined here, in this schema, but this is not a closed set: any client can define its own, additional capabilities.
@@ -311,7 +311,7 @@ export const ClientCapabilitiesSchema = z.object({
311311
/**
312312
* Present if the client supports eliciting user input.
313313
*/
314-
elicitation: ElicitationCapabilitySchema,
314+
elicitation: ElicitationCapabilitySchema.optional(),
315315
/**
316316
* Present if the client supports listing roots.
317317
*/

0 commit comments

Comments
 (0)