|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#34686] The open server's HTTP `initialize` instructions carry the |
| 5 | + * metric-first selection rule (ADR-0149 D3) and the published skills install |
| 6 | + * line (#34659 ruling D5 = A). |
| 7 | + * |
| 8 | + * - The rule rides only where this request registers BOTH dataset tools: the |
| 9 | + * same gate `registerDatasetTools` reads (`datasetToolDescriptions`), so a |
| 10 | + * grant without `data:read`, a bridge without the dataset members, and a |
| 11 | + * bridge that defines no description for one of the pair get no rule — each |
| 12 | + * pinned beside its `tools/list` control. |
| 13 | + * - The install line names `@objectstack/skills` at this package's own |
| 14 | + * version (one `fixed` changeset group), for every grant. |
| 15 | + * - Host-injected text and the runtime's configured text are whole answers |
| 16 | + * their author owns: nothing is appended to either. |
| 17 | + */ |
| 18 | + |
| 19 | +import { createRequire } from 'node:module'; |
| 20 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 21 | +import { |
| 22 | + LIST_DATASETS_TOOL_NAME, |
| 23 | + MCP_OAUTH_SCOPE_DATA_READ, |
| 24 | + MCP_OAUTH_SCOPE_DATA_WRITE, |
| 25 | + MCP_OAUTH_SCOPE_METADATA_WRITE, |
| 26 | + QUERY_DATASET_TOOL_NAME, |
| 27 | +} from '@objectstack/spec/ai'; |
| 28 | +import { MCPServerRuntime } from './mcp-server-runtime.js'; |
| 29 | +import type { McpDataBridge } from './mcp-http-tools.js'; |
| 30 | +import type { McpAuthoringInjection } from './types.js'; |
| 31 | + |
| 32 | +const OWN_VERSION = (createRequire(import.meta.url)('../package.json') as { version: string }).version; |
| 33 | + |
| 34 | +const DEFINITIONS = [ |
| 35 | + { name: LIST_DATASETS_TOOL_NAME, description: 'list the governed datasets' }, |
| 36 | + { name: QUERY_DATASET_TOOL_NAME, description: 'compute a governed metric' }, |
| 37 | +]; |
| 38 | + |
| 39 | +type DatasetMember = 'listDatasets' | 'queryDataset' | 'datasetToolDefinitions'; |
| 40 | + |
| 41 | +function makeBridge( |
| 42 | + without: readonly DatasetMember[] = [], |
| 43 | + definitions: ReadonlyArray<{ name: string; description: string }> = DEFINITIONS, |
| 44 | +): McpDataBridge { |
| 45 | + const bridge: McpDataBridge = { |
| 46 | + async listObjects() { return []; }, |
| 47 | + async describeObject() { return null; }, |
| 48 | + async query() { return { records: [] }; }, |
| 49 | + async get() { return null; }, |
| 50 | + async create() { return {}; }, |
| 51 | + async update() { return {}; }, |
| 52 | + async remove() { return {}; }, |
| 53 | + async listDatasets() { return { datasets: [] }; }, |
| 54 | + async queryDataset() { return { rows: [], fields: [], rowCount: 0, truncated: false } as never; }, |
| 55 | + datasetToolDefinitions() { return definitions; }, |
| 56 | + }; |
| 57 | + for (const member of without) delete bridge[member]; |
| 58 | + return bridge; |
| 59 | +} |
| 60 | + |
| 61 | +interface Opts { |
| 62 | + grantedScopes?: string[]; |
| 63 | + authoring?: Partial<McpAuthoringInjection>; |
| 64 | +} |
| 65 | + |
| 66 | +async function rpc(runtime: MCPServerRuntime, bridge: McpDataBridge, body: unknown, o: Opts = {}) { |
| 67 | + const res = await runtime.handleHttpRequest( |
| 68 | + new Request('http://localhost/api/v1/mcp', { |
| 69 | + method: 'POST', |
| 70 | + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, |
| 71 | + body: JSON.stringify(body), |
| 72 | + }), |
| 73 | + { |
| 74 | + bridge, |
| 75 | + parsedBody: body, |
| 76 | + ...(o.grantedScopes ? { toolOptions: { grantedScopes: o.grantedScopes } } : {}), |
| 77 | + ...(o.authoring |
| 78 | + ? { |
| 79 | + authoring: { |
| 80 | + registry: { getAll: () => [], execute: async () => ({}) as never }, |
| 81 | + principal: { userId: 'usr_1', scopes: o.grantedScopes ?? [] }, |
| 82 | + ...o.authoring, |
| 83 | + } as McpAuthoringInjection, |
| 84 | + } |
| 85 | + : {}), |
| 86 | + }, |
| 87 | + ); |
| 88 | + expect(res.status).toBe(200); |
| 89 | + return (await res.json()) as any; |
| 90 | +} |
| 91 | + |
| 92 | +async function instructionsOf(runtime: MCPServerRuntime, bridge: McpDataBridge, o: Opts = {}): Promise<string> { |
| 93 | + const json = await rpc(runtime, bridge, { |
| 94 | + jsonrpc: '2.0', |
| 95 | + id: 1, |
| 96 | + method: 'initialize', |
| 97 | + params: { protocolVersion: '2025-06-18', capabilities: {}, clientInfo: { name: 'pin', version: '0' } }, |
| 98 | + }, o); |
| 99 | + expect(json.error).toBeUndefined(); |
| 100 | + expect(typeof json.result.instructions).toBe('string'); |
| 101 | + return json.result.instructions; |
| 102 | +} |
| 103 | + |
| 104 | +async function datasetToolsListed(runtime: MCPServerRuntime, bridge: McpDataBridge, o: Opts = {}): Promise<string[]> { |
| 105 | + const json = await rpc(runtime, bridge, { jsonrpc: '2.0', id: 2, method: 'tools/list' }, o); |
| 106 | + return ((json.result?.tools ?? []) as Array<{ name: string }>) |
| 107 | + .map((t) => t.name) |
| 108 | + .filter((n) => n === LIST_DATASETS_TOOL_NAME || n === QUERY_DATASET_TOOL_NAME) |
| 109 | + .sort(); |
| 110 | +} |
| 111 | + |
| 112 | +/** The rule names both dataset tools; neither name appears anywhere else in the open server's text. */ |
| 113 | +const carriesRule = (text: string) => text.includes(LIST_DATASETS_TOOL_NAME) && text.includes(QUERY_DATASET_TOOL_NAME); |
| 114 | +const namesNoDatasetTool = (text: string) => !text.includes(LIST_DATASETS_TOOL_NAME) && !text.includes(QUERY_DATASET_TOOL_NAME); |
| 115 | + |
| 116 | +describe('[#34686] initialize instructions — the selection rule follows the dataset tools', () => { |
| 117 | + let runtime: MCPServerRuntime; |
| 118 | + beforeEach(() => { |
| 119 | + runtime = new MCPServerRuntime({ name: 't', version: '1.0.0' }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('carries the rule where both dataset tools register: an API key / session and a data:read token', async () => { |
| 123 | + for (const o of [{}, { grantedScopes: [MCP_OAUTH_SCOPE_DATA_READ] }] as Opts[]) { |
| 124 | + expect(await datasetToolsListed(runtime, makeBridge(), o)).toEqual([LIST_DATASETS_TOOL_NAME, QUERY_DATASET_TOOL_NAME].sort()); |
| 125 | + expect(carriesRule(await instructionsOf(runtime, makeBridge(), o))).toBe(true); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + it('a grant without data:read gets neither dataset sentence (and lists neither tool)', async () => { |
| 130 | + for (const grantedScopes of [[MCP_OAUTH_SCOPE_DATA_WRITE], [MCP_OAUTH_SCOPE_METADATA_WRITE]]) { |
| 131 | + expect(await datasetToolsListed(runtime, makeBridge(), { grantedScopes })).toEqual([]); |
| 132 | + expect(namesNoDatasetTool(await instructionsOf(runtime, makeBridge(), { grantedScopes }))).toBe(true); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + it('a bridge without the dataset pair gets no rule, whatever the grant', async () => { |
| 137 | + for (const without of [['listDatasets'], ['queryDataset'], ['datasetToolDefinitions']] as DatasetMember[][]) { |
| 138 | + expect(await datasetToolsListed(runtime, makeBridge(without))).toEqual([]); |
| 139 | + expect(namesNoDatasetTool(await instructionsOf(runtime, makeBridge(without)))).toBe(true); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + it('a bridge that registers only one of the pair gets no rule: the rule names both', async () => { |
| 144 | + const listOnly = makeBridge([], DEFINITIONS.filter((d) => d.name === LIST_DATASETS_TOOL_NAME)); |
| 145 | + expect(await datasetToolsListed(runtime, listOnly)).toEqual([LIST_DATASETS_TOOL_NAME]); |
| 146 | + expect(namesNoDatasetTool(await instructionsOf(runtime, listOnly))).toBe(true); |
| 147 | + }); |
| 148 | +}); |
| 149 | + |
| 150 | +describe('[#34686] initialize instructions — the skills install line', () => { |
| 151 | + let runtime: MCPServerRuntime; |
| 152 | + beforeEach(() => { |
| 153 | + // The operator's serverInfo.version is not the skills version: a distinct value proves which one is read. |
| 154 | + runtime = new MCPServerRuntime({ name: 't', version: '0.0.1-operator' }); |
| 155 | + }); |
| 156 | + |
| 157 | + it('names @objectstack/skills at this package\'s own version, for every grant', async () => { |
| 158 | + expect(OWN_VERSION).toMatch(/^\d+\.\d+\.\d+/); |
| 159 | + for (const o of [{}, { grantedScopes: [MCP_OAUTH_SCOPE_DATA_READ] }, { grantedScopes: [MCP_OAUTH_SCOPE_DATA_WRITE] }] as Opts[]) { |
| 160 | + const text = await instructionsOf(runtime, makeBridge(), o); |
| 161 | + expect(text).toContain(`Published skills: @objectstack/skills ${OWN_VERSION}`); |
| 162 | + expect(text).toContain(`@objectstack/skills@${OWN_VERSION}`); |
| 163 | + expect(text).not.toContain('0.0.1-operator'); |
| 164 | + } |
| 165 | + }); |
| 166 | +}); |
| 167 | + |
| 168 | +describe('[#34686] initialize instructions — host and configured texts are left whole', () => { |
| 169 | + const HOST = 'Host text for a connection that can build.'; |
| 170 | + const DATA_ONLY = 'Host text for a connection that cannot build.'; |
| 171 | + const CONFIGURED = 'Configured server-wide instructions.'; |
| 172 | + |
| 173 | + it('the host\'s per-request texts answer exactly as injected, with no rule or install line appended', async () => { |
| 174 | + const runtime = new MCPServerRuntime({ name: 't', version: '1.0.0' }); |
| 175 | + expect( |
| 176 | + await instructionsOf(runtime, makeBridge(), { |
| 177 | + grantedScopes: [MCP_OAUTH_SCOPE_DATA_READ, MCP_OAUTH_SCOPE_METADATA_WRITE], |
| 178 | + authoring: { instructions: HOST }, |
| 179 | + }), |
| 180 | + ).toBe(HOST); |
| 181 | + expect( |
| 182 | + await instructionsOf(runtime, makeBridge(), { |
| 183 | + grantedScopes: [MCP_OAUTH_SCOPE_DATA_READ], |
| 184 | + authoring: { dataOnlyInstructions: DATA_ONLY }, |
| 185 | + }), |
| 186 | + ).toBe(DATA_ONLY); |
| 187 | + }); |
| 188 | + |
| 189 | + it('a runtime configured with instructions answers them exactly, dataset tools served or not', async () => { |
| 190 | + const runtime = new MCPServerRuntime({ name: 't', version: '1.0.0', instructions: CONFIGURED }); |
| 191 | + expect(await datasetToolsListed(runtime, makeBridge())).toHaveLength(2); |
| 192 | + expect(await instructionsOf(runtime, makeBridge())).toBe(CONFIGURED); |
| 193 | + }); |
| 194 | +}); |
0 commit comments