holon-ui 0.0.1-dev.13 → 0.0.1-dev.15
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 +59 -13
- package/package.json +1 -2
- package/runtime.d.ts +19 -20
- package/runtime.host.mjs +1 -3
- package/runtime.web.mjs +21 -3
- package/legacy.mjs +0 -88
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://holonlabs.ai/docs/)
|
|
4
4
|
|
|
5
|
-
Typed MCP clients for browser pages
|
|
5
|
+
Typed MCP clients for browser pages. One `connect()` serves a page as an MCP Apps view inside a host and as a web page that offers its tools to the browser's agent through WebMCP.
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -17,9 +17,9 @@ npm install -D holon-types typescript
|
|
|
17
17
|
npx create-holon-app foo
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
This creates an MCP server with a `greet` tool and a
|
|
20
|
+
This creates an MCP server with a `greet` tool and a page in `ui/main.ts`.
|
|
21
21
|
|
|
22
|
-
## Calling your server from
|
|
22
|
+
## Calling your server from the page
|
|
23
23
|
|
|
24
24
|
The server declares the tool's input and output:
|
|
25
25
|
|
|
@@ -37,33 +37,79 @@ server.registerTool(
|
|
|
37
37
|
);
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
`connect` from `#holon-ui` types the server's tools, so a wrong tool name, argument or result is a type error:
|
|
41
41
|
|
|
42
42
|
```sh
|
|
43
43
|
npx holon types check
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
```ts
|
|
47
|
-
import {
|
|
47
|
+
import { connect } from "#holon-ui";
|
|
48
48
|
|
|
49
|
-
const
|
|
50
|
-
await view.connect();
|
|
49
|
+
const app = await connect();
|
|
51
50
|
|
|
52
|
-
const { result } = await
|
|
51
|
+
const { result } = await app.tools.greet({ name: "Ada" });
|
|
53
52
|
|
|
54
|
-
await
|
|
53
|
+
await app.tools.great({ name: "Ada" });
|
|
55
54
|
// error TS2551: Property 'great' does not exist on type '{ readonly greet: (input: { name: string; }, options?: ToolCallOptions | undefined) => Promise<{ result: string; }>; }'. Did you mean 'greet'?
|
|
56
55
|
|
|
57
|
-
await
|
|
56
|
+
await app.tools.greet({ name: 42 });
|
|
58
57
|
// error TS2322: Type 'number' is not assignable to type 'string'.
|
|
59
58
|
|
|
60
|
-
await view.tools.greet({});
|
|
61
|
-
// error TS2741: Property '"name"' is missing in type '{}' but required in type '{ name: string; }'.
|
|
62
|
-
|
|
63
59
|
result.toFixed();
|
|
64
60
|
// error TS2551: Property 'toFixed' does not exist on type 'string'. Did you mean 'fixed'?
|
|
65
61
|
```
|
|
66
62
|
|
|
63
|
+
## Tools that run in the page
|
|
64
|
+
|
|
65
|
+
`connect` offers them to whichever agent is present: the host's agent in an MCP Apps host, the browser's agent through WebMCP on a web page.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const app = await connect({
|
|
69
|
+
tools: {
|
|
70
|
+
select_shapes: {
|
|
71
|
+
description: "Select shapes on the board the person is looking at.",
|
|
72
|
+
inputSchema: z.object({ ids: z.array(z.string()) }),
|
|
73
|
+
annotations: { destructiveHint: false, openWorldHint: false },
|
|
74
|
+
execute: ({ ids }) => ({ selected: board.select(ids) }),
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`execute` also receives a `signal` that aborts when the agent cancels.
|
|
81
|
+
|
|
82
|
+
On a web page, the browser's agent is also offered the server's tools that the generated clients know, leaving out those whose `_meta.ui.visibility` lacks `"model"` and those that need a file argument. In a host, its agent already has them.
|
|
83
|
+
|
|
84
|
+
## The call that opened a view
|
|
85
|
+
|
|
86
|
+
In an MCP Apps host, `app.opened` is the tool call that opened the view, with the output its tool returns. On a web page it is `undefined`.
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const opened = await app.opened;
|
|
90
|
+
if (opened?.tool === "greet") greeting.value = opened.output.result;
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Host-only features are on `app.view`, the ext-apps `App`:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
if (app.view) app.view.onhostcontextchanged = ({ theme }) => { if (theme) document.documentElement.style.colorScheme = theme; };
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Building the view
|
|
100
|
+
|
|
101
|
+
A view is its own build. Set the `mcp-app` condition in it, and `holon-ui` calls through the host there and over HTTP everywhere else:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { defaultClientConditions, defineConfig } from "vite";
|
|
105
|
+
|
|
106
|
+
export default defineConfig(({ mode }) => ({
|
|
107
|
+
resolve: { conditions: mode === "mcp" ? ["mcp-app", ...defaultClientConditions] : undefined },
|
|
108
|
+
}));
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`create-holon-app` sets this up.
|
|
112
|
+
|
|
67
113
|
## Community
|
|
68
114
|
|
|
69
115
|
Ask questions and share what you're building on the [Holon Discord](https://discord.gg/jXExyBdy).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "holon-ui",
|
|
3
|
-
"version": "0.0.1-dev.
|
|
3
|
+
"version": "0.0.1-dev.15",
|
|
4
4
|
"description": "Typed MCP clients for browser pages: one connect() for MCP Apps views and WebMCP pages.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"runtime.d.ts",
|
|
24
24
|
"runtime.host.mjs",
|
|
25
25
|
"runtime.web.mjs",
|
|
26
|
-
"legacy.mjs",
|
|
27
26
|
"README.md"
|
|
28
27
|
],
|
|
29
28
|
"scripts": {
|
package/runtime.d.ts
CHANGED
|
@@ -27,29 +27,28 @@ type Tools<C> = C extends { tools: infer T } ? T : never;
|
|
|
27
27
|
export type Opened<C> =
|
|
28
28
|
| { [K in keyof Tools<C> & string]: Tools<C>[K] extends (input: infer I, ...rest: never[]) => Promise<infer O> ? { tool: K; input: I; output: O } : never }[keyof Tools<C> & string]
|
|
29
29
|
| { tool: undefined; input: Record<string, unknown>; output: unknown };
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
readonly
|
|
34
|
-
/** In an MCP Apps host, the tool call that opened the view, with the output its tool returns; undefined on a web page. */
|
|
35
|
-
readonly opened: Promise<Opened<C>> | undefined;
|
|
30
|
+
type Resources<C> = C extends { readonly resources: infer R } ? R : never;
|
|
31
|
+
/** What a view can do with its app: call its tools and read its resources. */
|
|
32
|
+
type Relayed<C> = Pick<C, Extract<keyof C, "tools" | "callTool" | "close">> & {
|
|
33
|
+
readonly resources: { readonly [U in keyof Resources<C>]: Pick<Resources<C>[U], Extract<keyof Resources<C>[U], "read">> };
|
|
36
34
|
};
|
|
35
|
+
/** The page's own app C, as a view in an MCP Apps host or as a web page, told apart by view. Only a web page can list the app's tools, watch a resource or subscribe to events. close() also unregisters the page's tools and closes the view. */
|
|
36
|
+
export type Page<C> =
|
|
37
|
+
| (Relayed<C> & {
|
|
38
|
+
/** The connected ext-apps App, for host-only features. */
|
|
39
|
+
readonly view: App;
|
|
40
|
+
/** The tool call that opened the view, with the output its tool returns. */
|
|
41
|
+
readonly opened: Promise<Opened<C>>;
|
|
42
|
+
})
|
|
43
|
+
| (C & {
|
|
44
|
+
/** Undefined on a web page. */
|
|
45
|
+
readonly view: undefined;
|
|
46
|
+
readonly opened: undefined;
|
|
47
|
+
});
|
|
37
48
|
/** connect, narrowed by the generated #holon-ui module to the client C of the project's own app. A page tool cannot take the name of one of the app's tools. */
|
|
38
49
|
export type Connect<C> = <S extends Record<string, unknown> = {}>(options?: ConnectOptions<S> & { tools?: { [K in keyof Tools<C>]?: never } }) => Promise<Page<C>>;
|
|
39
50
|
/** Bind the page to its own app on the surface its build chose, and offer every tool to whichever agent is present. Most pages call the generated connect from #holon-ui. */
|
|
40
|
-
export declare function connect<S extends Record<string, unknown> = {}>(
|
|
41
|
-
app: { identity: Identity; contracts: Record<string, Contract | null>; own:
|
|
51
|
+
export declare function connect<S extends Record<string, unknown> = {}, Own extends string = string>(
|
|
52
|
+
app: { identity: Identity; /** Must include the contract of the page's own app. */ contracts: Record<string, Contract | null> & Record<NoInfer<Own>, Contract>; own: Own; /** The app's MCP endpoint on the page's origin, such as "/mcp/checkout". */ endpoint: string },
|
|
42
53
|
options?: ConnectOptions<S>,
|
|
43
54
|
): Promise<Page<RuntimeClient>>;
|
|
44
|
-
|
|
45
|
-
/** The ext-apps App with the app's own tools and resources typed; calls go through the host that embeds the page. */
|
|
46
|
-
export type View = App & Pick<RuntimeClient, "tools" | "resources" | "callTool">;
|
|
47
|
-
export declare function appClass(identity: Identity, contracts: Record<string, Contract | null>, own: string): new (info?: ConstructorParameters<typeof App>[0], ...rest: ConstructorParameters<typeof App> extends [unknown, ...infer Rest] ? Rest : never) => View;
|
|
48
|
-
export interface ExposeOptions { signal?: AbortSignal; execute?: (name: string, args: Record<string, unknown>) => Promise<unknown> }
|
|
49
|
-
export declare function exposeTools(app: Pick<RuntimeClient, "callTool" | "listTools">, options?: ExposeOptions): Promise<boolean>;
|
|
50
|
-
export type PageResult<I> = { readonly value: I; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string }> };
|
|
51
|
-
export interface PageSchema<I = unknown> { readonly "~standard": { readonly version: 1; readonly vendor: string; readonly types?: { readonly input: unknown; readonly output: I }; readonly validate: (value: unknown) => PageResult<I> | Promise<PageResult<I>>; readonly jsonSchema: { readonly input: (options: { target: "draft-2020-12" }) => object } } }
|
|
52
|
-
export type PageInput<S> = S extends PageSchema<infer I> ? I : never;
|
|
53
|
-
export interface PageAnnotations { title?: string; readOnlyHint?: boolean; destructiveHint?: boolean; idempotentHint?: boolean; openWorldHint?: boolean }
|
|
54
|
-
export interface PageHost { registerTool(name: string, config: { description: string; inputSchema: PageSchema; annotations?: PageAnnotations }, callback: (args: any, extra: any) => any): { remove(): void } }
|
|
55
|
-
export declare function registerPageTools<S extends { [name: string]: PageSchema }>(tools: { [K in keyof S]: { description: string; inputSchema: S[K]; annotations?: PageAnnotations; execute: (args: PageInput<S[K]>) => unknown } }, options?: { view?: PageHost; signal?: AbortSignal }): Promise<boolean>;
|
package/runtime.host.mjs
CHANGED
|
@@ -6,7 +6,6 @@ import { App } from "@modelcontextprotocol/ext-apps";
|
|
|
6
6
|
import { bindApps, ToolCallError } from "holon-sh/runtime";
|
|
7
7
|
|
|
8
8
|
export { bindApps, ToolCallError };
|
|
9
|
-
export { appClass, exposeTools, registerPageTools } from "./legacy.mjs";
|
|
10
9
|
|
|
11
10
|
export async function connect({ identity, contracts, own }, { tools = {}, view: { transport, ...options } = {} } = {}) {
|
|
12
11
|
const bind = session => bindApps(identity, contracts, { [own]: { session } })[own];
|
|
@@ -47,10 +46,9 @@ export async function connect({ identity, contracts, own }, { tools = {}, view:
|
|
|
47
46
|
const client = bind({
|
|
48
47
|
callTool: (request, { signal, timeout }) => app.callServerTool(request, { signal, timeout }),
|
|
49
48
|
readResource: (request, { signal, timeout }) => app.readServerResource(request, { signal, timeout }),
|
|
50
|
-
listTools: async () => { throw new Error("An MCP Apps host owns the tool catalog, so a view cannot list tools."); },
|
|
51
49
|
});
|
|
52
50
|
// Closing the view removes its tools from the host.
|
|
53
|
-
return Object.freeze({
|
|
51
|
+
return Object.freeze({ tools: client.tools, callTool: client.callTool, resources: client.resources, view: app, opened, close: () => app.close() });
|
|
54
52
|
}
|
|
55
53
|
|
|
56
54
|
function toolResult(value) {
|
package/runtime.web.mjs
CHANGED
|
@@ -5,8 +5,6 @@ import { getDisplayName } from "@modelcontextprotocol/client";
|
|
|
5
5
|
import { bindApps, ToolCallError } from "holon-sh/runtime";
|
|
6
6
|
|
|
7
7
|
export { bindApps, ToolCallError };
|
|
8
|
-
export { appClass, exposeTools, registerPageTools } from "./legacy.mjs";
|
|
9
|
-
import { withoutFiles } from "./legacy.mjs";
|
|
10
8
|
|
|
11
9
|
export async function connect({ identity, contracts, own, endpoint }, { tools = {} } = {}) {
|
|
12
10
|
const client = bindApps(identity, contracts, { [own]: { endpoint: new URL(endpoint, location.origin) } })[own];
|
|
@@ -18,7 +16,7 @@ export async function connect({ identity, contracts, own, endpoint }, { tools =
|
|
|
18
16
|
const { signal } = lifetime;
|
|
19
17
|
const offer = tool => context.registerTool(tool, { signal }).catch(error => console.warn(`WebMCP did not accept the tool ${tool.name}:`, error));
|
|
20
18
|
for (const [name, tool] of Object.entries(tools)) offer(pageTool(name, tool));
|
|
21
|
-
const contracted = name =>
|
|
19
|
+
const contracted = name => contracts[own].tools.some(tool => tool.name === name);
|
|
22
20
|
client.listTools({ signal }).then(
|
|
23
21
|
({ tools: listed }) => {
|
|
24
22
|
for (const tool of listed) {
|
|
@@ -71,3 +69,23 @@ function hints(annotations) {
|
|
|
71
69
|
const readOnlyHint = annotations?.readOnlyHint === true;
|
|
72
70
|
return { readOnlyHint, consequentialHint: !readOnlyHint && annotations?.destructiveHint !== false, untrustedContentHint: annotations?.openWorldHint !== false };
|
|
73
71
|
}
|
|
72
|
+
|
|
73
|
+
const hasFile = node => Array.isArray(node) ? node.some(hasFile) : typeof node === "object" && node !== null && ("x-mcp-file" in node || Object.values(node).some(hasFile));
|
|
74
|
+
|
|
75
|
+
// An agent writes arguments as tokens, so it is not shown a file argument (x-mcp-file): an anyOf or oneOf branch that
|
|
76
|
+
// holds one is left out, and a tool that still has one is not exposed.
|
|
77
|
+
// https://github.com/modelcontextprotocol/experimental-ext-file-uploads/blob/main/specification/draft/file-uploads.md#file-arguments
|
|
78
|
+
function withoutFiles(schema) {
|
|
79
|
+
const prune = node => {
|
|
80
|
+
if (Array.isArray(node)) return node.map(prune);
|
|
81
|
+
if (typeof node !== "object" || node === null) return node;
|
|
82
|
+
const pruned = Object.fromEntries(Object.entries(node).map(([key, value]) => [key, prune(value)]));
|
|
83
|
+
for (const key of ["anyOf", "oneOf"]) {
|
|
84
|
+
const kept = Array.isArray(pruned[key]) ? pruned[key].filter(branch => !hasFile(branch)) : [];
|
|
85
|
+
if (kept.length) pruned[key] = kept;
|
|
86
|
+
}
|
|
87
|
+
return pruned;
|
|
88
|
+
};
|
|
89
|
+
const pruned = prune(schema);
|
|
90
|
+
return hasFile(pruned) ? undefined : pruned;
|
|
91
|
+
}
|
package/legacy.mjs
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
// The page API that connect() replaces, kept until the CLI generates connect.
|
|
2
|
-
import { App } from "@modelcontextprotocol/ext-apps";
|
|
3
|
-
import { bindApps, ToolCallError } from "holon-sh/runtime";
|
|
4
|
-
|
|
5
|
-
// appClass returns the ext-apps App with the app's own tools and resources (contracts[own]) typed.
|
|
6
|
-
// Calls go through the host that embeds the page, so connect the App first, as with any App.
|
|
7
|
-
// Unless told otherwise, the page introduces itself to the host as the app.
|
|
8
|
-
export function appClass(identity, contracts, own) {
|
|
9
|
-
return class extends App {
|
|
10
|
-
constructor(info = { name: identity.name, version: identity.version }, ...rest) { super(info, ...rest); }
|
|
11
|
-
#client = bindApps(identity, { [own]: contracts[own] }, { [own]: { session: {
|
|
12
|
-
callTool: (request, { signal, timeout } = {}) => this.callServerTool(request, { signal, timeout }),
|
|
13
|
-
readResource: (request, { signal, timeout } = {}) => this.readServerResource(request, { signal, timeout }),
|
|
14
|
-
listTools: () => { throw new Error("The host owns the tool catalog; bind an endpoint to list tools."); },
|
|
15
|
-
} } })[own];
|
|
16
|
-
get tools() { return this.#client.tools; }
|
|
17
|
-
get resources() { return this.#client.resources; }
|
|
18
|
-
callTool(request, options) { return this.#client.callTool(request, options); }
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const hasFile = node => Array.isArray(node) ? node.some(hasFile) : typeof node === "object" && node !== null && ("x-mcp-file" in node || Object.values(node).some(hasFile));
|
|
23
|
-
|
|
24
|
-
// An agent writes arguments as tokens, so it is not shown a file argument (x-mcp-file): an anyOf or oneOf branch that
|
|
25
|
-
// holds one is left out, and a tool that still has one is not exposed.
|
|
26
|
-
// https://github.com/modelcontextprotocol/experimental-ext-file-uploads/blob/main/specification/draft/file-uploads.md#file-arguments
|
|
27
|
-
export function withoutFiles(schema) {
|
|
28
|
-
const prune = node => {
|
|
29
|
-
if (Array.isArray(node)) return node.map(prune);
|
|
30
|
-
if (typeof node !== "object" || node === null) return node;
|
|
31
|
-
const pruned = Object.fromEntries(Object.entries(node).map(([key, value]) => [key, prune(value)]));
|
|
32
|
-
for (const key of ["anyOf", "oneOf"]) {
|
|
33
|
-
const kept = Array.isArray(pruned[key]) ? pruned[key].filter(branch => !hasFile(branch)) : [];
|
|
34
|
-
if (kept.length) pruned[key] = kept;
|
|
35
|
-
}
|
|
36
|
-
return pruned;
|
|
37
|
-
};
|
|
38
|
-
const pruned = hasFile(schema) ? prune(schema) : schema;
|
|
39
|
-
return hasFile(pruned) ? undefined : pruned;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export async function exposeTools(app, options = {}) {
|
|
43
|
-
const context = globalThis.document?.modelContext ?? globalThis.navigator?.modelContext;
|
|
44
|
-
if (!context) return false;
|
|
45
|
-
const { signal } = options;
|
|
46
|
-
const execute = options.execute ?? (async (name, args) => {
|
|
47
|
-
const result = await app.callTool({ name, arguments: args }, { signal });
|
|
48
|
-
// An agent can only supply arguments, so it cannot answer. A custom execute can.
|
|
49
|
-
if (result.resultType === "input_required") throw new ToolCallError("page", name, result);
|
|
50
|
-
const { downloads, ...shown } = result;
|
|
51
|
-
return shown;
|
|
52
|
-
});
|
|
53
|
-
const { tools } = await app.listTools({ signal });
|
|
54
|
-
for (const tool of tools) {
|
|
55
|
-
// MCP Apps: a tool whose visibility omits "model" must not reach the agent; absent means ["model", "app"].
|
|
56
|
-
// https://github.com/modelcontextprotocol/ext-apps/blob/main/specification/2026-01-26/apps.mdx#resource-discovery
|
|
57
|
-
if (!(tool._meta?.ui?.visibility ?? ["model"]).includes("model")) continue;
|
|
58
|
-
const inputSchema = withoutFiles(tool.inputSchema);
|
|
59
|
-
if (!inputSchema) continue;
|
|
60
|
-
await context.registerTool({
|
|
61
|
-
name: tool.name, title: tool.title, description: tool.description ?? "", inputSchema, annotations: tool.annotations,
|
|
62
|
-
execute: args => execute(tool.name, args ?? {}),
|
|
63
|
-
}, { signal });
|
|
64
|
-
}
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export async function registerPageTools(tools, { view, signal } = {}) {
|
|
69
|
-
const context = globalThis.document?.modelContext ?? globalThis.navigator?.modelContext;
|
|
70
|
-
const removals = [];
|
|
71
|
-
for (const [name, { description, inputSchema, annotations, execute }] of Object.entries(tools)) {
|
|
72
|
-
if (view) {
|
|
73
|
-
removals.push(view.registerTool(name, { description, inputSchema, annotations }, args => execute(args)).remove);
|
|
74
|
-
} else if (context) {
|
|
75
|
-
const standard = inputSchema["~standard"];
|
|
76
|
-
await context.registerTool({
|
|
77
|
-
name, description, annotations, inputSchema: standard.jsonSchema.input({ target: "draft-2020-12" }),
|
|
78
|
-
execute: async args => {
|
|
79
|
-
const parsed = await standard.validate(args ?? {});
|
|
80
|
-
if (parsed.issues) throw new Error(`Invalid arguments for ${name}: ${JSON.stringify(parsed.issues)}`);
|
|
81
|
-
return execute(parsed.value);
|
|
82
|
-
},
|
|
83
|
-
}, { signal });
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
signal?.addEventListener("abort", () => { for (const remove of removals) remove(); }, { once: true });
|
|
87
|
-
return Boolean(view || context);
|
|
88
|
-
}
|