|
20 | 20 | */ |
21 | 21 |
|
22 | 22 | import { describe, it, expect, vi } from 'vitest'; |
| 23 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 24 | +import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'; |
| 25 | +import { recordNotFoundError } from '@objectstack/core'; |
23 | 26 | import type { ExecutionContext } from '@objectstack/spec/kernel'; |
24 | 27 | import type { IDataEngine, IMetadataService } from '@objectstack/spec/contracts'; |
25 | 28 | import { createStdioDataBridge } from './stdio-data-bridge.js'; |
| 29 | +import { MCPServerRuntime } from './mcp-server-runtime.js'; |
| 30 | +import type { McpDataBridge } from './mcp-http-tools.js'; |
26 | 31 | import { assertEngineFindOnePredicate, type EngineFindOneQueryInput } from '@objectstack/metadata-core'; |
27 | 32 |
|
28 | 33 | /** An engine that resolves NO row for any id — every by-id write is a miss. */ |
@@ -124,3 +129,148 @@ describe('#8422 stdio bridge by-id writes throw the shared not-found envelope', |
124 | 129 | expect(updateErr?.code).toBe('RECORD_NOT_FOUND'); |
125 | 130 | }); |
126 | 131 | }); |
| 132 | + |
| 133 | +// --------------------------------------------------------------------------- |
| 134 | +// The tool door: `get_record` over the long-lived server stdio attaches |
| 135 | +// --------------------------------------------------------------------------- |
| 136 | + |
| 137 | +/** |
| 138 | + * One row, `secret1`, that only its owner reads: the engine double answers the |
| 139 | + * row to `owner` and nothing to anyone else, which is what the security |
| 140 | + * middleware's row filter does to a by-id read the caller may not see. So for |
| 141 | + * {@link PRINCIPAL} `secret1` is HIDDEN and `ghost` is MISSING, and the two |
| 142 | + * reach the bridge as the same empty read. |
| 143 | + */ |
| 144 | +function makeRowFilteringEngine() { |
| 145 | + const row = { id: 'secret1', title: 'owner only' }; |
| 146 | + const visibleTo = (opts?: { context?: { userId?: string } }) => opts?.context?.userId === 'owner'; |
| 147 | + // The file's one engine double, with its by-id read made row-filtering. |
| 148 | + const engine = makeEmptyEngine(); |
| 149 | + const find = vi.fn(async (_object: string, query?: { where?: { id?: unknown } }, opts?: { context?: { userId?: string } }) => |
| 150 | + query?.where?.id === row.id && visibleTo(opts) ? [row] : []); |
| 151 | + return Object.assign(engine, { find }); |
| 152 | +} |
| 153 | + |
| 154 | +function stdioBridgeAs(principal: ExecutionContext) { |
| 155 | + const engine = makeRowFilteringEngine(); |
| 156 | + const bridge = createStdioDataBridge({ |
| 157 | + engine: engine as unknown as IDataEngine, |
| 158 | + metadataService: makeMetadata() as unknown as IMetadataService, |
| 159 | + resolvePrincipal: async () => principal, |
| 160 | + }); |
| 161 | + return { bridge, engine }; |
| 162 | +} |
| 163 | + |
| 164 | +/** A `tools/call` on the long-lived server (`runtime.server`, the one `start()` attaches stdio to). */ |
| 165 | +async function callOnLongLivedServer(bridge: McpDataBridge, name: string, args: Record<string, unknown>) { |
| 166 | + const runtime = new MCPServerRuntime({ name: 'pin', version: '0.0.0' }); |
| 167 | + runtime.bridgeDataTools(bridge); |
| 168 | + const [clientSide, serverSide] = InMemoryTransport.createLinkedPair(); |
| 169 | + await runtime.server.connect(serverSide); |
| 170 | + const client = new Client({ name: 'pin', version: '0.0.0' }); |
| 171 | + await client.connect(clientSide); |
| 172 | + try { |
| 173 | + return await client.callTool({ name, arguments: args }); |
| 174 | + } finally { |
| 175 | + await client.close(); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +/** |
| 180 | + * The HTTP `/mcp` door's answer to the same call: its bridge's `get` reaches |
| 181 | + * `protocol.getData` through `callData`, which throws `recordNotFoundError` |
| 182 | + * for an id that names no row the caller may read (pinned as the HTTP face's |
| 183 | + * answer in `mcp-coded-refusal.test.ts`). Driven through `handleHttpRequest`, |
| 184 | + * the real Streamable HTTP transport, so the two answers compared below come |
| 185 | + * from the two doors' own tool registrations. |
| 186 | + */ |
| 187 | +async function getRecordOverHttp(objectName: string, recordId: string) { |
| 188 | + const bridge: McpDataBridge = { |
| 189 | + async listObjects() { return [{ name: 'task', label: 'Task', fieldCount: 0 }]; }, |
| 190 | + async describeObject() { return null; }, |
| 191 | + async query(object) { return { object, records: [], total: 0 }; }, |
| 192 | + async get(object, id) { throw recordNotFoundError(object, id); }, |
| 193 | + async create(object, data) { return { object, id: 'n1', record: data }; }, |
| 194 | + async update(object, id, data) { return { object, id, record: data }; }, |
| 195 | + async remove(object, id) { return { object, id, success: true }; }, |
| 196 | + }; |
| 197 | + const body = { jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name: 'get_record', arguments: { objectName, recordId } } }; |
| 198 | + const res = await new MCPServerRuntime({ name: 'pin', version: '0.0.0' }).handleHttpRequest( |
| 199 | + new Request('http://localhost/api/v1/mcp', { |
| 200 | + method: 'POST', |
| 201 | + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, |
| 202 | + body: JSON.stringify(body), |
| 203 | + }), |
| 204 | + { bridge, parsedBody: body }, |
| 205 | + ); |
| 206 | + expect(res.status).toBe(200); |
| 207 | + return ((await res.json()) as { result: unknown }).result; |
| 208 | +} |
| 209 | + |
| 210 | +/** The coded envelope `get_record` answers a miss with, as both doors put it on the wire. */ |
| 211 | +function notFoundWire(object: string, id: string) { |
| 212 | + return { error: { code: 'RECORD_NOT_FOUND', message: `Record ${id} not found in ${object}`, status: 404 } }; |
| 213 | +} |
| 214 | + |
| 215 | +describe('stdio get_record answers a missing or hidden id with the coded RECORD_NOT_FOUND / 404', () => { |
| 216 | + it('a MISSING id: isError, and the text is the coded envelope, not a bare sentence', async () => { |
| 217 | + const { bridge } = stdioBridgeAs(PRINCIPAL); |
| 218 | + const result = (await callOnLongLivedServer(bridge, 'get_record', { objectName: 'task', recordId: 'ghost' })) as { |
| 219 | + isError?: boolean; |
| 220 | + content: Array<{ text: string }>; |
| 221 | + }; |
| 222 | + expect(result.isError).toBe(true); |
| 223 | + expect(JSON.parse(result.content[0]!.text)).toEqual(notFoundWire('task', 'ghost')); |
| 224 | + }); |
| 225 | + |
| 226 | + it('a HIDDEN id answers the same envelope as a missing one: no existence oracle', async () => { |
| 227 | + const { bridge, engine } = stdioBridgeAs(PRINCIPAL); |
| 228 | + const hidden = (await callOnLongLivedServer(bridge, 'get_record', { objectName: 'task', recordId: 'secret1' })) as { |
| 229 | + isError?: boolean; |
| 230 | + content: Array<{ text: string }>; |
| 231 | + }; |
| 232 | + expect(hidden.isError).toBe(true); |
| 233 | + expect(JSON.parse(hidden.content[0]!.text)).toEqual(notFoundWire('task', 'secret1')); |
| 234 | + // The read ran as the caller, so the row filter is what hid it. |
| 235 | + expect(engine.find).toHaveBeenCalledWith('task', expect.objectContaining({ where: { id: 'secret1' } }), { context: PRINCIPAL }); |
| 236 | + }); |
| 237 | + |
| 238 | + it('CONTROL: the hidden row is served to its owner, so the refusal above is the row filter, not a missing row', async () => { |
| 239 | + const { bridge } = stdioBridgeAs({ userId: 'owner', isSystem: false } as unknown as ExecutionContext); |
| 240 | + const served = (await callOnLongLivedServer(bridge, 'get_record', { objectName: 'task', recordId: 'secret1' })) as { |
| 241 | + isError?: boolean; |
| 242 | + content: Array<{ text: string }>; |
| 243 | + }; |
| 244 | + expect(served.isError).toBeFalsy(); |
| 245 | + expect(JSON.parse(served.content[0]!.text)).toEqual({ id: 'secret1', title: 'owner only' }); |
| 246 | + }); |
| 247 | + |
| 248 | + it('PARITY: the stdio answer equals the HTTP /mcp answer for the same call, byte for byte', async () => { |
| 249 | + for (const recordId of ['ghost', 'secret1']) { |
| 250 | + const { bridge } = stdioBridgeAs(PRINCIPAL); |
| 251 | + const overStdio = await callOnLongLivedServer(bridge, 'get_record', { objectName: 'task', recordId }); |
| 252 | + const overHttp = await getRecordOverHttp('task', recordId); |
| 253 | + expect(overStdio, `get_record ${recordId}`).toEqual(overHttp); |
| 254 | + } |
| 255 | + }); |
| 256 | + |
| 257 | + it('update_record and delete_record on the same ids answer the same coded envelope (already coded, held here)', async () => { |
| 258 | + for (const recordId of ['ghost', 'secret1']) { |
| 259 | + const { bridge } = stdioBridgeAs(PRINCIPAL); |
| 260 | + const updated = (await callOnLongLivedServer(bridge, 'update_record', { |
| 261 | + objectName: 'task', |
| 262 | + recordId, |
| 263 | + data: { title: 'x' }, |
| 264 | + })) as { isError?: boolean; content: Array<{ text: string }> }; |
| 265 | + expect(updated.isError).toBe(true); |
| 266 | + expect(JSON.parse(updated.content[0]!.text)).toEqual(notFoundWire('task', recordId)); |
| 267 | + const deleted = (await callOnLongLivedServer(bridge, 'delete_record', { |
| 268 | + objectName: 'task', |
| 269 | + recordId, |
| 270 | + confirm: true, |
| 271 | + })) as { isError?: boolean; content: Array<{ text: string }> }; |
| 272 | + expect(deleted.isError).toBe(true); |
| 273 | + expect(JSON.parse(deleted.content[0]!.text)).toEqual(notFoundWire('task', recordId)); |
| 274 | + } |
| 275 | + }); |
| 276 | +}); |
0 commit comments