@kya-os/mcp-i
Version:
COMING SOON:Production-ready MCP Identity with automatic registration, key rotation, and optimized performance
174 lines (173 loc) • 6.75 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KnowThatRegistry = void 0;
const transport_1 = require("../transport");
const polling_1 = require("../polling");
const logger_1 = require("../logger");
const platform_info_1 = require("../platform-info");
class KnowThatRegistry {
constructor(endpoint = 'https://knowthat.ai', transport, options) {
this.name = 'knowthat';
this.type = 'primary';
this.endpoint = endpoint;
this.transport = transport || transport_1.TransportFactory.create();
this.options = options;
}
async publishViaCLI(data) {
const logger = (0, logger_1.getLogger)();
try {
logger.debug('Using fast CLI registration endpoint');
const response = await this.transport.post(`${this.endpoint}/api/agents/cli-register`, {
name: data.name,
description: data.description,
repository: data.repository,
publicKey: data.publicKey
}, {
timeout: 5000,
headers: {
'Content-Type': 'application/json',
'User-Agent': `-os/mcp-i/${(0, platform_info_1.generateClientInfo)().sdkVersion}`
}
});
logger.debug(`CLI registration completed in ${response.data.responseTime}ms`);
if (response.data.keys && !data.publicKey) {
logger.warn(response.data.keys.warning || 'Server generated keys - store them securely!');
}
return {
success: true,
registryAgentId: response.data.agent.id,
profileUrl: response.data.agent.url,
claimUrl: response.data.claimUrl,
claimToken: response.data.claimToken,
generatedKeys: response.data.keys
};
}
catch (error) {
logger.debug(`CLI registration failed: ${error.message}`);
throw error;
}
}
shouldUseCLIEndpoint() {
if (this.options?.registryEndpoint === 'cli')
return true;
if (this.options?.registryEndpoint === 'auto-register')
return false;
if (this.options?.registryEndpoint === 'auto' || !this.options?.registryEndpoint) {
if (this.options?.onProgress)
return true;
if (this.options?.processingMode === 'sync')
return true;
if (process.stdout?.isTTY)
return true;
}
return false;
}
async publish(data) {
const logger = (0, logger_1.getLogger)();
const useCLI = this.shouldUseCLIEndpoint();
if (useCLI) {
try {
return await this.publishViaCLI(data);
}
catch (error) {
logger.debug('CLI endpoint failed, falling back to auto-register');
}
}
return await this.publishViaAutoRegister(data);
}
async publishViaAutoRegister(data) {
const logger = (0, logger_1.getLogger)();
try {
const clientInfo = (0, platform_info_1.generateClientInfo)({
processingMode: this.options?.processingMode === 'auto'
? undefined
: this.options?.processingMode || 'sync',
customMetadata: {
source: 'mcp-i-sdk'
}
});
logger.debug(`Generated client info: ${JSON.stringify(clientInfo)}`);
const response = await this.transport.post(`${this.endpoint}/api/agents/auto-register`, {
metadata: {
name: data.name,
description: data.description,
repository: data.repository,
version: '1.0.0'
},
clientInfo,
publicKey: data.publicKey || undefined
}, {
timeout: 30000,
headers: {
'Content-Type': 'application/json',
'User-Agent': `-os/mcp-i/${clientInfo.sdkVersion}`
}
});
logger.debug(`Registration response status: ${response.status}`);
logger.debug(`Registration response data: ${JSON.stringify(response.data, null, 2)}`);
if (response.status === 202 && (0, polling_1.isAsyncRegistrationResponse)(response.data)) {
logger.debug('Got async registration response, polling for completion...');
const result = await (0, polling_1.pollRegistrationStatus)(response.data.status, this.transport, {
pollInterval: 2000,
maxPollingTime: 60000,
logger,
onProgress: (message) => {
logger.debug(message);
}
});
return {
success: true,
registryAgentId: result.agent.id,
profileUrl: result.agent.url
};
}
const syncResponse = response.data;
return {
success: true,
registryAgentId: syncResponse.agent.id,
profileUrl: syncResponse.agent.url
};
}
catch (error) {
return {
success: false,
error: error.message || 'Failed to register with KnowThat.ai'
};
}
}
async verify(did) {
try {
const agentSlug = this.extractAgentSlug(did);
const response = await this.transport.get(`${this.endpoint}/api/agents/${agentSlug}/verify`, { timeout: 5000 });
return response.data.valid === true;
}
catch {
return false;
}
}
async getStatus(did) {
try {
const agentSlug = this.extractAgentSlug(did);
const response = await this.transport.get(`${this.endpoint}/api/agents/${agentSlug}/status`, { timeout: 5000 });
return {
name: this.name,
status: response.data.verified ? 'active' : 'pending',
registeredAt: response.data.registeredAt,
type: 'primary'
};
}
catch (error) {
return {
name: this.name,
status: 'failed',
type: 'primary',
error: 'Failed to get status'
};
}
}
extractAgentSlug(did) {
const parts = did.split(':');
return parts[parts.length - 1];
}
}
exports.KnowThatRegistry = KnowThatRegistry;