UNPKG

@kya-os/mcp-i

Version:

The TypeScript MCP framework with identity features built-in

169 lines (168 loc) 7.04 kB
"use strict"; /** * KTA (Know-That-AI) Registration Module * * Handles agent registration with the KTA registry. * Based on alpha implementation from @kya-os/mcp-i@0.1.0-alpha.3.9 */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerWithKTA = registerWithKTA; exports.registerWithKTACLI = registerWithKTACLI; const axios_1 = __importDefault(require("axios")); /** * MCP-I Capabilities (from contracts) */ const MCP_I_CAPABILITIES = [ "identity", "signatures", "well-known-endpoints", "did-resolution", "key-rotation", ]; /** * Register agent with Know-That-AI * * Makes an API call to the KTA registry to register the agent. * Returns registration details including claim URL. */ async function registerWithKTA(options) { const { did, publicKey, kid, name, description, repository, endpoint = "https://knowthat.ai", } = options; try { // Construct verification endpoint from DID const verificationEndpoint = did.includes("localhost") ? `http://localhost:3000/.well-known/did.json` : `${did.replace("did:web:", "https://").replace(":", "/")}/.well-known/did.json`; // Prepare registration payload const payload = { did, publicKey, kid, name, description, repository: repository || undefined, verificationEndpoint, capabilities: MCP_I_CAPABILITIES, }; // Make registration request const response = await axios_1.default.post(`${endpoint}/api/agents/auto-register`, payload, { headers: { "Content-Type": "application/json", "User-Agent": "@kya-os/mcp-i-cli", }, timeout: 30000, // 30 second timeout }); const data = response.data; // Extract agent ID from DID const agentId = did.split(":").pop() || `agent-${Date.now()}`; // Build result structure // Handle both camelCase (agentUrl, claimUrl) and uppercase (agentURL, claimURL) from API const result = { agentDID: did, agentURL: data.agentURL || data.agentUrl || `${endpoint}/agents/by-did/${encodeURIComponent(did)}`, agentId: data.agentId || agentId, agentSlug: data.agentSlug || data.agent?.slug || agentId, claimURL: data.claimURL || data.claimUrl || `${endpoint}/claim/${agentId}`, claimToken: data.claimToken, verificationEndpoint, conformanceCapabilities: MCP_I_CAPABILITIES, mirrorStatus: data.mirrorStatus || "pending", mirrorLink: data.mirrorLink || `${endpoint}/agents/pending`, }; return result; } catch (error) { // Handle different error types if (axios_1.default.isAxiosError(error)) { if (error.code === "ECONNREFUSED") { throw new Error(`Could not connect to KTA registry at ${endpoint}. ` + "Please check your internet connection or try again later."); } if (error.response?.status === 503) { throw new Error("KTA registry is temporarily unavailable (maintenance mode). " + "Please try again in a few minutes."); } if (error.response?.status === 429) { throw new Error("Rate limit exceeded. Please wait a moment and try again."); } if (error.response?.data?.message) { throw new Error(`KTA registration failed: ${error.response.data.message}`); } } throw new Error(`Failed to register with KTA: ${error.message}`); } } /** * Register agent with Know-That-AI using fast CLI endpoint * * Uses the /cli-register endpoint which returns immediately without polling. * Returns claimUrl and claimToken for browser-based claiming. */ async function registerWithKTACLI(options) { const { did, publicKey, kid, name, description, repository, endpoint = "https://knowthat.ai", } = options; try { // Construct verification endpoint from DID const verificationEndpoint = did.includes("localhost") ? `http://localhost:3000/.well-known/did.json` : `${did.replace("did:web:", "https://").replace(":", "/")}/.well-known/did.json`; // Prepare registration payload for CLI endpoint const payload = { name, description, githubRepo: repository, publicKey, kid, did, verificationEndpoint, capabilities: MCP_I_CAPABILITIES, }; // Make registration request to CLI endpoint const response = await axios_1.default.post(`${endpoint}/api/agents/cli-register`, payload, { headers: { "Content-Type": "application/json", "User-Agent": "@kya-os/mcp-i-cli", }, timeout: 30000, // 30 second timeout }); const data = response.data; // Extract agent ID and slug from response const agentId = data.agent?.id || did.split(":").pop() || `agent-${Date.now()}`; const agentSlug = data.agent?.slug || agentId; // Build result structure from CLI registration response const result = { agentDID: data.did || did, agentURL: data.agent?.url || `${endpoint}/agents/${agentSlug}`, agentId, agentSlug, claimURL: data.claimUrl || `${endpoint}/agents/claim?did=${encodeURIComponent(did)}`, claimToken: data.claimToken, verificationEndpoint, conformanceCapabilities: MCP_I_CAPABILITIES, mirrorStatus: "pending", // CLI register doesn't poll for mirror status mirrorLink: `${endpoint}/agents/pending`, }; return result; } catch (error) { // Handle different error types if (axios_1.default.isAxiosError(error)) { if (error.code === "ECONNREFUSED") { throw new Error(`Could not connect to KTA registry at ${endpoint}. ` + "Please check your internet connection or try again later."); } if (error.response?.status === 503) { throw new Error("KTA registry is temporarily unavailable (maintenance mode). " + "Please try again in a few minutes."); } if (error.response?.status === 429) { throw new Error("Rate limit exceeded. Please wait a moment and try again."); } if (error.response?.data?.message) { throw new Error(`KTA registration failed: ${error.response.data.message}`); } } throw new Error(`Failed to register with KTA: ${error.message}`); } }