/**
 * MCP subpath — @vauban-org/agent-sdk/mcp
 *
 * Sprint-563: B5 — Convert a Vauban SkillRegistry (Record<string, Skill>)
 * into MCP-compatible tool definitions + call handler.
 *
 * Peer dependency: @modelcontextprotocol/sdk >= 1.5.0 (optional).
 */
/**
 * Convert a Vauban SkillRegistry into MCP-compatible tool list + handler.
 */
export function skillRegistryToMCPServer(registry, opts) {
    const tools = [];
    for (const [name, skill] of Object.entries(registry)) {
        const inputSchema = {
            type: "object",
            properties: {},
            additionalProperties: false,
        };
        // Try to extract JSON Schema from Zod if available
        if (skill.inputSchema && typeof skill.inputSchema === "object") {
            try {
                const zodSchema = skill.inputSchema;
                if (zodSchema.shape) {
                    inputSchema.properties = {};
                    for (const [key, def] of Object.entries(zodSchema.shape)) {
                        inputSchema.properties[key] = {
                            type: inferJsonSchemaType(def),
                            description: `Parameter: ${key}`,
                        };
                    }
                }
            }
            catch {
                // Keep defaults
            }
        }
        tools.push({
            name,
            description: skill.name,
            inputSchema,
        });
    }
    return {
        name: opts.name,
        version: opts.version,
        tools,
        async handleToolCall(toolName, args) {
            const skill = registry[toolName];
            if (!skill) {
                return {
                    content: [{ type: "text", text: `Unknown tool: ${toolName}` }],
                };
            }
            try {
                // Validate input if schema available
                let input = args;
                if (skill.inputSchema) {
                    try {
                        input = skill.inputSchema.parse(args);
                    }
                    catch (parseErr) {
                        return {
                            content: [{ type: "text", text: `Invalid arguments: ${String(parseErr)}` }],
                        };
                    }
                }
                const result = await skill.execute(input, {
                    isReplay: false,
                    dryRunMocks: {},
                    db: undefined,
                    logger: { info: () => { }, warn: () => { }, error: () => { }, debug: () => { } },
                });
                return {
                    content: [
                        {
                            type: "text",
                            text: typeof result === "string" ? result : JSON.stringify(result, null, 2),
                        },
                    ],
                };
            }
            catch (err) {
                return {
                    content: [
                        {
                            type: "text",
                            text: `Tool execution error: ${err instanceof Error ? err.message : String(err)}`,
                        },
                    ],
                };
            }
        },
    };
}
function inferJsonSchemaType(def) {
    if (!def || typeof def !== "object")
        return "string";
    const d = def;
    const innerDef = d._def;
    if (innerDef?.typeName) {
        switch (innerDef.typeName) {
            case "ZodString": return "string";
            case "ZodNumber": return "number";
            case "ZodBoolean": return "boolean";
            case "ZodArray": return "array";
            case "ZodObject": return "object";
            default: return "string";
        }
    }
    return "string";
}
//# sourceMappingURL=index.js.map