@copilotkit/mcp-apps-renderer 2.0.0-rc.5 → 2.0.0-rc.50
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/README.md +2 -3
- package/dist/_virtual/_rolldown/runtime.cjs +29 -0
- package/dist/activity.cjs +6 -0
- package/dist/activity.d.cts +2 -1
- package/dist/activity.d.mts +2 -1
- package/dist/activity.mjs +2 -1
- package/dist/activity.umd.js +369 -35
- package/dist/activity.umd.js.map +1 -1
- package/dist/content-schema.cjs +37 -35
- package/dist/content-schema.cjs.map +1 -1
- package/dist/content-schema.d.cts +40 -3309
- package/dist/content-schema.d.cts.map +1 -1
- package/dist/content-schema.d.mts +40 -3309
- package/dist/content-schema.d.mts.map +1 -1
- package/dist/content-schema.mjs +2 -2
- package/dist/content-schema.mjs.map +1 -1
- package/dist/host.cjs +246 -0
- package/dist/host.cjs.map +1 -0
- package/dist/host.d.cts +99 -0
- package/dist/host.d.cts.map +1 -0
- package/dist/host.d.mts +99 -0
- package/dist/host.d.mts.map +1 -0
- package/dist/host.mjs +241 -0
- package/dist/host.mjs.map +1 -0
- package/dist/request-queue.mjs.map +1 -1
- package/dist/session.d.cts +172 -0
- package/dist/session.d.cts.map +1 -0
- package/dist/session.d.mts +1 -58
- package/dist/session.d.mts.map +1 -1
- package/dist/session.mjs +2 -2
- package/dist/session.mjs.map +1 -1
- package/package.json +17 -8
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-schema.d.mts","names":[],"sources":["../src/content-schema.ts"],"mappings":";;;;cA8Fa,4BAAA,EAA4B,CAAA,CAAA,SAAA
|
|
1
|
+
{"version":3,"file":"content-schema.d.mts","names":[],"sources":["../src/content-schema.ts"],"mappings":";;;;cA8Fa,4BAAA,EAA4B,CAAA,CAAA,SAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAU7B,sBAAA,GAAyB,CAAA,CAAE,KAAA,QAC9B,4BAAA"}
|
package/dist/content-schema.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as z from "zod";
|
|
2
2
|
|
|
3
3
|
//#region src/content-schema.ts
|
|
4
|
-
const looseObject = (shape) => z.object(shape).
|
|
4
|
+
const looseObject = (shape) => z.object(shape).loose();
|
|
5
5
|
const metaSchema = z.record(z.string(), z.unknown());
|
|
6
6
|
const annotationsSchema = looseObject({
|
|
7
7
|
audience: z.array(z.enum(["user", "assistant"])).optional(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"content-schema.mjs","names":[],"sources":["../src/content-schema.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"content-schema.mjs","names":[],"sources":["../src/content-schema.ts"],"sourcesContent":["import * as z from \"zod\";\n\n// Keep /activity bridge-free: the MCP SDK is only used as a test oracle.\n// This is the Angular host's content contract, expressed using our zod API.\nconst looseObject = <T extends z.ZodRawShape>(shape: T) =>\n z.object(shape).loose();\n\n// `_meta` is an extension channel, so its CONTENTS stay unconstrained - but the\n// SDK still requires it to be an object, and a string or array there is a bug\n// worth catching rather than forwarding.\nconst metaSchema = z.record(z.string(), z.unknown());\n\nconst annotationsSchema = looseObject({\n audience: z.array(z.enum([\"user\", \"assistant\"])).optional(),\n priority: z.number().min(0).max(1).optional(),\n lastModified: z.string().optional(),\n});\n\n// Validated by decoding, mirroring the MCP SDK's own base64 check, rather than\n// by pattern. The pattern this replaces additionally required canonical padding,\n// which the SDK does not: it accepts an unpadded payload that we rejected. Since\n// one bad block fails the whole content array, that cost the entire widget.\nconst base64Schema = z.string().refine((value) => {\n try {\n atob(value);\n return true;\n } catch {\n return false;\n }\n}, \"Expected base64-encoded data\");\n\nconst resourceContentsSchema = z.union([\n looseObject({\n uri: z.string(),\n mimeType: z.string().optional(),\n text: z.string(),\n _meta: metaSchema.optional(),\n }),\n looseObject({\n uri: z.string(),\n mimeType: z.string().optional(),\n blob: base64Schema,\n _meta: metaSchema.optional(),\n }),\n]);\n\nconst contentItemSchema = z.discriminatedUnion(\"type\", [\n looseObject({\n type: z.literal(\"text\"),\n text: z.string(),\n annotations: annotationsSchema.optional(),\n _meta: metaSchema.optional(),\n }),\n looseObject({\n type: z.literal(\"image\"),\n data: base64Schema,\n mimeType: z.string(),\n annotations: annotationsSchema.optional(),\n _meta: metaSchema.optional(),\n }),\n looseObject({\n type: z.literal(\"audio\"),\n data: base64Schema,\n mimeType: z.string(),\n annotations: annotationsSchema.optional(),\n _meta: metaSchema.optional(),\n }),\n looseObject({\n type: z.literal(\"resource\"),\n resource: resourceContentsSchema,\n annotations: annotationsSchema.optional(),\n _meta: metaSchema.optional(),\n }),\n looseObject({\n type: z.literal(\"resource_link\"),\n uri: z.string(),\n name: z.string(),\n title: z.string().optional(),\n description: z.string().optional(),\n mimeType: z.string().optional(),\n size: z.number().optional(),\n annotations: annotationsSchema.optional(),\n _meta: metaSchema.optional(),\n }),\n]);\n\nconst callToolResultSchema = looseObject({\n content: z.array(contentItemSchema).default([]),\n structuredContent: z.record(z.string(), z.unknown()).optional(),\n isError: z.boolean().optional(),\n _meta: z.record(z.string(), z.unknown()).optional(),\n});\n\n/** Activity input accepts an omitted result.content; validation defaults it to []. */\nexport const MCPAppsActivityContentSchema = looseObject({\n result: callToolResultSchema,\n resourceUri: z.string(),\n serverHash: z.string(),\n serverId: z.string().optional(),\n toolInput: z.record(z.string(), z.unknown()).optional(),\n});\n\n// Keep the public input contract compatible with structured-only results.\n// Parsed output is normalized before it crosses the bridge.\nexport type MCPAppsActivityContent = z.input<\n typeof MCPAppsActivityContentSchema\n>;\n"],"mappings":";;;AAIA,MAAM,eAAwC,UAC5C,EAAE,OAAO,MAAM,CAAC,OAAO;AAKzB,MAAM,aAAa,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,SAAS,CAAC;AAEpD,MAAM,oBAAoB,YAAY;CACpC,UAA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EAChC,KAAK,EAAE,QAAQ;EACf,MAAM,EAAE,QAAQ;EAChB,OAAO,EAAE,QAAQ,CAAC,UAAU;EAC5B,aAAa,EAAE,QAAQ,CAAC,UAAU;EAClC,UAAU,EAAE,QAAQ,CAAC,UAAU;EAC/B,MAAM,EAAE,QAAQ,CAAC,UAAU;EAC3B,aAAa,kBAAkB,UAAU;EACzC,OAAO,WAAW,UAAU;EAC7B,CAAC;CACH,CAAC;AAEF,MAAM,uBAAuB,YAAY;CACvC,SAAS,EAAE,MAAM,kBAAkB,CAAC,QAAQ,EAAE,CAAC;CAC/C,mBAAmB,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,SAAS,CAAC,CAAC,UAAU;CAC/D,SAAS,EAAE,SAAS,CAAC,UAAU;CAC/B,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,SAAS,CAAC,CAAC,UAAU;CACpD,CAAC;;AAGF,MAAa,+BAA+B,YAAY;CACtD,QAAQ;CACR,aAAa,EAAE,QAAQ;CACvB,YAAY,EAAE,QAAQ;CACtB,UAAU,EAAE,QAAQ,CAAC,UAAU;CAC/B,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,SAAS,CAAC,CAAC,UAAU;CACxD,CAAC"}
|
package/dist/host.cjs
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/host.ts
|
|
3
|
+
/** Message shown when the bridge package cannot be loaded. */
|
|
4
|
+
const MCP_APPS_RENDERER_MISSING_MESSAGE = "MCP Apps require '@copilotkit/mcp-apps-renderer' and its '@modelcontextprotocol/ext-apps' dependency. Reinstall your dependencies if this package is missing.";
|
|
5
|
+
/** Error text when an MCP App renders without an agent to fetch it through. */
|
|
6
|
+
const MCP_APPS_NO_AGENT_MESSAGE = "No agent available to fetch resource";
|
|
7
|
+
/** Tallest an MCP App iframe may grow, in pixels. */
|
|
8
|
+
const MCP_APP_MAX_HEIGHT = 5e3;
|
|
9
|
+
/** The height the iframe starts at before the widget reports its size. */
|
|
10
|
+
const INITIAL_HEIGHT_PX = 100;
|
|
11
|
+
/**
|
|
12
|
+
* Loads the bridge (`bindMcpApp`) lazily. The bridge pulls the MCP SDK and
|
|
13
|
+
* its zod schemas (~40-50 kB gzipped), so an app only pays for it when an MCP
|
|
14
|
+
* App renders. The `import()` itself stays in the frontend, where its bundler
|
|
15
|
+
* can split it out. A failed load rethrows with what to install, since a raw
|
|
16
|
+
* module-resolution error does not say.
|
|
17
|
+
*
|
|
18
|
+
* @param importRenderer - `() => import("@copilotkit/mcp-apps-renderer")`.
|
|
19
|
+
*/
|
|
20
|
+
function loadMcpAppsRenderer(importRenderer) {
|
|
21
|
+
return importRenderer().catch((importErr) => {
|
|
22
|
+
throw new Error(MCP_APPS_RENDERER_MISSING_MESSAGE, { cause: importErr });
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function identityOf(content) {
|
|
26
|
+
if (!content) return "";
|
|
27
|
+
return JSON.stringify([
|
|
28
|
+
content.resourceUri,
|
|
29
|
+
content.serverHash,
|
|
30
|
+
content.serverId
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
const INITIAL_STATE = {
|
|
34
|
+
loading: true,
|
|
35
|
+
error: null,
|
|
36
|
+
contentError: null,
|
|
37
|
+
size: {},
|
|
38
|
+
prefersBorder: false
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Hosts one MCP App: creates the sandbox iframe in a container, binds the
|
|
42
|
+
* MCP session to it, sizes it as the widget reports, and keeps the state a
|
|
43
|
+
* frontend renders. It binds again (new iframe, new session) only when the
|
|
44
|
+
* container, agent, message id or widget identity (resource URI, server)
|
|
45
|
+
* changes; new tool input or results feed the existing session.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* const app = new ɵMcpAppHost({ host: core });
|
|
50
|
+
* app.subscribe(() => render(app.getState()));
|
|
51
|
+
* app.setContent(content);
|
|
52
|
+
* app.setAgent(agent);
|
|
53
|
+
* app.setMessageId(message.id);
|
|
54
|
+
* app.setContainer(element); // on mount
|
|
55
|
+
* app.destroy(); // on unmount
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
var McpAppHost = class {
|
|
59
|
+
#options;
|
|
60
|
+
#listeners = /* @__PURE__ */ new Set();
|
|
61
|
+
#container = null;
|
|
62
|
+
#content;
|
|
63
|
+
#agent;
|
|
64
|
+
#messageId;
|
|
65
|
+
#bindKey = "";
|
|
66
|
+
#iframe = null;
|
|
67
|
+
#session = null;
|
|
68
|
+
/** Bumped on every bind and on destroy; stale async work checks it. */
|
|
69
|
+
#generation = 0;
|
|
70
|
+
#state = INITIAL_STATE;
|
|
71
|
+
constructor(options) {
|
|
72
|
+
this.getState = () => this.#state;
|
|
73
|
+
this.subscribe = (listener) => {
|
|
74
|
+
this.#listeners.add(listener);
|
|
75
|
+
return () => {
|
|
76
|
+
this.#listeners.delete(listener);
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
this.#options = options;
|
|
80
|
+
}
|
|
81
|
+
/** Sets the element the iframe goes into. `null` removes it. */
|
|
82
|
+
setContainer(container) {
|
|
83
|
+
this.#container = container;
|
|
84
|
+
this.#rebindIfNeeded();
|
|
85
|
+
}
|
|
86
|
+
/** Sets the latest activity content. */
|
|
87
|
+
setContent(content) {
|
|
88
|
+
this.#content = content;
|
|
89
|
+
if (!this.#rebindIfNeeded()) this.#session?.syncContent(content);
|
|
90
|
+
}
|
|
91
|
+
/** Sets the agent the widget's requests go through. */
|
|
92
|
+
setAgent(agent) {
|
|
93
|
+
this.#agent = agent;
|
|
94
|
+
this.#rebindIfNeeded();
|
|
95
|
+
}
|
|
96
|
+
/** Sets the id of the activity message, so the session follows the store. */
|
|
97
|
+
setMessageId(messageId) {
|
|
98
|
+
this.#messageId = messageId;
|
|
99
|
+
this.#rebindIfNeeded();
|
|
100
|
+
}
|
|
101
|
+
/** Tears the session down and removes the iframe. A later setter binds again. */
|
|
102
|
+
destroy() {
|
|
103
|
+
this.#unbind();
|
|
104
|
+
this.#bindKey = "";
|
|
105
|
+
}
|
|
106
|
+
#rebindIfNeeded() {
|
|
107
|
+
const key = JSON.stringify([
|
|
108
|
+
identityOf(this.#content),
|
|
109
|
+
this.#messageId ?? null,
|
|
110
|
+
this.#container !== null,
|
|
111
|
+
this.#agent !== void 0
|
|
112
|
+
]);
|
|
113
|
+
const agentChanged = this.#session !== null && this.#boundAgent !== this.#agent;
|
|
114
|
+
const containerChanged = this.#boundContainer !== this.#container;
|
|
115
|
+
if (key === this.#bindKey && !agentChanged && !containerChanged) return false;
|
|
116
|
+
this.#bindKey = key;
|
|
117
|
+
this.#bind();
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
#boundAgent;
|
|
121
|
+
#boundContainer = null;
|
|
122
|
+
#bind() {
|
|
123
|
+
this.#unbind();
|
|
124
|
+
const container = this.#container;
|
|
125
|
+
const content = this.#content;
|
|
126
|
+
const agent = this.#agent;
|
|
127
|
+
this.#boundAgent = agent;
|
|
128
|
+
this.#boundContainer = container;
|
|
129
|
+
if (!content || !container) return;
|
|
130
|
+
if (!agent) {
|
|
131
|
+
this.#setState({
|
|
132
|
+
...INITIAL_STATE,
|
|
133
|
+
loading: false,
|
|
134
|
+
error: MCP_APPS_NO_AGENT_MESSAGE
|
|
135
|
+
});
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const generation = this.#generation;
|
|
139
|
+
const isCurrent = () => generation === this.#generation;
|
|
140
|
+
this.#setState(INITIAL_STATE);
|
|
141
|
+
const iframe = document.createElement("iframe");
|
|
142
|
+
for (const [name, value] of Object.entries(this.#options.iframeAttributes ?? {})) iframe.setAttribute(name, value);
|
|
143
|
+
iframe.style.width = "100%";
|
|
144
|
+
iframe.style.height = `${String(INITIAL_HEIGHT_PX)}px`;
|
|
145
|
+
iframe.style.border = "none";
|
|
146
|
+
iframe.style.backgroundColor = "transparent";
|
|
147
|
+
iframe.style.display = "block";
|
|
148
|
+
container.appendChild(iframe);
|
|
149
|
+
this.#iframe = iframe;
|
|
150
|
+
const scoped = this.#options.scopeToWidget === true;
|
|
151
|
+
const fail = (err) => {
|
|
152
|
+
if (!isCurrent()) return;
|
|
153
|
+
this.#setState({
|
|
154
|
+
...this.#state,
|
|
155
|
+
loading: false,
|
|
156
|
+
error: err.message
|
|
157
|
+
});
|
|
158
|
+
if (scoped) {
|
|
159
|
+
this.#session?.teardown();
|
|
160
|
+
this.#session = null;
|
|
161
|
+
iframe.removeAttribute("srcdoc");
|
|
162
|
+
iframe.removeAttribute("data-mcp-app-initialized");
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
loadMcpAppsRenderer(this.#options.importRenderer).then((mod) => {
|
|
166
|
+
if (!isCurrent()) return;
|
|
167
|
+
const session = mod.bindMcpApp({
|
|
168
|
+
iframe,
|
|
169
|
+
getContent: () => this.#content ?? content,
|
|
170
|
+
getAgent: () => this.#agent,
|
|
171
|
+
host: this.#options.host,
|
|
172
|
+
messageId: this.#messageId,
|
|
173
|
+
options: this.#options.sessionOptions,
|
|
174
|
+
cancelFollowUpsOnTeardown: scoped,
|
|
175
|
+
requireExactResourceUri: scoped,
|
|
176
|
+
cancelRunningWaitOnTeardown: scoped,
|
|
177
|
+
hooks: {
|
|
178
|
+
onResource: (resource) => {
|
|
179
|
+
if (!isCurrent()) return;
|
|
180
|
+
this.#setState({
|
|
181
|
+
...this.#state,
|
|
182
|
+
loading: false,
|
|
183
|
+
prefersBorder: resource._meta?.ui?.prefersBorder === true
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
onInitialized: () => {
|
|
187
|
+
if (isCurrent()) iframe.setAttribute("data-mcp-app-initialized", "true");
|
|
188
|
+
},
|
|
189
|
+
onSizeChanged: (size) => {
|
|
190
|
+
if (isCurrent()) this.#applySize(iframe, size);
|
|
191
|
+
},
|
|
192
|
+
onContentError: (err) => {
|
|
193
|
+
if (!isCurrent()) return;
|
|
194
|
+
this.#setState({
|
|
195
|
+
...this.#state,
|
|
196
|
+
contentError: err?.message ?? null
|
|
197
|
+
});
|
|
198
|
+
},
|
|
199
|
+
onError: fail,
|
|
200
|
+
...scoped ? { onFollowUpError: fail } : {}
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
this.#session = session;
|
|
204
|
+
session.syncContent(this.#content ?? content);
|
|
205
|
+
}).catch((err) => {
|
|
206
|
+
console.error("[MCPAppsRenderer] Setup error:", err);
|
|
207
|
+
fail(err instanceof Error ? err : new Error(String(err)));
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
#applySize(iframe, size) {
|
|
211
|
+
const next = {};
|
|
212
|
+
if (typeof size.width === "number" && Number.isFinite(size.width)) {
|
|
213
|
+
next.width = size.width;
|
|
214
|
+
iframe.style.minWidth = `min(${String(size.width)}px, 100%)`;
|
|
215
|
+
iframe.style.width = "100%";
|
|
216
|
+
}
|
|
217
|
+
const height = size.height;
|
|
218
|
+
if (typeof height === "number" && Number.isFinite(height) && height > 0) {
|
|
219
|
+
next.height = Math.ceil(Math.min(height, MCP_APP_MAX_HEIGHT));
|
|
220
|
+
iframe.style.height = `${String(next.height)}px`;
|
|
221
|
+
}
|
|
222
|
+
this.#setState({
|
|
223
|
+
...this.#state,
|
|
224
|
+
size: next
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
#unbind() {
|
|
228
|
+
this.#generation += 1;
|
|
229
|
+
this.#session?.teardown();
|
|
230
|
+
this.#session = null;
|
|
231
|
+
this.#iframe?.remove();
|
|
232
|
+
this.#iframe = null;
|
|
233
|
+
}
|
|
234
|
+
#setState(state) {
|
|
235
|
+
this.#state = state;
|
|
236
|
+
for (const listener of this.#listeners) listener();
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
//#endregion
|
|
241
|
+
exports.MCP_APPS_NO_AGENT_MESSAGE = MCP_APPS_NO_AGENT_MESSAGE;
|
|
242
|
+
exports.MCP_APPS_RENDERER_MISSING_MESSAGE = MCP_APPS_RENDERER_MISSING_MESSAGE;
|
|
243
|
+
exports.MCP_APP_MAX_HEIGHT = MCP_APP_MAX_HEIGHT;
|
|
244
|
+
exports.McpAppHost = McpAppHost;
|
|
245
|
+
exports.loadMcpAppsRenderer = loadMcpAppsRenderer;
|
|
246
|
+
//# sourceMappingURL=host.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host.cjs","names":["#options","#listeners","#state","#container","#rebindIfNeeded","#content","#session","#agent","#messageId","#unbind","#bindKey","#boundAgent","#boundContainer","#bind","#setState","#generation","#iframe","#applySize"],"sources":["../src/host.ts"],"sourcesContent":["// The framework-neutral host shell around `bindMcpApp`: the iframe, its size,\n// and the loading / error / content-error state every frontend renders. It is\n// bridge-free (the bridge loads lazily through `loadMcpAppsRenderer`), so\n// frontends can import it statically from the `/activity` entry.\nimport type { AbstractAgent } from \"@ag-ui/client\";\nimport type { MCPAppsActivityContent } from \"./content-schema\";\nimport type { ɵMcpFollowUpHost } from \"./follow-up\";\nimport type {\n BindMcpAppOptions,\n FetchedResource,\n McpAppSession,\n McpAppSessionOptions,\n} from \"./session\";\n\n/** The part of the bridge module the host uses, loaded on demand. */\nexport interface McpAppsRendererModule {\n bindMcpApp(options: BindMcpAppOptions): McpAppSession;\n}\n\n/** Message shown when the bridge package cannot be loaded. */\nexport const MCP_APPS_RENDERER_MISSING_MESSAGE =\n \"MCP Apps require '@copilotkit/mcp-apps-renderer' and its \" +\n \"'@modelcontextprotocol/ext-apps' dependency. Reinstall your \" +\n \"dependencies if this package is missing.\";\n\n/** Error text when an MCP App renders without an agent to fetch it through. */\nexport const MCP_APPS_NO_AGENT_MESSAGE = \"No agent available to fetch resource\";\n\n/** Tallest an MCP App iframe may grow, in pixels. */\nexport const MCP_APP_MAX_HEIGHT = 5000;\n\n/** The height the iframe starts at before the widget reports its size. */\nconst INITIAL_HEIGHT_PX = 100;\n\n/** Imports the bridge module; frontends pass `() => import(\"@copilotkit/mcp-apps-renderer\")`. */\nexport type McpAppsRendererImporter = () => Promise<McpAppsRendererModule>;\n\n/**\n * Loads the bridge (`bindMcpApp`) lazily. The bridge pulls the MCP SDK and\n * its zod schemas (~40-50 kB gzipped), so an app only pays for it when an MCP\n * App renders. The `import()` itself stays in the frontend, where its bundler\n * can split it out. A failed load rethrows with what to install, since a raw\n * module-resolution error does not say.\n *\n * @param importRenderer - `() => import(\"@copilotkit/mcp-apps-renderer\")`.\n */\nexport function loadMcpAppsRenderer(importRenderer: McpAppsRendererImporter) {\n return importRenderer().catch((importErr: unknown) => {\n throw new Error(MCP_APPS_RENDERER_MISSING_MESSAGE, { cause: importErr });\n });\n}\n\n/** What a frontend renders around the MCP App iframe. */\nexport interface McpAppHostState {\n loading: boolean;\n /** Fatal setup failure; shown until the host binds again. */\n error: string | null;\n /** Recoverable: the latest content was rejected and not forwarded. */\n contentError: string | null;\n /** The size the widget reported. */\n size: { width?: number; height?: number };\n /** The resource asked the host for a border and background. */\n prefersBorder: boolean;\n}\n\nexport interface McpAppHostOptions {\n /** The CopilotKit core, for `ui/message` follow-up runs. */\n host: ɵMcpFollowUpHost;\n /** Session options (host info, timeouts, `allowSameOriginFallback`). */\n sessionOptions?: McpAppSessionOptions;\n /**\n * Scope the session to the widget (Angular's contract): teardown cancels\n * its queued follow-up and releases running waits, the fetched resource URI\n * must match exactly, and a failed follow-up or setup error tears the\n * session down, blanks the iframe and shows the error.\n */\n scopeToWidget?: boolean;\n /** Attributes set on the iframe, such as a `data-testid` or `title`. */\n iframeAttributes?: Record<string, string>;\n /** `() => import(\"@copilotkit/mcp-apps-renderer\")`; see {@link loadMcpAppsRenderer}. */\n importRenderer: McpAppsRendererImporter;\n}\n\nfunction identityOf(content: MCPAppsActivityContent | undefined) {\n if (!content) return \"\";\n return JSON.stringify([\n content.resourceUri,\n content.serverHash,\n content.serverId,\n ]);\n}\n\nconst INITIAL_STATE: McpAppHostState = {\n loading: true,\n error: null,\n contentError: null,\n size: {},\n prefersBorder: false,\n};\n\n/**\n * Hosts one MCP App: creates the sandbox iframe in a container, binds the\n * MCP session to it, sizes it as the widget reports, and keeps the state a\n * frontend renders. It binds again (new iframe, new session) only when the\n * container, agent, message id or widget identity (resource URI, server)\n * changes; new tool input or results feed the existing session.\n *\n * @example\n * ```ts\n * const app = new ɵMcpAppHost({ host: core });\n * app.subscribe(() => render(app.getState()));\n * app.setContent(content);\n * app.setAgent(agent);\n * app.setMessageId(message.id);\n * app.setContainer(element); // on mount\n * app.destroy(); // on unmount\n * ```\n */\nexport class McpAppHost {\n readonly #options: McpAppHostOptions;\n readonly #listeners = new Set<() => void>();\n #container: HTMLElement | null = null;\n #content: MCPAppsActivityContent | undefined;\n #agent: AbstractAgent | undefined;\n #messageId: string | undefined;\n #bindKey = \"\";\n #iframe: HTMLIFrameElement | null = null;\n #session: McpAppSession | null = null;\n /** Bumped on every bind and on destroy; stale async work checks it. */\n #generation = 0;\n #state: McpAppHostState = INITIAL_STATE;\n\n constructor(options: McpAppHostOptions) {\n this.#options = options;\n }\n\n /** The current state. The same object until something changes. */\n getState = () => this.#state;\n\n /** Calls `listener` when the state changes. Returns the unsubscribe function. */\n subscribe = (listener: () => void) => {\n this.#listeners.add(listener);\n return () => {\n this.#listeners.delete(listener);\n };\n };\n\n /** Sets the element the iframe goes into. `null` removes it. */\n setContainer(container: HTMLElement | null) {\n this.#container = container;\n this.#rebindIfNeeded();\n }\n\n /** Sets the latest activity content. */\n setContent(content: MCPAppsActivityContent) {\n this.#content = content;\n if (!this.#rebindIfNeeded()) this.#session?.syncContent(content);\n }\n\n /** Sets the agent the widget's requests go through. */\n setAgent(agent: AbstractAgent | undefined) {\n this.#agent = agent;\n this.#rebindIfNeeded();\n }\n\n /** Sets the id of the activity message, so the session follows the store. */\n setMessageId(messageId: string | undefined) {\n this.#messageId = messageId;\n this.#rebindIfNeeded();\n }\n\n /** Tears the session down and removes the iframe. A later setter binds again. */\n destroy() {\n this.#unbind();\n this.#bindKey = \"\";\n }\n\n #rebindIfNeeded() {\n const key = JSON.stringify([\n identityOf(this.#content),\n this.#messageId ?? null,\n this.#container !== null,\n this.#agent !== undefined,\n ]);\n const agentChanged =\n this.#session !== null && this.#boundAgent !== this.#agent;\n const containerChanged = this.#boundContainer !== this.#container;\n if (key === this.#bindKey && !agentChanged && !containerChanged) {\n return false;\n }\n this.#bindKey = key;\n this.#bind();\n return true;\n }\n\n #boundAgent: AbstractAgent | undefined;\n #boundContainer: HTMLElement | null = null;\n\n #bind() {\n this.#unbind();\n const container = this.#container;\n const content = this.#content;\n const agent = this.#agent;\n this.#boundAgent = agent;\n this.#boundContainer = container;\n if (!content || !container) return;\n if (!agent) {\n this.#setState({\n ...INITIAL_STATE,\n loading: false,\n error: MCP_APPS_NO_AGENT_MESSAGE,\n });\n return;\n }\n\n const generation = this.#generation;\n const isCurrent = () => generation === this.#generation;\n this.#setState(INITIAL_STATE);\n\n const iframe = document.createElement(\"iframe\");\n for (const [name, value] of Object.entries(\n this.#options.iframeAttributes ?? {},\n )) {\n iframe.setAttribute(name, value);\n }\n iframe.style.width = \"100%\";\n iframe.style.height = `${String(INITIAL_HEIGHT_PX)}px`;\n iframe.style.border = \"none\";\n iframe.style.backgroundColor = \"transparent\";\n iframe.style.display = \"block\";\n container.appendChild(iframe);\n this.#iframe = iframe;\n\n const scoped = this.#options.scopeToWidget === true;\n const fail = (err: Error) => {\n if (!isCurrent()) return;\n this.#setState({ ...this.#state, loading: false, error: err.message });\n if (scoped) {\n // Blank the widget, so a failed app does not stay interactive.\n this.#session?.teardown();\n this.#session = null;\n iframe.removeAttribute(\"srcdoc\");\n iframe.removeAttribute(\"data-mcp-app-initialized\");\n }\n };\n loadMcpAppsRenderer(this.#options.importRenderer)\n .then((mod) => {\n if (!isCurrent()) return;\n const session = mod.bindMcpApp({\n iframe,\n getContent: () => this.#content ?? content,\n getAgent: () => this.#agent,\n host: this.#options.host,\n messageId: this.#messageId,\n options: this.#options.sessionOptions,\n cancelFollowUpsOnTeardown: scoped,\n requireExactResourceUri: scoped,\n cancelRunningWaitOnTeardown: scoped,\n hooks: {\n onResource: (resource: FetchedResource) => {\n if (!isCurrent()) return;\n this.#setState({\n ...this.#state,\n loading: false,\n prefersBorder: resource._meta?.ui?.prefersBorder === true,\n });\n },\n onInitialized: () => {\n if (isCurrent()) {\n iframe.setAttribute(\"data-mcp-app-initialized\", \"true\");\n }\n },\n onSizeChanged: (size) => {\n if (isCurrent()) this.#applySize(iframe, size);\n },\n onContentError: (err) => {\n if (!isCurrent()) return;\n this.#setState({\n ...this.#state,\n contentError: err?.message ?? null,\n });\n },\n onError: fail,\n ...(scoped ? { onFollowUpError: fail } : {}),\n },\n });\n this.#session = session;\n // Seed the content: an activity rendered from an external message\n // list is not in the agent's store, so nothing else would send it.\n session.syncContent(this.#content ?? content);\n })\n .catch((err: unknown) => {\n console.error(\"[MCPAppsRenderer] Setup error:\", err);\n fail(err instanceof Error ? err : new Error(String(err)));\n });\n }\n\n #applySize(\n iframe: HTMLIFrameElement,\n size: { width?: number; height?: number },\n ) {\n const next: McpAppHostState[\"size\"] = {};\n if (typeof size.width === \"number\" && Number.isFinite(size.width)) {\n next.width = size.width;\n // Allow expansion but cap at the container width.\n iframe.style.minWidth = `min(${String(size.width)}px, 100%)`;\n iframe.style.width = \"100%\";\n }\n const height = size.height;\n if (typeof height === \"number\" && Number.isFinite(height) && height > 0) {\n next.height = Math.ceil(Math.min(height, MCP_APP_MAX_HEIGHT));\n iframe.style.height = `${String(next.height)}px`;\n }\n this.#setState({ ...this.#state, size: next });\n }\n\n #unbind() {\n this.#generation += 1;\n this.#session?.teardown();\n this.#session = null;\n this.#iframe?.remove();\n this.#iframe = null;\n }\n\n #setState(state: McpAppHostState) {\n this.#state = state;\n for (const listener of this.#listeners) listener();\n }\n}\n"],"mappings":";;;AAoBA,MAAa,oCACX;;AAKF,MAAa,4BAA4B;;AAGzC,MAAa,qBAAqB;;AAGlC,MAAM,oBAAoB;;;;;;;;;;AAc1B,SAAgB,oBA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AAO,IAAI,SAAS,GAAG;AACvE,QAAK,SAAS,KAAK,KAAK,KAAK,IAAI,QAAQ,mBAAmB,CAAC;AAC7D,UAAO,MAAM,SAAS,GAAG,OAAO,KAAK,OAAO,CAAC;;AAE/C,QAAKS,SAAU;GAAE,GAAG,MAAKZ;GAAQ,MAAM;GAAM,CAAC;;CAGhD,UAAU;AACR,QAAKa,cAAe;AACpB,QAAKT,SAAU,UAAU;AACzB,QAAKA,UAAW;AAChB,QAAKU,QAAS,QAAQ;AACtB,QAAKA,SAAU;;CAGjB,UAAU,OAAwB;AAChC,QAAKd,QAAS;AACd,OAAK,MAAM,YAAY,MAAKD,UAAY,WAAU"}
|
package/dist/host.d.cts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { MCPAppsActivityContent } from "./content-schema.cjs";
|
|
2
|
+
import { ɵMcpFollowUpHost } from "./follow-up.cjs";
|
|
3
|
+
import { BindMcpAppOptions, McpAppSession, McpAppSessionOptions } from "./session.cjs";
|
|
4
|
+
import { AbstractAgent } from "@ag-ui/client";
|
|
5
|
+
|
|
6
|
+
//#region src/host.d.ts
|
|
7
|
+
/** The part of the bridge module the host uses, loaded on demand. */
|
|
8
|
+
interface McpAppsRendererModule {
|
|
9
|
+
bindMcpApp(options: BindMcpAppOptions): McpAppSession;
|
|
10
|
+
}
|
|
11
|
+
/** Message shown when the bridge package cannot be loaded. */
|
|
12
|
+
declare const MCP_APPS_RENDERER_MISSING_MESSAGE: string;
|
|
13
|
+
/** Error text when an MCP App renders without an agent to fetch it through. */
|
|
14
|
+
declare const MCP_APPS_NO_AGENT_MESSAGE = "No agent available to fetch resource";
|
|
15
|
+
/** Tallest an MCP App iframe may grow, in pixels. */
|
|
16
|
+
declare const MCP_APP_MAX_HEIGHT = 5000;
|
|
17
|
+
/** Imports the bridge module; frontends pass `() => import("@copilotkit/mcp-apps-renderer")`. */
|
|
18
|
+
type McpAppsRendererImporter = () => Promise<McpAppsRendererModule>;
|
|
19
|
+
/**
|
|
20
|
+
* Loads the bridge (`bindMcpApp`) lazily. The bridge pulls the MCP SDK and
|
|
21
|
+
* its zod schemas (~40-50 kB gzipped), so an app only pays for it when an MCP
|
|
22
|
+
* App renders. The `import()` itself stays in the frontend, where its bundler
|
|
23
|
+
* can split it out. A failed load rethrows with what to install, since a raw
|
|
24
|
+
* module-resolution error does not say.
|
|
25
|
+
*
|
|
26
|
+
* @param importRenderer - `() => import("@copilotkit/mcp-apps-renderer")`.
|
|
27
|
+
*/
|
|
28
|
+
declare function loadMcpAppsRenderer(importRenderer: McpAppsRendererImporter): Promise<McpAppsRendererModule>;
|
|
29
|
+
/** What a frontend renders around the MCP App iframe. */
|
|
30
|
+
interface McpAppHostState {
|
|
31
|
+
loading: boolean;
|
|
32
|
+
/** Fatal setup failure; shown until the host binds again. */
|
|
33
|
+
error: string | null;
|
|
34
|
+
/** Recoverable: the latest content was rejected and not forwarded. */
|
|
35
|
+
contentError: string | null;
|
|
36
|
+
/** The size the widget reported. */
|
|
37
|
+
size: {
|
|
38
|
+
width?: number;
|
|
39
|
+
height?: number;
|
|
40
|
+
};
|
|
41
|
+
/** The resource asked the host for a border and background. */
|
|
42
|
+
prefersBorder: boolean;
|
|
43
|
+
}
|
|
44
|
+
interface McpAppHostOptions {
|
|
45
|
+
/** The CopilotKit core, for `ui/message` follow-up runs. */
|
|
46
|
+
host: ɵMcpFollowUpHost;
|
|
47
|
+
/** Session options (host info, timeouts, `allowSameOriginFallback`). */
|
|
48
|
+
sessionOptions?: McpAppSessionOptions;
|
|
49
|
+
/**
|
|
50
|
+
* Scope the session to the widget (Angular's contract): teardown cancels
|
|
51
|
+
* its queued follow-up and releases running waits, the fetched resource URI
|
|
52
|
+
* must match exactly, and a failed follow-up or setup error tears the
|
|
53
|
+
* session down, blanks the iframe and shows the error.
|
|
54
|
+
*/
|
|
55
|
+
scopeToWidget?: boolean;
|
|
56
|
+
/** Attributes set on the iframe, such as a `data-testid` or `title`. */
|
|
57
|
+
iframeAttributes?: Record<string, string>;
|
|
58
|
+
/** `() => import("@copilotkit/mcp-apps-renderer")`; see {@link loadMcpAppsRenderer}. */
|
|
59
|
+
importRenderer: McpAppsRendererImporter;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Hosts one MCP App: creates the sandbox iframe in a container, binds the
|
|
63
|
+
* MCP session to it, sizes it as the widget reports, and keeps the state a
|
|
64
|
+
* frontend renders. It binds again (new iframe, new session) only when the
|
|
65
|
+
* container, agent, message id or widget identity (resource URI, server)
|
|
66
|
+
* changes; new tool input or results feed the existing session.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const app = new ɵMcpAppHost({ host: core });
|
|
71
|
+
* app.subscribe(() => render(app.getState()));
|
|
72
|
+
* app.setContent(content);
|
|
73
|
+
* app.setAgent(agent);
|
|
74
|
+
* app.setMessageId(message.id);
|
|
75
|
+
* app.setContainer(element); // on mount
|
|
76
|
+
* app.destroy(); // on unmount
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
declare class McpAppHost {
|
|
80
|
+
#private;
|
|
81
|
+
constructor(options: McpAppHostOptions);
|
|
82
|
+
/** The current state. The same object until something changes. */
|
|
83
|
+
getState: () => McpAppHostState;
|
|
84
|
+
/** Calls `listener` when the state changes. Returns the unsubscribe function. */
|
|
85
|
+
subscribe: (listener: () => void) => () => void;
|
|
86
|
+
/** Sets the element the iframe goes into. `null` removes it. */
|
|
87
|
+
setContainer(container: HTMLElement | null): void;
|
|
88
|
+
/** Sets the latest activity content. */
|
|
89
|
+
setContent(content: MCPAppsActivityContent): void;
|
|
90
|
+
/** Sets the agent the widget's requests go through. */
|
|
91
|
+
setAgent(agent: AbstractAgent | undefined): void;
|
|
92
|
+
/** Sets the id of the activity message, so the session follows the store. */
|
|
93
|
+
setMessageId(messageId: string | undefined): void;
|
|
94
|
+
/** Tears the session down and removes the iframe. A later setter binds again. */
|
|
95
|
+
destroy(): void;
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
export { MCP_APPS_NO_AGENT_MESSAGE, MCP_APPS_RENDERER_MISSING_MESSAGE, MCP_APP_MAX_HEIGHT, McpAppHost, McpAppHostOptions, McpAppHostState, McpAppsRendererImporter, loadMcpAppsRenderer };
|
|
99
|
+
//# sourceMappingURL=host.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host.d.cts","names":[],"sources":["../src/host.ts"],"mappings":";;;;;;;UAeiB,qBAAA;EACf,UAAA,CAAW,OAAA,EAAS,iBAAA,GAAoB,aAAA;AAAA;;cAI7B,iCAAA;;cAMA,yBAAA;;cAGA,kBAAA;;KAMD,uBAAA,SAAgC,OAAA,CAAQ,qBAAA;AAfpD;;;;;AAMA;;;;AANA,iBA0BgB,mBAAA,CAAoB,cAAA,EAAgB,uBAAA,GAAuB,OAAA,CAAA,qBAAA;AAjB3E;AAAA,UAwBiB,eAAA;EACf,OAAA;EAzB6B;EA2B7B,KAAA;EArBU;EAuBV,YAAA;;EAEA,IAAA;IAAQ,KAAA;IAAgB,MAAA;EAAA;;EAExB,aAAA;AAAA;AAAA,UAGe,iBAAA;EAnB0D;EAqBzE,IAAA,EAAM,gBAAA;EArB4C;EAuBlD,cAAA,GAAiB,oBAAA;EAvBwD;;;;AAO3E;;EAuBE,aAAA;EAvB8B;EAyB9B,gBAAA,GAAmB,MAAA;EAtBnB;EAwBA,cAAA,EAAgB,uBAAA;AAAA;;;;;;AAflB;;;;;;;;;;;;;cAqDa,UAAA;EAAA;cAcC,OAAA,EAAS,iBAAA;EApDrB;EAyDA,QAAA,QAAQ,eAAA;EAzD+B;EA4DvC,SAAA,GAAa,QAAA;EAtBF;EA8BX,YAAA,CAAa,SAAA,EAAW,WAAA;;EAMxB,UAAA,CAAW,OAAA,EAAS,sBAAA;EAjBZ;EAuBR,QAAA,CAAS,KAAA,EAAO,aAAA;EANI;EAYpB,YAAA,CAAa,SAAA;EANgB;EAY7B,OAAA,CAAA;AAAA"}
|
package/dist/host.d.mts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { MCPAppsActivityContent } from "./content-schema.mjs";
|
|
2
|
+
import { ɵMcpFollowUpHost } from "./follow-up.mjs";
|
|
3
|
+
import { BindMcpAppOptions, McpAppSession, McpAppSessionOptions } from "./session.mjs";
|
|
4
|
+
import { AbstractAgent } from "@ag-ui/client";
|
|
5
|
+
|
|
6
|
+
//#region src/host.d.ts
|
|
7
|
+
/** The part of the bridge module the host uses, loaded on demand. */
|
|
8
|
+
interface McpAppsRendererModule {
|
|
9
|
+
bindMcpApp(options: BindMcpAppOptions): McpAppSession;
|
|
10
|
+
}
|
|
11
|
+
/** Message shown when the bridge package cannot be loaded. */
|
|
12
|
+
declare const MCP_APPS_RENDERER_MISSING_MESSAGE: string;
|
|
13
|
+
/** Error text when an MCP App renders without an agent to fetch it through. */
|
|
14
|
+
declare const MCP_APPS_NO_AGENT_MESSAGE = "No agent available to fetch resource";
|
|
15
|
+
/** Tallest an MCP App iframe may grow, in pixels. */
|
|
16
|
+
declare const MCP_APP_MAX_HEIGHT = 5000;
|
|
17
|
+
/** Imports the bridge module; frontends pass `() => import("@copilotkit/mcp-apps-renderer")`. */
|
|
18
|
+
type McpAppsRendererImporter = () => Promise<McpAppsRendererModule>;
|
|
19
|
+
/**
|
|
20
|
+
* Loads the bridge (`bindMcpApp`) lazily. The bridge pulls the MCP SDK and
|
|
21
|
+
* its zod schemas (~40-50 kB gzipped), so an app only pays for it when an MCP
|
|
22
|
+
* App renders. The `import()` itself stays in the frontend, where its bundler
|
|
23
|
+
* can split it out. A failed load rethrows with what to install, since a raw
|
|
24
|
+
* module-resolution error does not say.
|
|
25
|
+
*
|
|
26
|
+
* @param importRenderer - `() => import("@copilotkit/mcp-apps-renderer")`.
|
|
27
|
+
*/
|
|
28
|
+
declare function loadMcpAppsRenderer(importRenderer: McpAppsRendererImporter): Promise<McpAppsRendererModule>;
|
|
29
|
+
/** What a frontend renders around the MCP App iframe. */
|
|
30
|
+
interface McpAppHostState {
|
|
31
|
+
loading: boolean;
|
|
32
|
+
/** Fatal setup failure; shown until the host binds again. */
|
|
33
|
+
error: string | null;
|
|
34
|
+
/** Recoverable: the latest content was rejected and not forwarded. */
|
|
35
|
+
contentError: string | null;
|
|
36
|
+
/** The size the widget reported. */
|
|
37
|
+
size: {
|
|
38
|
+
width?: number;
|
|
39
|
+
height?: number;
|
|
40
|
+
};
|
|
41
|
+
/** The resource asked the host for a border and background. */
|
|
42
|
+
prefersBorder: boolean;
|
|
43
|
+
}
|
|
44
|
+
interface McpAppHostOptions {
|
|
45
|
+
/** The CopilotKit core, for `ui/message` follow-up runs. */
|
|
46
|
+
host: ɵMcpFollowUpHost;
|
|
47
|
+
/** Session options (host info, timeouts, `allowSameOriginFallback`). */
|
|
48
|
+
sessionOptions?: McpAppSessionOptions;
|
|
49
|
+
/**
|
|
50
|
+
* Scope the session to the widget (Angular's contract): teardown cancels
|
|
51
|
+
* its queued follow-up and releases running waits, the fetched resource URI
|
|
52
|
+
* must match exactly, and a failed follow-up or setup error tears the
|
|
53
|
+
* session down, blanks the iframe and shows the error.
|
|
54
|
+
*/
|
|
55
|
+
scopeToWidget?: boolean;
|
|
56
|
+
/** Attributes set on the iframe, such as a `data-testid` or `title`. */
|
|
57
|
+
iframeAttributes?: Record<string, string>;
|
|
58
|
+
/** `() => import("@copilotkit/mcp-apps-renderer")`; see {@link loadMcpAppsRenderer}. */
|
|
59
|
+
importRenderer: McpAppsRendererImporter;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Hosts one MCP App: creates the sandbox iframe in a container, binds the
|
|
63
|
+
* MCP session to it, sizes it as the widget reports, and keeps the state a
|
|
64
|
+
* frontend renders. It binds again (new iframe, new session) only when the
|
|
65
|
+
* container, agent, message id or widget identity (resource URI, server)
|
|
66
|
+
* changes; new tool input or results feed the existing session.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const app = new ɵMcpAppHost({ host: core });
|
|
71
|
+
* app.subscribe(() => render(app.getState()));
|
|
72
|
+
* app.setContent(content);
|
|
73
|
+
* app.setAgent(agent);
|
|
74
|
+
* app.setMessageId(message.id);
|
|
75
|
+
* app.setContainer(element); // on mount
|
|
76
|
+
* app.destroy(); // on unmount
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
declare class McpAppHost {
|
|
80
|
+
#private;
|
|
81
|
+
constructor(options: McpAppHostOptions);
|
|
82
|
+
/** The current state. The same object until something changes. */
|
|
83
|
+
getState: () => McpAppHostState;
|
|
84
|
+
/** Calls `listener` when the state changes. Returns the unsubscribe function. */
|
|
85
|
+
subscribe: (listener: () => void) => () => void;
|
|
86
|
+
/** Sets the element the iframe goes into. `null` removes it. */
|
|
87
|
+
setContainer(container: HTMLElement | null): void;
|
|
88
|
+
/** Sets the latest activity content. */
|
|
89
|
+
setContent(content: MCPAppsActivityContent): void;
|
|
90
|
+
/** Sets the agent the widget's requests go through. */
|
|
91
|
+
setAgent(agent: AbstractAgent | undefined): void;
|
|
92
|
+
/** Sets the id of the activity message, so the session follows the store. */
|
|
93
|
+
setMessageId(messageId: string | undefined): void;
|
|
94
|
+
/** Tears the session down and removes the iframe. A later setter binds again. */
|
|
95
|
+
destroy(): void;
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
export { MCP_APPS_NO_AGENT_MESSAGE, MCP_APPS_RENDERER_MISSING_MESSAGE, MCP_APP_MAX_HEIGHT, McpAppHost, McpAppHostOptions, McpAppHostState, McpAppsRendererImporter, loadMcpAppsRenderer };
|
|
99
|
+
//# sourceMappingURL=host.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"host.d.mts","names":[],"sources":["../src/host.ts"],"mappings":";;;;;;;UAeiB,qBAAA;EACf,UAAA,CAAW,OAAA,EAAS,iBAAA,GAAoB,aAAA;AAAA;;cAI7B,iCAAA;;cAMA,yBAAA;;cAGA,kBAAA;;KAMD,uBAAA,SAAgC,OAAA,CAAQ,qBAAA;AAfpD;;;;;AAMA;;;;AANA,iBA0BgB,mBAAA,CAAoB,cAAA,EAAgB,uBAAA,GAAuB,OAAA,CAAA,qBAAA;AAjB3E;AAAA,UAwBiB,eAAA;EACf,OAAA;EAzB6B;EA2B7B,KAAA;EArBU;EAuBV,YAAA;;EAEA,IAAA;IAAQ,KAAA;IAAgB,MAAA;EAAA;;EAExB,aAAA;AAAA;AAAA,UAGe,iBAAA;EAnB0D;EAqBzE,IAAA,EAAM,gBAAA;EArB4C;EAuBlD,cAAA,GAAiB,oBAAA;EAvBwD;;;;AAO3E;;EAuBE,aAAA;EAvB8B;EAyB9B,gBAAA,GAAmB,MAAA;EAtBnB;EAwBA,cAAA,EAAgB,uBAAA;AAAA;;;;;;AAflB;;;;;;;;;;;;;cAqDa,UAAA;EAAA;cAcC,OAAA,EAAS,iBAAA;EApDrB;EAyDA,QAAA,QAAQ,eAAA;EAzD+B;EA4DvC,SAAA,GAAa,QAAA;EAtBF;EA8BX,YAAA,CAAa,SAAA,EAAW,WAAA;;EAMxB,UAAA,CAAW,OAAA,EAAS,sBAAA;EAjBZ;EAuBR,QAAA,CAAS,KAAA,EAAO,aAAA;EANI;EAYpB,YAAA,CAAa,SAAA;EANgB;EAY7B,OAAA,CAAA;AAAA"}
|