UNPKG

@sentry/core

Version:
73 lines (72 loc) • 2.35 kB
import { getClient } from "../../currentScopes.js"; import { captureException } from "../../exports.js"; import "../../tracing/spanstatus.js"; import "../../tracing/index.js"; import { isObjectLike } from "../../utils/is.js"; import { getActiveSpan } from "../../utils/spanUtils.js"; // src/integrations/mcp-server/errorCapture.ts /** * Safe error capture utilities for MCP server instrumentation * * Ensures error reporting never interferes with MCP server operation. * All capture operations are wrapped in try-catch to prevent side effects. */ /** * Classifies JSON-RPC errors using MCP server semantic conventions. * @param code - JSON-RPC error code * @see https://github.com/open-telemetry/semantic-conventions-genai/blob/main/docs/gen-ai/mcp.md#server */ function isJsonRpcServerError(code) { return ![ -32700, -32600, -32601, -32602, -32002 ].includes(code); } /** * Captures an error without affecting MCP server operation. * * The active span already contains all MCP context (method, tool, arguments, etc.) * @param error - Error to capture * @param errorType - Classification of error type for filtering * @param extraData - Additional context data to include */ function captureError(error, errorType, extraData) { try { if (!getClient()) return; const activeSpan = getActiveSpan(); if (activeSpan?.isRecording()) activeSpan.setStatus({ code: 2, message: "internal_error" }); captureException(error, { mechanism: { type: "auto.ai.mcp_server", handled: false, data: { error_type: errorType || "handler_execution", ...extraData } } }); } catch {} } /** * Captures JSON-RPC error responses for server-side errors. * @see https://www.jsonrpc.org/specification#error_object * @param errorResponse - JSON-RPC error response */ function captureJsonRpcErrorResponse(errorResponse) { try { if (isObjectLike(errorResponse) && "code" in errorResponse && "message" in errorResponse) { const jsonRpcError = errorResponse; if (typeof jsonRpcError.code === "number" && isJsonRpcServerError(jsonRpcError.code)) { const error = new Error(jsonRpcError.message); error.name = `JsonRpcError_${jsonRpcError.code}`; captureError(error, "protocol"); } } } catch {} } export { captureError, captureJsonRpcErrorResponse, isJsonRpcServerError }; //# sourceMappingURL=errorCapture.js.map