Skip to content

Commit 9342e2a

Browse files
authored
docs: add detailed walkthroughs (#45)
1 parent 4dcb4b0 commit 9342e2a

11 files changed

Lines changed: 468 additions & 53 deletions

File tree

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<a href="#-whats-mcp-ui">What's mcp-ui?</a> •
1616
<a href="#-core-concepts">Core Concepts</a> •
1717
<a href="#-installation">Installation</a> •
18-
<a href="#-quickstart">Quickstart</a> •
18+
<a href="#-getting-started">Getting Started</a> •
19+
<a href="#-walkthrough">Walkthrough</a> •
1920
<a href="#-examples">Examples</a> •
2021
<a href="#-security">Security</a> •
2122
<a href="#-roadmap">Roadmap</a> •
@@ -105,7 +106,7 @@ Rendered using the internal `<HTMLResourceRenderer />` component, which displays
105106

106107
Rendered using the internal `<RemoteDOMResourceRenderer />` component, which utilizes Shopify's [`remote-dom`](https://github.com/Shopify/remote-dom). The server responds with a script that describes the UI and events. On the host, the script is securely rendered in a sandboxed iframe, and the UI changes are communicated to the host in JSON, where they're rendered using the host's component library. This is more flexible than iframes and allows for UIs that match the host's look-and-feel.
107108

108-
* **`mimeType`**: `application/vnd.mcp-ui.remote-dom; framework={react | webcomponents}`
109+
* **`mimeType`**: `application/vnd.mcp-ui.remote-dom+javascript; framework={react | webcomponents}`
109110

110111
### UI Action
111112

@@ -132,7 +133,7 @@ yarn add @mcp-ui/server @mcp-ui/client
132133
gem install mcp_ui_server
133134
```
134135

135-
## 🎬 Quickstart
136+
## 🚀 Getting Started
136137

137138
You can use [GitMCP](https://gitmcp.io/idosal/mcp-ui) to give your IDE access to `mcp-ui`'s latest documentation!
138139

@@ -245,6 +246,15 @@ You can use [GitMCP](https://gitmcp.io/idosal/mcp-ui) to give your IDE access to
245246
)
246247
```
247248

249+
## 🚶 Walkthrough
250+
251+
For a detailed, simple, step-by-step guide on how to integrate `mcp-ui` into your own server, check out the full server walkthroughs on the [mcp-ui documentation site](https://mcpui.dev):
252+
253+
- **[TypeScript Server Walkthrough](https://mcpui.dev/guide/server/typescript/walkthrough)**
254+
- **[Ruby Server Walkthrough](https://mcpui.dev/guide/server/ruby/walkthrough)**
255+
256+
These guides will show you how to add a `mcp-ui` endpoint to an existing server, create tools that return UI resources, and test your setup with the `ui-inspector`!
257+
248258
## 🌍 Examples
249259

250260
**Client Examples**

docs/src/.vitepress/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export default defineConfig({
107107
collapsed: false,
108108
items: [
109109
{ text: 'Overview', link: '/guide/server/typescript/overview' },
110+
{ text: 'Walkthrough', link: '/guide/server/typescript/walkthrough' },
110111
{ text: 'Usage & Examples', link: '/guide/server/typescript/usage-examples' },
111112
],
112113
},
@@ -115,6 +116,7 @@ export default defineConfig({
115116
collapsed: false,
116117
items: [
117118
{ text: 'Overview', link: '/guide/server/ruby/overview' },
119+
{ text: 'Walkthrough', link: '/guide/server/ruby/walkthrough' },
118120
{ text: 'Usage & Examples', link: '/guide/server/ruby/usage-examples' },
119121
],
120122
},

docs/src/guide/introduction.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ This project is an experimental playground for MCP-UI ideas, that aims to test o
119119
## Next Steps
120120

121121
- [Getting Started](./getting-started.md) - Set up your development environment
122+
- [Server Walkthroughs](./server/typescript/walkthrough.md) - Step-by-step guides for integrating `mcp-ui` into your server
122123
- [Client SDK](./client/overview.md) - Learn to render UI resources
123124
- [Typescript Server SDK](./server/typescript/overview.md) - Learn to create UI resources in Typescript
124125
- [Ruby Server SDK](./server/ruby/overview.md) - Learn to create UI resources in Ruby
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Ruby Server Walkthrough
2+
3+
This guide provides a step-by-step walkthrough for creating a barebones MCP UI server using Ruby's built-in WEBrick library.
4+
5+
For a complete, runnable example, see the [`ruby-server-demo`](https://github.com/idosal/mcp-ui/tree/main/examples/ruby-server-demo).
6+
7+
## 1. Project Setup
8+
9+
First, set up your project directory and dependencies.
10+
11+
Create a `Gemfile` with the necessary gems:
12+
13+
```ruby
14+
source "https://rubygems.org"
15+
16+
gem "mcp", git: "https://github.com/modelcontextprotocol/ruby-sdk"
17+
gem "mcp_ui_server"
18+
```
19+
20+
The `mcp` gem is the core Model Context Protocol SDK, while `mcp_ui_server` provides helpers for creating UI resources.
21+
22+
Install the dependencies:
23+
24+
```sh
25+
bundle install
26+
```
27+
28+
Create an empty `server.rb` file. We will add code to it in the next steps.
29+
30+
```sh
31+
touch server.rb
32+
```
33+
34+
## 2. Create an MCP Tool
35+
36+
In MCP, a "tool" is a class that can be invoked by a client. For this example, we'll create a tool that returns a `UIResource` containing an external URL.
37+
38+
Add the following to your `server.rb`:
39+
40+
```ruby
41+
require 'mcp'
42+
require 'mcp_ui_server'
43+
require 'webrick'
44+
require 'json'
45+
46+
class ExternalUrlTool < MCP::Tool
47+
description 'A simple tool that returns an external URL resource'
48+
input_schema(
49+
type: 'object',
50+
properties: {}
51+
)
52+
53+
def self.call(server_context:)
54+
ui_resource_object = McpUiServer.create_ui_resource(
55+
uri: 'ui://my-external-site',
56+
content: { type: :external_url, iframeUrl: 'https://example.com' },
57+
encoding: :text
58+
)
59+
60+
MCP::Tool::Response.new([ui_resource_object])
61+
end
62+
end
63+
```
64+
65+
The `require 'mcp_ui_server'` line imports the mcp-ui library. The `ExternalUrlTool` is a standard MCP `Tool`, but it uses `McpUiServer.create_ui_resource` to return a `UIResource`, which is the primary integration point with `mcp-ui`. The following sections describe how to set up a standard MCP server and expose it over HTTP.
66+
67+
## 3. Set Up the MCP Server
68+
69+
Next, instantiate the MCP server and register the tool with it.
70+
71+
Add this to `server.rb`:
72+
73+
```ruby
74+
# --- MCP Server Setup ---
75+
mcp_server = MCP::Server.new(tools: [ExternalUrlTool])
76+
```
77+
78+
## 4. Set Up an HTTP Server
79+
80+
We'll use WEBrick to create a simple HTTP server to handle MCP requests.
81+
82+
Add the following to `server.rb` to create an HTTP server that listens on port 8081 and handles requests at the `/mcp` endpoint:
83+
84+
```ruby
85+
# --- WEBrick HTTP Server Setup ---
86+
http_server = WEBrick::HTTPServer.new(Port: 8081)
87+
88+
# Create a servlet to handle requests to /mcp
89+
class MCPServlet < WEBrick::HTTPServlet::AbstractServlet
90+
def initialize(server, mcp_instance)
91+
super(server)
92+
@mcp = mcp_instance
93+
end
94+
95+
# Handle pre-flight CORS requests
96+
def do_OPTIONS(_request, response)
97+
response.status = 200
98+
response['Access-Control-Allow-Origin'] = '*'
99+
response['Access-Control-Allow-Methods'] = 'POST, OPTIONS'
100+
response['Access-Control-Allow-Headers'] = 'Content-Type, Accept'
101+
end
102+
103+
def do_POST(request, response)
104+
response['Access-control-Allow-Origin'] = '*'
105+
response.status = 200
106+
response['Content-Type'] = 'application/json'
107+
response.body = @mcp.handle_json(request.body)
108+
end
109+
end
110+
111+
# Mount the servlet at the /mcp endpoint
112+
http_server.mount('/mcp', MCPServlet, mcp_server)
113+
```
114+
115+
The `MCPServlet` processes incoming `POST` requests, passes them to the MCP server instance, and returns the JSON response. It also handles CORS `OPTIONS` requests to allow cross-origin communication with a web client.
116+
117+
## 5. Run the Server
118+
119+
Finally, add the code to start the server.
120+
121+
```ruby
122+
# Start the server and handle shutdown
123+
trap('INT') { http_server.shutdown }
124+
puts 'MCP server running on http://localhost:8081/mcp'
125+
http_server.start
126+
```
127+
128+
Your `server.rb` is now complete. You can run it with:
129+
130+
```sh
131+
ruby server.rb
132+
```
133+
134+
The server will be available at `http://localhost:8081/mcp`.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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.
Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
# Ruby MCP Server Demo
1+
# ruby-server-demo
22

3-
This is a barebones demo of a Ruby Model Context Protocol server that uses the `mcp` gem.
3+
This barebones server demonstrates how to use `mcp_ui_server` to generate three types of UI resources: `externalUrl`, `rawHtml`, and `remoteDom`.
44

5-
## Running the demo
5+
Each resource type has a dedicated tool:
6+
- `ExternalUrlTool`: Returns a resource that renders an external webpage (`https://example.com`) in an iframe.
7+
- `RawHtmlTool`: Returns a resource containing a simple raw HTML string.
8+
- `RemoteDomTool`: Returns a resource with a JavaScript snippet that manipulates the DOM of the client application.
69

7-
1. Install the dependencies:
10+
For a detailed explanation of how this server works, see the [Ruby Server Walkthrough](https://mcpui.dev/guide/server/ruby/walkthrough).
811

9-
```bash
10-
bundle install
11-
```
12+
## Running the server
1213

13-
2. Start the server:
14+
1. **Install dependencies:**
15+
```sh
16+
bundle install
17+
```
18+
2. **Start the server:**
19+
```sh
20+
ruby server.rb
21+
```
22+
The server will be running at `http://localhost:8081/mcp`.
1423

15-
```bash
16-
bundle exec ruby server.rb
17-
```
18-
19-
The server will start and listen for requests on http://localhost:8081/mcp.
24+
You can view the UI resources from this server by connecting to it with the [`ui-inspector`](https://github.com/idosal/ui-inspector) (target `http://localhost:8081/mcp`).

0 commit comments

Comments
 (0)