context-mode 0.9.16 → 0.9.18

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.
@@ -46,6 +46,44 @@
46
46
  "command": "node ${CLAUDE_PLUGIN_ROOT}/hooks/pretooluse.mjs"
47
47
  }
48
48
  ]
49
+ },
50
+ {
51
+ "matcher": "mcp__plugin_context-mode_context-mode__execute",
52
+ "hooks": [
53
+ {
54
+ "type": "command",
55
+ "command": "node ${CLAUDE_PLUGIN_ROOT}/hooks/pretooluse.mjs"
56
+ }
57
+ ]
58
+ },
59
+ {
60
+ "matcher": "mcp__plugin_context-mode_context-mode__execute_file",
61
+ "hooks": [
62
+ {
63
+ "type": "command",
64
+ "command": "node ${CLAUDE_PLUGIN_ROOT}/hooks/pretooluse.mjs"
65
+ }
66
+ ]
67
+ },
68
+ {
69
+ "matcher": "mcp__plugin_context-mode_context-mode__batch_execute",
70
+ "hooks": [
71
+ {
72
+ "type": "command",
73
+ "command": "node ${CLAUDE_PLUGIN_ROOT}/hooks/pretooluse.mjs"
74
+ }
75
+ ]
76
+ }
77
+ ],
78
+ "SessionStart": [
79
+ {
80
+ "matcher": "",
81
+ "hooks": [
82
+ {
83
+ "type": "command",
84
+ "command": "node ${CLAUDE_PLUGIN_ROOT}/hooks/sessionstart.mjs"
85
+ }
86
+ ]
49
87
  }
50
88
  ]
51
89
  }
@@ -13,7 +13,7 @@
13
13
  "name": "context-mode",
14
14
  "source": "./",
15
15
  "description": "Claude Code MCP plugin that saves 98% of your context window. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and intent-driven search.",
16
- "version": "0.9.16",
16
+ "version": "0.9.18",
17
17
  "author": {
18
18
  "name": "Mert Koseoğlu"
19
19
  },
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-mode",
3
- "version": "0.9.16",
3
+ "version": "0.9.18",
4
4
  "description": "Claude Code MCP plugin that saves 98% of your context window. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and intent-driven search.",
