import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { type ApiParams, getClient } from './client.js';
import { createToolError } from './errors.js';
import { normalizeResponse } from './normalize.js';
import { compositeTools } from './schemas/index.js';
import {
registerCheckDomainTool,
registerGenerateIdeasTool,
registerHelpTool,
} from './tools/index.js';
export function registerAllTools(server: McpServer): void {
for (const tool of compositeTools) {
// Build action enum from keys
const actionKeys = Object.keys(tool.actions) as [string, ...string[]];
// Build combined input schema with action + all possible params
const actionDescriptions = actionKeys
.map((k) => {
const actionDef = tool.actions[k];
return actionDef ? `${k}: ${actionDef.description}` : k;
})
.join(' | ');
const inputSchema: Record<string, z.ZodTypeAny> = {
action: z.enum(actionKeys).describe(`Action to perform: ${actionDescriptions}`),
};
// Collect all unique params across actions
for (const action of Object.values(tool.actions)) {
if (action?.params) {
const shape = action.params.shape;
for (const [key, schema] of Object.entries(shape)) {
if (!inputSchema[key]) {
// Make optional since not all actions need all params
inputSchema[key] = (schema as z.ZodTypeAny).optional();
}
}
}
}
server.registerTool(
tool.name,
{
description: tool.description,
inputSchema,
},
async (input) => {
const action = input.action as string;
const actionDef = tool.actions[action];
if (!actionDef) {
const error = createToolError(`Unknown action: ${action}`, {
type: 'UNKNOWN_ACTION',
action,
validActions: actionKeys,
tool: tool.name,
});
return {
content: [{ type: 'text', text: error.toJSON() }],
isError: true,
};
}
const client = getClient();
const params = actionDef.transform
? actionDef.transform(action, input as Record<string, unknown>)
: (input as ApiParams);
// Remove 'action' from params sent to API
delete params.action;
const result = await client.execute(actionDef.command, params);
const normalized = normalizeResponse(actionDef.command, result);
return {
content: [{ type: 'text', text: JSON.stringify(normalized, null, 2) }],
};
},
);
}
// Register standalone tools
registerCheckDomainTool(server);
registerGenerateIdeasTool(server);
registerHelpTool(server);
}