-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_mgr.js
More file actions
146 lines (127 loc) · 4.03 KB
/
script_mgr.js
File metadata and controls
146 lines (127 loc) · 4.03 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
const fs = require("node:fs/promises");
const path = require("node:path");
const { Client } = require("@modelcontextprotocol/sdk/client/index.js");
const { SSEClientTransport } = require("@modelcontextprotocol/sdk/client/sse.js");
const { CallToolResultSchema } = require("@modelcontextprotocol/sdk/types.js");
const SERVER_URL = "http://localhost:6767/sse";
function usage() {
return [
'Usage:',
' node script_mgr.js list',
' node script_mgr.js add --title "<title>" --description "<description>" --file "<path>"',
' node script_mgr.js save --title "<title>" [--out "<path>"]',
].join("\n");
}
function getTextContent(result) {
return (result.content || [])
.filter((item) => item && item.type === "text" && typeof item.text === "string")
.map((item) => item.text)
.join("\n");
}
function parseFlags(args) {
const flags = {};
for (let i = 0; i < args.length; i += 1) {
const part = args[i];
if (!part.startsWith("--")) {
continue;
}
const key = part.slice(2);
const value = args[i + 1];
if (!value || value.startsWith("--")) {
throw new Error(`Missing value for --${key}`);
}
flags[key] = value;
i += 1;
}
return flags;
}
function sanitizeTitleToFilename(title) {
const sanitized = title
.replace(/[<>:"/\\|?*\x00-\x1F]/g, "_")
.replace(/\s+/g, " ")
.trim()
.replace(/[. ]+$/g, "");
const base = sanitized || "script";
return base.toLowerCase().replace(/\s+/g, "-") + ".js";
}
async function callTool(client, name, args) {
return client.request(
{
method: "tools/call",
params: {
name,
arguments: args,
},
},
CallToolResultSchema
);
}
async function run() {
const [command, ...rest] = process.argv.slice(2);
if (!command) {
throw new Error(usage());
}
const client = new Client({ name: "script-mgr-cli", version: "1.0.0" });
const transport = new SSEClientTransport(new URL(SERVER_URL));
try {
await client.connect(transport);
if (command === "list") {
const result = await callTool(client, "list_library_scripts", {});
const text = getTextContent(result);
if (text) {
console.log(text);
} else {
console.log(JSON.stringify(result, null, 2));
}
return;
}
const flags = parseFlags(rest);
if (command === "add") {
const title = (flags.title || "").trim();
const description = (flags.description || "").trim();
const filePath = (flags.file || "").trim();
if (!title || !description || !filePath) {
throw new Error(`Missing required flags for add.\n${usage()}`);
}
const code = await fs.readFile(path.resolve(process.cwd(), filePath), "utf8");
const result = await callTool(client, "save_script_to_library", {
title,
description,
code,
});
const text = getTextContent(result);
console.log(text || JSON.stringify(result, null, 2));
return;
}
if (command === "save") {
const title = (flags.title || "").trim();
if (!title) {
throw new Error(`Missing required flag --title for save.\n${usage()}`);
}
const result = await callTool(client, "read_library_script", { title });
const scriptText = getTextContent(result);
if (!scriptText) {
throw new Error(`No script content returned for "${title}".`);
}
const outputPath = flags.out
? path.resolve(process.cwd(), flags.out)
: path.resolve(process.cwd(), sanitizeTitleToFilename(title));
await fs.mkdir(path.dirname(outputPath), { recursive: true });
await fs.writeFile(outputPath, scriptText, "utf8");
console.log(`Saved script "${title}" to ${outputPath}`);
return;
}
throw new Error(`Unknown command "${command}".\n${usage()}`);
} finally {
try {
await transport.close();
} catch (_) {
// Ignore close errors.
}
}
}
run().catch((error) => {
const message = error instanceof Error ? error.message : String(error);
console.error(message);
process.exitCode = 1;
});