/**
 * Client-mode MCP server: lightweight proxy to the running primary server.
 * Zero local imports — fetches tool metadata via HTTP, proxies calls via HTTP.
 * Used when another terminal already owns the port.
 */
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { ListToolsRequestSchema, CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js';
export async function startMcpClientServer(port) {
    const baseUrl = `http://localhost:${port}`;
    // Fetch tool metadata from the primary server
    const res = await fetch(`${baseUrl}/api/mcp-tools`);
    if (!res.ok)
        throw new Error(`Failed to fetch tools from ${baseUrl}: ${res.status}`);
    const { tools } = await res.json();
    const server = new Server({ name: 'openwriter-client', version: '0.2.0' }, { capabilities: { tools: {} } });
    server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools }));
    server.setRequestHandler(CallToolRequestSchema, async (request) => {
        try {
            const callRes = await fetch(`${baseUrl}/api/mcp-call`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ tool: request.params.name, arguments: request.params.arguments }),
            });
            if (!callRes.ok) {
                const text = await callRes.text();
                return { content: [{ type: 'text', text: `Server error (${callRes.status}): ${text}` }] };
            }
            return await callRes.json();
        }
        catch (err) {
            return { content: [{ type: 'text', text: `Connection error: ${err.message}` }] };
        }
    });
    console.error(`[MCP-Client] Proxying ${tools.length} tools to ${baseUrl}`);
    const transport = new StdioServerTransport();
    await server.connect(transport);
}
