Skip to content

Commit fd93425

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 4c51eb0 commit fd93425

2 files changed

Lines changed: 20 additions & 16 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: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,47 @@ import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE, RESOURCE_URI_
77
import { startServer } from "./server-utils.js";
88

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

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

22-
// MCP Apps require two-part registration: a tool (what the LLM calls) and a
23-
// resource (the UI it renders). The `_meta` field on the tool links to the
24-
// resource URI, telling hosts which UI to display when the tool executes.
20+
// Two-part registration: tool + resource, tied together by the resource URI.
21+
const resourceUri = "ui://get-time/mcp-app.html";
22+
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.
2526
registerAppTool(server,
2627
"get-time",
2728
{
2829
title: "Get Time",
2930
description: "Returns the current server time as an ISO 8601 string.",
3031
inputSchema: {},
31-
_meta: { [RESOURCE_URI_META_KEY]: RESOURCE_URI },
32+
_meta: { [RESOURCE_URI_META_KEY]: resourceUri },
3233
},
3334
async (): Promise<CallToolResult> => {
3435
const time = new Date().toISOString();
35-
return {
36-
content: [{ type: "text", text: JSON.stringify({ time }) }],
37-
};
36+
return { content: [{ type: "text", text: time }] };
3837
},
3938
);
4039

40+
// Register the resource, which returns the bundled HTML/JavaScript for the UI.
4141
registerAppResource(server,
42-
RESOURCE_URI,
43-
RESOURCE_URI,
42+
resourceUri,
43+
resourceUri,
4444
{ mimeType: RESOURCE_MIME_TYPE },
4545
async (): Promise<ReadResourceResult> => {
4646
const html = await fs.readFile(path.join(DIST_DIR, "mcp-app.html"), "utf-8");
4747

4848
return {
4949
contents: [
50-
// Per the MCP App specification, "text/html;profile=mcp-app" signals
51-
// to the Host that this resource is indeed for an MCP App UI.
52-
{ uri: RESOURCE_URI, mimeType: RESOURCE_MIME_TYPE, text: html },
50+
{ uri: resourceUri, mimeType: RESOURCE_MIME_TYPE, text: html },
5351
],
5452
};
5553
},

0 commit comments

Comments
 (0)