Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/35633-mcp-doc-answer-rule-export.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 9 additions & 0 deletions packages/mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 16 additions & 4 deletions packages/mcp/src/mcp-server-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand All @@ -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>): 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`:
Expand All @@ -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));
}

/**
Expand Down
80 changes: 80 additions & 0 deletions packages/mcp/src/package-doc-answer-rule-barrel-35633.test.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> {
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));
}
});
});
Loading