Skip to content

Commit bea0ae2

Browse files
ochafikclaudejonathanhefner
authored
feat: add server helpers, make connect() to default to parent post transport (#165)
* feat: add server helpers and optional connect() transport Add `src/server/` with convenience functions for registering MCP App tools and resources: - `registerAppTool(server, name, config, handler)` - `registerAppResource(server, name, uri, config, callback)` The `transport` parameter in `App.connect()` is now optional, defaulting to `PostMessageTransport(window.parent)`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Update src/server/index.ts Co-authored-by: Jonathan Hefner <[email protected]> * fix: refine server helper types - Make RESOURCE_URI_META_KEY required in McpUiAppToolConfig (tools need a UI resource) - Make _meta optional in McpUiAppResourceConfig (not all resources need custom metadata) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * refactor: simplify RESOURCE_URI_META_KEY re-export * refactor: consolidate export statements --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Jonathan Hefner <[email protected]>
1 parent 50cf6a4 commit bea0ae2

File tree

20 files changed

+407
-40
lines changed

20 files changed

+407
-40
lines changed

examples/basic-server-react/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
33
import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
44
import fs from "node:fs/promises";
55
import path from "node:path";
6-
import { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app";
6+
import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "@modelcontextprotocol/ext-apps/server";
77
import { startServer } from "../shared/server-utils.js";
88

99
const DIST_DIR = path.join(import.meta.dirname, "dist");
@@ -22,7 +22,7 @@ function createServer(): McpServer {
2222
// MCP Apps require two-part registration: a tool (what the LLM calls) and a
2323
// resource (the UI it renders). The `_meta` field on the tool links to the
2424
// resource URI, telling hosts which UI to display when the tool executes.
25-
server.registerTool(
25+
registerAppTool(server,
2626
"get-time",
2727
{
2828
title: "Get Time",
@@ -38,7 +38,7 @@ function createServer(): McpServer {
3838
},
3939
);
4040

41-
server.registerResource(
41+
registerAppResource(server,
4242
RESOURCE_URI,
4343
RESOURCE_URI,
4444
{ mimeType: RESOURCE_MIME_TYPE },

examples/basic-server-vanillajs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ button.addEventListener("click", () => {
5252
});
5353

5454
// Connect to host
55-
app.connect(new PostMessageTransport(window.parent));
55+
app.connect();
5656
```

examples/basic-server-vanillajs/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
33
import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
44
import fs from "node:fs/promises";
55
import path from "node:path";
6-
import { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app";
6+
import { registerAppTool, registerAppResource, RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "@modelcontextprotocol/ext-apps/server";
77
import { startServer } from "../shared/server-utils.js";
88

99
const DIST_DIR = path.join(import.meta.dirname, "dist");
@@ -22,7 +22,7 @@ function createServer(): McpServer {
2222
// MCP Apps require two-part registration: a tool (what the LLM calls) and a
2323
// resource (the UI it renders). The `_meta` field on the tool links to the
2424
// resource URI, telling hosts which UI to display when the tool executes.
25-
server.registerTool(
25+
registerAppTool(server,
2626
"get-time",
2727
{
2828
title: "Get Time",
@@ -38,7 +38,7 @@ function createServer(): McpServer {
3838
},
3939
);
4040

41-
server.registerResource(
41+
registerAppResource(server,
4242
RESOURCE_URI,
4343
RESOURCE_URI,
4444
{ mimeType: RESOURCE_MIME_TYPE },

examples/basic-server-vanillajs/src/mcp-app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file App that demonstrates a few features using MCP Apps SDK with vanilla JS.
33
*/
4-
import { App, PostMessageTransport } from "@modelcontextprotocol/ext-apps";
4+
import { App } from "@modelcontextprotocol/ext-apps";
55
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
66
import "./global.css";
77
import "./mcp-app.css";
@@ -98,4 +98,4 @@ openLinkBtn.addEventListener("click", async () => {
9898

9999

100100
// Connect to host
101-
app.connect(new PostMessageTransport(window.parent));
101+
app.connect();

examples/budget-allocator-server/server.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import type {
1313
import fs from "node:fs/promises";
1414
import path from "node:path";
1515
import { z } from "zod";
16-
import { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app";
16+
import {
17+
RESOURCE_MIME_TYPE,
18+
RESOURCE_URI_META_KEY,
19+
} from "@modelcontextprotocol/ext-apps/server";
1720
import { startServer } from "../shared/server-utils.js";
1821

1922
const DIST_DIR = path.join(import.meta.dirname, "dist");
@@ -235,7 +238,8 @@ function createServer(): McpServer {
235238
version: "1.0.0",
236239
});
237240

238-
server.registerTool(
241+
registerAppTool(
242+
server,
239243
"get-budget-data",
240244
{
241245
title: "Get Budget Data",
@@ -277,7 +281,8 @@ function createServer(): McpServer {
277281
},
278282
);
279283

280-
server.registerResource(
284+
registerAppResource(
285+
server,
281286
resourceUri,
282287
resourceUri,
283288
{

examples/budget-allocator-server/src/mcp-app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Budget Allocator App - Interactive budget allocation with real-time visualization
33
*/
4-
import { App, PostMessageTransport } from "@modelcontextprotocol/ext-apps";
4+
import { App } from "@modelcontextprotocol/ext-apps";
55
import { Chart, registerables } from "chart.js";
66
import "./global.css";
77
import "./mcp-app.css";
@@ -631,4 +631,4 @@ window
631631
});
632632

633633
// Connect to host
634-
app.connect(new PostMessageTransport(window.parent));
634+
app.connect();

examples/cohort-heatmap-server/server.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import type { ReadResourceResult } from "@modelcontextprotocol/sdk/types.js";
44
import fs from "node:fs/promises";
55
import path from "node:path";
66
import { z } from "zod";
7-
import { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app";
7+
import {
8+
RESOURCE_MIME_TYPE,
9+
RESOURCE_URI_META_KEY,
10+
} from "@modelcontextprotocol/ext-apps/server";
811
import { startServer } from "../shared/server-utils.js";
912

1013
const DIST_DIR = path.join(import.meta.dirname, "dist");
@@ -156,7 +159,8 @@ function createServer(): McpServer {
156159
// Register tool and resource
157160
const resourceUri = "ui://get-cohort-data/mcp-app.html";
158161

159-
server.registerTool(
162+
registerAppTool(
163+
server,
160164
"get-cohort-data",
161165
{
162166
title: "Get Cohort Retention Data",
@@ -179,7 +183,8 @@ function createServer(): McpServer {
179183
},
180184
);
181185

182-
server.registerResource(
186+
registerAppResource(
187+
server,
183188
resourceUri,
184189
resourceUri,
185190
{ mimeType: RESOURCE_MIME_TYPE },

examples/customer-segmentation-server/server.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import type {
77
import fs from "node:fs/promises";
88
import path from "node:path";
99
import { z } from "zod";
10-
import { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app";
10+
import {
11+
RESOURCE_MIME_TYPE,
12+
RESOURCE_URI_META_KEY,
13+
} from "@modelcontextprotocol/ext-apps/server";
1114
import { startServer } from "../shared/server-utils.js";
1215
import {
1316
generateCustomers,
@@ -65,7 +68,8 @@ function createServer(): McpServer {
6568
{
6669
const resourceUri = "ui://customer-segmentation/mcp-app.html";
6770

68-
server.registerTool(
71+
registerAppTool(
72+
server,
6973
"get-customer-data",
7074
{
7175
title: "Get Customer Data",
@@ -83,7 +87,8 @@ function createServer(): McpServer {
8387
},
8488
);
8589

86-
server.registerResource(
90+
registerAppResource(
91+
server,
8792
resourceUri,
8893
resourceUri,
8994
{

examples/customer-segmentation-server/src/mcp-app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ app.onhostcontextchanged = (params) => {
463463
}
464464
};
465465

466-
app.connect(new PostMessageTransport(window.parent)).then(() => {
466+
app.connect().then(() => {
467467
// Apply initial host context after connection
468468
const ctx = app.getHostContext();
469469
if (ctx?.theme) {

examples/qr-server/widget.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
}
4848
};
4949

50-
await app.connect(new PostMessageTransport(window.parent));
50+
await app.connect();
5151
</script>
5252
</body>
5353
</html>

0 commit comments

Comments
 (0)