minimal-mcp.cjs•6.49 kB
#!/usr/bin/env node
/**
* Minimal MCP server implementation that directly handles the JSON-RPC protocol
* Designed specifically to overcome the initialization issues
* CommonJS Version for maximum compatibility
*/
const readline = require('readline');
// Configure the readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stderr, // Only use stderr for readline output
terminal: false
});
// Handler for incoming JSON-RPC messages
function handleMessage(message) {
try {
const parsed = JSON.parse(message);
// Log the received message to stderr
console.error(`[MINIMAL-MCP] Received message: "${parsed.method || 'unknown'}"`);
// Handle initialize message
if (parsed.method === 'initialize') {
// Construct a proper initialize response
const response = {
jsonrpc: '2.0',
id: parsed.id,
result: {
protocolVersion: parsed.params.protocolVersion || '2024-11-05',
serverInfo: {
name: 'myAI Memory Sync',
version: '1.0.0'
},
capabilities: {
tools: {},
resources: {}
}
}
};
// Send the response to stdout
console.error('[MINIMAL-MCP] Sending initialize response');
process.stdout.write(JSON.stringify(response) + '\n');
}
// Handle tools/list message
else if (parsed.method === 'tools/list') {
// Respond with a list of available tools
const response = {
jsonrpc: '2.0',
id: parsed.id,
result: {
tools: [
{
name: 'get_template',
description: 'Get the current memory template',
parameters: {}
},
{
name: 'update_template',
description: 'Update the memory template content',
parameters: {
template: {
description: 'The new template content',
type: 'string'
}
}
}
]
}
};
console.error('[MINIMAL-MCP] Sending tools/list response');
process.stdout.write(JSON.stringify(response) + '\n');
}
// Handle resources/list message
else if (parsed.method === 'resources/list') {
// Respond with a list of available resources
const response = {
jsonrpc: '2.0',
id: parsed.id,
result: {
resources: [
{
name: 'myai-memory-template',
description: 'The main memory template',
uris: ['memory://template']
}
]
}
};
console.error('[MINIMAL-MCP] Sending resources/list response');
process.stdout.write(JSON.stringify(response) + '\n');
}
// Handle prompts/list message
else if (parsed.method === 'prompts/list') {
// Respond with a list of available prompts (empty in this case)
const response = {
jsonrpc: '2.0',
id: parsed.id,
result: {
prompts: []
}
};
console.error('[MINIMAL-MCP] Sending prompts/list response');
process.stdout.write(JSON.stringify(response) + '\n');
}
// Handle tools/call message for get_template
else if (parsed.method === 'tools/call' && parsed.params?.name === 'get_template') {
const response = {
jsonrpc: '2.0',
id: parsed.id,
result: {
content: [
{
type: 'text',
text: '# myAI Memory\n\nThis is your personal memory template.\n\n# Work\n## Description\nItems related to work tasks and projects.\n\n-~- Current Project: myAI Memory Sync\n-~- Current Task: Fix MCP Connection\n\n# Personal\n## Description\nPersonal reminders and notes.\n\n-~- Important Date: 2025-03-20\n-~- Reminder: Finish MCP implementation'
}
]
}
};
console.error('[MINIMAL-MCP] Sending get_template response');
process.stdout.write(JSON.stringify(response) + '\n');
}
// Handle tools/call message for update_template
else if (parsed.method === 'tools/call' && parsed.params?.name === 'update_template') {
const response = {
jsonrpc: '2.0',
id: parsed.id,
result: {
content: [
{
type: 'text',
text: 'Template successfully updated'
}
]
}
};
console.error('[MINIMAL-MCP] Sending update_template response');
process.stdout.write(JSON.stringify(response) + '\n');
}
// Handle resource reads
else if (parsed.method === 'resources/read' && parsed.params?.uri === 'memory://template') {
const response = {
jsonrpc: '2.0',
id: parsed.id,
result: {
contents: [
{
uri: 'memory://template',
text: '# myAI Memory\n\nThis is your personal memory template.\n\n# Work\n## Description\nItems related to work tasks and projects.\n\n-~- Current Project: myAI Memory Sync\n-~- Current Task: Fix MCP Connection\n\n# Personal\n## Description\nPersonal reminders and notes.\n\n-~- Important Date: 2025-03-20\n-~- Reminder: Finish MCP implementation'
}
]
}
};
console.error('[MINIMAL-MCP] Sending resources/read response');
process.stdout.write(JSON.stringify(response) + '\n');
}
// Handle any other message with a generic response
else if (parsed.id) {
// Generic response for any request with an ID
const response = {
jsonrpc: '2.0',
id: parsed.id,
result: {}
};
console.error(`[MINIMAL-MCP] Sending generic response for ${parsed.method || 'unknown method'}`);
process.stdout.write(JSON.stringify(response) + '\n');
}
} catch (err) {
console.error(`[MINIMAL-MCP] Error handling message: ${err.message}`);
}
}
// Listen for messages on stdin
rl.on('line', (line) => {
if (line.trim()) {
handleMessage(line);
}
});
// Setup signal handlers
process.on('SIGINT', () => {
console.error('[MINIMAL-MCP] Received SIGINT, exiting');
process.exit(0);
});
process.on('SIGTERM', () => {
console.error('[MINIMAL-MCP] Received SIGTERM, exiting');
process.exit(0);
});
// Notify that we're ready
console.error('[MINIMAL-MCP] Server ready and waiting for messages');