{
  "markdown": "# glm-mcp — GLM as a cheap delegate for your AI coding agent\n\n**GLM** (Zhipu / Z.ai) as a **~10x cheaper** delegate for your AI coding agent. Your expensive\nmain model — **Claude Opus**, **Copilot's** default, or **Codex** — orchestrates and reviews; **GLM** does\nthe actual work, billed on cheap GLM tokens. GLM exposes an Anthropic-compatible `/v1/messages`\nendpoint, so it drops into anything that already speaks Anthropic. This repo wraps it as an\n**MCP server** with four tools, plus one-command installers for **Claude Code**, **GitHub Copilot**,\nand **Codex**. The same server powers every edition.\n\n<p align=\"center\">\n  <a href=\"claude/\"><img src=\"https://img.shields.io/badge/Claude_Code-glm--mcp--claude-d97757?style=for-the-badge&logo=anthropic&logoColor=white\" alt=\"Claude Code edition\"></a>\n  &nbsp;\n  <a href=\"copilot/\"><img src=\"https://img.shields.io/badge/GitHub_Copilot-glm--mcp--copilot-24292e?style=for-the-badge&logo=githubcopilot&logoColor=white\" alt=\"GitHub Copilot edition\"></a>\n  &nbsp;\n  <a href=\"codex/\"><img src=\"https://img.shields.io/badge/Codex-glm--mcp--codex-10a37f?style=for-the-badge&logo=openai&logoColor=white\" alt=\"Codex edition\"></a>\n</p>\n\n<p align=\"center\">\n  <a href=\"https://www.npmjs.com/package/glm-mcp\"><img src=\"https://img.shields.io/npm/v/glm-mcp?label=glm-mcp&color=cb3837&logo=npm\" alt=\"glm-mcp\"></a>\n  <a href=\"https://www.npmjs.com/package/glm-mcp-claude\"><img src=\"https://img.shields.io/npm/v/glm-mcp-claude?label=glm-mcp-claude&color=cb3837&logo=npm\" alt=\"glm-mcp-claude\"></a>\n  <a href=\"https://www.npmjs.com/package/glm-mcp-copilot\"><img src=\"https://img.shields.io/npm/v/glm-mcp-copilot?label=glm-mcp-copilot&color=cb3837&logo=npm\" alt=\"glm-mcp-copilot\"></a>\n  <a href=\"https://www.npmjs.com/package/glm-mcp-codex\"><img src=\"https://img.shields.io/npm/v/glm-mcp-codex?label=glm-mcp-codex&color=cb3837&logo=npm\" alt=\"glm-mcp-codex\"></a>\n  <a href=\"https://github.com/djerok/glm-mcp/actions/workflows/ci.yml\"><img src=\"https://github.com/djerok/glm-mcp/actions/workflows/ci.yml/badge.svg\" alt=\"CI\"></a>\n  <a href=\"https://glama.ai/mcp/servers/djerok/glm-mcp\"><img src=\"https://glama.ai/mcp/servers/djerok/glm-mcp/badges/score.svg\" alt=\"Glama\"></a>\n  <a href=\"LICENSE\"><img src=\"https://img.shields.io/badge/license-MIT-blue.svg\" alt=\"MIT\"></a>\n</p>\n\n## How it works\n\n```mermaid\nflowchart TD\n    You[\"You\"]\n    Main[\"Main agent (Claude Opus / Copilot / Codex)<br/>orchestrates + reviews\"]\n    Srv[\"glm MCP server (stdio)<br/>4 tools\"]\n    Rt[\"Router<br/>peak-aware model pick + cost bias\"]\n    Zai[/\"Z.ai Anthropic endpoint<br/>POST /v1/messages\"/]\n    Loop[\"glm_agent tool loop<br/>read_file / write_file / edit_file<br/>list_dir / run_bash — on your repo\"]\n    Led[(\"usage.jsonl<br/>every GLM call: model + tokens\")]\n\n    Repo[(\"your repo\")]\n\n    You --> Main -->|\"glm_agent(task, workdir)\"| Srv\n    Srv --> Rt --> Zai\n    Zai -->|\"tool calls\"| Loop\n    Loop -->|\"tool results\"| Zai\n    Loop -->|\"reads / writes / runs\"| Repo\n    Zai --> Led\n    Srv -->|\"summary + GLM STATS<br/>(model, tokens, est. cost)\"| Main\n    Main -->|\"review · diff · revert\"| You\n```\n\nPlain-English walkthrough:\n\n1. **You ask** the main agent for work.\n2. The **main agent delegates** via `glm_agent` — it passes a goal plus an absolute `workdir`.\n3. The server's **router** picks a GLM model (peak-aware) and calls the **Z.ai** `/v1/messages`\n   endpoint; the **cost bias** keeps GLM the default.\n4. GLM runs **its own agent loop** (`read_file` / `write_file` / `edit_file` / `list_dir` /\n   `run_bash`) directly against your repo, then stops with a summary.\n5. The server returns a **concise summary + a `GLM STATS` block** (model, tokens, est. cost) to\n   the main agent.\n6. The main agent **reviews**; every GLM call is also appended to the `usage.jsonl` **ledger**.\n\n**Token economics.** Delegated work bills **GLM tokens** (~10x cheaper). The main model only\npays for orchestration + review. A **near-100% GLM share** requires the full-GLM launcher\n([`claude/glm-code.mjs`](claude/glm-code.mjs)), because a hybrid main agent always carries\nper-turn session context — that context is the floor on its token share.\n\n## The four tools\n\n| Tool | Cost | What it does |\n|---|---|---|\n| `glm_agent` | GLM tokens | GLM as a real coding agent in your repo (read/write/edit/run). `dry_run: true` previews a diff and writes nothing; after a real run a **git-checkpoint revert line** is printed. |\n| `glm_delegate` | GLM tokens (opt-in) | Pure text generation — text in, text out. **Hidden by default** (`glm_agent` handles text-only tasks too); set `GLM_DELEGATE=on` to expose it. |\n| `glm_recommend` | free (local) | GLM-vs-main-model advisory: which engine, which GLM model, confidence, and reasons. No GLM call. |\n| `glm_status` | free (local) | Peak window, active model, **usage-ledger totals** (proof of GLM spend), and config health. No GLM call. |\n\n**Live progress.** `glm_agent` streams MCP **progress notifications** while it runs —\ncurrent iteration, token count, and **tok/s** — shown live in Claude Code and mapped to\n`tool.execution_progress` in VS Code Copilot. This heartbeat also keeps long calls alive on clients that\nreset their timeout on progress, and cancelling a run stops GLM **promptly** (partial changes are shown\nand revertable). `max_tokens` defaults to **`auto`** (uncapped/generous; the orchestrating agent may\npass a number to cap a call). The server uses an **idle/stall timeout** (`GLM_STALL_TIMEOUT_MS`, 2 min),\nso an actively-streaming turn is never cut off. If a very long run is still cancelled by your client's\ntool-call timeout, raise it with `MCP_TOOL_TIMEOUT` / `CLAUDE_CODE_MCP_TOOL_IDLE_TIMEOUT`.\n\n## Install\n\n### (a) Claude Code\n\n```bash\nnpx glm-mcp-claude --key YOUR_ZAI_KEY\n```\n\nInstalls **globally by default** (user-scoped): the MCP server, a full-tool **`glm` subagent**, a\n**PreToolUse auto-routing hook**, and an optional **`glm-code`** full-GLM launcher. Restart\nClaude Code, then run `glm_status` to confirm `api_key_loaded: true`.\nFull details: [claude/README.md](claude/README.md).\n\n### (b) GitHub Copilot / VS Code\n\n```bash\nnpx glm-mcp-copilot --key YOUR_ZAI_KEY            # current workspace\nnpx glm-mcp-copilot --global --key YOUR_ZAI_KEY   # every workspace\n```\n\nInstalls the MCP server in **agent mode**, a **`GLM` custom agent (subagent)**, a **PreToolUse\nauto-routing hook**, and delegation **instructions files**. Reload the VS Code window, open\nCopilot Chat in Agent mode, start the `glm` server.\nFull details: [copilot/README.md](copilot/README.md).\n\n### (c) Codex\n\nInstall the published Codex package:\n\n```bash\nnpx glm-mcp-codex --key YOUR_ZAI_KEY\n```\n\nInstalls a Codex MCP registration, a `glm` custom agent, the `glm-delegate` skill, and an advisory\n`UserPromptSubmit`/`PreToolUse` hook. The config gives GLM tools a 30-minute timeout and prompts before\nmutating calls. Restart Codex, review the hook with `/hooks`, and run `glm_status`.\nFull details: [codex/README.md](codex/README.md).\n\n### (d) Any MCP client / Glama / Docker\n\nThe standalone [`glm-mcp`](https://www.npmjs.com/package/glm-mcp) package — no installer needed\nfor Cursor, Windsurf, Claude Desktop, Glama, etc.:\n\n```json\n{\n  \"mcpServers\": {\n    \"glm\": {\n      \"command\": \"npx\",\n      \"args\": [\"-y\", \"glm-mcp\"],\n      \"env\": { \"GLM_API_KEY\": \"YOUR_ZAI_KEY\" }\n    }\n  }\n}\n```\n\nFor containers, the repo-root [`Dockerfile`](Dockerfile) runs the **same** server:\n\n```bash\ndocker build -t glm-mcp .\ndocker run --rm -i -e GLM_API_KEY=YOUR_ZAI_KEY glm-mcp\n```\n\nThe server **boots and answers MCP introspection without a key** — set `GLM_API_KEY` only for\nactual GLM calls.\n\n## Editions at a glance\n\n| | Claude Code -\\> [`claude/`](claude/) | GitHub Copilot (VS Code) -\\> [`copilot/`](copilot/) | Codex -\\> [`codex/`](codex/) |\n|---|---|---|---|\n| npm package | `glm-mcp-claude` | `glm-mcp-copilot` | `glm-mcp-codex` |\n| Install | `npx glm-mcp-claude --key ...` | `npx glm-mcp-copilot --key ...` (+ `--global`) | `npx glm-mcp-codex --key ...` |\n| MCP server | user-scoped (`claude mcp add glm -s user`) | VS Code agent mode (`mcp.json`) | `~/.codex/config.toml` (or trusted project config) |\n| Subagent | `glm` subagent (`~/.claude/agents/glm.md`) | `GLM` custom agent (`glm.agent.md`) | `glm` custom agent (`~/.codex/agents/glm.toml`) |\n| Auto-routing hook | PreToolUse, `Task` matcher (`glm_subagent_router.mjs`) | PreToolUse, fires on all calls (`glm_router_hook.mjs`) | UserPromptSubmit + PreToolUse, advisory only |\n| Delegation policy | appended to `~/.claude/CLAUDE.md` | `.instructions.md` files | `glm-delegate` skill + optional `AGENTS.md` snippet |\n| Full-GLM launcher | `glm-code.mjs` (Claude only) | — |\n| Docs | [claude/README.md](claude/README.md) | [copilot/README.md](copilot/README.md) | [codex/README.md](codex/README.md) |\n\n**Parity.** All three editions expose the same four **tools** and a **subagent** while using the same\nserver underneath. Codex uses its native custom-agent, skill, and hook surfaces; its hook is advisory\nonly and must be trusted by the user. Only Claude ships the standalone **`glm-code`** full-GLM launcher.\n\n## Configuration\n\nAll knobs live in `.env` (git-ignored). Location per edition: **Claude**\n`~/.claude/glm-mcp/.env` (set during install); **Copilot** `~/.glm-mcp/glm-mcp/.env`; **Codex**\n`~/.codex/glm-mcp/.env`. Codex sets `tool_timeout_sec = 1800` because its default MCP tool timeout is\n60 seconds. Full reference with comments: [`claude/glm-mcp/.env.example`](claude/glm-mcp/.env.example).\n\n| Var | Default | Meaning |\n|---|---|---|\n| `GLM_API_KEY` | — | Your Z.ai / Zhipu **GLM Coding Plan** key. Required for GLM calls. |\n| `GLM_BASE_URL` | `https://api.z.ai/api/anthropic` | Anthropic-compatible endpoint (`/v1/messages`). |\n| `GLM_USE_HAIKU` | `off` | `off` calls GLM directly so **all tokens stay on GLM**; `on` allows the Haiku-orchestrated subagent (spends some Claude tokens). |\n| `GLM_COST_BIAS` | `7` | How hard to favor GLM. `7` ≈ 98–100% of eligible tasks to GLM. Lower (e.g. `1.5`) to send more hard tasks to the main model; `0` = capability only. |\n| `GLM_MAX_CONCURRENT` | `1` | GLM caps in-flight requests (~1); keep at 1 unless your tier allows more. |\n| `GLM_CAP` | `off` | `off` = generous (up to `GLM_MAX_TOKENS_CEILING`); `on` enforces `GLM_MAX_TOKENS`. |\n| `GLM_MAX_TOKENS` | `32768` | Hard per-call limit applied **only when `GLM_CAP=on`**. |\n| `GLM_MAX_TOKENS_CEILING` | `131072` | Generous default used when the cap is **off**. |\n| `GLM_MAX_RETRIES` | `4` | Retries on 429 / concurrency / 5xx with exponential backoff. |\n| `GLM_TIMEOUT_MS` | `300000` | Per GLM HTTP request timeout (5 min). |\n| `GLM_AGENT_MAX_ITERS` | `30` | Max tool-loop turns for `glm_agent` before it stops. |\n| `GLM_AGENT_BASH_TIMEOUT_MS` | `120000` | Per-`run_bash` command timeout inside `glm_agent`. |\n| `GLM_OFFPEAK_MODEL` | `glm-5.2` | Candidate model(s) for `auto` off-peak. **Comma list** allowed; router auto-picks. |\n| `GLM_PEAK_MODEL` | `glm-5.2` | Candidate model(s) for `auto` at peak. **Comma list** allowed; include a no-surcharge model (e.g. `glm-4.7`) to dodge the peak tax. |\n| `GLM_CHEAP_MODEL` | `glm-4.5-air` | The cheap model (used in the full-GLM launcher's Haiku slot). |\n| `GLM_PEAK_START_CN` | `14` | Peak window start, China hour (UTC+8). |\n| `GLM_PEAK_END_CN` | `18` | Peak window end (exclusive), China hour (UTC+8). |\n\n## Peak-aware routing & cost\n\nChina peak window is **14:00–18:00 (UTC+8)**. The **glm-5.x** family carries a surcharge at peak\n(~3x peak / ~2x off-peak), so when `auto` lands on a glm-5.x model at peak the router routes\n**less** work to GLM; if you list a no-surcharge model (e.g. `GLM_PEAK_MODEL=glm-5.2,glm-4.7`) the\nrouter prefers it at peak and GLM stays fine to use. The cost bias keeps GLM the default either\nway — even at peak it is cheaper than the main model.\n\n**What stays on the main model:** sensitive / secret code, vision input, parallel fan-out,\n>128K context, latency-tight loops, and heavy dependent tool-loops (the router's hard overrides).\n\n## Proof it's really GLM\n\n- **`usage.jsonl` ledger** — every GLM call is appended on disk with `model` + `input_tokens` +\n  `output_tokens`. Claude: `~/.claude/glm-mcp/usage.jsonl`; Copilot:\n  `~/.glm-mcp/glm-mcp/usage.jsonl`. Independent of the Z.ai dashboard.\n- **`glm_status`** — prints the cumulative ledger totals (calls, tokens, per-model counts).\n- **`=== GLM STATS ===`** block — printed after every `glm_agent` run: model, tokens delegated,\n  iterations, files changed, est. cost vs Opus.\n\nIf the ledger is empty, GLM was never called — the work ran on the main model.\n\n## Oversight & safety\n\n- **`dry_run: true`** on `glm_agent` — GLM proposes a full diff and writes nothing; approve before\n  applying.\n- **Git checkpoint revert line** — printed after every real `glm_agent` run (when the workdir is a\n  git repo), so you can undo in one command.\n- **Key isolation** — `GLM_API_KEY` lives only in the git-ignored `.env`; it is never baked into\n  the npm packages (`scripts/publish-server.mjs` scans every pack for `.env` / `usage.jsonl` /\n  `node_modules` and fails loudly).\n- **Data residency** — GLM traffic goes to Z.ai servers in China. Keep secrets and regulated code\n  on the main model; the router's `sensitive` flag forces it there.\n\n## Development / CI\n\nCI (see [`.github/workflows/ci.yml`](.github/workflows/ci.yml)) runs: syntax checks on the server\nand every installer/hook/script, the **keyless stdio smoke** ([`scripts/smoke-stdio.mjs`](scripts/smoke-stdio.mjs))\nthat asserts the four-tool MCP handshake with no key on disk, a **Docker introspection** test\n(`initialize` piped into the built image), and an **npm-pack secret scan**\n([`scripts/publish-server.mjs`](scripts/publish-server.mjs)). PRs welcome — see\n[CONTRIBUTING.md](CONTRIBUTING.md).\n\n## License\n\n[MIT](LICENSE) © [djerok](https://github.com/djerok) · Canonical repo: https://github.com/djerok/glm-mcp\n",
  "bytes": 13905,
  "sha": "52a1834dd1705a7d6e2fa26336038087398bddebf1372838c41e966d2cc54826",
  "repo_slug": "djerok/glm-mcp",
  "fonte": "repo",
  "truncated": false,
  "api": "https://agentalog.com/api/listings/mcp_io_github_djerok_glm_mcp_d5d788b5/readme"
}