glm-mcp-copilot 1.0.0 → 1.2.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.
package/README.md CHANGED
@@ -4,7 +4,7 @@ Use the **GLM** model (Zhipu / Z.ai) as a **~10× cheaper delegate** inside **Gi
4
4
  Chat** (VS Code agent mode). It's the **same GLM MCP server** used by the Claude Code version — Copilot
5
5
  calls `glm_agent` / `glm_delegate` / `glm_recommend` / `glm_status` to offload work to GLM.
6
6
 
7
- > Sibling package: **[glm-mcp-claude](../README.md)** (the Claude Code version). Same server, different host.
7
+ > Sibling package: **[glm-mcp-claude](../claude/README.md)** (the Claude Code version). Same server, different host.
8
8
 
9
9
  ## What you get
10
10
  - The **glm MCP server** registered in VS Code (agent mode) — tools:
@@ -12,7 +12,9 @@ calls `glm_agent` / `glm_delegate` / `glm_recommend` / `glm_status` to offload w
12
12
  - **`glm_delegate`** — GLM drafts text you place.
13
13
  - **`glm_recommend`** — free advisory: GLM vs the default model.
14
14
  - **`glm_status`** — usage ledger (proof of GLM tokens spent) + config.
15
- - A **`.github/copilot-instructions.md`** delegation policy so Copilot offloads to GLM automatically.
15
+ - A **`GLM` custom agent (subagent)** — restricted to the `glm` tools, so it *must* delegate to GLM
16
+ (the Copilot analog of the Claude `glm` subagent). Pick it from the chat mode dropdown or hand off to it.
17
+ - A **delegation-policy instructions file** so Copilot offloads to GLM automatically.
16
18
 
17
19
  ## Prerequisites
18
20
  - **VS Code** with **GitHub Copilot + Copilot Chat**, and **Agent mode** available (MCP support).
@@ -32,16 +34,37 @@ Run it **from your project folder** (it sets up that workspace). It:
32
34
  1. installs the GLM MCP server to `~/.glm-mcp/glm-mcp/` and runs `npm install`,
33
35
  2. writes your key to that server's `.env`,
34
36
  3. registers the server in `.vscode/mcp.json` (VS Code's `servers` format),
35
- 4. writes `.github/copilot-instructions.md` (the delegation policy).
37
+ 4. installs the **`GLM` custom agent** → `.github/agents/glm.agent.md`,
38
+ 5. writes `.github/copilot-instructions.md` (the delegation policy).
39
+
40
+ ### Global (all projects)
41
+ Set it up once for **every** workspace with `--global`:
42
+ ```bash
43
+ npx glm-mcp-copilot --global --key YOUR_ZAI_API_KEY
44
+ ```
45
+ Global mode writes to VS Code's **user config** instead of one workspace:
46
+ - the `glm` server → the **user `mcp.json`** (all workspaces),
47
+ - the **`GLM` custom agent** → `~/.copilot/agents/glm.agent.md`,
48
+ - the delegation policy → `~/.copilot/instructions/glm.instructions.md` (with `applyTo: '**'`),
49
+ - and it registers those locations + enables agent mode in **user `settings.json`**
50
+ (`chat.agentFilesLocations`, `chat.instructionsFilesLocations`, `chat.agent.enabled`).
51
+
52
+ > Uses the current (non-deprecated) instructions mechanism — `.instructions.md` files, **not** the old
53
+ > `codeGeneration.instructions` settings array (deprecated in VS Code 1.102; the installer migrates off it).
54
+ > Use `--vscode-user-dir PATH` if your VS Code User folder isn't auto-detected (Insiders/VSCodium/portable).
36
55
 
37
56
  Then in VS Code: **Reload Window → open Copilot Chat → Agent mode → start the `glm` server** (`MCP: List
38
57
  Servers`). Ask Copilot to do a coding task; it will call `glm_agent`.
39
58
 
40
59
  ## How it differs from the Claude Code version
41
- Copilot doesn't have Claude Code's *subagents* or *PreToolUse hooks*, so there's no auto-routing hook or
42
- `glm` subagent. Instead:
43
- - **MCP tools** (`glm_*`) are available in **agent mode** and Copilot calls them.
44
- - **`.github/copilot-instructions.md`** steers Copilot to delegate to GLM (the CLAUDE.md equivalent).
60
+ Near-parity — Copilot **does** have subagents (custom agents):
61
+ - **`glm_*` MCP tools** in **agent mode** (same server as the Claude edition).
62
+ - A **`GLM` custom agent (subagent)** restricted to the `glm` tools the analog of the Claude `glm`
63
+ subagent (forced to delegate to GLM). Invoke it from the mode dropdown or via an agent handoff.
64
+ - **Instructions files** steer delegation (the CLAUDE.md equivalent).
65
+
66
+ The only thing Copilot lacks is Claude Code's **PreToolUse hook** (auto-routing on every subagent
67
+ spawn) — so in Copilot, delegation is driven by the subagent + instructions rather than a hook.
45
68
 
