csvlod-ai-mcp-server
Version:
CSVLOD-AI MCP Server v3.0 with Quantum Context Intelligence - Revolutionary Context Intelligence Engine and Multimodal Processor for sovereign AI development
234 lines • 8.91 kB
JavaScript
import * as fs from 'fs/promises';
import * as path from 'path';
import * as os from 'os';
export class LocalMCPRegistry {
constructor() {
// Local-first: Store registry in user's home directory
this.registryPath = path.join(os.homedir(), '.csvlod-ai', 'mcp-registry');
this.cachePath = path.join(os.homedir(), '.csvlod-ai', 'mcp-cache');
}
async execute(action, options) {
switch (action) {
case 'sync':
return this.syncRegistry(options?.localOnly);
case 'search':
return this.searchServers(options?.query || '');
case 'install':
return this.installServer(options?.query || '');
case 'list':
return this.listServers();
default:
return {
success: false,
message: `Unknown action: ${action}`,
};
}
}
async orchestrate(task, options) {
// Analyze task to determine required capabilities
const requiredCapabilities = this.analyzeTask(task);
// Find matching servers
const servers = await this.findMatchingServers(requiredCapabilities, options);
// Create execution plan
const executionOrder = this.determineExecutionOrder(servers, task);
return {
task,
servers,
executionOrder,
isLocal: options?.preferLocal ?? true,
estimatedTime: this.estimateExecutionTime(servers),
};
}
async syncRegistry(localOnly) {
try {
await fs.mkdir(this.registryPath, { recursive: true });
await fs.mkdir(this.cachePath, { recursive: true });
if (localOnly) {
return {
success: true,
message: 'Using local registry only (sovereignty mode)',
};
}
// In real implementation, would sync with MCP registry
// For now, create a mock local registry
const mockRegistry = {
servers: [
{
name: 'github-mcp',
type: 'npm',
version: '0.1.0',
capabilities: ['code_search', 'pr_management', 'issue_tracking'],
},
{
name: 'filesystem-mcp',
type: 'npm',
version: '0.2.0',
capabilities: ['file_read', 'file_write', 'directory_management'],
},
{
name: 'docker-mcp',
type: 'docker',
version: '1.0.0',
capabilities: ['container_management', 'image_building'],
},
],
lastSync: new Date().toISOString(),
};
await fs.writeFile(path.join(this.registryPath, 'registry.json'), JSON.stringify(mockRegistry, null, 2));
return {
success: true,
message: `Synced ${mockRegistry.servers.length} servers to local registry`,
data: mockRegistry,
};
}
catch (error) {
return {
success: false,
message: `Failed to sync registry: ${error}`,
};
}
}
async searchServers(query) {
try {
const registryFile = path.join(this.registryPath, 'registry.json');
const registryData = await fs.readFile(registryFile, 'utf-8');
const registry = JSON.parse(registryData);
const matches = registry.servers.filter((server) => server.name.includes(query) ||
server.capabilities.some((cap) => cap.includes(query)));
return {
success: true,
message: `Found ${matches.length} servers matching "${query}"`,
data: matches,
};
}
catch (error) {
return {
success: false,
message: 'Local registry not found. Run sync first.',
};
}
}
async installServer(serverName) {
try {
// Check if server exists in registry
const searchResult = await this.searchServers(serverName);
if (!searchResult.success || !searchResult.data?.length) {
return {
success: false,
message: `Server "${serverName}" not found in registry`,
};
}
const server = searchResult.data[0];
const installPath = path.join(this.cachePath, server.name);
// Create local installation record
await fs.mkdir(installPath, { recursive: true });
await fs.writeFile(path.join(installPath, 'server.json'), JSON.stringify(server, null, 2));
// In real implementation, would download and cache the server
// For now, just record the installation
return {
success: true,
message: `Installed ${serverName} locally (${server.type})`,
data: { installPath, server },
};
}
catch (error) {
return {
success: false,
message: `Failed to install server: ${error}`,
};
}
}
async listServers() {
try {
const installedServers = await fs.readdir(this.cachePath);
const servers = [];
for (const serverDir of installedServers) {
try {
const serverFile = path.join(this.cachePath, serverDir, 'server.json');
const serverData = await fs.readFile(serverFile, 'utf-8');
servers.push(JSON.parse(serverData));
}
catch {
// Skip invalid entries
}
}
return {
success: true,
message: `${servers.length} servers installed locally`,
data: servers,
};
}
catch (error) {
return {
success: false,
message: 'No local servers found',
};
}
}
analyzeTask(task) {
// Simple capability analysis based on task keywords
const capabilities = [];
if (task.includes('file') || task.includes('read') || task.includes('write')) {
capabilities.push('file_read', 'file_write');
}
if (task.includes('code') || task.includes('search')) {
capabilities.push('code_search');
}
if (task.includes('github') || task.includes('pr') || task.includes('issue')) {
capabilities.push('pr_management', 'issue_tracking');
}
if (task.includes('docker') || task.includes('container')) {
capabilities.push('container_management');
}
return capabilities;
}
async findMatchingServers(capabilities, options) {
const searchResults = await this.listServers();
const servers = [];
if (searchResults.success && searchResults.data) {
for (const server of searchResults.data) {
const hasCapability = capabilities.some(cap => server.capabilities.includes(cap));
if (hasCapability) {
servers.push({
name: server.name,
version: server.version,
type: server.type,
capabilities: server.capabilities,
isEphemeral: server.type === 'npm' && (options?.ephemeral ?? false),
});
}
}
}
return servers;
}
determineExecutionOrder(servers, task) {
// Simple ordering: filesystem first, then others
const order = servers
.sort((a, b) => {
if (a.name.includes('filesystem'))
return -1;
if (b.name.includes('filesystem'))
return 1;
return 0;
})
.map(s => s.name);
return order;
}
estimateExecutionTime(servers) {
// Simple estimation based on server count and types
let timeMs = 0;
for (const server of servers) {
if (server.isEphemeral) {
timeMs += 500; // Fast ephemeral startup
}
else if (server.type === 'docker') {
timeMs += 2000; // Docker startup time
}
else {
timeMs += 1000; // Regular startup
}
}
return `${timeMs}ms`;
}
}
//# sourceMappingURL=registry.js.map