inspector/core/mcp/apps.ts
cliffhall a42d7fd99e refactor: drop the ext-apps SDK-v1 shims now that 2.0.0 peers on SDK v2
Every site tagged `ext-apps#702` carried a workaround for ext-apps
peering on the v1 SDK while the Inspector runs v2. With 2.0.0 the
package's `AppBridge` extends the v2 `Protocol`, takes the v2 `Client`
and `Implementation` directly, and registers its list-changed forwarding
on that client with `setNotificationHandler("notifications/…", handler)`,
so:

- core/mcp/apps.ts: `getToolUiResourceUri(tool)` takes the v2 `Tool`
  without a cast; `UiMetaShape` is replaced by ext-apps' own
  `McpUiToolMeta` / `McpUiResourceMeta`, which resolve under NodeNext
  since ext-apps#705. `AppInfo.csp` / `.permissions` / `.visibility`
  take the named types too.
- core/mcp/inspectorClient.ts: `getAppRendererClient()` returns the SDK
  client itself; the Proxy that translated the 1.x schema-first
  `setNotificationHandler(Schema, handler)` into the method-string form,
  its `notificationMethodFromSchema` helper and its cached field are
  gone. `AppRendererClient` is now `Client` rather than `unknown`.
- createAppBridgeFactory.ts: `new AppBridge(client, HOST_INFO, …)` with
  no `as unknown as`; `resolveSourceUri` passes the tool straight through.
- AppRenderer.tsx: `sendToolResult(result)` — its params ARE the v2
  `CallToolResult` now.
- requestAppElicitation.ts: the double cast on the `elicitation/create`
  send goes too — v2 `Protocol.request` takes the standard `Request`
  shape, which `ElicitRequest` is. The helper itself stays: ext-apps#733
  is not in 2.0.0.
- core/mcp/extensions.ts: `MCP_APP_MIME_TYPE` re-exports ext-apps'
  `RESOURCE_MIME_TYPE` from `/app-bridge`; `UI_EXTENSION_KEY` stays
  hardcoded because `EXTENSION_ID` is still `/server`-only in 2.0.0.

