Files
reverse-engineering-anything/src/server/toolRegistrationOptions.ts
T
ChiragandVincent Koc d840860bd7 feat(mcp): advertise each tool's family in tools/list _meta (#7154)
* feat(mcp): advertise each tool's family in tools/list _meta

Clients that index a large catalog lazily had to guess each REA tool's
group from its name. Every tools/list entry now carries the contract's
adapter family under the vendor-prefixed _meta key
"io.github.morluto/rea", attached once at the contract-to-registration
boundary. The recorded catalog replays it unchanged on the deferred path.

No tool is hidden or renamed and input schemas are unchanged.

Closes #6870

* refactor(mcp): advertise the selectable family id in tools/list _meta

Since #7228 the contract-owned family inventory gives every tool one
selectable id, the vocabulary REA_MCP_TOOL_FAMILIES accepts. Advertise
that id instead of the adapter kind so clients can map an advertised tool
back to the scope that enables it.

* fix(mcp): omit family metadata for contracts outside the inventory

Renamed test fixtures have no published family; advertise nothing for
them instead of throwing. Drop the docs claim that a family move changes
the catalog digest, since the digest hashes the adapter kind.

* docs(mcp): scope family _meta promise to domain tools; pin catalog env in test

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-11-30 19:58:25 +08:00

88 lines
2.9 KiB
TypeScript

import {
REA_MCP_META_KEY,
type ToolContract,
type ToolFamilyMeta,
} from "../contracts/toolContractTypes.js";
import { toolFamilyId } from "../contracts/toolFamilies.js";
import type { StandardSchemaWithJSON } from "@modelcontextprotocol/server";
import type { z } from "zod";
import {
getEvidenceInputContract,
type EvidenceRequestFrameInput,
} from "../domain/evidenceInputContract.js";
import {
toolInputSchemaWithMetadata,
toolOutputSchemaWithMetadata,
} from "../contracts/toolSchemaMetadata.js";
import {
canReferenceInlineEvidence,
markEvidenceInput,
} from "./evidenceInputRegistration.js";
/** Project the canonical Zod contracts directly into SDK registration. */
export const toolRegistrationOptions = <Contract extends ToolContract>(
contract: Contract,
): {
readonly title: Contract["title"];
readonly description: Contract["description"];
readonly inputSchema: StandardSchemaWithJSON<
z.input<Contract["inputSchema"]>,
EvidenceRequestFrameInput<z.output<Contract["inputSchema"]>>
>;
readonly outputSchema: Contract["outputSchema"];
readonly annotations: Contract["annotations"];
readonly _meta?: ToolFamilyMeta;
} => {
// Every catalog contract has a family (toolFamilies.test.ts). A renamed test
// fixture has none, and advertises no family rather than a guessed one.
const family = toolFamilyId(contract.name);
const advertised = toolInputSchemaWithMetadata(contract);
const evidenceInputs = getEvidenceInputContract<Contract["inputSchema"]>(
contract.inputSchema,
);
// The application owns source admission. Preserve the exact advertised
// declaration while the SDK validates only the borrowing request envelope.
const inputSchema = {
"~standard": {
version: advertised["~standard"].version,
vendor: advertised["~standard"].vendor,
jsonSchema: advertised["~standard"].jsonSchema,
validate: evidenceInputs.frameSchema["~standard"].validate,
},
};
return {
title: contract.title,
description: contract.description,
inputSchema: evidenceInputs.hasEvidenceReferenceAlternative
? markEvidenceInput(inputSchema)
: inputSchema,
outputSchema: toolOutputSchemaWithMetadata(contract),
annotations: contract.annotations,
...(family === undefined
? {}
: { _meta: { [REA_MCP_META_KEY]: { family } } }),
};
};
/** Register a workflow that validates raw input before effects and projects its own errors. */
export const applicationToolRegistrationOptions = <
Contract extends ToolContract,
>(
contract: Contract,
) => {
const registration = toolRegistrationOptions(contract);
const inputSchema: StandardSchemaWithJSON = {
"~standard": {
...registration.inputSchema["~standard"],
vendor: "rea-application",
validate: (value: unknown) => ({ value }),
},
};
return {
...registration,
inputSchema: canReferenceInlineEvidence(registration.inputSchema)
? markEvidenceInput(inputSchema)
: inputSchema,
};
};