|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#34721] The stdio transport's `list_objects` and `describe_object` answer |
| 5 | + * the object's and each field's AUTHORED `description`, read off the object |
| 6 | + * as the principal is served it (ADR-0106 D5): a field the principal cannot |
| 7 | + * read is withheld with its description, and a definition that authors none |
| 8 | + * keeps the shape it always had (no `description` key). Driven through the |
| 9 | + * real tools (`registerObjectTools`) over the stdio bridge, with a security |
| 10 | + * service built by the shared ADR-0106 double (`securityDoubleFor`). |
| 11 | + * |
| 12 | + * The HTTP twin is `packages/runtime/src/domains/mcp-object-descriptions-34721.test.ts`. |
| 13 | + */ |
| 14 | + |
| 15 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 16 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 17 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 18 | +import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js'; |
| 19 | +import type { ExecutionContext } from '@objectstack/spec/kernel'; |
| 20 | +import type { IDataEngine, IMetadataService } from '@objectstack/spec/contracts'; |
| 21 | +import { securityDoubleFor } from '@objectstack/metadata-core/testing'; |
| 22 | +import { OBJECT_SCHEMA_MASK_DISABLE_ENV } from '@objectstack/metadata-core'; |
| 23 | +import { createStdioDataBridge } from './stdio-data-bridge.js'; |
| 24 | +import { registerObjectTools } from './mcp-http-tools.js'; |
| 25 | + |
| 26 | +const OBJ = 'deal'; |
| 27 | +const PLAIN = 'plain'; |
| 28 | +const DENIED = 'secret_margin'; |
| 29 | +const OBJECT_TEXT = 'A sales deal, from first call to signed order.'; |
| 30 | +const AMOUNT_TEXT = 'Amount, tax excluded.'; |
| 31 | +const DENIED_TEXT = 'Internal margin after rebates.'; |
| 32 | + |
| 33 | +const deal = () => ({ |
| 34 | + name: OBJ, |
| 35 | + label: 'Deal', |
| 36 | + description: OBJECT_TEXT, |
| 37 | + fields: { |
| 38 | + id: { name: 'id', type: 'text', label: 'ID' }, |
| 39 | + subject: { name: 'subject', type: 'text', label: 'Subject' }, |
| 40 | + amount: { name: 'amount', type: 'currency', label: 'Amount', description: AMOUNT_TEXT }, |
| 41 | + blank_note: { name: 'blank_note', type: 'text', label: 'Note', description: ' ' }, |
| 42 | + [DENIED]: { name: DENIED, type: 'currency', label: 'Margin', description: DENIED_TEXT }, |
| 43 | + }, |
| 44 | +}); |
| 45 | +const plain = () => ({ name: PLAIN, label: 'Plain', fields: { id: { name: 'id', type: 'text' }, title: { name: 'title', type: 'text' } } }); |
| 46 | + |
| 47 | +/** The real tools over the stdio bridge, the principal reading `readable` (every field when omitted). */ |
| 48 | +async function connect(readable?: readonly string[]) { |
| 49 | + const security = readable |
| 50 | + ? securityDoubleFor({ |
| 51 | + id: 'restricted-caller/34721', |
| 52 | + why: 'a principal denied one field', |
| 53 | + context: { userId: 'u_member', systemPermissions: [] }, |
| 54 | + readable: [...readable], |
| 55 | + expect: { kind: 'unmasked' }, |
| 56 | + } as never) |
| 57 | + : undefined; |
| 58 | + const defs = () => [deal(), plain()]; |
| 59 | + const bridge = createStdioDataBridge({ |
| 60 | + engine: {} as unknown as IDataEngine, |
| 61 | + metadataService: { |
| 62 | + listObjects: vi.fn(async () => defs()), |
| 63 | + getObject: vi.fn(async (name: string) => defs().find((d) => d.name === name)), |
| 64 | + } as unknown as IMetadataService, |
| 65 | + resolvePrincipal: async () => ({ userId: 'u_member', systemPermissions: [] }) as unknown as ExecutionContext, |
| 66 | + ...(security ? { security: () => security } : {}), |
| 67 | + }); |
| 68 | + const server = new McpServer({ name: 'pin', version: '0' }); |
| 69 | + registerObjectTools(server, bridge); |
| 70 | + const [clientSide, serverSide] = InMemoryTransport.createLinkedPair(); |
| 71 | + await server.connect(serverSide); |
| 72 | + const client = new Client({ name: 'pin', version: '0' }); |
| 73 | + await client.connect(clientSide); |
| 74 | + const call = async (name: string, args: Record<string, unknown>): Promise<{ text: string; json: any; isError: boolean }> => { |
| 75 | + const res = (await client.callTool({ name, arguments: args })) as { isError?: boolean; content: Array<{ text: string }> }; |
| 76 | + const text = res.content[0]!.text; |
| 77 | + return { text, json: JSON.parse(text), isError: res.isError === true }; |
| 78 | + }; |
| 79 | + return { call, close: () => client.close() }; |
| 80 | +} |
| 81 | + |
| 82 | +const prevMask = process.env[OBJECT_SCHEMA_MASK_DISABLE_ENV]; |
| 83 | +beforeEach(() => { delete process.env[OBJECT_SCHEMA_MASK_DISABLE_ENV]; }); |
| 84 | +afterEach(() => { |
| 85 | + if (prevMask === undefined) delete process.env[OBJECT_SCHEMA_MASK_DISABLE_ENV]; |
| 86 | + else process.env[OBJECT_SCHEMA_MASK_DISABLE_ENV] = prevMask; |
| 87 | +}); |
| 88 | + |
| 89 | +/** Every field but the denied one (the double answers one readable set for every object). */ |
| 90 | +const MEMBER_READS = ['id', 'subject', 'amount', 'blank_note', 'title'] as const; |
| 91 | + |
| 92 | +const field = (described: any, name: string) => described.fields.find((f: any) => f.name === name); |
| 93 | + |
| 94 | +describe('[#34721] stdio list_objects and describe_object serve the authored descriptions, as the principal is served the object', () => { |
| 95 | + it('describe_object: a readable field\'s description is returned, a field the principal cannot read is withheld with its description', async () => { |
| 96 | + const session = await connect(MEMBER_READS); |
| 97 | + try { |
| 98 | + const described = await session.call('describe_object', { objectName: OBJ }); |
| 99 | + expect(described.isError).toBe(false); |
| 100 | + expect(described.json.description).toBe(OBJECT_TEXT); |
| 101 | + expect(field(described.json, 'amount')).toMatchObject({ name: 'amount', label: 'Amount', description: AMOUNT_TEXT }); |
| 102 | + expect(field(described.json, DENIED)).toBeUndefined(); |
| 103 | + expect(described.text).not.toContain(DENIED_TEXT); |
| 104 | + } finally { |
| 105 | + await session.close(); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + it('CONTROL — a principal who may read every field is returned that field\'s description', async () => { |
| 110 | + const session = await connect(); |
| 111 | + try { |
| 112 | + const described = await session.call('describe_object', { objectName: OBJ }); |
| 113 | + expect(field(described.json, DENIED)).toMatchObject({ name: DENIED, description: DENIED_TEXT }); |
| 114 | + } finally { |
| 115 | + await session.close(); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + it('describe_object: a field or object that authors no description (or a blank one) carries no description key', async () => { |
| 120 | + const session = await connect(MEMBER_READS); |
| 121 | + try { |
| 122 | + const described = (await session.call('describe_object', { objectName: OBJ })).json; |
| 123 | + expect('description' in field(described, 'subject')).toBe(false); |
| 124 | + expect('description' in field(described, 'blank_note')).toBe(false); |
| 125 | + expect('description' in (await session.call('describe_object', { objectName: PLAIN })).json).toBe(false); |
| 126 | + } finally { |
| 127 | + await session.close(); |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + it('list_objects: each object\'s authored description, and no key on an object that authors none', async () => { |
| 132 | + const session = await connect(MEMBER_READS); |
| 133 | + try { |
| 134 | + const listed = (await session.call('list_objects', {})).json; |
| 135 | + const found = (name: string) => listed.objects.find((o: any) => o.name === name); |
| 136 | + expect(found(OBJ)).toEqual({ name: OBJ, label: 'Deal', description: OBJECT_TEXT, fieldCount: 4 }); |
| 137 | + expect(found(PLAIN)).toEqual({ name: PLAIN, label: 'Plain', fieldCount: 2 }); |
| 138 | + } finally { |
| 139 | + await session.close(); |
| 140 | + } |
| 141 | + }); |
| 142 | +}); |
0 commit comments