|
| 1 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 2 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 3 | +import type { |
| 4 | + CallToolResult, |
| 5 | + ReadResourceResult, |
| 6 | +} from "@modelcontextprotocol/sdk/types.js"; |
| 7 | +import fs from "node:fs/promises"; |
| 8 | +import path from "node:path"; |
| 9 | +import { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app"; |
| 10 | +import { startServer } from "../shared/server-utils.js"; |
| 11 | + |
| 12 | +const DIST_DIR = path.join(import.meta.dirname, "dist"); |
| 13 | +const RESOURCE_URI = "ui://get-time/mcp-app.html"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Creates a new MCP server instance with tools and resources registered. |
| 17 | + * Each HTTP session needs its own server instance because McpServer only supports one transport. |
| 18 | + */ |
| 19 | +function createServer(): McpServer { |
| 20 | + const server = new McpServer({ |
| 21 | + name: "Integration Test Server", |
| 22 | + version: "1.0.0", |
| 23 | + }); |
| 24 | + |
| 25 | + // MCP Apps require two-part registration: a tool (what the LLM calls) and a |
| 26 | + // resource (the UI it renders). The `_meta` field on the tool links to the |
| 27 | + // resource URI, telling hosts which UI to display when the tool executes. |
| 28 | + server.registerTool( |
| 29 | + "get-time", |
| 30 | + { |
| 31 | + title: "Get Time", |
| 32 | + description: "Returns the current server time as an ISO 8601 string.", |
| 33 | + inputSchema: {}, |
| 34 | + _meta: { [RESOURCE_URI_META_KEY]: RESOURCE_URI }, |
| 35 | + }, |
| 36 | + async (): Promise<CallToolResult> => { |
| 37 | + const time = new Date().toISOString(); |
| 38 | + return { |
| 39 | + content: [{ type: "text", text: JSON.stringify({ time }) }], |
| 40 | + }; |
| 41 | + }, |
| 42 | + ); |
| 43 | + |
| 44 | + server.registerResource( |
| 45 | + RESOURCE_URI, |
| 46 | + RESOURCE_URI, |
| 47 | + { mimeType: RESOURCE_MIME_TYPE }, |
| 48 | + async (): Promise<ReadResourceResult> => { |
| 49 | + const html = await fs.readFile( |
| 50 | + path.join(DIST_DIR, "mcp-app.html"), |
| 51 | + "utf-8", |
| 52 | + ); |
| 53 | + |
| 54 | + return { |
| 55 | + contents: [ |
| 56 | + // Per the MCP App specification, "text/html;profile=mcp-app" signals |
| 57 | + // to the Host that this resource is indeed for an MCP App UI. |
| 58 | + { uri: RESOURCE_URI, mimeType: RESOURCE_MIME_TYPE, text: html }, |
| 59 | + ], |
| 60 | + }; |
| 61 | + }, |
| 62 | + ); |
| 63 | + |
| 64 | + return server; |
| 65 | +} |
| 66 | + |
| 67 | +async function main() { |
| 68 | + if (process.argv.includes("--stdio")) { |
| 69 | + await createServer().connect(new StdioServerTransport()); |
| 70 | + } else { |
| 71 | + const port = parseInt(process.env.PORT ?? "3101", 10); |
| 72 | + await startServer(createServer, { port, name: "Integration Test Server" }); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +main().catch((e) => { |
| 77 | + console.error(e); |
| 78 | + process.exit(1); |
| 79 | +}); |
0 commit comments