Files
reverse-engineering-anything/src/server/mcpClientMetadata.ts
T
morluto b8a9da0c0f refactor(mcp): make analysis states unrepresentable (#468)
* refactor(config): parse capability settings into exact states

* refactor(runtime): encode replay and process outcomes exactly

* refactor: make analysis states unrepresentable

* refactor(mcp): register canonical schemas with SDK

* fix: preserve capability and catalog invariants

* fix(build): remove stale compiled modules

* fix(replay): update real verifier policy dependency

* fix(package): verify the complete MCP catalog

* fix(test): supply complete replay reload policy

* refactor(contracts): centralize output schema lookup

* refactor(mcp): delegate protocol boundaries to SDK

* fix(browser): approve real source capture

* fix(mcp): preserve catalog guidance
2026-08-10 02:28:11 +08:00

57 lines
2.0 KiB
TypeScript

import {
CLIENT_CAPABILITIES_META_KEY,
CLIENT_INFO_META_KEY,
PROTOCOL_VERSION_META_KEY,
type ServerContext,
} from "@modelcontextprotocol/server";
import type { ClientFeatureAvailability } from "../contracts/toolOutputSchemaPrimitives.js";
import type { ConnectedClientIdentity } from "../serverIdentity.js";
/** Client metadata bound to the current SDK request rather than server-global state. */
export const mcpClientMetadata = (context: {
readonly mcpReq: Pick<ServerContext["mcpReq"], "envelope">;
}) => {
const envelope = context.mcpReq.envelope;
const client = implementation(
mcpEnvelopeValue(envelope, CLIENT_INFO_META_KEY),
);
const protocolVersion = mcpEnvelopeValue(envelope, PROTOCOL_VERSION_META_KEY);
return {
...(client === undefined ? {} : { client }),
clientFeatures: capabilityFeatures(
mcpEnvelopeValue(envelope, CLIENT_CAPABILITIES_META_KEY),
),
...(typeof protocolVersion === "string" ? { protocolVersion } : {}),
};
};
/** Read an SDK-defined metadata key from the SDK's currently opaque envelope type. */
export const mcpEnvelopeValue = (
envelope: ServerContext["mcpReq"]["envelope"],
key: string,
): unknown => (envelope === undefined ? undefined : Reflect.get(envelope, key));
const implementation = (value: unknown): ConnectedClientIdentity | undefined =>
isRecord(value) &&
typeof value.name === "string" &&
typeof value.version === "string"
? { name: value.name, version: value.version }
: undefined;
const capabilityFeatures = (value: unknown): ClientFeatureAvailability => {
const capabilities = isRecord(value) ? value : {};
const elicitation = isRecord(capabilities.elicitation)
? capabilities.elicitation
: {};
return {
elicitation_form: elicitation.form !== undefined,
elicitation_url: elicitation.url !== undefined,
roots: capabilities.roots !== undefined,
sampling: capabilities.sampling !== undefined,
};
};
const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);