UNPKG

@yeepay/awesome-components-mcp

Version:

MCP server providing access to awesome-components documentation and integration guides with dual-mode operation: direct fetch and GitLab MCP instruction generation

174 lines (165 loc) 6.62 kB
"use strict"; /** * Components Discovery MCP Tool * * This tool discovers and categorizes all available components from the main llms.txt file. * It supports multiple output formats: raw, parsed, and summary. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.componentsDiscoverySchema = void 0; exports.componentsDiscoveryTool = componentsDiscoveryTool; const zod_1 = require("zod"); const config_js_1 = require("../config.js"); const gitlabClient_js_1 = require("../services/gitlabClient.js"); /** * Schema for components discovery tool parameters */ exports.componentsDiscoverySchema = zod_1.z.object({}); /** * Parses llms.txt content and categorizes components */ function parseComponentsContent(content) { const lines = content.split('\n') .map(line => line.trim()) .filter(line => line && !line.startsWith('#') && !line.startsWith('//')); const components = lines.map(line => { const name = line.split('/').pop() || line; let type = 'other'; if (line.includes('/guide') || line.startsWith('guide/')) { type = 'guide'; } else if (line.includes('/docs') || line.includes('/documentation') || line.endsWith('.md')) { type = 'documentation'; } else if (line.includes('/tutorial') || line.startsWith('tutorial/')) { type = 'tutorial'; } else if (!line.includes('/') || line.split('/').length <= 2) { type = 'component'; } return { name, type, path: line }; }); const componentsByType = {}; components.forEach(comp => { componentsByType[comp.type] = (componentsByType[comp.type] || 0) + 1; }); return { totalComponents: components.length, componentsByType, components }; } /** * Formats content as JSON data */ function formatOutput(content, sourceUrl) { const source = sourceUrl || config_js_1.config.llmsTxtUrl || config_js_1.config.urls.mainLlms; const parsed = parseComponentsContent(content); // Always return JSON format return JSON.stringify({ source, totalComponents: parsed.totalComponents, componentsByType: parsed.componentsByType, components: parsed.components }, null, 2); } /** * Components Discovery MCP Tool Implementation */ async function componentsDiscoveryTool() { try { // Mode 1: Direct fetch mode (when LLMS_TXT_URL is configured) if (config_js_1.config.llmsTxtUrl) { console.log(`Fetching components list from: ${config_js_1.config.llmsTxtUrl}`); // Fetch content from the configured URL const content = await (0, gitlabClient_js_1.fetchGitLabFileContentSafe)(config_js_1.config.llmsTxtUrl); // Format the output as JSON const formattedOutput = formatOutput(content, config_js_1.config.llmsTxtUrl); return { content: [{ type: 'text', text: formattedOutput }] }; } // Mode 2: gitlab-mcp instruction mode (when GITLAB_PROJECT_ID and LLMS_TXT_FILE are configured) if (config_js_1.config.llmsTxtProjectId) { console.log(`Generating gitlab-mcp instruction for project: ${config_js_1.config.llmsTxtProjectId}, file: ${config_js_1.config.llmsTxtFile}`); // Generate JSON instruction for LLM to call gitlab-mcp const instruction = { action_type: 'mcp_call', tool_name: 'get_file_contents', parameters: { project_id: config_js_1.config.llmsTxtProjectId, // Already URL-encoded as per PRD file_path: config_js_1.config.llmsTxtFile, ref: 'main' // Default branch } }; const instructionOutput = `# Components Discovery - GitLab MCP Instruction To retrieve the components list, please call the gitlab-mcp server with the following instruction: \`\`\`json ${JSON.stringify(instruction, null, 2)} \`\`\` **Instructions for LLM:** 1. Use the above JSON instruction to call the gitlab-mcp server 2. Call the \`get_file_contents\` tool with the provided parameters 3. The response will contain the components list (llms.txt content) **Configuration:** - Project ID: \`${config_js_1.config.llmsTxtProjectId}\` - File Path: \`${config_js_1.config.llmsTxtFile}\` - Branch: \`main\``; return { content: [{ type: 'text', text: instructionOutput }] }; } // Configuration error: neither mode is properly configured throw new Error('Configuration error: Either LLMS_TXT_URL or both GITLAB_PROJECT_ID and LLMS_TXT_FILE must be configured'); } catch (error) { console.error('Components discovery failed:', error); let errorMessage = 'Unknown error occurred'; let statusInfo = ''; if (error instanceof gitlabClient_js_1.GitLabError) { errorMessage = `GitLab Error: ${error.message}`; if (error.statusCode) { statusInfo = `\n**Status Code:** ${error.statusCode}`; } if (error.url) { statusInfo += `\n**URL:** ${error.url}`; } } else if (error instanceof Error) { errorMessage = `Error: ${error.message}`; } else { errorMessage = `Error: ${String(error)}`; } const errorResponse = `# Error: Components Discovery Failed ${errorMessage}${statusInfo} **Suggestions:** - Check your network connection - Verify the GitLab URL is accessible: ${config_js_1.config.llmsTxtUrl || config_js_1.config.urls.mainLlms} - Ensure you have proper authentication if the repository is private - Check if the llms.txt file exists at the specified location **Configuration:** - Mode: ${config_js_1.config.llmsTxtUrl ? 'Direct fetch' : 'GitLab MCP instruction'} - LLMS_TXT_URL: ${config_js_1.config.llmsTxtUrl || 'Not configured'} - GITLAB_PROJECT_ID: ${config_js_1.config.llmsTxtProjectId || 'Not configured'} - LLMS_TXT_FILE: ${config_js_1.config.llmsTxtFile || 'Not configured'} - Authentication: ${config_js_1.config.auth.gitlabToken ? 'Configured' : 'Not configured'}`; return { content: [{ type: 'text', text: errorResponse }], isError: true }; } }