Skip to content

Commit a7d73c7

Browse files
authored
Make vite work out-of-the-box for simple-host (#33)
1 parent 9d8b705 commit a7d73c7

File tree

6 files changed

+33
-104
lines changed

6 files changed

+33
-104
lines changed

examples/simple-host/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<meta http-equiv="refresh" content="0; url=/example-host-react.html">

examples/simple-host/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"version": "1.0.0",
55
"type": "module",
66
"scripts": {
7-
"start": "NODE_ENV=development npm run build && concurrently 'npm run start:server'",
8-
"start:server": "bun serve.ts",
9-
"build": "concurrently 'INPUT=example-host-vanilla.html vite build' 'INPUT=example-host-react.html vite build' 'INPUT=sandbox.html vite build'"
7+
"start": "vite dev",
8+
"start:server": "vite preview",
9+
"build": "vite build"
1010
},
1111
"dependencies": {
1212
"@modelcontextprotocol/ext-apps": "../..",

examples/simple-host/serve.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.

examples/simple-host/src/example-host-react.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import { Tool } from "@modelcontextprotocol/sdk/types.js";
88
import { AppRenderer, AppRendererProps } from "../src/AppRenderer";
99
import { AppBridge } from "../../../dist/src/app-bridge";
1010

11-
const SANDBOX_PROXY_URL = new URL("http://localhost:8081/sandbox.html");
11+
// We use '[::1]' for the sandbox to ensure it's a different origin from 'localhost'.
12+
const SANDBOX_PROXY_URL = new URL(
13+
"/sandbox.html",
14+
location.href.replace("localhost:", "[::1]:"),
15+
);
1216

1317
/**
1418
* Example React application demonstrating the AppRenderer component.

examples/simple-host/src/example-host-vanilla.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import {
1818
McpUiSizeChangeNotificationSchema,
1919
} from "@modelcontextprotocol/ext-apps";
2020

21-
const SANDBOX_PROXY_URL = new URL("http://localhost:8081/sandbox.html");
21+
// We use '[::1]' for the sandbox to ensure it's a different origin from 'localhost'.
22+
const SANDBOX_PROXY_URL = new URL(
23+
"/sandbox.html",
24+
location.href.replace("localhost:", "[::1]:"),
25+
);
2226

2327
window.addEventListener("load", async () => {
2428
const client = new Client({
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import { defineConfig } from "vite";
22
import react from "@vitejs/plugin-react";
3-
import { viteSingleFile } from "vite-plugin-singlefile";
43

5-
const INPUT = process.env.INPUT;
6-
if (!INPUT) {
7-
throw new Error("INPUT environment variable is not set");
8-
}
9-
10-
const isDevelopment = process.env.NODE_ENV === "development";
11-
12-
export default defineConfig({
13-
plugins: [react(), viteSingleFile()],
14-
build: {
15-
sourcemap: isDevelopment ? "inline" : undefined,
16-
cssMinify: !isDevelopment,
17-
minify: !isDevelopment,
18-
rollupOptions: {
19-
input: INPUT,
4+
export default defineConfig(({ mode }) => {
5+
const isDevelopment = mode === "development";
6+
return {
7+
plugins: [react()],
8+
build: {
9+
sourcemap: isDevelopment ? "inline" : undefined,
10+
cssMinify: !isDevelopment,
11+
minify: !isDevelopment,
12+
rollupOptions: {
13+
input: [
14+
"index.html",
15+
"example-host-vanilla.html",
16+
"example-host-react.html",
17+
"sandbox.html",
18+
],
19+
},
20+
outDir: `dist`,
21+
emptyOutDir: false,
2022
},
21-
outDir: `dist`,
22-
emptyOutDir: false,
23-
},
23+
};
2424
});

0 commit comments

Comments
 (0)