glm-mcp-copilot 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env node
2
+ // smoke.js -- quick offline + online sanity check. Run: npm run smoke
3
+ // Offline checks always run. The live API call runs only if a key is present.
4
+ import "./loadEnv.js";
5
+ import { isPeak, chinaHour, resolveModel, recommend, estimateCost } from "./router.js";
6
+ import { glmMessage, config } from "./glmClient.js";
7
+
8
+ console.log("=== GLM MCP smoke test ===");
9
+ console.log("base_url:", config.BASE_URL);
10
+ console.log("api_key_loaded:", config.hasKey);
11
+ console.log("china_hour:", chinaHour(), "peak_now:", isPeak());
12
+ console.log("auto model now:", resolveModel("auto"));
13
+ console.log(
14
+ "recommend(frontend/low):",
15
+ JSON.stringify(recommend({ taskType: "frontend", complexity: "low" }))
16
+ );
17
+ console.log(
18
+ "recommend(architecture/high):",
19
+ JSON.stringify(recommend({ taskType: "architecture", complexity: "high" }))
20
+ );
21
+ console.log("recommend(sensitive):", JSON.stringify(recommend({ sensitive: true })));
22
+ console.log("est cost glm-5.2 (1k in / 2k out):", "$" + estimateCost("glm-5.2", 1000, 2000));
23
+
24
+ if (!config.hasKey) {
25
+ console.log("\nNo API key -> skipping live call. Offline checks passed.");
26
+ process.exit(0);
27
+ }
28
+
29
+ console.log("\nLive call to GLM…");
30
+ try {
31
+ const { text, usage } = await glmMessage({
32
+ model: resolveModel("auto"),
33
+ messages: [{ role: "user", content: "Reply with exactly: GLM_OK" }],
34
+ maxTokens: 32,
35
+ });
36
+ console.log("response:", text);
37
+ console.log("usage:", JSON.stringify(usage));
38
+ console.log(text.includes("GLM_OK") ? "\n✅ Live call OK." : "\n⚠️ Live call returned unexpected text.");
39
+ } catch (e) {
40
+ console.error("\n❌ Live call failed:", e.message);
41
+ process.exit(1);
42
+ }
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env node
2
+ // install-copilot.mjs — set up GLM as a delegate for GitHub Copilot / Copilot Chat (VS Code agent mode).
3
+ // Installs the shared GLM MCP server, registers it in the workspace .vscode/mcp.json (VS Code's
4
+ // "servers" format), and writes .github/copilot-instructions.md so Copilot delegates to GLM.
5
+ // It does NOT touch any Claude Code setup.
6
+ //
7
+ // Usage:
8
+ // node install-copilot.mjs --key YOUR_ZAI_KEY # set up in the current workspace
9
+ // node install-copilot.mjs --workspace PATH # target another project folder
10
+ // node install-copilot.mjs --server-dir PATH # where to install the server (default ~/.glm-mcp)
11
+ // node install-copilot.mjs --skip-npm
12
+
13
+ import { cpSync, mkdirSync, existsSync, readFileSync, writeFileSync, copyFileSync } from "node:fs";
14
+ import { homedir } from "node:os";
15
+ import { join, dirname } from "node:path";
16
+ import { fileURLToPath } from "node:url";
17
+ import { execSync } from "node:child_process";
18
+
19
+ const SELF = dirname(fileURLToPath(import.meta.url));
20
+ const args = process.argv.slice(2);
21
+ const getFlag = (n) => args.includes(n);
22
+ const getOpt = (n) => { const i = args.indexOf(n); return i >= 0 ? args[i + 1] : undefined; };
23
+
24
+ const SERVER_HOME = getOpt("--server-dir") || join(homedir(), ".glm-mcp");
25
+ const WORKSPACE = getOpt("--workspace") || process.cwd();
26
+ const KEY = getOpt("--key") || process.env.GLM_API_KEY || "";
27
+ const SKIP_NPM = getFlag("--skip-npm");
28
+
29
+ const log = (s) => console.log(s);
30
+ const step = (s) => console.log("\n→ " + s);
31
+
32
+ log("GLM-for-Copilot installer");
33
+ log(" server : " + join(SERVER_HOME, "glm-mcp"));
34
+ log(" workspace : " + WORKSPACE);
35
+
36
+ // 1. Install the shared MCP server (skip node_modules/.env/usage.jsonl).
37
+ step("Installing the GLM MCP server");
38
+ mkdirSync(SERVER_HOME, { recursive: true });
39
+ cpSync(join(SELF, "glm-mcp"), join(SERVER_HOME, "glm-mcp"), {
40
+ recursive: true,
41
+ filter: (src) => {
42
+ const b = src.split(/[\\/]/).pop();
43
+ return b !== "node_modules" && b !== ".env" && b !== "usage.jsonl";
44
+ },
45
+ });
46
+
47
+ // 2. .env (API key)
48
+ step("Setting up .env");
49
+ const envPath = join(SERVER_HOME, "glm-mcp", ".env");
50
+ if (!existsSync(envPath)) copyFileSync(join(SERVER_HOME, "glm-mcp", ".env.example"), envPath);
51
+ if (KEY) {
52
+ let env = readFileSync(envPath, "utf8");
53
+ env = /^GLM_API_KEY=/m.test(env) ? env.replace(/^GLM_API_KEY=.*$/m, "GLM_API_KEY=" + KEY) : "GLM_API_KEY=" + KEY + "\n" + env;
54
+ writeFileSync(envPath, env);
55
+ log(" wrote GLM_API_KEY into .env");
56
+ } else if (!/^GLM_API_KEY=\S+/m.test(readFileSync(envPath, "utf8"))) {
57
+ log(" ⚠ No API key yet — edit " + envPath + " and set GLM_API_KEY=... (a Z.ai GLM Coding Plan key).");
58
+ }
59
+
60
+ // 3. npm install
61
+ if (!SKIP_NPM) {
62
+ step("Installing dependencies (npm install)");
63
+ execSync("npm install --no-audit --no-fund", { cwd: join(SERVER_HOME, "glm-mcp"), stdio: "inherit" });
64
+ }
65
+
66
+ // 4. Register the server in VS Code (workspace .vscode/mcp.json). VS Code uses the "servers" key.
67
+ step("Registering the glm server in VS Code (.vscode/mcp.json)");
68
+ const idx = join(SERVER_HOME, "glm-mcp", "src", "index.js").replace(/\\/g, "/");
69
+ const vscodeDir = join(WORKSPACE, ".vscode");
70
+ mkdirSync(vscodeDir, { recursive: true });
71
+ const mcpPath = join(vscodeDir, "mcp.json");
72
+ let mcp = {};
73
+ if (existsSync(mcpPath)) {
74
+ try { mcp = JSON.parse(readFileSync(mcpPath, "utf8")); } catch { mcp = {}; }
75
+ writeFileSync(mcpPath + ".bak-" + Date.now(), readFileSync(mcpPath));
76
+ }
77
+ mcp.servers ||= {};
78
+ mcp.servers.glm = { type: "stdio", command: "node", args: [idx] };
79
+ writeFileSync(mcpPath, JSON.stringify(mcp, null, 2) + "\n");
80
+ log(" " + mcpPath);
81
+
82
+ // 5. Copilot instructions (workspace .github/copilot-instructions.md).
83
+ step("Adding delegation policy (.github/copilot-instructions.md)");
84
+ const ghDir = join(WORKSPACE, ".github");
85
+ mkdirSync(ghDir, { recursive: true });
86
+ const ciPath = join(ghDir, "copilot-instructions.md");
87
+ const policy = readFileSync(join(SELF, "copilot-instructions.md"), "utf8");
88
+ const existing = existsSync(ciPath) ? readFileSync(ciPath, "utf8") : "";
89
+ if (!existing.includes("glm_agent")) {
90
+ writeFileSync(ciPath, existing + (existing ? "\n\n" : "") + policy);
91
+ log(" " + ciPath);
92
+ } else {
93
+ log(" policy already present (left as-is)");
94
+ }
95
+
96
+ log("\n✅ Done. Next steps:");
97
+ log(" 1. Ensure GLM_API_KEY is set in " + envPath);
98
+ log(" 2. In VS Code: Reload Window, open Copilot Chat, switch to Agent mode.");
99
+ log(" 3. Start the 'glm' server: run 'MCP: List Servers' (or VS Code will offer to start it).");
100
+ log(" 4. Ask Copilot to do a coding task — it will call glm_agent. Run glm_status for the GLM usage ledger.");
101
+ log("\nNote: this sets up the CURRENT workspace. For all projects, run 'MCP: Open User Configuration' in");
102
+ log("VS Code and add the same 'glm' server block (see mcp.json.example).");
@@ -0,0 +1,9 @@
1
+ {
2
+ "servers": {
3
+ "glm": {
4
+ "type": "stdio",
5
+ "command": "node",
6
+ "args": ["${userHome}/.glm-mcp/glm-mcp/src/index.js"]
7
+ }
8
+ }
9
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "glm-mcp-copilot",
3
+ "version": "1.0.0",
4
+ "description": "GLM (Zhipu/Z.ai) as a cheap delegate for GitHub Copilot / Copilot Chat in VS Code — the same GLM MCP tools (glm_agent/glm_delegate/glm_recommend/glm_status) wired into VS Code agent mode.",
5
+ "type": "module",
6
+ "bin": {
7
+ "glm-mcp-copilot": "install-copilot.mjs"
8
+ },
9
+ "files": [
10
+ "glm-mcp/",
11
+ "install-copilot.mjs",
12
+ "copilot-instructions.md",
13
+ "mcp.json.example",
14
+ "README.md",
15
+ "LICENSE"
16
+ ],
17
+ "engines": {
18
+ "node": ">=18"
19
+ },
20
+ "keywords": [
21
+ "github-copilot",
22
+ "copilot",
23
+ "copilot-chat",
24
+ "vscode",
25
+ "mcp",
26
+ "model-context-protocol",
27
+ "glm",
28
+ "zhipu",
29
+ "z.ai",
30
+ "cost-optimization"
31
+ ],
32
+ "author": "djerok",
33
+ "license": "MIT",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/djerok/glm-mcp.git",
37
+ "directory": "copilot"
38
+ },
39
+ "bugs": {
40
+ "url": "https://github.com/djerok/glm-mcp/issues"
41
+ },
42
+ "homepage": "https://github.com/djerok/glm-mcp/tree/main/copilot#readme"
43
+ }