|  | 
|  | 1 | +/** | 
|  | 2 | + * Structured Tool Call Logger for Test Analysis | 
|  | 3 | + * | 
|  | 4 | + * Logs every tool call in structured JSON format for easy parsing by test runner. | 
|  | 5 | + * Can output to STDOUT (JSON lines) or separate file. | 
|  | 6 | + */ | 
|  | 7 | +import fs from 'fs'; | 
|  | 8 | +import path from 'path'; | 
|  | 9 | + | 
|  | 10 | +class ToolCallLogger { | 
|  | 11 | +  constructor(options = {}) { | 
|  | 12 | +    this.enabled = options.enabled !== false; // Default: enabled | 
|  | 13 | +    this.outputMode = options.outputMode || 'file'; // 'file' | 'stdout' | 'both' | 
|  | 14 | +    this.logFile = options.logFile || '/tmp/mcp-tool-calls.jsonl'; // JSON lines format | 
|  | 15 | + | 
|  | 16 | +    // Ensure log directory exists | 
|  | 17 | +    if (this.outputMode === 'file' || this.outputMode === 'both') { | 
|  | 18 | +      const dir = path.dirname(this.logFile); | 
|  | 19 | +      if (!fs.existsSync(dir)) { | 
|  | 20 | +        fs.mkdirSync(dir, { recursive: true }); | 
|  | 21 | +      } | 
|  | 22 | +    } | 
|  | 23 | +  } | 
|  | 24 | + | 
|  | 25 | +  /** | 
|  | 26 | +   * Log a tool call start | 
|  | 27 | +   */ | 
|  | 28 | +  logToolCallStart(toolName, args, metadata = {}) { | 
|  | 29 | +    if (!this.enabled) return; | 
|  | 30 | + | 
|  | 31 | +    const entry = { | 
|  | 32 | +      type: 'tool_call_start', | 
|  | 33 | +      timestamp: new Date().toISOString(), | 
|  | 34 | +      tool: toolName, | 
|  | 35 | +      args: args, | 
|  | 36 | +      session_id: metadata.sessionId, | 
|  | 37 | +      request_id: metadata.requestId, | 
|  | 38 | +    }; | 
|  | 39 | + | 
|  | 40 | +    this._write(entry); | 
|  | 41 | +  } | 
|  | 42 | + | 
|  | 43 | +  /** | 
|  | 44 | +   * Log a tool call success | 
|  | 45 | +   */ | 
|  | 46 | +  logToolCallSuccess(toolName, args, result, metadata = {}) { | 
|  | 47 | +    if (!this.enabled) return; | 
|  | 48 | + | 
|  | 49 | +    const entry = { | 
|  | 50 | +      type: 'tool_call_success', | 
|  | 51 | +      timestamp: new Date().toISOString(), | 
|  | 52 | +      tool: toolName, | 
|  | 53 | +      args: args, | 
|  | 54 | +      result_summary: this._summarizeResult(result), | 
|  | 55 | +      duration_ms: metadata.duration, | 
|  | 56 | +      session_id: metadata.sessionId, | 
|  | 57 | +      request_id: metadata.requestId, | 
|  | 58 | +    }; | 
|  | 59 | + | 
|  | 60 | +    this._write(entry); | 
|  | 61 | +  } | 
|  | 62 | + | 
|  | 63 | +  /** | 
|  | 64 | +   * Log a tool call failure | 
|  | 65 | +   */ | 
|  | 66 | +  logToolCallError(toolName, args, error, metadata = {}) { | 
|  | 67 | +    if (!this.enabled) return; | 
|  | 68 | + | 
|  | 69 | +    const entry = { | 
|  | 70 | +      type: 'tool_call_error', | 
|  | 71 | +      timestamp: new Date().toISOString(), | 
|  | 72 | +      tool: toolName, | 
|  | 73 | +      args: args, | 
|  | 74 | +      error: { | 
|  | 75 | +        message: error.message, | 
|  | 76 | +        code: error.code, | 
|  | 77 | +        name: error.name, | 
|  | 78 | +      }, | 
|  | 79 | +      duration_ms: metadata.duration, | 
|  | 80 | +      session_id: metadata.sessionId, | 
|  | 81 | +      request_id: metadata.requestId, | 
|  | 82 | +    }; | 
|  | 83 | + | 
|  | 84 | +    this._write(entry); | 
|  | 85 | +  } | 
|  | 86 | + | 
|  | 87 | +  /** | 
|  | 88 | +   * Clear the log file (for new test session) | 
|  | 89 | +   */ | 
|  | 90 | +  clear() { | 
|  | 91 | +    if (this.outputMode === 'file' || this.outputMode === 'both') { | 
|  | 92 | +      try { | 
|  | 93 | +        fs.writeFileSync(this.logFile, '', 'utf8'); | 
|  | 94 | +      } catch (error) { | 
|  | 95 | +        console.error('Failed to clear tool call log:', error.message); | 
|  | 96 | +      } | 
|  | 97 | +    } | 
|  | 98 | +  } | 
|  | 99 | + | 
|  | 100 | +  /** | 
|  | 101 | +   * Get all logged tool calls (for analysis) | 
|  | 102 | +   */ | 
|  | 103 | +  getToolCalls() { | 
|  | 104 | +    if (this.outputMode === 'stdout') { | 
|  | 105 | +      throw new Error('Cannot retrieve tool calls when outputMode is stdout'); | 
|  | 106 | +    } | 
|  | 107 | + | 
|  | 108 | +    try { | 
|  | 109 | +      const content = fs.readFileSync(this.logFile, 'utf8'); | 
|  | 110 | +      return content | 
|  | 111 | +        .split('\n') | 
|  | 112 | +        .filter(line => line.trim()) | 
|  | 113 | +        .map(line => JSON.parse(line)); | 
|  | 114 | +    } catch (error) { | 
|  | 115 | +      if (error.code === 'ENOENT') { | 
|  | 116 | +        return []; // File doesn't exist yet | 
|  | 117 | +      } | 
|  | 118 | +      throw error; | 
|  | 119 | +    } | 
|  | 120 | +  } | 
|  | 121 | + | 
|  | 122 | +  /** | 
|  | 123 | +   * Write log entry | 
|  | 124 | +   */ | 
|  | 125 | +  _write(entry) { | 
|  | 126 | +    const jsonLine = JSON.stringify(entry); | 
|  | 127 | + | 
|  | 128 | +    // Write to stdout (for BashOutput monitoring) | 
|  | 129 | +    if (this.outputMode === 'stdout' || this.outputMode === 'both') { | 
|  | 130 | +      console.log(`TOOL_CALL:${jsonLine}`); | 
|  | 131 | +    } | 
|  | 132 | + | 
|  | 133 | +    // Write to file (for Read tool analysis) | 
|  | 134 | +    if (this.outputMode === 'file' || this.outputMode === 'both') { | 
|  | 135 | +      try { | 
|  | 136 | +        fs.appendFileSync(this.logFile, jsonLine + '\n', 'utf8'); | 
|  | 137 | +      } catch (error) { | 
|  | 138 | +        console.error('Failed to write tool call log:', error.message); | 
|  | 139 | +      } | 
|  | 140 | +    } | 
|  | 141 | +  } | 
|  | 142 | + | 
|  | 143 | +  /** | 
|  | 144 | +   * Summarize result for logging (avoid huge objects) | 
|  | 145 | +   */ | 
|  | 146 | +  _summarizeResult(result) { | 
|  | 147 | +    if (!result) return null; | 
|  | 148 | + | 
|  | 149 | +    // For MCP tool results with content array | 
|  | 150 | +    if (result.content && Array.isArray(result.content)) { | 
|  | 151 | +      return { | 
|  | 152 | +        type: 'mcp_result', | 
|  | 153 | +        content_count: result.content.length, | 
|  | 154 | +        content_types: result.content.map(c => c.type), | 
|  | 155 | +        has_text: result.content.some(c => c.type === 'text'), | 
|  | 156 | +        text_length: result.content | 
|  | 157 | +          .filter(c => c.type === 'text') | 
|  | 158 | +          .reduce((sum, c) => sum + (c.text?.length || 0), 0), | 
|  | 159 | +      }; | 
|  | 160 | +    } | 
|  | 161 | + | 
|  | 162 | +    // For simple results | 
|  | 163 | +    if (typeof result === 'object') { | 
|  | 164 | +      return { | 
|  | 165 | +        type: 'object', | 
|  | 166 | +        keys: Object.keys(result), | 
|  | 167 | +      }; | 
|  | 168 | +    } | 
|  | 169 | + | 
|  | 170 | +    return { | 
|  | 171 | +      type: typeof result, | 
|  | 172 | +      value: String(result).substring(0, 100), // First 100 chars | 
|  | 173 | +    }; | 
|  | 174 | +  } | 
|  | 175 | +} | 
|  | 176 | + | 
|  | 177 | +// Singleton instance | 
|  | 178 | +let instance = null; | 
|  | 179 | + | 
|  | 180 | +/** | 
|  | 181 | + * Initialize tool call logger (call once at server startup) | 
|  | 182 | + */ | 
|  | 183 | +export function initializeToolCallLogger(options = {}) { | 
|  | 184 | +  // Check environment variable to enable/disable | 
|  | 185 | +  const enabled = process.env.LOG_TOOL_CALLS !== 'false'; // Default: enabled | 
|  | 186 | +  const outputMode = process.env.TOOL_CALL_LOG_MODE || 'file'; // 'file' | 'stdout' | 'both' | 
|  | 187 | +  const logFile = process.env.TOOL_CALL_LOG_FILE || '/tmp/mcp-tool-calls.jsonl'; | 
|  | 188 | + | 
|  | 189 | +  instance = new ToolCallLogger({ | 
|  | 190 | +    enabled, | 
|  | 191 | +    outputMode, | 
|  | 192 | +    logFile, | 
|  | 193 | +    ...options, | 
|  | 194 | +  }); | 
|  | 195 | + | 
|  | 196 | +  return instance; | 
|  | 197 | +} | 
|  | 198 | + | 
|  | 199 | +/** | 
|  | 200 | + * Get the tool call logger instance | 
|  | 201 | + */ | 
|  | 202 | +export function getToolCallLogger() { | 
|  | 203 | +  if (!instance) { | 
|  | 204 | +    // Auto-initialize with defaults if not explicitly initialized | 
|  | 205 | +    instance = new ToolCallLogger(); | 
|  | 206 | +  } | 
|  | 207 | +  return instance; | 
|  | 208 | +} | 
|  | 209 | + | 
|  | 210 | +export { ToolCallLogger }; | 
0 commit comments