forked from sendaifun/solana-new
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive-mcps.ts
More file actions
307 lines (258 loc) · 7.83 KB
/
Copy pathinteractive-mcps.ts
File metadata and controls
307 lines (258 loc) · 7.83 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import { normalizeAgentCommand } from "./agent-cli.js";
const RESET = "\x1b[0m";
const DIM = "\x1b[2m";
const BOLD = "\x1b[1m";
const CYAN = "\x1b[36m";
const YELLOW = "\x1b[33m";
const MAGENTA = "\x1b[35m";
const MAX_VISIBLE = 15;
function gradientSearch(): string {
const chars = "Search";
const codes = [
"\x1b[38;2;130;80;255m",
"\x1b[38;2;160;60;240m",
"\x1b[38;2;190;50;220m",
"\x1b[38;2;215;40;190m",
"\x1b[38;2;240;35;155m",
"\x1b[38;2;255;30;130m",
];
return chars.split("").map((c, i) => `${codes[i]}${c}`).join("") + RESET;
}
export interface McpItem {
id: string;
name: string;
repo: string;
description: string;
category: string;
setup_command: string;
url: string;
keywords: string[];
}
export interface McpsData {
mcps: McpItem[];
}
export function buildMcpsIndex(data: McpsData): McpItem[] {
return data.mcps.map((m) => ({
...m,
setup_command: normalizeAgentCommand(m.setup_command),
keywords: [
m.id,
...m.id.split("-"),
...m.name.toLowerCase().split(/\s+/),
...m.description.toLowerCase().split(/\s+/),
m.category,
...m.category.split("-"),
m.repo,
...m.keywords,
],
}));
}
function filterItems(items: McpItem[], query: string): McpItem[] {
if (!query.trim()) return items;
const words = query.toLowerCase().split(/\s+/).filter(Boolean);
return items.filter((item) =>
words.every((w) => item.keywords.some((kw) => kw.includes(w))),
);
}
export function searchMcps(items: McpItem[], query: string): McpItem[] {
return filterItems(items, query);
}
function visibleLength(str: string): number {
return str.replace(/\x1b\[[0-9;]*m/g, "").length;
}
function padVisible(str: string, width: number): string {
const vis = visibleLength(str);
const needed = Math.max(width - vis, 0);
return str + " ".repeat(needed);
}
interface BuildResult {
lines: string[];
inputLineIndex: number;
}
function buildLines(
query: string,
items: McpItem[],
selected: number,
rows: number,
showDescriptions: boolean,
): BuildResult {
const lines: string[] = [];
const W = 62;
lines.push(` ${BOLD}${CYAN}solana.new${RESET} ${DIM}Solana MCP Servers${RESET}`);
lines.push("");
const topBorder = ` \u256D${"\u2500".repeat(W + 2)}\u256E`;
const botBorder = ` \u2570${"\u2500".repeat(W + 2)}\u256F`;
let innerContent: string;
if (query) {
innerContent = `${BOLD}>${RESET} ${query}`;
} else {
innerContent = `${BOLD}>${RESET} ${DIM}search anything across defi, tooling, infra, development${RESET}`;
}
const innerLine = ` \u2502 ${padVisible(innerContent, W)} \u2502`;
lines.push(topBorder);
const inputLineIndex = lines.length;
lines.push(innerLine);
lines.push(botBorder);
const descToggle = showDescriptions ? "D:hide desc" : "D:show desc";
lines.push(` ${gradientSearch()} ${DIM}${items.length} server${items.length === 1 ? "" : "s"}${RESET} ${DIM}${descToggle}${RESET}`);
lines.push("");
const total = items.length;
if (total === 0) {
lines.push(` ${DIM}No servers${query ? ` matching "${query}"` : ""}${RESET}`);
} else {
let start = 0;
if (total > MAX_VISIBLE) {
start = Math.max(0, Math.min(selected - Math.floor(MAX_VISIBLE / 2), total - MAX_VISIBLE));
}
const end = Math.min(start + MAX_VISIBLE, total);
if (start > 0) {
lines.push(` ${DIM} \u25B2 ${start} more${RESET}`);
}
for (let i = start; i < end; i++) {
const item = items[i];
const isSelected = i === selected;
const pointer = isSelected ? `${CYAN}\u276F${RESET}` : " ";
const repoTag = `${YELLOW}(${item.repo})${RESET}`;
const catTag = ` ${DIM}${item.category}${RESET}`;
const nameColor = isSelected ? BOLD + CYAN : BOLD;
lines.push(` ${pointer} ${nameColor}${item.name}${RESET} ${repoTag}${catTag}`);
if (showDescriptions || isSelected) {
lines.push(` ${DIM}${item.description}${RESET}`);
}
if (isSelected) {
lines.push(` ${MAGENTA}$ ${item.setup_command}${RESET}`);
}
lines.push(""); // spacing between items
}
if (end < total) {
lines.push(` ${DIM} \u25BC ${total - end} more${RESET}`);
}
}
const footerLines: string[] = [""];
if (items.length > 0) {
const sel = items[selected];
footerLines.push(` ${DIM}\u2191\u2193 scroll${RESET} ${BOLD}enter${RESET} ${DIM}install${RESET} ${DIM}esc quit${RESET} ${MAGENTA}\u25B8 ${sel.id}${RESET}`);
} else {
footerLines.push(` ${DIM}esc quit${RESET}`);
}
while (lines.length < rows - footerLines.length) {
lines.push("");
}
lines.push(...footerLines);
return { lines: lines.slice(0, rows), inputLineIndex };
}
export interface InteractiveMcpResult {
item: McpItem | null;
action: "select" | "quit";
}
export async function interactiveMcps(
data: McpsData,
): Promise<InteractiveMcpResult> {
const allItems = buildMcpsIndex(data);
let query = "";
let selected = 0;
let filtered = filterItems(allItems, query);
let showDescriptions = false;
const stdin = process.stdin;
const stdout = process.stdout;
if (!stdin.isTTY) {
for (const item of allItems) {
console.log(` ${item.id} (${item.repo}) ${item.description}`);
}
return { item: null, action: "quit" };
}
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding("utf8");
stdout.write("\x1b[?1049h");
stdout.write("\x1b[?25h");
function getRows(): number {
return stdout.rows || 24;
}
function draw() {
const rows = getRows();
const { lines, inputLineIndex } = buildLines(query, filtered, selected, rows, showDescriptions);
const cursorPos = `\x1b[${inputLineIndex + 1};${7 + query.length}H`;
stdout.write(`\x1b[H\x1b[J${lines.join("\n")}${cursorPos}`);
}
draw();
const onResize = () => draw();
stdout.on("resize", onResize);
return new Promise((resolve) => {
function cleanup() {
stdout.removeListener("resize", onResize);
stdin.setRawMode(false);
stdin.pause();
stdin.removeListener("data", onData);
stdout.write("\x1b[?1049l");
}
function onData(key: string) {
// Ctrl+C
if (key === "\x03") {
cleanup();
process.exit(0);
}
// Bare escape (not part of arrow sequence)
if (key === "\x1b") {
cleanup();
resolve({ item: null, action: "quit" });
return;
}
// Enter
if (key === "\r" || key === "\n") {
const item = filtered[selected] ?? null;
cleanup();
resolve({ item, action: "select" });
return;
}
// Cmd+Backspace / Ctrl+U — clear entire line
if (key === "\x15" || key === "\x17") {
query = "";
filtered = filterItems(allItems, query);
selected = 0;
draw();
return;
}
// Backspace
if (key === "\x7f" || key === "\b") {
query = query.slice(0, -1);
filtered = filterItems(allItems, query);
selected = Math.min(selected, Math.max(filtered.length - 1, 0));
draw();
return;
}
// Arrow up
if (key === "\x1b[A") {
selected = Math.max(selected - 1, 0);
draw();
return;
}
// Arrow down
if (key === "\x1b[B") {
selected = Math.min(selected + 1, Math.max(filtered.length - 1, 0));
draw();
return;
}
// Tab
if (key === "\t") {
selected = (selected + 1) % Math.max(filtered.length, 1);
draw();
return;
}
// Toggle descriptions with 'd' only when search is empty
if (key === "D") {
showDescriptions = !showDescriptions;
draw();
return;
}
// Printable characters
if (key.length === 1 && key >= " ") {
query += key;
filtered = filterItems(allItems, query);
selected = 0;
draw();
}
}
stdin.on("data", onData);
});
}