Skip to content

Commit 3116282

Browse files
Add basic-server-vue example
Demonstrates MCP App SDK usage with Vue 3, providing framework parity alongside the existing React and vanilla JS examples. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent bf23882 commit 3116282

16 files changed

Lines changed: 784 additions & 996 deletions

File tree

.prettierignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
examples/basic-host/**/*.ts
22
examples/basic-host/**/*.tsx
3-
examples/basic-server-react/**/*.ts
4-
examples/basic-server-react/**/*.tsx
5-
examples/basic-server-vanillajs/**/*.ts
6-
examples/basic-server-vanillajs/**/*.tsx
3+
examples/basic-server-*/**/*.ts
4+
examples/basic-server-*/**/*.tsx

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ Or edit your `package.json` manually:
4747

4848
Start with these foundational examples to learn the SDK:
4949

50-
- [`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)
51-
- [`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)
52-
- [`examples/basic-host`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-host) — Bare-bones example of hosting MCP Apps
50+
- [`examples/basic-server-vanillajs`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-vanillajs) — MCP server + MCP App using vanilla JS
51+
- [`examples/basic-server-react`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-react) — MCP server + MCP App using React
52+
- [`examples/basic-server-vue`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-vue) — MCP server + MCP App using Vue
53+
- [`examples/basic-host`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-host) — MCP host application supporting MCP Apps
5354