5
5
  "author": {
6
6
  "name": "Mert Koseoğlu",
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "homepage": "https://github.com/mksglu/claude-context-mode#readme",
10
10
  "repository": "https://github.com/mksglu/claude-context-mode",
11
- "license": "MIT",
11
+ "license": "Elastic-2.0",
12
12
  "keywords": [
13
13
  "mcp",
14
14
  "context-window",
package/LICENSE CHANGED
@@ -1,21 +1,94 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Mert Koseoğlu
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ Elastic License 2.0 (ELv2)
2
+
3
+ Copyright 2026 Mert Koseoglu
4
+
5
+ ## Acceptance
6
+
7
+ By using the software, you agree to all of the terms and conditions below.
8
+
9
+ ## Copyright License
10
+
11
+ The licensor grants you a non-exclusive, royalty-free, worldwide,
12
+ non-sublicensable, non-transferable license to use, copy, distribute, make
13
+ available, and prepare derivative works of the software, in each case subject
14
+ to the limitations and conditions below.
15
+
16
+ ## Limitations
17
+
18
+ You may not provide the software to third parties as a hosted or managed
19
+ service, where the service provides users with access to any substantial set
20
+ of the features or functionality of the software.
21
+
22
+ You may not move, change, disable, or circumvent the license key
23
+ functionality in the software, and you may not remove or obscure any
24
+ functionality in the software that is protected by the license key.
25
+
26
+ You may not alter, remove, or obscure any licensing, copyright, or other
27
+ notices of the licensor in the software. Any use of the licensor's trademarks
28
+ is subject to applicable law.
29
+
30
+ ## Patents
31
+
32
+ The licensor grants you a license, under any patent claims the licensor can
33
+ license, or becomes able to license, to make, have made, use, sell, offer for
34
+ sale, import and have imported the software, in each case subject to the
35
+ limitations and conditions in this license. This license does not cover any
36
+ patent claims that you cause to be infringed by modifications or additions to
37
+ the software. If you or your company make any written claim that the software
38
+ infringes or contributes to infringement of any patent, your patent license
39
+ for the software granted under these terms ends immediately. If your company
40
+ makes such a claim, your patent license ends immediately for work on behalf
41
+ of your company.
42
+
43
+ ## Notices
44
+
45
+ You must ensure that anyone who gets a copy of any part of the software from
46
+ you also gets a copy of these terms.
47
+
48
+ If you modify the software, you must include in any modified copies of the
49
+ software prominent notices stating that you have modified the software.
50
+
51
+ ## No Other Rights
52
+
53
+ These terms do not imply any licenses other than those expressly granted in
54
+ these terms.
55
+
56
+ ## Termination
57
+
58
+ If you use the software in violation of these terms, such use is not
59
+ licensed, and your licenses will automatically terminate. If the licensor
60
+ provides you with a notice of your violation, and you cease all violation of
61
+ this license no later than 30 days after you receive that notice, your
62
+ licenses will be reinstated retroactively. However, if you violate these
63
+ terms after such reinstatement, any additional violation of these terms will
64
+ cause your licenses to terminate automatically and permanently.
65
+
66
+ ## No Liability
67
+
68
+ *As far as the law allows, the software comes as is, without any warranty or
69
+ condition, and the licensor will not be liable to you for any damages arising
70
+ out of these terms or the use or nature of the software, under any kind of
71
+ legal claim.*
72
+
73
+ ## Definitions
74
+
75
+ The **licensor** is the entity offering these terms, and the **software** is
76
+ the software the licensor makes available under these terms, including any
77
+ portion of it.
78
+
79
+ **you** refers to the individual or entity agreeing to these terms.
80
+
81
+ **your company** is any legal entity, sole proprietorship, or other kind of
82
+ organization that you work for, plus all organizations that have control over,
83
+ are under the control of, or are under common control with that organization.
84
+ **control** means ownership of substantially all the assets of an entity, or
85
+ the power to direct its management and policies by vote, contract, or
86
+ otherwise. Control can be direct or indirect.
87
+
88
+ **your licenses** are all the licenses granted to you for the software under
89
+ these terms.
90
+
91
+ **use** means anything you do with the software requiring one of your
92
+ licenses.
93
+
94
+ **trademark** means trademarks, service marks, and similar rights.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  **The other half of the context problem.**
4
4
 
5
- [![users](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2Fmksglu%2Fclaude-context-mode%40main%2Fstats.json&query=%24.message&label=users&color=brightgreen)](https://www.npmjs.com/package/context-mode) [![npm](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2Fmksglu%2Fclaude-context-mode%40main%2Fstats.json&query=%24.npm&label=npm&color=blue)](https://www.npmjs.com/package/context-mode) [![marketplace](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2Fmksglu%2Fclaude-context-mode%40main%2Fstats.json&query=%24.marketplace&label=marketplace&color=blue)](https://github.com/mksglu/claude-context-mode) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
5
+ [![users](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2Fmksglu%2Fclaude-context-mode%40main%2Fstats.json&query=%24.message&label=users&color=brightgreen)](https://www.npmjs.com/package/context-mode) [![npm](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2Fmksglu%2Fclaude-context-mode%40main%2Fstats.json&query=%24.npm&label=npm&color=blue)](https://www.npmjs.com/package/context-mode) [![marketplace](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2Fmksglu%2Fclaude-context-mode%40main%2Fstats.json&query=%24.marketplace&label=marketplace&color=blue)](https://github.com/mksglu/claude-context-mode) [![GitHub stars](https://img.shields.io/github/stars/mksglu/claude-context-mode?style=flat&color=yellow)](https://github.com/mksglu/claude-context-mode/stargazers) [![GitHub forks](https://img.shields.io/github/forks/mksglu/claude-context-mode?style=flat&color=blue)](https://github.com/mksglu/claude-context-mode/network/members) [![Last commit](https://img.shields.io/github/last-commit/mksglu/claude-context-mode?color=green)](https://github.com/mksglu/claude-context-mode/commits) [![License: ELv2](https://img.shields.io/badge/License-ELv2-blue.svg)](LICENSE)
6
6
 
7
7
  Every MCP tool call in Claude Code dumps raw data into your 200K context window. A Playwright snapshot costs 56 KB. Twenty GitHub issues cost 59 KB. One access log — 45 KB. After 30 minutes, 40% of your context is gone.
8
8
 
@@ -62,7 +62,7 @@ Code Mode showed that tool definitions can be compressed by 99.9%. Context Mode
62
62
  | `execute_file` | Process files in sandbox. Raw content never leaves. | 45 KB → 155 B |
63
63
  | `index` | Chunk markdown into FTS5 with BM25 ranking. | 60 KB → 40 B |
64
64
  | `search` | Query indexed content with multiple queries in one call. | On-demand retrieval |
65
- | `fetch_and_index` | Fetch URL, convert to markdown, index. | 60 KB → 40 B |
65
+ | `fetch_and_index` | Fetch URL, detect content type (HTML/JSON/text), chunk and index. | 60 KB → 40 B |
66
66
 
67
67
  ## How the Sandbox Works
68
68
 
@@ -184,13 +184,139 @@ Fetch the React useEffect docs, index them, and find the cleanup pattern
184
184
  with code examples. Then run /context-mode:stats.
185
185
  ```
186
186
 
187
+ ## Security
188
+
189
+ Context Mode enforces the same permission rules you already use in Claude Code — but extends them to the MCP sandbox. If you block `sudo` in Claude Code, it's also blocked inside `execute`, `execute_file`, and `batch_execute`.
190
+
191
+ **Zero setup required.** If you haven't configured any permissions, nothing changes. This only activates when you add rules.
192
+
193
+ ### Getting started
194
+
195
+ Find your settings file:
196
+
197
+ ```bash
198
+ # macOS / Linux
199
+ cat ~/.claude/settings.json
200
+
201
+ # Windows
202
+ type %USERPROFILE%\.claude\settings.json
203
+ ```
204
+
205
+ Add a `permissions` section (keep your existing settings, just add this block). Then restart Claude Code.
206
+
207
+ ```json
208
+ {
209
+ "permissions": {
210
+ "deny": [
211
+ "Bash(sudo *)",
212
+ "Bash(rm -rf /*)",
213
+ "Read(.env)",
214
+ "Read(**/.env*)"
215
+ ],
216
+ "allow": [
217
+ "Bash(git:*)",
218
+ "Bash(npm:*)"
219
+ ]
220
+ }
221
+ }
222
+ ```
223
+
224
+ The pattern is `Tool(what to match)` where `*` means "anything".
225
+
226
+ <details>
227
+ <summary><strong>Common deny patterns</strong> (click to expand)</summary>
228
+
229
+ **Dangerous commands:**
230
+ ```
231
+ "Bash(sudo *)" — block all sudo commands
232
+ "Bash(rm -rf /*)" — block recursive delete from root
233
+ "Bash(chmod 777 *)" — block open permissions
234
+ "Bash(shutdown *)" — block shutdown/reboot
235
+ "Bash(kill -9 *)" — block force kill
236
+ "Bash(mkfs *)" — block filesystem format
237
+ "Bash(dd *)" — block disk write
238
+ ```
239
+
240
+ **Network access:**
241
+ ```
242
+ "Bash(curl *)" — block curl
243
+ "Bash(wget *)" — block wget
244
+ "Bash(ssh *)" — block ssh connections
245
+ "Bash(scp *)" — block secure copy
246
+ "Bash(nc *)" — block netcat
247
+ ```
248
+
249
+ **Package managers and deploys:**
250
+ ```
251
+ "Bash(npm publish *)" — block npm publish
252
+ "Bash(docker push *)" — block docker push
253
+ "Bash(pip install *)" — block pip install
254
+ "Bash(brew install *)" — block brew install
255
+ "Bash(apt install *)" — block apt install
256
+ "Bash(wrangler deploy *)" — block Cloudflare deploys
257
+ "Bash(terraform apply *)" — block terraform apply
258
+ "Bash(kubectl delete *)" — block k8s delete
259
+ ```
260
+
261
+ **Sensitive files:**
262
+ ```
263
+ "Read(.env)" — block .env in project root
264
+ "Read(**/.env*)" — block .env files everywhere
265
+ "Read(**/*secret*)" — block files with "secret" in the name
266
+ "Read(**/*credential*)" — block credential files
267
+ "Read(**/*.pem)" — block private keys
268
+ "Read(**/*id_rsa*)" — block SSH keys
269
+ ```
270
+
271
+ </details>
272
+
273
+ <details>
274
+ <summary><strong>Common allow patterns</strong> (click to expand)</summary>
275
+
276
+ ```
277
+ "Bash(git:*)" — allow git (with or without args)
278
+ "Bash(npm:*)" — allow npm
279
+ "Bash(npx:*)" — allow npx
280
+ "Bash(node:*)" — allow node
281
+ "Bash(python:*)" — allow python
282
+ "Bash(ls:*)" — allow ls
283
+ "Bash(cat:*)" — allow cat
284
+ "Bash(echo:*)" — allow echo
285
+ "Bash(grep:*)" — allow grep
286
+ "Bash(make:*)" — allow make
287
+ ```
288
+
289
+ </details>
290
+
291
+ ### Chained commands
292
+
293
+ Commands chained with `&&`, `;`, or `|` are split — each part is checked separately:
294
+
295
+ ```
296
+ echo hello && sudo rm -rf /tmp
297
+ ```
298
+
299
+ Blocked. Even though it starts with `echo`, the `sudo` part matches the deny rule.
300
+
301
+ ### Where to put rules
302
+
303
+ Rules can go in three places (checked in this order):
304
+
305
+ 1. `.claude/settings.local.json` — this project only (gitignored)
306
+ 2. `.claude/settings.json` — this project, shared with team
307
+ 3. `~/.claude/settings.json` — all projects
308
+
309
+ **deny** always wins over **allow**. More specific (project-level) rules override global ones.
310
+
187
311
  ## Requirements
188
312
 
189
313
  - **Node.js 18+**
190
314
  - **Claude Code** with MCP support
191
315
  - Optional: Bun (auto-detected, 3-5x faster JS/TS)
192
316
 
193
- ## Development
317
+ ## Contributing
318
+
319
+ We welcome contributions! See [CONTRIBUTING.md](CONTRIBUTING.md) for the full local development workflow, TDD guidelines, and how to test your changes in a live Claude Code session.
194
320
 
195
321
  ```bash
196
322
  git clone https://github.com/mksglu/claude-context-mode.git
@@ -211,4 +337,4 @@ npm run test:all # full suite
211
337
 
212
338
  ## License
213
339
 
214
- MIT
340
+ [Elastic License 2.0 (ELv2)](LICENSE) — free to use, modify, and share. You may not rebrand and redistribute this software as a competing plugin, product, or managed service.
package/build/cli.js CHANGED
@@ -231,6 +231,24 @@ async function doctor() {
231
231
  " — No PreToolUse hooks found" +
232
232
  color.dim("\n Run: npx context-mode upgrade"));
233
233
  }
234
+ // Check SessionStart hook
235
+ const sessionStart = hooks?.SessionStart;
236
+ if (sessionStart && sessionStart.length > 0) {
237
+ const hasSessionHook = sessionStart.some((entry) => entry.hooks?.some((h) => h.command?.includes("sessionstart.mjs")));
238
+ if (hasSessionHook) {
239
+ p.log.success(color.green("SessionStart hook: PASS") + " — SessionStart hook configured");
240
+ }
241
+ else {
242
+ p.log.error(color.red("SessionStart hook: FAIL") +
243
+ " — SessionStart exists but does not point to sessionstart.mjs" +
244
+ color.dim("\n Run: npx context-mode upgrade"));
245
+ }
246
+ }
247
+ else {
248
+ p.log.error(color.red("SessionStart hook: FAIL") +
249
+ " — No SessionStart hooks found" +
250
+ color.dim("\n Run: npx context-mode upgrade"));
251
+ }
234
252
  }
235
253
  else {
236
254
  p.log.error(color.red("Hooks installed: FAIL") +
@@ -500,7 +518,7 @@ async function upgrade() {
500
518
  const hookScriptPath = resolve(pluginRoot, "hooks", "pretooluse.mjs");
501
519
  const settings = readSettings() ?? {};
502
520
  const desiredHookEntry = {
503
- matcher: "Bash|Read|Grep|Glob|WebFetch|WebSearch|Task",
521
+ matcher: "Bash|Read|Grep|WebFetch|Task|mcp__plugin_context-mode_context-mode__execute|mcp__plugin_context-mode_context-mode__execute_file|mcp__plugin_context-mode_context-mode__batch_execute",
504
522
  hooks: [
505
523
  {
506
524
  type: "command",
@@ -532,6 +550,41 @@ async function upgrade() {
532
550
  p.log.info(color.dim("Created PreToolUse hooks section"));
533
551
  changes.push("Created PreToolUse hooks section");
534
552
  }
553
+ // --- SessionStart hook ---
554
+ p.log.step("Configuring SessionStart hook...");
555
+ const sessionHookScriptPath = resolve(pluginRoot, "hooks", "sessionstart.mjs");
556
+ const desiredSessionHookEntry = {
557
+ matcher: "",
558
+ hooks: [
559
+ {
560
+ type: "command",
561
+ command: "node " + sessionHookScriptPath,
562
+ },
563
+ ],
564
+ };
565
+ const existingSessionStart = hooks.SessionStart;
566
+ if (existingSessionStart && Array.isArray(existingSessionStart)) {
567
+ const existingSessionIdx = existingSessionStart.findIndex((entry) => {
568
+ const entryHooks = entry.hooks;
569
+ return entryHooks?.some((h) => h.command?.includes("sessionstart.mjs"));
570
+ });
571
+ if (existingSessionIdx >= 0) {
572
+ existingSessionStart[existingSessionIdx] = desiredSessionHookEntry;
573
+ p.log.info(color.dim("Updated existing SessionStart hook entry"));
574
+ changes.push("Updated existing SessionStart hook entry");
575
+ }
576
+ else {
577
+ existingSessionStart.push(desiredSessionHookEntry);
578
+ p.log.info(color.dim("Added SessionStart hook entry"));
579
+ changes.push("Added SessionStart hook entry to existing hooks");
580
+ }
581
+ hooks.SessionStart = existingSessionStart;
582
+ }
583
+ else {
584
+ hooks.SessionStart = [desiredSessionHookEntry];
585
+ p.log.info(color.dim("Created SessionStart hooks section"));
586
+ changes.push("Created SessionStart hooks section");
587
+ }
535
588
  settings.hooks = hooks;
536
589
  // Write updated settings
537
590
  try {
package/build/executor.js CHANGED
@@ -41,10 +41,20 @@ export class PolyglotExecutor {
41
41
  if (cmd[0] === "__rust_compile_run__") {
42
42
  return await this.#compileAndRun(filePath, tmpDir, timeout);
43
43
  }
44
- return await this.#spawn(cmd, tmpDir, timeout);
44
+ // Shell commands run in the project directory so git, relative paths,
45
+ // and other project-aware tools work naturally. Non-shell languages
46
+ // run in the temp directory where their script file is written.
47
+ const cwd = language === "shell" ? this.#projectRoot : tmpDir;
48
+ return await this.#spawn(cmd, cwd, timeout);
45
49
  }
46
50
  finally {
47
- rmSync(tmpDir, { recursive: true, force: true });
51
+ try {
52
+ rmSync(tmpDir, { recursive: true, force: true });
53
+ }
54
+ catch {
55
+ // On Windows, bash may still hold file handles when rmSync runs.
56
+ // Ignore EPERM/EBUSY — the OS will clean up %TEMP% eventually.
57
+ }
48
58
  }
49
59
  }
50
60
  async executeFile(opts) {
@@ -155,7 +165,21 @@ export class PolyglotExecutor {
155
165
  // Only .cmd/.bat shims need shell on Windows; real executables don't.
156
166
  // Using shell: true globally causes process-tree kill issues with MSYS2/Git Bash.
157
167
  const needsShell = isWin && ["tsx", "ts-node", "elixir"].includes(cmd[0]);
158
- const proc = spawn(cmd[0], cmd.slice(1), {
168
+ // On Windows with Git Bash, pass the script as `bash -c "source /posix/path"`
169
+ // rather than `bash /path/to/script.sh`. This avoids MSYS2 path mangling
170
+ // while still allowing MSYS_NO_PATHCONV to protect non-ASCII paths in commands.
171
+ let spawnCmd = cmd[0];
172
+ let spawnArgs;
173
+ if (isWin && cmd.length === 2 && cmd[1]) {
174
+ const posixPath = cmd[1].replace(/\\/g, "/");
175
+ spawnArgs = [posixPath];
176
+ }
177
+ else {
178
+ spawnArgs = isWin
179
+ ? cmd.slice(1).map(a => a.replace(/\\/g, "/"))
180
+ : cmd.slice(1);
181
+ }
182
+ const proc = spawn(spawnCmd, spawnArgs, {
159
183
  cwd,
160
184
  stdio: ["ignore", "pipe", "pipe"],
161
185
  env: this.#buildSafeEnv(cwd),
@@ -256,6 +280,12 @@ export class PolyglotExecutor {
256
280
  // XDG (config paths for gh, gcloud, etc.)
257
281
  "XDG_CONFIG_HOME",
258
282
  "XDG_DATA_HOME",
283
+ // SSH agent socket — required for git/jj operations that use SSH remotes.
284
+ // Without this, subprocesses cannot reach the agent and fall back to
285
+ // prompting for the key passphrase directly on the TTY, which corrupts
286
+ // Claude Code's PTY ownership.
287
+ "SSH_AUTH_SOCK",
288
+ "SSH_AGENT_PID",
259
289
  ];
260
290
  const env = {
261
291
  PATH: process.env.PATH ?? (isWin ? "" : "/usr/local/bin:/usr/bin:/bin"),
@@ -264,6 +294,7 @@ export class PolyglotExecutor {
264
294
  LANG: "en_US.UTF-8",
265
295
  PYTHONDONTWRITEBYTECODE: "1",
266
296
  PYTHONUNBUFFERED: "1",
297
+ PYTHONUTF8: "1",
267
298
  NO_COLOR: "1",
268
299
  };
269
300
  // Windows-critical env vars
@@ -277,6 +308,18 @@ export class PolyglotExecutor {
277
308
  if (process.env[key])
278
309
  env[key] = process.env[key];
279
310
  }
311
+ // Prevent MSYS2/Git Bash from converting non-ASCII Windows paths
312
+ // (e.g. Chinese characters in project paths) to POSIX paths.
313
+ env["MSYS_NO_PATHCONV"] = "1";
314
+ env["MSYS2_ARG_CONV_EXCL"] = "*";
315
+ // Ensure Git Bash unix tools (cat, ls, head, etc.) are on PATH.
316
+ // The MCP server process may not inherit the full user PATH that
317
+ // includes Git's usr/bin directory.
318
+ const gitUsrBin = "C:\\Program Files\\Git\\usr\\bin";
319
+ const gitBin = "C:\\Program Files\\Git\\bin";
320
+ if (!env["PATH"].includes(gitUsrBin)) {
321
+ env["PATH"] = `${gitUsrBin};${gitBin};${env["PATH"]}`;
322
+ }
280
323
  }
281
324
  for (const key of passthrough) {
282
325
  if (process.env[key]) {
@@ -292,14 +335,14 @@ export class PolyglotExecutor {
292
335
  case "typescript":
293
336
  return `const FILE_CONTENT_PATH = ${escaped};\nconst FILE_CONTENT = require("fs").readFileSync(FILE_CONTENT_PATH, "utf-8");\n${code}`;
294
337
  case "python":
295
- return `FILE_CONTENT_PATH = ${escaped}\nwith open(FILE_CONTENT_PATH, "r") as _f:\n FILE_CONTENT = _f.read()\n${code}`;
338
+ return `FILE_CONTENT_PATH = ${escaped}\nwith open(FILE_CONTENT_PATH, "r", encoding="utf-8") as _f:\n FILE_CONTENT = _f.read()\n${code}`;
296
339
  case "shell": {
297
340
  // Single-quote the path to prevent $, backtick, and ! expansion
298
341
  const sq = "'" + absolutePath.replace(/'/g, "'\\''") + "'";
299
342
  return `FILE_CONTENT_PATH=${sq}\nFILE_CONTENT=$(cat ${sq})\n${code}`;
300
343
  }
301
344
  case "ruby":
302
- return `FILE_CONTENT_PATH = ${escaped}\nFILE_CONTENT = File.read(FILE_CONTENT_PATH)\n${code}`;
345
+ return `FILE_CONTENT_PATH = ${escaped}\nFILE_CONTENT = File.read(FILE_CONTENT_PATH, encoding: "utf-8")\n${code}`;
303
346
  case "go":
304
347
  return `package main\n\nimport (\n\t"fmt"\n\t"os"\n)\n\nvar FILE_CONTENT_PATH = ${escaped}\n\nfunc main() {\n\tb, _ := os.ReadFile(FILE_CONTENT_PATH)\n\tFILE_CONTENT := string(b)\n\t_ = FILE_CONTENT\n\t_ = fmt.Sprint()\n${code}\n}\n`;
305
348
  case "rust":
@@ -307,9 +350,9 @@ export class PolyglotExecutor {
307
350
  case "php":
308
351
  return `<?php\n$FILE_CONTENT_PATH = ${escaped};\n$FILE_CONTENT = file_get_contents($FILE_CONTENT_PATH);\n${code}`;
309
352
  case "perl":
310
- return `my $FILE_CONTENT_PATH = ${escaped};\nopen(my $fh, '<', $FILE_CONTENT_PATH) or die "Cannot open: $!";\nmy $FILE_CONTENT = do { local $/; <$fh> };\nclose($fh);\n${code}`;
353
+ return `my $FILE_CONTENT_PATH = ${escaped};\nopen(my $fh, '<:encoding(UTF-8)', $FILE_CONTENT_PATH) or die "Cannot open: $!";\nmy $FILE_CONTENT = do { local $/; <$fh> };\nclose($fh);\n${code}`;
311
354
  case "r":
312
- return `FILE_CONTENT_PATH <- ${escaped}\nFILE_CONTENT <- readLines(FILE_CONTENT_PATH, warn=FALSE)\nFILE_CONTENT <- paste(FILE_CONTENT, collapse="\\n")\n${code}`;
355
+ return `FILE_CONTENT_PATH <- ${escaped}\nFILE_CONTENT <- readLines(FILE_CONTENT_PATH, warn=FALSE, encoding="UTF-8")\nFILE_CONTENT <- paste(FILE_CONTENT, collapse="\\n")\n${code}`;
313
356
  case "elixir":
314
357
  return `file_content_path = ${escaped}\nfile_content = File.read!(file_content_path)\n${code}`;
315
358
  }
package/build/runtime.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { execSync } from "node:child_process";
2
+ import { existsSync } from "node:fs";
2
3
  const isWindows = process.platform === "win32";
3
4
  function commandExists(cmd) {
4
5
  try {
@@ -10,6 +11,39 @@ function commandExists(cmd) {
10
11
  return false;
11
12
  }
12
13
  }
14
+ /**
15
+ * On Windows, resolve the first non-WSL bash in PATH.
16
+ * WSL bash (C:\Windows\System32\bash.exe) cannot handle Windows paths,
17
+ * so we skip it and prefer Git Bash or MSYS2 bash instead.
18
+ */
19
+ function resolveWindowsBash() {
20
+ // First, try well-known Git Bash locations directly (works even when
21
+ // Git\usr\bin is not on PATH, which is common in MCP server environments
22
+ // that only inherit Git\cmd from the system PATH).
23
+ const knownPaths = [
24
+ "C:\\Program Files\\Git\\usr\\bin\\bash.exe",
25
+ "C:\\Program Files (x86)\\Git\\usr\\bin\\bash.exe",
26
+ ];
27
+ for (const p of knownPaths) {
28
+ if (existsSync(p))
29
+ return p;
30
+ }
31
+ // Fallback: scan PATH via `where bash`, skipping WSL and WindowsApps entries.
32
+ try {
33
+ const result = execSync("where bash", { encoding: "utf-8", stdio: "pipe" });
34
+ const candidates = result.trim().split(/\r?\n/).map(p => p.trim()).filter(Boolean);
35
+ for (const p of candidates) {
36
+ const lower = p.toLowerCase();
37
+ if (lower.includes("system32") || lower.includes("windowsapps"))
38
+ continue;
39
+ return p;
40
+ }
41
+ return null;
42
+ }
43
+ catch {
44
+ return null;
45
+ }
46
+ }
13
47
  function getVersion(cmd) {
14
48
  try {
15
49
  return execSync(`${cmd} --version`, {
@@ -40,7 +74,9 @@ export function detectRuntimes() {
40
74
  : commandExists("python")
41
75
  ? "python"
42
76
  : null,
43
- shell: commandExists("bash") ? "bash" : "sh",
77
+ shell: isWindows
78
+ ? (resolveWindowsBash() ?? (commandExists("sh") ? "sh" : commandExists("powershell") ? "powershell" : "cmd.exe"))
79
+ : commandExists("bash") ? "bash" : "sh",
44
80
  ruby: commandExists("ruby") ? "ruby" : null,
45
81
  go: commandExists("go") ? "go" : null,
46
82
  rust: commandExists("rustc") ? "rustc" : null,