@logspace/mcp-server
Version:
MCP server for Logspace log analysis integration with AI models.
92 lines • 3.54 kB
JavaScript
/**
* Optimized response formatting utilities
*/
export class ResponseFormatter {
static defaultOptions = {
includeTimestamps: true,
timestampFormat: 'iso',
includeBodies: true,
maxBodyLength: 1000,
maxArrayLength: 100,
compactMode: false,
};
static formatNetworkRequest(req, options = {}) {
const opts = { ...this.defaultOptions, ...options };
const base = {
method: req.method,
url: req.url,
status: req.status,
duration: `${req.duration}ms`,
};
if (opts.includeTimestamps) {
base.timestamp = this.formatTimestamp(req.timestamp, opts.timestampFormat);
}
if (opts.includeBodies) {
return {
...base,
requestBody: this.truncateIfNeeded(req.requestBody, opts.maxBodyLength),
responseBody: this.truncateIfNeeded(req.responseBody, opts.maxBodyLength),
headers: opts.compactMode ? undefined : Object.fromEntries(req.headers || []),
};
}
return base;
}
static formatConsoleLog(log, options = {}) {
const opts = { ...this.defaultOptions, ...options };
return {
type: log.type,
message: this.formatMessage(log.message, opts.maxBodyLength),
timestamp: opts.includeTimestamps ? this.formatTimestamp(log.timestamp, opts.timestampFormat) : undefined,
};
}
static formatError(error, options = {}) {
const opts = { ...this.defaultOptions, ...options };
return {
message: error.message,
type: error.type,
location: error.filename ? `${error.filename}:${error.lineno}:${error.colno}` : 'Unknown',
stack: opts.compactMode ? undefined : error.stack,
timestamp: opts.includeTimestamps ? this.formatTimestamp(error.timestamp, opts.timestampFormat) : undefined,
};
}
static formatUserAction(action, options = {}) {
const opts = { ...this.defaultOptions, ...options };
return {
type: action.type,
element: action.element,
value: this.truncateIfNeeded(action.value, opts.maxBodyLength),
timestamp: opts.includeTimestamps ? this.formatTimestamp(action.timestamp, opts.timestampFormat) : undefined,
};
}
static formatTimestamp(timestamp, format) {
switch (format) {
case 'unix':
return timestamp;
case 'relative':
const now = Date.now();
const diff = now - timestamp;
return `${Math.floor(diff / 1000)}s ago`;
case 'iso':
default:
return new Date(timestamp).toISOString();
}
}
static formatMessage(message, maxLength) {
if (!message || message.length === 0)
return '';
let result = '';
for (const item of message.slice(0, 10)) {
// Limit items
const str = typeof item === 'object' ? JSON.stringify(item).substring(0, 200) : String(item).substring(0, 200);
result += str + ' ';
}
return this.truncateIfNeeded(result.trim(), maxLength);
}
static truncateIfNeeded(value, maxLength) {
if (typeof value === 'string' && value.length > maxLength) {
return value.substring(0, maxLength) + '...';
}
return value;
}
}
//# sourceMappingURL=responseFormatter.js.map