// services/satellite/src/core/mcp-server-wrapper.ts
interface BufferedRequestEntry {
installation_id: string;
team_id: string;
user_id?: string;
tool_name: string;
tool_params: Record<string, unknown>;
tool_response?: unknown; // Full MCP server response
response_time_ms: number;
success: boolean;
error_message?: string;
timestamp: string;
}
class McpServerWrapper {
private requestLogBuffer: BufferedRequestEntry[] = [];
private requestLogFlushTimeout: NodeJS.Timeout | null = null;
private readonly REQUEST_LOG_BATCH_INTERVAL_MS = 3000;
private readonly REQUEST_LOG_BATCH_MAX_SIZE = 20;
async handleExecuteTool(toolPath: string, toolArguments: unknown) {
const startTime = Date.now();
let result: unknown;
let success = false;
let errorMessage: string | undefined;
try {
result = await this.executeToolCall(toolPath, toolArguments);
success = true;
} catch (error) {
errorMessage = error instanceof Error ? error.message : 'Unknown error';
} finally {
const responseTimeMs = Date.now() - startTime;
// Check if logging is enabled (default: true)
const loggingEnabled = config?.settings?.request_logging_enabled !== false;
// Buffer request log if installation context exists and logging enabled
if ((config?.installation_id && config?.team_id) && loggingEnabled) {
this.bufferRequestLogEntry({
installation_id: config.installation_id,
team_id: config.team_id,
user_id: config.user_id,
tool_name: toolPath,
tool_params: toolArguments as Record<string, unknown>,
tool_response: result, // Captured response
response_time_ms: responseTimeMs,
success,
error_message: errorMessage,
timestamp: new Date().toISOString()
});
}
}
return result;
}
private bufferRequestLogEntry(entry: BufferedRequestEntry) {
this.requestLogBuffer.push(entry);
// Force flush if buffer full
if (this.requestLogBuffer.length >= this.REQUEST_LOG_BATCH_MAX_SIZE) {
this.flushRequestLogBuffer();
} else {
this.scheduleRequestLogFlush();
}
}
private flushRequestLogBuffer() {
if (this.requestLogBuffer.length === 0) return;
// Group by installation
const grouped = this.groupRequestsByInstallation(this.requestLogBuffer);
// Emit one event per installation
for (const [key, requests] of grouped.entries()) {
this.eventBus?.emit('mcp.request.logs', {
installation_id: requests[0].installation_id,
team_id: requests[0].team_id,
requests: requests.map(req => ({
user_id: req.user_id,
tool_name: req.tool_name,
tool_params: req.tool_params,
tool_response: req.tool_response, // Include response
response_time_ms: req.response_time_ms,
success: req.success,
error_message: req.error_message,
timestamp: req.timestamp
}))
});
}
// Clear buffer
this.requestLogBuffer = [];
this.requestLogFlushTimeout = null;
}
}