Skip to content

fix: Replace non-standard enumNames with standard oneOf in EnumSchema #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1040,8 +1040,11 @@ server.tool(
type: "string",
title: "Date flexibility",
description: "How flexible are your dates?",
enum: ["next_day", "same_week", "next_week"],
enumNames: ["Next day", "Same week", "Next week"]
oneOf: [
{ const: "next_day", title: "Next day" },
{ const: "same_week", title: "Same week" },
{ const: "next_week", title: "Next week" }
],
}
},
required: ["checkAlternatives"]
Expand Down
100 changes: 88 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"client": "tsx src/cli.ts client"
},
"dependencies": {
"ajv": "^6.12.6",
"ajv": "^8.17.1",
"content-type": "^1.0.5",
"cors": "^2.8.5",
"cross-spawn": "^7.0.5",
Expand Down
4 changes: 2 additions & 2 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import {
ErrorCode,
McpError,
} from "../types.js";
import Ajv from "ajv";
import { Ajv } from 'ajv';
import type { ValidateFunction } from "ajv";

export type ClientOptions = ProtocolOptions & {
Expand Down Expand Up @@ -92,7 +92,7 @@ export class Client<
private _capabilities: ClientCapabilities;
private _instructions?: string;
private _cachedToolOutputValidators: Map<string, ValidateFunction> = new Map();
private _ajv: InstanceType<typeof Ajv>;
private _ajv: Ajv;

/**
* Initializes this client with the given name and version information.
Expand Down
4 changes: 2 additions & 2 deletions src/examples/client/simpleStreamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
ReadResourceResultSchema,
} from '../../types.js';
import { getDisplayName } from '../../shared/metadataUtils.js';
import Ajv from "ajv";
import { Ajv } from 'ajv';

// Create readline interface for user input
const readline = createInterface({
Expand Down Expand Up @@ -373,7 +373,7 @@ async function connect(url?: string): Promise<void> {
if (!isValid) {
console.log('❌ Validation errors:');
validate.errors?.forEach(error => {
console.log(` - ${error.dataPath || 'root'}: ${error.message}`);
console.log(` - ${error.instancePath || 'root'}: ${error.message}`);
});

if (attempts < maxAttempts) {
Expand Down
14 changes: 10 additions & 4 deletions src/examples/server/simpleStreamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,11 @@ const getServer = () => {
type: 'string',
title: 'Theme',
description: 'Choose your preferred theme',
enum: ['light', 'dark', 'auto'],
enumNames: ['Light', 'Dark', 'Auto'],
oneOf: [
{ const: 'light', title: 'Light' },
{ const: 'dark', title: 'Dark' },
{ const: 'auto', title: 'Auto' }
],
},
notifications: {
type: 'boolean',
Expand All @@ -154,8 +157,11 @@ const getServer = () => {
type: 'string',
title: 'Notification Frequency',
description: 'How often would you like notifications?',
enum: ['daily', 'weekly', 'monthly'],
enumNames: ['Daily', 'Weekly', 'Monthly'],
oneOf: [
{ const: 'daily', title: 'Daily' },
{ const: 'weekly', title: 'Weekly' },
{ const: 'monthly', title: 'Monthly' }
],
},
},
required: ['theme'],
Expand Down
10 changes: 5 additions & 5 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
ServerResult,
SUPPORTED_PROTOCOL_VERSIONS,
} from "../types.js";
import Ajv from "ajv";
import { Ajv } from 'ajv';

export type ServerOptions = ProtocolOptions & {
/**
Expand Down Expand Up @@ -85,6 +85,7 @@ export class Server<
private _clientVersion?: Implementation;
private _capabilities: ServerCapabilities;
private _instructions?: string;
private _ajv: Ajv;

/**
* Callback for when initialization has fully completed (i.e., the client has sent an `initialized` notification).
Expand All @@ -101,6 +102,7 @@ export class Server<
super(options);
this._capabilities = options?.capabilities ?? {};
this._instructions = options?.instructions;
this._ajv = new Ajv({ code: { source: false } });

this.setRequestHandler(InitializeRequestSchema, (request) =>
this._oninitialize(request),
Expand Down Expand Up @@ -323,15 +325,13 @@ export class Server<
// Validate the response content against the requested schema if action is "accept"
if (result.action === "accept" && result.content) {
try {
const ajv = new Ajv();

const validate = ajv.compile(params.requestedSchema);
const validate = this._ajv.compile(params.requestedSchema);
const isValid = validate(result.content);

if (!isValid) {
throw new McpError(
ErrorCode.InvalidParams,
`Elicitation response content does not match requested schema: ${ajv.errorsText(validate.errors)}`,
`Elicitation response content does not match requested schema: ${this._ajv.errorsText(validate.errors)}`,
);
}
} catch (error) {
Expand Down
7 changes: 5 additions & 2 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4119,8 +4119,11 @@ describe("elicitInput()", () => {
type: "string",
title: "Date flexibility",
description: "How flexible are your dates?",
enum: ["next_day", "same_week", "next_week"],
enumNames: ["Next day", "Same week", "Next week"]
oneOf: [
{ const: "next_day", title: "Next day" },
{ const: "same_week", title: "Same week" },
{ const: "next_week", title: "Next week" }
],
}
},
required: ["checkAlternatives"]
Expand Down
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1212,8 +1212,10 @@ export const EnumSchemaSchema = z
type: z.literal("string"),
title: z.optional(z.string()),
description: z.optional(z.string()),
enum: z.array(z.string()),
enumNames: z.optional(z.array(z.string())),
oneOf: z.array(z.object({
const: z.string(),
title: z.string()
})),
})
.passthrough();

Expand Down
Loading