import { createMcpSdk } from '@metorial/mcp-sdk-utils';
import { jsonSchema, tool } from 'ai';

export let metorialAiSdk = createMcpSdk(async ({ tools }) => {
  let toolMap: Record<string, any> = {};

  for (let t of tools.getTools()) {
    let parameters = t.getParametersAs('json-schema') as any;

    if (parameters && typeof parameters === 'object' && !parameters.type) {
      parameters.type = 'object';
    }

    if (!parameters) {
      parameters = { type: 'object', properties: {} };
    }

    toolMap[t.id] = tool({
      description: t.description ?? undefined,
      inputSchema: jsonSchema(parameters),
      execute: async (params: any) => {
        return await t.call(params);
      }
    });
  }

  return { tools: () => toolMap };
});
