"use strict";
// MCP Server uses Stdio, so we avoid global hooks that might write to stdout.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
const zod_1 = require("zod");
const http_1 = __importDefault(require("http"));
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
let JWT_SECRET = process.env.INTERNAL_AUTH_TOKEN;
if (!JWT_SECRET) {
    try {
        const tokenPath = path_1.default.join(process.env.HOME || process.env.USERPROFILE || '', '.nyxora', 'runtime.token');
        JWT_SECRET = fs_1.default.readFileSync(tokenPath, 'utf8').trim();
    }
    catch (e) {
        console.error("Missing INTERNAL_AUTH_TOKEN in mcp-server process and could not read ~/.nyxora/runtime.token");
        process.exit(1);
    }
}
const server = new mcp_js_1.McpServer({
    name: "Nyxora MCP Server",
    version: "1.6.5"
});
// Helper to make internal requests to Policy Engine
function callPolicyEngine(path, method, payload) {
    return new Promise((resolve, reject) => {
        const dataString = payload ? JSON.stringify(payload) : '';
        const options = {
            hostname: '127.0.0.1',
            port: 3001,
            path: path,
            method: method,
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${jsonwebtoken_1.default.sign({ service: 'mcp-server' }, JWT_SECRET, { expiresIn: '1m' })}`,
                'Content-Length': Buffer.byteLength(dataString)
            }
        };
        const req = http_1.default.request(options, (res) => {
            let data = '';
            res.on('data', chunk => data += chunk);
            res.on('end', () => {
                try {
                    const parsed = JSON.parse(data);
                    if (res.statusCode && res.statusCode >= 400) {
                        reject(new Error(`Policy Engine Rejected (${res.statusCode}): ${parsed.error || parsed.message || data}`));
                    }
                    else {
                        resolve(parsed);
                    }
                }
                catch (e) {
                    reject(new Error(`Failed to parse Policy Engine response: ${data}`));
                }
            });
        });
        req.on('error', (e) => reject(new Error(`Policy Engine Connection Error: ${e.message}`)));
        if (dataString) {
            req.write(dataString);
        }
        req.end();
    });
}
// Tool: get_wallet_address
server.tool("get_wallet_address", "Fetch the secure Ethereum wallet address managed by the Nyxora Signer Vault. Use this to know the agent's identity.", {}, async () => {
    try {
        const response = await callPolicyEngine('/address', 'GET');
        return {
            content: [{ type: "text", text: `Wallet Address: ${response.address}` }]
        };
    }
    catch (e) {
        return {
            isError: true,
            content: [{ type: "text", text: e.message }]
        };
    }
});
// Tool: request_transaction
server.tool("request_transaction", "Request an EVM transaction (transfer, swap, bridge). This will be evaluated by the Policy Engine. If approved, it returns a pending Transaction ID.", {
    type: zod_1.z.enum(['transfer', 'swap', 'bridge', 'mint', 'custom']).describe("The type of transaction"),
    chainName: zod_1.z.string().describe("The target blockchain network (e.g. 'ethereum', 'base', 'arbitrum')"),
    details: zod_1.z.any().describe("Detailed payload. For 'transfer': { to: string, amountWei: string }. For 'swap': { tokenIn: string, tokenOut: string, amountInWei: string }.")
}, async (args) => {
    try {
        const response = await callPolicyEngine('/request-tx', 'POST', args);
        return {
            content: [{ type: "text", text: `Transaction requested successfully. Transaction ID: ${response.id}. Status: ${response.status}` }]
        };
    }
    catch (e) {
        return {
            isError: true,
            content: [{ type: "text", text: `Transaction Request Failed! ${e.message}` }]
        };
    }
});
async function main() {
    const transport = new stdio_js_1.StdioServerTransport();
    await server.connect(transport);
    console.error("Nyxora MCP Server running on stdio"); // stdio logs use stderr so stdout is purely for MCP JSON-RPC
}
main().catch((error) => {
    console.error("Fatal error in MCP Server:", error);
    process.exit(1);
});
