Heuristic:0xKoda WireMCP Temp File Cleanup
| Knowledge Sources | |
|---|---|
| Domains | Optimization, System_Administration |
| Last Updated | 2026-10-28 00:00 GMT |
Overview
Delete the temporary `temp_capture.pcap` file with `fs.unlink(...).catch(...)` after each live-capture tool finishes, so repeated invocations do not accumulate capture artifacts on disk.
Description
The temp file cleanup heuristic ensures the intermediate PCAP file (`temp_capture.pcap`) produced by every live-capture tool is removed once analysis is complete. Because tshark writes the capture to a fixed filename in the working directory, an unbounded series of captures would otherwise leave stale files. The cleanup uses `await fs.unlink(tempPcap).catch(err => console.error(...))` — the `.catch` makes cleanup non-fatal: if the file is already gone or deletion fails, the tool still returns its result rather than erroring. Cleanup runs in the success path (after analysis, before the return) and is additionally covered by the tool-level try/catch for the error path.
Usage
Use this heuristic for any tool that writes an intermediate file to a fixed path during processing. It applies to the four live-capture tools: `capture_packets`, `get_summary_stats`, `get_conversations`, and `check_threats` — all of which write then read `temp_capture.pcap`.
The Insight (Rule of Thumb)
- Action: After reading the temp file back for analysis, call `await fs.unlink(tempPcap).catch(err => console.error(...))` before returning the tool response.
- Value: Fixed filename `temp_capture.pcap` in the process working directory; cleanup via `fs.promises.unlink`.
- Trade-off: A fixed filename means concurrent invocations would clobber each other's capture — the tool is implicitly single-flight. Gains: no disk accumulation, no manual cleanup needed.
- Non-fatal cleanup: Always `.catch` the unlink so a deletion failure cannot turn a successful analysis into an error response.
- Order: Clean up in the success path; rely on the tool's outer try/catch for the error path (the file may persist on an uncaught error — acceptable for a small temp file).
Reasoning
tshark's `-w temp_capture.pcap` writes to a fixed relative path. Without cleanup, each capture leaves a file behind, and since the filename is reused, a stale file could be misread as the current capture on a failed re-run. Deleting after each use keeps the working directory clean and avoids confusing a stale capture with a fresh one. Making the unlink non-fatal (`.catch`) is important because the tool's primary job is to return analysis results — a cleanup failure should not mask a successful capture. The fixed-filename trade-off (no concurrency) is acceptable for an MCP tool invoked serially by an LLM client.
Code Evidence
Cleanup in `capture_packets` from `index.js:84`:
await fs.unlink(tempPcap).catch(err => console.error(`Failed to delete ${tempPcap}: ${err.message}`));
Identical cleanup in `get_summary_stats` from `index.js:125`:
await fs.unlink(tempPcap).catch(err => console.error(`Failed to delete ${tempPcap}: ${err.message}`));
Identical cleanup in `get_conversations` (L166) and `check_threats` (L237).
The fixed temp filename declared at the start of each live-capture handler, e.g. `index.js:59`:
const tempPcap = 'temp_capture.pcap';