context-gatekeeper 0.3.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.
Files changed (305) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +774 -0
  3. package/README.zh.md +765 -0
  4. package/bin/context-gatekeeper-cli.js +60 -0
  5. package/dist/api/gdpr.d.ts +104 -0
  6. package/dist/api/gdpr.d.ts.map +1 -0
  7. package/dist/api/gdpr.js +229 -0
  8. package/dist/api/gdpr.js.map +1 -0
  9. package/dist/api/health-check.d.ts +13 -0
  10. package/dist/api/health-check.d.ts.map +1 -0
  11. package/dist/api/health-check.js +2 -0
  12. package/dist/api/health-check.js.map +1 -0
  13. package/dist/api/index.d.ts +7 -0
  14. package/dist/api/index.d.ts.map +1 -0
  15. package/dist/api/index.js +8 -0
  16. package/dist/api/index.js.map +1 -0
  17. package/dist/api/observability.d.ts +39 -0
  18. package/dist/api/observability.d.ts.map +1 -0
  19. package/dist/api/observability.js +132 -0
  20. package/dist/api/observability.js.map +1 -0
  21. package/dist/api/session-manager.d.ts +41 -0
  22. package/dist/api/session-manager.d.ts.map +1 -0
  23. package/dist/api/session-manager.js +129 -0
  24. package/dist/api/session-manager.js.map +1 -0
  25. package/dist/index.d.ts +2 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +7 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/mcp/server.d.ts +8 -0
  30. package/dist/mcp/server.d.ts.map +1 -0
  31. package/dist/mcp/server.js +613 -0
  32. package/dist/mcp/server.js.map +1 -0
  33. package/dist/mcp/tools/configure-llm.d.ts +26 -0
  34. package/dist/mcp/tools/configure-llm.d.ts.map +1 -0
  35. package/dist/mcp/tools/configure-llm.js +32 -0
  36. package/dist/mcp/tools/configure-llm.js.map +1 -0
  37. package/dist/mcp/tools/context-compress.d.ts +15 -0
  38. package/dist/mcp/tools/context-compress.d.ts.map +1 -0
  39. package/dist/mcp/tools/context-compress.js +15 -0
  40. package/dist/mcp/tools/context-compress.js.map +1 -0
  41. package/dist/mcp/tools/dual-mode-execute.d.ts +78 -0
  42. package/dist/mcp/tools/dual-mode-execute.d.ts.map +1 -0
  43. package/dist/mcp/tools/dual-mode-execute.js +299 -0
  44. package/dist/mcp/tools/dual-mode-execute.js.map +1 -0
  45. package/dist/mcp/tools/index.d.ts +19 -0
  46. package/dist/mcp/tools/index.d.ts.map +1 -0
  47. package/dist/mcp/tools/index.js +20 -0
  48. package/dist/mcp/tools/index.js.map +1 -0
  49. package/dist/mcp/tools/intelligent-recall.d.ts +67 -0
  50. package/dist/mcp/tools/intelligent-recall.d.ts.map +1 -0
  51. package/dist/mcp/tools/intelligent-recall.js +208 -0
  52. package/dist/mcp/tools/intelligent-recall.js.map +1 -0
  53. package/dist/mcp/tools/memory-anchor.d.ts +13 -0
  54. package/dist/mcp/tools/memory-anchor.d.ts.map +1 -0
  55. package/dist/mcp/tools/memory-anchor.js +16 -0
  56. package/dist/mcp/tools/memory-anchor.js.map +1 -0
  57. package/dist/mcp/tools/memory-delete-batch.d.ts +16 -0
  58. package/dist/mcp/tools/memory-delete-batch.d.ts.map +1 -0
  59. package/dist/mcp/tools/memory-delete-batch.js +26 -0
  60. package/dist/mcp/tools/memory-delete-batch.js.map +1 -0
  61. package/dist/mcp/tools/memory-extract.d.ts +68 -0
  62. package/dist/mcp/tools/memory-extract.d.ts.map +1 -0
  63. package/dist/mcp/tools/memory-extract.js +280 -0
  64. package/dist/mcp/tools/memory-extract.js.map +1 -0
  65. package/dist/mcp/tools/memory-recall.d.ts +42 -0
  66. package/dist/mcp/tools/memory-recall.d.ts.map +1 -0
  67. package/dist/mcp/tools/memory-recall.js +37 -0
  68. package/dist/mcp/tools/memory-recall.js.map +1 -0
  69. package/dist/mcp/tools/memory-report-usage.d.ts +17 -0
  70. package/dist/mcp/tools/memory-report-usage.d.ts.map +1 -0
  71. package/dist/mcp/tools/memory-report-usage.js +15 -0
  72. package/dist/mcp/tools/memory-report-usage.js.map +1 -0
  73. package/dist/mcp/tools/memory-search.d.ts +43 -0
  74. package/dist/mcp/tools/memory-search.d.ts.map +1 -0
  75. package/dist/mcp/tools/memory-search.js +38 -0
  76. package/dist/mcp/tools/memory-search.js.map +1 -0
  77. package/dist/mcp/tools/memory-session.d.ts +118 -0
  78. package/dist/mcp/tools/memory-session.d.ts.map +1 -0
  79. package/dist/mcp/tools/memory-session.js +113 -0
  80. package/dist/mcp/tools/memory-session.js.map +1 -0
  81. package/dist/mcp/tools/memory-stats.d.ts +10 -0
  82. package/dist/mcp/tools/memory-stats.d.ts.map +1 -0
  83. package/dist/mcp/tools/memory-stats.js +35 -0
  84. package/dist/mcp/tools/memory-stats.js.map +1 -0
  85. package/dist/mcp/tools/memory-store-batch.d.ts +49 -0
  86. package/dist/mcp/tools/memory-store-batch.d.ts.map +1 -0
  87. package/dist/mcp/tools/memory-store-batch.js +48 -0
  88. package/dist/mcp/tools/memory-store-batch.js.map +1 -0
  89. package/dist/mcp/tools/memory-store.d.ts +37 -0
  90. package/dist/mcp/tools/memory-store.d.ts.map +1 -0
  91. package/dist/mcp/tools/memory-store.js +34 -0
  92. package/dist/mcp/tools/memory-store.js.map +1 -0
  93. package/dist/mcp/tools/project-create.d.ts +16 -0
  94. package/dist/mcp/tools/project-create.d.ts.map +1 -0
  95. package/dist/mcp/tools/project-create.js +14 -0
  96. package/dist/mcp/tools/project-create.js.map +1 -0
  97. package/dist/models/types.d.ts +88 -0
  98. package/dist/models/types.d.ts.map +1 -0
  99. package/dist/models/types.js +19 -0
  100. package/dist/models/types.js.map +1 -0
  101. package/dist/schema/compression.d.ts +7 -0
  102. package/dist/schema/compression.d.ts.map +1 -0
  103. package/dist/schema/compression.js +66 -0
  104. package/dist/schema/compression.js.map +1 -0
  105. package/dist/schema/fulltext-search.d.ts +10 -0
  106. package/dist/schema/fulltext-search.d.ts.map +1 -0
  107. package/dist/schema/fulltext-search.js +73 -0
  108. package/dist/schema/fulltext-search.js.map +1 -0
  109. package/dist/schema/index.d.ts +9 -0
  110. package/dist/schema/index.d.ts.map +1 -0
  111. package/dist/schema/index.js +9 -0
  112. package/dist/schema/index.js.map +1 -0
  113. package/dist/schema/knowledge-graph.d.ts +108 -0
  114. package/dist/schema/knowledge-graph.d.ts.map +1 -0
  115. package/dist/schema/knowledge-graph.js +372 -0
  116. package/dist/schema/knowledge-graph.js.map +1 -0
  117. package/dist/schema/memory-session.d.ts +62 -0
  118. package/dist/schema/memory-session.d.ts.map +1 -0
  119. package/dist/schema/memory-session.js +258 -0
  120. package/dist/schema/memory-session.js.map +1 -0
  121. package/dist/schema/memory.d.ts +84 -0
  122. package/dist/schema/memory.d.ts.map +1 -0
  123. package/dist/schema/memory.js +622 -0
  124. package/dist/schema/memory.js.map +1 -0
  125. package/dist/schema/project.d.ts +8 -0
  126. package/dist/schema/project.d.ts.map +1 -0
  127. package/dist/schema/project.js +68 -0
  128. package/dist/schema/project.js.map +1 -0
  129. package/dist/schema/schema-init.d.ts +2 -0
  130. package/dist/schema/schema-init.d.ts.map +1 -0
  131. package/dist/schema/schema-init.js +199 -0
  132. package/dist/schema/schema-init.js.map +1 -0
  133. package/dist/schema/vector-index.d.ts +28 -0
  134. package/dist/schema/vector-index.d.ts.map +1 -0
  135. package/dist/schema/vector-index.js +179 -0
  136. package/dist/schema/vector-index.js.map +1 -0
  137. package/dist/scripts/agents/base.d.ts +89 -0
  138. package/dist/scripts/agents/base.d.ts.map +1 -0
  139. package/dist/scripts/agents/base.js +148 -0
  140. package/dist/scripts/agents/base.js.map +1 -0
  141. package/dist/scripts/agents/base.ts +193 -0
  142. package/dist/scripts/agents/claude-code.d.ts +21 -0
  143. package/dist/scripts/agents/claude-code.d.ts.map +1 -0
  144. package/dist/scripts/agents/claude-code.js +33 -0
  145. package/dist/scripts/agents/claude-code.js.map +1 -0
  146. package/dist/scripts/agents/claude-code.ts +36 -0
  147. package/dist/scripts/agents/claude-desktop.d.ts +25 -0
  148. package/dist/scripts/agents/claude-desktop.d.ts.map +1 -0
  149. package/dist/scripts/agents/claude-desktop.js +36 -0
  150. package/dist/scripts/agents/claude-desktop.js.map +1 -0
  151. package/dist/scripts/agents/claude-desktop.ts +39 -0
  152. package/dist/scripts/agents/cline.d.ts +22 -0
  153. package/dist/scripts/agents/cline.d.ts.map +1 -0
  154. package/dist/scripts/agents/cline.js +35 -0
  155. package/dist/scripts/agents/cline.js.map +1 -0
  156. package/dist/scripts/agents/cline.ts +38 -0
  157. package/dist/scripts/agents/continue.d.ts +20 -0
  158. package/dist/scripts/agents/continue.d.ts.map +1 -0
  159. package/dist/scripts/agents/continue.js +35 -0
  160. package/dist/scripts/agents/continue.js.map +1 -0
  161. package/dist/scripts/agents/continue.ts +38 -0
  162. package/dist/scripts/agents/cursor.d.ts +27 -0
  163. package/dist/scripts/agents/cursor.d.ts.map +1 -0
  164. package/dist/scripts/agents/cursor.js +38 -0
  165. package/dist/scripts/agents/cursor.js.map +1 -0
  166. package/dist/scripts/agents/cursor.ts +41 -0
  167. package/dist/scripts/cli/config-gen.d.ts +59 -0
  168. package/dist/scripts/cli/config-gen.d.ts.map +1 -0
  169. package/dist/scripts/cli/config-gen.js +156 -0
  170. package/dist/scripts/cli/config-gen.js.map +1 -0
  171. package/dist/scripts/cli/config-gen.ts +164 -0
  172. package/dist/scripts/cli/detect.d.ts +42 -0
  173. package/dist/scripts/cli/detect.d.ts.map +1 -0
  174. package/dist/scripts/cli/detect.js +131 -0
  175. package/dist/scripts/cli/detect.js.map +1 -0
  176. package/dist/scripts/cli/detect.ts +162 -0
  177. package/dist/scripts/cli/install.d.ts +31 -0
  178. package/dist/scripts/cli/install.d.ts.map +1 -0
  179. package/dist/scripts/cli/install.js +125 -0
  180. package/dist/scripts/cli/install.js.map +1 -0
  181. package/dist/scripts/cli/install.ts +157 -0
  182. package/dist/scripts/cli/status.d.ts +8 -0
  183. package/dist/scripts/cli/status.d.ts.map +1 -0
  184. package/dist/scripts/cli/status.js +39 -0
  185. package/dist/scripts/cli/status.js.map +1 -0
  186. package/dist/scripts/cli/status.ts +48 -0
  187. package/dist/scripts/cli/uninstall.d.ts +22 -0
  188. package/dist/scripts/cli/uninstall.d.ts.map +1 -0
  189. package/dist/scripts/cli/uninstall.js +141 -0
  190. package/dist/scripts/cli/uninstall.js.map +1 -0
  191. package/dist/scripts/cli/uninstall.ts +157 -0
  192. package/dist/scripts/cli.d.ts +23 -0
  193. package/dist/scripts/cli.d.ts.map +1 -0
  194. package/dist/scripts/cli.js +166 -0
  195. package/dist/scripts/cli.js.map +1 -0
  196. package/dist/scripts/cli.ts +173 -0
  197. package/dist/services/classifier/index.d.ts +36 -0
  198. package/dist/services/classifier/index.d.ts.map +1 -0
  199. package/dist/services/classifier/index.js +104 -0
  200. package/dist/services/classifier/index.js.map +1 -0
  201. package/dist/services/classifier/llm.d.ts +37 -0
  202. package/dist/services/classifier/llm.d.ts.map +1 -0
  203. package/dist/services/classifier/llm.js +119 -0
  204. package/dist/services/classifier/llm.js.map +1 -0
  205. package/dist/services/classifier/rules.d.ts +22 -0
  206. package/dist/services/classifier/rules.d.ts.map +1 -0
  207. package/dist/services/classifier/rules.js +98 -0
  208. package/dist/services/classifier/rules.js.map +1 -0
  209. package/dist/services/compressor/index.d.ts +3 -0
  210. package/dist/services/compressor/index.d.ts.map +1 -0
  211. package/dist/services/compressor/index.js +3 -0
  212. package/dist/services/compressor/index.js.map +1 -0
  213. package/dist/services/compressor/threshold.d.ts +35 -0
  214. package/dist/services/compressor/threshold.d.ts.map +1 -0
  215. package/dist/services/compressor/threshold.js +60 -0
  216. package/dist/services/compressor/threshold.js.map +1 -0
  217. package/dist/services/compressor/trigger.d.ts +24 -0
  218. package/dist/services/compressor/trigger.d.ts.map +1 -0
  219. package/dist/services/compressor/trigger.js +91 -0
  220. package/dist/services/compressor/trigger.js.map +1 -0
  221. package/dist/services/constraint-extractor.d.ts +25 -0
  222. package/dist/services/constraint-extractor.d.ts.map +1 -0
  223. package/dist/services/constraint-extractor.js +97 -0
  224. package/dist/services/constraint-extractor.js.map +1 -0
  225. package/dist/services/database-health.d.ts +22 -0
  226. package/dist/services/database-health.d.ts.map +1 -0
  227. package/dist/services/database-health.js +122 -0
  228. package/dist/services/database-health.js.map +1 -0
  229. package/dist/services/embedding-fixed.d.ts +9 -0
  230. package/dist/services/embedding-fixed.d.ts.map +1 -0
  231. package/dist/services/embedding-fixed.js +70 -0
  232. package/dist/services/embedding-fixed.js.map +1 -0
  233. package/dist/services/embedding-provider.d.ts +79 -0
  234. package/dist/services/embedding-provider.d.ts.map +1 -0
  235. package/dist/services/embedding-provider.js +229 -0
  236. package/dist/services/embedding-provider.js.map +1 -0
  237. package/dist/services/embedding.d.ts +17 -0
  238. package/dist/services/embedding.d.ts.map +1 -0
  239. package/dist/services/embedding.js +99 -0
  240. package/dist/services/embedding.js.map +1 -0
  241. package/dist/services/hnsw-index.d.ts +76 -0
  242. package/dist/services/hnsw-index.d.ts.map +1 -0
  243. package/dist/services/hnsw-index.js +301 -0
  244. package/dist/services/hnsw-index.js.map +1 -0
  245. package/dist/services/llm.d.ts +39 -0
  246. package/dist/services/llm.d.ts.map +1 -0
  247. package/dist/services/llm.js +207 -0
  248. package/dist/services/llm.js.map +1 -0
  249. package/dist/services/memory-tiers.d.ts +75 -0
  250. package/dist/services/memory-tiers.d.ts.map +1 -0
  251. package/dist/services/memory-tiers.js +275 -0
  252. package/dist/services/memory-tiers.js.map +1 -0
  253. package/dist/services/memory.d.ts +33 -0
  254. package/dist/services/memory.d.ts.map +1 -0
  255. package/dist/services/memory.js +209 -0
  256. package/dist/services/memory.js.map +1 -0
  257. package/dist/services/multi-agent-sharing.d.ts +83 -0
  258. package/dist/services/multi-agent-sharing.d.ts.map +1 -0
  259. package/dist/services/multi-agent-sharing.js +278 -0
  260. package/dist/services/multi-agent-sharing.js.map +1 -0
  261. package/dist/services/reranker.d.ts +88 -0
  262. package/dist/services/reranker.d.ts.map +1 -0
  263. package/dist/services/reranker.js +234 -0
  264. package/dist/services/reranker.js.map +1 -0
  265. package/dist/services/triple-extractor.d.ts +35 -0
  266. package/dist/services/triple-extractor.d.ts.map +1 -0
  267. package/dist/services/triple-extractor.js +293 -0
  268. package/dist/services/triple-extractor.js.map +1 -0
  269. package/dist/services/vector-provider.d.ts +40 -0
  270. package/dist/services/vector-provider.d.ts.map +1 -0
  271. package/dist/services/vector-provider.js +225 -0
  272. package/dist/services/vector-provider.js.map +1 -0
  273. package/dist/utils/after-chain-executor.d.ts +26 -0
  274. package/dist/utils/after-chain-executor.d.ts.map +1 -0
  275. package/dist/utils/after-chain-executor.js +135 -0
  276. package/dist/utils/after-chain-executor.js.map +1 -0
  277. package/dist/utils/after-chain.d.ts +94 -0
  278. package/dist/utils/after-chain.d.ts.map +1 -0
  279. package/dist/utils/after-chain.js +155 -0
  280. package/dist/utils/after-chain.js.map +1 -0
  281. package/dist/utils/db.d.ts +29 -0
  282. package/dist/utils/db.d.ts.map +1 -0
  283. package/dist/utils/db.js +201 -0
  284. package/dist/utils/db.js.map +1 -0
  285. package/dist/utils/encryption.d.ts +87 -0
  286. package/dist/utils/encryption.d.ts.map +1 -0
  287. package/dist/utils/encryption.js +175 -0
  288. package/dist/utils/encryption.js.map +1 -0
  289. package/dist/utils/errors.d.ts +35 -0
  290. package/dist/utils/errors.d.ts.map +1 -0
  291. package/dist/utils/errors.js +56 -0
  292. package/dist/utils/errors.js.map +1 -0
  293. package/dist/utils/logger.d.ts +27 -0
  294. package/dist/utils/logger.d.ts.map +1 -0
  295. package/dist/utils/logger.js +177 -0
  296. package/dist/utils/logger.js.map +1 -0
  297. package/dist/utils/priority.d.ts +26 -0
  298. package/dist/utils/priority.d.ts.map +1 -0
  299. package/dist/utils/priority.js +43 -0
  300. package/dist/utils/priority.js.map +1 -0
  301. package/dist/utils/watchdog.d.ts +57 -0
  302. package/dist/utils/watchdog.d.ts.map +1 -0
  303. package/dist/utils/watchdog.js +164 -0
  304. package/dist/utils/watchdog.js.map +1 -0
  305. package/package.json +78 -0