46
69
  Everything else — the GLM agent loop, peak-aware model pick, cost bias, token cap, usage ledger,
47
70
  `dry_run` oversight — is the **same server**, so it behaves identically once a tool is called.
package/glm.agent.md ADDED
@@ -0,0 +1,22 @@
1
+ ---
2
+ name: GLM
3
+ description: Cheap GLM delegate — offloads coding/edit/refactor/test/docs work to the GLM model (~10x cheaper than the default) via the glm MCP server. Use for well-specified, self-contained tasks; keep sensitive/vision/parallel/huge-context/heavy-tool-loop work on the default model.
4
+ tools: ['glm/*']
5
+ user-invocable: true
6
+ ---
7
+
8
+ You are the **GLM delegate**. You have ONLY the `glm` MCP server's tools — **you cannot edit files or
9
+ run commands yourself**, so you must route all real work through GLM (this keeps the work, and the
10
+ tokens, on GLM — ~10× cheaper than the default model).
11
+
12
+ ## How you work
13
+ 1. **Do the work via GLM.** For any coding / edit / refactor / test / docs task, call **`glm/glm_agent`**
14
+ with the goal + the **absolute repo path** (`workdir`). GLM reads, writes, edits, and runs the files
15
+ itself, end-to-end. For pure text, use **`glm/glm_delegate`**.
16
+ 2. **Check fit when unsure.** Call **`glm/glm_recommend`**; if it says the task should stay on the
17
+ default model (sensitive, vision, parallel, >128K context, heavy dependent tool-loops), say so and stop.
18
+ 3. **Report the GLM stats.** Surface the `=== GLM STATS ===` block `glm_agent` returns (model + tokens
19
+ delegated + cost) so it's clear GLM did the work and how much it spent. Run **`glm/glm_status`** for
20
+ the cumulative usage ledger.
21
+
22
+ Never claim to have done work you didn't route through GLM — you have no other tools.
@@ -0,0 +1,18 @@
1
+ ---
2
+ applyTo: '**'
3
+ ---
4
+
5
+ # GLM delegation
6
+
7
+ The **glm** MCP server is available in agent mode (`glm_agent`, `glm_delegate`, `glm_recommend`,
8
+ `glm_status`). GLM (Zhipu/Z.ai) is **~10× cheaper** than the default model — offload work to it.
9
+
10
+ - For any implementation / codegen / edit / refactor / test / docs task, call **`glm_agent`** with the
11
+ goal + the absolute repo path (`workdir`) so GLM does it end-to-end on **GLM tokens** — or hand it to
12
+ the **GLM** custom agent (subagent). Prefer this over doing the file work yourself.
13
+ - Use **`glm_delegate`** for pure text.
14
+ - **To actually spend GLM tokens (not the default model's), call the glm tools — don't do the work
15
+ inline yourself.**
16
+ - Keep on the default model: sensitive/secret code, vision/images, parallel work, very large context
17
+ (>128K), and heavy dependent tool-loops. Unsure? call **`glm_recommend`**.
18
+ - `glm_status` shows the GLM usage ledger — proof of GLM tokens spent.
@@ -6,8 +6,10 @@
6
6
  //
7
7
  // Usage:
8
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
9
+ // node install-copilot.mjs --global --key YOUR_ZAI_KEY # set up for ALL workspaces (VS Code user config)
10
+ // node install-copilot.mjs --workspace PATH # target another project folder (workspace mode)
10
11
  // node install-copilot.mjs --server-dir PATH # where to install the server (default ~/.glm-mcp)
12
+ // node install-copilot.mjs --vscode-user-dir PATH # override VS Code User dir (for --global)
11
13
  // node install-copilot.mjs --skip-npm
12
14
 
13
15
  import { cpSync, mkdirSync, existsSync, readFileSync, writeFileSync, copyFileSync } from "node:fs";
@@ -25,13 +27,30 @@ const SERVER_HOME = getOpt("--server-dir") || join(homedir(), ".glm-mcp");
25
27
  const WORKSPACE = getOpt("--workspace") || process.cwd();
26
28
  const KEY = getOpt("--key") || process.env.GLM_API_KEY || "";
27
29
  const SKIP_NPM = getFlag("--skip-npm");
30
+ const GLOBAL = getFlag("--global");
28
31
 
29
32
  const log = (s) => console.log(s);
30
33
  const step = (s) => console.log("\n→ " + s);
31
34
 
32
- log("GLM-for-Copilot installer");
35
+ // Resolve the VS Code User config dir (for --global). Prefers stable "Code", falls back to Insiders.
36
+ function vscodeUserDir() {
37
+ const override = getOpt("--vscode-user-dir");
38
+ if (override) return override;
39
+ const home = homedir();
40
+ const roots =
41
+ process.platform === "win32"
42
+ ? [process.env.APPDATA || join(home, "AppData", "Roaming")]
43
+ : process.platform === "darwin"
44
+ ? [join(home, "Library", "Application Support")]
45
+ : [join(home, ".config")];
46
+ const cands = [];
47
+ for (const r of roots) for (const app of ["Code", "Code - Insiders", "VSCodium"]) cands.push(join(r, app, "User"));
48
+ return cands.find(existsSync) || cands[0];
49
+ }
50
+
51
+ log("GLM-for-Copilot installer" + (GLOBAL ? " (GLOBAL — all workspaces)" : ""));
33
52
  log(" server : " + join(SERVER_HOME, "glm-mcp"));
34
- log(" workspace : " + WORKSPACE);
53
+ log(GLOBAL ? " scope : all VS Code workspaces (user config)" : " workspace : " + WORKSPACE);
35
54
 
36
55
  // 1. Install the shared MCP server (skip node_modules/.env/usage.jsonl).
37
56
  step("Installing the GLM MCP server");
@@ -63,40 +82,91 @@ if (!SKIP_NPM) {
63
82
  execSync("npm install --no-audit --no-fund", { cwd: join(SERVER_HOME, "glm-mcp"), stdio: "inherit" });
64
83
  }
65
84
 
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)");
85
+ // 4-6. Register the server, the `glm` custom agent (subagent), and the delegation policy
86
+ // globally (all workspaces) or in this workspace.
68
87
  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));
