@khanglvm/relay 0.20.3 → 0.21.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.
@@ -21,20 +21,65 @@
21
21
  }
22
22
  return types;
23
23
  }
24
- async function ensureVendor(file, globalName) {
25
- if (window[globalName]) return true;
26
- try {
27
- const res = await request('resources/read', { uri: 'ui://relay/vendor/' + file });
28
- const c = res && Array.isArray(res.contents) ? res.contents[0] : null;
29
- const code = c && (c.text || (c.blob ? atob(c.blob) : ''));
30
- if (!code) return false;
24
+ function bytesOf(base64) {
25
+ const bin = atob(base64);
26
+ const out = new Uint8Array(bin.length);
27
+ for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
28
+ return out;
29
+ }
30
+ // The preferred route: the host reads the vendor file as a ui:// resource.
31
+ async function vendorFromResource(file) {
32
+ const res = await request('resources/read', { uri: 'ui://relay/vendor/' + file });
33
+ const c = res && Array.isArray(res.contents) ? res.contents[0] : null;
34
+ return c ? (c.text || (c.blob ? new TextDecoder().decode(bytesOf(c.blob)) : '')) : '';
35
+ }
36
+ // For a host that refuses resources/read: the server hands the file over in
37
+ // chunks. The chunks are joined as bytes before decoding, so a UTF-8 character
38
+ // split across two chunks stays whole.
39
+ async function vendorFromTool(file) {
40
+ const parts = [];
41
+ let count = 1;
42
+ for (let i = 0; i < count; i++) {
43
+ const chunk = await callServerTool('relay_asset', { name: file, index: i }, 60000);
44
+ count = chunk.count;
45
+ parts.push(bytesOf(chunk.data));
46
+ }
47
+ const all = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
48
+ let at = 0;
49
+ for (const p of parts) { all.set(p, at); at += p.length; }
50
+ return new TextDecoder().decode(all);
51
+ }
52
+ // Where the server says a vendor file can also be fetched over HTTP, from relay_view.
53
+ let vendorUrls = null;
54
+ function vendorFromUrl(file) {
55
+ return new Promise((resolve, reject) => {
56
+ const url = vendorUrls && vendorUrls[file];
57
+ if (!url) return reject(new Error('no url'));
31
58
  const s = document.createElement('script');
32
- s.textContent = code;
59
+ s.src = url;
60
+ s.onload = () => resolve('');
61
+ s.onerror = () => reject(new Error('script failed'));
33
62
  document.head.appendChild(s);
34
- return Boolean(window[globalName]);
35
- } catch {
36
- return false;
63
+ });
64
+ }
65
+ async function ensureVendor(file, globalName) {
66
+ if (window[globalName]) return true;
67
+ for (const load of [vendorFromResource, vendorFromTool, vendorFromUrl]) {
68
+ try {
69
+ const code = await load(file);
70
+ if (!code) {
71
+ if (window[globalName]) return true;
72
+ continue;
73
+ }
74
+ const s = document.createElement('script');
75
+ s.textContent = code;
76
+ document.head.appendChild(s);
77
+ if (window[globalName]) return true;
78
+ } catch {
79
+ // try the next route
80
+ }
37
81
  }
82
+ return false;
38
83
  }
39
84
  async function preloadVendors(spec) {
40
85
  const types = blockTypesIn(spec);
@@ -1,5 +1,5 @@
1
1
  // The browser files of each page, in the order they run. The web board
2
- // (src/server/page.js) and the MCP App page (src/mcp.js) both assemble their
2
+ // (src/server/page.js) and the MCP App page (src/mcp/page.js) both assemble their
3
3
  // scripts and styles from this list, so a file added here reaches both pages.
4
4
  //
5
5
  // Browser scripts share one scope per page. A bundle part holds files that read
@@ -0,0 +1,46 @@
1
+ export declare const AGENT_TOPICS: {
2
+ questions: {
3
+ summary: string;
4
+ headings: string[];
5
+ };
6
+ blocks: {
7
+ summary: string;
8
+ headings: string[];
9
+ };
10
+ annotate: {
11
+ summary: string;
12
+ headings: string[];
13
+ };
14
+ lifecycle: {
15
+ summary: string;
16
+ headings: string[];
17
+ preamble: boolean;
18
+ };
19
+ sharing: {
20
+ summary: string;
21
+ headings: string[];
22
+ };
23
+ mcp: {
24
+ summary: string;
25
+ headings: string[];
26
+ };
27
+ a2ui: {
28
+ summary: string;
29
+ file: string;
30
+ };
31
+ };
32
+ /** Splits a markdown guide into its text before the first H2 and its H2 sections. Headings inside code fences are text. */
33
+ export declare function splitSections(md: any): {
34
+ preamble: string;
35
+ sections: {
36
+ title: any;
37
+ text: any;
38
+ }[];
39
+ };
40
+ /** The topic list `rly agent` prints for an unknown topic. */
41
+ export declare function topicIndex(): string;
42
+ /**
43
+ * The text of one topic. `readDoc(file)` returns a docs/ file as text; the
44
+ * caller owns the filesystem so this stays testable.
45
+ */
46
+ export declare function renderAgentTopic(topic: any, readDoc: any): any;
@@ -0,0 +1,168 @@
1
+ /** One board call's mode: a board that wants a reply is `ask`, anything else `show`. Questions always want a reply. */
2
+ export declare const modeOf: (spec: any) => "ask" | "show";
3
+ /**
4
+ * Normalizes the tool arguments into a stored board. Spec errors come back as
5
+ * CliError so the tool can answer with an isError result the model can fix.
6
+ * @param {import('./store.js').RelayStore} store
7
+ * @param {Record<string, any>} args
8
+ * @param {{ displayOnly?: boolean }} [options]
9
+ */
10
+ export declare function createMcpBoard(store: import('./store.js').RelayStore, args: Record<string, any>, options?: {
11
+ displayOnly?: boolean;
12
+ }): Promise<{
13
+ record: import("./store.js").BoardRecord;
14
+ spec: {
15
+ title: string;
16
+ intro: string;
17
+ blocks: any[];
18
+ allowPartial: boolean;
19
+ note: boolean;
20
+ autoClose: boolean;
21
+ responseRequired: boolean;
22
+ questions: any[];
23
+ submitLabel: string;
24
+ };
25
+ mode: string;
26
+ }>;
27
+ /** The compact list of questions a model sees in the relay_board result: id, type, label. */
28
+ export declare function questionList(spec: any, labelMax?: number): any;
29
+ /**
30
+ * The relay_board result: a summary and structuredContent, never the spec.
31
+ * The question list shrinks until the whole result fits RESULT_BUDGET_BYTES.
32
+ * @returns {ToolResult}
33
+ */
34
+ export declare function boardResult(record: any, spec: any, mode: any): ToolResult;
35
+ export type ToolResult = import('@modelcontextprotocol/server').CallToolResult;
36
+ /** @typedef {import('@modelcontextprotocol/server').CallToolResult} ToolResult */
37
+ /** @returns {ToolResult} */
38
+ export declare const errorResult: (message: any) => ToolResult;
39
+ /** An invalid spec as a tool error; anything else is a server bug and rethrows. */
40
+ export declare function specError(err: any): {
41
+ [x: string]: unknown;
42
+ _meta?: {
43
+ [x: string]: unknown;
44
+ "io.modelcontextprotocol/serverInfo"?: {
45
+ version: string;
46
+ websiteUrl?: string;
47
+ description?: string;
48
+ icons?: {
49
+ src: string;
50
+ mimeType?: string;
51
+ sizes?: string[];
52
+ theme?: "dark" | "light";
53
+ }[];
54
+ name: string;
55
+ title?: string;
56
+ };
57
+ };
58
+ content: ({
59
+ type: "text";
60
+ text: string;
61
+ annotations?: {
62
+ audience?: ("assistant" | "user")[];
63
+ priority?: number;
64
+ lastModified?: string;
65
+ };
66
+ _meta?: {
67
+ [x: string]: unknown;
68
+ };
69
+ } | {
70
+ type: "image";
71
+ data: string;
72
+ mimeType: string;
73
+ annotations?: {
74
+ audience?: ("assistant" | "user")[];
75
+ priority?: number;
76
+ lastModified?: string;
77
+ };
78
+ _meta?: {
79
+ [x: string]: unknown;
80
+ };
81
+ } | {
82
+ type: "audio";
83
+ data: string;
84
+ mimeType: string;
85
+ annotations?: {
86
+ audience?: ("assistant" | "user")[];
87
+ priority?: number;
88
+ lastModified?: string;
89
+ };
90
+ _meta?: {
91
+ [x: string]: unknown;
92
+ };
93
+ } | {
94
+ uri: string;
95
+ description?: string;
96
+ mimeType?: string;
97
+ size?: number;
98
+ annotations?: {
99
+ audience?: ("assistant" | "user")[];
100
+ priority?: number;
101
+ lastModified?: string;
102
+ };
103
+ _meta?: {
104
+ [x: string]: unknown;
105
+ };
106
+ icons?: {
107
+ src: string;
108
+ mimeType?: string;
109
+ sizes?: string[];
110
+ theme?: "dark" | "light";
111
+ }[];
112
+ name: string;
113
+ title?: string;
114
+ type: "resource_link";
115
+ } | {
116
+ type: "resource";
117
+ resource?: {
118
+ uri: string;
119
+ mimeType?: string;
120
+ _meta?: {
121
+ [x: string]: unknown;
122
+ };
123
+ text: string;
124
+ } | {
125
+ uri: string;
126
+ mimeType?: string;
127
+ _meta?: {
128
+ [x: string]: unknown;
129
+ };
130
+ blob: string;
131
+ };
132
+ annotations?: {
133
+ audience?: ("assistant" | "user")[];
134
+ priority?: number;
135
+ lastModified?: string;
136
+ };
137
+ _meta?: {
138
+ [x: string]: unknown;
139
+ };
140
+ })[];
141
+ structuredContent?: unknown;
142
+ isError?: boolean;
143
+ };
144
+ /** Saves the person's answers so far, so relay_result can show them before the final submit. */
145
+ export declare function saveDraft(store: any, id: any, draft: any): Promise<any>;
146
+ /**
147
+ * Stores the final answers. A second submit is refused so a stray click cannot
148
+ * overwrite what the agent may already have read.
149
+ * @returns {Promise<{ ok: boolean, status?: string, error?: string }>}
150
+ */
151
+ export declare function submitBoard(store: any, record: any, body: any): Promise<{
152
+ ok: boolean;
153
+ status?: string;
154
+ error?: string;
155
+ }>;
156
+ /**
157
+ * Appends one a2ui button click for relay_result to report. The click must name
158
+ * an a2ui block on this board and pass the checks the browser board applies to a2ui state.
159
+ * @returns {Promise<{ ok: boolean, entry?: object, error?: string }>}
160
+ */
161
+ export declare function recordEvent(store: any, record: any, event: any): Promise<{
162
+ ok: boolean;
163
+ entry?: object;
164
+ error?: string;
165
+ }>;
166
+ /** What relay_result reports for a board right now. */
167
+ export declare function resultOf(record: any): any;
168
+ export declare const isFinal: (record: any) => boolean;
@@ -1,10 +1,4 @@
1
- export declare function runMcp(): Promise<any>;
2
- export declare function runMcpHttp({ port, host, token, allowOrigin }?: {
3
- allowOrigin?: string;
4
- host?: string;
5
- port?: number;
6
- token?: string;
7
- }): Promise<any>;
1
+ /** Setup for each host: a JSON entry, a Codex TOML block, and how to run the HTTP transport. */
8
2
  export declare function mcpConfig({ command, httpPort }?: {
9
3
  command?: string;
10
4
  httpPort?: number;
@@ -25,7 +19,6 @@ export declare function mcpConfig({ command, httpPort }?: {
25
19
  http: {
26
20
  run: string;
27
21
  url: string;
28
- deploy: string;
29
22
  note: string;
30
23
  };
31
24
  };
@@ -0,0 +1,21 @@
1
+ export declare const UI_EXT = "io.modelcontextprotocol/ui";
2
+ export declare const UI_MIME = "text/html;profile=mcp-app";
3
+ export declare const BOARD_URI = "ui://relay/board";
4
+ export declare const VENDOR_URI_PREFIX = "ui://relay/vendor/";
5
+ export declare const HTTP_PATH = "/mcp";
6
+ export declare const DEFAULT_HTTP_PORT = 4319;
7
+ export declare const VENDOR_CHUNK_BYTES: number;
8
+ export declare const MAX_WAIT_SECONDS = 600;
9
+ export declare const RESULT_BUDGET_BYTES = 2048;
10
+ export declare const TOOL: {
11
+ board: string;
12
+ help: string;
13
+ result: string;
14
+ view: string;
15
+ draft: string;
16
+ submit: string;
17
+ asset: string;
18
+ a2uiAction: string;
19
+ ask: string;
20
+ show: string;
21
+ };
@@ -0,0 +1,4 @@
1
+ export { mcpConfig } from './config.js';
2
+ export { runMcp, runMcpHttp } from './serve.js';
3
+ export { fileStore } from './store.js';
4
+ export { registerRelay } from './tools.js';
@@ -0,0 +1,27 @@
1
+ /** The vendored libraries a board may load, by file name. Nothing else under vendor/ is served. */
2
+ export declare const VENDOR_FILES: string[];
3
+ export declare const PKG: any;
4
+ export declare const DOCS_DIR: string;
5
+ /** The single self-contained page: styles, block renderer and the MCP client, inlined. Built once per process. */
6
+ export declare function boardPage(): any;
7
+ /** The text of one vendor file, or null when the name is not on the allowlist. */
8
+ export declare function vendorText(name: any): string;
9
+ /**
10
+ * One base64 chunk of a vendor file, for hosts that refuse resources/read.
11
+ * @returns {{ name: string, index: number, count: number, data: string } | null}
12
+ */
13
+ export declare function vendorChunk(name: any, index: any): {
14
+ name: string;
15
+ index: number;
16
+ count: number;
17
+ data: string;
18
+ } | null;
19
+ /** The resource metadata hosts read to build the iframe's CSP. */
20
+ export declare const BOARD_RESOURCE_META: {
21
+ prefersBorder: boolean;
22
+ csp: {
23
+ connectDomains: undefined[];
24
+ resourceDomains: string[];
25
+ frameDomains: string[];
26
+ };
27
+ };
@@ -0,0 +1,46 @@
1
+ import * as z from 'zod';
2
+ /** The relay_board input: the spec's top-level fields, with blocks and questions left open. */
3
+ export declare const boardInput: z.ZodObject<{
4
+ title: z.ZodOptional<z.ZodString>;
5
+ intro: z.ZodOptional<z.ZodString>;
6
+ blocks: z.ZodOptional<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
7
+ questions: z.ZodOptional<z.ZodArray<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
8
+ responseRequired: z.ZodOptional<z.ZodBoolean>;
9
+ }, z.core.$loose>;
10
+ /** The deprecated relay_ask and relay_show take the same fields, but their schema stays empty so the tool list stays small. */
11
+ export declare const aliasInput: z.ZodObject<{}, z.core.$loose>;
12
+ export declare const viewInput: z.ZodObject<{
13
+ boardId: z.ZodString;
14
+ token: z.ZodString;
15
+ }, z.core.$strip>;
16
+ export declare const draftInput: z.ZodObject<{
17
+ boardId: z.ZodString;
18
+ token: z.ZodString;
19
+ draft: z.ZodRecord<z.ZodString, z.ZodUnknown>;
20
+ }, z.core.$strip>;
21
+ export declare const submitInput: z.ZodObject<{
22
+ boardId: z.ZodString;
23
+ token: z.ZodString;
24
+ payload: z.ZodRecord<z.ZodString, z.ZodUnknown>;
25
+ }, z.core.$strip>;
26
+ export declare const actionInput: z.ZodObject<{
27
+ boardId: z.ZodString;
28
+ token: z.ZodString;
29
+ blockId: z.ZodString;
30
+ name: z.ZodString;
31
+ context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
32
+ }, z.core.$strip>;
33
+ export declare const assetInput: z.ZodObject<{
34
+ name: z.ZodString;
35
+ index: z.ZodDefault<z.ZodNumber>;
36
+ }, z.core.$strip>;
37
+ export declare const helpInput: z.ZodObject<{
38
+ topic: z.ZodOptional<z.ZodEnum<{
39
+ [x: string]: string;
40
+ }>>;
41
+ schema: z.ZodOptional<z.ZodString>;
42
+ }, z.core.$strip>;
43
+ export declare const resultInput: z.ZodObject<{
44
+ boardId: z.ZodString;
45
+ wait_seconds: z.ZodOptional<z.ZodNumber>;
46
+ }, z.core.$strip>;
@@ -0,0 +1,16 @@
1
+ /** Serves MCP over this process's stdin and stdout until the host closes stdin. */
2
+ export declare function runMcp(): Promise<any>;
3
+ /** A browser Origin is trusted when it is local or the one the operator allowed. A client with no Origin is not a browser. */
4
+ export declare function originAllowed(origin: any, allowOrigin: any): boolean;
5
+ /** Constant-time string comparison for the bearer token. */
6
+ export declare function safeEqual(a: any, b: any): boolean;
7
+ /**
8
+ * Serves MCP over Streamable HTTP. Localhost by default; a bearer token and an
9
+ * allowed Origin are the controls for anything wider.
10
+ */
11
+ export declare function runMcpHttp({ port, host, token, allowOrigin }?: {
12
+ allowOrigin?: string;
13
+ host?: string;
14
+ port?: number;
15
+ token?: string;
16
+ }): Promise<any>;
@@ -0,0 +1,4 @@
1
+ import { McpServer } from '@modelcontextprotocol/server';
2
+ import { registerRelay } from './tools.js';
3
+ /** @param {Parameters<typeof registerRelay>[1]} [options] */
4
+ export declare function createRelayServer(options?: Parameters<typeof registerRelay>[1]): McpServer;
@@ -0,0 +1,52 @@
1
+ export type BoardRecord = {
2
+ id: string;
3
+ /**
4
+ * the normalized board spec
5
+ */
6
+ spec: any;
7
+ /**
8
+ * the person's answers so far
9
+ */
10
+ draft?: any;
11
+ /**
12
+ * set once the person submits
13
+ */
14
+ result?: any;
15
+ /**
16
+ * a2ui button clicks, newest last
17
+ */
18
+ events?: any[];
19
+ };
20
+ export type RelayStore = {
21
+ create: (spec: any) => BoardRecord | Promise<BoardRecord>;
22
+ get: (id: string) => BoardRecord | null | Promise<BoardRecord | null>;
23
+ update: (id: string, patch: Partial<BoardRecord>) => BoardRecord | null | Promise<BoardRecord | null>;
24
+ };
25
+ /**
26
+ * @typedef {object} BoardRecord
27
+ * @property {string} id
28
+ * @property {any} spec the normalized board spec
29
+ * @property {any} [draft] the person's answers so far
30
+ * @property {any} [result] set once the person submits
31
+ * @property {any[]} [events] a2ui button clicks, newest last
32
+ */
33
+ /**
34
+ * @typedef {object} RelayStore
35
+ * @property {(spec: any) => BoardRecord | Promise<BoardRecord>} create
36
+ * @property {(id: string) => BoardRecord | null | Promise<BoardRecord | null>} get
37
+ * @property {(id: string, patch: Partial<BoardRecord>) => BoardRecord | null | Promise<BoardRecord | null>} update
38
+ */
39
+ /** The default store: one JSON file per board under the relay home, shared with the CLI. */
40
+ export declare function fileStore(): {
41
+ create: (spec: any) => {
42
+ id: string;
43
+ createdAt: string;
44
+ title: any;
45
+ cwd: string;
46
+ spec: any;
47
+ draft: any;
48
+ result: any;
49
+ };
50
+ get: (id: any) => any;
51
+ update(id: any, patch: any): any;
52
+ };
@@ -0,0 +1,5 @@
1
+ /** The server's HMAC key: a random value kept in the relay home, created on first use. */
2
+ export declare function loadServerKey(file?: string): string;
3
+ export declare const boardToken: (key: any, boardId: any) => string;
4
+ /** Constant-time comparison, so a wrong token leaks nothing about the right one. */
5
+ export declare function tokenValid(key: any, boardId: any, token: any): boolean;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Adds the relay tools and the board resource to `server`.
3
+ *
4
+ * @param {any} server an McpServer
5
+ * @param {object} [options]
6
+ * @param {import('./store.js').RelayStore} [options.store] where boards live; defaults to the relay home on disk
7
+ * @param {string | Buffer} [options.tokenKey] the secret behind board tokens; defaults to a key file in the relay home
8
+ * @param {(name: string) => string} [options.assetUrl] URL of a vendor file, for hosts that can fetch it
9
+ * @param {(boardId: string) => string} [options.linkUrl] a URL where a person can open the board outside the host
10
+ * @returns the app-only tools, so a server can hide them from a client that cannot render the board
11
+ */
12
+ export declare function registerRelay(server: any, options?: {
13
+ store?: import('./store.js').RelayStore;
14
+ tokenKey?: string | Buffer;
15
+ assetUrl?: (name: string) => string;
16
+ linkUrl?: (boardId: string) => string;
17
+ }): any[];
@@ -0,0 +1,10 @@
1
+ export declare const ARTIFACT_MAX_BYTES: number;
2
+ export declare const ARTIFACT_MIMES: {
3
+ 'image/png': string;
4
+ 'image/jpeg': string;
5
+ 'image/webp': string;
6
+ };
7
+ export declare function artifactBytesMatch(mime: any, bytes: any): boolean;
8
+ export declare function runOnResult(cmd: any, result: any, { quiet }?: {
9
+ quiet?: boolean;
10
+ }): void;
@@ -0,0 +1,6 @@
1
+ export declare function sanitizeAnnotations(value: any, boardId: any): any[];
2
+ export declare function sanitizeBlockEdits(value: any): {};
3
+ export declare function findBlock(spec: any, blockId: any, type: any): any;
4
+ export declare function findHtmlBlock(spec: any, blockId: any): any;
5
+ export declare function firstBoardHtml(spec: any): any;
6
+ export declare function firstQuestionHtml(q: any): any;