Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(mcp): let memory tools target an explicit store (#4350)

ordinary being5c2fadad8252
diff --git a/v3/@claude-flow/cli/__tests__/mcp-memory-explicit-dbpath-3196.test.ts b/v3/@claude-flow/cli/__tests__/mcp-memory-explicit-dbpath-3196.test.ts
new file mode 100644
index 0000000000..8d8489e17b
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/mcp-memory-explicit-dbpath-3196.test.ts
@@ -0,0 +1,119 @@
+/**
+ * #3196: the CLI accepts an explicit `--path`, but the five primary MCP memory
+ * handlers called the store with no `dbPath`. An MCP caller that knew the
+ * project store therefore could not select it: reads came from the MCP default
+ * (the AgentDB sibling) and writes went there too.
+ *
+ * These handlers now accept the same explicit `dbPath` as `memory_stats`,
+ * `memory_import` and `memory_export`. No path keeps the previous MCP default;
+ * this is not an implicit env override, because the native/encrypted CLI file
+ * and the AgentDB sibling are intentionally separate backends.
+ */
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+import { memoryTools } from '../src/mcp-tools/memory-tools.js';
+import { MEMORY_SCHEMA_V3 } from '../src/memory/memory-initializer.js';
+
+const tool = (name: string) => memoryTools.find(t => t.name === name)!;
+
+async function seed(dbPath: string, keys: string[]): Promise<void> {
+ const initSqlJs = (await import('sql.js')).default;
+ const SQL = await initSqlJs();
+ const db = new SQL.Database();
+ try {
+ db.run(MEMORY_SCHEMA_V3);
+ const now = Date.now();
+ for (const [i, key] of keys.entries()) {
+ db.run(
+ `INSERT INTO memory_entries
+ (id, key, namespace, content, access_count, created_at, updated_at, status)
+ VALUES (?, ?, 'pinned', ?, 0, ?, ?, 'active')`,
+ [`id-${i}`, key, `value for ${key}`, now, now],
+ );
+ }
+ writeFileSync(dbPath, Buffer.from(db.export()));
+ } finally {
+ db.close();
+ }
+}
+
+describe('MCP memory tools accept an explicit dbPath (#3196)', () => {
+ let dir: string;
+ let dbPath: string;
+ let defaultDir: string;
+ let defaultDbPath: string;
+ let prevMemory: string | undefined;
+ let prevBridge: string | undefined;
+
+ beforeEach(async () => {
+ dir = mkdtempSync(join(tmpdir(), 'mcp-memory-path-'));
+ dbPath = join(dir, 'pinned.db');
+ await seed(dbPath, ['alpha', 'beta']);
+
+ // A populated default store makes "read the wrong store" observable rather
+ // than indistinguishable from an empty one.
+ defaultDir = join(dir, 'unused-default');
+ mkdirSync(defaultDir, { recursive: true });
+ defaultDbPath = join(defaultDir, 'memory.db');
+ await seed(defaultDbPath, ['default-only']);
+
+ prevMemory = process.env.CLAUDE_FLOW_MEMORY_PATH;
+ prevBridge = process.env.CLAUDE_FLOW_DISABLE_BRIDGE;
+ process.env.CLAUDE_FLOW_MEMORY_PATH = defaultDir;
+ process.env.CLAUDE_FLOW_DISABLE_BRIDGE = '1';
+ });
+
+ afterEach(() => {
+ if (prevMemory === undefined) delete process.env.CLAUDE_FLOW_MEMORY_PATH;
+ else process.env.CLAUDE_FLOW_MEMORY_PATH = prevMemory;
+ if (prevBridge === undefined) delete process.env.CLAUDE_FLOW_DISABLE_BRIDGE;
+ else process.env.CLAUDE_FLOW_DISABLE_BRIDGE = prevBridge;
+ rmSync(dir, { recursive: true, force: true });
+ });
+
+ it('lists and retrieves from the selected store', async () => {
+ const list = await tool('memory_list').handler({ dbPath }) as Record<string, any>;
+ expect(list.total).toBe(2);
+ expect(list.entries.map((e: { key: string }) => e.key).sort()).toEqual(['alpha', 'beta']);
+
+ const get = await tool('memory_retrieve').handler({
+ key: 'alpha',
+ namespace: 'pinned',
+ dbPath,
+ }) as Record<string, any>;
+ expect(get.found).toBe(true);
+ expect(get.value).toBe('value for alpha');
+ });
+
+ it('uses the explicit path instead of the default', async () => {
+ const otherPath = join(dir, 'other.db');
+ await seed(otherPath, ['gamma']);
+
+ const list = await tool('memory_list').handler({ dbPath: otherPath }) as Record<string, any>;
+ expect(list.total).toBe(1);
+ expect(list.entries.map((e: { key: string }) => e.key)).toEqual(['gamma']);
+ });
+
+ it('deletes and reports stats from the selected store', async () => {
+ const del = await tool('memory_delete').handler({
+ key: 'alpha',
+ namespace: 'pinned',
+ dbPath,
+ }) as Record<string, any>;
+ expect(del.deleted).toBe(true);
+
+ const stats = await tool('memory_stats').handler({ dbPath }) as Record<string, any>;
+ expect(stats.totalEntries).toBe(1);
+ });
+
+ it('leaves the default store untouched', async () => {
+ await tool('memory_delete').handler({ key: 'alpha', namespace: 'pinned', dbPath });
+
+ const list = await tool('memory_list').handler({ dbPath: defaultDbPath }) as Record<string, any>;
+ expect(list.total).toBe(1);
+ expect(list.entries.map((e: { key: string }) => e.key)).toEqual(['default-only']);
+ });
+});
diff --git a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts
index ff5307b1d9..8605652ccb 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/memory-tools.ts
@@ -38,6 +38,18 @@ interface LegacyMemoryStore {
const MEMORY_DIR = '.swarm';
const LEGACY_MEMORY_FILE = 'store.json';
const LEGACY_MEMORY_DIR = '.claude-flow/memory';
+
+/**
+ * #3196: allow an MCP caller to select the same explicit store the CLI accepts
+ * via `--path`. No path means the existing MCP default is unchanged; this is
+ * deliberately not an implicit `CLAUDE_FLOW_DB_PATH` override because the
+ * native/encrypted CLI store and the AgentDB sibling are separate backends.
+ */
+function resolveDbPathOption(inputDbPath: unknown): string | undefined {
+ return typeof inputDbPath === 'string' && inputDbPath.trim().length > 0
+ ? resolve(inputDbPath)
+ : undefined;
+}
const MIGRATION_MARKER = '.migrated-to-sqlite';
function getMemoryDir(): string {
@@ -510,6 +522,7 @@ export const memoryTools: MCPTool[] = [
enum: ['user_claim', 'agent_output', 'system_observation', 'tool_result', 'unknown'],
description: 'ADR-323: who/what produced this value, so shared-namespace retrieval can filter by trust level instead of conflating a user\'s stated claim with an agent\'s own output. Default: "unknown".',
},
+ dbPath: { type: 'string', description: 'Explicit SQLite store to read/write; omit to use the MCP default (the native/AgentDB sibling).' },
},
required: ['key', 'value'],
},
@@ -519,7 +532,8 @@ export const memoryTools: MCPTool[] = [
return { success: false, key: input.key, stored: false, hasEmbedding: false, ...missingKey };
}
- await ensureInitialized();
+ const dbPath = resolveDbPathOption(input.dbPath);
+ if (!dbPath) await ensureInitialized();
const { storeEntry } = await getMemoryFunctions();
const key = input.key as string;
@@ -562,6 +576,7 @@ export const memoryTools: MCPTool[] = [
ttl,
upsert,
provenanceType,
+ dbPath,
});
const duration = performance.now() - startTime;
@@ -599,6 +614,7 @@ export const memoryTools: MCPTool[] = [
properties: {
key: { type: 'string', description: 'Memory key' },
namespace: { type: 'string', description: 'Namespace (default: "default")' },
+ dbPath: { type: 'string', description: 'Explicit SQLite store to read/write; omit to use the MCP default (the native/AgentDB sibling).' },
},
required: ['key'],
},
@@ -608,7 +624,8 @@ export const memoryTools: MCPTool[] = [
return { key: input.key, namespace: input.namespace, value: null, found: false, ...missingKey };
}
- await ensureInitialized();
+ const dbPath = resolveDbPathOption(input.dbPath);
+ if (!dbPath) await ensureInitialized();
const { getEntry } = await getMemoryFunctions();
const key = input.key as string;
@@ -617,7 +634,7 @@ export const memoryTools: MCPTool[] = [
validateMemoryInput(key, undefined, undefined, namespace);
try {
- const result = await getEntry({ key, namespace });
+ const result = await getEntry({ key, namespace, dbPath });
if (result.found && result.entry) {
// Try to parse JSON value
@@ -676,6 +693,7 @@ export const memoryTools: MCPTool[] = [
items: { type: 'string', enum: ['user_claim', 'agent_output', 'system_observation', 'tool_result', 'unknown'] },
description: 'ADR-323: restrict results to these provenance types (e.g. exclude user_claim when fact-checking). Omit for no filtering. Enforced for standard and SmartRetrieval searches.',
},
+ dbPath: { type: 'string', description: 'Explicit SQLite store to read/write; omit to use the MCP default (the native/AgentDB sibling).' },
},
required: ['query'],
},
@@ -685,7 +703,8 @@ export const memoryTools: MCPTool[] = [
return { query: input.query, results: [], total: 0, ...missingQuery };
}
- await ensureInitialized();
+ const dbPath = resolveDbPathOption(input.dbPath);
+ if (!dbPath) await ensureInitialized();
const { searchEntries } = await getMemoryFunctions();
const query = input.query as string;
@@ -736,6 +755,7 @@ export const memoryTools: MCPTool[] = [
limit: req.limit || limit * 3,
threshold: req.threshold ?? threshold,
provenanceFilter,
+ dbPath,
});
return {
results: r.results.map(e => ({
@@ -781,7 +801,7 @@ export const memoryTools: MCPTool[] = [
});
const smartNearMisses = results.length === 0 && thresholdWasDefault
- ? await nearMisses(searchEntries, { query, namespace, provenanceFilter })
+ ? await nearMisses(searchEntries, { query, namespace, provenanceFilter, dbPath })
: undefined;
return {
@@ -812,6 +832,7 @@ export const memoryTools: MCPTool[] = [
limit,
threshold,
provenanceFilter,
+ dbPath,
});
if (!result.success) {
@@ -839,7 +860,7 @@ export const memoryTools: MCPTool[] = [
});
const nearMissInfo = results.length === 0 && thresholdWasDefault
- ? await nearMisses(searchEntries, { query, namespace, provenanceFilter })
+ ? await nearMisses(searchEntries, { query, namespace, provenanceFilter, dbPath })
: undefined;
return {
@@ -870,6 +891,7 @@ export const memoryTools: MCPTool[] = [
properties: {
key: { type: 'string', description: 'Memory key' },
namespace: { type: 'string', description: 'Namespace (default: "default")' },
+ dbPath: { type: 'string', description: 'Explicit SQLite store to read/write; omit to use the MCP default (the native/AgentDB sibling).' },
},
required: ['key'],
},
@@ -879,7 +901,8 @@ export const memoryTools: MCPTool[] = [
return { success: false, key: input.key, namespace: input.namespace, deleted: false, ...missingKey };
}
- await ensureInitialized();
+ const dbPath = resolveDbPathOption(input.dbPath);
+ if (!dbPath) await ensureInitialized();
const { deleteEntry } = await getMemoryFunctions();
const key = input.key as string;
@@ -888,7 +911,7 @@ export const memoryTools: MCPTool[] = [
validateMemoryInput(key, undefined, undefined, namespace);
try {
- const result = await deleteEntry({ key, namespace });
+ const result = await deleteEntry({ key, namespace, dbPath });
return {
success: result.deleted,
@@ -924,10 +947,12 @@ export const memoryTools: MCPTool[] = [
},
limit: { type: 'number', description: 'Maximum results (default: 50)' },
offset: { type: 'number', description: 'Offset for pagination (default: 0)' },
+ dbPath: { type: 'string', description: 'Explicit SQLite store to read/write; omit to use the MCP default (the native/AgentDB sibling).' },
},
},
handler: async (input) => {
- await ensureInitialized();
+ const dbPath = resolveDbPathOption(input.dbPath);
+ if (!dbPath) await ensureInitialized();
const { listEntries } = await getMemoryFunctions();
const namespace = input.namespace as string | undefined;
@@ -945,6 +970,7 @@ export const memoryTools: MCPTool[] = [
tags,
limit,
offset,
+ dbPath,
});
const entries = result.entries.map(e => ({
@@ -983,14 +1009,14 @@ export const memoryTools: MCPTool[] = [
inputSchema: {
type: 'object',
properties: {
- dbPath: { type: 'string', description: 'Database file to inspect; omitted uses the MCP store default' },
+ dbPath: { type: 'string', description: 'Explicit SQLite store to read/write; omit to use the MCP default (the native/AgentDB sibling).' },
},
},
handler: async (input) => {
if (input.dbPath !== undefined && (typeof input.dbPath !== 'string' || !input.dbPath.trim())) {
return memoryStatsUnavailable('dbPath must be a non-empty string');
}
- const dbPath = typeof input.dbPath === 'string' ? resolve(input.dbPath) : undefined;
+ const dbPath = resolveDbPathOption(input.dbPath);
// An explicit read must not initialize or migrate the unrelated default store.
if (!dbPath) await ensureInitialized();
const { listEntries } = await getMemoryFunctions();