-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathdemo.js
More file actions
175 lines (155 loc) · 4.91 KB
/
demo.js
File metadata and controls
175 lines (155 loc) · 4.91 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
#!/usr/bin/env node
import { spawn } from 'child_process';
// Example React code to analyze and modify
const exampleCode = `import React from 'react';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>My PWA Storefront</h1>
</header>
</div>
);
}
export default App;`;
async function demonstrateMCPTools() {
console.log('🚀 Demonstrating MCP Server Tools...\n');
// Start the MCP server process
const serverProcess = spawn('node', ['src/server/server.js'], {
stdio: ['pipe', 'pipe', 'pipe']
});
let responseData = '';
serverProcess.stdout.on('data', (data) => {
responseData += data.toString();
});
serverProcess.stderr.on('data', (data) => {
console.log('Server:', data.toString().trim());
});
try {
// Initialize connection
const initRequest = {
jsonrpc: '2.0',
id: 1,
method: 'initialize',
params: {
protocolVersion: '2024-11-05',
capabilities: {},
clientInfo: { name: 'demo-client', version: '1.0.0' }
}
};
serverProcess.stdin.write(JSON.stringify(initRequest) + '\n');
await wait(500);
// Test 1: Analyze code structure
console.log('📋 1. Analyzing code structure...');
const analyzeRequest = {
jsonrpc: '2.0',
id: 2,
method: 'tools/call',
params: {
name: 'analyze_code_structure',
arguments: { code: exampleCode }
}
};
serverProcess.stdin.write(JSON.stringify(analyzeRequest) + '\n');
await wait(1000);
// Test 2: Insert a Product Card component
console.log('\n🛍️ 2. Inserting a ProductCard component...');
const insertRequest = {
jsonrpc: '2.0',
id: 3,
method: 'tools/call',
params: {
name: 'insert_react_component',
arguments: {
code: exampleCode,
componentType: 'product',
options: {
name: 'ProductCard',
styling: 'tailwind',
showPrice: true,
showRating: true
}
}
}
};
serverProcess.stdin.write(JSON.stringify(insertRequest) + '\n');
await wait(1000);
// Test 3: Create a new Button component file
console.log('\n🔘 3. Creating a new Button component file...');
const createRequest = {
jsonrpc: '2.0',
id: 4,
method: 'tools/call',
params: {
name: 'create_component_file',
arguments: {
componentName: 'PrimaryButton',
componentType: 'button',
options: {
variant: 'primary',
size: 'medium',
styling: 'tailwind'
}
}
}
};
serverProcess.stdin.write(JSON.stringify(createRequest) + '\n');
await wait(1000);
// Parse and display results
console.log('\n📨 Results:');
parseAndDisplayResults(responseData);
} catch (error) {
console.error('❌ Error:', error);
} finally {
serverProcess.kill();
}
}
function parseAndDisplayResults(responseData) {
if (!responseData.trim()) {
console.log('No responses received');
return;
}
const responses = responseData.trim().split('\n').filter(line => line.trim());
responses.forEach((response, index) => {
try {
const parsed = JSON.parse(response);
if (parsed.id === 1) {
console.log('✅ Server initialized');
} else if (parsed.id === 2 && parsed.result) {
const data = JSON.parse(parsed.result.content[0].text);
console.log('\n📊 Code Analysis:');
console.log(`- Components found: ${data.summary.totalComponents}`);
console.log(`- Imports found: ${data.summary.totalImports}`);
console.log(`- Has React: ${data.summary.hasReact}`);
console.log(`- Has Tailwind: ${data.summary.hasTailwind}`);
console.log(`- Insertion points: ${data.summary.insertionPoints}`);
} else if (parsed.id === 3 && parsed.result) {
const data = JSON.parse(parsed.result.content[0].text);
if (data.success) {
console.log('\n✅ Component inserted successfully!');
console.log('Modified code preview:');
console.log('```javascript');
console.log(data.modifiedCode.substring(0, 500) + '...');
console.log('```');
}
} else if (parsed.id === 4 && parsed.result) {
const data = JSON.parse(parsed.result.content[0].text);
if (data.success) {
console.log('\n✅ New component file created!');
console.log(`Component: ${data.componentName}`);
console.log('Generated code preview:');
console.log('```javascript');
console.log(data.code.substring(0, 300) + '...');
console.log('```');
}
}
} catch (e) {
console.log(`Response ${index + 1}:`, response);
}
});
}
function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
demonstrateMCPTools().catch(console.error);