|
| 1 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 2 | +import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js"; |
| 3 | +import type { CallToolResult, ReadResourceResult } from "@modelcontextprotocol/sdk/types.js"; |
| 4 | +import cors from "cors"; |
| 5 | +import express, { type Request, type Response } from "express"; |
| 6 | +import fs from "node:fs/promises"; |
| 7 | +import path from "node:path"; |
| 8 | +import { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app"; |
| 9 | + |
| 10 | +const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3001; |
| 11 | +const DIST_DIR = path.join(import.meta.dirname, "dist"); |
| 12 | + |
| 13 | + |
| 14 | +const server = new McpServer({ |
| 15 | + name: "Basic MCP App Server (Solid)", |
| 16 | + version: "1.0.0", |
| 17 | +}); |
| 18 | + |
| 19 | + |
| 20 | +// MCP Apps require two-part registration: a tool (what the LLM calls) and a |
| 21 | +// resource (the UI it renders). The `_meta` field on the tool links to the |
| 22 | +// resource URI, telling hosts which UI to display when the tool executes. |
| 23 | +{ |
| 24 | + const resourceUri = "ui://get-time/mcp-app.html"; |
| 25 | + |
| 26 | + server.registerTool( |
| 27 | + "get-time", |
| 28 | + { |
| 29 | + title: "Get Time", |
| 30 | + description: "Returns the current server time as an ISO 8601 string.", |
| 31 | + inputSchema: {}, |
| 32 | + _meta: { [RESOURCE_URI_META_KEY]: resourceUri }, |
| 33 | + }, |
| 34 | + async (): Promise<CallToolResult> => { |
| 35 | + const time = new Date().toISOString(); |
| 36 | + return { |
| 37 | + content: [{ type: "text", text: JSON.stringify({ time }) }], |
| 38 | + }; |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + server.registerResource( |
| 43 | + resourceUri, |
| 44 | + resourceUri, |
| 45 | + {}, |
| 46 | + async (): Promise<ReadResourceResult> => { |
| 47 | + const html = await fs.readFile(path.join(DIST_DIR, "mcp-app.html"), "utf-8"); |
| 48 | + |
| 49 | + return { |
| 50 | + contents: [ |
| 51 | + // Per the MCP App specification, "text/html;profile=mcp-app" signals |
| 52 | + // to the Host that this resource is indeed for an MCP App UI. |
| 53 | + { uri: resourceUri, mimeType: RESOURCE_MIME_TYPE, text: html }, |
| 54 | + ], |
| 55 | + }; |
| 56 | + }, |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +const app = express(); |
| 62 | +app.use(cors()); |
| 63 | +app.use(express.json()); |
| 64 | + |
| 65 | +app.post("/mcp", async (req: Request, res: Response) => { |
| 66 | + try { |
| 67 | + const transport = new StreamableHTTPServerTransport({ |
| 68 | + sessionIdGenerator: undefined, |
| 69 | + enableJsonResponse: true, |
| 70 | + }); |
| 71 | + res.on("close", () => { transport.close(); }); |
| 72 | + |
| 73 | + await server.connect(transport); |
| 74 | + |
| 75 | + await transport.handleRequest(req, res, req.body); |
| 76 | + } catch (error) { |
| 77 | + console.error("Error handling MCP request:", error); |
| 78 | + if (!res.headersSent) { |
| 79 | + res.status(500).json({ |
| 80 | + jsonrpc: "2.0", |
| 81 | + error: { code: -32603, message: "Internal server error" }, |
| 82 | + id: null, |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | +}); |
| 87 | + |
| 88 | +const httpServer = app.listen(PORT, (err) => { |
| 89 | + if (err) { |
| 90 | + console.error("Error starting server:", err); |
| 91 | + process.exit(1); |
| 92 | + } |
| 93 | + console.log(`Server listening on http://localhost:${PORT}/mcp`); |
| 94 | +}); |
| 95 | + |
| 96 | +function shutdown() { |
| 97 | + console.log("\nShutting down..."); |
| 98 | + httpServer.close(() => { |
| 99 | + console.log("Server closed"); |
| 100 | + process.exit(0); |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | +process.on("SIGINT", shutdown); |
| 105 | +process.on("SIGTERM", shutdown); |
0 commit comments