ext-apps/examples/basic-server-vanillajs
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Olivier Chafik 4c51eb0301
chore: release 0.3.1 (#224)
* chore: release 0.3.1
2026-01-09 18:22:27 +00:00
..
src feat: publish example servers to npm (#184) 2026-01-08 20:08:50 +00:00
mcp-app.html Add color-scheme meta tag to all example apps 2025-12-15 11:42:18 -08:00
package.json chore: release 0.3.1 (#224) 2026-01-09 18:22:27 +00:00
README.md refactor: rename request methods to remove send prefix (#161) 2025-12-16 22:07:06 +00:00
server-utils.ts feat: publish example servers to npm (#184) 2026-01-08 20:08:50 +00:00
server.ts feat: publish example servers to npm (#184) 2026-01-08 20:08:50 +00:00
tsconfig.json Add basic-server-vanillajs example 2025-12-04 14:28:27 -06:00
vite.config.ts Add basic-server-vanillajs example 2025-12-04 14:28:27 -06:00

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!

Overview

Key Files

Getting Started

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

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();