UNPKG

@kya-os/mcp-i

Version:

The TypeScript MCP framework with identity features built-in

95 lines (94 loc) 3.92 kB
"use strict"; /** * CLI Adapter for XMCP-I * * Provides a bridge between CLI commands and the runtime identity system. * This adapter recreates the functionality from alpha @kya-os/mcp-i@0.1.0-alpha.3.9 * using the new runtime architecture. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.enableMCPIdentityCLI = enableMCPIdentityCLI; const identity_1 = require("../runtime/identity"); const did_helpers_1 = require("@kya-os/mcp-i-core/utils/did-helpers"); const kta_registration_1 = require("./kta-registration"); /** * Enable MCP Identity for CLI * * This is the main entry point that CLI commands use to initialize identity. * It recreates the alpha `enableMCPIdentityCLI` function. */ async function enableMCPIdentityCLI(options = {}) { const { name, description, repository, endpoint = "https://knowthat.ai", onProgress, skipRegistration = false, } = options; let isNewIdentity = false; let capturedClaimUrl; // Helper to emit progress events const emitProgress = async (stage, message, data) => { if (onProgress) { try { await onProgress({ stage, message, data }); } catch (error) { // Continue even if progress callback throws console.warn("Warning: Progress callback threw an error:", error); } } }; // Step 1: Check for existing identity await emitProgress("checking_existing", "Checking for existing identity..."); const identityManager = new identity_1.IdentityManager({ environment: "development", devIdentityPath: ".mcpi/identity.json", }); // Check if identity file exists const fs = await import("fs"); const identityExists = fs.existsSync(".mcpi/identity.json"); isNewIdentity = !identityExists; // Step 2: Load or generate identity if (isNewIdentity) { await emitProgress("generating_keys", "Generating cryptographic keys..."); } const identity = await identityManager.ensureIdentity(); // Step 3: Register with KTA (if not skipped and is new identity) let registrationResult = null; if (!skipRegistration && isNewIdentity) { await emitProgress("registering", "Registering with Know-That-AI..."); try { // Use fast CLI registration endpoint for immediate response registrationResult = await (0, kta_registration_1.registerWithKTACLI)({ did: identity.did, publicKey: identity.publicKey, kid: identity.kid, name: name || "My Agent", description: description || "XMCP-I agent with identity features", repository, endpoint, }); capturedClaimUrl = registrationResult.claimURL; } catch (error) { console.warn("Warning: KTA registration failed:", error.message); console.warn("Continuing with local identity only"); } } // Step 4: Save (already done by IdentityManager, but emit event for CLI) await emitProgress("saving", "Saving identity..."); // Step 5: Complete await emitProgress("complete", "Identity setup complete!", { claimUrl: capturedClaimUrl, }); // Use agent metadata from registration result if available, otherwise extract from DID const agentId = registrationResult?.agentId || (0, did_helpers_1.extractAgentId)(identity.did); const agentSlug = registrationResult?.agentSlug || (0, did_helpers_1.extractAgentSlug)(identity.did); const registryUrl = registrationResult?.agentURL || `https://knowthat.ai/agents/${agentSlug}`; return { identity, metadata: { isNewIdentity, did: identity.did, claimUrl: capturedClaimUrl, registryUrl, agentId, agentSlug, }, }; }