/**
* API endpoint to get MCP documentation index information
*/
import fs from 'fs';
import path from 'path';
export default async function handler(req, res) {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
// Read metadata file
const metadataPath = path.join(
process.cwd(),
'mcp-docs-server',
'data',
'chunks',
'metadata.json'
);
// Check if file exists
if (!fs.existsSync(metadataPath)) {
return res.status(404).json({
error: 'Index not found. Please run the indexer first.',
hint: 'Run: cd mcp-docs-server/scripts && python3 indexer.py moca_network_docs.json'
});
}
const metadata = JSON.parse(fs.readFileSync(metadataPath, 'utf-8'));
// Format response - support both single and multi-source formats
// Transform sources to camelCase
const formattedSources = (metadata.sources || []).map(source => ({
name: source.name,
url: source.url,
pages: source.pages || source.total_files,
chunks: source.chunks || source.total_chunks,
words: source.words || source.total_lines,
indexedAt: source.indexed_at,
file: source.file,
type: source.type || 'documentation', // 'documentation' or 'repository'
repoPath: source.repo_path // For repositories
}));
// Calculate totals from sources (handle both docs and repos)
const totalPagesFromSources = metadata.sources?.reduce((sum, s) => {
return sum + (s.pages || s.total_files || 0);
}, 0) || 0;
const totalChunksFromSources = metadata.sources?.reduce((sum, s) => {
return sum + (s.chunks || s.total_chunks || 0);
}, 0) || 0;
const totalWordsFromSources = metadata.sources?.reduce((sum, s) => {
return sum + (s.words || s.total_lines || 0);
}, 0) || 0;
const response = {
// Multi-source format
sources: formattedSources,
totalSources: metadata.sources ? metadata.sources.length : 1,
// Single source backward compatibility
source: metadata.source || (metadata.sources && metadata.sources.length > 0 ? metadata.sources[0].url : 'Unknown'),
// Totals (calculate from sources to include both docs and repos)
totalPages: totalPagesFromSources,
totalChunks: totalChunksFromSources,
totalWords: totalWordsFromSources,
// Metadata
indexedAt: metadata.indexed_at || metadata.last_updated,
embeddingModel: metadata.embedding_model,
chunkSize: metadata.chunk_size,
chunkOverlap: metadata.chunk_overlap,
};
res.status(200).json(response);
} catch (error) {
console.error('Error loading index info:', error);
res.status(500).json({
error: 'Failed to load index information',
details: error.message
});
}
}