UNPKG

endgame-mcp

Version:

[![github](https://github.com/user-attachments/assets/72708ed2-ab7e-49b4-8bb4-00c452ab4a44)](https://endgame.dev)

56 lines (50 loc) 1.89 kB
import { log } from './logger.js'; import { validateApiKey } from '../sdk.js'; /** * Creates a clean error object with only essential information and returns it as stringified JSON. * Used for consistent error responses across the MCP server tools. * * @param {object} params - The parameters object * @param {string} params.error - The main error message * @returns {string} Stringified JSON error object with error property */ export const mcpError = ({ error }) => { const errorObj = { error }; return JSON.stringify(errorObj); }; /** * Standard error handler for MCP tools that wraps tool functions and catches errors. * Validates API key before executing any tool and converts any thrown errors into clean mcpError format. * This validation happens before any dotfile validation or tool logic. * This is the only place that should use mcpError - all other code can throw standard errors. * * @param {Function} toolFunction - The tool function to wrap with error handling * @returns {Function} Wrapped function that handles errors consistently */ export const errorHandler = toolFunction => { return async params => { try { // ALWAYS validate API key first, before any other operations await validateApiKey(); // Execute the tool function and return its result return await toolFunction(params); } catch (error) { // Log the original error for debugging and monitoring log('mcp.tool.error', { error: error.message || String(error), stack: error.stack, }); // Extract error message from the error const errorMessage = error?.message || String(error); // Return standardized error format using mcpError return { content: [ { type: 'text', text: mcpError({ error: errorMessage }), }, ], }; } }; };