( fn: () => Promise<T> )
| 86 | * Wrap a tool handler to catch errors and return them as MCP error content. |
| 87 | */ |
| 88 | export async function withErrorHandling<T>( |
| 89 | fn: () => Promise<T> |
| 90 | ): Promise<{ content: [{ type: 'text'; text: string }]; isError?: boolean }> { |
| 91 | try { |
| 92 | const result = await fn(); |
| 93 | return toText(result); |
| 94 | } catch (err) { |
| 95 | const message = err instanceof Error ? err.message : String(err); |
| 96 | logger.error({ err }, `MCP tool error: ${message}`); |
| 97 | return { |
| 98 | content: [{ type: 'text' as const, text: `Error: ${message}` }], |
| 99 | isError: true, |
| 100 | }; |
| 101 | } |
| 102 | } |
no test coverage detected