-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo-mcp.js
More file actions
224 lines (190 loc) · 5.77 KB
/
Copy pathdemo-mcp.js
File metadata and controls
224 lines (190 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env node
/**
* Saha UI MCP Server v2.0 - Quick Demo
*
* This script demonstrates the dynamic features of the MCP server
* by running several example queries and showing the responses.
*/
import { spawn } from 'child_process';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// ANSI colors
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
dim: '\x1b[2m',
green: '\x1b[32m',
cyan: '\x1b[36m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
red: '\x1b[31m',
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
function logSection(title) {
console.log('\n' + '═'.repeat(70));
log(` ${title}`, 'bright');
console.log('═'.repeat(70) + '\n');
}
class MCPClient {
constructor() {
this.messageId = 0;
this.pendingRequests = new Map();
this.server = null;
}
async start() {
return new Promise((resolve, reject) => {
const serverPath = join(__dirname, 'dist', 'mcp', 'server.js');
this.server = spawn('node', [serverPath], {
stdio: ['pipe', 'pipe', 'pipe'],
});
let buffer = '';
this.server.stdout.on('data', (data) => {
buffer += data.toString();
const lines = buffer.split('\n');
buffer = lines.pop() || '';
lines.forEach((line) => {
if (line.trim()) {
try {
const message = JSON.parse(line);
this.handleResponse(message);
} catch (e) {
// Ignore non-JSON lines
}
}
});
});
this.server.stderr.on('data', (data) => {
const message = data.toString();
if (message.includes('running on stdio')) {
resolve();
}
});
this.server.on('error', reject);
setTimeout(() => resolve(), 1000);
});
}
handleResponse(message) {
if (message.id && this.pendingRequests.has(message.id)) {
const { resolve } = this.pendingRequests.get(message.id);
this.pendingRequests.delete(message.id);
resolve(message);
}
}
async sendRequest(method, params = {}) {
const id = ++this.messageId;
const request = {
jsonrpc: '2.0',
id,
method,
params,
};
return new Promise((resolve, reject) => {
this.pendingRequests.set(id, { resolve, reject });
this.server.stdin.write(JSON.stringify(request) + '\n');
setTimeout(() => {
if (this.pendingRequests.has(id)) {
this.pendingRequests.delete(id);
reject(new Error('Timeout'));
}
}, 5000);
});
}
async callTool(name, args = {}) {
return this.sendRequest('tools/call', { name, arguments: args });
}
stop() {
if (this.server) {
this.server.kill();
}
}
}
// Demo queries
const demos = [
{
title: '🔍 Fuzzy Matching Demo (Typo: "Buton")',
tool: 'get_component',
args: { name: 'Buton' },
explanation: 'The server handles typos gracefully using fuzzy matching',
},
{
title: '💬 Natural Language Query',
tool: 'ask_question',
args: { question: 'How do I change the primary color?' },
explanation: 'Ask questions in plain English - the server understands!',
},
{
title: '🎯 Smart Recommendations',
tool: 'get_recommendations',
args: { scenario: 'dashboard' },
explanation: 'Get personalized component suggestions based on your use case',
},
{
title: '📊 Filtered Component List',
tool: 'list_components_by_category',
args: { complexity: 'simple' },
explanation: 'Filter components by complexity, category, or tags',
},
{
title: '🎨 Theme Configuration',
tool: 'get_theme_config',
args: { aspect: 'colors' },
explanation: 'Get focused information about theme customization',
},
];
async function runDemo() {
logSection('🚀 Saha UI MCP Server v2.0 - Live Demo');
log('Starting MCP server...', 'dim');
const client = new MCPClient();
try {
await client.start();
log('✓ Server started successfully!\n', 'green');
await new Promise(resolve => setTimeout(resolve, 500));
for (const demo of demos) {
logSection(demo.title);
log(demo.explanation, 'dim');
console.log('');
log(`📤 Calling: ${demo.tool}`, 'cyan');
log(` Args: ${JSON.stringify(demo.args, null, 2).split('\n').join('\n ')}`, 'dim');
console.log('');
try {
const response = await client.callTool(demo.tool, demo.args);
if (response.result?.content?.[0]?.text) {
const text = response.result.content[0].text;
const preview = text.length > 800 ? text.substring(0, 800) + '\n\n...(truncated)' : text;
log('📥 Response:', 'green');
console.log(preview);
} else {
log('⚠️ No content in response', 'yellow');
}
} catch (error) {
log(`✗ Error: ${error.message}`, 'red');
}
await new Promise(resolve => setTimeout(resolve, 1000));
}
logSection('✨ Demo Complete!');
log('Key Features Demonstrated:', 'cyan');
log(' ✓ Fuzzy matching (typo tolerance)', 'green');
log(' ✓ Natural language understanding', 'green');
log(' ✓ Smart recommendations', 'green');
log(' ✓ Advanced filtering', 'green');
log(' ✓ Context-aware responses', 'green');
console.log('');
log('Try it yourself with the interactive client:', 'cyan');
log(' node test-client.js', 'bright');
console.log('');
} catch (error) {
log(`\n✗ Demo failed: ${error.message}`, 'red');
} finally {
client.stop();
}
}
// Run demo
runDemo().catch((error) => {
log(`\n✗ Fatal error: ${error.message}`, 'red');
process.exit(1);
});