|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// MCP business-action E2E — Community-Edition path. |
| 4 | +// |
| 5 | +// Proves that a self-host runtime composing ONLY the open framework |
| 6 | +// (@objectstack/runtime + objectql + a driver + the seeded app) and |
| 7 | +// @objectstack/mcp — NO @objectstack/service-ai, NO cloud studio — exposes MCP |
| 8 | +// tools that LIST and EXECUTE the app's business actions, permission-enforced. |
| 9 | +// |
| 10 | +// We boot the real ObjectQL engine, register app-todo's real action handlers, |
| 11 | +// then drive the real MCPServerRuntime over JSON-RPC (the same code path an |
| 12 | +// external MCP client hits) through the runtime's principal-bound action |
| 13 | +// bridge. `run_action` flows through engine.executeAction → the registered |
| 14 | +// handler → the real driver, exactly as in production. |
| 15 | +// |
| 16 | +// ## The organization the boot holds (ADR-0131 D3 / D9) |
| 17 | +// |
| 18 | +// Every `todo_task` row carries a NOT NULL organization. A real `single` |
| 19 | +// install creates its Default Organization at boot, before the app's seed |
| 20 | +// loads, and every organization-less system write is stamped with it. That is |
| 21 | +// the job of the plugin that registers the organization object: `AuthPlugin` |
| 22 | +// on a server, `createIdentityObjectsPlugin()` on a reduced kernel like this |
| 23 | +// one — plugin-auth's identity objects and the same boot invariant, with no |
| 24 | +// authentication mounted. A kernel with neither registers no organization |
| 25 | +// object, so the seed and every system insert here were refused with |
| 26 | +// "Organization is required". The suite boots the way the platform documents a |
| 27 | +// reduced kernel, and pins that the app's own seed landed. |
| 28 | +// |
| 29 | +// It runs in this example's own `pnpm test`, so CI sees it. |
| 30 | + |
| 31 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 32 | +import { ObjectKernel, DriverPlugin, AppPlugin } from '@objectstack/runtime'; |
| 33 | +import { HttpDispatcher } from '@objectstack/runtime'; |
| 34 | +import { SqliteWasmDriver } from '@objectstack/driver-sqlite-wasm'; |
| 35 | +import { ObjectQLPlugin } from '@objectstack/objectql'; |
| 36 | +import { MCPServerRuntime } from '@objectstack/mcp'; |
| 37 | +import { createIdentityObjectsPlugin } from '@objectstack/plugin-auth'; |
| 38 | +import TodoApp from '../objectstack.config.js'; |
| 39 | +import { registerTaskActionHandlers } from '../src/actions/register-handlers.js'; |
| 40 | + |
| 41 | +const BOOT_TIMEOUT = 60_000; |
| 42 | + |
| 43 | +/** Build a JSON-RPC request the MCP Streamable-HTTP transport accepts. */ |
| 44 | +function mcpRequest(body: unknown): Request { |
| 45 | + return new Request('http://localhost/api/v1/mcp', { |
| 46 | + method: 'POST', |
| 47 | + headers: { |
| 48 | + 'content-type': 'application/json', |
| 49 | + accept: 'application/json, text/event-stream', |
| 50 | + }, |
| 51 | + body: JSON.stringify(body), |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +const toolsCall = (id: number, name: string, args: Record<string, unknown>) => ({ |
| 56 | + jsonrpc: '2.0', id, method: 'tools/call', params: { name, arguments: args }, |
| 57 | +}); |
| 58 | + |
| 59 | +/** The app's merged stack with one action of `todo_task` rewritten. */ |
| 60 | +function withTaskAction(objects: any[], actionName: string, rewrite: (action: any) => any): any[] { |
| 61 | + return objects.map((o) => |
| 62 | + o.name !== 'todo_task' |
| 63 | + ? o |
| 64 | + : { ...o, actions: o.actions.map((a: any) => (a.name === actionName ? rewrite(a) : a)) }, |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +describe('app-todo business actions over MCP (CE runtime, no service-ai)', () => { |
| 69 | + let kernel: ObjectKernel; |
| 70 | + let engine: any; |
| 71 | + let mergedObjects: any[]; |
| 72 | + let bridgeFor: (executionContext: any, objects?: any[]) => any; |
| 73 | + const runtime = new MCPServerRuntime({ name: 'objectstack', version: '1.0.0' }); |
| 74 | + |
| 75 | + const callMcp = async (bridge: any, body: unknown): Promise<any> => { |
| 76 | + const res = await runtime.handleHttpRequest(mcpRequest(body), { bridge, parsedBody: body }); |
| 77 | + return res.json(); |
| 78 | + }; |
| 79 | + const listActions = async (bridge: any, id: number): Promise<any[]> => |
| 80 | + JSON.parse((await callMcp(bridge, toolsCall(id, 'list_actions', {}))).result.content[0].text).actions; |
| 81 | + const seedTask = async (subject: string): Promise<string> => { |
| 82 | + const seeded: any = await engine.insert('todo_task', { subject, status: 'not_started', priority: 'high' }); |
| 83 | + const id = seeded?.id ?? seeded?.record?.id; |
| 84 | + expect(id, `seeded '${subject}'`).toBeTruthy(); |
| 85 | + return id; |
| 86 | + }; |
| 87 | + const statusOf = async (id: string): Promise<unknown> => |
| 88 | + (await engine.find('todo_task', { where: { id } }))?.[0]?.status; |
| 89 | + |
| 90 | + // The acting user — an authenticated, non-system principal. |
| 91 | + const user = { userId: 'user_1', positions: [], permissions: [], systemPermissions: [] }; |
| 92 | + |
| 93 | + beforeAll(async () => { |
| 94 | + // ── Boot the real open-framework runtime ────────────────────────── |
| 95 | + kernel = new ObjectKernel({ logger: { level: 'silent' } } as any); |
| 96 | + await kernel.use(new ObjectQLPlugin()); |
| 97 | + await kernel.use(new DriverPlugin(new SqliteWasmDriver({ filename: ':memory:' }))); |
| 98 | + // The organization object and the Default Organization; see the header. |
| 99 | + await kernel.use(createIdentityObjectsPlugin()); |
| 100 | + await kernel.use(new AppPlugin(TodoApp)); |
| 101 | + await kernel.bootstrap(); |
| 102 | + |
| 103 | + // app-todo wires handlers via a named `onEnable` export that the default |
| 104 | + // AppPlugin import misses — register them directly against the engine. |
| 105 | + engine = await (kernel as any).getServiceAsync('data'); |
| 106 | + if (!engine) throw new Error('data engine not available'); |
| 107 | + registerTaskActionHandlers(engine); |
| 108 | + |
| 109 | + // defineStack() merges actions[] into their object by `objectName`; this is |
| 110 | + // exactly what a real metadata service returns. Surface it to the dispatcher. |
| 111 | + mergedObjects = (TodoApp.objects ?? []) as any[]; |
| 112 | + |
| 113 | + // A metadata service backed by the app's own merged stack. In a full |
| 114 | + // deployment MetadataPlugin returns these same objects; the action mechanism |
| 115 | + // under test (resolve → gate → executeAction → handler → driver) is 100% real. |
| 116 | + const makeMetadata = (objects: any[]) => ({ |
| 117 | + listObjects: async () => objects, |
| 118 | + getObject: async (n: string) => objects.find((o) => o.name === n), |
| 119 | + }); |
| 120 | + |
| 121 | + // Build a principal-bound MCP action bridge for a given user + metadata view. |
| 122 | + bridgeFor = (executionContext: any, objects: any[] = mergedObjects) => { |
| 123 | + const metadata = makeMetadata(objects); |
| 124 | + const fakeKernel: any = { |
| 125 | + context: { |
| 126 | + getService: (n: string) => |
| 127 | + n === 'objectql' || n === 'data' ? engine : n === 'metadata' ? metadata : null, |
| 128 | + }, |
| 129 | + }; |
| 130 | + const dispatcher = new HttpDispatcher(fakeKernel); |
| 131 | + return (dispatcher as any).buildMcpBridge({ executionContext, environmentId: undefined }); |
| 132 | + }; |
| 133 | + }, BOOT_TIMEOUT); |
| 134 | + |
| 135 | + afterAll(async () => { |
| 136 | + await kernel?.shutdown(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('boots with its Default Organization, so the app seed landed', async () => { |
| 140 | + const organizations: any[] = await engine.find('sys_organization', {}, { context: { isSystem: true } }); |
| 141 | + expect(organizations.length).toBe(1); |
| 142 | + const subjects = ((await engine.find('todo_task', {})) as any[]).map((t) => t.subject); |
| 143 | + expect(subjects).toEqual(expect.arrayContaining(['Learn ObjectStack', 'Build a cool app'])); |
| 144 | + }); |
| 145 | + |
| 146 | + it('declares its actions on todo_task', () => { |
| 147 | + const todo = mergedObjects.find((o) => o.name === 'todo_task'); |
| 148 | + expect(Array.isArray(todo?.actions) && todo.actions.length > 0).toBe(true); |
| 149 | + }); |
| 150 | + |
| 151 | + // ── Step 1 — the MCP server advertises the action tools ─────────── |
| 152 | + it('step 1: tools/list advertises list_actions + run_action', async () => { |
| 153 | + const list = await callMcp(bridgeFor(user), { jsonrpc: '2.0', id: 1, method: 'tools/list' }); |
| 154 | + const toolNames: string[] = list.result.tools.map((t: any) => t.name); |
| 155 | + expect(toolNames).toContain('list_actions'); |
| 156 | + expect(toolNames).toContain('run_action'); |
| 157 | + // Object tools remain exposed alongside them. |
| 158 | + expect(toolNames).toContain('query_records'); |
| 159 | + }); |
| 160 | + |
| 161 | + // ── Step 2 — list_actions enumerates the app's business actions ─── |
| 162 | + it('step 2: list_actions enumerates invokable business actions', async () => { |
| 163 | + const actions = await listActions(bridgeFor(user), 2); |
| 164 | + const actionNames = actions.map((a) => a.name); |
| 165 | + expect(actionNames).toContain('todo_complete_task'); |
| 166 | + // A modal/UI-only action is not invokable, so not listed. |
| 167 | + expect(actionNames).not.toContain('todo_defer_task'); |
| 168 | + expect(actions.find((a) => a.name === 'todo_complete_task')?.requiresRecord).toBe(true); |
| 169 | + // `variant: 'danger'` is reported as requiresConfirmation in the listing. |
| 170 | + expect(actions.find((a) => a.name === 'todo_delete_completed')?.requiresConfirmation).toBe(true); |
| 171 | + }); |
| 172 | + |
| 173 | + // ── Step 3 — run_action EXECUTES real business logic ────────────── |
| 174 | + it('step 3: run_action executes todo_complete_task against a real record', async () => { |
| 175 | + const taskId = await seedTask('Ship MCP actions'); |
| 176 | + const ran = await callMcp(bridgeFor(user), toolsCall(3, 'run_action', { actionName: 'todo_complete_task', recordId: taskId })); |
| 177 | + expect(ran.result?.isError, ran.result?.content?.[0]?.text).not.toBe(true); |
| 178 | + expect(JSON.parse(ran.result.content[0].text).ok).toBe(true); |
| 179 | + // The handler ran. |
| 180 | + expect(await statusOf(taskId)).toBe('completed'); |
| 181 | + }); |
| 182 | + |
| 183 | + // ── Step 4 — fail-closed on system-object actions ───────────────── |
| 184 | + it('step 4: system-object actions are blocked fail-closed', async () => { |
| 185 | + const sysRun = await callMcp(bridgeFor(user), toolsCall(4, 'run_action', { actionName: 'rotate', objectName: 'sys_api_key' })); |
| 186 | + expect(sysRun.result?.isError).toBe(true); |
| 187 | + expect(sysRun.result?.content?.[0]?.text ?? '').toMatch(/system object/i); |
| 188 | + }); |
| 189 | + |
| 190 | + // ── Step 5 — capability gate (ADR-0066 D4) end-to-end ───────────── |
| 191 | + it('step 5: the requiredPermissions gate denies, then allows', async () => { |
| 192 | + const taskId = await seedTask('Clone me'); |
| 193 | + // Same app, but todo_clone_task now declares a capability requirement. |
| 194 | + const gatedObjects = withTaskAction(mergedObjects, 'todo_clone_task', (a) => ({ ...a, requiredPermissions: ['todo_admin'] })); |
| 195 | + |
| 196 | + const denyBridge = bridgeFor({ userId: 'user_2', positions: [], permissions: [], systemPermissions: [] }, gatedObjects); |
| 197 | + const denied = await callMcp(denyBridge, toolsCall(5, 'run_action', { actionName: 'todo_clone_task', recordId: taskId })); |
| 198 | + expect(denied.result?.isError).toBe(true); |
| 199 | + expect(denied.result?.content?.[0]?.text ?? '').toMatch(/requires capability/i); |
| 200 | + // …and list_actions hides what the caller may not run. |
| 201 | + expect((await listActions(denyBridge, 6)).some((a) => a.name === 'todo_clone_task')).toBe(false); |
| 202 | + |
| 203 | + const allowBridge = bridgeFor({ userId: 'admin_1', positions: [], permissions: [], systemPermissions: ['todo_admin'] }, gatedObjects); |
| 204 | + const allowed = await callMcp(allowBridge, toolsCall(7, 'run_action', { actionName: 'todo_clone_task', recordId: taskId })); |
| 205 | + expect(allowed.result?.isError, allowed.result?.content?.[0]?.text).not.toBe(true); |
| 206 | + expect((await listActions(allowBridge, 8)).some((a) => a.name === 'todo_clone_task')).toBe(true); |
| 207 | + }); |
| 208 | + |
| 209 | + // ── Step 6 — AI-exposure gate (#2849) end-to-end ─────────────────── |
| 210 | + it('step 6: an action without ai.exposed is hidden and uninvokable', async () => { |
| 211 | + const taskId = await seedTask('Not for AI'); |
| 212 | + // Same app, but todo_clone_task no longer opts into the AI surface. |
| 213 | + const unexposedObjects = withTaskAction(mergedObjects, 'todo_clone_task', ({ ai: _ai, ...rest }: any) => rest); |
| 214 | + const unexposedBridge = bridgeFor({ userId: 'user_3', positions: [], permissions: [], systemPermissions: ['todo_admin'] }, unexposedObjects); |
| 215 | + expect((await listActions(unexposedBridge, 9)).some((a) => a.name === 'todo_clone_task')).toBe(false); |
| 216 | + const unexposedRun = await callMcp(unexposedBridge, toolsCall(10, 'run_action', { actionName: 'todo_clone_task', recordId: taskId })); |
| 217 | + expect(unexposedRun.result?.isError).toBe(true); |
| 218 | + expect(unexposedRun.result?.content?.[0]?.text ?? '').toMatch(/not exposed to AI/i); |
| 219 | + }); |
| 220 | + |
| 221 | + // ── Step 7 — the confirmation gate (#15942), BOTH directions ─────── |
| 222 | + // |
| 223 | + // The one drive that spans the whole path. A unit test on either side is |
| 224 | + // blind to the other: `invokeBusinessAction` called directly never sees the |
| 225 | + // MCP door strip the member (the SDK's shape wrap drops unknown keys, and |
| 226 | + // the handler forwards a rebuilt object), and a door test with a stubbed |
| 227 | + // bridge never sees the runtime gate. Here the member travels from a real |
| 228 | + // JSON-RPC `tools/call`, through both of those layers, into the real gate — |
| 229 | + // and the record afterwards says whether anything ran. |
| 230 | + it('step 7: ai.requiresConfirmation is ENFORCED, and satisfiable', async () => { |
| 231 | + // Same app, but todo_complete_task now declares the author's gate. |
| 232 | + const gatedConfirmObjects = withTaskAction(mergedObjects, 'todo_complete_task', (a) => ({ |
| 233 | + ...a, |
| 234 | + ai: { ...(a.ai ?? {}), requiresConfirmation: true }, |
| 235 | + })); |
| 236 | + const confirmBridge = bridgeFor(user, gatedConfirmObjects); |
| 237 | + |
| 238 | + // The listing tells a client the gate is there (unchanged behaviour). |
| 239 | + const gatedList = await listActions(confirmBridge, 11); |
| 240 | + expect(gatedList.find((a) => a.name === 'todo_complete_task')?.requiresConfirmation).toBe(true); |
| 241 | + |
| 242 | + const gatedId = await seedTask('Needs a human'); |
| 243 | + |
| 244 | + // 7a — WITHOUT the member: refused, with the declared code, and NOTHING ran. |
| 245 | + const refused = await callMcp(confirmBridge, toolsCall(12, 'run_action', { actionName: 'todo_complete_task', recordId: gatedId })); |
| 246 | + expect(refused.result?.isError).toBe(true); |
| 247 | + const refusedEnvelope = JSON.parse(refused.result?.content?.[0]?.text ?? '{}'); |
| 248 | + expect(refusedEnvelope?.error?.code).toBe('ACTION_CONFIRMATION_REQUIRED'); |
| 249 | + expect(refusedEnvelope?.error?.status).toBe(428); |
| 250 | + expect(refusedEnvelope?.error?.details?.actionName).toBe('todo_complete_task'); |
| 251 | + expect(refusedEnvelope?.error?.details?.confirmationMember).toBe('confirm'); |
| 252 | + expect(await statusOf(gatedId)).toBe('not_started'); |
| 253 | + |
| 254 | + // 7b — WITH the member: the identical call succeeds and the handler runs. |
| 255 | + // This is the leg that proves the member is not stripped: before the door |
| 256 | + // grew it, this call was refused exactly like 7a and the action was |
| 257 | + // permanently un-invokable. |
| 258 | + const confirmed = await callMcp(confirmBridge, toolsCall(13, 'run_action', { actionName: 'todo_complete_task', recordId: gatedId, confirm: true })); |
| 259 | + expect(confirmed.result?.isError, confirmed.result?.content?.[0]?.text).not.toBe(true); |
| 260 | + expect(await statusOf(gatedId)).toBe('completed'); |
| 261 | + |
| 262 | + // 7c — the heuristic must NOT gate: todo_delete_completed is `variant:'danger'` |
| 263 | + // and the listing calls it requiresConfirmation, but its author declared |
| 264 | + // nothing, so it stays invokable with no member (this change narrows a |
| 265 | + // published accept set; refusing here would be the regression). |
| 266 | + const undeclared = await callMcp(confirmBridge, toolsCall(14, 'run_action', { actionName: 'todo_delete_completed' })); |
| 267 | + expect(undeclared.result?.isError, undeclared.result?.content?.[0]?.text).not.toBe(true); |
| 268 | + }); |
| 269 | +}); |
0 commit comments