UNPKG

@logspace/mcp-server

Version:

MCP server for Logspace log analysis integration with AI models.

95 lines 3.68 kB
import { z } from 'zod'; import { getApiClient } from '../services/apiClient.js'; import { getLogParser } from '../services/logParser.js'; import { formatError, logError, logInfo, isAuthError } from '../utils/errorHandler.js'; import { validateBugId, validateErrorIndex } from '../utils/validator.js'; export const GetErrorContextSchema = z.object({ bugId: z.union([z.number(), z.string()]).describe('The ID of the bug'), errorIndex: z .union([z.number(), z.string()]) .optional() .describe('Index of the error to get context for (default: 0)'), }); export async function getErrorContext(args) { try { const bugId = validateBugId(args.bugId); const errorIndex = validateErrorIndex(args.errorIndex); logInfo(`Getting error context for bug ID: ${bugId}, error index: ${errorIndex}`); const apiClient = getApiClient(); const logData = await apiClient.fetchLogData(bugId); const logParser = getLogParser(); const context = logParser.getErrorContext(logData, errorIndex); // Format the context const formattedContext = { error: { message: context.error.message, type: context.error.type, location: context.error.filename ? `${context.error.filename}:${context.error.lineno}:${context.error.colno}` : 'Unknown', stack: context.error.stack, timestamp: new Date(context.error.timestamp).toISOString(), }, relatedLogs: context.relatedLogs.map((log) => ({ type: log.type, message: log.message.map((m) => (typeof m === 'object' ? JSON.stringify(m) : String(m))).join(' '), timestamp: new Date(log.timestamp).toISOString(), })), networkContext: context.networkContext.map((req) => ({ method: req.method, url: req.url, status: req.status, duration: `${req.duration}ms`, requestBody: req.requestBody, responseBody: req.responseBody, timestamp: new Date(req.timestamp).toISOString(), })), userActionsBeforeError: context.userActionsBeforeError.map((action) => ({ type: action.type, element: action.element, value: action.value, timestamp: new Date(action.timestamp).toISOString(), })), }; return { content: [ { type: 'text', text: JSON.stringify({ success: true, context: formattedContext, }, null, 2), }, ], }; } catch (error) { logError('get_error_context', error); // Check if this is an authentication error const authErrorMessage = isAuthError(error); if (authErrorMessage) { return { content: [ { type: 'text', text: authErrorMessage, }, ], isError: true, }; } return { content: [ { type: 'text', text: JSON.stringify({ success: false, error: formatError(error), }, null, 2), }, ], isError: true, }; } } //# sourceMappingURL=getErrorContext.js.map