diff --git a/.changeset/35633-mcp-doc-answer-rule-export.md b/.changeset/35633-mcp-doc-answer-rule-export.md new file mode 100644 index 00000000000..e8dd22ea20a --- /dev/null +++ b/.changeset/35633-mcp-doc-answer-rule-export.md @@ -0,0 +1,10 @@ +--- +'@objectstack/mcp': patch +--- + +feat(mcp): export the app-docs answer-order rule and its served-check from the package barrel + +Clause-②: yes — widening: four new exported symbols on the published `@objectstack/mcp` barrel (`packageDocAnswerRule`, `packageDocToolsServed`, `definitionReaderOf`, `DEFINITION_READER_TOOL_NAMES`); nothing removed or refused. Seat runs a contract read before ready. +Metadata-protocol: none + +`@objectstack/mcp` now exports `packageDocAnswerRule(definitionReader?)`, `packageDocToolsServed(bridge, options)`, `definitionReaderOf(toolNames)` and `DEFINITION_READER_TOOL_NAMES`. A host that writes its own `initialize` text (`instructions` / `dataOnlyInstructions`) can carry the same "read the manuals first, end with sources" rule the open server appends to its default text: send it where `packageDocToolsServed` is true, naming the reader `definitionReaderOf` picks from the tool names the connection lists. The open server calls the same exports, so there is one copy of the sentence. Nothing else changes. diff --git a/packages/mcp/src/index.ts b/packages/mcp/src/index.ts index f281afd9c62..6d13f958024 100644 --- a/packages/mcp/src/index.ts +++ b/packages/mcp/src/index.ts @@ -13,6 +13,13 @@ export { MCPServerPlugin } from './plugin.js'; export type { MCPServerPluginOptions } from './plugin.js'; export { MCPServerRuntime } from './mcp-server-runtime.js'; +// [#35633] The app-docs answer-order rule and the reader pick it names, for a +// host that writes its own `initialize` text; the open server sends the same. +export { + packageDocAnswerRule, + definitionReaderOf, + DEFINITION_READER_TOOL_NAMES, +} from './mcp-server-runtime.js'; export type { MCPServerRuntimeConfig, McpHttpCaller } from './mcp-server-runtime.js'; // [#26464] The host-injected authoring bridge behind `metadata:write`: the // shapes a host passes to `handleHttpRequest` (`authoring`) and the door @@ -46,6 +53,8 @@ export { registerActionTools, registerFederationTools, grantsAuthoringTools, + // [#35633] Whether both doc tools register: where the rule above is sent. + packageDocToolsServed, } from './mcp-http-tools.js'; export type { McpDataBridge, diff --git a/packages/mcp/src/mcp-server-runtime.ts b/packages/mcp/src/mcp-server-runtime.ts index de4b8f6130a..e06c8ac9e33 100644 --- a/packages/mcp/src/mcp-server-runtime.ts +++ b/packages/mcp/src/mcp-server-runtime.ts @@ -1894,8 +1894,11 @@ function skillsInstallLine(version: string | undefined): string | undefined { * definition reader the request registers ({@link definitionReaderServed}), * and without one the answer says the configuration was not checked. Sent * only where both doc tools register ({@link packageDocToolsServed}). + * [#35633] Exported from the package barrel, so a host that writes its own + * `instructions` / `dataOnlyInstructions` carries this same text: it passes + * the reader its connection lists ({@link definitionReaderOf}) as a plain name. */ -function packageDocAnswerRule(definitionReader: string | undefined): string { +export function packageDocAnswerRule(definitionReader?: string): string { const processStep = definitionReader ? `also read its real definition with ${definitionReader}; where the manual and the definition differ, the ` + 'definition is what runs: answer from it and name the difference.' @@ -1915,7 +1918,17 @@ function packageDocAnswerRule(definitionReader: string | undefined): string { * registry) and `get_metadata_item` (the open draft door's, * `mcp-authoring.ts` in `@objectstack/runtime`). */ -const DEFINITION_READER_TOOL_NAMES = ['describe_metadata', 'get_metadata_item'] as const; +export const DEFINITION_READER_TOOL_NAMES = ['describe_metadata', 'get_metadata_item'] as const; + +/** + * [#35633] The definition reader among the tool names a connection lists, in + * {@link DEFINITION_READER_TOOL_NAMES} order, or `undefined`: the one pick the + * open server and a host with its own `initialize` text both make. + */ +export function definitionReaderOf(toolNames: Iterable): string | undefined { + const names = new Set(toolNames); + return DEFINITION_READER_TOOL_NAMES.find((name) => names.has(name)); +} /** * [#35442] The definition reader this request registers, or `undefined`: @@ -1928,8 +1941,7 @@ function definitionReaderServed( grantedScopes: readonly string[] | undefined, ): string | undefined { if (!authoringToolsServed(authoring, grantedScopes)) return undefined; - const names = new Set(authoring!.registry.getAll().map((tool) => tool.name)); - return DEFINITION_READER_TOOL_NAMES.find((name) => names.has(name)); + return definitionReaderOf(authoring!.registry.getAll().map((tool) => tool.name)); } /** diff --git a/packages/mcp/src/package-doc-answer-rule-barrel-35633.test.ts b/packages/mcp/src/package-doc-answer-rule-barrel-35633.test.ts new file mode 100644 index 00000000000..2d99db92c5e --- /dev/null +++ b/packages/mcp/src/package-doc-answer-rule-barrel-35633.test.ts @@ -0,0 +1,80 @@ +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * [#35633] The app-docs answer-order rule (#35442) and its served-check are + * exported from the package barrel, so a host that writes its own `initialize` + * text (the cloud build door) carries the same sentence; the open server's + * default text holds exactly what the barrel export returns for the same reader. + */ + +import { describe, it, expect } from 'vitest'; +import { MCP_OAUTH_SCOPE_DATA_READ, MCP_OAUTH_SCOPE_METADATA_WRITE } from '@objectstack/spec/ai'; +import { + MCPServerRuntime, + packageDocAnswerRule, + packageDocToolsServed, + definitionReaderOf, + DEFINITION_READER_TOOL_NAMES, + type McpAuthoringInjection, + type McpDataBridge, +} from './index.js'; + +const bridge = { + async listObjects() { return []; }, + async describeObject() { return null; }, + async query() { return { records: [] }; }, + async get() { return null; }, + async create() { return {}; }, + async update() { return {}; }, + async remove() { return {}; }, + async searchPackageDocs() { return { terms: [], results: [], truncated: false }; }, + async readPackageDoc() { return { name: 'p', title: 'T', content: 'body' }; }, +} as unknown as McpDataBridge; + +/** The default `initialize` text's paragraphs, for a connection listing `authoringTools` (if any). */ +async function paragraphsOf(authoringTools?: string[]): Promise { + const body = { + jsonrpc: '2.0', id: 1, method: 'initialize', + params: { protocolVersion: '2025-06-18', capabilities: {}, clientInfo: { name: 'pin', version: '0' } }, + }; + const grantedScopes = [MCP_OAUTH_SCOPE_DATA_READ, ...(authoringTools ? [MCP_OAUTH_SCOPE_METADATA_WRITE] : [])]; + const authoring = authoringTools && ({ + registry: { + getAll: () => authoringTools.map((name) => ({ name, description: name, parameters: { type: 'object', properties: {} } })), + execute: async () => ({}) as never, + }, + principal: { userId: 'usr_1', scopes: grantedScopes }, + } as McpAuthoringInjection); + const res = await new MCPServerRuntime({ name: 't', version: '1.0.0' }).handleHttpRequest( + new Request('http://localhost/api/v1/mcp', { + method: 'POST', + headers: { 'content-type': 'application/json', accept: 'application/json, text/event-stream' }, + body: JSON.stringify(body), + }), + { bridge, parsedBody: body, toolOptions: { grantedScopes }, ...(authoring ? { authoring } : {}) }, + ); + const json = (await res.json()) as { result: { instructions: string } }; + return json.result.instructions.split('\n\n'); +} + +describe('[#35633] the answer-order rule is a barrel export, and the open server sends exactly it', () => { + it('exports the rule, the served-check and the reader pick', () => { + expect(typeof packageDocAnswerRule).toBe('function'); + expect(typeof packageDocToolsServed).toBe('function'); + expect(packageDocToolsServed(bridge, { grantedScopes: [MCP_OAUTH_SCOPE_DATA_READ] })).toBe(true); + expect([...DEFINITION_READER_TOOL_NAMES]).toEqual(['describe_metadata', 'get_metadata_item']); + expect(definitionReaderOf(['get_metadata_item', 'describe_metadata'])).toBe('describe_metadata'); + expect(definitionReaderOf(['save_metadata_draft'])).toBeUndefined(); + }); + + it('the default initialize text holds packageDocAnswerRule(reader) whole, for each reader a connection lists', async () => { + for (const [tools, reader] of [ + [undefined, undefined], + [['list_metadata_items', 'get_metadata_item'], 'get_metadata_item'], + [['get_metadata_item', 'describe_metadata'], 'describe_metadata'], + ] as Array<[string[] | undefined, string | undefined]>) { + expect(definitionReaderOf(tools ?? [])).toBe(reader); + expect(await paragraphsOf(tools)).toContain(packageDocAnswerRule(reader)); + } + }); +});