|
1 | | -# Svelte library |
| 1 | +# sveltekit-webmcp-forms |
2 | 2 |
|
3 | | -Everything you need to build a Svelte library, powered by [`sv`](https://npmjs.com/package/sv). |
| 3 | +Expose SvelteKit form actions as [WebMCP](https://docs.google.com/document/d/1rtU1fRPS0bMqd9abMG_hc6K9OAI6soUy3Kh00toAgyk/) tools with zero boilerplate. |
4 | 4 |
|
5 | | -Read more about creating a library [in the docs](https://svelte.dev/docs/kit/packaging). |
| 5 | +[WebMCP](https://docs.google.com/document/d/1rtU1fRPS0bMqd9abMG_hc6K9OAI6soUy3Kh00toAgyk/) is a living web standard that lets AI agents interact with websites through structured tools instead of screen-scraping. Pages register tools via `navigator.modelContext`, and agents discover and invoke them with typed inputs and outputs. The spec is available in Chrome 146+ behind the "WebMCP for testing" flag. |
6 | 6 |
|
7 | | -## Creating a project |
| 7 | +This library bridges WebMCP's imperative API with SvelteKit's form actions: define a tool, point it at an action, and the library handles validation, user confirmation, submission, and registration. |
8 | 8 |
|
9 | | -If you're seeing this, you've probably already done this step. Congrats! |
| 9 | +## Install |
10 | 10 |
|
11 | 11 | ```sh |
12 | | -# create a new project in the current directory |
13 | | -npx sv create |
14 | | - |
15 | | -# create a new project in my-app |
16 | | -npx sv create my-app |
17 | | -``` |
18 | | - |
19 | | -To recreate this project with the same configuration: |
20 | | - |
21 | | -```sh |
22 | | -# recreate this project |
23 | | -npx sv create --template library --types ts --add vitest="usages:unit,component" mcp="ide:claude-code,opencode,cursor+setup:remote" --install npm sveltekit-webmcp-forms |
| 12 | +npm install sveltekit-webmcp-forms |
24 | 13 | ``` |
25 | 14 |
|
26 | | -## Developing |
27 | | - |
28 | | -Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server: |
| 15 | +## Quick start |
29 | 16 |
|
30 | | -```sh |
31 | | -npm run dev |
| 17 | +Define a SvelteKit form action as usual: |
32 | 18 |
|
33 | | -# or start the server and open the app in a new browser tab |
34 | | -npm run dev -- --open |
| 19 | +```ts |
| 20 | +// src/routes/demo/+page.server.ts |
| 21 | +export const actions = { |
| 22 | + subscribe: async ({ request }) => { |
| 23 | + const data = Object.fromEntries(await request.formData()); |
| 24 | + // ... handle subscription |
| 25 | + return { success: true }; |
| 26 | + }, |
| 27 | +}; |
35 | 28 | ``` |
36 | 29 |
|
37 | | -Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app. |
38 | | - |
39 | | -## Building |
40 | | - |
41 | | -To build your library: |
42 | | - |
43 | | -```sh |
44 | | -npm pack |
| 30 | +Wire it up as a WebMCP tool and register it with `<McpTools>`: |
| 31 | + |
| 32 | +```svelte |
| 33 | +<!-- src/routes/demo/+page.svelte --> |
| 34 | +<script lang="ts"> |
| 35 | + import { McpTools, defineKitMcpActionTool } from "sveltekit-webmcp-forms"; |
| 36 | +
|
| 37 | + const subscribeTool = defineKitMcpActionTool({ |
| 38 | + name: "subscribe", |
| 39 | + description: "Subscribe an email address to the newsletter", |
| 40 | + action: "/demo?/subscribe", |
| 41 | + inputSchema: { |
| 42 | + type: "object", |
| 43 | + properties: { |
| 44 | + email: { type: "string", description: "Email address" }, |
| 45 | + }, |
| 46 | + required: ["email"], |
| 47 | + }, |
| 48 | + }); |
| 49 | +</script> |
| 50 | +
|
| 51 | +<McpTools tools={[subscribeTool]} /> |
45 | 52 | ``` |
46 | 53 |
|
47 | | -To create a production version of your showcase app: |
48 | | - |
49 | | -```sh |
50 | | -npm run build |
51 | | -``` |
| 54 | +An AI agent visiting this page will discover the `subscribe` tool via `navigator.modelContext` and can invoke it directly - no DOM scraping needed. |
52 | 55 |
|
53 | | -You can preview the production build with `npm run preview`. |
| 56 | +## Features |
54 | 57 |
|
55 | | -> To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment. |
| 58 | +- **`defineKitMcpActionTool`** - factory that creates a WebMCP tool backed by a SvelteKit form action, with optional validation, input normalization, and user confirmation |
| 59 | +- **`<McpTools>`** - Svelte component that registers/unregisters tools on the page's model context |
| 60 | +- **Validation** - plug in any schema validator (a default one is included) to validate agent inputs before submission |
| 61 | +- **Confirmation** - built-in `"always"` / `"never"` policies or a custom function to gate tool execution behind user consent |
| 62 | +- **Coercion helpers** - `coerceNumber`, `coerceBoolean`, `withDefaults`, `pipe` for transforming raw agent inputs into the types your action expects |
56 | 63 |
|
57 | | -## Publishing |
| 64 | +## API |
58 | 65 |
|
59 | | -Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)). |
| 66 | +| Export | Description | |
| 67 | +| -------------------------- | -------------------------------------------------------- | |
| 68 | +| `defineKitMcpActionTool()` | Create a WebMCP tool that submits to a SvelteKit action | |
| 69 | +| `<McpTools>` | Register tools on `navigator.modelContext` | |
| 70 | +| `submitToKitAction()` | Low-level: POST FormData to a SvelteKit action endpoint | |
| 71 | +| `getModelContext()` | Access `navigator.modelContext` (returns `null` if unavailable) | |
| 72 | +| `isWebMcpAvailable()` | Check if the browser supports WebMCP | |
| 73 | +| `defaultValidate()` | Built-in schema validation function | |
| 74 | +| `coerceNumber()` | Coerce string input to number | |
| 75 | +| `coerceBoolean()` | Coerce string input to boolean | |
| 76 | +| `withDefaults()` | Apply default values to missing fields | |
| 77 | +| `pipe()` | Compose multiple normalization functions | |
60 | 78 |
|
61 | | -To publish your library to [npm](https://www.npmjs.com): |
| 79 | +## License |
62 | 80 |
|
63 | | -```sh |
64 | | -npm publish |
65 | | -``` |
| 81 | +[MIT](LICENSE) |
0 commit comments