05323c9db5
* refactor(worker): remove dead code from worker-service.ts Remove ~216 lines of unreachable code: - Delete `runInteractiveSetup` function (defined but never called) - Remove unused imports: fs namespace, spawn, homedir, readline, existsSync/writeFileSync/readFileSync/mkdirSync - Clean up CursorHooksInstaller imports (keep only used exports) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(worker): only enable SDK fallback when Claude is configured Add isConfigured() method to SDKAgent that checks for ANTHROPIC_API_KEY or claude CLI availability. Worker now only sets SDK agent as fallback for third-party providers when credentials exist, preventing cascading failures for users who intentionally use Gemini/OpenRouter without Claude. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor(worker): remove misleading re-export indirection Remove unnecessary re-export of updateCursorContextForProject from worker-service.ts. ResponseProcessor now imports directly from CursorHooksInstaller.ts where the function is defined. This eliminates misleading indirection that suggested a circular dependency existed. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor(mcp): use build-time injected version instead of hardcoded strings Replace hardcoded '1.0.0' version strings with __DEFAULT_PACKAGE_VERSION__ constant that esbuild replaces at build time. This ensures MCP server and client versions stay synchronized with package.json. - worker-service.ts: MCP client version now uses packageVersion - ChromaSync.ts: MCP client version now uses packageVersion - mcp-server.ts: MCP server version now uses packageVersion - Added clarifying comments for empty MCP capabilities objects Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * feat: Implement cleanup and validation plans for worker-service.ts - Added a comprehensive cleanup plan addressing 23 identified issues in worker-service.ts, focusing on safe deletions, low-risk simplifications, and medium-risk improvements. - Created an execution plan for validating intentional patterns in worker-service.ts, detailing necessary actions and priorities. - Generated a report on unjustified logic in worker-service.ts, categorizing issues by severity and providing recommendations for immediate and short-term actions. - Introduced documentation for recent activity in the mem-search plugin, enhancing traceability and context for changes. * fix(sdk): remove dangerous ANTHROPIC_API_KEY check from isConfigured Claude Code uses CLI authentication, not direct API calls. Checking for ANTHROPIC_API_KEY could accidentally use a user's API key (from other projects) which costs 20x more than Claude Code's pricing. Now only checks for claude CLI availability. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix(worker): remove fallback agent concept entirely Users who choose Gemini/OpenRouter want those providers, not secret fallback behavior. Removed setFallbackAgent calls and the unused isConfigured() method. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
321 lines
9.6 KiB
TypeScript
321 lines
9.6 KiB
TypeScript
/**
|
|
* Claude-mem MCP Search Server - Thin HTTP Wrapper
|
|
*
|
|
* Refactored from 2,718 lines to ~600-800 lines
|
|
* Delegates all business logic to Worker HTTP API at localhost:37777
|
|
* Maintains MCP protocol handling and tool schemas
|
|
*/
|
|
|
|
// Version injected at build time by esbuild define
|
|
declare const __DEFAULT_PACKAGE_VERSION__: string;
|
|
const packageVersion = typeof __DEFAULT_PACKAGE_VERSION__ !== 'undefined' ? __DEFAULT_PACKAGE_VERSION__ : '0.0.0-dev';
|
|
|
|
// Import logger first
|
|
import { logger } from '../utils/logger.js';
|
|
|
|
// CRITICAL: Redirect console to stderr BEFORE other imports
|
|
// MCP uses stdio transport where stdout is reserved for JSON-RPC protocol messages.
|
|
// Any logs to stdout break the protocol (Claude Desktop parses "[2025..." as JSON array).
|
|
const _originalLog = console['log'];
|
|
console['log'] = (...args: any[]) => {
|
|
logger.error('CONSOLE', 'Intercepted console output (MCP protocol protection)', undefined, { args });
|
|
};
|
|
|
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
import {
|
|
CallToolRequestSchema,
|
|
ListToolsRequestSchema,
|
|
} from '@modelcontextprotocol/sdk/types.js';
|
|
import { getWorkerPort, getWorkerHost } from '../shared/worker-utils.js';
|
|
|
|
/**
|
|
* Worker HTTP API configuration
|
|
*/
|
|
const WORKER_PORT = getWorkerPort();
|
|
const WORKER_HOST = getWorkerHost();
|
|
const WORKER_BASE_URL = `http://${WORKER_HOST}:${WORKER_PORT}`;
|
|
|
|
/**
|
|
* Map tool names to Worker HTTP endpoints
|
|
*/
|
|
const TOOL_ENDPOINT_MAP: Record<string, string> = {
|
|
'search': '/api/search',
|
|
'timeline': '/api/timeline'
|
|
};
|
|
|
|
/**
|
|
* Call Worker HTTP API endpoint
|
|
*/
|
|
async function callWorkerAPI(
|
|
endpoint: string,
|
|
params: Record<string, any>
|
|
): Promise<{ content: Array<{ type: 'text'; text: string }>; isError?: boolean }> {
|
|
logger.debug('SYSTEM', '→ Worker API', undefined, { endpoint, params });
|
|
|
|
try {
|
|
const searchParams = new URLSearchParams();
|
|
|
|
// Convert params to query string
|
|
for (const [key, value] of Object.entries(params)) {
|
|
if (value !== undefined && value !== null) {
|
|
searchParams.append(key, String(value));
|
|
}
|
|
}
|
|
|
|
const url = `${WORKER_BASE_URL}${endpoint}?${searchParams}`;
|
|
const response = await fetch(url);
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
throw new Error(`Worker API error (${response.status}): ${errorText}`);
|
|
}
|
|
|
|
const data = await response.json() as { content: Array<{ type: 'text'; text: string }>; isError?: boolean };
|
|
|
|
logger.debug('SYSTEM', '← Worker API success', undefined, { endpoint });
|
|
|
|
// Worker returns { content: [...] } format directly
|
|
return data;
|
|
} catch (error) {
|
|
logger.error('SYSTEM', '← Worker API error', { endpoint }, error as Error);
|
|
return {
|
|
content: [{
|
|
type: 'text' as const,
|
|
text: `Error calling Worker API: ${error instanceof Error ? error.message : String(error)}`
|
|
}],
|
|
isError: true
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Call Worker HTTP API with POST body
|
|
*/
|
|
async function callWorkerAPIPost(
|
|
endpoint: string,
|
|
body: Record<string, any>
|
|
): Promise<{ content: Array<{ type: 'text'; text: string }>; isError?: boolean }> {
|
|
logger.debug('HTTP', 'Worker API request (POST)', undefined, { endpoint });
|
|
|
|
try {
|
|
const url = `${WORKER_BASE_URL}${endpoint}`;
|
|
const response = await fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(body)
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const errorText = await response.text();
|
|
throw new Error(`Worker API error (${response.status}): ${errorText}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
logger.debug('HTTP', 'Worker API success (POST)', undefined, { endpoint });
|
|
|
|
// Wrap raw data in MCP format
|
|
return {
|
|
content: [{
|
|
type: 'text' as const,
|
|
text: JSON.stringify(data, null, 2)
|
|
}]
|
|
};
|
|
} catch (error) {
|
|
logger.error('HTTP', 'Worker API error (POST)', { endpoint }, error as Error);
|
|
return {
|
|
content: [{
|
|
type: 'text' as const,
|
|
text: `Error calling Worker API: ${error instanceof Error ? error.message : String(error)}`
|
|
}],
|
|
isError: true
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Verify Worker is accessible
|
|
*/
|
|
async function verifyWorkerConnection(): Promise<boolean> {
|
|
try {
|
|
const response = await fetch(`${WORKER_BASE_URL}/api/health`);
|
|
return response.ok;
|
|
} catch (error) {
|
|
// Expected during worker startup or if worker is down
|
|
logger.debug('SYSTEM', 'Worker health check failed', {}, error as Error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Tool definitions with HTTP-based handlers
|
|
* Minimal descriptions - use help() tool with operation parameter for detailed docs
|
|
*/
|
|
const tools = [
|
|
{
|
|
name: '__IMPORTANT',
|
|
description: `3-LAYER WORKFLOW (ALWAYS FOLLOW):
|
|
1. search(query) → Get index with IDs (~50-100 tokens/result)
|
|
2. timeline(anchor=ID) → Get context around interesting results
|
|
3. get_observations([IDs]) → Fetch full details ONLY for filtered IDs
|
|
NEVER fetch full details without filtering first. 10x token savings.`,
|
|
inputSchema: {
|
|
type: 'object',
|
|
properties: {}
|
|
},
|
|
handler: async () => ({
|
|
content: [{
|
|
type: 'text' as const,
|
|
text: `# Memory Search Workflow
|
|
|
|
**3-Layer Pattern (ALWAYS follow this):**
|
|
|
|
1. **Search** - Get index of results with IDs
|
|
\`search(query="...", limit=20, project="...")\`
|
|
Returns: Table with IDs, titles, dates (~50-100 tokens/result)
|
|
|
|
2. **Timeline** - Get context around interesting results
|
|
\`timeline(anchor=<ID>, depth_before=3, depth_after=3)\`
|
|
Returns: Chronological context showing what was happening
|
|
|
|
3. **Fetch** - Get full details ONLY for relevant IDs
|
|
\`get_observations(ids=[...])\` # ALWAYS batch for 2+ items
|
|
Returns: Complete details (~500-1000 tokens/result)
|
|
|
|
**Why:** 10x token savings. Never fetch full details without filtering first.`
|
|
}]
|
|
})
|
|
},
|
|
{
|
|
name: 'search',
|
|
description: 'Step 1: Search memory. Returns index with IDs. Params: query, limit, project, type, obs_type, dateStart, dateEnd, offset, orderBy',
|
|
inputSchema: {
|
|
type: 'object',
|
|
properties: {},
|
|
additionalProperties: true
|
|
},
|
|
handler: async (args: any) => {
|
|
const endpoint = TOOL_ENDPOINT_MAP['search'];
|
|
return await callWorkerAPI(endpoint, args);
|
|
}
|
|
},
|
|
{
|
|
name: 'timeline',
|
|
description: 'Step 2: Get context around results. Params: anchor (observation ID) OR query (finds anchor automatically), depth_before, depth_after, project',
|
|
inputSchema: {
|
|
type: 'object',
|
|
properties: {},
|
|
additionalProperties: true
|
|
},
|
|
handler: async (args: any) => {
|
|
const endpoint = TOOL_ENDPOINT_MAP['timeline'];
|
|
return await callWorkerAPI(endpoint, args);
|
|
}
|
|
},
|
|
{
|
|
name: 'get_observations',
|
|
description: 'Step 3: Fetch full details for filtered IDs. Params: ids (array of observation IDs, required), orderBy, limit, project',
|
|
inputSchema: {
|
|
type: 'object',
|
|
properties: {
|
|
ids: {
|
|
type: 'array',
|
|
items: { type: 'number' },
|
|
description: 'Array of observation IDs to fetch (required)'
|
|
}
|
|
},
|
|
required: ['ids'],
|
|
additionalProperties: true
|
|
},
|
|
handler: async (args: any) => {
|
|
return await callWorkerAPIPost('/api/observations/batch', args);
|
|
}
|
|
}
|
|
];
|
|
|
|
// Create the MCP server
|
|
const server = new Server(
|
|
{
|
|
name: 'mcp-search-server',
|
|
version: packageVersion,
|
|
},
|
|
{
|
|
capabilities: {
|
|
tools: {}, // Exposes tools capability (handled by ListToolsRequestSchema and CallToolRequestSchema)
|
|
},
|
|
}
|
|
);
|
|
|
|
// Register tools/list handler
|
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
return {
|
|
tools: tools.map(tool => ({
|
|
name: tool.name,
|
|
description: tool.description,
|
|
inputSchema: tool.inputSchema
|
|
}))
|
|
};
|
|
});
|
|
|
|
// Register tools/call handler
|
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
const tool = tools.find(t => t.name === request.params.name);
|
|
|
|
if (!tool) {
|
|
throw new Error(`Unknown tool: ${request.params.name}`);
|
|
}
|
|
|
|
try {
|
|
return await tool.handler(request.params.arguments || {});
|
|
} catch (error) {
|
|
logger.error('SYSTEM', 'Tool execution failed', { tool: request.params.name }, error as Error);
|
|
return {
|
|
content: [{
|
|
type: 'text' as const,
|
|
text: `Tool execution failed: ${error instanceof Error ? error.message : String(error)}`
|
|
}],
|
|
isError: true
|
|
};
|
|
}
|
|
});
|
|
|
|
// Cleanup function
|
|
async function cleanup() {
|
|
logger.info('SYSTEM', 'MCP server shutting down');
|
|
process.exit(0);
|
|
}
|
|
|
|
// Register cleanup handlers for graceful shutdown
|
|
process.on('SIGTERM', cleanup);
|
|
process.on('SIGINT', cleanup);
|
|
|
|
// Start the server
|
|
async function main() {
|
|
// Start the MCP server
|
|
const transport = new StdioServerTransport();
|
|
await server.connect(transport);
|
|
logger.info('SYSTEM', 'Claude-mem search server started');
|
|
|
|
// Check Worker availability in background
|
|
setTimeout(async () => {
|
|
const workerAvailable = await verifyWorkerConnection();
|
|
if (!workerAvailable) {
|
|
logger.error('SYSTEM', 'Worker not available', undefined, { workerUrl: WORKER_BASE_URL });
|
|
logger.error('SYSTEM', 'Tools will fail until Worker is started');
|
|
logger.error('SYSTEM', 'Start Worker with: npm run worker:restart');
|
|
} else {
|
|
logger.info('SYSTEM', 'Worker available', undefined, { workerUrl: WORKER_BASE_URL });
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
main().catch((error) => {
|
|
logger.error('SYSTEM', 'Fatal error', undefined, error);
|
|
// Exit gracefully: Windows Terminal won't keep tab open on exit 0
|
|
// The wrapper/plugin will handle restart logic if needed
|
|
process.exit(0);
|
|
});
|