mcp-cognition-engine 1.0.0-alpha.1

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 (158) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +281 -0
  3. package/dist/cli.d.ts +18 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +66 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/cognition-engine/ast-constraint-solver.d.ts +67 -0
  8. package/dist/cognition-engine/ast-constraint-solver.d.ts.map +1 -0
  9. package/dist/cognition-engine/ast-constraint-solver.js +294 -0
  10. package/dist/cognition-engine/ast-constraint-solver.js.map +1 -0
  11. package/dist/cognition-engine/constraint-validator.d.ts +35 -0
  12. package/dist/cognition-engine/constraint-validator.d.ts.map +1 -0
  13. package/dist/cognition-engine/constraint-validator.js +55 -0
  14. package/dist/cognition-engine/constraint-validator.js.map +1 -0
  15. package/dist/cognition-engine/graph-traverser.d.ts +43 -0
  16. package/dist/cognition-engine/graph-traverser.d.ts.map +1 -0
  17. package/dist/cognition-engine/graph-traverser.js +205 -0
  18. package/dist/cognition-engine/graph-traverser.js.map +1 -0
  19. package/dist/cognition-engine/index.d.ts +51 -0
  20. package/dist/cognition-engine/index.d.ts.map +1 -0
  21. package/dist/cognition-engine/index.js +60 -0
  22. package/dist/cognition-engine/index.js.map +1 -0
  23. package/dist/cognition-engine/intent-recognizer.d.ts +25 -0
  24. package/dist/cognition-engine/intent-recognizer.d.ts.map +1 -0
  25. package/dist/cognition-engine/intent-recognizer.js +226 -0
  26. package/dist/cognition-engine/intent-recognizer.js.map +1 -0
  27. package/dist/cognition-engine/types.d.ts +96 -0
  28. package/dist/cognition-engine/types.d.ts.map +1 -0
  29. package/dist/cognition-engine/types.js +17 -0
  30. package/dist/cognition-engine/types.js.map +1 -0
  31. package/dist/conflict/arbitrator.d.ts +24 -0
  32. package/dist/conflict/arbitrator.d.ts.map +1 -0
  33. package/dist/conflict/arbitrator.js +43 -0
  34. package/dist/conflict/arbitrator.js.map +1 -0
  35. package/dist/index.d.ts +17 -0
  36. package/dist/index.d.ts.map +1 -0
  37. package/dist/index.js +90 -0
  38. package/dist/index.js.map +1 -0
  39. package/dist/legacy-engine/ast-diff.d.ts +23 -0
  40. package/dist/legacy-engine/ast-diff.d.ts.map +1 -0
  41. package/dist/legacy-engine/ast-diff.js +146 -0
  42. package/dist/legacy-engine/ast-diff.js.map +1 -0
  43. package/dist/legacy-engine/ast-node.d.ts +24 -0
  44. package/dist/legacy-engine/ast-node.d.ts.map +1 -0
  45. package/dist/legacy-engine/ast-node.js +52 -0
  46. package/dist/legacy-engine/ast-node.js.map +1 -0
  47. package/dist/legacy-engine/parsers.d.ts +34 -0
  48. package/dist/legacy-engine/parsers.d.ts.map +1 -0
  49. package/dist/legacy-engine/parsers.js +134 -0
  50. package/dist/legacy-engine/parsers.js.map +1 -0
  51. package/dist/legacy-engine/regex-fallback.d.ts +23 -0
  52. package/dist/legacy-engine/regex-fallback.d.ts.map +1 -0
  53. package/dist/legacy-engine/regex-fallback.js +40 -0
  54. package/dist/legacy-engine/regex-fallback.js.map +1 -0
  55. package/dist/legacy-engine/rule-generator.d.ts +31 -0
  56. package/dist/legacy-engine/rule-generator.d.ts.map +1 -0
  57. package/dist/legacy-engine/rule-generator.js +54 -0
  58. package/dist/legacy-engine/rule-generator.js.map +1 -0
  59. package/dist/legacy-engine/rule-matcher.d.ts +28 -0
  60. package/dist/legacy-engine/rule-matcher.d.ts.map +1 -0
  61. package/dist/legacy-engine/rule-matcher.js +79 -0
  62. package/dist/legacy-engine/rule-matcher.js.map +1 -0
  63. package/dist/legacy-engine/token-controller.d.ts +31 -0
  64. package/dist/legacy-engine/token-controller.d.ts.map +1 -0
  65. package/dist/legacy-engine/token-controller.js +66 -0
  66. package/dist/legacy-engine/token-controller.js.map +1 -0
  67. package/dist/middleware/response-validation.d.ts +27 -0
  68. package/dist/middleware/response-validation.d.ts.map +1 -0
  69. package/dist/middleware/response-validation.js +50 -0
  70. package/dist/middleware/response-validation.js.map +1 -0
  71. package/dist/modes/confirm.d.ts +29 -0
  72. package/dist/modes/confirm.d.ts.map +1 -0
  73. package/dist/modes/confirm.js +27 -0
  74. package/dist/modes/confirm.js.map +1 -0
  75. package/dist/modes/silent.d.ts +24 -0
  76. package/dist/modes/silent.d.ts.map +1 -0
  77. package/dist/modes/silent.js +24 -0
  78. package/dist/modes/silent.js.map +1 -0
  79. package/dist/resources/cognition-resources.d.ts +28 -0
  80. package/dist/resources/cognition-resources.d.ts.map +1 -0
  81. package/dist/resources/cognition-resources.js +124 -0
  82. package/dist/resources/cognition-resources.js.map +1 -0
  83. package/dist/storage/client.d.ts +26 -0
  84. package/dist/storage/client.d.ts.map +1 -0
  85. package/dist/storage/client.js +45 -0
  86. package/dist/storage/client.js.map +1 -0
  87. package/dist/storage/cognition-repository.d.ts +55 -0
  88. package/dist/storage/cognition-repository.d.ts.map +1 -0
  89. package/dist/storage/cognition-repository.js +287 -0
  90. package/dist/storage/cognition-repository.js.map +1 -0
  91. package/dist/storage/cognition-types.d.ts +96 -0
  92. package/dist/storage/cognition-types.d.ts.map +1 -0
  93. package/dist/storage/cognition-types.js +41 -0
  94. package/dist/storage/cognition-types.js.map +1 -0
  95. package/dist/storage/conflict-repo.d.ts +41 -0
  96. package/dist/storage/conflict-repo.d.ts.map +1 -0
  97. package/dist/storage/conflict-repo.js +67 -0
  98. package/dist/storage/conflict-repo.js.map +1 -0
  99. package/dist/storage/diff-log-repo.d.ts +48 -0
  100. package/dist/storage/diff-log-repo.d.ts.map +1 -0
  101. package/dist/storage/diff-log-repo.js +51 -0
  102. package/dist/storage/diff-log-repo.js.map +1 -0
  103. package/dist/storage/metric-repo.d.ts +20 -0
  104. package/dist/storage/metric-repo.d.ts.map +1 -0
  105. package/dist/storage/metric-repo.js +36 -0
  106. package/dist/storage/metric-repo.js.map +1 -0
  107. package/dist/storage/rule-repo.d.ts +66 -0
  108. package/dist/storage/rule-repo.d.ts.map +1 -0
  109. package/dist/storage/rule-repo.js +188 -0
  110. package/dist/storage/rule-repo.js.map +1 -0
  111. package/dist/tools/analyze-workspace.d.ts +26 -0
  112. package/dist/tools/analyze-workspace.d.ts.map +1 -0
  113. package/dist/tools/analyze-workspace.js +147 -0
  114. package/dist/tools/analyze-workspace.js.map +1 -0
  115. package/dist/tools/capture-diff.d.ts +26 -0
  116. package/dist/tools/capture-diff.d.ts.map +1 -0
  117. package/dist/tools/capture-diff.js +58 -0
  118. package/dist/tools/capture-diff.js.map +1 -0
  119. package/dist/tools/cognition-tools.d.ts +60 -0
  120. package/dist/tools/cognition-tools.d.ts.map +1 -0
  121. package/dist/tools/cognition-tools.js +170 -0
  122. package/dist/tools/cognition-tools.js.map +1 -0
  123. package/dist/tools/config-tools.d.ts +29 -0
  124. package/dist/tools/config-tools.d.ts.map +1 -0
  125. package/dist/tools/config-tools.js +58 -0
  126. package/dist/tools/config-tools.js.map +1 -0
  127. package/dist/tools/confirm-rule.d.ts +32 -0
  128. package/dist/tools/confirm-rule.d.ts.map +1 -0
  129. package/dist/tools/confirm-rule.js +64 -0
  130. package/dist/tools/confirm-rule.js.map +1 -0
  131. package/dist/tools/injection-approval.d.ts +43 -0
  132. package/dist/tools/injection-approval.d.ts.map +1 -0
  133. package/dist/tools/injection-approval.js +102 -0
  134. package/dist/tools/injection-approval.js.map +1 -0
  135. package/dist/tools/list-rules.d.ts +24 -0
  136. package/dist/tools/list-rules.d.ts.map +1 -0
  137. package/dist/tools/list-rules.js +20 -0
  138. package/dist/tools/list-rules.js.map +1 -0
  139. package/dist/tools/query-rules.d.ts +25 -0
  140. package/dist/tools/query-rules.d.ts.map +1 -0
  141. package/dist/tools/query-rules.js +65 -0
  142. package/dist/tools/query-rules.js.map +1 -0
  143. package/dist/tools/resolve-conflict.d.ts +33 -0
  144. package/dist/tools/resolve-conflict.d.ts.map +1 -0
  145. package/dist/tools/resolve-conflict.js +35 -0
  146. package/dist/tools/resolve-conflict.js.map +1 -0
  147. package/dist/types.d.ts +190 -0
  148. package/dist/types.d.ts.map +1 -0
  149. package/dist/types.js +50 -0
  150. package/dist/types.js.map +1 -0
  151. package/docs/api-reference.md +1229 -0
  152. package/docs/community/feedback-playbook.md +94 -0
  153. package/docs/integration-snippets.md +100 -0
  154. package/docs/mcp-integration-guide.md +151 -0
  155. package/docs/phase4-mcp-feedback.md +155 -0
  156. package/docs/trust-governance-protocol.md +72 -0
  157. package/package.json +74 -0
  158. package/prisma/schema.prisma +145 -0
