Skip to content

Commit ee4b232

Browse files
cliffhallclaude
andcommitted
refactor(core): narrow readUiMeta from unknown; document ui-meta precedence
Address round-1 review nits on apps.ts: - readUiMeta now accepts `unknown` and narrows internally, removing the three `as WithUiMeta` casts at its call sites (readability win). - Add a comment documenting that the content-block-then-result precedence for `_meta.ui` is intentional (posture lives on the content block; no shallow merge). - Add a TODO to switch to the named McpUiToolMeta/McpUiResourceMeta types once the upstream extensionless re-export is fixed for NodeNext. Coverage: apps.ts 100% lines / 97.05% branch / 100% funcs — clears the gate. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01S3fTN8H3R8YV4yUGvZjYnX
1 parent 1a6e32b commit ee4b232

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

‎core/mcp/apps.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ type WithUiMeta = { _meta?: { ui?: unknown } };
6868
* package's `.d.ts`, so we read the fields structurally instead. The values
6969
* pass through verbatim into {@link AppInfo}; callers can narrow them against
7070
* the published Zod schemas if they need strict typing.
71+
*
72+
* TODO: switch to the named `McpUiToolMeta` / `McpUiResourceMeta` types once
73+
* upstream fixes the extensionless re-export so they resolve under NodeNext.
7174
*/
7275
interface UiMetaShape {
7376
visibility?: readonly string[];
@@ -77,8 +80,16 @@ interface UiMetaShape {
7780
prefersBorder?: boolean;
7881
}
7982

80-
function readUiMeta(carrier: WithUiMeta | undefined): UiMetaShape | undefined {
81-
const ui = carrier?._meta?.ui;
83+
/**
84+
* Reads a carrier's `_meta.ui` structurally, narrowing from `unknown` so call
85+
* sites don't need their own casts. Returns `undefined` unless both the carrier
86+
* and its `_meta.ui` are non-null objects.
87+
*/
88+
function readUiMeta(carrier: unknown): UiMetaShape | undefined {
89+
const ui =
90+
carrier !== null && typeof carrier === "object"
91+
? (carrier as WithUiMeta)._meta?.ui
92+
: undefined;
8293
return ui !== null && typeof ui === "object"
8394
? (ui as UiMetaShape)
8495
: undefined;
@@ -135,11 +146,14 @@ export function extractAppInfo(
135146
if (resourceUri === undefined) {
136147
return { hasApp: false, toolName: tool.name };
137148
}
138-
const toolUi = readUiMeta(tool as WithUiMeta);
149+
const toolUi = readUiMeta(tool);
139150
const content = resource && findResourceContent(resource, resourceUri);
140-
const resourceUi =
141-
readUiMeta(content as WithUiMeta | undefined) ??
142-
readUiMeta(resource as WithUiMeta | undefined);
151+
// Precedence is intentional: prefer the matched content block's `_meta.ui`,
152+
// falling back to the result-level `_meta.ui`. Per the current spec the
153+
// security posture (csp/permissions/domain) lives on the content block, so we
154+
// don't shallow-merge the two carriers — a content block that declares any
155+
// `ui` is treated as authoritative for all of its fields.
156+
const resourceUi = readUiMeta(content) ?? readUiMeta(resource);
143157
return {
144158
hasApp: true,
145159
toolName: tool.name,

0 commit comments

Comments
 (0)