package/README.md ADDED
@@ -0,0 +1,774 @@
1
+ # Context Gatekeeper
2
+
3
+ MCP-based context management service for AI agents - keeps agents in the **100k token smart zone**.
4
+
5
+ Universal installation - works with **all MCP-compatible agents**: Cursor, Claude Desktop, Cline, Continue, Claude Code, and more.
6
+
7
+ > **Note**: 中文用户请参考 [README.zh.md](README.zh.md)
8
+
9
+ ## Table of Contents
10
+
11
+ - [Quick Start](#quick-start)
12
+ - [Features](#features)
13
+ - [Installation](#installation)
14
+ - [Configuration](#configuration)
15
+ - [Tools Reference](#tools-reference)
16
+ - [Cross-Agent Compatibility Tests](#cross-agent-compatibility-tests)
17
+ - [Architecture](#architecture)
18
+ - [Troubleshooting](#troubleshooting)
19
+ - [Best Practices](#best-practices)
20
+ - [Development](#development)
21
+
22
+ ## Quick Start
23
+
24
+ ### 1. Install via the CLI
25
+
26
+ The CLI auto-detects every supported agent on your machine and writes the
27
+ right config block to each agent's expected config file. No manual JSON.
28
+
29
+ ```bash
30
+ # Install to all detected agents (recommended)
31
+ npx -y context-gatekeeper-cli install --all
32
+
33
+ # Or pick specific agents
34
+ npx -y context-gatekeeper-cli install cursor claude-desktop claude-code
35
+
36
+ # See what the CLI would touch
37
+ npx -y context-gatekeeper-cli status
38
+
39
+ # Remove the entries later
40
+ npx -y context-gatekeeper-cli uninstall --all
41
+ ```
42
+
43
+ Supported agents: `cursor`, `claude-desktop`, `cline`, `continue`, `claude-code`.
44
+ Pass `--local` to write project-level configs (e.g. `./.cursor/mcp.json`) instead
45
+ of the global ones. `claude-desktop` is always global-only.
46
+
47
+ ### 2. Manual Configuration (advanced)
48
+
49
+ If you prefer to write the JSON yourself, every supported agent accepts
50
+ the same launch command (`npx -y context-gatekeeper`).
51
+
52
+ **Cursor** (`.cursor/mcp.json`):
53
+ ```json
54
+ {
55
+ "mcpServers": {
56
+ "context-gatekeeper": {
57
+ "command": "npx",
58
+ "args": ["-y", "context-gatekeeper"]
59
+ }
60
+ }
61
+ }
62
+ ```
63
+
64
+ **Claude Desktop** (`~/Library/Application Support/Claude/claude_desktop_config.json`):
65
+ ```json
66
+ {
67
+ "mcpServers": {
68
+ "context-gatekeeper": {
69
+ "command": "npx",
70
+ "args": ["-y", "context-gatekeeper"]
71
+ }
72
+ }
73
+ }
74
+ ```
75
+
76
+ **Claude Code** (`.mcp.json`):
77
+ ```json
78
+ {
79
+ "mcpServers": {
80
+ "context-gatekeeper": {
81
+ "command": "npx",
82
+ "args": ["-y", "context-gatekeeper"]
83
+ }
84
+ }
85
+ }
86
+ ```
87
+
88
+ On Windows, GUI apps (Cursor, Claude Desktop) may not inherit shell PATH.
89
+ If `npx` cannot be found, either wrap with `cmd /c` or use the absolute
90
+ path returned by `where npx`.
91
+
92
+ ### 3. Set Environment Variables (Optional)
93
+
94
+ ```bash
95
+ # Authentication (Watchdog Security)
96
+ export CG_READ_TOKEN="your-read-only-token"
97
+ export CG_WRITE_TOKEN="your-write-token"
98
+ export CG_WATCHDOG_TOKEN="your-watchdog-token"
99
+
100
+ # For semantic search (optional - defaults to TF-IDF)
101
+ export OPENAI_API_KEY="your-openai-key"
102
+ # or
103
+ export COHERE_API_KEY="your-cohere-key"
104
+ ```
105
+
106
+ ### 4. Use It
107
+
108
+ ```typescript
109
+ // Store a constraint
110
+ memory_store({
111
+ content: "Use TypeScript strict mode for all new files",
112
+ priority: "constraint",
113
+ project_tags: ["typescript", "code-style"]
114
+ });
115
+
116
+ // Recall relevant memories
117
+ memory_recall({
118
+ query: "TypeScript configuration",
119
+ limit: 5,
120
+ search_mode: "hybrid"
121
+ });
122
+ ```
123
+
124
+ ## Features
125
+
126
+ - **Universal MCP**: Works with all MCP-compatible agents
127
+ - **14+ Tools**: Store, recall, search, anchor, compress, batch operations, intelligent recall, dual-mode execution
128
+ - **Smart Search**: Keyword + semantic (TF-IDF/OpenAI/Cohere) + hybrid + BM25 search
129
+ - **Priority System**: anchored > constraint > decision > preference > fact
130
+ - **Auto Deduplication**: SHA256 hash-based duplicate detection
131
+ - **Context Compression**: Automatic cleanup of low-priority memories
132
+ - **Watchdog Security**: Token-based permission control for tools
133
+ - **After-Chain Automation**: Auto-trigger tools after other tools complete
134
+ - **Pure JavaScript**: No native compilation required (sql.js)
135
+ - **Structured Logging**: JSON output for log aggregation
136
+
137
+ ### Phase 1: AutoSkill-Style Constraint Extraction
138
+
139
+ Analyzes conversation turns to extract durable constraints:
140
+
141
+ ```typescript
142
+ memory_extract({
143
+ conversation_turns: [
144
+ { role: "user", content: "I always prefer using TypeScript strict mode." },
145
+ { role: "assistant", content: "I'll enable strict mode." }
146
+ ],
147
+ extract_mode: "all",
148
+ min_confidence: 0.5
149
+ });
150
+ ```
151
+
152
+ ### Phase 2: MemGate-Style Intelligent Recall
153
+
154
+ Semantic similarity + learned relevance patterns:
155
+
156
+ ```typescript
157
+ intelligent_recall({
158
+ query: "API implementation guidelines",
159
+ conversation_context: "Building a new REST service",
160
+ relevance_threshold: 0.07,
161
+ enable_soft_guidance: true
162
+ });
163
+ ```
164
+
165
+ ### Phase 3: MPR-Style Dual-Mode Execution
166
+
167
+ Soft guidance + hard admissibility checks:
168
+
169
+ ```typescript
170
+ dual_mode_execute({
171
+ action: "Use var keyword for this variable",
172
+ context: "Writing JavaScript code",
173
+ mode: "dual",
174
+ soft_guidance_style: "concise"
175
+ });
176
+ ```
177
+
178
+ ## Installation
179
+
180
+ ### One-command install via the CLI
181
+
182
+ The package ships with a `context-gatekeeper-cli` binary that detects
183
+ every supported MCP agent on your machine and writes the right entry
184
+ into each agent's config file. See [Quick Start](#quick-start) above
185
+ for the commands. To uninstall, run `context-gatekeeper-cli uninstall`.
186
+
187
+ ### Global Installation
188
+
189
+ ```bash
190
+ npm install -g context-gatekeeper
191
+ npm run build
192
+ ```
193
+
194
+ To find the global installation path:
195
+ ```bash
196
+ npm root -g
197
+ ```
198
+
199
+ ### Local Installation
200
+
201
+ ```bash
202
+ npm install context-gatekeeper
203
+ npm run build
204
+ ```
205
+
206
+ ### Verify Installation
207
+
208
+ ```bash
209
+ node dist/mcp/server.js --help
210
+ ```
211
+
212
+ ### CLI Reference
213
+
214
+ The `context-gatekeeper-cli` command supports:
215
+
216
+ ```
217
+ context-gatekeeper-cli install [agent ...] [--all] [--local] [--cwd <dir>]
218
+ context-gatekeeper-cli uninstall [agent ...] [--all] [--local] [--cwd <dir>]
219
+ context-gatekeeper-cli status [--local] [--cwd <dir>]
220
+ context-gatekeeper-cli help
221
+ ```
222
+
223
+ Supported agents: `cursor`, `claude-desktop`, `cline`, `continue`, `claude-code`.
224
+
225
+ - `--all` targets every agent. With no positional args and no `--all`,
226
+ the CLI prints an error.
227
+ - `--local` writes the project-level config (e.g. `./.cursor/mcp.json`)
228
+ instead of the global one. `claude-desktop` is always global and is
229
+ skipped under `--local`.
230
+ - `--cwd <dir>` overrides the project root used for `--local`. Useful
231
+ for scripted setup in CI or pre-commit hooks.
232
+
233
+ ## Configuration
234
+
235
+ ### Environment Variables
236
+
237
+ #### Authentication (Watchdog Security)
238
+
239
+ | Variable | Description | Required |
240
+ |----------|-------------|----------|
241
+ | `CG_READ_TOKEN` | Token for read-only operations | No |
242
+ | `CG_WRITE_TOKEN` | Token for read/write operations | No |
243
+ | `CG_WATCHDOG_TOKEN` | Token with full access (bypasses restrictions) | No |
244
+
245
+ If no tokens are set, the server operates in permissive mode (all tools accessible).
246
+
247
+ #### Embedding Providers
248
+
249
+ | Variable | Description | Default |
250
+ |----------|-------------|---------|
251
+ | `OPENAI_API_KEY` | OpenAI API key for embeddings | - |
252
+ | `COHERE_API_KEY` | Cohere API key for embeddings | - |
253
+ | `OPENAI_EMBEDDING_BASE_URL` | Custom OpenAI-compatible endpoint | https://api.openai.com/v1 |
254
+ | `CG_EMBEDDING_PROVIDER` | Embedding provider: `tfidf`, `openai`, `cohere` | `tfidf` |
255
+
256
+ #### Logging
257
+
258
+ | Variable | Description | Default |
259
+ |----------|-------------|---------|
260
+ | `CG_LOG_LEVEL` | Minimum log level: `trace`, `debug`, `info`, `warn`, `error` | `info` |
261
+ | `CG_LOG_PRETTY` | Pretty-print JSON output (`true` or `1`) | - |
262
+ | `CG_LOG_TO_FILE` | Enable file-based logging (`true` or `1`) | - |
263
+ | `CG_LOG_FILE_PATH` | Custom log file path | `context-gatekeeper.log` |
264
+
265
+ #### Database
266
+
267
+ | Variable | Description | Default |
268
+ |----------|-------------|---------|
269
+ | `DATA_DIR` | Database file location | Platform-specific |
270
+ | `CG_DB_FLUSH_INTERVAL_MS` | How often to flush to disk (ms) | 30000 |
271
+
272
+ Default `DATA_DIR` locations:
273
+ - **Windows**: `%APPDATA%/context-gatekeeper`
274
+ - **macOS**: `~/Library/Application Support/context-gatekeeper`
275
+ - **Linux**: `~/.context-gatekeeper`
276
+
277
+ ### Runtime Configuration
278
+
279
+ Use the `configure_llm` tool to set LLM settings at runtime:
280
+
281
+ ```typescript
282
+ // Configure OpenAI
283
+ configure_llm({
284
+ provider: "openai",
285
+ apiKey: "sk-...",
286
+ model: "gpt-4"
287
+ });
288
+
289
+ // Configure Ollama (local)
290
+ configure_llm({
291
+ provider: "ollama",
292
+ model: "llama3.2",
293
+ baseUrl: "http://localhost:11434"
294
+ });
295
+ ```
296
+
297
+ ## Tools Reference
298
+
299
+ ### Memory Operations
300
+
301
+ | Tool | Description |
302
+ |------|-------------|
303
+ | `memory_store` | Store a new memory with priority |
304
+ | `memory_recall` | Recall memories (4 search modes) |
305
+ | `memory_search` | Full-text search memories |
306
+ | `memory_anchor` | Anchor memory permanently |
307
+ | `memory_stats` | Get memory statistics |
308
+
309
+ ### Batch Operations
310
+
311
+ | Tool | Description |
312
+ |------|-------------|
313
+ | `memory_store_batch` | Batch store memories |
314
+ | `memory_delete_batch` | Batch delete memories |
315
+
316
+ ### Context Management
317
+
318
+ | Tool | Description |
319
+ |------|-------------|
320
+ | `memory_report_usage` | Report token usage |
321
+ | `context_compress` | Trigger context compression |
322
+
323
+ ### Intelligent Recall (Phase 2)
324
+
325
+ | Tool | Description |
326
+ |------|-------------|
327
+ | `intelligent_recall` | MemGate-style relevance recall |
328
+
329
+ ### Constraint Extraction (Phase 1)
330
+
331
+ | Tool | Description |
332
+ |------|-------------|
333
+ | `memory_extract` | Extract constraints from conversations |
334
+
335
+ ### Dual-Mode Execution (Phase 3)
336
+
337
+ | Tool | Description |
338
+ |------|-------------|
339
+ | `dual_mode_execute` | Validate actions with dual mode |
340
+
341
+ ### Session Management
342
+
343
+ | Tool | Description |
344
+ |------|-------------|
345
+ | `session_store` | Store session data |
346
+ | `session_get` | Get session data |
347
+ | `session_list` | List session keys |
348
+ | `session_delete` | Delete session data |
349
+
350
+ ### Configuration & Infrastructure
351
+
352
+ | Tool | Description |
353
+ |------|-------------|
354
+ | `configure_llm` | Configure LLM provider |
355
+ | `after_chain_configure` | Configure after-chain orchestration |
356
+ | `project_create` | Create a project |
357
+ | `db_flush` | Flush in-memory DB to disk |
358
+ | `watchdog_manage` | Manage watchdog tokens |
359
+
360
+ ### GDPR Compliance
361
+
362
+ | Tool | Description |
363
+ |------|-------------|
364
+ | `gdpr_export` | Export all memories as JSON |
365
+ | `gdpr_delete` | Delete memories |
366
+ | `data_summary` | Get data summary |
367
+
368
+ ### Search Modes
369
+
370
+ The `memory_recall` tool supports four search modes:
371
+
372
+ | Mode | Description | Best For |
373
+ |------|-------------|----------|
374
+ | `keyword` | Simple substring matching | Fast, exact matches |
375
+ | `semantic` | TF-IDF/OpenAI/Cohere embeddings | Meaning-based recall |
376
+ | `hybrid` | Combines keyword + semantic | Balanced accuracy |
377
+ | `bm25` | Okapi BM25 ranking | Text retrieval |
378
+
379
+ ### After-Chain Configuration
380
+
381
+ ```typescript
382
+ // List all chains
383
+ after_chain_configure({ action: "list_chains" });
384
+
385
+ // Disable a chain
386
+ after_chain_configure({
387
+ action: "toggle_chain",
388
+ chain_name: "store-then-extract",
389
+ enabled: false
390
+ });
391
+
392
+ // Register a custom chain
393
+ after_chain_configure({
394
+ action: "register_chain",
395
+ chain_config: {
396
+ name: "store-then-recall",
397
+ triggerTool: "memory_store",
398
+ followupTool: "memory_recall",
399
+ async: true
400
+ }
401
+ });
402
+ ```
403
+
404
+ ## Cross-Agent Compatibility Tests
405
+
406
+ Beyond unit tests, Context Gatekeeper ships with an end-to-end compatibility harness that verifies the MCP server behaves correctly under the **exact same JSON-RPC contract** that each supported agent runtime uses.
407
+
408
+ ### What It Tests
409
+
410
+ For every supported agent, the harness exercises four scenario classes against a freshly spawned MCP server process:
411
+
412
+ | Class | Coverage | Why It Matters |
413
+ |-------|----------|----------------|
414
+ | **Handshake** | `initialize` + `tools/list` + a sample read tool call | Confirms the MCP server boots, advertises 24 tools, and answers JSON-RPC correctly |
415
+ | **CRUD** | `memory_store` -> `memory_recall` round-trip, all 4 search modes (`keyword`, `semantic`, `hybrid`, `auto`), `memory_delete_batch`, `memory_anchor` | Validates the core storage path works under the agent's stdio transport |
416
+ | **After-Chain** | `after_chain_configure` list/toggle + `memory_store` triggering `memory_extract` followup | Confirms the in-process chain executor wires correctly across agent boundaries |
417
+ | **Watchdog** | 4x4 permission matrix: `no-token-strict`, `read-token`, `write-token`, `watchdog-token` against read and write probes | Ensures token enforcement matches the security model |
418
+
419
+ ### Supported Agents
420
+
421
+ - **Cursor** - uses `.cursor/mcp.json` shape
422
+ - **Claude Desktop** - uses `claude_desktop_config.json` shape
423
+ - **Cline** - VSCode workspace MCP server config
424
+ - **Continue.dev** - uses `experimental.modelContextProtocolServers` shape
425
+ - **Claude Code** - uses `.mcp.json` shape
426
+
427
+ ### Running the Tests
428
+
429
+ ```bash
430
+ # Run against all 5 agents
431
+ npm run test:agents
432
+
433
+ # Run against a single agent
434
+ npm run test:agents -- cursor
435
+
436
+ # Run against a subset
437
+ npm run test:agents -- cursor cline claude-code
438
+ ```
439
+
440
+ ### How It Works
441
+
442
+ Each adapter writes the agent's exact MCP config JSON to a temp directory, then spawns the MCP server using the same `command` + `args` that the agent runtime would use. The harness sends real MCP JSON-RPC messages over stdio - the same wire protocol Cursor, Claude Desktop, etc. use internally.
443
+
444
+ Key design choices:
445
+
446
+ - **One process per scenario**: every test case spawns a fresh MCP server, so failures point to a specific scenario, not shared state
447
+ - **Isolated `DATA_DIR`**: each agent gets its own database directory, eliminating cross-test contamination
448
+ - **Token injection via env vars**: `CG_READ_TOKEN`, `CG_WRITE_TOKEN`, `CG_WATCHDOG_TOKEN` are passed as environment variables, matching production deployment
449
+ - **Two report formats**: JSON for CI integration, Markdown for human review
450
+
451
+ ### Report Output
452
+
453
+ After a run, two files appear under `scripts/reports/`:
454
+
455
+ ```
456
+ scripts/reports/report-<timestamp>.json # Machine-readable
457
+ scripts/reports/report-<timestamp>.md # Human-readable
458
+ ```
459
+
460
+ The Markdown report groups results by agent:
461
+
462
+ ```
463
+ ## cursor
464
+ Status: 14/14 passed
465
+
466
+ | Tool | Success | Latency (ms) | Error |
467
+ |------|---------|--------------|-------|
468
+ | initialize+memory_stats | OK | 528 | |
469
+ | memory_store | OK | 9 | |
470
+ ...
471
+ ```
472
+
473
+ ### Sample Run
474
+
475
+ ```
476
+ [cursor] handshake ... handshakeOk=true tools=24 sampleOk=true 528ms
477
+ [cursor] CRUD ... 8/8 scenarios passed (2508ms)
478
+ [cursor] after-chain ... allOk=true 479ms
479
+ [cursor] watchdog ... 4/4 cells ok (9985ms)
480
+
481
+ [cross-agent-test] Summary:
482
+ Total: 70
483
+ Passed: 70
484
+ Failed: 0
485
+ ```
486
+
487
+ ### Adding a New Agent
488
+
489
+ 1. Create `scripts/agents/my-agent.ts` extending `BaseAgentAdapter`
490
+ 2. Implement `name` and `buildSpawnCommand()` (or override `spawnAgent()`)
491
+ 3. Add a static `buildMcpConfig(mcpBin)` that returns the agent's exact config shape
492
+ 4. Register the adapter in `scripts/cross-agent-test.ts`
493
+
494
+ The harness automatically picks up any new adapter via `buildAdapters()`.
495
+
496
+ ## Architecture
497
+
498
+ ### System Overview
499
+
500
+ ```
501
+ ┌─────────────────────────────────────────────────────────────────┐
502
+ │ MCP Agent (Cursor / Claude / Cline / Continue / Claude Code) │
503
+ └─────────────────────────┬───────────────────────────────────────┘
504
+ │ MCP Protocol
505
+
506
+ ┌─────────────────────────────────────────────────────────────────┐
507
+ │ Context Gatekeeper MCP Server │
508
+ │ │
509
+ │ ┌──────────────────────────────────────────────────────────┐ │
510
+ │ │ Watchdog Security │ │
511
+ │ │ Token Validation → Permission Check → Tool Execution │ │
512
+ │ └──────────────────────────────────────────────────────────┘ │
513
+ │ │
514
+ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────┐ │
515
+ │ │ After-Chain │ │ LLM │ │ Embedding Provider │ │
516
+ │ │ Executor │ │ Service │ │ (TF-IDF/OpenAI/Cohere) │ │
517
+ │ └─────────────┘ └─────────────┘ └─────────────────────────┘ │
518
+ │ │
519
+ │ ┌──────────────────────────────────────────────────────────┐ │
520
+ │ │ Memory Service / Constraint Extractor │ │
521
+ │ │ / Intelligent Recall / Dual-Mode │ │
522
+ │ └──────────────────────────────────────────────────────────┘ │
523
+ └─────────────────────────┬───────────────────────────────────────┘
524
+
525
+
526
+ ┌─────────────────────────────────────────────────────────────────┐
527
+ │ SQLite Database (sql.js) │
528
+ │ WAL mode + periodic flush │
529
+ └─────────────────────────────────────────────────────────────────┘
530
+ ```
531
+
532
+ ### Watchdog Architecture
533
+
534
+ The Watchdog security model provides token-based access control:
535
+
536
+ ```
537
+ ┌──────────────────────────────────────────────────────────────────┐
538
+ │ Token Types: │
539
+ ├──────────────────────────────────────────────────────────────────┤
540
+ │ ┌────────────┬────────────┬──────────────────────┐ │
541
+ │ │ read │ write │ watchdog │ │
542
+ │ ├────────────┼────────────┼──────────────────────┤ │
543
+ │ │ recall │ store │ ALL operations │ │
544
+ │ │ search │ anchor │ (bypass all │ │
545
+ │ │ stats │ compress │ restrictions) │ │
546
+ │ │ intelligent│ batch │ │ │
547
+ │ │ recall │ delete │ │ │
548
+ │ │ (soft mode)│ create │ │ │
549
+ │ └────────────┴────────────┴──────────────────────┘ │
550
+ └──────────────────────────────────────────────────────────────────┘
551
+ ```
552
+
553
+ ### Directory Structure
554
+
555
+ ```
556
+ src/
557
+ ├── mcp/ # MCP server implementation
558
+ │ ├── server.ts # Main MCP server entry
559
+ │ └── tools/ # Tool implementations
560
+ │ ├── memory-store.ts
561
+ │ ├── memory-recall.ts
562
+ │ ├── memory-extract.ts # Phase 1: AutoSkill
563
+ │ ├── intelligent-recall.ts # Phase 2: MemGate
564
+ │ ├── dual-mode-execute.ts # Phase 3: MPR
565
+ │ └── ...
566
+ ├── schema/ # Database schema & operations
567
+ ├── services/ # Business logic
568
+ │ ├── embedding-provider.ts # Multi-provider support
569
+ │ ├── llm.ts # LLM summarization
570
+ │ └── compressor/ # Context compression
571
+ ├── utils/
572
+ │ ├── db.ts # Database wrapper (sql.js)
573
+ │ ├── watchdog.ts # Token-based security
574
+ │ ├── after-chain.ts # Tool chaining
575
+ │ └── logger.ts # Structured JSON logging
576
+ └── models/ # Type definitions
577
+ ```
578
+
579
+ ## Troubleshooting
580
+
581
+ ### "Permission denied" Errors
582
+
583
+ **Cause**: You're using a read-only token to call a write tool.
584
+
585
+ **Solution**:
586
+ 1. Check which token type your agent is using
587
+ 2. Upgrade to a write token for write operations
588
+ 3. Or use the watchdog token for full access
589
+
590
+ ### "Database not initialized" Errors
591
+
592
+ **Cause**: The database wasn't properly initialized before use.
593
+
594
+ **Solution**:
595
+ ```bash
596
+ export DATA_DIR="/path/to/writable/directory"
597
+ ```
598
+
599
+ ### Memory Deduplication Not Working
600
+
601
+ **Cause**: Different content hashes for semantically identical memories.
602
+
603
+ **Solution**:
604
+ 1. Use identical wording for duplicate memories
605
+ 2. The hash is SHA256 of normalized content - slight variations create different hashes
606
+
607
+ ### Search Returning Unexpected Results
608
+
609
+ **Cause**: Wrong search mode or embedding provider.
610
+
611
+ **Solution**:
612
+ ```typescript
613
+ // For exact matches, use keyword mode
614
+ memory_recall({
615
+ query: "exact phrase here",
616
+ search_mode: "keyword"
617
+ });
618
+
619
+ // For semantic meaning, use hybrid or semantic
620
+ memory_recall({
621
+ query: "authentication",
622
+ search_mode: "hybrid"
623
+ });
624
+ ```
625
+
626
+ ### LLM Extraction Failures
627
+
628
+ **Cause**: LLM provider not configured or API errors.
629
+
630
+ **Solution**:
631
+ ```typescript
632
+ // Configure LLM first
633
+ configure_llm({
634
+ provider: "openai",
635
+ apiKey: "your-key",
636
+ model: "gpt-3.5-turbo"
637
+ });
638
+
639
+ // If LLM is unavailable, falls back to rule-based extraction
640
+ memory_extract({
641
+ conversation_turns: [...],
642
+ min_confidence: 0.3 // Lower threshold
643
+ });
644
+ ```
645
+
646
+ ## Best Practices
647
+
648
+ ### Priority Assignment
649
+
650
+ | Priority | When to Use |
651
+ |----------|-------------|
652
+ | `anchored` | Critical rules that must never be violated |
653
+ | `constraint` | API conventions, coding standards |
654
+ | `decision` | Architecture choices, selected approaches |
655
+ | `preference` | Coding style, workflow preferences |
656
+ | `fact` | Project details, historical context |
657
+
658
+ ```typescript
659
+ // Good: Critical project-wide constraints
660
+ memory_store({
661
+ content: "NEVER commit directly to main branch",
662
+ priority: "anchored",
663
+ project_tags: ["git-workflow"]
664
+ });
665
+
666
+ // Avoid: Common knowledge
667
+ memory_store({
668
+ content: "JavaScript uses camelCase",
669
+ priority: "fact"
670
+ });
671
+ ```
672
+
673
+ ### Project Tagging Strategy
674
+
675
+ Use consistent, hierarchical tags:
676
+
677
+ ```typescript
678
+ memory_store({
679
+ content: "Use React 18 for new features",
680
+ project_tags: ["frontend:react", "version:18"]
681
+ });
682
+ ```
683
+
684
+ ### Token Budget Management
685
+
686
+ Monitor and compress context regularly:
687
+
688
+ ```typescript
689
+ // Report token usage when near limits
690
+ memory_report_usage({
691
+ used_tokens: 85000,
692
+ max_tokens: 100000
693
+ });
694
+
695
+ // Trigger compression when needed
696
+ context_compress({
697
+ target_ratio: 0.6 // Target 60% of max tokens
698
+ });
699
+ ```
700
+
701
+ ### Watchdog Token Security
702
+
703
+ Use the principle of least privilege:
704
+
705
+ ```bash
706
+ # Main agent: read-only token
707
+ CG_READ_TOKEN="agent-read-token"
708
+
709
+ # Admin agent: write token
710
+ CG_WRITE_TOKEN="admin-write-token"
711
+
712
+ # Emergency: watchdog token (only for trusted processes)
713
+ CG_WATCHDOG_TOKEN="emergency-token"
714
+ ```
715
+
716
+ ### When to Use Which Search Mode
717
+
718
+ | Scenario | Recommended Mode |
719
+ |----------|----------------|
720
+ | Exact code snippet | `keyword` |
721
+ | Known constraint text | `keyword` |
722
+ | General concept recall | `semantic` or `hybrid` |
723
+ | Diverse knowledge retrieval | `bm25` |
724
+ | Intent-based recall | `hybrid` |
725
+
726
+ ### After-Chain Usage
727
+
728
+ Enable automatic tool chaining to reduce manual tool calls:
729
+
730
+ ```typescript
731
+ // After storing a memory, automatically extract constraints
732
+ after_chain_configure({
733
+ action: "toggle_chain",
734
+ chain_name: "store-then-extract",
735
+ enabled: true
736
+ });
737
+ ```
738
+
739
+ ## Development
740
+
741
+ ```bash
742
+ # Install dependencies
743
+ npm install
744
+
745
+ # Build TypeScript
746
+ npm run build
747
+
748
+ # Run unit tests
749
+ npm test
750
+
751
+ # Cross-agent compatibility tests (spawns MCP servers across all supported agents)
752
+ npm run test:agents
753
+
754
+ # Cross-agent tests for a single agent
755
+ npm run test:agents -- cursor
756
+
757
+ # Watch mode (auto-rebuild on changes)
758
+ npm run dev
759
+
760
+ # Type checking
761
+ npm run lint
762
+ ```
763
+
764
+ See [Cross-Agent Compatibility Tests](#cross-agent-compatibility-tests) for details on the harness under `scripts/agents/` and `scripts/tests/`.
765
+
766
+ ## Inspired By
767
+
768
+ - **AutoSkill**: Experience-driven skill extraction from conversations
769
+ - **MemGate**: Inference-time memory filtering with learned relevance
770
+ - **Meta-Policy Reflexion (MPR)**: Soft guidance + hard admissibility checks
771
+
772
+ ## License
773
+
774
+ MIT