/** * =============================================================================== * MCP RESPONSE ADAPTER * =============================================================================== * * Bridges the unified Result pattern to MCP's CallToolResult format. * Provides consistent response formatting for all MCP tool handlers. * * Usage: * import { toMcpResult, mcpSuccess, mcpFailure } from '../core/result/mcp-adapter.js'; * * // From a Result * const result = await someOperation(); * return toMcpResult(result); * * // Direct creation * return mcpSuccess({ id: 1, name: 'test' }); * return mcpFailure('NOT_FOUND', 'Item not found'); * * =============================================================================== */ import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js'; import type { Result } from './types.js'; /** * Options for MCP response formatting. */ export interface McpResponseOptions { /** Pretty print JSON output (default: true) */ prettyPrint?: boolean; /** Include metadata in response (default: false for cleaner output) */ includeMetadata?: boolean; /** Include timestamp in response (default: true) */ includeTimestamp?: boolean; } /** * Standard success response structure for MCP tools. */ export interface McpSuccessPayload { success: true; data: T; timestamp?: string; } /** * Standard error response structure for MCP tools. */ export interface McpErrorPayload { success: false; error: { code: string; message: string; details?: Record; retryable?: boolean; }; timestamp: string; } /** * Converts a Result to MCP's CallToolResult format. * * @param result - The Result to convert * @param options - Formatting options * @returns CallToolResult for MCP response * * @example * ```typescript * const result = await validateSymbol(input); * return toMcpResult(result); * ``` */ export declare function toMcpResult(result: Result, options?: McpResponseOptions): CallToolResult; /** * Creates an MCP success response directly from data. * * @param data - The success data * @param options - Formatting options * @returns CallToolResult for MCP response * * @example * ```typescript * return mcpSuccess({ validated: true, symbolId: 'Ξ.COMPANY.NVDA' }); * ``` */ export declare function mcpSuccess(data: T, options?: McpResponseOptions): CallToolResult; /** * Creates an MCP failure response directly. * * @param code - Machine-readable error code * @param message - Human-readable error message * @param details - Optional additional details * @param options - Formatting options * @returns CallToolResult with isError: true * * @example * ```typescript * return mcpFailure('VALIDATION_FAILED', 'Invalid symbol format', { input }); * ``` */ export declare function mcpFailure(code: string, message: string, details?: Record, options?: McpResponseOptions): CallToolResult; /** * Creates an MCP failure response in legacy format for backwards compatibility. * Uses simple { error: "message" } format instead of structured error. * * @param message - Error message * @param options - Formatting options * @returns CallToolResult with isError: true * * @deprecated Use mcpFailure for new code */ export declare function mcpLegacyFailure(message: string, options?: McpResponseOptions): CallToolResult; /** * Creates an MCP failure response from an Error object. * * @param error - The error to convert * @param defaultCode - Default error code if not present on error * @param options - Formatting options * @returns CallToolResult with isError: true * * @example * ```typescript * try { * await riskyOperation(); * } catch (err) { * return mcpFromError(err, 'OPERATION_FAILED'); * } * ``` */ export declare function mcpFromError(error: Error | unknown, defaultCode?: string, options?: McpResponseOptions): CallToolResult; /** * Legacy response format used by existing tool handlers. * For migration purposes only. */ export interface LegacyToolResponse { success?: boolean; error?: string; message?: string; [key: string]: unknown; } /** * Converts a legacy tool response to MCP format. * Use this to wrap existing tool handlers during migration. * * @param response - Legacy response object * @param options - Formatting options * @returns CallToolResult for MCP response * * @example * ```typescript * const legacyResult = await oldToolHandler(args); * return fromLegacyResponse(legacyResult); * ``` */ export declare function fromLegacyResponse(response: LegacyToolResponse, options?: McpResponseOptions): CallToolResult; /** * Creates an MCP response for batch operations with summary. * * @param results - Array of individual results * @param summary - Batch operation summary * @param options - Formatting options * @returns CallToolResult for MCP response */ export declare function mcpBatchResult(results: Array<{ key: string; result: Result; }>, summary: { total: number; succeeded: number; failed: number; }, options?: McpResponseOptions): CallToolResult; /** * Wraps an async tool handler to automatically convert Results to MCP format. * * @param handler - Async function returning Result * @param errorCode - Default error code for unexpected errors * @returns Wrapped handler returning CallToolResult * * @example * ```typescript * const handler = wrapToolHandler( * async (args) => validateSymbol(args.symbol), * 'VALIDATION_FAILED' * ); * ``` */ export declare function wrapToolHandler(handler: (args: TArgs) => Promise>, errorCode?: string): (args: TArgs) => Promise; /** * Wraps a sync tool handler to automatically convert Results to MCP format. * * @param handler - Sync function returning Result * @param errorCode - Default error code for unexpected errors * @returns Wrapped handler returning CallToolResult */ export declare function wrapToolHandlerSync(handler: (args: TArgs) => Result, errorCode?: string): (args: TArgs) => CallToolResult; //# sourceMappingURL=mcp-adapter.d.ts.map