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);