Skip to content

Commit 8b8ecbe

Browse files
Restore educational comments in basic-server-* examples
PR #184 accidentally removed/reverted educational comments from `basic-server-react` and `basic-server-vanillajs` that were established in PR #182. This restores consistency with the other basic-server examples (Vue, Svelte, Preact, Solid). Changes: - Add back the three-comment pattern explaining two-part registration - Restore full tool description ("as an ISO 8601 string") - Change vanillajs to return plain text instead of JSON (matching #182) - Use local `resourceUri` variable instead of top-level constant Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b273279 commit 8b8ecbe

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

examples/basic-server-react/server.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,27 @@ export function createServer(): McpServer {
1717
version: "1.0.0",
1818
});
1919

20+
// Two-part registration: tool + resource, tied together by the resource URI.
2021
const resourceUri = "ui://get-time/mcp-app.html";
2122

23+
// Register a tool with UI metadata. When the host calls this tool, it reads
24+
// `_meta[RESOURCE_URI_META_KEY]` to know which resource to fetch and render
25+
// as an interactive UI.
2226
registerAppTool(server,
2327
"get-time",
2428
{
2529
title: "Get Time",
26-
description: "Returns the current server time.",
30+
description: "Returns the current server time as an ISO 8601 string.",
2731
inputSchema: {},
2832
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
2933
},
3034
async (): Promise<CallToolResult> => {
31-
return { content: [{ type: "text", text: new Date().toISOString() }] };
35+
const time = new Date().toISOString();
36+
return { content: [{ type: "text", text: time }] };
3237
},
3338
);
3439

40+
// Register the resource, which returns the bundled HTML/JavaScript for the UI.
3541
registerAppResource(server,
3642
resourceUri,
3743
resourceUri,

examples/basic-server-vanillajs/server.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE, RESOURCE_URI_
88
import { startServer } from "./server-utils.js";
99

1010
const DIST_DIR = path.join(import.meta.dirname, "dist");
11-
const RESOURCE_URI = "ui://get-time/mcp-app.html";
1211

1312
/**
1413
* Creates a new MCP server instance with tools and resources registered.
15-
* Each HTTP session needs its own server instance because McpServer only supports one transport.
1614
*/
1715
export function createServer(): McpServer {
1816
const server = new McpServer({
1917
name: "Basic MCP App Server (Vanilla JS)",
2018
version: "1.0.0",
2119
});
2220

23-
// MCP Apps require two-part registration: a tool (what the LLM calls) and a
24-
// resource (the UI it renders). The `_meta` field on the tool links to the
25-
// resource URI, telling hosts which UI to display when the tool executes.
21+
// Two-part registration: tool + resource, tied together by the resource URI.
22+
const resourceUri = "ui://get-time/mcp-app.html";
23+
24+
// Register a tool with UI metadata. When the host calls this tool, it reads
25+
// `_meta[RESOURCE_URI_META_KEY]` to know which resource to fetch and render
26+
// as an interactive UI.
2627
registerAppTool(server,
2728
"get-time",
2829
{
@@ -32,7 +33,7 @@ export function createServer(): McpServer {
3233
outputSchema: z.object({
3334
time: z.string(),
3435
}),
35-
_meta: { [RESOURCE_URI_META_KEY]: RESOURCE_URI },
36+
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
3637
},
3738
async (): Promise<CallToolResult> => {
3839
const time = new Date().toISOString();
@@ -43,18 +44,17 @@ export function createServer(): McpServer {
4344
},
4445
);
4546

47+
// Register the resource, which returns the bundled HTML/JavaScript for the UI.
4648
registerAppResource(server,
47-
RESOURCE_URI,
48-
RESOURCE_URI,
49+
resourceUri,
50+
resourceUri,
4951
{ mimeType: RESOURCE_MIME_TYPE },
5052
async (): Promise<ReadResourceResult> => {
5153
const html = await fs.readFile(path.join(DIST_DIR, "mcp-app.html"), "utf-8");
5254

5355
return {
5456
contents: [
55-
// Per the MCP App specification, "text/html;profile=mcp-app" signals
56-
// to the Host that this resource is indeed for an MCP App UI.
57-
{ uri: RESOURCE_URI, mimeType: RESOURCE_MIME_TYPE, text: html },
57+
{ uri: resourceUri, mimeType: RESOURCE_MIME_TYPE, text: html },
5858
],
5959
};
6060
},

0 commit comments

Comments
 (0)