@@ -0,0 +1,94 @@
1
+ # Community Feedback Playbook
2
+
3
+ Prepared responses for the top 5 frequently asked questions about licensing and trademark policy.
4
+
5
+ ---
6
+
7
+ ## Q1: Can I use this in my commercial product?
8
+
9
+ **Scenario:** Enterprise developer asking about embedding the engine in a proprietary SaaS product.
10
+
11
+ **Answer Template:**
12
+
13
+ > Yes, you absolutely can. The Apache 2.0 License (Section 2) grants you a perpetual, worldwide, non-exclusive, royalty-free license to use, modify, and distribute the software in any context — including commercial products.
14
+ >
15
+ > The trademark policy in `TRADEMARK.md` only restricts the use of the "Cognition Graph" brand name and logo in your product naming or marketing. Using the underlying code in your commercial product has no trademark implications as long as you do not misrepresent affiliation.
16
+ >
17
+ > For reference: *Section 2 of Apache 2.0 (Grant of Copyright License)* and *Section 6 (Trademarks)* explicitly separate software rights from trademark rights.
18
+
19
+ ---
20
+
21
+ ## Q2: Can I modify and redistribute the code?
22
+
23
+ **Scenario:** Contributor wanting to fork and republish.
24
+
25
+ **Answer Template:**
26
+
27
+ > Yes, Apache 2.0 (Section 4) permits you to reproduce, prepare derivative works, and distribute copies. You may add your own copyright statement to your modifications and may offer different license terms for your modifications — but the original work must remain under Apache 2.0.
28
+ >
29
+ > Key requirements:
30
+ > - Retain all original copyright, patent, trademark, and attribution notices.
31
+ > - Include a conspicuous notice that your version includes modifications.
32
+ > - If you include the "NOTICE" file (if any), do not modify it.
33
+
34
+ ---
35
+
36
+ ## Q3: How do I report a trademark violation?
37
+
38
+ **Scenario:** Someone is using "Cognition Graph" in a way that implies endorsement without permission.
39
+
40
+ **Answer Template:**
41
+
42
+ > Thank you for helping protect the project. Please:
43
+ >
44
+ > 1. Gather evidence: screenshot, URL, and a description of how the mark is being used.
45
+ > 2. Open a private report via email: **wind-come@qq.com**
46
+ > 3. Alternatively, open a GitHub Discussion in the "Q&A" category with a private note flag.
47
+ >
48
+ > We review reports within 5 business days. If a violation is confirmed, we will:
49
+ > - Issue a cease-and-desist notice to the infringing party.
50
+ > - Document the resolution in the project records (without exposing reporter identity).
51
+ >
52
+ > We reserve the right to escalate under applicable trademark law.
53
+
54
+ ---
55
+
56
+ ## Q4: Do I need to sign a CLA to contribute?
57
+
58
+ **Scenario:** New contributor asking about legal paperwork before submitting a PR.
59
+
60
+ **Answer Template:**
61
+
62
+ > No, you do not need to sign a separate Contributor License Agreement (CLA). Apache 2.0 (Section 5, Submission of Contributions) handles this automatically:
63
+ >
64
+ > *"Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions."*
65
+ >
66
+ > This means:
67
+ > - By submitting a PR, you agree that your contribution is licensed under Apache 2.0.
68
+ > - You retain copyright over your contributions.
69
+ > - No additional CLA or legal agreement is required.
70
+ >
71
+ > If your organization requires a signed agreement for legal compliance, contact us at wind-come@qq.com to arrange a corporate CLA.
72
+
73
+ ---
74
+
75
+ ## Q5: What if my company requires GPL compatibility?
76
+
77
+ **Scenario:** Organization using GPL-licensed tools wanting to integrate this project.
78
+
79
+ **Answer Template:**
80
+
81
+ > Apache 2.0 is a permissive license and is compatible with GPLv3 (but not GPLv2). Specifically:
82
+ >
83
+ > - **Apache 2.0 → GPLv3:** Compatible. You can combine Apache 2.0 code with GPLv3 code.
84
+ > - **Apache 2.0 → GPLv2:** Not directly compatible. You cannot distribute Apache 2.0 code under GPLv2 alone.
85
+ >
86
+ > If your project is GPLv2-only, you have two options:
87
+ > 1. Keep the Apache 2.0 components as separate processes communicating via IPC/network (not derivative works).
88
+ > 2. Contact us at wind-come@qq.com to discuss alternative licensing arrangements.
89
+ >
90
+ > The core engines (cognition graph, MCP tools) will always remain Apache 2.0, but we are open to discussing dual-licensing for enterprise integration scenarios.
91
+
92
+ ---
93
+
94
+ *Last updated: 2026-06-15*
@@ -0,0 +1,100 @@
1
+ # MCP Integration Snippets
2
+
3
+ Ready-to-copy JSON configuration snippets for all major MCP clients.
4
+
5
+ ## Cursor
6
+
7
+ Add to `.cursor/mcp.json`:
8
+
9
+ ```json
10
+ {
11
+ "mcpServers": {
12
+ "cognition-engine": {
13
+ "command": "npx",
14
+ "args": ["-y", "mcp-cognition-engine@latest"],
15
+ "env": {
16
+ "COGNITION_DB_PATH": "~/.cognition/dev.db"
17
+ }
18
+ }
19
+ }
20
+ }
21
+ ```
22
+
23
+ After adding, open Cursor → Settings → MCP and click **Test Connection**.
24
+
25
+ ## Claude Desktop
26
+
27
+ Add to `claude_desktop_config.json` (or `~/Library/Application Support/Claude/claude_desktop_config.json` on macOS):
28
+
29
+ ```json
30
+ {
31
+ "mcpServers": {
32
+ "cognition-engine": {
33
+ "command": "npx",
34
+ "args": ["-y", "mcp-cognition-engine@latest"],
35
+ "env": {
36
+ "COGNITION_DB_PATH": "~/.cognition/dev.db"
37
+ }
38
+ }
39
+ }
40
+ }
41
+ ```
42
+
43
+ Restart Claude Desktop. The cognition engine tools should appear in the MCP tool list.
44
+
45
+ ## VS Code (GitHub Copilot Chat / MCP Extension)
46
+
47
+ Add to `.vscode/mcp.json` or User `settings.json`:
48
+
49
+ ```json
50
+ {
51
+ "mcp": {
52
+ "servers": {
53
+ "cognition-engine": {
54
+ "type": "stdio",
55
+ "command": "npx",
56
+ "args": ["-y", "mcp-cognition-engine@latest"],
57
+ "env": {
58
+ "COGNITION_DB_PATH": "~/.cognition/dev.db"
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
64
+ ```
65
+
66
+ Open VS Code Command Palette → 'MCP: Restart Servers'.
67
+
68
+ ## Local Development (from source)
69
+
70
+ If you cloned the repo and want to run locally:
71
+
72
+ ```json
73
+ {
74
+ "mcpServers": {
75
+ "cognition-engine": {
76
+ "command": "node",
77
+ "args": ["dist/cli.js"],
78
+ "env": {
79
+ "COGNITION_DB_PATH": "./prisma/dev.db"
80
+ }
81
+ }
82
+ }
83
+ }
84
+ ```
85
+
86
+ ---
87
+
88
+ ## Environment Variables
89
+
90
+ | Variable | Default | Description |
91
+ |----------|---------|-------------|
92
+ | `COGNITION_DB_PATH` | `~/.cognition/dev.db` | Path to the SQLite database file. Created automatically if missing. |
93
+
94
+ ## Troubleshooting
95
+
96
+ | Symptom | Fix |
97
+ |---------|-----|
98
+ | "Database not found" | Delete the DB file and restart — it will auto-initialize. |
99
+ | Connection timeout | Make sure `prisma` is available in `npx` cache. First run may take 30s for npm install. |
100
+ | Tool not appearing | Check MCP client logs for parse errors. Verify the JSON snippet syntax. |
@@ -0,0 +1,151 @@
1
+ # MCP Production-Grade Integration Guide
2
+
3
+ > For: Cursor, Claude Desktop, Cline / Roo Code
4
+ > Last updated: 2026-06-15
5
+
6
+ ---
7
+
8
+ ## 1. Quick Start
9
+
10
+ ### Prerequisites
11
+ - Node.js >= 18
12
+ - npm >= 9
13
+
14
+ ### Start the Server
15
+
16
+ ```bash
17
+ # stdio mode (default for Cursor / Claude Desktop)
18
+ npm run build && node dist/index.js
19
+
20
+ # HTTP mode (for Cline / Roo Code remote agents)
21
+ TRANSPORT=http PORT=3000 npm run build && node dist/index.js
22
+ ```
23
+
24
+ ---
25
+
26
+ ## 2. Agent Configuration
27
+
28
+ ### Cursor
29
+
30
+ Add to `.cursor/mcp.json`:
31
+
32
+ ```json
33
+ {
34
+ "mcpServers": {
35
+ "cognition-engine": {
36
+ "command": "node",
37
+ "args": ["dist/index.js"],
38
+ "env": {
39
+ "DATABASE_URL": "file:./dev.db"
40
+ }
41
+ }
42
+ }
43
+ }
44
+ ```
45
+
46
+ Restart Cursor. Check Settings > MCP for green status indicator.
47
+
48
+ ### Claude Desktop
49
+
50
+ Add to `claude_desktop_config.json`:
51
+
52
+ ```json
53
+ {
54
+ "mcpServers": {
55
+ "cognition-engine": {
56
+ "command": "node",
57
+ "args": ["path/to/mcp-rule-engine/dist/index.js"],
58
+ "env": {
59
+ "DATABASE_URL": "file:./dev.db"
60
+ }
61
+ }
62
+ }
63
+ }
64
+ ```
65
+
66
+ ### Cline / Roo Code (HTTP)
67
+
68
+ ```json
69
+ {
70
+ "mcpServers": {
71
+ "cognition-engine": {
72
+ "url": "http://localhost:3000",
73
+ "transport": "streamable-http"
74
+ }
75
+ }
76
+ }
77
+ ```
78
+
79
+ ---
80
+
81
+ ## 3. Tool Reference
82
+
83
+ ### cognition_query
84
+
85
+ Query the cognition graph by context hash. Returns relevant nodes sorted by relevance.
86
+
87
+ ```json
88
+ {
89
+ "contextHash": "abc123",
90
+ "intentHint": "BUGFIX",
91
+ "maxDepth": 3
92
+ }
93
+ ```
94
+
95
+ ### cognition_validate
96
+
97
+ Validate code against an AST template.
98
+
99
+ ```json
100
+ {
101
+ "nodeId": "node-123",
102
+ "targetFileContent": "function foo() { return 1; }"
103
+ }
104
+ ```
105
+
106
+ ### cognition_feedback
107
+
108
+ Provide feedback to refine future queries.
109
+
110
+ ```json
111
+ {
112
+ "nodeId": "node-123",
113
+ "edgeId": "edge-456",
114
+ "outcome": "ACCEPTED",
115
+ "comment": "Useful constraint"
116
+ }
117
+ ```
118
+
119
+ ---
120
+
121
+ ## 4. Resources
122
+
123
+ | URI | Type | Content |
124
+ |-----|------|---------|
125
+ | `cognition://schema` | application/json | Data model schema |
126
+ | `cognition://stats` | application/json | Graph statistics |
127
+ | `cognition://docs` | text/markdown | Full documentation |
128
+
129
+ ---
130
+
131
+ ## 5. Error Handling
132
+
133
+ | Code | Meaning | Retryable |
134
+ |------|---------|-----------|
135
+ | `-32602` | Invalid parameters | false |
136
+ | `-32603` | Internal engine error | true |
137
+ | `-32001` | Traversal timeout | true |
138
+
139
+ All error responses include a `retryable` boolean field.
140
+
141
+ ---
142
+
143
+ ## 6. Troubleshooting
144
+
145
+ | Symptom | Cause | Fix |
146
+ |---------|-------|-----|
147
+ | Tool list not loading | Missing environment variables | Set `DATABASE_URL` |
148
+ | Server exits immediately | Prisma client not generated | Run `npx prisma generate` |
149
+ | HTTP connection refused | Wrong port | Check `PORT` env var and firewall |
150
+ | "Tool not found" | SDK version mismatch | Use @modelcontextprotocol/sdk >= 1.0 |
151
+ | Resource returns empty | Database not migrated | Run `npx prisma migrate deploy` |
@@ -0,0 +1,155 @@
1
+ # Phase 4 — MCP 工具与反馈闭环设计文档
2
+
3
+ > **日期:** 2026-06-15
4
+ > **项目:** MCP Rule Engine → Cognition Engine Refactoring
5
+
6
+ ---
7
+
8
+ ## 1. 新增 MCP Tools
9
+
10
+ 三个新工具注册在 MCP Server 的 `ListToolsRequestSchema` 中,与 Legacy Engine 的 6 个工具完全独立、共存。
11
+
12
+ ### 1.1 cognition_query
13
+
14
+ | 属性 | 值 |
15
+ |------|-----|
16
+ | 名称 | `cognition_query` |
17
+ | 描述 | 主动查询认知图谱并召回关联认知节点 |
18
+ | 输入 | `contextHash` (string, required), `intentHint` (string enum, optional), `maxDepth` (number, optional) |
19
+ | 输出 | `{ nodes: [{id, type, abstractionLevel, relevanceScore}], traversalMs, truncated }` |
20
+
21
+ **内部逻辑:**
22
+ 1. 使用 `contextHash` 直接匹配认知图谱节点(跳过 hash 计算)
23
+ 2. 若提供 `intentHint`,则作为遍历偏置传入 GraphTraverser
24
+ 3. 加权 BFS 遍历子图,返回排序后的节点摘要
25
+ 4. **异步(fire-and-forget)** 记录 PENDING 反馈事件
26
+
27
+ ### 1.2 cognition_validate
28
+
29
+ | 属性 | 值 |
30
+ |------|-----|
31
+ | 名称 | `cognition_validate` |
32
+ | 描述 | 对文件内容执行 AST 级约束校验 |
33
+ | 输入 | `nodeId` (string, required), `targetFileContent` (string, required) |
34
+ | 输出 | `{ valid: boolean, violations: [{constraintPath, expected, actual}], transformPatch? }` |
35
+
36
+ **内部逻辑:**
37
+ 1. 根据 `nodeId` 查找 CognitionNode
38
+ 2. 获取关联的 AstTemplate.templateDsl
39
+ 3. 调用 `solveConstraints()` 对目标文件内容执行 AST 约束求解
40
+ 4. 返回校验结果
41
+
42
+ ### 1.3 cognition_feedback
43
+
44
+ | 属性 | 值 |
45
+ |------|-----|
46
+ | 名称 | `cognition_feedback` |
47
+ | 描述 | 接收 Agent 执行结果反馈,更新节点权重与边强度 |
48
+ | 输入 | `nodeId` (required), `edgeId` (optional), `outcome` (ACCEPTED/REJECTED/MODIFIED, required), `comment` (optional) |
49
+ | 输出 | `{ updatedWeight: number|null, feedbackId: string }` |
50
+
51
+ **内部逻辑:**
52
+ 1. 根据 `outcome` 计算权重增量:ACCEPTED=+0.1, REJECTED=-0.2, MODIFIED=+0.05
53
+ 2. 若提供 `edgeId`,调用 updateEdgeWeight() 调整边权重
54
+ 3. 调用 `recordFeedbackEvent()` 记录反馈事件
55
+ 4. 调用 `resolveFeedbackEvent()` 更新事件状态
56
+
57
+ ---
58
+
59
+ ## 2. 反馈闭环机制
60
+
61
+ ### 2.1 数据流
62
+
63
+ ```
64
+ cognition_query ---> recordFeedbackEvent(PENDING)
65
+ |
66
+ v
67
+ Agent 处理结果
68
+ |
69
+ v
70
+ cognition_feedback ---> resolveFeedbackEvent(RESOLVED)
71
+ |--- outcome=ACCEPTED/REJECTED/MODIFIED
72
+ v
73
+ updateEdgeWeight(delta)
74
+ |
75
+ v (下一次查询)
76
+ cognition_query 使用更新后的权重
77
+ ```
78
+
79
+ ### 2.2 事件存储
80
+
81
+ 使用现有的 `MetricEvent` 表(不新增 Prisma Model):
82
+ - `eventType`: `"cognition_feedback_pending"` (初始)
83
+ - `properties`: JSON 字符串,包含 `{nodeId, edgeId, outcome, status, comment}`
84
+
85
+ ### 2.3 异步非阻塞保证
86
+
87
+ - `recordFeedbackEvent()` 在 query/validate 响应发送后调用
88
+ - 调用方式为 `repo.recordFeedbackEvent(...).catch(() => {})`
89
+ - 不阻塞主工具响应路径
90
+
91
+ ---
92
+
93
+ ## 3. 向后兼容性
94
+
95
+ | 方面 | 保证 |
96
+ |------|------|
97
+ | Legacy 工具 | 6 个原始工具完全不变 |
98
+ | 工具注册 | 新工具追加在原有数组末尾 |
99
+ | Switch 路由 | 新 case 加在 default 前 |
100
+ | Import | 新文件 `cognition-tools.ts` 物理隔离 |
101
+
102
+ ---
103
+
104
+ ## 4. 测试覆盖
105
+
106
+ | 测试套件 | 测试数 | 说明 |
107
+ |---------|-------|------|
108
+ | cognition_query | 2 | 错误输入 + 正常路径 |
109
+ | cognition_validate | 3 | 缺失字段 + 节点不存在 |
110
+ | cognition_feedback | 4 | 错误输入 + 三种 outcome |
111
+ | 原有测试 | 85 | 无回归 |
112
+ | **总计** | **94+** | |
113
+
114
+ ---
115
+
116
+ ## 5. Tool Schema 定义
117
+
118
+ ### cognition_query
119
+ ```json
120
+ {
121
+ "type": "object",
122
+ "properties": {
123
+ "contextHash": { "type": "string" },
124
+ "intentHint": { "type": "string", "enum": ["REFACTOR", "BUGFIX", "BOILERPLATE"] },
125
+ "maxDepth": { "type": "number" }
126
+ },
127
+ "required": ["contextHash"]
128
+ }
129
+ ```
130
+
131
+ ### cognition_validate
132
+ ```json
133
+ {
134
+ "type": "object",
135
+ "properties": {
136
+ "nodeId": { "type": "string" },
137
+ "targetFileContent": { "type": "string" }
138
+ },
139
+ "required": ["nodeId", "targetFileContent"]
140
+ }
141
+ ```
142
+
143
+ ### cognition_feedback
144
+ ```json
145
+ {
146
+ "type": "object",
147
+ "properties": {
148
+ "nodeId": { "type": "string" },
149
+ "edgeId": { "type": "string" },
150
+ "outcome": { "type": "string", "enum": ["ACCEPTED", "REJECTED", "MODIFIED"] },
151
+ "comment": { "type": "string" }
152
+ },
153
+ "required": ["nodeId", "outcome"]
154
+ }
155
+ ```
@@ -0,0 +1,72 @@
1
+ # Trust Governance Protocol
2
+
3
+ ## Overview
4
+
5
+ The trust governance layer provides hard constraints, auditable injection approval, and dynamic configuration for AI agents using the MCP Rule Engine. It enforces that all code generation is validated before output, and all rule modifications are explicitly approved by a human operator.
6
+
7
+ ## Three-Tier Knowledge Base
8
+
9
+ | Tier | Scope | Storage | Validation Mode |
10
+ |------|-------|---------|-----------------|
11
+ | Global | Universal code patterns across all projects | CognitionNode(type=CONSTRAINT) with NegativeConstraint payload | REJECT (hard block) |
12
+ | Project | Project-specific conventions and rules | CognitionNode(type=CONSTRAINT) | WARN (soft warning) |
13
+ | Reuse | Cross-project patterns and heuristics | CognitionNode(type=INTENT or HEURISTIC) | Weight-based via traversal |
14
+
15
+ ## Injection Approval Flow
16
+
17
+ All rule modifications follow this protocol:
18
+
19
+ 1. Agent calls cognition_query, which implicitly creates a Proposal with TTL=5 minutes
20
+ 2. User receives the proposal via cognition_query output
21
+ 3. User calls cognition_approve_injection(proposalId, decision)
22
+ 4. Decision is recorded in the audit log
23
+ 5. If approved, the traversal or validation proceeds
24
+ 6. If expired (TTL exceeded), returns -32602 with retryable:true
25
+
26
+ ### TTL and Conflict Rules
27
+
28
+ - TTL = 5 minutes from proposal creation
29
+ - Only one active proposal per contextHash
30
+ - Duplicate proposals return the existing proposal
31
+ - Expired proposals can be retried
32
+
33
+ ## Constraint Validation Dual-Mode
34
+
35
+ - **REJECT (Hard Block)**: The constraint violation is returned as error code -32602 with the ruleId. The agent MUST NOT proceed with the current operation.
36
+ - **WARN (Soft Warning)**: The violation is returned as part of the output. The agent may proceed with user confirmation.
37
+
38
+ ## Config Hot Update
39
+
40
+ Dynamic thresholds (e.g., similarity 0.7/0.9) are stored as CognitionNode(type=HEURISTIC):
41
+
42
+ 1. Agent calls cognition_update_config(key, value, expertMode=true)
43
+ 2. A new config node is created with the updated value
44
+ 3. The old node receives a supersededBy metadata field pointing to the new node
45
+ 4. Non-expert mode calls return -32601 Unauthorized
46
+
47
+ ## Audit Logging
48
+
49
+ All governance events are recorded using the MetricEvent table:
50
+
51
+ - eventType: cognition_feedback_pending, proposal_created, proposal_approved, proposal_rejected, proposal_expired
52
+ - Properties: JSON with all relevant context data
53
+ - Written asynchronously (fire-and-forget)
54
+ - On write failure, events fall back to logs/fallback.log
55
+
56
+ ## Error Codes
57
+
58
+ | Code | Meaning | Retryable | Scenario |
59
+ |------|---------|-----------|----------|
60
+ | -32602 | Invalid Request | false | Missing required fields, invalid parameters |
61
+ | -32602 | Proposal Expired | true | TTL exceeded |
62
+ | -32602 | Unauthorized Injection | false | Non-expert mode config attempt |
63
+ | -32603 | Internal Error | true | Engine failure during validation or traversal |
64
+ | -32001 | Timeout | true | Graph traversal exceeded time limit |
65
+
66
+ ## Response Validation Middleware
67
+
68
+ All tools/call responses are validated for Schema compliance:
69
+
70
+ - If validationRequired is missing from cognition_query or cognition_validate responses, the middleware auto-adds it with value true
71
+ - A WARN log entry is written to logs/validation-warnings.log
72
+ - This ensures Agent constraint even if the Agent ignores the Schema declaration
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "mcp-cognition-engine",
3
+ "version": "1.0.0-alpha.1",
4
+ "description": "MCP Server for Cognition Engine & Trust Governance Layer — weighted graph traversal, AST constraint solving, and auditable injection approval for AI agents.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "start": "node dist/index.js",
10
+ "dev": "tsx watch src/index.ts",
11
+ "db:generate": "prisma generate",
12
+ "db:push": "prisma db push",
13
+ "test": "vitest run",
14
+ "test:watch": "vitest",
15
+ "license:check": "node scripts/check-license.js",
16
+ "license:fix": "node scripts/check-license.js",
17
+ "bench": "npx tsx benchmarks/intent-recognizer.bench.ts && npx tsx benchmarks/graph-traverser.bench.ts"
18
+ },
19
+ "dependencies": {
20
+ "@modelcontextprotocol/sdk": "^1.29.0",
21
+ "@prisma/client": "^5.22.0",
22
+ "better-sqlite3": "^12.10.1",
23
+ "tree-sitter": "^0.21.1",
24
+ "tree-sitter-javascript": "^0.25.0",
25
+ "tree-sitter-python": "^0.25.0",
26
+ "tree-sitter-typescript": "^0.23.2",
27
+ "web-tree-sitter": "^0.26.0"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^22.0.0",
31
+ "prisma": "^5.22.0",
32
+ "tsx": "^4.19.0",
33
+ "typescript": "^5.6.0",
34
+ "vitest": "^2.1.0"
35
+ },
36
+ "author": "MCP Rule Engine Contributors",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/sole03/mcp-rule-engine.git"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/sole03/mcp-rule-engine/issues"
43
+ },
44
+ "homepage": "https://github.com/sole03/mcp-rule-engine#readme",
45
+ "bin": {
46
+ "mcp-cognition-engine": "dist/cli.js"
47
+ },
48
+ "files": [
49
+ "dist",
50
+ "prisma/schema.prisma",
51
+ "README.md",
52
+ "docs/"
53
+ ],
54
+ "keywords": [
55
+ "mcp",
56
+ "model-context-protocol",
57
+ "cognition",
58
+ "graph",
59
+ "ast",
60
+ "ai-agent"
61
+ ],
62
+ "license": "Apache-2.0",
63
+ "publishConfig": {
64
+ "access": "public"
65
+ },
66
+ "peerDependencies": {
67
+ "prisma": "^5.22.0"
68
+ },
69
+ "peerDependenciesMeta": {
70
+ "prisma": {
71
+ "optional": true
72
+ }
73
+ }
74
+ }