Skip to content

Allow enum in registerPrompt argsSchema #710

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ server.registerPrompt(
{
title: "Code Review",
description: "Review code for best practices and potential issues",
// can use string or enum
argsSchema: { code: z.string() }
},
({ code }) => ({
Expand Down
63 changes: 63 additions & 0 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4010,6 +4010,69 @@ describe("Tool title precedence", () => {
});
});

test("registerPrompt schema should support strings and enums", async () => {
const mcpServer = new McpServer({
name: "test server",
version: "1.0",
});

const client = new Client({
name: "test client",
version: "1.0",
});

mcpServer.registerPrompt(
"test-prompt",
{
title: "Team Greeting",
description: "Generate a greeting for team members",
argsSchema: {
name: z.string(),
visibility: z.enum(["public", "private"]).optional().default("private"),
}
},
async ({ name, visibility }) => ({
messages: [
{
role: "assistant",
content: {
type: "text",
text: `Creating a new project named ${name} with visibility ${visibility}.`,
},
},
],
}),
);

const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();

await Promise.all([
client.connect(clientTransport),
mcpServer.server.connect(serverTransport),
]);

const result1 = await client.getPrompt({
name: "test-prompt",
arguments: {
name: "Test Project",
visibility: "public"
}
});

expect(result1.messages[0].content.text).toBe("Creating a new project named Test Project with visibility public.");

await expect(
client.getPrompt({
name: "test-prompt",
arguments: {
name: "Test Project",
visibility: "foo"
}
}),
).rejects.toThrow();
});

describe("elicitInput()", () => {

const checkAvailability = jest.fn().mockResolvedValue(false);
Expand Down
8 changes: 7 additions & 1 deletion src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
ZodType,
ZodTypeDef,
ZodOptional,
ZodDefault,
ZodEnum,
} from "zod";
import {
Implementation,
Expand Down Expand Up @@ -1246,7 +1248,11 @@ export type RegisteredResourceTemplate = {
type PromptArgsRawShape = {
[k: string]:
| ZodType<string, ZodTypeDef, string>
| ZodOptional<ZodType<string, ZodTypeDef, string>>;
| ZodEnum<[string, ...string[]]>
| ZodDefault<ZodEnum<[string, ...string[]]>>
| ZodDefault<ZodOptional<ZodEnum<[string, ...string[]]>>>
| ZodOptional<ZodType<string, ZodTypeDef, string>>
| ZodDefault<ZodOptional<ZodType<string, ZodTypeDef, string>>>;
};

export type PromptCallback<
Expand Down
5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,10 @@ export const GetPromptRequestSchema = RequestSchema.extend({
/**
* Arguments to use for templating the prompt.
*/
arguments: z.optional(z.record(z.string())),
arguments: z.optional(z.record(z.union([
z.string(),
z.object({ type: z.literal("enum"), enum: z.array(z.string()) })
]))),
}),
});

Expand Down