|
| 1 | +# TypeScript Server Walkthrough |
| 2 | + |
| 3 | +This guide provides a step-by-step walkthrough for integrating `@mcp-ui/server` into a an existing Node.js server using the [Express.js](https://expressjs.com) framework. |
| 4 | + |
| 5 | +For a complete, runnable example, see the [`typescript-server-demo`](https://github.com/idosal/mcp-ui/tree/main/examples/typescript-server-demo). |
| 6 | + |
| 7 | +## 1. Set up an Express Server |
| 8 | + |
| 9 | +If you don't have an existing server, you can create a simple one. |
| 10 | + |
| 11 | +First, install the necessary dependencies: |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install express cors @types/express @types/cors |
| 15 | +``` |
| 16 | + |
| 17 | +Then, create a basic server file (e.g., `server.ts`): |
| 18 | + |
| 19 | +```typescript |
| 20 | +import express from 'express'; |
| 21 | +import cors from 'cors'; |
| 22 | + |
| 23 | +const app = express(); |
| 24 | +const port = 3000; |
| 25 | + |
| 26 | +app.use(cors()); |
| 27 | +app.use(express.json()); |
| 28 | + |
| 29 | +app.get('/', (req, res) => { |
| 30 | + res.send('Hello, world!'); |
| 31 | +}); |
| 32 | + |
| 33 | +app.listen(port, () => { |
| 34 | + console.log(`Server listening at http://localhost:${port}`); |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +## 2. Install MCP and mcp-ui Dependencies |
| 39 | + |
| 40 | +Next, add the Model Context Protocol SDK and the `mcp-ui` server package to your project: |
| 41 | + |
| 42 | +```bash |
| 43 | +npm install @modelcontextprotocol/sdk @mcp-ui/server |
| 44 | +``` |
| 45 | + |
| 46 | +The `@modelcontextprotocol/sdk` package provides the core functionality for creating an MCP server, while `@mcp-ui/server` includes helpers specifically for creating UI resources. |
| 47 | + |
| 48 | +## 3. Create an MCP Tool |
| 49 | + |
| 50 | +In MCP, tools are functions that the client can invoke. For this example, we'll create a tool that returns a `UIResource`. |
| 51 | + |
| 52 | +Create a new file, `tools.ts`, and add the following code: |
| 53 | + |
| 54 | +```typescript |
| 55 | +import { Tool, ToolResponse } from '@modelcontextprotocol/sdk'; |
| 56 | +import { createUIResource } from '@mcp-ui/server'; |
| 57 | + |
| 58 | +export class GreetTool extends Tool { |
| 59 | + constructor() { |
| 60 | + super({ |
| 61 | + name: 'greet', |
| 62 | + description: 'A simple tool that returns a UI resource', |
| 63 | + inputSchema: { |
| 64 | + type: 'object', |
| 65 | + properties: {}, |
| 66 | + }, |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + async call(serverContext: any): Promise<ToolResponse> { |
| 71 | + const uiResource = createUIResource({ |
| 72 | + uri: 'ui://greeting', |
| 73 | + content: { |
| 74 | + type: 'externalUrl', |
| 75 | + iframeUrl: 'https://example.com', |
| 76 | + }, |
| 77 | + encoding: 'text', |
| 78 | + }); |
| 79 | + |
| 80 | + return new ToolResponse([uiResource]); |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +This tool, when called, will generate a simple HTML UI resource. The `import { createUIResource } from '@mcp-ui/server'` line imports the `mcp-ui` helper. The `GreetTool` is a standard MCP `Tool`, but it uses `createUIResource` to generate a `UIResource`, which is the primary integration point with `mcp-ui`. The following section describes how to set up a standard MCP server and expose it over HTTP. |
| 86 | + |
| 87 | +## 4. Set up the MCP Server Handler |
| 88 | + |
| 89 | +Now, let's create an MCP server instance and an endpoint to handle MCP requests. |
| 90 | + |
| 91 | +Modify your `server.ts` file to include the following: |
| 92 | + |
| 93 | +```typescript |
| 94 | +import express from 'express'; |
| 95 | +import cors from 'cors'; |
| 96 | +import { Server } from '@modelcontextprotocol/sdk'; |
| 97 | +import { GreetTool } from './tools'; |
| 98 | + |
| 99 | +const app = express(); |
| 100 | +const port = 3000; |
| 101 | + |
| 102 | +// Set up the MCP server with your tool |
| 103 | +const mcpServer = new Server({ |
| 104 | + tools: [new GreetTool()], |
| 105 | +}); |
| 106 | + |
| 107 | +app.use(cors()); |
| 108 | +app.use(express.json()); |
| 109 | + |
| 110 | +app.get('/', (req, res) => { |
| 111 | + res.send('Hello, world!'); |
| 112 | +}); |
| 113 | + |
| 114 | +// Add the /mcp endpoint |
| 115 | +app.post('/mcp', async (req, res) => { |
| 116 | + const response = await mcpServer.handle(req.body); |
| 117 | + res.json(response); |
| 118 | +}); |
| 119 | + |
| 120 | +app.listen(port, () => { |
| 121 | + console.log(`Server listening at http://localhost:${port}`); |
| 122 | + console.log(`MCP endpoint available at http://localhost:${port}/mcp`); |
| 123 | +}); |
| 124 | +``` |
| 125 | + |
| 126 | +## 5. Run and Test |
| 127 | + |
| 128 | +You can now run your server: |
| 129 | + |
| 130 | +```bash |
| 131 | +npx ts-node server.ts |
| 132 | +``` |
| 133 | + |
| 134 | +To test your new endpoint, you can use the [`ui-inspector`](https://github.com/idosal/ui-inspector): |
| 135 | + |
| 136 | +1. Go to the **[ui-inspector website](https://idosal.github.io/ui-inspector/)**. |
| 137 | +2. Enter your server's MCP endpoint URL: `http://localhost:3000/mcp`. |
| 138 | +3. Click "Connect". |
| 139 | + |
| 140 | +The inspector will show the `greet` tool. When you call it, the UI resource will be rendered in the inspector's "UI" tab. |
| 141 | + |
| 142 | +You've now successfully integrated `mcp-ui` into your TypeScript server! You can now create more complex tools that return different types of UI resources. |
0 commit comments