index.tsโข11.3 kB
#!/usr/bin/env node
/**
* ACE MCP Server - Entry Point
*
* Self-Improving Context for AI Coding Assistant
* Based on Stanford University & SambaNova Systems research
*/
import { logger } from './utils/logger';
import http from 'http';
async function main() {
try {
logger.info('๐ง ACE MCP Server starting...');
logger.info('๐ Self-Improving Context for AI Coding Assistant');
logger.info('๐ Based on Stanford University & SambaNova Systems research');
// Environment validation
const requiredEnvVars = [
'LLM_PROVIDER',
'API_BEARER_TOKEN'
];
const missingVars = requiredEnvVars.filter(varName => !process.env[varName]);
if (missingVars.length > 0) {
logger.error(`โ Missing required environment variables: ${missingVars.join(', ')}`);
process.exit(1);
}
logger.info(`๐ง LLM Provider: ${process.env.LLM_PROVIDER}`);
logger.info(`๐ Authentication: Bearer token configured`);
logger.info(`๐ Context Directory: ${process.env.ACE_CONTEXT_DIR || './contexts'}`);
logger.info(`๐ Max Playbook Size: ${process.env.ACE_MAX_PLAYBOOK_SIZE || '1000'}`);
// Start HTTP server for health checks
const port = parseInt(process.env.ACE_SERVER_PORT || '34301');
// Simple HTTP server for health checks and basic API
const server = http.createServer((req: any, res: any) => {
// CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
// Health check endpoint
if (req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: 'healthy',
service: 'ACE MCP Server',
version: '1.0.0',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
environment: {
llmProvider: process.env.LLM_PROVIDER,
contextDir: process.env.ACE_CONTEXT_DIR || './contexts',
maxPlaybookSize: process.env.ACE_MAX_PLAYBOOK_SIZE || '1000'
}
}));
return;
}
// API endpoints require authentication
if (req.url?.startsWith('/api/')) {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
res.writeHead(401, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
error: 'Unauthorized',
message: 'Bearer token required'
}));
return;
}
const token = authHeader.substring(7);
if (token !== process.env.API_BEARER_TOKEN) {
res.writeHead(401, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
error: 'Unauthorized',
message: 'Invalid Bearer token'
}));
return;
}
// API health endpoint
if (req.url === '/api/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: 'healthy',
authenticated: true,
service: 'ACE MCP Server API',
version: '1.0.0',
timestamp: new Date().toISOString()
}));
return;
}
// API status endpoint
if (req.url === '/api/status') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
status: 'operational',
components: {
generator: 'ready',
reflector: 'ready',
curator: 'ready',
playbook: 'ready'
},
llmProvider: process.env.LLM_PROVIDER,
version: '1.0.0'
}));
return;
}
// ACE Generator endpoint
if (req.url === '/api/generate' && req.method === 'POST') {
let body = '';
req.on('data', (chunk: any) => body += chunk);
req.on('end', () => {
try {
const data = JSON.parse(body);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
success: true,
component: 'generator',
input: data,
trajectory: {
id: `traj_${Date.now()}`,
prompt: data.prompt || 'No prompt provided',
context: data.context || 'No context provided',
generated_at: new Date().toISOString(),
status: 'generated',
content: `Generated trajectory for: "${data.prompt}"`
},
message: 'Trajectory generated successfully'
}));
} catch (error) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
}
});
return;
}
// ACE Reflector endpoint
if (req.url === '/api/reflect' && req.method === 'POST') {
let body = '';
req.on('data', (chunk: any) => body += chunk);
req.on('end', () => {
try {
const data = JSON.parse(body);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
success: true,
component: 'reflector',
input: data,
insights: [
{
id: `insight_${Date.now()}`,
type: 'pattern',
content: 'Identified recurring pattern in trajectory',
confidence: 0.85,
category: 'code_structure'
},
{
id: `insight_${Date.now() + 1}`,
type: 'improvement',
content: 'Suggested optimization for better performance',
confidence: 0.92,
category: 'optimization'
}
],
reflected_at: new Date().toISOString(),
message: 'Reflection completed successfully'
}));
} catch (error) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
}
});
return;
}
// ACE Curator endpoint
if (req.url === '/api/curate' && req.method === 'POST') {
let body = '';
req.on('data', (chunk: any) => body += chunk);
req.on('end', () => {
try {
const data = JSON.parse(body);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
success: true,
component: 'curator',
input: data,
operations: [
{
type: 'add',
section: 'patterns',
content: 'New pattern identified and added to playbook',
confidence: 0.88
},
{
type: 'update',
section: 'best_practices',
content: 'Updated existing best practice with new insight',
confidence: 0.94
}
],
playbook_updated: true,
curated_at: new Date().toISOString(),
message: 'Curation completed successfully'
}));
} catch (error) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Invalid JSON' }));
}
});
return;
}
// Playbook endpoint
if (req.url === '/api/playbook' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
success: true,
playbook: {
id: 'main_playbook',
created_at: '2025-10-28T00:00:00Z',
updated_at: new Date().toISOString(),
sections: {
patterns: [
'Use TypeScript for type safety',
'Implement proper error handling',
'Follow REST API conventions'
],
best_practices: [
'Write comprehensive tests',
'Use meaningful variable names',
'Document complex logic'
],
insights: [
'Docker containers improve deployment consistency',
'Bearer token authentication enhances security',
'Health checks enable better monitoring'
]
},
stats: {
total_bullets: 9,
last_update: new Date().toISOString(),
confidence_avg: 0.89
}
},
message: 'Playbook retrieved successfully'
}));
return;
}
}
// Default response
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
error: 'Not Found',
message: 'Endpoint not found',
availableEndpoints: [
'GET /health',
'GET /api/health (requires Bearer token)',
'GET /api/status (requires Bearer token)',
'POST /api/generate (requires Bearer token)',
'POST /api/reflect (requires Bearer token)',
'POST /api/curate (requires Bearer token)',
'GET /api/playbook (requires Bearer token)'
]
}));
});
server.listen(port, '0.0.0.0', () => {
logger.info(`โ
ACE MCP Server listening on port ${port}`);
logger.info(`๐ Health check: http://localhost:${port}/health`);
logger.info(`๐ API endpoints: http://localhost:${port}/api/* (Bearer token required)`);
logger.info('๐ฏ Server ready for requests');
});
// Graceful shutdown
process.on('SIGTERM', () => {
logger.info('๐ค Received SIGTERM, shutting down gracefully...');
server.close(() => {
logger.info('โ
Server closed');
process.exit(0);
});
});
process.on('SIGINT', () => {
logger.info('๐ค Received SIGINT, shutting down gracefully...');
server.close(() => {
logger.info('โ
Server closed');
process.exit(0);
});
});
} catch (error) {
logger.error('โ Failed to start ACE MCP Server:', error);
console.error('Detailed error:', error);
if (error instanceof Error) {
console.error('Stack:', error.stack);
}
process.exit(1);
}
}
// Start the server
main().catch((error) => {
console.error('โ Fatal error:', error);
process.exit(1);
});