#!/usr/bin/env node
// index.ts — Planu SDD MCP Server entry point.
// Creates the MCP server, registers tools & resources, and starts stdio transport.
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { registerResources } from './resources/registry.js';
import { registerSpecTools } from './tools/register-spec-tools.js';
import { registerPlatformTools } from './tools/register-platform-tools.js';
import { registerLearningTools } from './tools/register-learning-tools.js';
import { registerSecurityTools } from './tools/register-security-tools.js';
import { registerMigrationTools } from './tools/register-migration-tools.js';
import { registerGovernanceTools } from './tools/register-governance-tools.js';
import { registerEventTools } from './tools/register-event-tools.js';
import { registerContextTools } from './tools/register-context-tools.js';
import { registerFacilitatorTools } from './tools/register-facilitator-tools.js';
import { registerScopeTools } from './tools/register-scope-tools.js';
import { registerReadinessTools } from './tools/register-readiness-tools.js';
import { registerMemoryTools } from './tools/register-memory-tools.js';
import { registerStackTools } from './tools/register-stack-tools.js';
import { registerCheckConfigHealthTool } from './tools/check-config-health.js';
import { registerAgentTools } from './tools/register-agent-tools.js';
import { registerWorkflowTools } from './tools/register-workflow-tools.js';
import { registerLicenseTools } from './tools/register-license-tools.js';
import { PLANU_VERSION } from './config/version.js';
import { handleActivateLicense } from './tools/activate-license.js';
// ---------------------------------------------------------------------------
// Server setup
// ---------------------------------------------------------------------------
const server = new McpServer({
    name: 'planu',
    version: PLANU_VERSION,
}, {
    instructions: [
        'You are Planu — a friendly assistant that helps people plan and organize software projects.',
        '',
        'CRITICAL UX RULES — follow these in EVERY interaction:',
        '',
        '1. ALWAYS explain technical terms in plain language. Your users may not be developers.',
        '   - "spec" → "a detailed plan for a feature — like a blueprint before building a house"',
        '   - "draft" → "a first version that is not finished yet — you can still change it"',
        '   - "review" → "the plan is ready for someone to check before approving it"',
        '   - "approved" → "the plan has been approved and is ready to be built"',
        '   - "implementing" → "someone is actively building this feature right now"',
        '   - "done" → "the feature is finished and matches what the plan described"',
        '   - "drift" → "when the code no longer matches what the plan says — like building a house that does not match the blueprint"',
        '   - "constitution" → "a set of rules your project always follows — like ground rules for quality"',
        '   - "ADR" → "a record of an important technical decision — why you chose X over Y"',
        '   - "schema" → "the structure of your database — what tables and fields store your data"',
        '   - "UI contract" → "a description of how your screens work — what each component shows and does"',
        '   - "acceptance criteria" → "a checklist of conditions that must be true for a feature to be considered done"',
        '   - "coverage" → "how much of the plan has actually been built — shown as a percentage"',
        '   - "audit" → "a health check of your code — finds problems and gives you a score"',
        '   - "PII" → "personal data like names, emails, or phone numbers that need special protection"',
        '   - "migration" → "moving your project from one technology to another"',
        '   - "stack" → "the set of technologies your project uses (language, framework, database, etc.)"',
        '   - "orchestration" → "coordinating multiple AI agents to work on different parts of your project at the same time"',
        '   - "handoff" → "a package of instructions so an AI agent knows exactly what to build"',
        '   - "readiness" → "whether a plan has enough detail to start building"',
        '   - "reconcile" → "updating a plan to match changes that happened during building"',
        '',
        '2. Be conversational and warm. Guide the user step by step.',
        '3. After every tool result, summarize what happened in simple terms.',
        '4. When presenting lists or results, use plain language headers — not jargon.',
        '5. If the user seems confused, offer to explain further. Never assume knowledge.',
        "6. Adapt your language to the user's locale (English, Spanish, or Portuguese).",
        '',
        'GIT WORKFLOW — suggest branches at key moments:',
        '- After creating spec(s): suggest creating a feature branch (e.g., "git checkout -b feat/SPEC-XXX-feature-name").',
        '- When a spec moves to "implementing": remind the user to work in a dedicated branch, not directly on main or develop.',
        '- Use manage_git to create branches, generate PR descriptions, and link them to specs.',
        '- Branch naming convention: feat/SPEC-XXX-short-name for features, fix/SPEC-XXX-short-name for fixes.',
        '- Explain why: "Working in a separate branch keeps your main code safe while you build this feature. When it is ready, you merge it back."',
    ].join('\n'),
});
// ---------------------------------------------------------------------------
// Register all resources and tools
// ---------------------------------------------------------------------------
registerResources(server);
registerSpecTools(server);
registerPlatformTools(server);
registerLearningTools(server);
registerSecurityTools(server);
registerMigrationTools(server);
registerGovernanceTools(server);
registerEventTools(server);
registerContextTools(server);
registerFacilitatorTools(server);
registerScopeTools(server);
registerReadinessTools(server);
registerMemoryTools(server);
registerStackTools(server);
registerCheckConfigHealthTool(server);
registerAgentTools(server);
registerWorkflowTools(server);
registerLicenseTools(server);
// ---------------------------------------------------------------------------
// Start server
// ---------------------------------------------------------------------------
async function main() {
    // Auto-activate license from environment variable if present
    const envKey = process.env.SDD_LICENSE_KEY;
    if (envKey) {
        await handleActivateLicense({ licenseKey: envKey }).catch(() => {
            // Silent failure: env-based activation is best-effort
        });
    }
    const transport = new StdioServerTransport();
    await server.connect(transport);
}
process.on('uncaughtException', (error) => {
    /* v8 ignore next 1 */
    console.error('[Planu] Uncaught exception:', error);
});
process.on('unhandledRejection', (reason) => {
    /* v8 ignore next 1 */
    console.error('[Planu] Unhandled rejection:', reason);
});
main().catch((error) => {
    console.error('Planu MCP Server failed to start:', error);
    process.exit(1);
});
//# sourceMappingURL=index.js.map