|
1 | 1 | import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
2 | | -import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 2 | +import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; |
3 | 3 | import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js"; |
| 4 | +import cors from "cors"; |
| 5 | +import express, { type Request, type Response } from "express"; |
4 | 6 | import fs from "node:fs/promises"; |
5 | 7 | import path from "node:path"; |
6 | 8 | import { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app"; |
7 | | -import { startServer } from "../shared/server-utils.js"; |
8 | 9 |
|
| 10 | +const PORT = parseInt(process.env.PORT ?? "3002", 10); |
9 | 11 | const DIST_DIR = path.join(import.meta.dirname, "dist"); |
10 | | -const RESOURCE_URI = "ui://get-time/mcp-app.html"; |
11 | | - |
12 | | -/** |
13 | | - * 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. |
15 | | - */ |
16 | | -function createServer(): McpServer { |
17 | | - const server = new McpServer({ |
18 | | - name: "Basic MCP App Server (Vanilla JS)", |
19 | | - version: "1.0.0", |
20 | | - }); |
21 | 12 |
|
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. |
| 13 | + |
| 14 | +const server = new McpServer({ |
| 15 | + name: "Basic MCP App Server (Vanilla JS)", |
| 16 | + version: "1.0.0", |
| 17 | +}); |
| 18 | + |
| 19 | + |
| 20 | +{ |
| 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 as |
| 26 | + // an interactive UI. |
25 | 27 | server.registerTool( |
26 | 28 | "get-time", |
27 | 29 | { |
28 | 30 | title: "Get Time", |
29 | 31 | description: "Returns the current server time as an ISO 8601 string.", |
30 | 32 | inputSchema: {}, |
31 | | - _meta: { [RESOURCE_URI_META_KEY]: RESOURCE_URI }, |
| 33 | + _meta: { [RESOURCE_URI_META_KEY]: resourceUri }, |
32 | 34 | }, |
33 | 35 | async (): Promise<CallToolResult> => { |
34 | 36 | const time = new Date().toISOString(); |
35 | | - return { |
36 | | - content: [{ type: "text", text: JSON.stringify({ time }) }], |
37 | | - }; |
| 37 | + return { content: [{ type: "text", text: time }] }; |
38 | 38 | }, |
39 | 39 | ); |
40 | 40 |
|
| 41 | + // Register the resource, which returns the bundled HTML/JavaScript for the UI. |
41 | 42 | server.registerResource( |
42 | | - RESOURCE_URI, |
43 | | - RESOURCE_URI, |
| 43 | + resourceUri, |
| 44 | + resourceUri, |
44 | 45 | { mimeType: RESOURCE_MIME_TYPE }, |
45 | 46 | async (): Promise<ReadResourceResult> => { |
46 | 47 | const html = await fs.readFile(path.join(DIST_DIR, "mcp-app.html"), "utf-8"); |
47 | 48 |
|
48 | 49 | return { |
49 | 50 | 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 }, |
| 51 | + { uri: resourceUri, mimeType: RESOURCE_MIME_TYPE, text: html }, |
53 | 52 | ], |
54 | 53 | }; |
55 | 54 | }, |
56 | 55 | ); |
57 | | - |
58 | | - return server; |
59 | 56 | } |
60 | 57 |
|
61 | | -async function main() { |
62 | | - if (process.argv.includes("--stdio")) { |
63 | | - await createServer().connect(new StdioServerTransport()); |
64 | | - } else { |
65 | | - const port = parseInt(process.env.PORT ?? "3102", 10); |
66 | | - await startServer(createServer, { port, name: "Basic MCP App Server (Vanilla JS)" }); |
| 58 | + |
| 59 | +// Start an Express server that exposes the MCP endpoint. |
| 60 | +const expressApp = express(); |
| 61 | +expressApp.use(cors()); |
| 62 | +expressApp.use(express.json()); |
| 63 | + |
| 64 | +expressApp.post("/mcp", async (req: Request, res: Response) => { |
| 65 | + try { |
| 66 | + const transport = new StreamableHTTPServerTransport({ |
| 67 | + sessionIdGenerator: undefined, |
| 68 | + enableJsonResponse: true, |
| 69 | + }); |
| 70 | + res.on("close", () => { transport.close(); }); |
| 71 | + |
| 72 | + await server.connect(transport); |
| 73 | + |
| 74 | + await transport.handleRequest(req, res, req.body); |
| 75 | + } catch (error) { |
| 76 | + console.error("Error handling MCP request:", error); |
| 77 | + if (!res.headersSent) { |
| 78 | + res.status(500).json({ |
| 79 | + jsonrpc: "2.0", |
| 80 | + error: { code: -32603, message: "Internal server error" }, |
| 81 | + id: null, |
| 82 | + }); |
| 83 | + } |
67 | 84 | } |
68 | | -} |
| 85 | +}); |
69 | 86 |
|
70 | | -main().catch((e) => { |
71 | | - console.error(e); |
72 | | - process.exit(1); |
| 87 | +const httpServer = expressApp.listen(PORT, (err) => { |
| 88 | + if (err) { |
| 89 | + console.error("Error starting server:", err); |
| 90 | + process.exit(1); |
| 91 | + } |
| 92 | + console.log(`Server listening on http://localhost:${PORT}/mcp`); |
73 | 93 | }); |
| 94 | + |
| 95 | +function shutdown() { |
| 96 | + console.log("\nShutting down..."); |
| 97 | + httpServer.close(() => { |
| 98 | + console.log("Server closed"); |
| 99 | + process.exit(0); |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +process.on("SIGINT", shutdown); |
| 104 | +process.on("SIGTERM", shutdown); |
0 commit comments