// {{ display_name }} MCP server — generated by mcpify. Do not hand-edit.
//
// Wraps a tool's core logic with consistent MCP response formatting and
// error handling, so search/get/call each only implement their own business
// logic, not the MCP content-envelope boilerplate.

import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';

import { logger } from '../core/logger.js';

export async function executeTool(toolName: string, fn: () => Promise<unknown>): Promise<CallToolResult> {
  try {
    const result = await fn();
    return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
  } catch (error) {
    const message = error instanceof Error ? error.message : String(error);
    logger.error({ tool: toolName, err: message }, 'tool execution failed');
    return { content: [{ type: 'text', text: message }], isError: true };
  }
}
