Implementation:0xKoda WireMCP mcp tool response
| Knowledge Sources | |
|---|---|
| Domains | MCP, API_Design |
| Last Updated | 2026-10-28 00:00 GMT |
Overview
Concrete tool for wrapping tool output in the MCP content envelope and cleaning up temporary files provided by the `@modelcontextprotocol/sdk` `McpServer#tool()` callback return pattern, used across all seven WireMCP tools.
Description
The MCP tool response pattern returns `{ content: [{ type: 'text', text: <payload> }] }` from each `server.tool()` handler callback. On error, the return includes `isError: true`. For tools that create a temporary `temp_capture.pcap` file (live-capture tools), the pattern also calls `fs.unlink(tempPcap).catch(...)` to clean up regardless of success or failure. Output payloads exceeding 720,000 characters are trimmed by proportionally reducing the packet array.
Usage
Use this pattern as the return contract for every `server.tool()` handler. The MCP content envelope is mandatory — returning a bare string or object will not render correctly to the MCP client.
Code Reference
Source Location
- Repository: WireMCP
- File: index.js
- Lines: L74-91 (capture_packets), L125-132 (get_summary_stats), L166-173 (get_conversations), L232-241 (check_threats), L285-292 (check_ip_threats), L338-356 (analyze_pcap), L481-503 (extract_credentials)
Signature
// Success response
return {
content: [{ type: 'text', text: outputText }],
};
// Error response
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
// Temp file cleanup (live-capture tools)
await fs.unlink(tempPcap).catch(err =>
console.error(`Failed to delete ${tempPcap}: ${err.message}`)
);
// Output trimming (capture_packets uses slice, analyze_pcap uses splice)
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); // or: packets.splice(trimCount);
jsonString = JSON.stringify(packets);
}
Import
const fs = require('fs').promises;
const { McpServer } = require('@modelcontextprotocol/sdk/server/mcp.js');
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| outputText | string | Yes | The text payload to return to the MCP client |
| tempPcap | string | No | Temp file path to delete (live-capture tools only) |
| packets | Array | No | Packet array to trim (capture_packets, analyze_pcap) |
Outputs
| Name | Type | Description |
|---|---|---|
| (return) | Object | MCP content envelope `{ content, isError? }` |
Usage Examples
Standard Tool Response
server.tool('my_tool', 'description', { param: z.string() }, async (args) => {
try {
const result = doWork(args.param);
return {
content: [{ type: 'text', text: `Result: ${result}` }],
};
} catch (error) {
return {
content: [{ type: 'text', text: `Error: ${error.message}` }],
isError: true,
};
}
});
Response with Temp File Cleanup
server.tool('capture', 'desc', { interface: z.string().optional().default('en0') }, async (args) => {
const tempPcap = 'temp_capture.pcap';
try {
await execAsync(`${tsharkPath} -i ${args.interface} -w ${tempPcap} -a duration:5`);
const { stdout } = await execAsync(`${tsharkPath} -r "${tempPcap}" -T json`);
const packets = JSON.parse(stdout);
// Clean up temp file before returning
await fs.unlink(tempPcap).catch(err =>
console.error(`Failed to delete ${tempPcap}: ${err.message}`)
);
return { content: [{ type: 'text', text: JSON.stringify(packets) }] };
} catch (error) {
await fs.unlink(tempPcap).catch(() => {});
return { content: [{ type: 'text', text: `Error: ${error.message}` }], isError: true };
}
});
Related Pages
Implements Principle
- Principle:0xKoda_WireMCP_MCP_Tool_Interface
- Principle:0xKoda_WireMCP_Output_Trimming
- Principle:0xKoda_WireMCP_MCP_Server_Lifecycle
Uses Heuristic
- Heuristic:0xKoda_WireMCP_Output_Size_Management
- Heuristic:0xKoda_WireMCP_Temp_File_Cleanup
- Heuristic:0xKoda_WireMCP_Channel_Separation