5455
The [`examples/`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples) directory contains additional demo apps showcasing real-world use cases.
5556

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Example: Basic Server (Vue)
2+
3+
An MCP App example with a Vue 3 UI using the Composition API.
4+
5+
> [!TIP]
6+
> Looking for a vanilla JavaScript example? See [`basic-server-vanillajs`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-vanillajs)!
7+
8+
## Overview
9+
10+
- Tool registration with a linked UI resource
11+
- Vue 3 UI using the [`App`](https://modelcontextprotocol.github.io/ext-apps/api/classes/app.App.html) class
12+
- 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), [`openLink`](https://modelcontextprotocol.github.io/ext-apps/api/classes/app.App.html#openlink)
13+
14+
## Key Files
15+
16+
- [`server.ts`](server.ts) - MCP server with tool and resource registration
17+
- [`mcp-app.html`](mcp-app.html) / [`src/App.vue`](src/App.vue) - Vue 3 UI using `App` class
18+
19+
## Getting Started
20+
21+
```bash
22+
npm install
23+
npm run dev
24+
```
25+
26+
## How It Works
27+
28+
1. The server registers a `get-time` tool with metadata linking it to a UI HTML resource (`ui://get-time/mcp-app.html`).
29+
2. When the tool is invoked, the Host renders the UI from the resource.
30+
3. The UI uses the MCP App SDK API to communicate with the host and call server tools.

examples/basic-server-vue/env.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference types="vite/client" />
2+
3+
declare module "*.vue" {
4+
import type { DefineComponent } from "vue";
5+
const component: DefineComponent<object, object, unknown>;
6+
export default component;
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta name="color-scheme" content="light dark">
7+
<title>Get Time App</title>
8+
<link rel="stylesheet" href="/src/global.css">
9+
</head>
10+
<body>
11+
<div id="app"></div>
12+
<script type="module" src="/src/mcp-app.ts"></script>
13+
</body>
14+
</html>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "basic-server-vue",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"build": "tsc --noEmit && cross-env INPUT=mcp-app.html vite build",
8+
"watch": "cross-env INPUT=mcp-app.html vite build --watch",
9+
"serve": "bun server.ts",
10+
"start": "cross-env NODE_ENV=development npm run build && npm run serve",
11+
"dev": "cross-env NODE_ENV=development concurrently 'npm run watch' 'npm run serve'"
12+
},
13+
"dependencies": {
14+
"@modelcontextprotocol/ext-apps": "../..",
15+
"@modelcontextprotocol/sdk": "^1.24.0",
16+
"vue": "^3.5.0",
17+
"zod": "^4.1.13"
18+
},
19+
"devDependencies": {
20+
"@types/cors": "^2.8.19",
21+
"@types/express": "^5.0.0",
22+
"@types/node": "^22.0.0",
23+
"@vitejs/plugin-vue": "^5.0.0",
24+
"concurrently": "^9.2.1",
25+
"cors": "^2.8.5",
26+
"cross-env": "^7.0.3",
27+
"express": "^5.1.0",
28+
"typescript": "^5.9.3",
29+
"vite": "^6.0.0",
30+
"vite-plugin-singlefile": "^2.3.0"
31+
}
32+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2+
import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
3+
import fs from "node:fs/promises";
4+
import path from "node:path";
5+
import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "@modelcontextprotocol/ext-apps/server";
6+
import { startServer } from "./src/server-utils.js";
7+
8+
const DIST_DIR = path.join(import.meta.dirname, "dist");
9+
10+
/**
11+
* Creates a new MCP server instance with tools and resources registered.
12+
*/
13+
function createServer(): McpServer {
14+
const server = new McpServer({
15+
name: "Basic MCP App Server (Vue)",
16+
version: "1.0.0",
17+
});
18+
19+
// Two-part registration: tool + resource, tied together by the resource URI.
20+
const resourceUri = "ui://get-time/mcp-app.html";
21+
22+
// Register a tool with UI metadata. When the host calls this tool, it reads
23+
// `_meta[RESOURCE_URI_META_KEY]` to know which resource to fetch and render
24+
// as an interactive UI.
25+
registerAppTool(server,
26+
"get-time",
27+
{
28+
title: "Get Time",
29+
description: "Returns the current server time as an ISO 8601 string.",
30+
inputSchema: {},
31+
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
32+
},
33+
async (): Promise<CallToolResult> => {
34+
const time = new Date().toISOString();
35+
return { content: [{ type: "text", text: time }] };
36+
},
37+
);
38+
39+
// Register the resource, which returns the bundled HTML/JavaScript for the UI.
40+
registerAppResource(server,
41+
resourceUri,
42+
resourceUri,
43+
{ mimeType: RESOURCE_MIME_TYPE },
44+
async (): Promise<ReadResourceResult> => {
45+
const html = await fs.readFile(path.join(DIST_DIR, "mcp-app.html"), "utf-8");
46+
47+
return {
48+
contents: [
49+
{ uri: resourceUri, mimeType: RESOURCE_MIME_TYPE, text: html },
50+
],
51+
};
52+
},
53+
);
54+
55+
return server;
56+
}
57+
58+
startServer(createServer);
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<script setup lang="ts">
2+
import { ref, onMounted } from "vue";
3+
import { App, PostMessageTransport } from "@modelcontextprotocol/ext-apps";
4+
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
5+
6+
const log = {
7+
info: console.log.bind(console, "[APP]"),
8+
warn: console.warn.bind(console, "[APP]"),
9+
error: console.error.bind(console, "[APP]"),
10+
};
11+
12+
function extractTime(result: CallToolResult): string {
13+
const { text } = result.content?.find((c) => c.type === "text")!;
14+
return text;
15+
}
16+
17+
const app = ref<App | null>(null);
18+
const serverTime = ref("Loading...");
19+
const messageText = ref("This is message text.");
20+
const logText = ref("This is log text.");
21+
const linkUrl = ref("https://modelcontextprotocol.io/");
22+
23+
onMounted(async () => {
24+
const instance = new App({ name: "Get Time App", version: "1.0.0" });
25+
26+
instance.ontoolinput = (params) => {
27+
log.info("Received tool call input:", params);
28+
};
29+
30+
instance.ontoolresult = (result) => {
31+
log.info("Received tool call result:", result);
32+
serverTime.value = extractTime(result);
33+
};
34+
35+
instance.onerror = log.error;
36+
37+
await instance.connect(new PostMessageTransport(window.parent));
38+
app.value = instance;
39+
});
40+
41+
async function handleGetTime() {
42+
if (!app.value) return;
43+
try {
44+
log.info("Calling get-time tool...");
45+
const result = await app.value.callServerTool({ name: "get-time", arguments: {} });
46+
log.info("get-time result:", result);
47+
serverTime.value = extractTime(result);
48+
} catch (e) {
49+
log.error(e);
50+
serverTime.value = "[ERROR]";
51+
}
52+
}
53+
54+
async function handleSendMessage() {
55+
if (!app.value) return;
56+
const signal = AbortSignal.timeout(5000);
57+
try {
58+
log.info("Sending message text to Host:", messageText.value);
59+
const { isError } = await app.value.sendMessage(
60+
{ role: "user", content: [{ type: "text", text: messageText.value }] },
61+
{ signal },
62+
);
63+
log.info("Message", isError ? "rejected" : "accepted");
64+
} catch (e) {
65+
log.error("Message send error:", signal.aborted ? "timed out" : e);
66+
}
67+
}
68+
69+
async function handleSendLog() {
70+
if (!app.value) return;
71+
log.info("Sending log text to Host:", logText.value);
72+
await app.value.sendLog({ level: "info", data: logText.value });
73+
}
74+
75+
async function handleOpenLink() {
76+
if (!app.value) return;
77+
log.info("Sending open link request to Host:", linkUrl.value);
78+
const { isError } = await app.value.openLink({ url: linkUrl.value });
79+
log.info("Open link request", isError ? "rejected" : "accepted");
80+
}
81+
</script>
82+
83+
<template>
84+
<main class="main">
85+
<p class="notice">Watch activity in the DevTools console!</p>
86+
87+
<div class="action">
88+
<p><strong>Server Time:</strong> <code id="server-time">{{ serverTime }}</code></p>
89+
<button @click="handleGetTime">Get Server Time</button>
90+
</div>
91+
92+
<div class="action">
93+
<textarea v-model="messageText"></textarea>
94+
<button @click="handleSendMessage">Send Message</button>
95+
</div>
96+
97+
<div class="action">
98+
<input type="text" v-model="logText">
99+
<button @click="handleSendLog">Send Log</button>
100+
</div>
101+
102+
<div class="action">
103+
<input type="url" v-model="linkUrl">
104+
<button @click="handleOpenLink">Open Link</button>
105+
</div>
106+
</main>
107+
</template>
108+
109+
<style scoped>
110+
.main {
111+
--color-primary: #2563eb;
112+
--color-primary-hover: #1d4ed8;
113+
--color-notice-bg: #eff6ff;
114+
115+
width: 100%;
116+
max-width: 425px;
117+
box-sizing: border-box;
118+
119+
> * {
120+
margin-top: 0;
121+
margin-bottom: 0;
122+
}
123+
124+
> * + * {
125+
margin-top: 1.5rem;
126+
}
127+
}
128+
129+
.action {
130+
> * {
131+
margin-top: 0;
132+
margin-bottom: 0;
133+
width: 100%;
134+
}
135+
136+
> * + * {
137+
margin-top: 0.5rem;
138+
}
139+
140+
textarea,
141+
input {
142+
font-family: inherit;
143+
font-size: inherit;
144+
}
145+
146+
button {
147+
padding: 0.5rem 1rem;
148+
border: none;
149+
border-radius: 6px;
150+
color: white;
151+
font-weight: bold;
152+
background-color: var(--color-primary);
153+
cursor: pointer;
154+
155+
&:hover,
156+
&:focus-visible {
157+
background-color: var(--color-primary-hover);
158+
}
159+
}
160+
}
161+
162+
.notice {
163+
padding: 0.5rem 0.75rem;
164+
color: var(--color-primary);
165+
text-align: center;
166+
font-style: italic;
167+
background-color: var(--color-notice-bg);
168+
169+
&::before {
170+
content: "ℹ️ ";
171+
font-style: normal;
172+
}
173+
}
174+
</style>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
* {
2+
box-sizing: border-box;
3+
}
4+
5+
html, body {
6+
font-family: system-ui, -apple-system, sans-serif;
7+
font-size: 1rem;
8+
}
9+
10+
code {
11+
font-size: 1em;
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createApp } from "vue";
2+
import App from "./App.vue";
3+
import "./global.css";
4+
5+
createApp(App).mount("#app");

0 commit comments

Comments
 (0)