inspector/test-servers/src/resolve-config.ts
cliffhall 08fc20a22d feat(apps): negotiated app-rendered form elicitations
Let a server hand a form `elicitation/create` to an MCP App and return the
app's ordinary `ElicitResult`, with the native elicitation form as the
fallback on every failure.

No second extension, no custom method, and no custom result shape: the only
new wire surface is a nested `elicitation` flag on the existing
`io.modelcontextprotocol/ui` extension on each side, plus
`_meta.ui.resourceUri` on the request. App rendering is selected only when
all four gates hold — client `elicitation.form`, client MCP Apps MIME type,
the nested `elicitation` setting on BOTH peers, and a valid absolute `ui://`
URI on the request.

Only the web client advertises the nested client-side setting, and only
because it has a sandbox renderer: supplying
`InspectorClientOptions.appElicitation` is what opts a client in, so CLI and
TUI keep advertising the MIME type without ever claiming they can resolve an
elicitation through an app.

Routing lives in the one funnel both entry points already use
(`enqueuePendingElicitation`), so the inbound handler and the MRTR driver
cannot diverge. Ownership is request-scoped — a per-request id keys the
renderer, iframe and bridge — so two concurrent elicitations can never
resolve through each other's bridges. An explicit `decline`/`cancel` is a
completed elicitation and goes back to the server; everything else (absent or
malformed metadata, resource/sandbox/bridge failure, an app with no
elicitation capability, a timeout, an invalid result or one that fails the
requested schema) falls back.

The Inspector speaks the ext-apps#733 / SEP-3118 wire protocol but cannot yet
consume its helpers — the released `@modelcontextprotocol/ext-apps` (1.7.5)
predates that PR. `core/mcp/appElicitation.ts` and
`AppRenderer/requestAppElicitation.ts` mirror it exactly and are marked for
deletion once a release containing it ships. One consequence needed its own
seam: ext-apps 1.7.5 parses the view's `ui/initialize` through a schema that
strips `elicitation`, so an app that correctly advertises it would look like
one that did not, silently turning every negotiated elicitation into a
fallback. `AppRenderer/appCapabilities.ts` records the raw frame instead, and
prefers the bridge's own value once it carries the key.

`AppRenderer` now takes an `AppRenderSource` union rather than a `Tool`, so
the same renderer and bridge factory serve an App tool and an elicitation
without either faking the other's shape.

Adds the public fixture (`app_choose_option` + a self-contained
`ui://demo/choose-option.html` app covering accept/decline/cancel), its two
showcase configs, and `smoke:web:elicit`, which drives the negotiated path
end to end in headless Chromium and then the same tool against a server that
never advertised the capability, asserting the native form takes it.

Closes #1854

Signed-off-by: cliffhall <cliff@futurescale.com>
2026-08-22 19:05:33 -04:00

119 lines
3.8 KiB
TypeScript

/**
* Resolves config file preset refs to ServerConfig for createMcpServer
*/
import type { ServerConfig } from "./composable-test-server.js";
import type {
ToolDefinition,
TaskToolDefinition,
ResourceDefinition,
PromptDefinition,
ResourceTemplateDefinition,
} from "./composable-test-server.js";
import { createTestServerInfo } from "./test-server-fixtures.js";
import { resolvePreset } from "./preset-registry.js";
import type { ConfigFile, PresetRef } from "./load-config.js";
function mergeRequiredScopes<T extends { requiredScopes?: string[] }>(
item: T,
ref: PresetRef,
): T {
if (!ref.requiredScopes?.length) {
return item;
}
return { ...item, requiredScopes: ref.requiredScopes };
}
function resolvePresetRefs<T extends { requiredScopes?: string[] }>(
refs: Array<PresetRef | PresetRef[]> | undefined,
type: "tool" | "resource" | "resourceTemplate" | "prompt",
): T[] {
if (!refs || refs.length === 0) return [];
const result: Array<{ requiredScopes?: string[] }> = [];
for (const entry of refs) {
const items = Array.isArray(entry) ? entry : [entry];
for (const ref of items) {
const presetName = ref.preset;
if (!presetName || typeof presetName !== "string") {
throw new Error(
`Invalid preset ref: preset must be a non-empty string`,
);
}
const resolved = resolvePreset(type, presetName, ref.params);
const arr = Array.isArray(resolved) ? resolved : [resolved];
for (const item of arr) {
result.push(mergeRequiredScopes(item, ref));
}
}
}
// The caller pairs `type` with the matching `T`, so each resolved preset is
// the requested definition kind; TS can't see that correspondence.
return result as T[];
}
/**
* Resolve config file to ServerConfig for createMcpServer
*/
export function resolveConfig(config: ConfigFile): ServerConfig {
const tools = resolvePresetRefs<ToolDefinition | TaskToolDefinition>(
config.tools,
"tool",
);
const resources = resolvePresetRefs<ResourceDefinition>(
config.resources,
"resource",
);
const resourceTemplates = resolvePresetRefs<ResourceTemplateDefinition>(
config.resourceTemplates,
"resourceTemplate",
);
const prompts = resolvePresetRefs<PromptDefinition>(config.prompts, "prompt");
const serverInfo = createTestServerInfo(
config.serverInfo.name,
config.serverInfo.version,
);
const transport = config.transport;
const isHttp =
transport.type === "streamable-http" || transport.type === "sse";
const serverConfig: ServerConfig = {
serverInfo,
tools: tools.length > 0 ? tools : undefined,
resources: resources.length > 0 ? resources : undefined,
resourceTemplates:
resourceTemplates.length > 0 ? resourceTemplates : undefined,
prompts: prompts.length > 0 ? prompts : undefined,
logging: config.logging,
listChanged: config.listChanged,
subscriptions: config.subscriptions,
tasks: config.tasks,
tasksExtension: config.tasksExtension,
appElicitation: config.appElicitation,
maxPageSize: config.maxPageSize,
duplicateToolNames: config.duplicateToolNames,
extensionGatedTools: config.extensionGatedTools,
serverType: isHttp
? (transport.type as "sse" | "streamable-http")
: undefined,
port: isHttp ? transport.port : undefined,
};
// Normalize the modern flag: `true` is shorthand for the default (dual-era
// stateless) posture; the object form passes through.
if (transport.modern !== undefined) {
serverConfig.modern =
transport.modern === true ? {} : transport.modern || undefined;
}
if (config.oauth) {
const { issuerUrl, ...oauthRest } = config.oauth;
serverConfig.oauth = {
...oauthRest,
...(issuerUrl && { issuerUrl: new URL(issuerUrl) }),
};
}
return serverConfig;
}