@thegreataxios/webmcp-svelte 0.0.0-stage → 0.2.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sawyer Cutler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,53 @@
1
- # Temporary Holding Version
1
+ # @thegreataxios/webmcp-svelte
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Svelte 4 / 5 bindings for the [W3C WebMCP](https://github.com/webmachinelearning/webmcp) API.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @thegreataxios/webmcp-svelte @thegreataxios/webmcp-core
9
+ # or
10
+ bun add @thegreataxios/webmcp-svelte @thegreataxios/webmcp-core
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ```svelte
16
+ <script lang="ts">
17
+ import { createWebMCPProvider, useWebMCPTool } from "@thegreataxios/webmcp-svelte";
18
+
19
+ createWebMCPProvider({ name: "shop", version: "1.0.0" });
20
+
21
+ useWebMCPTool({
22
+ name: "search_catalog",
23
+ title: "Search Catalog",
24
+ description: "Search products by keyword",
25
+ inputSchema: {
26
+ type: "object",
27
+ properties: { q: { type: "string" } },
28
+ required: ["q"],
29
+ },
30
+ annotations: { readOnlyHint: true },
31
+ handler: async ({ q }, options) => {
32
+ const res = await fetch(`/api/search?q=${encodeURIComponent(String(q))}`, {
33
+ signal: options?.signal,
34
+ });
35
+ return res.json();
36
+ },
37
+ });
38
+ </script>
39
+ ```
40
+
41
+ ## Exports
42
+
43
+ - `createWebMCPProvider({ name, version })` / `getWebMCP()`
44
+ - `useWebMCPTool(options)` — auto-aborts on component destroy
45
+ - `useWaitForToolsUntil(promise)`
46
+ - `experimental_useWebMCPSync({ initial, tools })` — returns a Svelte `Readable<T>` store
47
+ - `experimental_useWebMCPJourney({ name, tools, active })`
48
+ - `experimental_createConfirmProvider()` / `experimental_useWebMCPConfirm()` / `experimental_useWebMCPGuardedTool(options)` — HITL confirmation queue & guarded tool registration
49
+ - `experimental_useWebMCPBridge({ url, token })`
50
+
51
+ ## License
52
+
53
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,216 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ createWebMCPProvider: () => createWebMCPProvider,
24
+ experimental_createWebMCPConfirm: () => experimental_createWebMCPConfirm,
25
+ experimental_useWebMCPBridge: () => experimental_useWebMCPBridge,
26
+ experimental_useWebMCPGuardedTool: () => experimental_useWebMCPGuardedTool,
27
+ experimental_useWebMCPJourney: () => experimental_useWebMCPJourney,
28
+ experimental_useWebMCPSync: () => experimental_useWebMCPSync,
29
+ getWebMCP: () => getWebMCP,
30
+ useWaitForToolsUntil: () => useWaitForToolsUntil,
31
+ useWebMCPTool: () => useWebMCPTool
32
+ });
33
+ module.exports = __toCommonJS(index_exports);
34
+ var import_svelte = require("svelte");
35
+ var import_store = require("svelte/store");
36
+ var import_webmcp_core = require("@thegreataxios/webmcp-core");
37
+ var WEBMCP_CONTEXT_KEY = /* @__PURE__ */ Symbol("webmcp-svelte-context");
38
+ var WEBMCP_CONFIRM_KEY = /* @__PURE__ */ Symbol("webmcp-svelte-confirm");
39
+ function tryOnDestroy(fn) {
40
+ try {
41
+ (0, import_svelte.onDestroy)(fn);
42
+ } catch {
43
+ }
44
+ }
45
+ function trySetContext(key, value) {
46
+ try {
47
+ (0, import_svelte.setContext)(key, value);
48
+ } catch {
49
+ }
50
+ }
51
+ function tryGetContext(key) {
52
+ try {
53
+ return (0, import_svelte.getContext)(key);
54
+ } catch {
55
+ return void 0;
56
+ }
57
+ }
58
+ function createWebMCPProvider(options) {
59
+ const journeyRegistry = (0, import_webmcp_core.experimental_createJourneyRegistry)();
60
+ (0, import_webmcp_core.setJourneyRegistry)(journeyRegistry);
61
+ (0, import_webmcp_core.installPolyfill)();
62
+ let destroyed = false;
63
+ const destroy = () => {
64
+ if (destroyed) return;
65
+ destroyed = true;
66
+ (0, import_webmcp_core.setJourneyRegistry)(null);
67
+ (0, import_webmcp_core.cleanupPolyfill)();
68
+ };
69
+ const ctx = {
70
+ available: typeof document !== "undefined" && document.modelContext != null,
71
+ native: (0, import_webmcp_core.isNativeModelContext)(),
72
+ appName: options.name,
73
+ appVersion: options.version,
74
+ journeyRegistry,
75
+ destroy
76
+ };
77
+ trySetContext(WEBMCP_CONTEXT_KEY, ctx);
78
+ tryOnDestroy(destroy);
79
+ return ctx;
80
+ }
81
+ function getWebMCP() {
82
+ return tryGetContext(WEBMCP_CONTEXT_KEY);
83
+ }
84
+ function useWebMCPTool(options) {
85
+ const controller = new AbortController();
86
+ let disposed = false;
87
+ const dispose = () => {
88
+ if (disposed) return;
89
+ disposed = true;
90
+ controller.abort();
91
+ };
92
+ if (typeof document !== "undefined" && document.modelContext) {
93
+ document.modelContext.registerTool(
94
+ {
95
+ name: options.name,
96
+ title: options.title,
97
+ description: options.description,
98
+ inputSchema: options.inputSchema,
99
+ outputSchema: options.outputSchema,
100
+ annotations: options.annotations,
101
+ execute: options.handler
102
+ },
103
+ { signal: controller.signal, exposedTo: options.exposedTo }
104
+ ).catch(() => {
105
+ });
106
+ }
107
+ tryOnDestroy(dispose);
108
+ return { dispose };
109
+ }
110
+ function useWaitForToolsUntil(promise) {
111
+ if (typeof document !== "undefined") {
112
+ document.modelContext?.waitForToolsUntil?.(promise);
113
+ }
114
+ }
115
+ function experimental_useWebMCPSync(options) {
116
+ const controller = (0, import_webmcp_core.experimental_createSyncStateController)(options.initial);
117
+ const store = (0, import_store.writable)(controller.getState());
118
+ const unsub = controller.subscribe((next) => store.set(next));
119
+ const handles = Object.entries(options.tools).map(
120
+ ([name, def]) => useWebMCPTool({
121
+ name,
122
+ title: def.title,
123
+ description: def.description,
124
+ inputSchema: def.inputSchema,
125
+ outputSchema: def.outputSchema,
126
+ annotations: def.annotations,
127
+ handler: (args, cbOptions) => controller.enqueueMutation(def.reducer, args, cbOptions)
128
+ })
129
+ );
130
+ const dispose = () => {
131
+ unsub();
132
+ for (const h of handles) h.dispose();
133
+ };
134
+ tryOnDestroy(dispose);
135
+ return {
136
+ state: { subscribe: store.subscribe },
137
+ set: controller.setState,
138
+ update: controller.updateState,
139
+ dispose
140
+ };
141
+ }
142
+ function experimental_useWebMCPJourney(options) {
143
+ const registry = options.registry ?? getWebMCP()?.journeyRegistry;
144
+ registry?.register({
145
+ name: options.name,
146
+ description: options.description,
147
+ tools: options.tools
148
+ });
149
+ if (options.active) {
150
+ registry?.setJourneyActive(options.name, true);
151
+ }
152
+ const dispose = () => {
153
+ registry?.unregister(options.name);
154
+ };
155
+ tryOnDestroy(dispose);
156
+ return {
157
+ setActive: (active) => registry?.setJourneyActive(options.name, active),
158
+ dispose
159
+ };
160
+ }
161
+ function experimental_useWebMCPBridge(options) {
162
+ const statusStore = (0, import_store.writable)("disconnected");
163
+ const client = (0, import_webmcp_core.createPageBridgeClient)({
164
+ url: options.url ?? "ws://127.0.0.1:17321/ws",
165
+ token: options.token,
166
+ onStatusChange: (next) => statusStore.set(next)
167
+ });
168
+ client.connect();
169
+ const disconnect = () => client.disconnect();
170
+ tryOnDestroy(disconnect);
171
+ return {
172
+ status: { subscribe: statusStore.subscribe },
173
+ disconnect
174
+ };
175
+ }
176
+ function experimental_createWebMCPConfirm() {
177
+ const queue = (0, import_webmcp_core.experimental_createConfirmQueue)();
178
+ const store = (0, import_store.writable)(queue.getPending());
179
+ const unsub = queue.subscribe((next) => store.set(next));
180
+ const dispose = () => {
181
+ unsub();
182
+ queue.dispose();
183
+ };
184
+ trySetContext(WEBMCP_CONFIRM_KEY, queue);
185
+ tryOnDestroy(dispose);
186
+ return {
187
+ pending: { subscribe: store.subscribe },
188
+ queue,
189
+ dispose
190
+ };
191
+ }
192
+ function experimental_useWebMCPGuardedTool(options) {
193
+ const queue = options.confirmQueue ?? tryGetContext(WEBMCP_CONFIRM_KEY);
194
+ return useWebMCPTool({
195
+ ...options,
196
+ handler: (0, import_webmcp_core.experimental_wrapGuardedHandler)({
197
+ name: options.name,
198
+ requiresConfirm: options.requiresConfirm,
199
+ queue,
200
+ handler: options.handler
201
+ })
202
+ });
203
+ }
204
+ // Annotate the CommonJS export names for ESM import in node:
205
+ 0 && (module.exports = {
206
+ createWebMCPProvider,
207
+ experimental_createWebMCPConfirm,
208
+ experimental_useWebMCPBridge,
209
+ experimental_useWebMCPGuardedTool,
210
+ experimental_useWebMCPJourney,
211
+ experimental_useWebMCPSync,
212
+ getWebMCP,
213
+ useWaitForToolsUntil,
214
+ useWebMCPTool
215
+ });
216
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { getContext, onDestroy, setContext } from \"svelte\";\nimport { writable, type Readable } from \"svelte/store\";\nimport {\n cleanupPolyfill,\n createPageBridgeClient,\n experimental_createConfirmQueue,\n experimental_createJourneyRegistry,\n experimental_createSyncStateController,\n experimental_wrapGuardedHandler,\n installPolyfill,\n isNativeModelContext,\n setJourneyRegistry,\n type ConfirmPrompt,\n type ConfirmQueue,\n type JourneyRegistry,\n type JsonSchema,\n type PageBridgeStatus,\n type ToolAnnotations,\n type ToolExecuteCallback,\n type ToolExecuteCallbackOptions,\n} from \"@thegreataxios/webmcp-core\";\n\nconst WEBMCP_CONTEXT_KEY = Symbol(\"webmcp-svelte-context\");\nconst WEBMCP_CONFIRM_KEY = Symbol(\"webmcp-svelte-confirm\");\n\nexport interface WebMCPSvelteContext {\n readonly available: boolean;\n readonly native: boolean;\n readonly appName: string;\n readonly appVersion?: string;\n readonly journeyRegistry: JourneyRegistry;\n destroy(): void;\n}\n\nexport interface CreateWebMCPProviderOptions {\n name: string;\n version?: string;\n}\n\nfunction tryOnDestroy(fn: () => void): void {\n try {\n onDestroy(fn);\n } catch {\n // Called outside a Svelte component initialization lifecycle (e.g. unit tests).\n }\n}\n\nfunction trySetContext<T>(key: symbol, value: T): void {\n try {\n setContext(key, value);\n } catch {\n // Outside component init.\n }\n}\n\nfunction tryGetContext<T>(key: symbol): T | undefined {\n try {\n return getContext<T>(key);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Initialize WebMCP in a Svelte root component (or standalone store) and register cleanup on destroy.\n */\nexport function createWebMCPProvider(options: CreateWebMCPProviderOptions): WebMCPSvelteContext {\n const journeyRegistry = experimental_createJourneyRegistry();\n setJourneyRegistry(journeyRegistry);\n installPolyfill();\n\n let destroyed = false;\n const destroy = () => {\n if (destroyed) return;\n destroyed = true;\n setJourneyRegistry(null);\n cleanupPolyfill();\n };\n\n const ctx: WebMCPSvelteContext = {\n available: typeof document !== \"undefined\" && document.modelContext != null,\n native: isNativeModelContext(),\n appName: options.name,\n appVersion: options.version,\n journeyRegistry,\n destroy,\n };\n\n trySetContext(WEBMCP_CONTEXT_KEY, ctx);\n tryOnDestroy(destroy);\n\n return ctx;\n}\n\nexport function getWebMCP(): WebMCPSvelteContext | undefined {\n return tryGetContext<WebMCPSvelteContext>(WEBMCP_CONTEXT_KEY);\n}\n\nexport interface SvelteToolOptions {\n name: string;\n description: string;\n title?: string;\n inputSchema?: JsonSchema;\n outputSchema?: JsonSchema;\n annotations?: ToolAnnotations;\n exposedTo?: string[];\n handler: ToolExecuteCallback;\n}\n\n/**\n * Register a WebMCP tool for the lifetime of the current Svelte component (or until `dispose()` is called).\n */\nexport function useWebMCPTool(options: SvelteToolOptions): { dispose: () => void } {\n const controller = new AbortController();\n let disposed = false;\n\n const dispose = () => {\n if (disposed) return;\n disposed = true;\n controller.abort();\n };\n\n if (typeof document !== \"undefined\" && document.modelContext) {\n document.modelContext\n .registerTool(\n {\n name: options.name,\n title: options.title,\n description: options.description,\n inputSchema: options.inputSchema,\n outputSchema: options.outputSchema,\n annotations: options.annotations,\n execute: options.handler,\n },\n { signal: controller.signal, exposedTo: options.exposedTo },\n )\n .catch(() => {});\n }\n\n tryOnDestroy(dispose);\n return { dispose };\n}\n\n/**\n * Signal async tool registration stability to `document.modelContext.waitForToolsUntil`.\n */\nexport function useWaitForToolsUntil(promise: Promise<unknown>): void {\n if (typeof document !== \"undefined\") {\n document.modelContext?.waitForToolsUntil?.(promise);\n }\n}\n\nexport interface SvelteSyncTool<TState> {\n description: string;\n title?: string;\n inputSchema?: JsonSchema;\n outputSchema?: JsonSchema;\n annotations?: ToolAnnotations;\n reducer: (\n state: TState,\n args: Record<string, unknown>,\n options?: ToolExecuteCallbackOptions,\n ) => TState | Promise<TState>;\n}\n\n/**\n * Bind a Svelte writable store to one or more WebMCP tools whose reducers update reactive state.\n */\nexport function experimental_useWebMCPSync<TState>(options: {\n initial: TState;\n tools: Record<string, SvelteSyncTool<TState>>;\n}): {\n state: Readable<TState>;\n set: (next: TState) => void;\n update: (fn: (prev: TState) => TState) => void;\n dispose: () => void;\n} {\n const controller = experimental_createSyncStateController(options.initial);\n const store = writable<TState>(controller.getState());\n const unsub = controller.subscribe((next) => store.set(next));\n\n const handles = Object.entries(options.tools).map(([name, def]) =>\n useWebMCPTool({\n name,\n title: def.title,\n description: def.description,\n inputSchema: def.inputSchema,\n outputSchema: def.outputSchema,\n annotations: def.annotations,\n handler: (args, cbOptions) => controller.enqueueMutation(def.reducer, args, cbOptions),\n }),\n );\n\n const dispose = () => {\n unsub();\n for (const h of handles) h.dispose();\n };\n tryOnDestroy(dispose);\n\n return {\n state: { subscribe: store.subscribe },\n set: controller.setState,\n update: controller.updateState,\n dispose,\n };\n}\n\n/**\n * Register and control a journey from Svelte, auto-cleaning up on destroy.\n */\nexport function experimental_useWebMCPJourney(options: {\n name: string;\n tools: string[];\n description?: string;\n active?: boolean;\n registry?: JourneyRegistry;\n}): {\n setActive: (active: boolean) => void;\n dispose: () => void;\n} {\n const registry = options.registry ?? getWebMCP()?.journeyRegistry;\n registry?.register({\n name: options.name,\n description: options.description,\n tools: options.tools,\n });\n if (options.active) {\n registry?.setJourneyActive(options.name, true);\n }\n\n const dispose = () => {\n registry?.unregister(options.name);\n };\n tryOnDestroy(dispose);\n\n return {\n setActive: (active: boolean) => registry?.setJourneyActive(options.name, active),\n dispose,\n };\n}\n\n/**\n * Connect a Svelte app to `webmcp-bridge` and expose reactive connection status.\n */\nexport function experimental_useWebMCPBridge(options: {\n url?: string;\n token: string;\n}): {\n status: Readable<PageBridgeStatus>;\n disconnect: () => void;\n} {\n const statusStore = writable<PageBridgeStatus>(\"disconnected\");\n const client = createPageBridgeClient({\n url: options.url ?? \"ws://127.0.0.1:17321/ws\",\n token: options.token,\n onStatusChange: (next) => statusStore.set(next),\n });\n client.connect();\n\n const disconnect = () => client.disconnect();\n tryOnDestroy(disconnect);\n\n return {\n status: { subscribe: statusStore.subscribe },\n disconnect,\n };\n}\n\n/**\n * Create a HITL confirmation queue for Svelte components and expose the active prompt as a store.\n */\nexport function experimental_createWebMCPConfirm(): {\n pending: Readable<ConfirmPrompt | null>;\n queue: ConfirmQueue;\n dispose: () => void;\n} {\n const queue = experimental_createConfirmQueue();\n const store = writable<ConfirmPrompt | null>(queue.getPending());\n const unsub = queue.subscribe((next) => store.set(next));\n\n const dispose = () => {\n unsub();\n queue.dispose();\n };\n\n trySetContext(WEBMCP_CONFIRM_KEY, queue);\n tryOnDestroy(dispose);\n\n return {\n pending: { subscribe: store.subscribe },\n queue,\n dispose,\n };\n}\n\nexport interface SvelteGuardedToolOptions extends SvelteToolOptions {\n requiresConfirm?: boolean;\n confirmQueue?: ConfirmQueue;\n}\n\n/**\n * Register a WebMCP tool guarded by a HITL confirmation queue (fails closed if none is provided).\n */\nexport function experimental_useWebMCPGuardedTool(\n options: SvelteGuardedToolOptions,\n): { dispose: () => void } {\n const queue = options.confirmQueue ?? tryGetContext<ConfirmQueue>(WEBMCP_CONFIRM_KEY);\n return useWebMCPTool({\n ...options,\n handler: experimental_wrapGuardedHandler({\n name: options.name,\n requiresConfirm: options.requiresConfirm,\n queue,\n handler: options.handler,\n }),\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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;AAAA,MACvC,MAAM,QAAQ;AAAA,MACd,iBAAiB,QAAQ;AAAA,MACzB;AAAA,MACA,SAAS,QAAQ;AAAA,IACnB,CAAC;AAAA,EACH,CAAC;AACH;","names":[]}
@@ -0,0 +1,103 @@
1
+ import { Readable } from 'svelte/store';
2
+ import { JsonSchema, ToolAnnotations, ToolExecuteCallback, ConfirmQueue, ToolExecuteCallbackOptions, JourneyRegistry, ConfirmPrompt, PageBridgeStatus } from '@thegreataxios/webmcp-core';
3
+
4
+ interface WebMCPSvelteContext {
5
+ readonly available: boolean;
6
+ readonly native: boolean;
7
+ readonly appName: string;
8
+ readonly appVersion?: string;
9
+ readonly journeyRegistry: JourneyRegistry;
10
+ destroy(): void;
11
+ }
12
+ interface CreateWebMCPProviderOptions {
13
+ name: string;
14
+ version?: string;
15
+ }
16
+ /**
17
+ * Initialize WebMCP in a Svelte root component (or standalone store) and register cleanup on destroy.
18
+ */
19
+ declare function createWebMCPProvider(options: CreateWebMCPProviderOptions): WebMCPSvelteContext;
20
+ declare function getWebMCP(): WebMCPSvelteContext | undefined;
21
+ interface SvelteToolOptions {
22
+ name: string;
23
+ description: string;
24
+ title?: string;
25
+ inputSchema?: JsonSchema;
26
+ outputSchema?: JsonSchema;
27
+ annotations?: ToolAnnotations;
28
+ exposedTo?: string[];
29
+ handler: ToolExecuteCallback;
30
+ }
31
+ /**
32
+ * Register a WebMCP tool for the lifetime of the current Svelte component (or until `dispose()` is called).
33
+ */
34
+ declare function useWebMCPTool(options: SvelteToolOptions): {
35
+ dispose: () => void;
36
+ };
37
+ /**
38
+ * Signal async tool registration stability to `document.modelContext.waitForToolsUntil`.
39
+ */
40
+ declare function useWaitForToolsUntil(promise: Promise<unknown>): void;
41
+ interface SvelteSyncTool<TState> {
42
+ description: string;
43
+ title?: string;
44
+ inputSchema?: JsonSchema;
45
+ outputSchema?: JsonSchema;
46
+ annotations?: ToolAnnotations;
47
+ reducer: (state: TState, args: Record<string, unknown>, options?: ToolExecuteCallbackOptions) => TState | Promise<TState>;
48
+ }
49
+ /**
50
+ * Bind a Svelte writable store to one or more WebMCP tools whose reducers update reactive state.
51
+ */
52
+ declare function experimental_useWebMCPSync<TState>(options: {
53
+ initial: TState;
54
+ tools: Record<string, SvelteSyncTool<TState>>;
55
+ }): {
56
+ state: Readable<TState>;
57
+ set: (next: TState) => void;
58
+ update: (fn: (prev: TState) => TState) => void;
59
+ dispose: () => void;
60
+ };
61
+ /**
62
+ * Register and control a journey from Svelte, auto-cleaning up on destroy.
63
+ */
64
+ declare function experimental_useWebMCPJourney(options: {
65
+ name: string;
66
+ tools: string[];
67
+ description?: string;
68
+ active?: boolean;
69
+ registry?: JourneyRegistry;
70
+ }): {
71
+ setActive: (active: boolean) => void;
72
+ dispose: () => void;
73
+ };
74
+ /**
75
+ * Connect a Svelte app to `webmcp-bridge` and expose reactive connection status.
76
+ */
77
+ declare function experimental_useWebMCPBridge(options: {
78
+ url?: string;
79
+ token: string;
80
+ }): {
81
+ status: Readable<PageBridgeStatus>;
82
+ disconnect: () => void;
83
+ };
84
+ /**
85
+ * Create a HITL confirmation queue for Svelte components and expose the active prompt as a store.
86
+ */
87
+ declare function experimental_createWebMCPConfirm(): {
88
+ pending: Readable<ConfirmPrompt | null>;
89
+ queue: ConfirmQueue;
90
+ dispose: () => void;
91
+ };
92
+ interface SvelteGuardedToolOptions extends SvelteToolOptions {
93
+ requiresConfirm?: boolean;
94
+ confirmQueue?: ConfirmQueue;
95
+ }
96
+ /**
97
+ * Register a WebMCP tool guarded by a HITL confirmation queue (fails closed if none is provided).
98
+ */
99
+ declare function experimental_useWebMCPGuardedTool(options: SvelteGuardedToolOptions): {
100
+ dispose: () => void;
101
+ };
102
+
103
+ export { type CreateWebMCPProviderOptions, type SvelteGuardedToolOptions, type SvelteSyncTool, type SvelteToolOptions, type WebMCPSvelteContext, createWebMCPProvider, experimental_createWebMCPConfirm, experimental_useWebMCPBridge, experimental_useWebMCPGuardedTool, experimental_useWebMCPJourney, experimental_useWebMCPSync, getWebMCP, useWaitForToolsUntil, useWebMCPTool };
@@ -0,0 +1,103 @@
1
+ import { Readable } from 'svelte/store';
2
+ import { JsonSchema, ToolAnnotations, ToolExecuteCallback, ConfirmQueue, ToolExecuteCallbackOptions, JourneyRegistry, ConfirmPrompt, PageBridgeStatus } from '@thegreataxios/webmcp-core';
3
+
4
+ interface WebMCPSvelteContext {
5
+ readonly available: boolean;
6
+ readonly native: boolean;
7
+ readonly appName: string;
8
+ readonly appVersion?: string;
9
+ readonly journeyRegistry: JourneyRegistry;
10
+ destroy(): void;
11
+ }
12
+ interface CreateWebMCPProviderOptions {
13
+ name: string;
14
+ version?: string;
15
+ }
16
+ /**
17
+ * Initialize WebMCP in a Svelte root component (or standalone store) and register cleanup on destroy.
18
+ */
19
+ declare function createWebMCPProvider(options: CreateWebMCPProviderOptions): WebMCPSvelteContext;
20
+ declare function getWebMCP(): WebMCPSvelteContext | undefined;
21
+ interface SvelteToolOptions {
22
+ name: string;
23
+ description: string;
24
+ title?: string;
25
+ inputSchema?: JsonSchema;
26
+ outputSchema?: JsonSchema;
27
+ annotations?: ToolAnnotations;
28
+ exposedTo?: string[];
29
+ handler: ToolExecuteCallback;
30
+ }
31
+ /**
32
+ * Register a WebMCP tool for the lifetime of the current Svelte component (or until `dispose()` is called).
33
+ */
34
+ declare function useWebMCPTool(options: SvelteToolOptions): {
35
+ dispose: () => void;
36
+ };
37
+ /**
38
+ * Signal async tool registration stability to `document.modelContext.waitForToolsUntil`.
39
+ */
40
+ declare function useWaitForToolsUntil(promise: Promise<unknown>): void;
41
+ interface SvelteSyncTool<TState> {
42
+ description: string;
43
+ title?: string;
44
+ inputSchema?: JsonSchema;
45
+ outputSchema?: JsonSchema;
46
+ annotations?: ToolAnnotations;
47
+ reducer: (state: TState, args: Record<string, unknown>, options?: ToolExecuteCallbackOptions) => TState | Promise<TState>;
48
+ }
49
+ /**
50
+ * Bind a Svelte writable store to one or more WebMCP tools whose reducers update reactive state.
51
+ */
52
+ declare function experimental_useWebMCPSync<TState>(options: {
53
+ initial: TState;
54
+ tools: Record<string, SvelteSyncTool<TState>>;
55
+ }): {
56
+ state: Readable<TState>;
57
+ set: (next: TState) => void;
58
+ update: (fn: (prev: TState) => TState) => void;
59
+ dispose: () => void;
60
+ };
61
+ /**
62
+ * Register and control a journey from Svelte, auto-cleaning up on destroy.
63
+ */
64
+ declare function experimental_useWebMCPJourney(options: {
65
+ name: string;
66
+ tools: string[];
67
+ description?: string;
68
+ active?: boolean;
69
+ registry?: JourneyRegistry;
70
+ }): {
71
+ setActive: (active: boolean) => void;
72
+ dispose: () => void;
73
+ };
74
+ /**
75
+ * Connect a Svelte app to `webmcp-bridge` and expose reactive connection status.
76
+ */
77
+ declare function experimental_useWebMCPBridge(options: {
78
+ url?: string;
79
+ token: string;
80
+ }): {
81
+ status: Readable<PageBridgeStatus>;
82
+ disconnect: () => void;
83
+ };
84
+ /**
85
+ * Create a HITL confirmation queue for Svelte components and expose the active prompt as a store.
86
+ */
87
+ declare function experimental_createWebMCPConfirm(): {
88
+ pending: Readable<ConfirmPrompt | null>;
89
+ queue: ConfirmQueue;
90
+ dispose: () => void;
91
+ };
92
+ interface SvelteGuardedToolOptions extends SvelteToolOptions {
93
+ requiresConfirm?: boolean;
94
+ confirmQueue?: ConfirmQueue;
95
+ }
96
+ /**
97
+ * Register a WebMCP tool guarded by a HITL confirmation queue (fails closed if none is provided).
98
+ */
99
+ declare function experimental_useWebMCPGuardedTool(options: SvelteGuardedToolOptions): {
100
+ dispose: () => void;
101
+ };
102
+
103
+ export { type CreateWebMCPProviderOptions, type SvelteGuardedToolOptions, type SvelteSyncTool, type SvelteToolOptions, type WebMCPSvelteContext, createWebMCPProvider, experimental_createWebMCPConfirm, experimental_useWebMCPBridge, experimental_useWebMCPGuardedTool, experimental_useWebMCPJourney, experimental_useWebMCPSync, getWebMCP, useWaitForToolsUntil, useWebMCPTool };
package/dist/index.js ADDED
@@ -0,0 +1,193 @@
1
+ // src/index.ts
2
+ import { getContext, onDestroy, setContext } from "svelte";
3
+ import { writable } from "svelte/store";
4
+ import {
5
+ cleanupPolyfill,
6
+ createPageBridgeClient,
7
+ experimental_createConfirmQueue,
8
+ experimental_createJourneyRegistry,
9
+ experimental_createSyncStateController,
10
+ experimental_wrapGuardedHandler,
11
+ installPolyfill,
12
+ isNativeModelContext,
13
+ setJourneyRegistry
14
+ } from "@thegreataxios/webmcp-core";
15
+ var WEBMCP_CONTEXT_KEY = /* @__PURE__ */ Symbol("webmcp-svelte-context");
16
+ var WEBMCP_CONFIRM_KEY = /* @__PURE__ */ Symbol("webmcp-svelte-confirm");
17
+ function tryOnDestroy(fn) {
18
+ try {
19
+ onDestroy(fn);
20
+ } catch {
21
+ }
22
+ }
23
+ function trySetContext(key, value) {
24
+ try {
25
+ setContext(key, value);
26
+ } catch {
27
+ }
28
+ }
29
+ function tryGetContext(key) {
30
+ try {
31
+ return getContext(key);
32
+ } catch {
33
+ return void 0;
34
+ }
35
+ }
36
+ function createWebMCPProvider(options) {
37
+ const journeyRegistry = experimental_createJourneyRegistry();
38
+ setJourneyRegistry(journeyRegistry);
39
+ installPolyfill();
40
+ let destroyed = false;
41
+ const destroy = () => {
42
+ if (destroyed) return;
43
+ destroyed = true;
44
+ setJourneyRegistry(null);
45
+ cleanupPolyfill();
46
+ };
47
+ const ctx = {
48
+ available: typeof document !== "undefined" && document.modelContext != null,
49
+ native: isNativeModelContext(),
50
+ appName: options.name,
51
+ appVersion: options.version,
52
+ journeyRegistry,
53
+ destroy
54
+ };
55
+ trySetContext(WEBMCP_CONTEXT_KEY, ctx);
56
+ tryOnDestroy(destroy);
57
+ return ctx;
58
+ }
59
+ function getWebMCP() {
60
+ return tryGetContext(WEBMCP_CONTEXT_KEY);
61
+ }
62
+ function useWebMCPTool(options) {
63
+ const controller = new AbortController();
64
+ let disposed = false;
65
+ const dispose = () => {
66
+ if (disposed) return;
67
+ disposed = true;
68
+ controller.abort();
69
+ };
70
+ if (typeof document !== "undefined" && document.modelContext) {
71
+ document.modelContext.registerTool(
72
+ {
73
+ name: options.name,
74
+ title: options.title,
75
+ description: options.description,
76
+ inputSchema: options.inputSchema,
77
+ outputSchema: options.outputSchema,
78
+ annotations: options.annotations,
79
+ execute: options.handler
80
+ },
81
+ { signal: controller.signal, exposedTo: options.exposedTo }
82
+ ).catch(() => {
83
+ });
84
+ }
85
+ tryOnDestroy(dispose);
86
+ return { dispose };
87
+ }
88
+ function useWaitForToolsUntil(promise) {
89
+ if (typeof document !== "undefined") {
90
+ document.modelContext?.waitForToolsUntil?.(promise);
91
+ }
92
+ }
93
+ function experimental_useWebMCPSync(options) {
94
+ const controller = experimental_createSyncStateController(options.initial);
95
+ const store = writable(controller.getState());
96
+ const unsub = controller.subscribe((next) => store.set(next));
97
+ const handles = Object.entries(options.tools).map(
98
+ ([name, def]) => useWebMCPTool({
99
+ name,
100
+ title: def.title,
101
+ description: def.description,
102
+ inputSchema: def.inputSchema,
103
+ outputSchema: def.outputSchema,
104
+ annotations: def.annotations,
105
+ handler: (args, cbOptions) => controller.enqueueMutation(def.reducer, args, cbOptions)
106
+ })
107
+ );
108
+ const dispose = () => {
109
+ unsub();
110
+ for (const h of handles) h.dispose();
111
+ };
112
+ tryOnDestroy(dispose);
113
+ return {
114
+ state: { subscribe: store.subscribe },
115
+ set: controller.setState,
116
+ update: controller.updateState,
117
+ dispose
118
+ };
119
+ }
120
+ function experimental_useWebMCPJourney(options) {
121
+ const registry = options.registry ?? getWebMCP()?.journeyRegistry;
122
+ registry?.register({
123
+ name: options.name,
124
+ description: options.description,
125
+ tools: options.tools
126
+ });
127
+ if (options.active) {
128
+ registry?.setJourneyActive(options.name, true);
129
+ }
130
+ const dispose = () => {
131
+ registry?.unregister(options.name);
132
+ };
133
+ tryOnDestroy(dispose);
134
+ return {
135
+ setActive: (active) => registry?.setJourneyActive(options.name, active),
136
+ dispose
137
+ };
138
+ }
139
+ function experimental_useWebMCPBridge(options) {
140
+ const statusStore = writable("disconnected");
141
+ const client = createPageBridgeClient({
142
+ url: options.url ?? "ws://127.0.0.1:17321/ws",
143
+ token: options.token,
144
+ onStatusChange: (next) => statusStore.set(next)
145
+ });
146
+ client.connect();
147
+ const disconnect = () => client.disconnect();
148
+ tryOnDestroy(disconnect);
149
+ return {
150
+ status: { subscribe: statusStore.subscribe },
151
+ disconnect
152
+ };
153
+ }
154
+ function experimental_createWebMCPConfirm() {
155
+ const queue = experimental_createConfirmQueue();
156
+ const store = writable(queue.getPending());
157
+ const unsub = queue.subscribe((next) => store.set(next));
158
+ const dispose = () => {
159
+ unsub();
160
+ queue.dispose();
161
+ };
162
+ trySetContext(WEBMCP_CONFIRM_KEY, queue);
163
+ tryOnDestroy(dispose);
164
+ return {
165
+ pending: { subscribe: store.subscribe },
166
+ queue,
167
+ dispose
168
+ };
169
+ }
170
+ function experimental_useWebMCPGuardedTool(options) {
171
+ const queue = options.confirmQueue ?? tryGetContext(WEBMCP_CONFIRM_KEY);
172
+ return useWebMCPTool({
173
+ ...options,
174
+ handler: experimental_wrapGuardedHandler({
175
+ name: options.name,
176
+ requiresConfirm: options.requiresConfirm,
177
+ queue,
178
+ handler: options.handler
179
+ })
180
+ });
181
+ }
182
+ export {
183
+ createWebMCPProvider,
184
+ experimental_createWebMCPConfirm,
185
+ experimental_useWebMCPBridge,
186
+ experimental_useWebMCPGuardedTool,
187
+ experimental_useWebMCPJourney,
188
+ experimental_useWebMCPSync,
189
+ getWebMCP,
190
+ useWaitForToolsUntil,
191
+ useWebMCPTool
192
+ };
193
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { getContext, onDestroy, setContext } from \"svelte\";\nimport { writable, type Readable } from \"svelte/store\";\nimport {\n cleanupPolyfill,\n createPageBridgeClient,\n experimental_createConfirmQueue,\n experimental_createJourneyRegistry,\n experimental_createSyncStateController,\n experimental_wrapGuardedHandler,\n installPolyfill,\n isNativeModelContext,\n setJourneyRegistry,\n type ConfirmPrompt,\n type ConfirmQueue,\n type JourneyRegistry,\n type JsonSchema,\n type PageBridgeStatus,\n type ToolAnnotations,\n type ToolExecuteCallback,\n type ToolExecuteCallbackOptions,\n} from \"@thegreataxios/webmcp-core\";\n\nconst WEBMCP_CONTEXT_KEY = Symbol(\"webmcp-svelte-context\");\nconst WEBMCP_CONFIRM_KEY = Symbol(\"webmcp-svelte-confirm\");\n\nexport interface WebMCPSvelteContext {\n readonly available: boolean;\n readonly native: boolean;\n readonly appName: string;\n readonly appVersion?: string;\n readonly journeyRegistry: JourneyRegistry;\n destroy(): void;\n}\n\nexport interface CreateWebMCPProviderOptions {\n name: string;\n version?: string;\n}\n\nfunction tryOnDestroy(fn: () => void): void {\n try {\n onDestroy(fn);\n } catch {\n // Called outside a Svelte component initialization lifecycle (e.g. unit tests).\n }\n}\n\nfunction trySetContext<T>(key: symbol, value: T): void {\n try {\n setContext(key, value);\n } catch {\n // Outside component init.\n }\n}\n\nfunction tryGetContext<T>(key: symbol): T | undefined {\n try {\n return getContext<T>(key);\n } catch {\n return undefined;\n }\n}\n\n/**\n * Initialize WebMCP in a Svelte root component (or standalone store) and register cleanup on destroy.\n */\nexport function createWebMCPProvider(options: CreateWebMCPProviderOptions): WebMCPSvelteContext {\n const journeyRegistry = experimental_createJourneyRegistry();\n setJourneyRegistry(journeyRegistry);\n installPolyfill();\n\n let destroyed = false;\n const destroy = () => {\n if (destroyed) return;\n destroyed = true;\n setJourneyRegistry(null);\n cleanupPolyfill();\n };\n\n const ctx: WebMCPSvelteContext = {\n available: typeof document !== \"undefined\" && document.modelContext != null,\n native: isNativeModelContext(),\n appName: options.name,\n appVersion: options.version,\n journeyRegistry,\n destroy,\n };\n\n trySetContext(WEBMCP_CONTEXT_KEY, ctx);\n tryOnDestroy(destroy);\n\n return ctx;\n}\n\nexport function getWebMCP(): WebMCPSvelteContext | undefined {\n return tryGetContext<WebMCPSvelteContext>(WEBMCP_CONTEXT_KEY);\n}\n\nexport interface SvelteToolOptions {\n name: string;\n description: string;\n title?: string;\n inputSchema?: JsonSchema;\n outputSchema?: JsonSchema;\n annotations?: ToolAnnotations;\n exposedTo?: string[];\n handler: ToolExecuteCallback;\n}\n\n/**\n * Register a WebMCP tool for the lifetime of the current Svelte component (or until `dispose()` is called).\n */\nexport function useWebMCPTool(options: SvelteToolOptions): { dispose: () => void } {\n const controller = new AbortController();\n let disposed = false;\n\n const dispose = () => {\n if (disposed) return;\n disposed = true;\n controller.abort();\n };\n\n if (typeof document !== \"undefined\" && document.modelContext) {\n document.modelContext\n .registerTool(\n {\n name: options.name,\n title: options.title,\n description: options.description,\n inputSchema: options.inputSchema,\n outputSchema: options.outputSchema,\n annotations: options.annotations,\n execute: options.handler,\n },\n { signal: controller.signal, exposedTo: options.exposedTo },\n )\n .catch(() => {});\n }\n\n tryOnDestroy(dispose);\n return { dispose };\n}\n\n/**\n * Signal async tool registration stability to `document.modelContext.waitForToolsUntil`.\n */\nexport function useWaitForToolsUntil(promise: Promise<unknown>): void {\n if (typeof document !== \"undefined\") {\n document.modelContext?.waitForToolsUntil?.(promise);\n }\n}\n\nexport interface SvelteSyncTool<TState> {\n description: string;\n title?: string;\n inputSchema?: JsonSchema;\n outputSchema?: JsonSchema;\n annotations?: ToolAnnotations;\n reducer: (\n state: TState,\n args: Record<string, unknown>,\n options?: ToolExecuteCallbackOptions,\n ) => TState | Promise<TState>;\n}\n\n/**\n * Bind a Svelte writable store to one or more WebMCP tools whose reducers update reactive state.\n */\nexport function experimental_useWebMCPSync<TState>(options: {\n initial: TState;\n tools: Record<string, SvelteSyncTool<TState>>;\n}): {\n state: Readable<TState>;\n set: (next: TState) => void;\n update: (fn: (prev: TState) => TState) => void;\n dispose: () => void;\n} {\n const controller = experimental_createSyncStateController(options.initial);\n const store = writable<TState>(controller.getState());\n const unsub = controller.subscribe((next) => store.set(next));\n\n const handles = Object.entries(options.tools).map(([name, def]) =>\n useWebMCPTool({\n name,\n title: def.title,\n description: def.description,\n inputSchema: def.inputSchema,\n outputSchema: def.outputSchema,\n annotations: def.annotations,\n handler: (args, cbOptions) => controller.enqueueMutation(def.reducer, args, cbOptions),\n }),\n );\n\n const dispose = () => {\n unsub();\n for (const h of handles) h.dispose();\n };\n tryOnDestroy(dispose);\n\n return {\n state: { subscribe: store.subscribe },\n set: controller.setState,\n update: controller.updateState,\n dispose,\n };\n}\n\n/**\n * Register and control a journey from Svelte, auto-cleaning up on destroy.\n */\nexport function experimental_useWebMCPJourney(options: {\n name: string;\n tools: string[];\n description?: string;\n active?: boolean;\n registry?: JourneyRegistry;\n}): {\n setActive: (active: boolean) => void;\n dispose: () => void;\n} {\n const registry = options.registry ?? getWebMCP()?.journeyRegistry;\n registry?.register({\n name: options.name,\n description: options.description,\n tools: options.tools,\n });\n if (options.active) {\n registry?.setJourneyActive(options.name, true);\n }\n\n const dispose = () => {\n registry?.unregister(options.name);\n };\n tryOnDestroy(dispose);\n\n return {\n setActive: (active: boolean) => registry?.setJourneyActive(options.name, active),\n dispose,\n };\n}\n\n/**\n * Connect a Svelte app to `webmcp-bridge` and expose reactive connection status.\n */\nexport function experimental_useWebMCPBridge(options: {\n url?: string;\n token: string;\n}): {\n status: Readable<PageBridgeStatus>;\n disconnect: () => void;\n} {\n const statusStore = writable<PageBridgeStatus>(\"disconnected\");\n const client = createPageBridgeClient({\n url: options.url ?? \"ws://127.0.0.1:17321/ws\",\n token: options.token,\n onStatusChange: (next) => statusStore.set(next),\n });\n client.connect();\n\n const disconnect = () => client.disconnect();\n tryOnDestroy(disconnect);\n\n return {\n status: { subscribe: statusStore.subscribe },\n disconnect,\n };\n}\n\n/**\n * Create a HITL confirmation queue for Svelte components and expose the active prompt as a store.\n */\nexport function experimental_createWebMCPConfirm(): {\n pending: Readable<ConfirmPrompt | null>;\n queue: ConfirmQueue;\n dispose: () => void;\n} {\n const queue = experimental_createConfirmQueue();\n const store = writable<ConfirmPrompt | null>(queue.getPending());\n const unsub = queue.subscribe((next) => store.set(next));\n\n const dispose = () => {\n unsub();\n queue.dispose();\n };\n\n trySetContext(WEBMCP_CONFIRM_KEY, queue);\n tryOnDestroy(dispose);\n\n return {\n pending: { subscribe: store.subscribe },\n queue,\n dispose,\n };\n}\n\nexport interface SvelteGuardedToolOptions extends SvelteToolOptions {\n requiresConfirm?: boolean;\n confirmQueue?: ConfirmQueue;\n}\n\n/**\n * Register a WebMCP tool guarded by a HITL confirmation queue (fails closed if none is provided).\n */\nexport function experimental_useWebMCPGuardedTool(\n options: SvelteGuardedToolOptions,\n): { dispose: () => void } {\n const queue = options.confirmQueue ?? tryGetContext<ConfirmQueue>(WEBMCP_CONFIRM_KEY);\n return useWebMCPTool({\n ...options,\n handler: experimental_wrapGuardedHandler({\n name: options.name,\n requiresConfirm: options.requiresConfirm,\n queue,\n handler: options.handler,\n }),\n });\n}\n"],"mappings":";AAAA,SAAS,YAAY,WAAW,kBAAkB;AAClD,SAAS,gBAA+B;AACxC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OASK;AAEP,IAAM,qBAAqB,uBAAO,uBAAuB;AACzD,IAAM,qBAAqB,uBAAO,uBAAuB;AAgBzD,SAAS,aAAa,IAAsB;AAC1C,MAAI;AACF,cAAU,EAAE;AAAA,EACd,QAAQ;AAAA,EAER;AACF;AAEA,SAAS,cAAiB,KAAa,OAAgB;AACrD,MAAI;AACF,eAAW,KAAK,KAAK;AAAA,EACvB,QAAQ;AAAA,EAER;AACF;AAEA,SAAS,cAAiB,KAA4B;AACpD,MAAI;AACF,WAAO,WAAc,GAAG;AAAA,EAC1B,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAKO,SAAS,qBAAqB,SAA2D;AAC9F,QAAM,kBAAkB,mCAAmC;AAC3D,qBAAmB,eAAe;AAClC,kBAAgB;AAEhB,MAAI,YAAY;AAChB,QAAM,UAAU,MAAM;AACpB,QAAI,UAAW;AACf,gBAAY;AACZ,uBAAmB,IAAI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,OAAO,WAAW;AAC3C,eAAa,UAAU;AAEvB,SAAO;AAAA,IACL,QAAQ,EAAE,WAAW,YAAY,UAAU;AAAA,IAC3C;AAAA,EACF;AACF;AAKO,SAAS,mCAId;AACA,QAAM,QAAQ,gCAAgC;AAC9C,QAAM,QAAQ,SAA+B,MAAM,WAAW,CAAC;AAC/D,QAAM,QAAQ,MAAM,UAAU,CAAC,SAAS,MAAM,IAAI,IAAI,CAAC;AAEvD,QAAM,UAAU,MAAM;AACpB,UAAM;AACN,UAAM,QAAQ;AAAA,EAChB;AAEA,gBAAc,oBAAoB,KAAK;AACvC,eAAa,OAAO;AAEpB,SAAO;AAAA,IACL,SAAS,EAAE,WAAW,MAAM,UAAU;AAAA,IACtC;AAAA,IACA;AAAA,EACF;AACF;AAUO,SAAS,kCACd,SACyB;AACzB,QAAM,QAAQ,QAAQ,gBAAgB,cAA4B,kBAAkB;AACpF,SAAO,cAAc;AAAA,IACnB,GAAG;AAAA,IACH,SAAS,gCAAgC;AAAA,MACvC,MAAM,QAAQ;AAAA,MACd,iBAAiB,QAAQ;AAAA,MACzB;AAAA,MACA,SAAS,QAAQ;AAAA,IACnB,CAAC;AAAA,EACH,CAAC;AACH;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
2
  "name": "@thegreataxios/webmcp-svelte",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.1",
4
+ "description": "Svelte bindings for W3C WebMCP — context provider, tool registration, journeys, state sync, and bridge",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "LICENSE"
19
+ ],
20
+ "sideEffects": false,
21
+ "scripts": {
22
+ "build": "tsup",
23
+ "typecheck": "tsc --noEmit",
24
+ "test": "bun test"
25
+ },
26
+ "keywords": [
27
+ "webmcp",
28
+ "svelte",
29
+ "svelte5",
30
+ "document.modelContext"
31
+ ],
32
+ "license": "MIT",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/TheGreatAxios/webmcp"
36
+ },
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "peerDependencies": {
41
+ "svelte": ">=4.0.0"
42
+ },
43
+ "dependencies": {
44
+ "@thegreataxios/webmcp-core": "0.2.1"
45
+ },
46
+ "devDependencies": {
47
+ "@happy-dom/global-registrator": "^20.0.0",
48
+ "svelte": "^5.0.0",
49
+ "typescript": "^5.9.3"
50
+ }
51
+ }