Implementation:0xKoda WireMCP output trimming
| Knowledge Sources | |
|---|---|
| Domains | Data_Transformation, Output_Management |
| Last Updated | 2026-10-28 00:00 GMT |
Overview
Concrete tool for trimming large packet JSON outputs to fit within a 720,000-character limit, implemented as a pattern within the `capture_packets` and `analyze_pcap` tool handlers.
Description
The output trimming pattern caps the serialized JSON length of a packet array at `maxChars = 720000` characters. When the JSON exceeds this limit, the pattern computes a proportional reduction factor and removes packets from the tail: `capture_packets` uses `packets.slice(0, trimCount)` (non-mutating), while `analyze_pcap` uses `packets.splice(trimCount)` (mutating). Both then re-serialize the reduced array. This is a user-defined pattern (not a library API) that prevents MCP response payloads from exceeding practical size limits.
Usage
Use this pattern when returning large JSON payloads through the MCP content envelope and you need to bound the response size.
Code Reference
Source Location
- Repository: WireMCP
- File: index.js
- Lines: L74-82 (capture_packets, `slice`), L338-346 (analyze_pcap, `splice`)
Signature
const maxChars = 720000;
let jsonString = JSON.stringify(packets);
if (jsonString.length > maxChars) {
const trimFactor = maxChars / jsonString.length;
const trimCount = Math.floor(packets.length * trimFactor);
packets = packets.slice(0, trimCount); // capture_packets (non-mutating)
// OR: packets.splice(trimCount); // analyze_pcap (mutating)
jsonString = JSON.stringify(packets);
console.error(`Trimmed packets to ${trimCount} to fit ${maxChars} chars`);
}
Import
// No import needed — JSON.stringify, Array.prototype.slice/splice are built-in
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| packets | Array<Object> | Yes | Parsed packet array to trim |
| maxChars | number | No | Hardcoded to `720000` |
Outputs
| Name | Type | Description |
|---|---|---|
| jsonString | string | Re-serialized JSON of the trimmed packet array |
| packets | Array<Object> | The (possibly reduced) packet array |
Usage Examples
Trim with slice (capture_packets)
const maxChars = 720000;
let jsonString = JSON.stringify(packets);
if (jsonString.length > maxChars) {
const trimFactor = maxChars / jsonString.length;
const trimCount = Math.floor(packets.length * trimFactor);
packets = packets.slice(0, trimCount); // non-mutating
jsonString = JSON.stringify(packets);
console.error(`Trimmed to ${trimCount} packets to fit ${maxChars} chars`);
}
Trim with splice (analyze_pcap)
const maxChars = 720000;
let jsonString = JSON.stringify(packets);
if (jsonString.length > maxChars) {
const trimFactor = maxChars / jsonString.length;
const trimCount = Math.floor(packets.length * trimFactor);
packets.splice(trimCount); // mutating — removes elements after index trimCount
jsonString = JSON.stringify(packets);
console.error(`Trimmed to ${trimCount} packets to fit ${maxChars} chars`);
}