@ldavis9000aws/mcp-project-memory
Version:
Enhanced memory system for software development projects with persistent context across sessions
151 lines (144 loc) • 4.96 kB
JavaScript
/**
* CLI tool for managing the Project Memory MCP server
*/
import { spawn } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
import { promises as fsPromises } from 'fs';
// Get directory of current file
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Parse command-line arguments
const args = process.argv.slice(2);
const command = args[0];
const memoryFilePath = args[1] || 'memory.json';
// Help text
const helpText = `
Project Memory MCP Server
USAGE:
mcp-project-memory [COMMAND] [OPTIONS]
COMMANDS:
start Start the server in the foreground
daemon Start the server as a background process
help Show this help message
version Show version information
OPTIONS:
[memory-file] Path to memory file (default: memory.json)
EXAMPLES:
# Start the server in foreground with default memory file
npx @ldavis9000aws/mcp-project-memory start
# Start the server as a background process with custom memory file
npx @ldavis9000aws/mcp-project-memory daemon path/to/memory.json
# Using with Claude Desktop
# Include this in your launch configuration:
{
"mcp": {
"memory": {
"command": "npx @ldavis9000aws/mcp-project-memory start"
}
}
}
`;
// Version information
async function showVersion() {
try {
const packagePath = path.join(__dirname, '../package.json');
const packageData = await fsPromises.readFile(packagePath, 'utf8');
const { version } = JSON.parse(packageData);
console.log(`Project Memory MCP Server v${version}`);
}
catch (error) {
console.log('Version information not available');
if (error.message)
console.error(error.message);
}
}
// Start server in foreground mode
function startServer() {
console.log(`Starting Project Memory MCP Server (memory file: ${memoryFilePath})`);
console.log('Press Ctrl+C to stop');
// Set environment variable for memory file path
process.env.MEMORY_FILE_PATH = memoryFilePath;
// Import and run the server
import('./index.js').catch((error) => {
console.error('Failed to start server:', error);
process.exit(1);
});
}
// Start server as daemon (background process)
async function startDaemon() {
// Create logs directory if it doesn't exist
const logsDir = path.join(__dirname, '../logs');
try {
await fsPromises.mkdir(logsDir, { recursive: true });
}
catch (error) {
console.error(`Error creating logs directory: ${error.message}`);
}
// Configure log files
const date = new Date().toISOString().replace(/:/g, '-');
const stdoutPath = path.join(logsDir, `server-${date}.log`);
const stderrPath = path.join(logsDir, `server-error-${date}.log`);
// Define the server path
const serverPath = path.join(__dirname, 'index.js');
// Use the simpler approach - let Node.js handle the daemon
const cmd = process.platform === 'win32' ? 'start' : 'nohup';
const args = process.platform === 'win32'
? ['/B', 'node', serverPath]
: ['node', serverPath, '>', stdoutPath, '2>', stderrPath, '&'];
try {
console.log('Starting MCP Project Memory Server as daemon...');
console.log(`Memory file path: ${memoryFilePath}`);
// Set environment variable for memory file path
process.env.MEMORY_FILE_PATH = memoryFilePath;
if (process.platform === 'win32') {
// Windows specific - use spawn with detached option
const options = {
detached: true,
shell: true,
env: { ...process.env }
};
const child = spawn(cmd, args, options);
child.unref();
console.log('Server started in background');
}
else {
// Unix systems - use exec
const { exec } = require('child_process');
exec(args.join(' '), (error) => {
if (error) {
console.error(`Failed to start daemon: ${error.message}`);
process.exit(1);
}
});
console.log('Server started in background');
}
console.log(`Output logs: ${stdoutPath}`);
console.log(`Error logs: ${stderrPath}`);
}
catch (error) {
console.error(`Failed to start daemon: ${error.message}`);
process.exit(1);
}
}
// Route commands
async function main() {
switch (command) {
case 'start':
startServer();
break;
case 'daemon':
await startDaemon();
break;
case 'version':
await showVersion();
break;
case 'help':
default:
console.log(helpText);
break;
}
}
main().catch((error) => {
console.error('Error:', error);
});