Tests: the Proxy-translation test is removed with the Proxy; the
`getAppRendererClient` tests assert the real `Client` instance and that
disconnect withdraws it; the `useInspectorClient` sentinel is a real
(unconnected) `Client`. Comments that cited ext-apps 1.7.5 now cite 2.0.0
where the fact still holds (#733 still unreleased).

Closes #1745
Refs #2309

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PsmUAZ6aLUAFqenjJKSSRp
Signed-off-by: cliffhall <cliff@futurescale.com>
2026-09-13 19:21:06 -04:00

167 lines
6.9 KiB
TypeScript

import { getToolUiResourceUri } from "@modelcontextprotocol/ext-apps/app-bridge";
import type {
McpUiResourceCsp,
McpUiResourceMeta,
McpUiResourcePermissions,
McpUiToolMeta,
McpUiToolVisibility,
} from "@modelcontextprotocol/ext-apps/app-bridge";
import type { ReadResourceResult, Tool } from "@modelcontextprotocol/client";
/**
* Returns the UI resource URI advertised by an MCP App tool, or `undefined`
* for non-App tools.
*
* Reads from `tool._meta.ui.resourceUri` (preferred nested format) and falls
* back to the deprecated flat `tool._meta["ui/resourceUri"]` key. The nested
* format wins when both are present.
*
* Throws when `_meta` advertises a UI resource URI that is not a string
* starting with `ui://`. We surface the underlying ext-apps error rather than
* silently dropping the tool, because a malformed URI is a server bug worth
* making visible.
*
* Wraps `getToolUiResourceUri` from `@modelcontextprotocol/ext-apps/app-bridge`
* so that web, CLI, and TUI all consume the same implementation through
* `@inspector/core`.
*/
export function getAppResourceUri(tool: Tool): string | undefined {
return getToolUiResourceUri(tool);
}
/**
* Single source of truth for App-tool detection across all Inspector clients.
* Wraps {@link getAppResourceUri}; throws on a malformed `_meta.ui.resourceUri`
* for the same reason.
*
* Note: because this throws on malformed URIs, callers using
* `tools.filter(isAppTool)` will halt iteration on the first bad tool. Wrap in
* a try/catch (or a safe-predicate helper) at the call site if you need to
* tolerate mixed-validity tool lists.
*/
export function isAppTool(tool: Tool): boolean {
return getAppResourceUri(tool) !== undefined;
}
/**
* Machine-readable summary of an MCP App's UI metadata, combining what the
* tool advertises (`resourceUri`, `visibility`) with what the UI resource
* itself declares (`csp`, `permissions`, `domain`, `prefersBorder`). Emitted by
* the CLI's `--app-info` mode and reusable by any client that needs to surface
* an app's security posture without rendering it.
*/
export interface AppInfo {
hasApp: boolean;
toolName: string;
resourceUri?: string;
visibility?: readonly McpUiToolVisibility[];
/** From the UI resource's `_meta.ui` (per spec, csp/permissions/domain live on the resource, not the tool). Absent when the resource was not read. */
csp?: Readonly<McpUiResourceCsp>;
permissions?: Readonly<McpUiResourcePermissions>;
domain?: string;
prefersBorder?: boolean;
resourceMimeType?: string;
}
type WithUiMeta = { _meta?: { ui?: unknown } };
/**
* Reads a carrier's `_meta.ui` as one of ext-apps' named `_meta.ui` shapes —
* `McpUiToolMeta` for a tool, `McpUiResourceMeta` for a UI resource — narrowing
* from `unknown` so call sites don't need their own casts. Returns `undefined`
* unless both the carrier and its `_meta.ui` are non-null objects.
*
* The narrowing is structural, not validated: the fields pass through verbatim
* into {@link AppInfo}, and a caller that needs strict typing can parse them
* against the published Zod schemas (`McpUiToolMetaSchema` /
* `McpUiResourceMetaSchema`). Typed with the package's own names since ext-apps
* 2.0.0, whose declarations resolve under NodeNext (ext-apps#705) — the
* structural stand-in that preceded it is gone (#1745).
*/
function readUiMeta<T extends McpUiToolMeta | McpUiResourceMeta>(
carrier: unknown,
): T | undefined {
const ui =
carrier !== null && typeof carrier === "object"
? (carrier as WithUiMeta)._meta?.ui
: undefined;
return ui !== null && typeof ui === "object" ? (ui as T) : undefined;
}
/**
* Normalizes a `ui://…` URI for comparison so trivial server-side differences
* (case, trailing slash) don't cause us to miss the resource block and silently
* drop its `_meta.ui` (csp/permissions/domain). `ui://` is a non-special scheme
* so the WHATWG URL parser preserves case and trailing slashes verbatim — we
* apply a targeted lowercase + single trailing-slash strip instead.
*/
function normalizeResourceUri(uri: string): string {
const lowered = uri.toLowerCase();
return lowered.endsWith("/") ? lowered.slice(0, -1) : lowered;
}
/**
* Locates the content block in a `resources/read` result that corresponds to
* the requested UI resource. Tries exact match, then normalized-URI match, then
* falls back to the sole content block when there is only one — `resources/read`
* by definition returns the content for the URI we asked for, so a single-block
* response is the right block even when the server echoes the URI back in a
* slightly different form.
*/
function findResourceContent(
resource: ReadResourceResult,
requestedUri: string,
): ReadResourceResult["contents"][number] | undefined {
const exact = resource.contents.find((c) => c.uri === requestedUri);
if (exact) return exact;
const norm = normalizeResourceUri(requestedUri);
const normalized = resource.contents.find(
(c) => normalizeResourceUri(c.uri) === norm,
);
if (normalized) return normalized;
return resource.contents.length === 1 ? resource.contents[0] : undefined;
}
/**
* Build an {@link AppInfo} from a tool definition and (optionally) the
* `resources/read` result for its UI resource. Per the spec, `csp` /
* `permissions` / `domain` belong on the resource — not the tool — so a caller
* that wants the security posture must read the resource and pass it here.
*
* Returns `{ hasApp: false, toolName }` for non-App tools. Throws on a
* malformed `resourceUri` for the same reason {@link getAppResourceUri} does.
*/
export function extractAppInfo(
tool: Tool,
resource?: ReadResourceResult,
): AppInfo {
const resourceUri = getAppResourceUri(tool);
if (resourceUri === undefined) {
return { hasApp: false, toolName: tool.name };
}
const toolUi = readUiMeta<McpUiToolMeta>(tool);
const content = resource && findResourceContent(resource, resourceUri);
// Precedence is intentional: prefer the matched content block's `_meta.ui`,
// falling back to the result-level `_meta.ui`. Per the current spec the
// security posture (csp/permissions/domain) lives on the content block, so we
// don't shallow-merge the two carriers — a content block that declares any
// `ui` is treated as authoritative for all of its fields.
const resourceUi =
readUiMeta<McpUiResourceMeta>(content) ??
readUiMeta<McpUiResourceMeta>(resource);
return {
hasApp: true,
toolName: tool.name,
resourceUri,
...(toolUi?.visibility ? { visibility: toolUi.visibility } : {}),
...(resourceUi?.csp ? { csp: resourceUi.csp } : {}),
...(resourceUi?.permissions ? { permissions: resourceUi.permissions } : {}),
...(resourceUi?.domain ? { domain: resourceUi.domain } : {}),
...(resourceUi?.prefersBorder !== undefined
? { prefersBorder: resourceUi.prefersBorder }
: {}),
...(typeof content?.mimeType === "string"
? { resourceMimeType: content.mimeType }
: {}),
};
}