Skip to main content
Glama

CodeCompass MCP

basic-usage.js•6.19 kB
#!/usr/bin/env node /** * Basic usage example for CodeCompass MCP Server * * This example demonstrates how to use the CodeCompass MCP server * to analyze a GitHub repository and perform basic refactoring operations. */ import { MCPClient } from '@modelcontextprotocol/sdk/client/index.js'; import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'; import { spawn } from 'child_process'; async function main() { // Start the MCP server const serverProcess = spawn('node', ['../build/index.js'], { stdio: ['pipe', 'pipe', 'pipe'], }); // Create MCP client const transport = new StdioClientTransport({ reader: serverProcess.stdout, writer: serverProcess.stdin, }); const client = new MCPClient({ name: 'codecompass-example', version: '1.0.0', }); try { await client.connect(transport); console.log('šŸš€ Connected to CodeCompass MCP Server'); console.log('šŸ“Š Starting repository analysis...\n'); // Example 1: Analyze a popular React repository console.log('=== Example 1: Repository Analysis ==='); const repoUrl = 'https://github.com/facebook/react'; const analysis = await client.request({ method: 'tools/call', params: { name: 'analyze_repository', arguments: { url: repoUrl, options: { includeTests: true, includeDocs: true, maxFiles: 20, // Limit for demo }, }, }, }); console.log('āœ… Analysis complete:'); console.log(`- Repository: ${JSON.parse(analysis.content[0].text).info.name}`); console.log(`- Language: ${JSON.parse(analysis.content[0].text).info.language}`); console.log(`- Files: ${JSON.parse(analysis.content[0].text).info.fileCount}`); console.log(`- Dependencies: ${JSON.parse(analysis.content[0].text).dependencies.length}`); console.log(); // Example 2: Extract reusable components console.log('=== Example 2: Component Extraction ==='); const components = await client.request({ method: 'tools/call', params: { name: 'extract_reusable_components', arguments: { url: repoUrl, componentTypes: ['ui-components', 'hooks', 'utilities'], }, }, }); const componentList = JSON.parse(components.content[0].text); console.log(`āœ… Found ${componentList.length} reusable components:`); componentList.slice(0, 5).forEach((comp, i) => { console.log(`${i + 1}. ${comp.name} (${comp.type}) - Reusability: ${comp.reusabilityScore}%`); }); console.log(); // Example 3: Get file content console.log('=== Example 3: File Content Analysis ==='); const fileContent = await client.request({ method: 'tools/call', params: { name: 'get_file_content', arguments: { url: repoUrl, filePath: 'package.json', }, }, }); const packageJson = JSON.parse(fileContent.content[0].text); console.log('āœ… Package.json analysis:'); console.log(`- Name: ${JSON.parse(packageJson).name}`); console.log(`- Version: ${JSON.parse(packageJson).version}`); console.log(`- Description: ${JSON.parse(packageJson).description}`); console.log(); // Example 4: Code refactoring console.log('=== Example 4: Code Refactoring ==='); const sampleCode = ` var userName = "John Doe"; var userEmail = "john@example.com"; function getUserInfo() { return userName + " - " + userEmail; } `; const refactoredCode = await client.request({ method: 'tools/call', params: { name: 'modernize_code', arguments: { code: sampleCode, language: 'javascript', targetVersion: 'ES2022', }, }, }); console.log('āœ… Code modernization:'); console.log('Original:'); console.log(sampleCode); console.log('Refactored:'); console.log(JSON.parse(refactoredCode.content[0].text).refactoredCode); console.log(); // Example 5: AI-powered chat (if OpenAI key is configured) console.log('=== Example 5: AI-Powered Repository Chat ==='); try { const chatResponse = await client.request({ method: 'tools/call', params: { name: 'chat_with_repository', arguments: { url: repoUrl, message: 'What are the main architectural patterns used in this React codebase?', }, }, }); console.log('āœ… AI Response:'); console.log(chatResponse.content[0].text); } catch (error) { console.log('āš ļø AI features require OpenAI API key configuration'); } console.log(); // Example 6: Generate project template console.log('=== Example 6: Template Generation ==='); const template = await client.request({ method: 'tools/call', params: { name: 'generate_boilerplate', arguments: { url: repoUrl, templateType: 'starter', options: { name: 'my-react-app', framework: 'react', includeTests: true, includeDocs: true, }, }, }, }); const templateData = JSON.parse(template.content[0].text); console.log('āœ… Generated template:'); console.log(`- Name: ${templateData.name}`); console.log(`- Files: ${templateData.files.length}`); console.log(`- Dependencies: ${templateData.dependencies.length}`); console.log(); console.log('šŸŽ‰ All examples completed successfully!'); console.log('šŸ’” Try modifying the examples to explore different repositories and options.'); } catch (error) { console.error('āŒ Error:', error.message); console.error('šŸ’” Make sure you have a valid GitHub token configured in your environment'); } finally { await client.close(); serverProcess.kill(); } } // Handle errors and cleanup process.on('SIGINT', () => { console.log('\nšŸ‘‹ Goodbye!'); process.exit(0); }); process.on('unhandledRejection', (error) => { console.error('āŒ Unhandled error:', error); process.exit(1); }); // Run the example main().catch(console.error);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/TheAlchemist6/codecompass-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server