MCPcopy Create free account
hub / github.com/Mnexa-AI/e2a / runTool

Function runTool

mcp/src/tools/util.ts:38–73  ·  view source on GitHub ↗
(fn: () => Promise<T>)

Source from the content-addressed store, hash-verified

36} as const;
37
38export async function runTool<T>(fn: () => Promise<T>): Promise<ToolResult> {
39 try {
40 const result = await fn();
41 const text =
42 result === undefined
43 ? "OK"
44 : typeof result === "string"
45 ? result
46 : JSON.stringify(result, null, 2);
47 return { content: [{ type: "text", text }] };
48 } catch (err) {
49 // Surface the API envelope's machine-branchable `code` (§6a #4) so an agent
50 // can branch on a stable code (e.g. domain_not_verified, message_not_pending,
51 // recipient_suppressed) instead of parsing prose. The retryable hint tells
52 // it whether a retry could ever help. Errors thrown by the wrapper itself
53 // (plain Error — e.g. "email is required") have no code and fall through to
54 // the prose form.
55 if (err instanceof E2AError && err.code) {
56 const retry = err.retryable ? " (retryable)" : "";
57 // Only the `code` (a trusted snake_case token) goes inside the brackets.
58 // The message is free-form and can echo caller/recipient input, so bound +
59 // sanitize it: strip control chars/newlines (keep the `[code]` convention
60 // parseable) and cap length (avoid context blowup). We surface code +
61 // message only — never the raw response body, headers, or request_id.
62 return {
63 content: [{ type: "text", text: `e2a error [${err.code}]${retry}: ${sanitizeMessage(err.message)}` }],
64 isError: true,
65 };
66 }
67 const message = err instanceof Error ? err.message : String(err);
68 return {
69 content: [{ type: "text", text: `e2a error: ${sanitizeMessage(message)}` }],
70 isError: true,
71 };
72 }
73}
74
75// sanitizeMessage makes an error message safe to splice into the single-line
76// `[code]: <message>` tool-error text: collapse control chars / newlines to

Callers 6

registerDomainToolsFunction · 0.85
registerAgentToolsFunction · 0.85
registerEventToolsFunction · 0.85
registerReviewToolsFunction · 0.85
registerWebhookToolsFunction · 0.85
registerMessageToolsFunction · 0.85

Calls 2

sanitizeMessageFunction · 0.85
stringifyMethod · 0.80

Tested by

no test coverage detected