vibe-coder-mcp
Version:
Production-ready MCP server with complete agent integration, multi-transport support, and comprehensive development automation tools for AI-assisted workflows.
57 lines (56 loc) • 2.92 kB
JavaScript
import { readFileSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { generateEmbedding } from '../../utils/embeddingHelper.js';
import logger from '../../logger.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export const toolEmbeddingStore = new Map();
export async function initializeToolEmbeddings() {
logger.info('Initializing tool embeddings...');
const configPath = path.resolve(__dirname, '../../../mcp-config.json');
let config;
try {
const configFile = readFileSync(configPath, 'utf-8');
config = JSON.parse(configFile);
if (!config || typeof config.tools !== 'object') {
throw new Error('Invalid mcp-config.json structure or missing tools object.');
}
logger.debug(`Loaded mcp-config.json from: ${configPath}`);
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
logger.error({ err: error, path: configPath }, 'Failed to load or parse mcp-config.json');
throw new Error(`Critical error: Could not load tool configuration from ${configPath}. ${errorMessage}`);
}
let processedCount = 0;
const toolEntries = Object.entries(config.tools);
for (const [toolName, toolData] of toolEntries) {
logger.debug(`Processing embeddings for tool: ${toolName}`);
try {
const description = typeof toolData.description === 'string' ? toolData.description : '';
const useCases = Array.isArray(toolData.use_cases) ? toolData.use_cases : [];
const descriptionEmbedding = await generateEmbedding(description);
const useCaseEmbeddingsPromises = useCases.map((uc) => generateEmbedding(uc));
const generatedUseCaseEmbeddings = await Promise.all(useCaseEmbeddingsPromises);
const successfulUseCaseEmbeddings = generatedUseCaseEmbeddings.filter(vec => vec.length > 0);
if (descriptionEmbedding.length > 0) {
toolEmbeddingStore.set(toolName, {
descriptionEmbedding,
useCaseEmbeddings: successfulUseCaseEmbeddings,
description: description,
useCases: useCases
});
logger.debug(`Stored embeddings for tool: ${toolName} (Use cases: ${successfulUseCaseEmbeddings.length}/${useCases.length})`);
processedCount++;
}
else {
logger.warn(`Skipping tool ${toolName} due to failed description embedding generation.`);
}
}
catch (error) {
logger.error({ err: error, tool: toolName }, `Error processing embeddings for tool ${toolName}`);
}
}
logger.info(`Tool embedding initialization complete. Processed ${processedCount}/${toolEntries.length} tools.`);
}