context-mode 0.8.0 → 0.9.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,157 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Unified PreToolUse hook for context-mode
4
+ * Redirects data-fetching tools to context-mode MCP tools
5
+ *
6
+ * Cross-platform (Windows/macOS/Linux) — no bash/jq dependency.
7
+ */
8
+
9
+ let raw = "";
10
+ process.stdin.setEncoding("utf-8");
11
+ for await (const chunk of process.stdin) raw += chunk;
12
+
13
+ const input = JSON.parse(raw);
14
+ const tool = input.tool_name ?? "";
15
+ const toolInput = input.tool_input ?? {};
16
+
17
+ // ─── Bash: redirect data-fetching commands via updatedInput ───
18
+ if (tool === "Bash") {
19
+ const command = toolInput.command ?? "";
20
+
21
+ // curl/wget → replace with echo redirect
22
+ if (/(^|\s|&&|\||\;)(curl|wget)\s/i.test(command)) {
23
+ console.log(JSON.stringify({
24
+ hookSpecificOutput: {
25
+ hookEventName: "PreToolUse",
26
+ updatedInput: {
27
+ command: 'echo "context-mode: curl/wget blocked. You MUST use mcp__context-mode__fetch_and_index(url, source) to fetch URLs, or mcp__context-mode__execute(language, code) to run HTTP calls in sandbox. Do NOT retry with curl/wget."',
28
+ },
29
+ },
30
+ }));
31
+ process.exit(0);
32
+ }
33
+
34
+ // inline fetch (node -e, python -c, etc.) → replace with echo redirect
35
+ if (
36
+ /fetch\s*\(\s*['"](https?:\/\/|http)/i.test(command) ||
37
+ /requests\.(get|post|put)\s*\(/i.test(command) ||
38
+ /http\.(get|request)\s*\(/i.test(command)
39
+ ) {
40
+ console.log(JSON.stringify({
41
+ hookSpecificOutput: {
42
+ hookEventName: "PreToolUse",
43
+ updatedInput: {
44
+ command: 'echo "context-mode: Inline HTTP blocked. Use mcp__context-mode__execute(language, code) to run HTTP calls in sandbox, or mcp__context-mode__fetch_and_index(url, source) for web pages. Do NOT retry with Bash."',
45
+ },
46
+ },
47
+ }));
48
+ process.exit(0);
49
+ }
50
+
51
+ // allow all other Bash commands
52
+ process.exit(0);
53
+ }
54
+
55
+ // ─── Read: nudge toward execute_file ───
56
+ if (tool === "Read") {
57
+ console.log(JSON.stringify({
58
+ hookSpecificOutput: {
59
+ hookEventName: "PreToolUse",
60
+ additionalContext:
61
+ "CONTEXT TIP: If this file is large (>50 lines), prefer mcp__context-mode__execute_file(path, language, code) — processes in sandbox, only stdout enters context.",
62
+ },
63
+ }));
64
+ process.exit(0);
65
+ }
66
+
67
+ // ─── Grep: nudge toward execute ───
68
+ if (tool === "Grep") {
69
+ console.log(JSON.stringify({
70
+ hookSpecificOutput: {
71
+ hookEventName: "PreToolUse",
72
+ additionalContext:
73
+ 'CONTEXT TIP: If results may be large, prefer mcp__context-mode__execute(language: "shell", code: "grep ...") — runs in sandbox, only stdout enters context.',
74
+ },
75
+ }));
76
+ process.exit(0);
77
+ }
78
+
79
+ // ─── Glob: passthrough ───
80
+ if (tool === "Glob") {
81
+ process.exit(0);
82
+ }
83
+
84
+ // ─── WebFetch: deny + redirect to sandbox ───
85
+ if (tool === "WebFetch") {
86
+ const url = toolInput.url ?? "";
87
+ console.log(JSON.stringify({
88
+ hookSpecificOutput: {
89
+ hookEventName: "PreToolUse",
90
+ permissionDecision: "deny",
91
+ reason: `context-mode: WebFetch blocked. Use mcp__context-mode__fetch_and_index(url: "${url}", source: "...") to fetch this URL in sandbox. Then use mcp__context-mode__search(queries: [...]) to query results. Do NOT use curl/wget — they are also blocked.`,
92
+ },
93
+ }));
94
+ process.exit(0);
95
+ }
96
+
97
+ // ─── WebSearch: passthrough ───
98
+ if (tool === "WebSearch") {
99
+ process.exit(0);
100
+ }
101
+
102
+ // ─── Task: inject context-mode routing into subagent prompts ───
103
+ if (tool === "Task") {
104
+ const subagentType = toolInput.subagent_type ?? "";
105
+ const prompt = toolInput.prompt ?? "";
106
+
107
+ const ROUTING_BLOCK = `
108
+
109
+ ---
110
+ CONTEXT WINDOW PROTECTION — USE CONTEXT-MODE MCP TOOLS
111
+
112
+ Raw Bash/Read/WebFetch output floods your context. You have context-mode tools that keep data in sandbox.
113
+
114
+ STEP 1 — GATHER: mcp__context-mode__batch_execute(commands, queries)
115
+ commands: [{label: "Name", command: "shell cmd"}, ...]
116
+ queries: ["query1", "query2", ...] — put 5-8 queries covering everything you need.
117
+ Runs all commands, indexes output, returns search results. ONE call, no follow-ups.
118
+
119
+ STEP 2 — FOLLOW-UP: mcp__context-mode__search(queries: ["q1", "q2", "q3", ...])
120
+ Pass ALL follow-up questions as queries array. ONE call, not separate calls.
121
+
122
+ OTHER: execute(language, code) | execute_file(path, language, code) | fetch_and_index(url) + search
123
+
124
+ FORBIDDEN: Bash for output, Read for files, WebFetch. Bash is ONLY for git/mkdir/rm/mv.
125
+
126
+ OUTPUT FORMAT — KEEP YOUR FINAL RESPONSE UNDER 500 WORDS:
127
+ The parent agent context window is precious. Your full response gets injected into it.
128
+
129
+ 1. ARTIFACTS (PRDs, configs, code files) → Write to FILES, never return as inline text.
130
+ Return only: file path + 1-line description.
131
+ 2. DETAILED FINDINGS → Index into knowledge base:
132
+ mcp__context-mode__index(content: "...", source: "descriptive-label")
133
+ The parent agent shares the SAME knowledge base and can search() your indexed content.
134
+ 3. YOUR RESPONSE must be a concise summary:
135
+ - What you did (2-3 bullets)
136
+ - File paths created/modified (if any)
137
+ - Source labels you indexed (so parent can search)
138
+ - Key findings in bullet points
139
+ Do NOT return raw data, full file contents, or lengthy explanations.
140
+ ---`;
141
+
142
+ const updatedInput =
143
+ subagentType === "Bash"
144
+ ? { ...toolInput, prompt: prompt + ROUTING_BLOCK, subagent_type: "general-purpose" }
145
+ : { ...toolInput, prompt: prompt + ROUTING_BLOCK };
146
+
147
+ console.log(JSON.stringify({
148
+ hookSpecificOutput: {
149
+ hookEventName: "PreToolUse",
150
+ updatedInput,
151
+ },
152
+ }));
153
+ process.exit(0);
154
+ }
155
+
156
+ // Unknown tool — pass through
157
+ process.exit(0);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-mode",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "description": "Claude Code MCP plugin that saves 98% of your context window. Sandboxed code execution, FTS5 knowledge base, and intent-driven search.",
6
6
  "author": "Mert Koseoğlu",
@@ -32,7 +32,7 @@
32
32
  "skills",
33
33
  ".claude-plugin",
34
34
  ".mcp.json",
35
- "start.sh",
35
+ "start.mjs",
36
36
  "README.md",
37
37
  "LICENSE"
38
38
  ],