-
Notifications
You must be signed in to change notification settings - Fork 44
Add basic-server-* examples for Vue, Svelte, Preact, and Solid
#141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jonathanhefner
wants to merge
5
commits into
modelcontextprotocol:main
Choose a base branch
from
jonathanhefner:basic-examples-multi-framework
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1d00e60
Add basic-server-vue example
jonathanhefner b1705ea
Add basic-server-svelte example
jonathanhefner 6278542
Add basic-server-preact example
jonathanhefner e09b5e6
Add basic-server-solid example
jonathanhefner 7157f65
Add framework GitHub links to README and fix server name
jonathanhefner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,4 @@ | ||
| 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 | ||
| examples/basic-server-*/**/*.ts | ||
| examples/basic-server-*/**/*.tsx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Example: Basic Server (Preact) | ||
|
|
||
| An MCP App example with a Preact 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 | ||
| - Preact UI using the [`App`](https://modelcontextprotocol.github.io/ext-apps/api/classes/app.App.html) class | ||
| - 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.tsx`](src/mcp-app.tsx) - Preact 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Get Time App</title> | ||
| <link rel="stylesheet" href="/src/global.css"> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="/src/mcp-app.tsx"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| { | ||
| "name": "basic-server-preact", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "INPUT=mcp-app.html vite build", | ||
| "watch": "INPUT=mcp-app.html vite build --watch", | ||
| "serve": "bun server.ts", | ||
| "start": "NODE_ENV=development npm run build && npm run serve", | ||
| "dev": "NODE_ENV=development concurrently 'npm run watch' 'npm run serve'" | ||
| }, | ||
| "dependencies": { | ||
| "@modelcontextprotocol/ext-apps": "../..", | ||
| "@modelcontextprotocol/sdk": "^1.22.0", | ||
| "preact": "^10.0.0", | ||
| "zod": "^3.25.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@preact/preset-vite": "^2.0.0", | ||
| "@types/cors": "^2.8.19", | ||
| "@types/express": "^5.0.0", | ||
| "@types/node": "^22.0.0", | ||
| "bun": "^1.3.2", | ||
| "concurrently": "^9.2.1", | ||
| "cors": "^2.8.5", | ||
| "express": "^5.1.0", | ||
| "typescript": "^5.9.3", | ||
| "vite": "^6.0.0", | ||
| "vite-plugin-singlefile": "^2.3.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; | ||
| import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; | ||
| import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js"; | ||
| import cors from "cors"; | ||
| import express, { type Request, type Response } from "express"; | ||
| import fs from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app"; | ||
|
|
||
| const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3001; | ||
| const DIST_DIR = path.join(import.meta.dirname, "dist"); | ||
|
|
||
|
|
||
| const server = new McpServer({ | ||
| name: "Basic MCP App Server (Preact)", | ||
| version: "1.0.0", | ||
| }); | ||
|
|
||
|
|
||
| // MCP Apps require two-part registration: a tool (what the LLM calls) and a | ||
| // resource (the UI it renders). The `_meta` field on the tool links to the | ||
| // resource URI, telling hosts which UI to display when the tool executes. | ||
| { | ||
| const resourceUri = "ui://get-time/mcp-app.html"; | ||
|
|
||
| server.registerTool( | ||
| "get-time", | ||
| { | ||
| title: "Get Time", | ||
| description: "Returns the current server time as an ISO 8601 string.", | ||
| inputSchema: {}, | ||
| _meta: { [RESOURCE_URI_META_KEY]: resourceUri }, | ||
| }, | ||
| async (): Promise<CallToolResult> => { | ||
| const time = new Date().toISOString(); | ||
| return { | ||
| content: [{ type: "text", text: JSON.stringify({ time }) }], | ||
| }; | ||
| }, | ||
| ); | ||
|
|
||
| server.registerResource( | ||
| resourceUri, | ||
| resourceUri, | ||
| {}, | ||
| async (): Promise<ReadResourceResult> => { | ||
| const html = await fs.readFile(path.join(DIST_DIR, "mcp-app.html"), "utf-8"); | ||
|
|
||
| return { | ||
| contents: [ | ||
| // Per the MCP App specification, "text/html;profile=mcp-app" signals | ||
| // to the Host that this resource is indeed for an MCP App UI. | ||
| { uri: resourceUri, mimeType: RESOURCE_MIME_TYPE, text: html }, | ||
| ], | ||
| }; | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| const app = express(); | ||
| app.use(cors()); | ||
| app.use(express.json()); | ||
|
|
||
| app.post("/mcp", async (req: Request, res: Response) => { | ||
| try { | ||
| const transport = new StreamableHTTPServerTransport({ | ||
| sessionIdGenerator: undefined, | ||
| enableJsonResponse: true, | ||
| }); | ||
| res.on("close", () => { transport.close(); }); | ||
|
|
||
| await server.connect(transport); | ||
|
|
||
| await transport.handleRequest(req, res, req.body); | ||
| } catch (error) { | ||
| console.error("Error handling MCP request:", error); | ||
| if (!res.headersSent) { | ||
| res.status(500).json({ | ||
| jsonrpc: "2.0", | ||
| error: { code: -32603, message: "Internal server error" }, | ||
| id: null, | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| const httpServer = app.listen(PORT, (err) => { | ||
| if (err) { | ||
| console.error("Error starting server:", err); | ||
| process.exit(1); | ||
| } | ||
| console.log(`Server listening on http://localhost:${PORT}/mcp`); | ||
| }); | ||
|
|
||
| function shutdown() { | ||
| console.log("\nShutting down..."); | ||
| httpServer.close(() => { | ||
| console.log("Server closed"); | ||
| process.exit(0); | ||
| }); | ||
| } | ||
|
|
||
| process.on("SIGINT", shutdown); | ||
| process.on("SIGTERM", shutdown); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| * { | ||
| box-sizing: border-box; | ||
| } | ||
|
|
||
| html, body { | ||
| font-family: system-ui, -apple-system, sans-serif; | ||
| font-size: 1rem; | ||
| } | ||
|
|
||
| code { | ||
| font-size: 1em; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| .main { | ||
| --color-primary: #2563eb; | ||
| --color-primary-hover: #1d4ed8; | ||
| --color-notice-bg: #eff6ff; | ||
|
|
||
| min-width: 425px; | ||
|
|
||
| > * { | ||
| margin-top: 0; | ||
| margin-bottom: 0; | ||
| } | ||
|
|
||
| > * + * { | ||
| margin-top: 1.5rem; | ||
| } | ||
| } | ||
|
|
||
| .action { | ||
| > * { | ||
| margin-top: 0; | ||
| margin-bottom: 0; | ||
| width: 100%; | ||
| } | ||
|
|
||
| > * + * { | ||
| margin-top: 0.5rem; | ||
| } | ||
|
|
||
| /* Consistent font for form inputs (inherits from global.css) */ | ||
| textarea, | ||
| input { | ||
| font-family: inherit; | ||
| font-size: inherit; | ||
| } | ||
|
|
||
| button { | ||
| padding: 0.5rem 1rem; | ||
| border: none; | ||
| border-radius: 6px; | ||
| color: white; | ||
| font-weight: bold; | ||
| background-color: var(--color-primary); | ||
| cursor: pointer; | ||
|
|
||
| &:hover, | ||
| &:focus-visible { | ||
| background-color: var(--color-primary-hover); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| .notice { | ||
| padding: 0.5rem 0.75rem; | ||
| color: var(--color-primary); | ||
| text-align: center; | ||
| font-style: italic; | ||
| background-color: var(--color-notice-bg); | ||
|
|
||
| &::before { | ||
| content: "ℹ️ "; | ||
| font-style: normal; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These need a createServer refactoring similar to #115
(McpServer instances can't be shared between sessions)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, I would like to keep these examples as simple and self-contained as
basic-server-reactin #157. Once we decide what to do with that PR, I'll update these example to match.