Watch activity in the DevTools console!
+ +Server Time: Loading...
diff --git a/.prettierignore b/.prettierignore index 0de301e..e2a4cdb 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,3 +2,5 @@ examples/basic-host/**/*.ts examples/basic-host/**/*.tsx examples/basic-server-react/**/*.ts examples/basic-server-react/**/*.tsx +examples/basic-server-vanillajs/**/*.ts +examples/basic-server-vanillajs/**/*.tsx diff --git a/README.md b/README.md index 8137cce..d9e84b3 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,8 @@ Your `package.json` will then look like: ## Examples -- [`examples/basic-server-react`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-react) — Example MCP server with tools that return UI Apps +- [`examples/basic-server-react`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-react) — Example MCP server with tools that return UI Apps (React) +- [`examples/basic-server-vanillajs`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-vanillajs) — Example MCP server with tools that return UI Apps (vanilla JS) - [`examples/basic-host`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-host) — Bare-bones example of hosting MCP Apps To run the examples end-to-end: diff --git a/examples/basic-server-react/README.md b/examples/basic-server-react/README.md index 087904d..120f4ce 100644 --- a/examples/basic-server-react/README.md +++ b/examples/basic-server-react/README.md @@ -2,6 +2,9 @@ An MCP App example with a React UI. +> [!TIP] +> Looking for a vanilla JavaScript example? See [`basic-server-vanillajs`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-vanillajs)! + ## Overview - Tool registration with a linked UI resource diff --git a/examples/basic-server-vanillajs/README.md b/examples/basic-server-vanillajs/README.md new file mode 100644 index 0000000..8d22726 --- /dev/null +++ b/examples/basic-server-vanillajs/README.md @@ -0,0 +1,56 @@ +# Example: Basic Server (Vanilla JS) + +An MCP App example with a vanilla JavaScript UI (no framework). + +> [!TIP] +> Looking for a React-based example? See [`basic-server-react`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-react)! + +## Overview + +- Tool registration with a linked UI resource +- Vanilla JS UI using the [`App`](https://modelcontextprotocol.github.io/ext-apps/api/classes/app.App.html) class directly +- App communication APIs: [`callServerTool`](https://modelcontextprotocol.github.io/ext-apps/api/classes/app.App.html#callservertool), [`sendMessage`](https://modelcontextprotocol.github.io/ext-apps/api/classes/app.App.html#sendmessage), [`sendLog`](https://modelcontextprotocol.github.io/ext-apps/api/classes/app.App.html#sendlog), [`sendOpenLink`](https://modelcontextprotocol.github.io/ext-apps/api/classes/app.App.html#sendopenlink) + +## Key Files + +- [`server.ts`](server.ts) - MCP server with tool and resource registration +- [`mcp-app.html`](mcp-app.html) / [`src/mcp-app.ts`](src/mcp-app.ts) - Vanilla JS UI using `App` class + +## Getting Started + +```bash +npm install +npm run dev +``` + +## How It Works + +1. The server registers a `get-time` tool with metadata linking it to a UI HTML resource (`ui://get-time/mcp-app.html`). +2. When the tool is invoked, the Host renders the UI from the resource. +3. The UI uses the MCP App SDK API to communicate with the host and call server tools. + +## Vanilla JS Pattern + +```typescript +import { App, PostMessageTransport } from "@modelcontextprotocol/ext-apps"; + +// Get element references from static HTML +const button = document.getElementById("my-button")!; + +// Create app instance +const app = new App({ name: "My App", version: "1.0.0" }); + +// Register handlers BEFORE connecting +app.ontoolresult = (result) => { + /* handle result */ +}; +app.onerror = console.error; + +// Add event listeners +button.addEventListener("click", () => { + /* ... */ +}); + +// Connect to host +app.connect(new PostMessageTransport(window.parent)); +``` diff --git a/examples/basic-server-vanillajs/mcp-app.html b/examples/basic-server-vanillajs/mcp-app.html new file mode 100644 index 0000000..1a88d60 --- /dev/null +++ b/examples/basic-server-vanillajs/mcp-app.html @@ -0,0 +1,34 @@ + + +
+ + +Watch activity in the DevTools console!
+ +Server Time: Loading...