mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
fix(mcp): prompts take the trip id as the string every client sends (#2207)
Prompt arguments cross the wire as strings, but the three trip prompts declared tripId as z.number(), so every prompts/get failed with -32602 "expected number, received string", MCP Inspector included. The id is now parsed from the string through a shared tripIdPromptArg (still an integer in the safe range), PromptOptions only admits string-input schemas so the mistake cannot come back unnoticed, and a prompt declared with an empty argsSchema no longer demands an empty arguments object from the client. The prompt tests run through a real in-memory MCP client instead of calling the callbacks directly.
This commit is contained in:
@@ -38,6 +38,7 @@ export class ThingsMcp {
|
||||
- `@Resource(options)` — fixed URI (`uri`, `mimeType`, …). Handler: `(uri: URL, ctx)`.
|
||||
- `@ResourceTemplate(options)` — `uriTemplate` (RFC 6570). Handler: `(uri: URL, variables, ctx)`. (No `list`/`complete` template callbacks yet — extend when a domain needs them.)
|
||||
- `@Prompt(options)` — mirrors `registerPrompt` (`argsSchema` entries must be string-valued Zod schemas). Handler: `(args, ctx)`.
|
||||
Prompt arguments arrive as strings, so every `argsSchema` entry has to accept a string (`PromptArgsShape`): parse numbers with `z.string().regex(...).transform(Number)`, never `z.number()`. The handler receives the transformed values.
|
||||
|
||||
`ctx` always takes the SDK `extra` slot — the last handler argument.
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ export {
|
||||
type McpModuleOptions,
|
||||
type McpRegistryListing,
|
||||
type McpZodSchema,
|
||||
type PromptArgsShape,
|
||||
type PromptOptions,
|
||||
type ResourceOptions,
|
||||
type ResourceTemplateOptions,
|
||||
|
||||
@@ -368,13 +368,19 @@ export class McpRegistry {
|
||||
ctx: McpContext,
|
||||
opts?: McpAttachOptions,
|
||||
): void {
|
||||
// An empty shape means "no arguments", and is registered as such: with a
|
||||
// shape the SDK parses request.params.arguments, which a client that has
|
||||
// nothing to send may omit entirely, and z.object({}) then rejects the
|
||||
// undefined with -32602.
|
||||
const argsSchema =
|
||||
options.argsSchema !== undefined && Object.keys(options.argsSchema).length > 0 ? options.argsSchema : undefined;
|
||||
const config: Record<string, unknown> = {
|
||||
title: options.title,
|
||||
description: options.description,
|
||||
argsSchema: options.argsSchema,
|
||||
argsSchema,
|
||||
};
|
||||
const cb =
|
||||
options.argsSchema !== undefined
|
||||
argsSchema !== undefined
|
||||
? (args: unknown, _extra: unknown) => {
|
||||
opts?.onInvoke?.({ kind: 'prompt', name: options.name });
|
||||
return handler.call(instance, args, ctx);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Scope, ScopeGroup } from '../mcp/scopes';
|
||||
|
||||
import type { ZodRawShape } from 'zod';
|
||||
import type { ZodRawShape, ZodType } from 'zod';
|
||||
|
||||
/**
|
||||
* The mode half of every scope: 'read' | 'write' | 'delete' | 'share'.
|
||||
@@ -152,9 +152,18 @@ export interface ResourceTemplateOptions extends McpEntryOptionsBase {
|
||||
_meta?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
/** Mirrors the SDK's `registerPrompt` config (argsSchema entries must be string-valued Zod schemas). */
|
||||
/**
|
||||
* Prompt arguments cross the wire as strings (GetPromptRequest.arguments is
|
||||
* a Record<string, string>), so every entry has to accept a string and may
|
||||
* transform from there. A bare z.number() type-checks against ZodRawShape but
|
||||
* rejects every real client with "expected number, received string" (#2207);
|
||||
* the input constraint here turns that into a compile error.
|
||||
*/
|
||||
export type PromptArgsShape = Record<string, ZodType<unknown, string | undefined>>;
|
||||
|
||||
/** Mirrors the SDK's `registerPrompt` config. */
|
||||
export interface PromptOptions extends McpEntryOptionsBase {
|
||||
argsSchema?: ZodRawShape;
|
||||
argsSchema?: PromptArgsShape;
|
||||
}
|
||||
|
||||
export type McpEntryKind = 'tool' | 'resource' | 'resourceTemplate' | 'prompt';
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Prompt arguments arrive as strings (GetPromptRequest.arguments is a
|
||||
* Record<string, string>), so a numeric id has to be parsed out of one.
|
||||
* The old z.number().int().positive() failed every client, MCP Inspector
|
||||
* included, with "expected number, received string" (#2207).
|
||||
*/
|
||||
export const tripIdPromptArg = z
|
||||
.string()
|
||||
.regex(/^[1-9]\d*$/, 'Trip ID must be a positive integer')
|
||||
.transform(Number)
|
||||
.pipe(z.number().int().positive());
|
||||
@@ -1,8 +1,8 @@
|
||||
import { McpController, Prompt, type McpContext } from '../../nest-mcp';
|
||||
import { z } from 'zod';
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
import { addonGate } from '../addons/addon-gate';
|
||||
import { AddonsService } from '../addons/addons.service';
|
||||
import { tripIdPromptArg } from '../mcp-shared/prompt-args';
|
||||
import { PackingService } from '../packing/packing.service';
|
||||
import { TripReadModelService } from '../trip-read-model/trip-read-model.service';
|
||||
import { TripsService } from './trips.service';
|
||||
@@ -35,7 +35,7 @@ export class TripPromptsMcp {
|
||||
title: 'Budget Overview',
|
||||
description: 'Get a formatted budget summary for a trip',
|
||||
argsSchema: {
|
||||
tripId: z.number().int().positive().describe('Trip ID'),
|
||||
tripId: tripIdPromptArg.describe('Trip ID'),
|
||||
},
|
||||
when: budgetAddonOn,
|
||||
})
|
||||
@@ -72,7 +72,7 @@ export class TripPromptsMcp {
|
||||
title: 'Packing List',
|
||||
description: 'Get a formatted packing checklist for a trip',
|
||||
argsSchema: {
|
||||
tripId: z.number().int().positive().describe('Trip ID'),
|
||||
tripId: tripIdPromptArg.describe('Trip ID'),
|
||||
},
|
||||
when: packingAddonOn,
|
||||
})
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
demoDenied, errorResult, ok,
|
||||
} from '../../nest-mcp';
|
||||
import { McpToolGuardsService } from '../mcp-shared/mcp-tool-guards.service';
|
||||
import { tripIdPromptArg } from '../mcp-shared/prompt-args';
|
||||
import { z } from 'zod';
|
||||
import { AuthService } from '../auth/auth.service';
|
||||
import { CalendarService } from '../calendar/calendar.service';
|
||||
@@ -579,7 +580,7 @@ export class TripsMcp {
|
||||
title: 'Trip Summary',
|
||||
description: 'Load a full summary of a trip for context before planning or modifications',
|
||||
argsSchema: {
|
||||
tripId: z.number().int().positive().describe('Trip ID to summarize'),
|
||||
tripId: tripIdPromptArg.describe('Trip ID to summarize'),
|
||||
},
|
||||
})
|
||||
async tripSummaryPrompt({ tripId }: { tripId: number }, ctx: McpContext) {
|
||||
|
||||
Reference in New Issue
Block a user