|
| 1 | +/* |
| 2 | + * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. |
| 3 | + */ |
| 4 | + |
| 5 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 6 | +import { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js"; |
| 7 | +import { GetPromptResult } from "@modelcontextprotocol/sdk/types.js"; |
| 8 | +import { |
| 9 | + objectOutputType, |
| 10 | + ZodOptional, |
| 11 | + ZodType, |
| 12 | + ZodTypeAny, |
| 13 | + ZodTypeDef, |
| 14 | +} from "zod"; |
| 15 | +import { UnstructuredClientCore } from "../core.js"; |
| 16 | +import { ConsoleLogger } from "./console-logger.js"; |
| 17 | +import { MCPScope } from "./scopes.js"; |
| 18 | + |
| 19 | +// '@modelcontextprotocol/sdk' currently does not export this type |
| 20 | +export type PromptArgsRawShape = { |
| 21 | + [k: string]: |
| 22 | + | ZodType<string, ZodTypeDef, string> |
| 23 | + | ZodOptional<ZodType<string, ZodTypeDef, string>>; |
| 24 | +}; |
| 25 | + |
| 26 | +export type PromptDefinition< |
| 27 | + Args extends undefined | PromptArgsRawShape = undefined, |
| 28 | +> = Args extends PromptArgsRawShape ? { |
| 29 | + name: string; |
| 30 | + description?: string; |
| 31 | + scopes?: MCPScope[]; |
| 32 | + args: Args; |
| 33 | + prompt: ( |
| 34 | + client: UnstructuredClientCore, |
| 35 | + args: objectOutputType<Args, ZodTypeAny>, |
| 36 | + extra: RequestHandlerExtra, |
| 37 | + ) => GetPromptResult | Promise<GetPromptResult>; |
| 38 | + } |
| 39 | + : { |
| 40 | + name: string; |
| 41 | + description?: string; |
| 42 | + scopes?: MCPScope[]; |
| 43 | + args?: undefined; |
| 44 | + prompt: ( |
| 45 | + client: UnstructuredClientCore, |
| 46 | + extra: RequestHandlerExtra, |
| 47 | + ) => GetPromptResult | Promise<GetPromptResult>; |
| 48 | + }; |
| 49 | + |
| 50 | +// Optional function to assist with formatting prompt results |
| 51 | +export async function formatResult(value: string): Promise<GetPromptResult> { |
| 52 | + return { |
| 53 | + messages: [ |
| 54 | + { |
| 55 | + role: "user", |
| 56 | + content: { |
| 57 | + type: "text", |
| 58 | + text: value, |
| 59 | + }, |
| 60 | + }, |
| 61 | + ], |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +export function createRegisterPrompt( |
| 66 | + logger: ConsoleLogger, |
| 67 | + server: McpServer, |
| 68 | + sdk: UnstructuredClientCore, |
| 69 | + allowedScopes: Set<MCPScope>, |
| 70 | +): <A extends PromptArgsRawShape | undefined>( |
| 71 | + prompt: PromptDefinition<A>, |
| 72 | +) => void { |
| 73 | + return <A extends PromptArgsRawShape | undefined>( |
| 74 | + prompt: PromptDefinition<A>, |
| 75 | + ): void => { |
| 76 | + const scopes = prompt.scopes ?? []; |
| 77 | + if (!scopes.every((s: MCPScope) => allowedScopes.has(s))) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (prompt.args) { |
| 82 | + if (prompt.description) { |
| 83 | + server.prompt( |
| 84 | + prompt.name, |
| 85 | + prompt.description, |
| 86 | + prompt.args, |
| 87 | + async (args, ctx) => prompt.prompt(sdk, args, ctx), |
| 88 | + ); |
| 89 | + } else { |
| 90 | + server.prompt( |
| 91 | + prompt.name, |
| 92 | + prompt.args, |
| 93 | + async (args, ctx) => prompt.prompt(sdk, args, ctx), |
| 94 | + ); |
| 95 | + } |
| 96 | + } else { |
| 97 | + if (prompt.description) { |
| 98 | + server.prompt( |
| 99 | + prompt.name, |
| 100 | + prompt.description, |
| 101 | + async (ctx) => prompt.prompt(sdk, ctx), |
| 102 | + ); |
| 103 | + } else { |
| 104 | + server.prompt(prompt.name, async (ctx) => prompt.prompt(sdk, ctx)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + logger.debug("Registered prompt", { name: prompt.name }); |
| 109 | + }; |
| 110 | +} |
0 commit comments