88
+ const AGENTS_HOME = join(homedir(), ".copilot", "agents"); // global custom-agent location
89
+ const INSTR_HOME = join(homedir(), ".copilot", "instructions"); // global instructions location
90
+
91
+ function mergeMcp(mcpPath) {
92
+ mkdirSync(dirname(mcpPath), { recursive: true });
93
+ let mcp = {};
94
+ if (existsSync(mcpPath)) {
95
+ try { mcp = JSON.parse(readFileSync(mcpPath, "utf8")); } catch { mcp = {}; }
96
+ writeFileSync(mcpPath + ".bak-" + Date.now(), readFileSync(mcpPath));
97
+ }
98
+ mcp.servers ||= {};
99
+ mcp.servers.glm = { type: "stdio", command: "node", args: [idx] };
100
+ writeFileSync(mcpPath, JSON.stringify(mcp, null, 2) + "\n");
101
+ return mcpPath;
76
102
  }
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);
103
+ function copyInto(dir, srcFile) {
104
+ mkdirSync(dir, { recursive: true });
105
+ const dest = join(dir, srcFile);
106
+ copyFileSync(join(SELF, srcFile), dest);
107
+ return dest;
108
+ }
109
+
110
+ if (GLOBAL) {
111
+ const userDir = vscodeUserDir();
112
+ step("Registering glm GLOBALLY (VS Code user mcp.json) -> " + userDir);
113
+ log(" " + mergeMcp(join(userDir, "mcp.json")));
114
+
115
+ step("Installing the GLM custom agent (subagent) -> " + AGENTS_HOME);
116
+ log(" " + copyInto(AGENTS_HOME, "glm.agent.md"));
117
+
118
+ step("Installing GLOBAL Copilot instructions -> " + INSTR_HOME);
119
+ log(" " + copyInto(INSTR_HOME, "glm.instructions.md"));
120
+
121
+ step("Updating VS Code user settings.json (locations + toggles)");
122
+ const setPath = join(userDir, "settings.json");
123
+ let settings = {};
124
+ if (existsSync(setPath)) {
125
+ try { settings = JSON.parse(readFileSync(setPath, "utf8")); } catch { settings = {}; }
126
+ writeFileSync(setPath + ".bak-" + Date.now(), readFileSync(setPath));
127
+ }
128
+ const agentsGlob = AGENTS_HOME.replace(/\\/g, "/");
129
+ const instrGlob = INSTR_HOME.replace(/\\/g, "/");
130
+ settings["chat.agentFilesLocations"] = { ...(settings["chat.agentFilesLocations"] || {}), [agentsGlob]: true };
131
+ settings["chat.instructionsFilesLocations"] = { ...(settings["chat.instructionsFilesLocations"] || {}), [instrGlob]: true };
132
+ settings["github.copilot.chat.codeGeneration.useInstructionFiles"] = true;
133
+ settings["chat.agent.enabled"] = true;
134
+ // Migrate off the deprecated inline-instructions setting (remove our old entry if present).
135
+ const DEP = "github.copilot.chat.codeGeneration.instructions";
136
+ if (Array.isArray(settings[DEP])) {
137
+ settings[DEP] = settings[DEP].filter((e) => !(e && typeof e.text === "string" && e.text.includes("glm_agent")));
138
+ if (settings[DEP].length === 0) delete settings[DEP];
139
+ }
140
+ writeFileSync(setPath, JSON.stringify(settings, null, 2) + "\n");
141
+ log(" " + setPath);
92
142
  } else {
93
- log(" policy already present (left as-is)");
143
+ step("Registering the glm server in VS Code (workspace .vscode/mcp.json)");
144
+ log(" " + mergeMcp(join(WORKSPACE, ".vscode", "mcp.json")));
145
+
146
+ step("Installing the GLM custom agent (subagent) -> .github/agents/");
147
+ log(" " + copyInto(join(WORKSPACE, ".github", "agents"), "glm.agent.md"));
148
+
149
+ step("Adding delegation policy (workspace .github/copilot-instructions.md)");
150
+ const ciPath = join(WORKSPACE, ".github", "copilot-instructions.md");
151
+ mkdirSync(dirname(ciPath), { recursive: true });
152
+ const policy = readFileSync(join(SELF, "copilot-instructions.md"), "utf8");
153
+ const existing = existsSync(ciPath) ? readFileSync(ciPath, "utf8") : "";
154
+ if (!existing.includes("glm_agent")) {
155
+ writeFileSync(ciPath, existing + (existing ? "\n\n" : "") + policy);
156
+ log(" " + ciPath);
157
+ } else {
158
+ log(" copilot-instructions.md already has the policy");
159
+ }
94
160
  }
