MCPcopy Create free account
hub / github.com/modelcontextprotocol/typescript-sdk / setPromptRequestHandlers

Method setPromptRequestHandlers

src/server/mcp.ts:566–626  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

564 private _promptHandlersInitialized = false;
565
566 private setPromptRequestHandlers() {
567 if (this._promptHandlersInitialized) {
568 return;
569 }
570
571 this.server.assertCanSetRequestHandler(getMethodValue(ListPromptsRequestSchema));
572 this.server.assertCanSetRequestHandler(getMethodValue(GetPromptRequestSchema));
573
574 this.server.registerCapabilities({
575 prompts: {
576 listChanged: true
577 }
578 });
579
580 this.server.setRequestHandler(
581 ListPromptsRequestSchema,
582 (): ListPromptsResult => ({
583 prompts: Object.entries(this._registeredPrompts)
584 .filter(([, prompt]) => prompt.enabled)
585 .map(([name, prompt]): Prompt => {
586 return {
587 name,
588 title: prompt.title,
589 description: prompt.description,
590 arguments: prompt.argsSchema ? promptArgumentsFromSchema(prompt.argsSchema) : undefined
591 };
592 })
593 })
594 );
595
596 this.server.setRequestHandler(GetPromptRequestSchema, async (request, extra): Promise<GetPromptResult> => {
597 const prompt = this._registeredPrompts[request.params.name];
598 if (!prompt) {
599 throw new McpError(ErrorCode.InvalidParams, `Prompt ${request.params.name} not found`);
600 }
601
602 if (!prompt.enabled) {
603 throw new McpError(ErrorCode.InvalidParams, `Prompt ${request.params.name} disabled`);
604 }
605
606 if (prompt.argsSchema) {
607 const argsObj = normalizeObjectSchema(prompt.argsSchema) as AnyObjectSchema;
608 const parseResult = await safeParseAsync(argsObj, request.params.arguments);
609 if (!parseResult.success) {
610 const error = 'error' in parseResult ? parseResult.error : 'Unknown error';
611 const errorMessage = getParseErrorMessage(error);
612 throw new McpError(ErrorCode.InvalidParams, `Invalid arguments for prompt ${request.params.name}: ${errorMessage}`);
613 }
614
615 const args = parseResult.data;
616 const cb = prompt.callback as PromptCallback<PromptArgsRawShape>;
617 return await Promise.resolve(cb(args, extra));
618 } else {
619 const cb = prompt.callback as PromptCallback<undefined>;
620 // eslint-disable-next-line @typescript-eslint/no-explicit-any
621 return await Promise.resolve((cb as any)(extra));
622 }
623 });

Callers 2

promptMethod · 0.95
registerPromptMethod · 0.95

Calls 8

getMethodValueFunction · 0.85
normalizeObjectSchemaFunction · 0.85
safeParseAsyncFunction · 0.85
getParseErrorMessageFunction · 0.85
cbFunction · 0.85
registerCapabilitiesMethod · 0.45
setRequestHandlerMethod · 0.45

Tested by

no test coverage detected