mcp-ai-agent-guidelines
Version:
A comprehensive Model Context Protocol server providing advanced tools, resources, and prompts for implementing AI agent best practices
39 lines • 1.78 kB
JavaScript
import { ZodError } from "zod";
import { ErrorCode } from "./error-codes.js";
// biome-ignore lint/correctness/noUnusedImports: validationError import required by specification
import { schemaViolationError } from "./error-factory.js";
import { McpToolError } from "./errors.js";
export function handleToolError(error) {
// Already an McpToolError - use directly
if (error instanceof McpToolError) {
return error.toResponse();
}
// ZodError - convert to schema violation
if (error instanceof ZodError) {
return schemaViolationError(error.errors).toResponse();
}
// Standard Error - detect type from message patterns
if (error instanceof Error) {
const mcpError = detectErrorType(error);
return mcpError.toResponse();
}
// Unknown error type
return new McpToolError(ErrorCode.INTERNAL_ERROR, "An unexpected error occurred", { originalError: String(error) }).toResponse();
}
function detectErrorType(error) {
const message = error.message.toLowerCase();
if (message.includes("required") || message.includes("missing")) {
return new McpToolError(ErrorCode.MISSING_REQUIRED_FIELD, error.message);
}
if (message.includes("invalid") || message.includes("validation")) {
return new McpToolError(ErrorCode.VALIDATION_FAILED, error.message);
}
if (message.includes("session") && message.includes("not found")) {
return new McpToolError(ErrorCode.SESSION_NOT_FOUND, error.message);
}
if (message.includes("enoent") || message.includes("file not found")) {
return new McpToolError(ErrorCode.FILE_NOT_FOUND, error.message);
}
return new McpToolError(ErrorCode.INTERNAL_ERROR, error.message, {}, error);
}
//# sourceMappingURL=error-handler.js.map