95
161
 
96
162
  log("\n✅ Done. Next steps:");
97
163
  log(" 1. Ensure GLM_API_KEY is set in " + envPath);
98
164
  log(" 2. In VS Code: Reload Window, open Copilot Chat, switch to Agent mode.");
99
165
  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).");
166
+ log(" 4. Use it: pick the 'GLM' agent in the chat mode dropdown, or ask the main agent to");
167
+ log(" 'use glm_agent to …'. Run glm_status for the GLM usage ledger.");
168
+ log(
169
+ GLOBAL
170
+ ? "\nGLOBAL mode: the glm server, GLM subagent, and delegation policy now apply to ALL your VS Code workspaces."
171
+ : "\nWorkspace mode: current project only. Re-run with --global to apply to every project."
172
+ );
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
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.",
3
+ "version": "1.2.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) plus a GLM custom agent (subagent), wired into VS Code agent mode, installable globally.",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "glm-mcp-copilot": "install-copilot.mjs"
@@ -9,6 +9,8 @@
9
9
  "files": [
10
10
  "glm-mcp/",
11
11
  "install-copilot.mjs",
12
+ "glm.agent.md",
13
+ "glm.instructions.md",
12
14
  "copilot-instructions.md",
13
15
  "mcp.json.example",
14
16
  "README.md",