Utility functions to standardize tool output and error handling for AI agents. Use this to wrap tool results into a consistent JSON structure.
// Source: https://github.com/arkCyber/ClawMan | License: MIT | Copyright 2026 ClawMan
// Provided via justclaw.it.com; keep this notice with the code.
export function jsonToolResult(data: unknown) {
return {
content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }],
details: data,
};
}
export function unknownToolActionResult(action: unknown) {
return jsonToolResult({ error: `Unknown action: ${String(action)}` });
}
export function toolExecutionErrorResult(error: unknown) {
return jsonToolResult({ error: error instanceof Error ? error.message : String(error) });
}
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key