-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.mjs
More file actions
31 lines (28 loc) · 1.35 KB
/
Copy pathcli.mjs
File metadata and controls
31 lines (28 loc) · 1.35 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
// web-mcp CLI 包装:供 Python/脚本调用(stdio JSON 输出)
// node search-cli.mjs "查询词" [条数] [引擎,逗号分隔] → {"results": [...], "failures": [...]}
// node fetch-cli.mjs "URL" [text|readable|json] [maxChars] → {"content": "...", "title": "..."}
import { webSearch } from "./src/search.mjs";
import { fetchPage } from "./src/fetch.mjs";
const [,, cmd, arg1, arg2, arg3] = process.argv;
async function main() {
if (cmd === "search") {
const query = arg1 || "";
const n = parseInt(arg2 || "5", 10);
const engines = arg3 ? arg3.split(",").map((s) => s.trim()).filter(Boolean) : undefined;
if (!query) { console.log(JSON.stringify({ error: "no query" })); return; }
const res = await webSearch(query, { maxResults: n, engines });
console.log(JSON.stringify(res));
} else if (cmd === "fetch") {
const url = arg1 || "";
const mode = arg2 || "readable";
const maxChars = parseInt(arg3 || "6000", 10);
if (!url) { console.log(JSON.stringify({ error: "no url" })); return; }
const res = await fetchPage(url, { mode, maxChars });
console.log(JSON.stringify(res));
} else {
console.log(JSON.stringify({ error: "usage: search-cli.mjs search|fetch ..." }));
}
}
main()
.then(() => process.exit(0))
.catch((e) => { console.log(JSON.stringify({ error: String(e) })); process.exit(1); });