From c48781811c6fa522ef85d2370d24dc97b050de33 Mon Sep 17 00:00:00 2001 From: Studio Date: Mon, 30 Nov 2026 23:29:51 -0500 Subject: [PATCH] T-129: edits made in the worktree --- libs/research/src/mcp/protocol.ts | 37 +++++++++++++++++++++++++++---- libs/research/src/mcp/server.ts | 14 +++++------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/libs/research/src/mcp/protocol.ts b/libs/research/src/mcp/protocol.ts index f3d66d8c..fe7efcd6 100644 --- a/libs/research/src/mcp/protocol.ts +++ b/libs/research/src/mcp/protocol.ts @@ -113,6 +113,24 @@ export interface McpToolDescriptor { readonly inputSchema: JsonSchemaObject; } +/** Valid MCP progress token (`string | finite number`). */ +export const mcpProgressTokenSchema = z.union([z.string(), z.number().finite()]); +export type McpProgressToken = z.infer; + +/** Standard optional MCP `_meta` request/notification metadata bag. */ +export const mcpRequestMetaSchema = z + .object({ + progressToken: mcpProgressTokenSchema.optional(), + }) + .passthrough(); +export type McpRequestMeta = z.infer; + +const jsonObjectSchema = z.custom>( + (value): value is Record => + typeof value === 'object' && value !== null && !Array.isArray(value), + { message: 'Expected a JSON object.' } +); + /** Strict MCP `initialize` params schema. */ export const mcpInitializeParamsSchema = z .object({ @@ -125,27 +143,38 @@ export const mcpInitializeParamsSchema = z }) .strict() .optional(), + _meta: mcpRequestMetaSchema.optional(), }) .strict(); /** Strict MCP `notifications/initialized` params schema. */ -export const mcpInitializedNotificationParamsSchema = z.object({}).strict(); +export const mcpInitializedNotificationParamsSchema = z + .object({ + _meta: mcpRequestMetaSchema.optional(), + }) + .strict(); /** Strict MCP `ping` params schema. */ -export const mcpPingParamsSchema = z.object({}).strict(); +export const mcpPingParamsSchema = z + .object({ + _meta: mcpRequestMetaSchema.optional(), + }) + .strict(); /** Strict MCP `tools/list` params schema. */ export const mcpToolsListParamsSchema = z .object({ cursor: z.string().min(1).optional(), + _meta: mcpRequestMetaSchema.optional(), }) .strict(); -/** Strict MCP `tools/call` params schema. */ +/** Strict MCP `tools/call` params schema (preserves own keys on `arguments`). */ export const mcpToolsCallParamsSchema = z .object({ name: z.string().min(1), - arguments: z.record(z.unknown()).optional(), + arguments: jsonObjectSchema.optional(), + _meta: mcpRequestMetaSchema.optional(), }) .strict(); diff --git a/libs/research/src/mcp/server.ts b/libs/research/src/mcp/server.ts index 08a80b73..dec43185 100644 --- a/libs/research/src/mcp/server.ts +++ b/libs/research/src/mcp/server.ts @@ -320,17 +320,13 @@ function formatZodIssues(error: z.ZodError): string { return parts.join('; '); } +const SAFE_ERROR_NAME_RE = /^[A-Za-z][A-Za-z0-9_]{0,63}$/; + function sanitizeToolError(err: unknown): string { if (err instanceof Error) { - const rawName = typeof err.name === 'string' && err.name.trim().length > 0 ? err.name.trim() : 'Error'; - const firstLine = (typeof err.message === 'string' ? err.message : '') - .split(/\r?\n/, 1)[0] - ?.replace(/\s+at\s+\S.*$/u, '') - .trim(); - if (firstLine !== undefined && firstLine.length > 0) { - return `${rawName}: ${firstLine}`; - } - return `${rawName}: Tool execution failed.`; + const rawName = typeof err.name === 'string' ? err.name.trim() : ''; + const safeName = SAFE_ERROR_NAME_RE.test(rawName) ? rawName : 'Error'; + return `${safeName}: Tool execution failed.`; } return 'Error: Tool execution failed.'; }