|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#27745] The federation pair, `list_remote_tables` and `import_object`: an |
| 5 | + * agent lists a federated datasource's entity sets and imports one through the |
| 6 | + * same `IExternalDatasourceService` calls Studio's datasource page makes. |
| 7 | + * |
| 8 | + * Pinned here: the pair registers only for a token whose scopes name |
| 9 | + * `metadata:write` (the authoring family's gate, since an import lands object |
| 10 | + * metadata) and only on a bridge that carries both members; each call reaches |
| 11 | + * the bridge with exactly the caller's arguments; a refusal the bridge throws |
| 12 | + * comes back as the coded tool error. |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 16 | +import { MCP_OAUTH_SCOPE_DATA_READ, MCP_OAUTH_SCOPE_METADATA_WRITE } from '@objectstack/spec/ai'; |
| 17 | +import { MCPServerRuntime } from './mcp-server-runtime.js'; |
| 18 | +import type { McpDataBridge, McpFederationBridge } from './mcp-http-tools.js'; |
| 19 | +import { renderSkillMarkdown } from './skill-md.js'; |
| 20 | + |
| 21 | +const FEDERATION_TOOLS = ['import_object', 'list_remote_tables']; |
| 22 | + |
| 23 | +function makeBridge(withFederation = true): McpDataBridge & Partial<McpFederationBridge> & { calls: unknown[] } { |
| 24 | + const calls: unknown[] = []; |
| 25 | + const data: McpDataBridge = { |
| 26 | + async listObjects() { return []; }, |
| 27 | + async describeObject() { return null; }, |
| 28 | + async query() { return { records: [] }; }, |
| 29 | + async get() { return null; }, |
| 30 | + async create() { return {}; }, |
| 31 | + async update() { return {}; }, |
| 32 | + async remove() { return {}; }, |
| 33 | + }; |
| 34 | + if (!withFederation) return { ...data, calls }; |
| 35 | + return { |
| 36 | + ...data, |
| 37 | + calls, |
| 38 | + async listRemoteTables(datasource, opts) { |
| 39 | + calls.push(['listRemoteTables', datasource, opts]); |
| 40 | + if (datasource === 'locked') { |
| 41 | + throw Object.assign(new Error('Introspecting an external datasource requires the `manage_platform_settings` capability.'), { |
| 42 | + code: 'PERMISSION_DENIED', |
| 43 | + status: 403, |
| 44 | + }); |
| 45 | + } |
| 46 | + return [{ name: 'Products', columnCount: 6 }, { name: 'Categories', columnCount: 4 }]; |
| 47 | + }, |
| 48 | + async importObject(datasource, remoteName, opts) { |
| 49 | + calls.push(['importObject', datasource, remoteName, opts]); |
| 50 | + return { |
| 51 | + name: 'products', |
| 52 | + definition: { name: 'products', datasource, external: { remoteName } }, |
| 53 | + review: [], |
| 54 | + }; |
| 55 | + }, |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +async function rpc(runtime: MCPServerRuntime, bridge: unknown, grantedScopes: string[] | undefined, body: unknown) { |
| 60 | + const req = new Request('http://localhost/api/v1/mcp', { |
| 61 | + method: 'POST', |
| 62 | + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, |
| 63 | + body: JSON.stringify(body), |
| 64 | + }); |
| 65 | + const res = await runtime.handleHttpRequest(req, { |
| 66 | + bridge: bridge as McpDataBridge, |
| 67 | + parsedBody: body, |
| 68 | + ...(grantedScopes ? { toolOptions: { grantedScopes } } : {}), |
| 69 | + }); |
| 70 | + return (await res.json()) as any; |
| 71 | +} |
| 72 | + |
| 73 | +async function toolNames(runtime: MCPServerRuntime, bridge: unknown, grantedScopes?: string[]): Promise<string[]> { |
| 74 | + const json = await rpc(runtime, bridge, grantedScopes, { jsonrpc: '2.0', id: 1, method: 'tools/list' }); |
| 75 | + return (json.result?.tools ?? []).map((t: { name: string }) => t.name); |
| 76 | +} |
| 77 | + |
| 78 | +async function callTool(runtime: MCPServerRuntime, bridge: unknown, name: string, args: Record<string, unknown>) { |
| 79 | + const json = await rpc(runtime, bridge, [MCP_OAUTH_SCOPE_METADATA_WRITE], { |
| 80 | + jsonrpc: '2.0', |
| 81 | + id: 2, |
| 82 | + method: 'tools/call', |
| 83 | + params: { name, arguments: args }, |
| 84 | + }); |
| 85 | + return json.result as { content: Array<{ text: string }>; isError?: boolean }; |
| 86 | +} |
| 87 | + |
| 88 | +describe('the federation tools register behind metadata:write, on a bridge that carries them', () => { |
| 89 | + let runtime: MCPServerRuntime; |
| 90 | + beforeEach(() => { |
| 91 | + runtime = new MCPServerRuntime({ name: 't', version: '1.0.0' }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('registers both for a token naming metadata:write', async () => { |
| 95 | + const names = await toolNames(runtime, makeBridge(), [MCP_OAUTH_SCOPE_METADATA_WRITE]); |
| 96 | + expect(names.filter((n) => FEDERATION_TOOLS.includes(n)).sort()).toEqual(FEDERATION_TOOLS); |
| 97 | + }); |
| 98 | + |
| 99 | + it('registers neither for a key or a session (no scope list), nor for a token without metadata:write', async () => { |
| 100 | + for (const scopes of [undefined, [MCP_OAUTH_SCOPE_DATA_READ]]) { |
| 101 | + const names = await toolNames(runtime, makeBridge(), scopes); |
| 102 | + expect(names.filter((n) => FEDERATION_TOOLS.includes(n)), String(scopes)).toEqual([]); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + it('registers neither on a bridge without the federation members', async () => { |
| 107 | + const names = await toolNames(runtime, makeBridge(false), [MCP_OAUTH_SCOPE_METADATA_WRITE]); |
| 108 | + expect(names.filter((n) => FEDERATION_TOOLS.includes(n))).toEqual([]); |
| 109 | + }); |
| 110 | + |
| 111 | + it('annotates the listing read-only and the import as a non-destructive write', async () => { |
| 112 | + const json = await rpc(runtime, makeBridge(), [MCP_OAUTH_SCOPE_METADATA_WRITE], { jsonrpc: '2.0', id: 1, method: 'tools/list' }); |
| 113 | + const byName = new Map((json.result.tools as Array<{ name: string; annotations?: Record<string, unknown> }>).map((t) => [t.name, t])); |
| 114 | + expect(byName.get('list_remote_tables')?.annotations).toMatchObject({ readOnlyHint: true, destructiveHint: false }); |
| 115 | + expect(byName.get('import_object')?.annotations).toMatchObject({ readOnlyHint: false, destructiveHint: false }); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +describe('the federation tools reach the bridge with the caller\'s arguments', () => { |
| 120 | + let runtime: MCPServerRuntime; |
| 121 | + beforeEach(() => { |
| 122 | + runtime = new MCPServerRuntime({ name: 't', version: '1.0.0' }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('list_remote_tables answers the bridge\'s tables with their count', async () => { |
| 126 | + const bridge = makeBridge(); |
| 127 | + const result = await callTool(runtime, bridge, 'list_remote_tables', { datasource: 'erp' }); |
| 128 | + expect(result.isError).toBeFalsy(); |
| 129 | + expect(JSON.parse(result.content[0]!.text)).toEqual({ |
| 130 | + tables: [{ name: 'Products', columnCount: 6 }, { name: 'Categories', columnCount: 4 }], |
| 131 | + totalCount: 2, |
| 132 | + }); |
| 133 | + expect(bridge.calls).toEqual([['listRemoteTables', 'erp', {}]]); |
| 134 | + }); |
| 135 | + |
| 136 | + it('import_object forwards the name override and the write opt-in, and nothing it was not sent', async () => { |
| 137 | + const bridge = makeBridge(); |
| 138 | + await callTool(runtime, bridge, 'import_object', { datasource: 'erp', remoteName: 'Products' }); |
| 139 | + await callTool(runtime, bridge, 'import_object', { |
| 140 | + datasource: 'erp', |
| 141 | + remoteName: 'Products', |
| 142 | + name: 'erp_products', |
| 143 | + writable: true, |
| 144 | + }); |
| 145 | + expect(bridge.calls).toEqual([ |
| 146 | + ['importObject', 'erp', 'Products', {}], |
| 147 | + ['importObject', 'erp', 'Products', { name: 'erp_products', writable: true }], |
| 148 | + ]); |
| 149 | + }); |
| 150 | + |
| 151 | + it('refuses an unknown argument by name instead of dropping it', async () => { |
| 152 | + const bridge = makeBridge(); |
| 153 | + const result = await callTool(runtime, bridge, 'import_object', { datasource: 'erp', entitySet: 'Products' }); |
| 154 | + expect(result.isError).toBe(true); |
| 155 | + expect(result.content[0]!.text).toContain('remoteName'); |
| 156 | + expect(bridge.calls).toEqual([]); |
| 157 | + }); |
| 158 | + |
| 159 | + it('answers a refusal the bridge throws as the coded tool error', async () => { |
| 160 | + const result = await callTool(runtime, makeBridge(), 'list_remote_tables', { datasource: 'locked' }); |
| 161 | + expect(result.isError).toBe(true); |
| 162 | + expect(JSON.parse(result.content[0]!.text).error).toMatchObject({ code: 'PERMISSION_DENIED', status: 403 }); |
| 163 | + }); |
| 164 | +}); |
| 165 | + |
| 166 | +describe('the generated SKILL.md teaches the pair exactly where it registers', () => { |
| 167 | + it('names both for a metadata:write grant on a host that carries them, and neither otherwise', () => { |
| 168 | + const granted = renderSkillMarkdown({ grantedScopes: [MCP_OAUTH_SCOPE_METADATA_WRITE], federationTools: true }); |
| 169 | + for (const name of FEDERATION_TOOLS) expect(granted).toContain(`**${name}(`); |
| 170 | + for (const options of [ |
| 171 | + { federationTools: true }, |
| 172 | + { grantedScopes: [MCP_OAUTH_SCOPE_DATA_READ], federationTools: true }, |
| 173 | + { grantedScopes: [MCP_OAUTH_SCOPE_METADATA_WRITE] }, |
| 174 | + ]) { |
| 175 | + const skill = renderSkillMarkdown(options); |
| 176 | + for (const name of FEDERATION_TOOLS) expect(skill).not.toContain(name); |
| 177 | + } |
| 178 | + }); |
| 179 | +}); |
0 commit comments