// tools/safe-handler.ts — Generic error-boundary wrapper for MCP tool handlers.
// Catches any unhandled error and returns a graceful MCP error response
// instead of crashing the server.
import { withLicenseGate } from './license-gate.js';
/**
 * Wraps a tool handler so that any unhandled error is caught and returned as a
 * graceful MCP error response, preventing server crashes.
 *
 * Usage:
 *   server.registerTool('my_tool', schema, safe(async (args) => { ... }));
 */
export function safe(handler) {
    return async (args) => {
        try {
            return await handler(args);
        }
        catch (error) {
            const message = error instanceof Error ? error.message : String(error);
            return {
                content: [{ type: 'text', text: `Error: ${message}` }],
                isError: true,
            };
        }
    };
}
/**
 * Composable wrapper: license gate + error boundary.
 * Use for tools that require a paid plan. Free tools should use safe() directly.
 *
 * Usage:
 *   server.registerTool('pro_tool', schema, safeLicensed('pro_tool', async (args) => { ... }));
 */
export function safeLicensed(toolName, handler) {
    return safe(withLicenseGate(toolName, handler));
}
//# sourceMappingURL=safe-handler.js.map