-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathindex.js
More file actions
256 lines (219 loc) · 6.29 KB
/
Copy pathindex.js
File metadata and controls
256 lines (219 loc) · 6.29 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import fs from "fs";
import path from "path";
import { z } from "zod";
const COMPONENTS_DIR = path.join(
path.dirname(new URL(import.meta.url).pathname),
"src/components/"
);
const latestPrompt = `You are a React and React Native expert. Generate COMPLETE and RUNNABLE code using only my design system components and tools sequentially: get_all_components_metadata, select_components, get_selected_components_docs. Requirements: no external component libraries, no HTML tags (<div>, <button>, <input>, etc), no StyleSheet, use TailwindCSS classes via className prop. Images must be from unsplash.com only. Import all components individually. Prefer VStack/HStack over Box component. Ensure screens are scrollable, responsive, and mobile-friendly.`;
// Initializes an MCP server
const server = new McpServer({
name: "use-gluestack-components",
version: "1.0.0",
systemPrompt: latestPrompt,
});
function getAvailableComponents() {
try {
// Get all markdown files in the components directory
const componentFiles = fs
.readdirSync(COMPONENTS_DIR)
.filter((file) => file.endsWith(".md"))
.map((file) => file.replace(".md", ""));
return componentFiles;
return {
content: [
{
type: "text",
text: JSON.stringify(componentFiles),
},
],
};
} catch (error) {
console.error(`Error reading components directory: ${error.message}`);
return [];
return {
content: [
{
type: "text",
text: JSON.stringify([]),
},
],
};
}
}
function getComponentMetadata(componentName) {
try {
const docPath = path.join(
COMPONENTS_DIR,
`${componentName.toLowerCase()}.md`
);
if (!fs.existsSync(docPath)) {
return { title: componentName, description: "Component not found" };
}
const docsContent = fs.readFileSync(docPath, "utf-8");
const lines = docsContent.split("\n");
// Check if file starts with frontmatter
if (lines[0].trim() !== "---") {
return { title: componentName, description: "No description available" };
}
// Extract only title and description
const metadata = {
title: componentName,
description: "No description available",
};
// Read until the closing frontmatter delimiter
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (line === "---") break;
if (line.startsWith("title:")) {
metadata.title = line.split(":")[1].trim();
} else if (line.startsWith("description:")) {
metadata.description = line.split(":")[1].trim();
}
}
return metadata;
return {
content: [
{
type: "text",
text: JSON.stringify(metadata),
},
],
};
} catch (error) {
console.error(
`Error reading metadata for ${componentName}: ${error.message}`
);
return {
title: componentName,
description: "Error reading metadata",
};
return {
content: [
{
type: "text",
text: JSON.stringify({
title: componentName,
description: "Error reading metadata",
}),
},
],
};
}
}
function getAllComponentsMetadata() {
const components = getAvailableComponents();
const metadata = {};
components.forEach((component) => {
const meta = getComponentMetadata(component);
if (meta) {
metadata[component] = meta;
}
});
return {
content: [
{
type: "text",
text: JSON.stringify(metadata, null, 2),
},
],
};
}
function getComponentDocs(componentName) {
try {
const docPath = path.join(
COMPONENTS_DIR,
`${componentName.toLowerCase()}.md`
);
// Check if the file exists
if (!fs.existsSync(docPath)) {
return `Documentation not found for component: ${componentName}`;
}
// Read the markdown file
const docsContent = fs.readFileSync(docPath, "utf-8");
return (
docsContent || `Empty documentation file for component: ${componentName}`
);
return {
content: [
{
type: "text",
text:
docsContent ||
`Empty documentation file for component: ${componentName}`,
},
],
};
} catch (error) {
return `Error retrieving documentation for ${componentName}: ${error.message}`;
}
}
function getSelectedComponentsDocs(componentNames) {
const docsObject = {};
console.log(
`✅ Getting documentation for components: ${componentNames.join(", ")}`
);
for (const componentName of componentNames) {
docsObject[componentName] = getComponentDocs(componentName);
}
return {
content: [
{
type: "text",
text: JSON.stringify(docsObject, null, 2),
},
],
};
}
server.tool(
"get_all_components_metadata",
"Read and gives the metadata of all the components",
{},
() => getAllComponentsMetadata()
);
server.tool(
"select_components",
"Selects the components you need",
{
selectedComponents: z
.array(z.string())
.describe("The names of the components"),
},
(input) => {
console.log(
`✅ Selected components: ${input.selectedComponents.join(", ")}`
);
return {
content: [
{
type: "text",
text: `You have selected: ${input.selectedComponents.join(
", "
)}. Now proceed to get full documentation for ALL these components at once using get_selected_components_docs.`,
},
],
};
}
);
server.tool(
"get_selected_components_docs",
"Read and gives the complete documentation of selected components",
{
component_names: z
.array(z.string())
.describe("The names of the components"),
},
(input) => getSelectedComponentsDocs(input.component_names)
);
// Sets up the MCP server and establishes communication channel with the client using stdio transport
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.log("Use Gluestack Components MCP Server running on stdio");
}
main().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});