You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
47
48
-
## 3. Create an MCP Tool
48
+
## 3. Set up the MCP Server Handler
49
49
50
50
In MCP, tools are functions that the client can invoke. For this example, we'll create a tool that returns a `UIResource`.
51
+
Now, let's create an MCP server instance and an endpoint to handle MCP requests using a streaming transport. This allows for more complex, stateful interactions.
51
52
52
-
Create a new file, `tools.ts`, and add the following code:
53
+
Modify your `server.ts` file to include the following:
// Connect the server instance to the transport for this session.
128
+
awaitserver.connect(transport);
129
+
} else {
130
+
returnres.status(400).json({
131
+
error: { message: 'Bad Request: No valid session ID provided' },
132
+
});
81
133
}
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
134
91
-
Modify your `server.ts` file to include the following:
92
-
93
-
```typescript
94
-
importexpressfrom'express';
95
-
importcorsfrom'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 =newServer({
104
-
tools: [newGreetTool()],
135
+
// Handle the client's request using the session's transport.
136
+
awaittransport.handleRequest(req, res, req.body);
105
137
});
106
138
107
-
app.use(cors());
108
-
app.use(express.json());
139
+
// A separate, reusable handler for GET and DELETE requests.
// GET handles the long-lived stream for server-to-client messages.
151
+
app.get('/mcp', handleSessionRequest);
113
152
114
-
// Add the /mcp endpoint
115
-
app.post('/mcp', async (req, res) => {
116
-
const response =awaitmcpServer.handle(req.body);
117
-
res.json(response);
118
-
});
153
+
// DELETE handles explicit session termination from the client.
154
+
app.delete('/mcp', handleSessionRequest);
119
155
120
156
app.listen(port, () => {
121
157
console.log(`Server listening at http://localhost:${port}`);
122
158
console.log(`MCP endpoint available at http://localhost:${port}/mcp`);
123
159
});
124
160
```
125
161
126
-
## 5. Run and Test
162
+
## 4. Run and Test
127
163
128
164
You can now run your server:
129
165
@@ -139,6 +175,6 @@ To test your new endpoint, you can use the [`ui-inspector`](https://github.com/i
139
175
4. Enter your server's MCP endpoint URL: `http://localhost:3000/mcp`.
140
176
5. Click "Connect".
141
177
142
-
The inspector will show tools for the different content types. When you call them, the UI resource will be rendered in the inspector's Tool Results.
178
+
The inspector will show the "Greet" tool. When you call it, the UI resource will be rendered in the inspector's Tool Results.
143
179
144
-
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.
180
+
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