#!/usr/bin/env node
// =============================================================================
// vibn-agent-mcp
// -----------------------------------------------------------------------------
// Stdio MCP server exposing the vibn-agent-runner sub-agent orchestration API.
// This lets any MCP-speaking client (Goose, Claude Desktop, Cursor, etc.)
// spawn Coder / PM / Marketing jobs against the vibn-agent-runner HTTP service
// and poll their status.
//
// Config (env):
//   AGENT_RUNNER_URL   (default: http://localhost:3333) — URL of the runner
// =============================================================================

import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';

import * as api from '../tools/agent-api';
import type { AgentRunnerConfig } from '../tools/agent-api';

function loadConfig(): AgentRunnerConfig {
    const runnerUrl = process.env.AGENT_RUNNER_URL?.trim() || 'http://localhost:3333';
    return { runnerUrl };
}

const TOOL_DEFINITIONS = [
    {
        name: 'spawn_agent',
        description: 'Dispatch a sub-agent job to run in the background on the vibn-agent-runner. Returns a job ID.',
        inputSchema: {
            type: 'object' as const,
            properties: {
                agent: { type: 'string', description: '"Coder", "PM", or "Marketing"' },
                task: { type: 'string', description: 'Detailed task description for the agent' },
                repo: { type: 'string', description: 'Gitea repo in "owner/name" format' },
            },
            required: ['agent', 'task', 'repo'],
        },
    },
    {
        name: 'get_job_status',
        description: 'Check the status of a previously spawned agent job.',
        inputSchema: {
            type: 'object' as const,
            properties: {
                job_id: { type: 'string', description: 'Job ID returned by spawn_agent' },
            },
            required: ['job_id'],
        },
    },
];

async function dispatch(cfg: AgentRunnerConfig, name: string, args: Record<string, unknown>): Promise<unknown> {
    switch (name) {
        case 'spawn_agent':
            return api.spawnAgent(cfg, {
                agent: String(args.agent),
                task: String(args.task),
                repo: String(args.repo),
            });
        case 'get_job_status':
            return api.getJobStatus(cfg, String(args.job_id));
        default:
            throw new Error(`Unknown tool: ${name}`);
    }
}

function buildServer(cfg: AgentRunnerConfig): Server {
    const server = new Server(
        { name: 'vibn-agent-mcp', version: '0.1.0' },
        { capabilities: { tools: {} } },
    );

    server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: TOOL_DEFINITIONS }));

    server.setRequestHandler(CallToolRequestSchema, async (request) => {
        const name = request.params.name;
        const args = (request.params.arguments ?? {}) as Record<string, unknown>;
        try {
            const result = await dispatch(cfg, name, args);
            return { content: [{ type: 'text', text: typeof result === 'string' ? result : JSON.stringify(result, null, 2) }] };
        } catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            return { isError: true, content: [{ type: 'text', text: `Error: ${message}` }] };
        }
    });

    return server;
}

async function main(): Promise<void> {
    const cfg = loadConfig();
    const server = buildServer(cfg);
    const transport = new StdioServerTransport();
    await server.connect(transport);
    // eslint-disable-next-line no-console
    console.error(`[vibn-agent-mcp] ready — ${TOOL_DEFINITIONS.length} tools exposed (runner=${cfg.runnerUrl})`);
}

main().catch((err) => {
    // eslint-disable-next-line no-console
    console.error('[vibn-agent-mcp] fatal:', err);
    process.exit(1);
});
