diff --git a/.changeset/mcp-describe-object-search-companion.md b/.changeset/mcp-describe-object-search-companion.md new file mode 100644 index 00000000000..40527d8105e --- /dev/null +++ b/.changeset/mcp-describe-object-search-companion.md @@ -0,0 +1,13 @@ +--- +'@objectstack/mcp': patch +'@objectstack/runtime': patch +--- + +fix(mcp): `describe_object` no longer lists the hidden `__search` search companion, on either MCP transport + +Clause-②: no +Metadata-protocol: none + +Where pinyin search is on, the registry serves an object with the hidden `__search` companion column beside its display field. `describe_object` described every served field, so it offered an agent that column (as `readonly` and `system`), while no caller may read, write or name it: a record never echoes it, a `$searchFields` override naming it is refused, and `validate_expression` answers `unknown-field` for a read of it. + +`describe_object` now leaves the companion out on both transports: the HTTP bridge in `@objectstack/runtime`, and the stdio bridge and the `objectstack://objects/{name}` resource in `@objectstack/mcp`. Both skip it by the protocol's one constant, `SEARCH_COMPANION_FIELD` from `@objectstack/spec/data`. Every other field is listed as before, the injected system columns such as `organization_id` and `created_at` included. diff --git a/packages/mcp/src/mcp-server-runtime.ts b/packages/mcp/src/mcp-server-runtime.ts index 487fb1756a7..f73e1a4c099 100644 --- a/packages/mcp/src/mcp-server-runtime.ts +++ b/packages/mcp/src/mcp-server-runtime.ts @@ -59,7 +59,7 @@ import { z } from 'zod'; // [#28715] The engine's own reading of "this field declares options" (its // `label()` map is built from it), never a second spelling here. import { declaresOptions } from '@objectstack/formula'; -import { RUNTIME_OWNED_FIELD_TYPES, isVirtualSearchField } from '@objectstack/spec/data'; +import { RUNTIME_OWNED_FIELD_TYPES, SEARCH_COMPANION_FIELD, isVirtualSearchField } from '@objectstack/spec/data'; /** * Configuration for the MCP Server Runtime. @@ -124,6 +124,13 @@ interface ServedFieldDef { * — ⛔ never its condition, pattern or transition table: the rule's message is * what a refused write answers with, and its expression is server policy. * + * ⛔ Never the hidden search companion (`SEARCH_COMPANION_FIELD`), which the + * registry serves beside the display field where pinyin search is on: no + * caller may read, write or name it (a record never echoes it, a + * `$searchFields` override naming it is refused, `validate_expression` + * answers `unknown-field`), so the door that offers an agent a field to use + * does not list it. The HTTP twin skips it by the same constant. + * * One projection for this package's two readers of it: the stdio bridge's * `describeObject` (`stdio-data-bridge.ts`) and the `objectstack://objects/{name}` * resource ({@link buildObjectSchemaResource}), each handing it the object as @@ -140,7 +147,7 @@ export function describeServedObject(def: ServedObjectDef, declared: ServedObjec return { name: def.name, label: def.label ?? def.name, - fields: Object.entries(def.fields ?? {}).map(([key, f]) => ({ + fields: Object.entries(def.fields ?? {}).filter(([key]) => key !== SEARCH_COMPANION_FIELD).map(([key, f]) => ({ name: key, type: f?.type, label: f?.label ?? key, diff --git a/packages/mcp/src/stdio-data-bridge.object-schema-mask.test.ts b/packages/mcp/src/stdio-data-bridge.object-schema-mask.test.ts index f18303ff42d..a15910bfdc8 100644 --- a/packages/mcp/src/stdio-data-bridge.object-schema-mask.test.ts +++ b/packages/mcp/src/stdio-data-bridge.object-schema-mask.test.ts @@ -40,9 +40,12 @@ import { } from '@objectstack/metadata-core/testing'; import { OBJECT_SCHEMA_MASK_DISABLE_ENV, + applyInjectedSystemColumns, assertEngineUpdateDispatch, type ObjectSchemaReadDelegateSource, } from '@objectstack/metadata-core'; +import { SEARCH_COMPANION_FIELD } from '@objectstack/spec/data'; +import { provisionSearchCompanion } from '@objectstack/objectql/core'; import { callerObjectView, createStdioDataBridge, stdioObjectView } from './stdio-data-bridge.js'; import { registerObjectTools } from './mcp-http-tools.js'; import { MCPServerPlugin } from './plugin.js'; @@ -721,3 +724,67 @@ describe('stdio describe_object and the objects resource judge readonly on the D expect((await describeOverResource()).json.fields).toEqual((await describeOverStdio()).json.fields); }); }); + +describe('stdio describe_object and the objects resource never list the hidden search companion', () => { + // A field-scoped search object as the registry serves it with pinyin search + // on: the injected system columns, and the companion the engine stamps + // beside the display field (`provisionSearchCompanion`, the producer's own + // function, not a transcription of its output). The companion is a column + // no caller may read, write or name, so the door that offers an agent a + // field to use does not list it. The HTTP twin is pinned on a real engine in + // `packages/runtime/src/domains/mcp-object-schema-mask.test.ts`. + const CONTACT = 'sc_contact'; + const served = () => provisionSearchCompanion(applyInjectedSystemColumns({ + name: CONTACT, + label: 'Contact', + fields: { + name: { type: 'text', label: 'Name', required: true }, + email: { type: 'email', label: 'Email' }, + }, + })); + const metadataService = () => ({ + listObjects: vi.fn(async () => [served()]), + getObject: vi.fn(async (name: string) => (name === CONTACT ? served() : undefined)), + }) as unknown as IMetadataService; + const KEPT = ['name', 'email', 'created_at', 'updated_at', 'organization_id']; + const namesOf = (described: any): string[] => described.fields.map((f: any) => f.name); + + it('precondition: the served object carries the companion', () => { + expect(Object.keys(served().fields)).toContain(SEARCH_COMPANION_FIELD); + }); + + it('describe_object over stdio', async () => { + const bridge = createStdioDataBridge({ + engine: {} as unknown as IDataEngine, + metadataService: metadataService(), + resolvePrincipal: async () => ({ userId: 'u_member' }) as unknown as ExecutionContext, + }); + expect(Object.keys((await bridge.servedObjectDefinitionOf!(CONTACT))!.fields as object)).toContain(SEARCH_COMPANION_FIELD); + const server = new McpServer({ name: 'pin', version: '0' }); + registerObjectTools(server, bridge); + const [clientSide, serverSide] = InMemoryTransport.createLinkedPair(); + await server.connect(serverSide); + const client = new Client({ name: 'pin', version: '0' }); + await client.connect(clientSide); + try { + const res = (await client.callTool({ name: 'describe_object', arguments: { objectName: CONTACT } })) as { + isError?: boolean; + content: Array<{ text: string }>; + }; + expect(res.isError === true).toBe(false); + const names = namesOf(JSON.parse(res.content[0]!.text)); + expect(names).not.toContain(SEARCH_COMPANION_FIELD); + expect(names).toEqual(expect.arrayContaining(KEPT)); + expect(res.content[0]!.text).not.toContain(SEARCH_COMPANION_FIELD); + } finally { + await client.close(); + } + }); + + it('the objectstack://objects/{name} resource', async () => { + const result = await buildObjectSchemaResource(metadataService(), CONTACT); + const text = result.contents[0]!.text; + expect(namesOf(JSON.parse(text))).toEqual(expect.arrayContaining(KEPT)); + expect(text).not.toContain(SEARCH_COMPANION_FIELD); + }); +}); diff --git a/packages/runtime/src/domains/mcp-object-schema-mask.test.ts b/packages/runtime/src/domains/mcp-object-schema-mask.test.ts index 1fc72b8a059..108ab921664 100644 --- a/packages/runtime/src/domains/mcp-object-schema-mask.test.ts +++ b/packages/runtime/src/domains/mcp-object-schema-mask.test.ts @@ -42,6 +42,7 @@ import { SysMetadataObject, } from '@objectstack/metadata-core'; import { PermissionSetSchema } from '@objectstack/spec/security'; +import { SEARCH_COMPANION_FIELD } from '@objectstack/spec/data'; import { ObjectQL } from '@objectstack/objectql'; import { SqlDriver } from '@objectstack/driver-sql'; import { SecurityPlugin } from '@objectstack/plugin-security'; @@ -1711,3 +1712,78 @@ describe('[#32530] describe_object serves readonly where a write never stands expect((engine.getSchema(OBJ) as any).fields.marked_up.expression, 'the declaration still carries it').toBeDefined(); }); }); + +// The hidden search companion the registry provisions beside an object's +// display field (`provisionSearchCompanion`, `@objectstack/objectql`) is a +// column no caller may read, write or name: a record never echoes it, a +// `$searchFields` override naming it is refused, and `validate_expression` +// answers `unknown-field` for a read of it. `describe_object` is the door that +// offers an agent a field to use, so it does not list it either. The stdio +// twin is pinned in `packages/mcp/src/stdio-data-bridge.object-schema-mask.test.ts`. +describe('describe_object never lists the hidden search companion — the real engine', () => { + const OBJ = 'mcp_search_contact'; + let engine: ObjectQL; + let services: Record; + const CALLER = { userId: 'usr_search_member', positions: [] as string[], permissions: [] as string[], systemPermissions: [] as string[] }; + const prevPinyin = process.env.OS_SEARCH_PINYIN_ENABLED; + + beforeAll(async () => { + // The deployment switch the registry provisions the companion under, + // read once, when the registry is built. + process.env.OS_SEARCH_PINYIN_ENABLED = 'true'; + engine = new ObjectQL(); + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true } as any); + const storeObjects = [SysMetadataObject, SysMetadataHistoryObject, SysMetadataAuditObject] as any[]; + await driver.initObjects(storeObjects); + engine.registerDriver(driver as any, true); + await engine.init(); + for (const o of storeObjects) engine.registry.registerObject(o, '@objectstack/platform-objects'); + engine.registerApp({ + id: 'com.objectstack.qa.mcp-describe-search-companion', + name: 'MCP describe search companion', + version: '1.0.0', + type: 'plugin', + scope: 'system', + objects: [{ + name: OBJ, + label: 'Contact', + sharingModel: 'public_read_write', + fields: { + // The display field: the companion's source. + name: { name: 'name', type: 'text', label: 'Name', required: true }, + email: { name: 'email', type: 'email', label: 'Email' }, + }, + }], + } as never); + await engine.syncSchemas(); + vi.spyOn((engine as unknown as { logger: { warn: () => void } }).logger, 'warn').mockImplementation(() => undefined); + services = { protocol: new ObjectStackProtocolImplementation(engine as any), objectql: engine }; + vi.spyOn(console, 'warn').mockImplementation(() => undefined); + }, 60_000); + + afterAll(async () => { + vi.restoreAllMocks(); + if (prevPinyin === undefined) delete process.env.OS_SEARCH_PINYIN_ENABLED; + else process.env.OS_SEARCH_PINYIN_ENABLED = prevPinyin; + try { await engine?.destroy(); } catch { /* noop */ } + }); + + const describedNames = async (): Promise => { + const { bridge } = await bridgeFor(kernelOver(services), CALLER); + return ((await bridge.describeObject(OBJ)) as any).fields.map((f: any) => f.name); + }; + + it('precondition: the registry serves the object WITH the companion', async () => { + expect(Object.keys((engine.getSchema(OBJ) as any).fields)).toContain(SEARCH_COMPANION_FIELD); + const { bridge } = await bridgeFor(kernelOver(services), CALLER); + expect(Object.keys((await bridge.servedObjectDefinitionOf(OBJ)).fields)).toContain(SEARCH_COMPANION_FIELD); + }); + + it('the answer has no companion, and keeps the declared fields and the platform\'s other system columns', async () => { + const names = await describedNames(); + expect(names).not.toContain(SEARCH_COMPANION_FIELD); + expect(names).toEqual(expect.arrayContaining(['name', 'email', 'created_at', 'updated_at', 'organization_id'])); + const { bridge } = await bridgeFor(kernelOver(services), CALLER); + expect(JSON.stringify(await bridge.describeObject(OBJ))).not.toContain(SEARCH_COMPANION_FIELD); + }); +}); diff --git a/packages/runtime/src/domains/mcp.ts b/packages/runtime/src/domains/mcp.ts index 261d8bc7615..8b51baf8536 100644 --- a/packages/runtime/src/domains/mcp.ts +++ b/packages/runtime/src/domains/mcp.ts @@ -43,7 +43,7 @@ import { readDispatchedBody } from '../deferred-request-body.js'; // [#28715] The engine's own reading of "this field declares options" (its // `label()` map is built from it), never a second spelling here. import { declaresOptions } from '@objectstack/formula'; -import { RUNTIME_OWNED_FIELD_TYPES, isVirtualSearchField } from '@objectstack/spec/data'; +import { RUNTIME_OWNED_FIELD_TYPES, SEARCH_COMPANION_FIELD, isVirtualSearchField } from '@objectstack/spec/data'; // [#15705] The resume door's one refusal table, shared with // `POST /automation/:name/runs/:runId/resume` so the two doors answer one // engine result identically. @@ -676,6 +676,13 @@ async function warnIfSkillListIncomplete( * ⛔ never its condition, pattern or transition table: the rule's message is * what a refused write answers with, and its expression is server policy. * + * ⛔ Never the hidden search companion (`SEARCH_COMPANION_FIELD`), which the + * registry serves beside the display field where pinyin search is on: no + * caller may read, write or name it (a record never echoes it, a + * `$searchFields` override naming it is refused, `validate_expression` + * answers `unknown-field`), so the door that offers an agent a field to use + * does not list it. The stdio twin skips it by the same constant. + * * `declared` is the object `def` was projected from (the caller's mask * applied to it gives `def`): `readonly` is judged on it, * {@link writeNeverSetsField}. Nothing else is read from it. @@ -690,7 +697,7 @@ function describeServedObject(def: any, declared: any): Record return { name: def.name, label: def.label ?? def.name, - fields: Object.entries(def.fields ?? {}).map(([k, f]: [string, any]) => ({ + fields: Object.entries(def.fields ?? {}).filter(([k]) => k !== SEARCH_COMPANION_FIELD).map(([k, f]: [string, any]) => ({ name: k, type: f?.type, label: f?.label ?? k,