|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// ADR-0097 §6 acceptance, `provider: 'mcp'` form (#3056; deferred from #3017): |
| 4 | +// a declarative `connectors:` entry pointing at an MCP server — here the |
| 5 | +// in-repo stdio fixture (examples/app-showcase/scripts/mcp-fixture.mjs) — is |
| 6 | +// materialized at boot into a live connector (spawn → tools/list → actions) |
| 7 | +// and dispatched end-to-end by a flow `connector_action`. Also pins: |
| 8 | +// - #3055: the spawn only happens because the host allowlists `node` via |
| 9 | +// `declarativeStdio` (remove it and boot fails loudly); |
| 10 | +// - the GET /connectors surface: origin 'declarative' + state 'ready' for |
| 11 | +// all three generic-executor instances (rest / openapi / mcp). |
| 12 | +// |
| 13 | +// cwd note: the fixture command (`node ./scripts/mcp-fixture.mjs`) and the |
| 14 | +// openapi instance's file-path spec both resolve relative to the app root — |
| 15 | +// exactly how `os dev`/`serve` run — so the boot chdirs there for its |
| 16 | +// duration (vitest gives each test FILE its own process, so this is isolated). |
| 17 | + |
| 18 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 19 | +import { fileURLToPath } from 'node:url'; |
| 20 | +import showcaseStack from '@objectstack/example-showcase'; |
| 21 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 22 | +import { ConnectorMcpPlugin } from '@objectstack/connector-mcp'; |
| 23 | +import { ConnectorOpenApiPlugin } from '@objectstack/connector-openapi'; |
| 24 | +import { ConnectorRestPlugin } from '@objectstack/connector-rest'; |
| 25 | + |
| 26 | +const SHOWCASE_DIR = fileURLToPath(new URL('../../../examples/app-showcase/', import.meta.url)); |
| 27 | + |
| 28 | +interface ConnectorDescriptor { |
| 29 | + name: string; |
| 30 | + origin?: string; |
| 31 | + state?: string; |
| 32 | + degradedReason?: string; |
| 33 | + actions?: Array<{ key: string }>; |
| 34 | +} |
| 35 | + |
| 36 | +describe('showcase declarative MCP connector — ADR-0097 §6 acceptance (#3056)', () => { |
| 37 | + let stack: VerifyStack; |
| 38 | + let token: string; |
| 39 | + let prevCwd: string; |
| 40 | + |
| 41 | + beforeAll(async () => { |
| 42 | + prevCwd = process.cwd(); |
| 43 | + process.chdir(SHOWCASE_DIR); |
| 44 | + // The three generic executors, exactly as objectstack.config.ts wires |
| 45 | + // them (bootStack does not register a stack's `plugins:` — it mirrors |
| 46 | + // the service pairs only — so the harness injects them here). |
| 47 | + stack = await bootStack(showcaseStack, { |
| 48 | + automation: true, |
| 49 | + extraPlugins: [ |
| 50 | + new ConnectorRestPlugin(), |
| 51 | + new ConnectorOpenApiPlugin(), |
| 52 | + new ConnectorMcpPlugin({ declarativeStdio: ['node'] }), |
| 53 | + ], |
| 54 | + }); |
| 55 | + token = await stack.signIn(); |
| 56 | + }, 120_000); |
| 57 | + |
| 58 | + afterAll(async () => { |
| 59 | + await stack?.stop(); |
| 60 | + process.chdir(prevCwd); |
| 61 | + }); |
| 62 | + |
| 63 | + it('materializes all three generic-executor instances at boot — mcp included, state ready', async () => { |
| 64 | + const res = await stack.apiAs(token, 'GET', '/automation/connectors'); |
| 65 | + expect(res.status).toBeLessThan(300); |
| 66 | + const body = (await res.json()) as { data?: { connectors?: ConnectorDescriptor[] } }; |
| 67 | + const connectors = body.data?.connectors ?? []; |
| 68 | + const byName = Object.fromEntries(connectors.map((c) => [c.name, c])); |
| 69 | + |
| 70 | + // The MCP instance: fixture spawned, tools/list mapped to actions. |
| 71 | + const mcp = byName['showcase_mcp_tools']; |
| 72 | + expect(mcp, `showcase_mcp_tools missing from registry: ${JSON.stringify(Object.keys(byName))}`).toBeDefined(); |
| 73 | + expect(mcp.origin).toBe('declarative'); |
| 74 | + expect(mcp.state, `mcp instance degraded: ${mcp.degradedReason ?? ''}`).toBe('ready'); |
| 75 | + expect(mcp.actions?.map((a) => a.key)).toContain('echo_upper'); |
| 76 | + |
| 77 | + // Its rest / openapi siblings (ADR-0097 + #3016) on the same surface. |
| 78 | + expect(byName['showcase_status_api']?.state).toBe('ready'); |
| 79 | + expect(byName['showcase_status_openapi']?.state).toBe('ready'); |
| 80 | + |
| 81 | + // The catalog descriptor stays inert (never reaches this registry). |
| 82 | + expect(byName['showcase_erp_catalog']).toBeUndefined(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('dispatches the MCP tool end-to-end through the flow connector_action', async () => { |
| 86 | + const res = await stack.apiAs(token, 'POST', '/automation/showcase_mcp_connector_echo/trigger', {}); |
| 87 | + expect(res.status, `trigger failed: ${res.status} ${await res.clone().text()}`).toBeLessThan(300); |
| 88 | + const body = (await res.json()) as { |
| 89 | + success?: boolean; |
| 90 | + data?: { success?: boolean; error?: string; output?: Record<string, unknown> }; |
| 91 | + }; |
| 92 | + expect(body.success).toBe(true); |
| 93 | + expect(body.data?.success, `flow run failed: ${JSON.stringify(body.data)}`).toBe(true); |
| 94 | + // The fixture's tools/call result round-tripped into the run output. |
| 95 | + expect(JSON.stringify(body.data?.output ?? body.data)).toContain('OBJECTSTACK'); |
| 96 | + }); |
| 97 | +}); |
0 commit comments