@mcpview/components 0.0.0-stage → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +575 -2
- package/esm/_dnt.polyfills.d.ts +12 -0
- package/esm/_dnt.polyfills.d.ts.map +1 -0
- package/esm/_dnt.polyfills.js +15 -0
- package/esm/fonts.d.ts +13 -0
- package/esm/fonts.d.ts.map +1 -0
- package/esm/fonts.js +12 -0
- package/esm/layout.d.ts +14 -0
- package/esm/layout.d.ts.map +1 -0
- package/esm/layout.js +13 -0
- package/esm/mod.d.ts +19 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +14 -0
- package/esm/package.json +3 -0
- package/esm/preact-components.d.ts +22 -0
- package/esm/preact-components.d.ts.map +1 -0
- package/esm/preact-components.js +20 -0
- package/esm/preact.d.ts +10 -0
- package/esm/preact.d.ts.map +1 -0
- package/esm/preact.js +9 -0
- package/esm/src/component-primitives.d.ts +41 -0
- package/esm/src/component-primitives.d.ts.map +1 -0
- package/esm/src/component-primitives.js +121 -0
- package/esm/src/components.d.ts +50 -0
- package/esm/src/components.d.ts.map +1 -0
- package/esm/src/components.js +283 -0
- package/esm/src/font-families.d.ts +16 -0
- package/esm/src/font-families.d.ts.map +1 -0
- package/esm/src/font-families.js +11 -0
- package/esm/src/fonts-data.d.ts +2 -0
- package/esm/src/fonts-data.d.ts.map +1 -0
- package/esm/src/fonts-data.js +42 -0
- package/esm/src/fonts.d.ts +12 -0
- package/esm/src/fonts.d.ts.map +1 -0
- package/esm/src/fonts.js +19 -0
- package/esm/src/i18n.d.ts +44 -0
- package/esm/src/i18n.d.ts.map +1 -0
- package/esm/src/i18n.js +98 -0
- package/esm/src/layout/hooks.d.ts +60 -0
- package/esm/src/layout/hooks.d.ts.map +1 -0
- package/esm/src/layout/hooks.js +121 -0
- package/esm/src/layout/mod.d.ts +6 -0
- package/esm/src/layout/mod.d.ts.map +1 -0
- package/esm/src/layout/mod.js +3 -0
- package/esm/src/layout/viewer-layout.d.ts +82 -0
- package/esm/src/layout/viewer-layout.d.ts.map +1 -0
- package/esm/src/layout/viewer-layout.js +74 -0
- package/esm/src/preact/components.d.ts +215 -0
- package/esm/src/preact/components.d.ts.map +1 -0
- package/esm/src/preact/components.js +176 -0
- package/esm/src/preact/focused-view.d.ts +42 -0
- package/esm/src/preact/focused-view.d.ts.map +1 -0
- package/esm/src/preact/focused-view.js +20 -0
- package/esm/src/preact/mod.d.ts +6 -0
- package/esm/src/preact/mod.d.ts.map +1 -0
- package/esm/src/preact/mod.js +3 -0
- package/esm/src/preact/plots.d.ts +119 -0
- package/esm/src/preact/plots.d.ts.map +1 -0
- package/esm/src/preact/plots.js +152 -0
- package/esm/src/preact/semantic-element.d.ts +103 -0
- package/esm/src/preact/semantic-element.d.ts.map +1 -0
- package/esm/src/preact/semantic-element.js +103 -0
- package/esm/src/preact/structural.d.ts +178 -0
- package/esm/src/preact/structural.d.ts.map +1 -0
- package/esm/src/preact/structural.js +147 -0
- package/esm/src/preact/surface.d.ts +37 -0
- package/esm/src/preact/surface.d.ts.map +1 -0
- package/esm/src/preact/surface.js +47 -0
- package/esm/src/surface-app.d.ts +188 -0
- package/esm/src/surface-app.d.ts.map +1 -0
- package/esm/src/surface-app.js +374 -0
- package/esm/src/theme.d.ts +65 -0
- package/esm/src/theme.d.ts.map +1 -0
- package/esm/src/theme.js +1877 -0
- package/esm/surface.d.ts +12 -0
- package/esm/surface.d.ts.map +1 -0
- package/esm/surface.js +10 -0
- package/package.json +68 -4
- package/script/_dnt.polyfills.d.ts +12 -0
- package/script/_dnt.polyfills.d.ts.map +1 -0
- package/script/_dnt.polyfills.js +16 -0
- package/script/fonts.d.ts +13 -0
- package/script/fonts.d.ts.map +1 -0
- package/script/fonts.js +19 -0
- package/script/layout.d.ts +14 -0
- package/script/layout.d.ts.map +1 -0
- package/script/layout.js +29 -0
- package/script/mod.d.ts +19 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +40 -0
- package/script/package.json +3 -0
- package/script/preact-components.d.ts +22 -0
- package/script/preact-components.d.ts.map +1 -0
- package/script/preact-components.js +68 -0
- package/script/preact.d.ts +10 -0
- package/script/preact.d.ts.map +1 -0
- package/script/preact.js +25 -0
- package/script/src/component-primitives.d.ts +41 -0
- package/script/src/component-primitives.d.ts.map +1 -0
- package/script/src/component-primitives.js +127 -0
- package/script/src/components.d.ts +50 -0
- package/script/src/components.d.ts.map +1 -0
- package/script/src/components.js +297 -0
- package/script/src/font-families.d.ts +16 -0
- package/script/src/font-families.d.ts.map +1 -0
- package/script/src/font-families.js +14 -0
- package/script/src/fonts-data.d.ts +2 -0
- package/script/src/fonts-data.d.ts.map +1 -0
- package/script/src/fonts-data.js +45 -0
- package/script/src/fonts.d.ts +12 -0
- package/script/src/fonts.d.ts.map +1 -0
- package/script/src/fonts.js +24 -0
- package/script/src/i18n.d.ts +44 -0
- package/script/src/i18n.d.ts.map +1 -0
- package/script/src/i18n.js +102 -0
- package/script/src/layout/hooks.d.ts +60 -0
- package/script/src/layout/hooks.d.ts.map +1 -0
- package/script/src/layout/hooks.js +126 -0
- package/script/src/layout/mod.d.ts +6 -0
- package/script/src/layout/mod.d.ts.map +1 -0
- package/script/src/layout/mod.js +17 -0
- package/script/src/layout/viewer-layout.d.ts +82 -0
- package/script/src/layout/viewer-layout.d.ts.map +1 -0
- package/script/src/layout/viewer-layout.js +83 -0
- package/script/src/preact/components.d.ts +215 -0
- package/script/src/preact/components.d.ts.map +1 -0
- package/script/src/preact/components.js +199 -0
- package/script/src/preact/focused-view.d.ts +42 -0
- package/script/src/preact/focused-view.d.ts.map +1 -0
- package/script/src/preact/focused-view.js +24 -0
- package/script/src/preact/mod.d.ts +6 -0
- package/script/src/preact/mod.d.ts.map +1 -0
- package/script/src/preact/mod.js +55 -0
- package/script/src/preact/plots.d.ts +119 -0
- package/script/src/preact/plots.d.ts.map +1 -0
- package/script/src/preact/plots.js +157 -0
- package/script/src/preact/semantic-element.d.ts +103 -0
- package/script/src/preact/semantic-element.d.ts.map +1 -0
- package/script/src/preact/semantic-element.js +115 -0
- package/script/src/preact/structural.d.ts +178 -0
- package/script/src/preact/structural.d.ts.map +1 -0
- package/script/src/preact/structural.js +158 -0
- package/script/src/preact/surface.d.ts +37 -0
- package/script/src/preact/surface.d.ts.map +1 -0
- package/script/src/preact/surface.js +53 -0
- package/script/src/surface-app.d.ts +188 -0
- package/script/src/surface-app.d.ts.map +1 -0
- package/script/src/surface-app.js +379 -0
- package/script/src/theme.d.ts +65 -0
- package/script/src/theme.d.ts.map +1 -0
- package/script/src/theme.js +1881 -0
- package/script/surface.d.ts +12 -0
- package/script/surface.d.ts.map +1 -0
- package/script/surface.js +15 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Preact bindings for result-driven MCP App component surfaces. */
|
|
2
|
+
import { createElement, render } from "preact";
|
|
3
|
+
import { defineViewComponent, } from "../components.js";
|
|
4
|
+
import { startSurfaceApp, } from "../surface-app.js";
|
|
5
|
+
import { renderStatusMessage } from "./components.js";
|
|
6
|
+
export { SurfaceAppError } from "../surface-app.js";
|
|
7
|
+
export const preactSurfaceRenderer = {
|
|
8
|
+
mount(component, props, target) {
|
|
9
|
+
render(createElement(component, props), target);
|
|
10
|
+
},
|
|
11
|
+
unmount(target) {
|
|
12
|
+
render(null, target);
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
/** Turn one Preact component into a renderer-neutral mcp-view component. */
|
|
16
|
+
export function definePreactComponent(descriptor, component, renderer = preactSurfaceRenderer) {
|
|
17
|
+
return defineViewComponent({
|
|
18
|
+
descriptor,
|
|
19
|
+
mount(target, context) {
|
|
20
|
+
renderer.mount(component, {
|
|
21
|
+
data: context.data,
|
|
22
|
+
context: context.appContext,
|
|
23
|
+
instanceId: context.instanceId,
|
|
24
|
+
props: context.props,
|
|
25
|
+
}, target);
|
|
26
|
+
return () => renderer.unmount(target);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Start a result-driven MCP App whose statuses render through the Preact
|
|
32
|
+
* `StateMessage`. Everything else is `startSurfaceApp`; the shell keeps the
|
|
33
|
+
* `mcp-view-preact-surface` class 0.5 viewers style.
|
|
34
|
+
*/
|
|
35
|
+
export async function startPreactSurfaceApp(options, runtime) {
|
|
36
|
+
const { statusClassName, renderStatus, surfaceClassName = "mcp-view-preact-surface", ...rest } = options;
|
|
37
|
+
return await startSurfaceApp({
|
|
38
|
+
...rest,
|
|
39
|
+
surfaceClassName,
|
|
40
|
+
renderStatus: renderStatus ?? ((status) => renderStatusMessage(status.message, {
|
|
41
|
+
title: status.title,
|
|
42
|
+
tone: status.tone,
|
|
43
|
+
busy: status.busy,
|
|
44
|
+
className: statusClassName,
|
|
45
|
+
})),
|
|
46
|
+
}, runtime);
|
|
47
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result-driven MCP App over one component registry, renderer-neutral.
|
|
3
|
+
*
|
|
4
|
+
* Every componentized viewer repeats the same lifecycle: install the theme,
|
|
5
|
+
* show a status until the host sends a result, project that result into a
|
|
6
|
+
* data model, mount the host-selected surface, remount it when the host
|
|
7
|
+
* context changes, and make sure only the latest mount survives. Copies of
|
|
8
|
+
* that loop drifted — one lost the generation guard in its error path,
|
|
9
|
+
* another buffered recorded sessions by hand. This module is the loop, once.
|
|
10
|
+
* Renderers plug in through `renderStatus`; `startPreactSurfaceApp` binds it
|
|
11
|
+
* to the Preact `StateMessage`.
|
|
12
|
+
*/
|
|
13
|
+
import { type AppConfig, type AppContext, createMcpApp } from "@mcpview/view";
|
|
14
|
+
import { type ComponentSurface, type ViewComponentRegistry } from "./components.js";
|
|
15
|
+
import type { ComponentTone } from "./component-primitives.js";
|
|
16
|
+
import { type McpViewMessages, type Translator } from "./i18n.js";
|
|
17
|
+
export type SurfaceLabel = string | ((locale?: string) => string);
|
|
18
|
+
export interface SurfaceAppState<TData> {
|
|
19
|
+
currentData?: TData;
|
|
20
|
+
}
|
|
21
|
+
export type SurfaceAppContext<TData> = AppContext<SurfaceAppState<TData>>;
|
|
22
|
+
/** The host tool result exactly as `createMcpApp` delivers it. */
|
|
23
|
+
export type SurfaceToolResult = Parameters<NonNullable<AppConfig<SurfaceAppState<unknown>>["onToolResult"]>>[0];
|
|
24
|
+
type HostApp = AppContext<SurfaceAppState<unknown>>["app"];
|
|
25
|
+
/** The server reads an App may need while projecting a result or a session. */
|
|
26
|
+
export interface SurfaceHostAccess {
|
|
27
|
+
/** Presentation locale at projection time, never an input to domain validation. */
|
|
28
|
+
readonly locale?: string;
|
|
29
|
+
readonly readServerResource: (uri: string) => ReturnType<HostApp["readServerResource"]>;
|
|
30
|
+
}
|
|
31
|
+
export type SurfaceStatusTone = ComponentTone;
|
|
32
|
+
/**
|
|
33
|
+
* What the App displays. `result` mounts the component surface; every other
|
|
34
|
+
* kind renders one status through `renderStatus`. `notice` carries a
|
|
35
|
+
* caller-owned presentation for domain states such as "recorded evidence
|
|
36
|
+
* unresolved" that are neither an error nor an absence of data. `code` is a
|
|
37
|
+
* caller-owned machine identifier, forwarded verbatim to the status.
|
|
38
|
+
* `error` and `notice` `title`/`message` accept `SurfaceLabel` callbacks,
|
|
39
|
+
* resolved on each visible status render; literal strings stay unchanged.
|
|
40
|
+
*/
|
|
41
|
+
export type SurfaceDisplayState<TData> = {
|
|
42
|
+
readonly kind: "loading";
|
|
43
|
+
} | {
|
|
44
|
+
readonly kind: "empty";
|
|
45
|
+
} | {
|
|
46
|
+
readonly kind: "error";
|
|
47
|
+
readonly message: SurfaceLabel;
|
|
48
|
+
readonly title?: SurfaceLabel;
|
|
49
|
+
readonly code?: string;
|
|
50
|
+
} | {
|
|
51
|
+
readonly kind: "notice";
|
|
52
|
+
readonly message: SurfaceLabel;
|
|
53
|
+
readonly title?: SurfaceLabel;
|
|
54
|
+
readonly tone?: SurfaceStatusTone;
|
|
55
|
+
readonly busy?: boolean;
|
|
56
|
+
readonly code?: string;
|
|
57
|
+
} | {
|
|
58
|
+
readonly kind: "result";
|
|
59
|
+
readonly result: TData;
|
|
60
|
+
};
|
|
61
|
+
export type SurfaceMessageKind = "loading" | "empty" | "error" | "notice" | "surface-required";
|
|
62
|
+
/** One status resolved to its presentation, ready for a renderer. */
|
|
63
|
+
export interface SurfaceStatus {
|
|
64
|
+
readonly kind: SurfaceMessageKind;
|
|
65
|
+
readonly message: string;
|
|
66
|
+
readonly title?: string;
|
|
67
|
+
readonly tone: SurfaceStatusTone;
|
|
68
|
+
readonly busy: boolean;
|
|
69
|
+
/** Caller-owned identifier from a `notice` or `error` state, never invented. */
|
|
70
|
+
readonly code?: string;
|
|
71
|
+
}
|
|
72
|
+
export type SurfaceProjection<TInput, TData> = (input: TInput, host: SurfaceHostAccess) => SurfaceDisplayState<TData> | Promise<SurfaceDisplayState<TData>>;
|
|
73
|
+
/** Whole-view `viewer.session.apply` handling, installed before the App connects. */
|
|
74
|
+
export interface SurfaceViewerSession<TData, TSession> {
|
|
75
|
+
readonly validate: (value: unknown) => value is TSession;
|
|
76
|
+
/** Project one validated session exactly as a tool result would be. */
|
|
77
|
+
readonly toState: SurfaceProjection<TSession, TData>;
|
|
78
|
+
readonly onInvalid?: (value: unknown) => void;
|
|
79
|
+
}
|
|
80
|
+
export interface SurfaceAppOptions<TData, TSession = never> {
|
|
81
|
+
readonly root: HTMLElement;
|
|
82
|
+
readonly info: {
|
|
83
|
+
readonly name: string;
|
|
84
|
+
readonly version: string;
|
|
85
|
+
};
|
|
86
|
+
readonly registry: ViewComponentRegistry<TData, SurfaceAppContext<TData>>;
|
|
87
|
+
/**
|
|
88
|
+
* Project one host tool result into a display state. Without it the result's
|
|
89
|
+
* structured content (or JSON text) is the data, checked by `validate`.
|
|
90
|
+
*
|
|
91
|
+
* A throw becomes an `error` status and drops the remembered result, like
|
|
92
|
+
* any non-`result` state. To keep the last values visible under a failure,
|
|
93
|
+
* return a `result` state that carries the failure — the scaffold does.
|
|
94
|
+
*/
|
|
95
|
+
readonly fromToolResult?: SurfaceProjection<SurfaceToolResult, TData>;
|
|
96
|
+
/** Domain guard for the default projection. Exclusive with `fromToolResult`. */
|
|
97
|
+
readonly validate?: (value: unknown) => value is TData;
|
|
98
|
+
readonly viewerSession?: SurfaceViewerSession<TData, TSession>;
|
|
99
|
+
/**
|
|
100
|
+
* The surface a result owns. A returned surface composes that result as is:
|
|
101
|
+
* neither the host selection nor the registry default is consulted, so a
|
|
102
|
+
* recorded session that replaces the whole read model can compose itself.
|
|
103
|
+
* `undefined` keeps the host flow (host-selected surface, then the registry
|
|
104
|
+
* default). Called on every mount of the result, remounts included.
|
|
105
|
+
*/
|
|
106
|
+
readonly surfaceFor?: (data: TData) => ComponentSurface | undefined;
|
|
107
|
+
/** Detail of the `loading` status. Default `Waiting for data…`. */
|
|
108
|
+
readonly loadingLabel?: SurfaceLabel;
|
|
109
|
+
/** Detail of the `empty` status. Default `No structured data received.`. */
|
|
110
|
+
readonly emptyLabel?: SurfaceLabel;
|
|
111
|
+
/**
|
|
112
|
+
* Detail of the `surface-required` status. Default
|
|
113
|
+
* `This App exposes components and requires a host-selected surface.`.
|
|
114
|
+
*/
|
|
115
|
+
readonly surfaceRequiredLabel?: SurfaceLabel;
|
|
116
|
+
/** Translate interface wording only; domain messages and codes remain caller-owned. */
|
|
117
|
+
readonly messages?: (locale?: string) => Translator<keyof McpViewMessages>;
|
|
118
|
+
/**
|
|
119
|
+
* Sets `document.documentElement.lang` at boot and when the host locale
|
|
120
|
+
* changes. Pass `messages.locale` from `createTranslator` so the document
|
|
121
|
+
* language identifies the selected dictionary. Omit to leave `lang` as is.
|
|
122
|
+
* Independent of remount policy; not inferred from message callbacks.
|
|
123
|
+
*/
|
|
124
|
+
readonly documentLanguage?: (hostLocale?: string) => string;
|
|
125
|
+
/**
|
|
126
|
+
* Opt into in-place theme-only updates when components follow CSS tokens (or
|
|
127
|
+
* observe them, e.g. a 3D scene). Other context changes still remount the surface.
|
|
128
|
+
* Default `remount` preserves components that read the theme only at mount.
|
|
129
|
+
* In-place mode compares non-theme fields structurally so equal JSON snapshots
|
|
130
|
+
* with new object identities do not remount.
|
|
131
|
+
*/
|
|
132
|
+
readonly themeUpdates?: "remount" | "in-place";
|
|
133
|
+
/** Class of the element wrapping the mounted surface. Default `mcp-view-surface-shell`. */
|
|
134
|
+
readonly surfaceClassName?: string;
|
|
135
|
+
/**
|
|
136
|
+
* Install the shared mcp-view stylesheet at boot. Default `true`. Only the
|
|
137
|
+
* boot install is skipped: kit primitives still install it when they mount,
|
|
138
|
+
* and the host theme (`createMcpApp`'s `autoTheme`) is unaffected.
|
|
139
|
+
*/
|
|
140
|
+
readonly theme?: boolean;
|
|
141
|
+
/** Forwarded to `createMcpApp`; unset keeps its default (`false`). */
|
|
142
|
+
readonly strict?: boolean;
|
|
143
|
+
/** Render one resolved status as the element the router mounts. */
|
|
144
|
+
readonly renderStatus: (status: SurfaceStatus) => Node;
|
|
145
|
+
/** Receives every failure the lifecycle absorbs. Default `console.error`. */
|
|
146
|
+
readonly onError?: (error: unknown) => void;
|
|
147
|
+
/**
|
|
148
|
+
* Runs once when the host tears the App down or `dispose()` runs, before
|
|
149
|
+
* the router disposes the surface. For what the App holds outside its
|
|
150
|
+
* surface — a bridge to the parent page, a subscription — since the handle
|
|
151
|
+
* has no other end-of-life signal. A throw is absorbed through `onError`.
|
|
152
|
+
*/
|
|
153
|
+
readonly onTeardown?: () => void | Promise<void>;
|
|
154
|
+
}
|
|
155
|
+
export interface SurfaceAppHandle<TData> {
|
|
156
|
+
/**
|
|
157
|
+
* Route one display state exactly as a host tool result would be. Rejects
|
|
158
|
+
* with `SURFACE_APP_CLOSED` once the host tore the App down or `dispose()`
|
|
159
|
+
* ran: there is no router left to show anything.
|
|
160
|
+
*/
|
|
161
|
+
show(state: SurfaceDisplayState<TData>): Promise<void>;
|
|
162
|
+
/** Manual disposal. Host teardown needs no call. */
|
|
163
|
+
dispose(): Promise<void>;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Injection seam for tests that cannot open an ext-apps transport. It is not
|
|
167
|
+
* a production extension point: a viewer never passes it.
|
|
168
|
+
*/
|
|
169
|
+
export interface SurfaceAppRuntime {
|
|
170
|
+
readonly createApp: typeof createMcpApp;
|
|
171
|
+
}
|
|
172
|
+
export type SurfaceAppErrorCode = "SURFACE_APP_PROJECTION_CONFLICT" | "SURFACE_APP_CLOSED";
|
|
173
|
+
/** Options refused at the boundary. Match on `code`, not on `message`. */
|
|
174
|
+
export declare class SurfaceAppError extends TypeError {
|
|
175
|
+
readonly code: SurfaceAppErrorCode;
|
|
176
|
+
readonly data: Readonly<Record<string, unknown>>;
|
|
177
|
+
constructor(code: SurfaceAppErrorCode, message: string, data?: Record<string, unknown>);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Start a result-driven MCP App backed by a component registry.
|
|
181
|
+
*
|
|
182
|
+
* The registry may expose a default surface or be component-only. In the
|
|
183
|
+
* latter case a host-selected surface is required and no artificial
|
|
184
|
+
* standalone composition is invented.
|
|
185
|
+
*/
|
|
186
|
+
export declare function startSurfaceApp<TData, TSession = never>(options: SurfaceAppOptions<TData, TSession>, runtime?: SurfaceAppRuntime): Promise<SurfaceAppHandle<TData>>;
|
|
187
|
+
export {};
|
|
188
|
+
//# sourceMappingURL=surface-app.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"surface-app.d.ts","sourceRoot":"","sources":["../../src/src/surface-app.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EACL,KAAK,SAAS,EACd,KAAK,UAAU,EAEf,YAAY,EAIb,MAAM,eAAe,CAAC;AACvB,OAAO,EAIL,KAAK,gBAAgB,EAIrB,KAAK,qBAAqB,EAC3B,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAE/D,OAAO,EAAE,KAAK,eAAe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EACf,IAAI,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM;CAOrC;AAID;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,KAAK,EAAE,QAAQ,GAAG,KAAK,EAC3D,OAAO,EAAE,iBAAiB,CAAC,KAAK,EAAE,QAAQ,CAAC,EAC3C,OAAO,GAAE,iBAAmC,GAC3C,OAAO,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CA6UlC"}
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Result-driven MCP App over one component registry, renderer-neutral.
|
|
3
|
+
*
|
|
4
|
+
* Every componentized viewer repeats the same lifecycle: install the theme,
|
|
5
|
+
* show a status until the host sends a result, project that result into a
|
|
6
|
+
* data model, mount the host-selected surface, remount it when the host
|
|
7
|
+
* context changes, and make sure only the latest mount survives. Copies of
|
|
8
|
+
* that loop drifted — one lost the generation guard in its error path,
|
|
9
|
+
* another buffered recorded sessions by hand. This module is the loop, once.
|
|
10
|
+
* Renderers plug in through `renderStatus`; `startPreactSurfaceApp` binds it
|
|
11
|
+
* to the Preact `StateMessage`.
|
|
12
|
+
*/
|
|
13
|
+
import { createMcpApp, defineView, readResultData, } from "@mcpview/view";
|
|
14
|
+
import { activeComponentSurface, applySurfaceContext, componentCatalogCapabilities, defineComponentSurface, mountComponentSurface, } from "./components.js";
|
|
15
|
+
import { installMcpViewTheme } from "./theme.js";
|
|
16
|
+
import { mcpViewMessages } from "./i18n.js";
|
|
17
|
+
/** Options refused at the boundary. Match on `code`, not on `message`. */
|
|
18
|
+
export class SurfaceAppError extends TypeError {
|
|
19
|
+
code;
|
|
20
|
+
data;
|
|
21
|
+
constructor(code, message, data = {}) {
|
|
22
|
+
super(message);
|
|
23
|
+
this.name = "SurfaceAppError";
|
|
24
|
+
this.code = code;
|
|
25
|
+
this.data = Object.freeze({ ...data });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const DEFAULT_RUNTIME = { createApp: createMcpApp };
|
|
29
|
+
/**
|
|
30
|
+
* Start a result-driven MCP App backed by a component registry.
|
|
31
|
+
*
|
|
32
|
+
* The registry may expose a default surface or be component-only. In the
|
|
33
|
+
* latter case a host-selected surface is required and no artificial
|
|
34
|
+
* standalone composition is invented.
|
|
35
|
+
*/
|
|
36
|
+
export async function startSurfaceApp(options, runtime = DEFAULT_RUNTIME) {
|
|
37
|
+
if (options.fromToolResult && options.validate) {
|
|
38
|
+
throw new SurfaceAppError("SURFACE_APP_PROJECTION_CONFLICT", "startSurfaceApp accepts either fromToolResult or validate, not both", { recovery: "Keep fromToolResult and validate inside it, or drop it and keep validate." });
|
|
39
|
+
}
|
|
40
|
+
if (options.theme !== false)
|
|
41
|
+
installMcpViewTheme();
|
|
42
|
+
const state = {};
|
|
43
|
+
const project = options.fromToolResult ?? defaultProjection(options.validate);
|
|
44
|
+
const session = options.viewerSession;
|
|
45
|
+
let mounted;
|
|
46
|
+
let pendingMount;
|
|
47
|
+
let mountGeneration = 0;
|
|
48
|
+
let navigation = Promise.resolve();
|
|
49
|
+
let renderedHostContext;
|
|
50
|
+
let hostLocale;
|
|
51
|
+
let renderedStatusLocale;
|
|
52
|
+
let activeStatus = { kind: "loading" };
|
|
53
|
+
let closed = false;
|
|
54
|
+
let removeHostContextListener;
|
|
55
|
+
const reportError = (error) => {
|
|
56
|
+
if (options.onError)
|
|
57
|
+
options.onError(error);
|
|
58
|
+
else
|
|
59
|
+
console.error("[mcp-view-components] Surface App failed", error);
|
|
60
|
+
};
|
|
61
|
+
const message = (key, values) => (options.messages ?? mcpViewMessages)(hostLocale)(key, values);
|
|
62
|
+
const resolveLabel = (value) => typeof value === "function" ? value(hostLocale) : value;
|
|
63
|
+
const label = (override, key) => override === undefined ? message(key) : resolveLabel(override);
|
|
64
|
+
const resolve = (display) => {
|
|
65
|
+
switch (display.kind) {
|
|
66
|
+
case "loading":
|
|
67
|
+
return {
|
|
68
|
+
kind: "loading",
|
|
69
|
+
title: message("loadingTitle"),
|
|
70
|
+
message: label(options.loadingLabel, "loadingMessage"),
|
|
71
|
+
tone: "info",
|
|
72
|
+
busy: true,
|
|
73
|
+
};
|
|
74
|
+
case "empty":
|
|
75
|
+
return {
|
|
76
|
+
kind: "empty",
|
|
77
|
+
title: message("emptyTitle"),
|
|
78
|
+
message: label(options.emptyLabel, "emptyMessage"),
|
|
79
|
+
tone: "neutral",
|
|
80
|
+
busy: false,
|
|
81
|
+
};
|
|
82
|
+
case "error":
|
|
83
|
+
return {
|
|
84
|
+
kind: "error",
|
|
85
|
+
title: display.title === undefined
|
|
86
|
+
? message(display.rejectionTitleKey ?? "errorTitle")
|
|
87
|
+
: resolveLabel(display.title),
|
|
88
|
+
message: resolveLabel(display.message),
|
|
89
|
+
tone: "danger",
|
|
90
|
+
busy: false,
|
|
91
|
+
...(display.code === undefined ? {} : { code: display.code }),
|
|
92
|
+
};
|
|
93
|
+
case "notice":
|
|
94
|
+
return {
|
|
95
|
+
kind: "notice",
|
|
96
|
+
title: display.title === undefined ? undefined : resolveLabel(display.title),
|
|
97
|
+
message: resolveLabel(display.message),
|
|
98
|
+
tone: display.tone ?? "neutral",
|
|
99
|
+
busy: display.busy ?? false,
|
|
100
|
+
...(display.code === undefined ? {} : { code: display.code }),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const failure = (title, message) => ({
|
|
105
|
+
kind: "error",
|
|
106
|
+
title,
|
|
107
|
+
message,
|
|
108
|
+
tone: "danger",
|
|
109
|
+
busy: false,
|
|
110
|
+
});
|
|
111
|
+
/**
|
|
112
|
+
* Every navigation the App issues is chained here. The router serializes
|
|
113
|
+
* navigations too, but a step chained behind the last one observes the
|
|
114
|
+
* router settled — never a view mid-transition, when `currentView` throws
|
|
115
|
+
* and `currentData` still names a result on its way out.
|
|
116
|
+
*/
|
|
117
|
+
const enqueue = (step) => {
|
|
118
|
+
const next = navigation.then(step);
|
|
119
|
+
navigation = next.catch(() => { });
|
|
120
|
+
return next;
|
|
121
|
+
};
|
|
122
|
+
const show = (navigate, display) => enqueue(() => {
|
|
123
|
+
activeStatus = display.kind === "result" ? undefined : display;
|
|
124
|
+
return display.kind === "result"
|
|
125
|
+
? navigate("surface", display.result)
|
|
126
|
+
: navigate("status", resolve(display));
|
|
127
|
+
});
|
|
128
|
+
/** A projection that throws shows its failure; it never breaks the lifecycle. */
|
|
129
|
+
const settle = async (compute, rejectionTitleKey) => {
|
|
130
|
+
try {
|
|
131
|
+
return await compute();
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
reportError(error);
|
|
135
|
+
return { kind: "error", message: errorMessage(error), rejectionTitleKey };
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const hostAccess = (app) => ({
|
|
139
|
+
locale: app.ctx.hostContext.locale,
|
|
140
|
+
readServerResource: (uri) => app.ctx.app.readServerResource({ uri }),
|
|
141
|
+
});
|
|
142
|
+
const disposeSurface = async () => {
|
|
143
|
+
mountGeneration += 1;
|
|
144
|
+
await pendingMount;
|
|
145
|
+
pendingMount = undefined;
|
|
146
|
+
const active = mounted;
|
|
147
|
+
mounted = undefined;
|
|
148
|
+
await active?.dispose();
|
|
149
|
+
};
|
|
150
|
+
const status = defineView({
|
|
151
|
+
onEnter(_context, next) {
|
|
152
|
+
state.currentData = undefined;
|
|
153
|
+
return next;
|
|
154
|
+
},
|
|
155
|
+
render: (context, next) => {
|
|
156
|
+
hostLocale = context.hostContext.locale;
|
|
157
|
+
renderedStatusLocale = hostLocale;
|
|
158
|
+
return options.renderStatus(activeStatus ? resolve(activeStatus) : next);
|
|
159
|
+
},
|
|
160
|
+
onLeave: disposeSurface,
|
|
161
|
+
});
|
|
162
|
+
const surface = defineView({
|
|
163
|
+
onEnter(_context, data) {
|
|
164
|
+
state.currentData = data;
|
|
165
|
+
return data;
|
|
166
|
+
},
|
|
167
|
+
render(context, data) {
|
|
168
|
+
hostLocale = context.hostContext.locale;
|
|
169
|
+
renderedHostContext = context.hostContext;
|
|
170
|
+
const shell = document.createElement("div");
|
|
171
|
+
shell.className = options.surfaceClassName ?? "mcp-view-surface-shell";
|
|
172
|
+
let selected;
|
|
173
|
+
// Named before the call: a `surfaceFor` that throws is the data-owned
|
|
174
|
+
// path failing, not the host selection it was asked to replace.
|
|
175
|
+
let owner = options.surfaceFor ? "data-owned" : "host-selected";
|
|
176
|
+
try {
|
|
177
|
+
const owned = options.surfaceFor?.(data);
|
|
178
|
+
if (!owned)
|
|
179
|
+
owner = "host-selected";
|
|
180
|
+
selected = owned
|
|
181
|
+
? defineComponentSurface(owned)
|
|
182
|
+
: activeComponentSurface(options.registry, context.hostContext);
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
// A malformed surface must not unmount the route: the App stays on
|
|
186
|
+
// its surface view and says why nothing is composed.
|
|
187
|
+
reportError(error);
|
|
188
|
+
shell.replaceChildren(options.renderStatus(failure(message("surfaceInvalidTitle"), message("surfaceInvalidMessage", { owner, error: errorMessage(error) }))));
|
|
189
|
+
return shell;
|
|
190
|
+
}
|
|
191
|
+
if (!selected) {
|
|
192
|
+
shell.replaceChildren(options.renderStatus({
|
|
193
|
+
kind: "surface-required",
|
|
194
|
+
title: message("surfaceRequiredTitle"),
|
|
195
|
+
message: label(options.surfaceRequiredLabel, "surfaceRequiredMessage"),
|
|
196
|
+
tone: "warning",
|
|
197
|
+
busy: false,
|
|
198
|
+
}));
|
|
199
|
+
return shell;
|
|
200
|
+
}
|
|
201
|
+
const generation = ++mountGeneration;
|
|
202
|
+
pendingMount = mountComponentSurface({
|
|
203
|
+
root: shell,
|
|
204
|
+
registry: options.registry,
|
|
205
|
+
data,
|
|
206
|
+
appContext: context,
|
|
207
|
+
hostContext: context.hostContext,
|
|
208
|
+
surface: selected,
|
|
209
|
+
}).then(async (next) => {
|
|
210
|
+
if (generation !== mountGeneration) {
|
|
211
|
+
await next.dispose();
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
mounted = next;
|
|
215
|
+
}).catch((error) => {
|
|
216
|
+
reportError(error);
|
|
217
|
+
// A superseded mount that fails must not overwrite the newer route.
|
|
218
|
+
if (generation !== mountGeneration)
|
|
219
|
+
return;
|
|
220
|
+
shell.replaceChildren(options.renderStatus(failure(message("surfaceFailedTitle"), message("surfaceFailedMessage", { error: errorMessage(error) }))));
|
|
221
|
+
});
|
|
222
|
+
return shell;
|
|
223
|
+
},
|
|
224
|
+
onLeave: disposeSurface,
|
|
225
|
+
});
|
|
226
|
+
const handle = await runtime.createApp({
|
|
227
|
+
info: options.info,
|
|
228
|
+
root: options.root,
|
|
229
|
+
...(options.strict === undefined ? {} : { strict: options.strict }),
|
|
230
|
+
views: { status, surface },
|
|
231
|
+
initialView: "status",
|
|
232
|
+
initialArgs: resolve({ kind: "loading" }),
|
|
233
|
+
initialState: state,
|
|
234
|
+
...(session
|
|
235
|
+
? {
|
|
236
|
+
viewerSession: {
|
|
237
|
+
validate: session.validate,
|
|
238
|
+
onSession: async (value, _payload, app) => {
|
|
239
|
+
hostLocale = app.ctx.hostContext.locale;
|
|
240
|
+
const next = await settle(() => session.toState(value, hostAccess(app)), "sessionRejectedTitle");
|
|
241
|
+
await show(app.navigate, next);
|
|
242
|
+
},
|
|
243
|
+
onInvalid: (payload) => session.onInvalid?.(payload.data),
|
|
244
|
+
onError: reportError,
|
|
245
|
+
},
|
|
246
|
+
}
|
|
247
|
+
: {}),
|
|
248
|
+
capabilities: {
|
|
249
|
+
experimental: componentCatalogCapabilities(options.registry),
|
|
250
|
+
},
|
|
251
|
+
// Either notification means a run is under way: a host that does not
|
|
252
|
+
// stream arguments sends only the complete `tool-input`.
|
|
253
|
+
onToolInput: async (_params, app) => {
|
|
254
|
+
await show(app.navigate, { kind: "loading" });
|
|
255
|
+
},
|
|
256
|
+
onToolInputPartial: async (_params, app) => {
|
|
257
|
+
await show(app.navigate, { kind: "loading" });
|
|
258
|
+
},
|
|
259
|
+
onToolResult: async (result, app) => {
|
|
260
|
+
hostLocale = app.ctx.hostContext.locale;
|
|
261
|
+
const next = await settle(() => project(result, hostAccess(app)), "resultRejectedTitle");
|
|
262
|
+
await show(app.navigate, next);
|
|
263
|
+
},
|
|
264
|
+
onTeardown: async () => {
|
|
265
|
+
// The router runs the active view's onLeave, which disposes the surface.
|
|
266
|
+
closed = true;
|
|
267
|
+
removeHostContextListener?.();
|
|
268
|
+
removeHostContextListener = undefined;
|
|
269
|
+
try {
|
|
270
|
+
await options.onTeardown?.();
|
|
271
|
+
}
|
|
272
|
+
catch (error) {
|
|
273
|
+
reportError(error);
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
const surfaceHandle = {
|
|
278
|
+
show: (display) => closed
|
|
279
|
+
? Promise.reject(new SurfaceAppError("SURFACE_APP_CLOSED", "The surface App has been torn down", {
|
|
280
|
+
recovery: "Start a new App with startSurfaceApp; this handle cannot show anything.",
|
|
281
|
+
}))
|
|
282
|
+
: show(handle.navigate, display),
|
|
283
|
+
dispose: () => handle.dispose(),
|
|
284
|
+
};
|
|
285
|
+
// A host teardown buffered during the handshake has already run: there is
|
|
286
|
+
// no App left to listen to.
|
|
287
|
+
if (closed)
|
|
288
|
+
return surfaceHandle;
|
|
289
|
+
/** Remount the displayed result when the host context moved since it rendered. */
|
|
290
|
+
const remountIfStale = () => enqueue(async () => {
|
|
291
|
+
const data = state.currentData;
|
|
292
|
+
if (closed)
|
|
293
|
+
return;
|
|
294
|
+
if (data === undefined) {
|
|
295
|
+
if (activeStatus && hostLocale !== renderedStatusLocale) {
|
|
296
|
+
await handle.navigate("status", resolve(activeStatus));
|
|
297
|
+
}
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
if (handle.ctx.hostContext === renderedHostContext) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (options.themeUpdates === "in-place" && renderedHostContext &&
|
|
304
|
+
onlyThemeChanged(renderedHostContext, handle.ctx.hostContext)) {
|
|
305
|
+
renderedHostContext = handle.ctx.hostContext;
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
await handle.navigate("surface", data);
|
|
309
|
+
});
|
|
310
|
+
const onHostContextChanged = () => {
|
|
311
|
+
hostLocale = handle.ctx.hostContext.locale;
|
|
312
|
+
if (options.documentLanguage) {
|
|
313
|
+
document.documentElement.lang = options.documentLanguage(hostLocale);
|
|
314
|
+
}
|
|
315
|
+
applySurfaceContext(handle.ctx.hostContext, document.documentElement);
|
|
316
|
+
remountIfStale().catch(reportError);
|
|
317
|
+
};
|
|
318
|
+
handle.ctx.app.addEventListener("hostcontextchanged", onHostContextChanged);
|
|
319
|
+
removeHostContextListener = () => {
|
|
320
|
+
handle.ctx.app.removeEventListener("hostcontextchanged", onHostContextChanged);
|
|
321
|
+
};
|
|
322
|
+
// Host notifications replayed by `createMcpApp` may have rendered a result
|
|
323
|
+
// and then moved the host context before this listener existed.
|
|
324
|
+
onHostContextChanged();
|
|
325
|
+
return surfaceHandle;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Conservative: only `theme` is exempt. Any other field whose JSON-shaped value
|
|
329
|
+
* changed retains the normal remount behavior. Object key order is ignored;
|
|
330
|
+
* array order is kept. No stringify, no coercion.
|
|
331
|
+
*/
|
|
332
|
+
function onlyThemeChanged(previous, next) {
|
|
333
|
+
const keys = new Set([...Object.keys(previous), ...Object.keys(next)]);
|
|
334
|
+
return [...keys].every((key) => key === "theme" || jsonValueEqual(previous[key], next[key]));
|
|
335
|
+
}
|
|
336
|
+
function jsonValueEqual(left, right) {
|
|
337
|
+
if (left === right)
|
|
338
|
+
return true;
|
|
339
|
+
if (left === null || right === null)
|
|
340
|
+
return false;
|
|
341
|
+
if (Array.isArray(left) || Array.isArray(right)) {
|
|
342
|
+
if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) {
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
return left.every((item, index) => jsonValueEqual(item, right[index]));
|
|
346
|
+
}
|
|
347
|
+
if (!isPlainJsonObject(left) || !isPlainJsonObject(right))
|
|
348
|
+
return false;
|
|
349
|
+
const leftKeys = Object.keys(left);
|
|
350
|
+
const rightKeys = Object.keys(right);
|
|
351
|
+
if (leftKeys.length !== rightKeys.length)
|
|
352
|
+
return false;
|
|
353
|
+
return leftKeys.every((key) => Object.hasOwn(right, key) && jsonValueEqual(left[key], right[key]));
|
|
354
|
+
}
|
|
355
|
+
function isPlainJsonObject(value) {
|
|
356
|
+
if (typeof value !== "object" || value === null)
|
|
357
|
+
return false;
|
|
358
|
+
const proto = Object.getPrototypeOf(value);
|
|
359
|
+
return proto === Object.prototype || proto === null;
|
|
360
|
+
}
|
|
361
|
+
function defaultProjection(validate) {
|
|
362
|
+
return (result) => {
|
|
363
|
+
const data = readResultData(result, {
|
|
364
|
+
fallback: "json-text",
|
|
365
|
+
...(validate ? { validate: (value) => validate(value) } : {}),
|
|
366
|
+
});
|
|
367
|
+
return data === undefined
|
|
368
|
+
? { kind: "empty" }
|
|
369
|
+
: { kind: "result", result: data };
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
function errorMessage(error) {
|
|
373
|
+
return error instanceof Error ? error.message : String(error);
|
|
374
|
+
}
|