|
| 1 | +// Force synthetic embeddings + sqlite backend BEFORE importing anything |
| 2 | +// that loads cfg/db. vitest.config.ts already sets these via env, but keep |
| 3 | +// this guard for standalone tsx runs. |
| 4 | +process.env.OM_EMBEDDINGS = "synthetic"; |
| 5 | +process.env.OM_EMBEDDING_FALLBACK = "synthetic"; |
| 6 | +process.env.OM_METADATA_BACKEND = process.env.OM_METADATA_BACKEND || "sqlite"; |
| 7 | +process.env.OM_VECTOR_BACKEND = process.env.OM_VECTOR_BACKEND || "sqlite"; |
| 8 | + |
| 9 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 10 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 11 | +import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; |
| 12 | +import { create_mcp_srv } from "../src/ai/mcp"; |
| 13 | +import { run_async, q } from "../src/core/db"; |
| 14 | + |
| 15 | +const T_ALICE = "tenant-alice-mcp"; |
| 16 | +const T_BOB = "tenant-bob-mcp"; |
| 17 | + |
| 18 | +async function cleanup() { |
| 19 | + await run_async(`DELETE FROM memories`); |
| 20 | + try { |
| 21 | + await run_async(`DELETE FROM vectors`); |
| 22 | + } catch { |
| 23 | + /* schema variant */ |
| 24 | + } |
| 25 | + try { |
| 26 | + await run_async(`DELETE FROM openmemory_vectors`); |
| 27 | + } catch { |
| 28 | + /* schema variant */ |
| 29 | + } |
| 30 | + try { |
| 31 | + await run_async(`DELETE FROM waypoints`); |
| 32 | + } catch { |
| 33 | + /* schema variant */ |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +async function connect_client(tenant?: string) { |
| 38 | + const srv = create_mcp_srv(tenant); |
| 39 | + const [client_transport, server_transport] = |
| 40 | + InMemoryTransport.createLinkedPair(); |
| 41 | + await srv.connect(server_transport); |
| 42 | + const client = new Client({ name: "test-client", version: "0.0.0" }); |
| 43 | + await client.connect(client_transport); |
| 44 | + return { client, srv }; |
| 45 | +} |
| 46 | + |
| 47 | +function parse_items(result: any): Array<{ id: string; user_id?: string }> { |
| 48 | + // openmemory_list returns two text blocks; the second is a JSON dump. |
| 49 | + const blocks = (result?.content ?? []) as Array<{ |
| 50 | + type: string; |
| 51 | + text: string; |
| 52 | + }>; |
| 53 | + const jsonBlock = blocks |
| 54 | + .filter((b) => b.type === "text") |
| 55 | + .map((b) => b.text) |
| 56 | + .find((t) => t.trim().startsWith("{")); |
| 57 | + if (!jsonBlock) return []; |
| 58 | + const parsed = JSON.parse(jsonBlock); |
| 59 | + return parsed.items ?? []; |
| 60 | +} |
| 61 | + |
| 62 | +function parse_store(result: any): { id?: string; project_id?: string } { |
| 63 | + const blocks = (result?.content ?? []) as Array<{ |
| 64 | + type: string; |
| 65 | + text: string; |
| 66 | + }>; |
| 67 | + const jsonBlock = blocks |
| 68 | + .filter((b) => b.type === "text") |
| 69 | + .map((b) => b.text) |
| 70 | + .find((t) => t.trim().startsWith("{")); |
| 71 | + if (!jsonBlock) return {}; |
| 72 | + const parsed = JSON.parse(jsonBlock); |
| 73 | + return { id: parsed?.hsg?.id, project_id: parsed?.project_id }; |
| 74 | +} |
| 75 | + |
| 76 | +describe("MCP per-tenant scoping", () => { |
| 77 | + beforeEach(async () => { |
| 78 | + await cleanup(); |
| 79 | + }); |
| 80 | + |
| 81 | + it("openmemory_store binds writes to the authenticated tenant", async () => { |
| 82 | + const { client } = await connect_client(T_ALICE); |
| 83 | + const stored = await client.callTool({ |
| 84 | + name: "openmemory_store", |
| 85 | + arguments: { |
| 86 | + content: |
| 87 | + "Nginx 502 on a fresh VM: check that the upstream service is actually running before looking at nginx config.", |
| 88 | + tags: ["nginx", "sysadmin"], |
| 89 | + }, |
| 90 | + }); |
| 91 | + const { id } = parse_store(stored); |
| 92 | + expect(id).toBeTruthy(); |
| 93 | + |
| 94 | + // The DB row must carry the tenant as user_id — without this fix |
| 95 | + // it would have been "anonymous" and invisible to REST /memory/all. |
| 96 | + const row = await q.get_mem.get(id!); |
| 97 | + expect(row).toBeTruthy(); |
| 98 | + expect(row.user_id).toBe(T_ALICE); |
| 99 | + expect(row.project_id).toBe("system_global"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("openmemory_list returns the tenant's own MCP-stored memories (regression)", async () => { |
| 103 | + // Reproduces the symptom from the bug report: a memory stored via |
| 104 | + // MCP openmemory_store must appear in MCP openmemory_list on the |
| 105 | + // same authenticated session. |
| 106 | + const { client } = await connect_client(T_ALICE); |
| 107 | + |
| 108 | + await client.callTool({ |
| 109 | + name: "openmemory_store", |
| 110 | + arguments: { |
| 111 | + content: |
| 112 | + "Nginx 502 on a fresh VM: check the upstream service is running before touching nginx config.", |
| 113 | + tags: ["nginx"], |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + const listed = await client.callTool({ |
| 118 | + name: "openmemory_list", |
| 119 | + arguments: { limit: 50 }, |
| 120 | + }); |
| 121 | + const items = parse_items(listed); |
| 122 | + expect(items.length).toBeGreaterThan(0); |
| 123 | + expect(items.every((i) => i.user_id === T_ALICE)).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + it("openmemory_list isolates tenants from each other", async () => { |
| 127 | + const alice = await connect_client(T_ALICE); |
| 128 | + const bob = await connect_client(T_BOB); |
| 129 | + |
| 130 | + await alice.client.callTool({ |
| 131 | + name: "openmemory_store", |
| 132 | + arguments: { content: "Alice's private dev notes about nginx." }, |
| 133 | + }); |
| 134 | + await bob.client.callTool({ |
| 135 | + name: "openmemory_store", |
| 136 | + arguments: { content: "Bob's private dev notes about postgres." }, |
| 137 | + }); |
| 138 | + |
| 139 | + const bob_list = parse_items( |
| 140 | + await bob.client.callTool({ |
| 141 | + name: "openmemory_list", |
| 142 | + arguments: { limit: 50 }, |
| 143 | + }), |
| 144 | + ); |
| 145 | + // Bob must not see Alice's memories. |
| 146 | + expect(bob_list.every((i) => i.user_id === T_BOB)).toBe(true); |
| 147 | + expect(bob_list.length).toBe(1); |
| 148 | + |
| 149 | + const alice_list = parse_items( |
| 150 | + await alice.client.callTool({ |
| 151 | + name: "openmemory_list", |
| 152 | + arguments: { limit: 50 }, |
| 153 | + }), |
| 154 | + ); |
| 155 | + expect(alice_list.every((i) => i.user_id === T_ALICE)).toBe(true); |
| 156 | + expect(alice_list.length).toBe(1); |
| 157 | + }); |
| 158 | + |
| 159 | + it("openmemory_store rejects a user_id arg that disagrees with the tenant", async () => { |
| 160 | + const { client } = await connect_client(T_ALICE); |
| 161 | + const result: any = await client.callTool({ |
| 162 | + name: "openmemory_store", |
| 163 | + arguments: { |
| 164 | + content: "attempt to forge another tenant's identity", |
| 165 | + user_id: T_BOB, |
| 166 | + }, |
| 167 | + }); |
| 168 | + // ToolRegistry catches errors and turns them into an isError result |
| 169 | + // with a textual "Error: ..." block. |
| 170 | + expect(result.isError).toBe(true); |
| 171 | + const text = (result.content ?? []) |
| 172 | + .map((b: any) => b.text ?? "") |
| 173 | + .join("\n"); |
| 174 | + expect(text).toMatch(/tenant_mismatch/); |
| 175 | + }); |
| 176 | + |
| 177 | + it("stdio-style server (no tenant) preserves legacy behaviour", async () => { |
| 178 | + // No tenant bound — this is the stdio MCP shape. Stored memories |
| 179 | + // get the "anonymous" fallback from add_hsg_memory and openmemory_list |
| 180 | + // returns everything in the table (the pre-existing local-dev contract). |
| 181 | + const { client } = await connect_client(undefined); |
| 182 | + const stored = await client.callTool({ |
| 183 | + name: "openmemory_store", |
| 184 | + arguments: { content: "stdio-mode memory with no tenant binding" }, |
| 185 | + }); |
| 186 | + const { id } = parse_store(stored); |
| 187 | + expect(id).toBeTruthy(); |
| 188 | + |
| 189 | + const row = await q.get_mem.get(id!); |
| 190 | + expect(row.user_id).toBe("anonymous"); |
| 191 | + |
| 192 | + const items = parse_items( |
| 193 | + await client.callTool({ |
| 194 | + name: "openmemory_list", |
| 195 | + arguments: { limit: 50 }, |
| 196 | + }), |
| 197 | + ); |
| 198 | + expect(items.length).toBe(1); |
| 199 | + expect(items[0].id).toBe(id); |
| 200 | + }); |
| 201 | +}); |
0 commit comments