#!/usr/bin/env node
/**
 * stdio entry point — how a local agent connects.
 *
 *   claude mcp add pig -- npx -y @pig/mcp
 *
 * Configuration comes from the environment because stdio servers have no other
 * channel: PIG_URL and PIG_API_KEY. The key is minted in PIG under Settings →
 * API keys, and should carry the narrowest scope that does the job.
 */
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { createPigMcpServer } from './server';

const baseUrl = process.env.PIG_URL ?? 'http://localhost:8920';
const apiKey = process.env.PIG_API_KEY;

if (!apiKey) {
  // stdout is the protocol channel — diagnostics must go to stderr or they
  // corrupt the stream and the client reports an unhelpful parse error.
  process.stderr.write(
    'PIG_API_KEY is not set.\n' +
      'Create a key in PIG under Settings → API keys, then:\n' +
      '  export PIG_API_KEY=pig_...\n' +
      '  export PIG_URL=https://your-pig-host\n',
  );
  process.exit(1);
}

const server = createPigMcpServer({ baseUrl, apiKey });
await server.connect(new StdioServerTransport());
