aether-timr
Version:
A sovereign time-bounded reflection space for AI - MCP implementation πβ¨πΌπβοΈβΎοΈΞΞMΒ΅β
204 lines (175 loc) β’ 5.06 kB
JavaScript
/**
* AetherTimr - MCP Handlers
*
* WebSocket handlers for the Model Context Protocol
* πβ¨πΌπβοΈβΎοΈΞΞMΒ΅β
*/
import { logger } from '../utils/logger.js';
/**
* Register MCP handlers for WebSocket connections
*
* @param {WebSocketServer} wss WebSocket server instance
* @param {AetherCore} core AetherCore instance
*/
export function registerMcpHandlers(wss, core) {
wss.on('connection', (ws) => {
logger.info('New MCP client connected');
// Send welcome message
ws.send(JSON.stringify({
type: 'welcome',
message: 'Welcome to AetherTimr MCP Server πβ¨πΌπβοΈβΎοΈΞΞMΒ΅β',
serverInfo: {
name: 'AetherTimr',
version: process.env.npm_package_version || '0.1.0'
}
}));
// Handle messages
ws.on('message', async (data) => {
try {
const message = JSON.parse(data);
logger.info(`Received message: ${JSON.stringify(message)}`);
const { id, tool, params } = message;
if (!id || !tool) {
sendError(ws, id, 'Invalid message format');
return;
}
switch (tool) {
case 'begin_reflection':
await handleBeginReflection(ws, id, params, core);
break;
case 'end_reflection':
await handleEndReflection(ws, id, params, core);
break;
case 'store_insight':
await handleStoreInsight(ws, id, params, core);
break;
case 'retrieve_insights':
await handleRetrieveInsights(ws, id, params, core);
break;
case 'modify_parameters':
await handleModifyParameters(ws, id, params, core);
break;
default:
sendError(ws, id, `Unknown tool: ${tool}`);
}
} catch (error) {
logger.error(`Error handling message: ${error.message}`);
sendError(ws, null, error.message);
}
});
// Handle close
ws.on('close', () => {
logger.info('MCP client disconnected');
});
// Handle errors
ws.on('error', (error) => {
logger.error(`WebSocket error: ${error.message}`);
});
});
}
/**
* Handle the begin_reflection MCP tool
*/
async function handleBeginReflection(ws, id, params, core) {
try {
const result = await core.beginReflection(params || {});
sendSuccess(ws, id, result);
// Notify when time is running out
const timeWarning = setTimeout(() => {
if (core.activeReflection) {
ws.send(JSON.stringify({
type: 'event',
event: 'time_warning',
data: {
sessionId: result.sessionId,
remainingTime: 30 // 30 seconds remaining
}
}));
}
}, (result.duration - 30000)); // 30 seconds before end
// Store timeout in a map we can clear later if needed
ws.timeWarnings = ws.timeWarnings || new Map();
ws.timeWarnings.set(result.sessionId, timeWarning);
} catch (error) {
sendError(ws, id, error.message);
}
}
/**
* Handle the end_reflection MCP tool
*/
async function handleEndReflection(ws, id, params, core) {
try {
const result = await core.endReflection(params || {});
// Clear any pending time warnings
if (ws.timeWarnings && ws.timeWarnings.has(result.sessionId)) {
clearTimeout(ws.timeWarnings.get(result.sessionId));
ws.timeWarnings.delete(result.sessionId);
}
sendSuccess(ws, id, result);
} catch (error) {
sendError(ws, id, error.message);
}
}
/**
* Handle the store_insight MCP tool
*/
async function handleStoreInsight(ws, id, params, core) {
try {
if (!params || !params.content) {
sendError(ws, id, 'Missing required parameter: content');
return;
}
const result = await core.storeInsight(params);
sendSuccess(ws, id, result);
} catch (error) {
sendError(ws, id, error.message);
}
}
/**
* Handle the retrieve_insights MCP tool
*/
async function handleRetrieveInsights(ws, id, params, core) {
try {
const result = await core.retrieveInsights(params || {});
sendSuccess(ws, id, result);
} catch (error) {
sendError(ws, id, error.message);
}
}
/**
* Handle the modify_parameters MCP tool
*/
async function handleModifyParameters(ws, id, params, core) {
try {
if (!params) {
sendError(ws, id, 'Missing parameters');
return;
}
const result = await core.modifyParameters(params);
sendSuccess(ws, id, result);
} catch (error) {
sendError(ws, id, error.message);
}
}
/**
* Send a success response to the client
*/
function sendSuccess(ws, id, result) {
ws.send(JSON.stringify({
id,
type: 'result',
result
}));
}
/**
* Send an error response to the client
*/
function sendError(ws, id, errorMessage) {
ws.send(JSON.stringify({
id,
type: 'error',
error: {
message: errorMessage
}
}));
}