canpoint-knowledge-mcp 0.1.0__tar.gz

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 (35) hide show
  1. canpoint_knowledge_mcp-0.1.0/LICENSE +201 -0
  2. canpoint_knowledge_mcp-0.1.0/PKG-INFO +208 -0
  3. canpoint_knowledge_mcp-0.1.0/README.md +182 -0
  4. canpoint_knowledge_mcp-0.1.0/canpoint_knowledge_mcp.egg-info/PKG-INFO +208 -0
  5. canpoint_knowledge_mcp-0.1.0/canpoint_knowledge_mcp.egg-info/SOURCES.txt +33 -0
  6. canpoint_knowledge_mcp-0.1.0/canpoint_knowledge_mcp.egg-info/dependency_links.txt +1 -0
  7. canpoint_knowledge_mcp-0.1.0/canpoint_knowledge_mcp.egg-info/entry_points.txt +3 -0
  8. canpoint_knowledge_mcp-0.1.0/canpoint_knowledge_mcp.egg-info/requires.txt +3 -0
  9. canpoint_knowledge_mcp-0.1.0/canpoint_knowledge_mcp.egg-info/top_level.txt +5 -0
  10. canpoint_knowledge_mcp-0.1.0/clients/__init__.py +1 -0
  11. canpoint_knowledge_mcp-0.1.0/clients/query/__init__.py +21 -0
  12. canpoint_knowledge_mcp-0.1.0/clients/query/query.py +425 -0
  13. canpoint_knowledge_mcp-0.1.0/clients/sync/__init__.py +1 -0
  14. canpoint_knowledge_mcp-0.1.0/clients/sync/sync.py +519 -0
  15. canpoint_knowledge_mcp-0.1.0/config/__init__.py +16 -0
  16. canpoint_knowledge_mcp-0.1.0/config/env_config.py +233 -0
  17. canpoint_knowledge_mcp-0.1.0/config/logging_config.py +93 -0
  18. canpoint_knowledge_mcp-0.1.0/logging_config.py +60 -0
  19. canpoint_knowledge_mcp-0.1.0/pyproject.toml +44 -0
  20. canpoint_knowledge_mcp-0.1.0/query_mcp.py +248 -0
  21. canpoint_knowledge_mcp-0.1.0/server.py +108 -0
  22. canpoint_knowledge_mcp-0.1.0/setup.cfg +4 -0
  23. canpoint_knowledge_mcp-0.1.0/tests/test_app_lifespan.py +142 -0
  24. canpoint_knowledge_mcp-0.1.0/tests/test_embedding.py +58 -0
  25. canpoint_knowledge_mcp-0.1.0/tests/test_graph_extractor.py +466 -0
  26. canpoint_knowledge_mcp-0.1.0/tests/test_kb2_graph_service.py +1140 -0
  27. canpoint_knowledge_mcp-0.1.0/tests/test_kb2_hop.py +183 -0
  28. canpoint_knowledge_mcp-0.1.0/tests/test_kb2_links.py +156 -0
  29. canpoint_knowledge_mcp-0.1.0/tests/test_kb2_parsing.py +103 -0
  30. canpoint_knowledge_mcp-0.1.0/tests/test_kb2_query_client.py +119 -0
  31. canpoint_knowledge_mcp-0.1.0/tests/test_kb2_query_service.py +161 -0
  32. canpoint_knowledge_mcp-0.1.0/tests/test_kb2_store_graph_cache.py +706 -0
  33. canpoint_knowledge_mcp-0.1.0/tests/test_kb2_sync_service.py +300 -0
  34. canpoint_knowledge_mcp-0.1.0/tests/test_sync_client.py +224 -0
  35. canpoint_knowledge_mcp-0.1.0/tests/test_sync_mcp.py +246 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,208 @@
1
+ Metadata-Version: 2.4
2
+ Name: canpoint-knowledge-mcp
3
+ Version: 0.1.0
4
+ Summary: KB2 产品知识库的查询/同步 MCP 服务:项目绑定的混合检索、大纲、文档、章节与引用边跳转;同步 MCP 经环境变量绑定项目,是唯一写侧入口。
5
+ Author: 若清风
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/mjwyr/canpoint-mcp
8
+ Project-URL: Repository, https://github.com/mjwyr/canpoint-mcp.git
9
+ Project-URL: Issues, https://github.com/mjwyr/canpoint-mcp/issues
10
+ Keywords: mcp,model-context-protocol,knowledge-base,rag,search
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Environment :: Console
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
19
+ Requires-Python: >=3.12
20
+ Description-Content-Type: text/markdown
21
+ License-File: LICENSE
22
+ Requires-Dist: mcp<2,>=1.6.0
23
+ Requires-Dist: httpx>=0.27.0
24
+ Requires-Dist: pydantic-settings>=2.5.0
25
+ Dynamic: license-file
26
+
27
+ # KB2 产品知识库
28
+
29
+ 本仓库提供一个面向多个业务项目的 Markdown 知识库服务:AI 在业务项目发布后调用
30
+ 同步 MCP,把 `docs/` 完整清单发送到共享 HTTP 服务;PostgreSQL 保存文档、章节、层级和
31
+ 引用边,Milvus `kb2_chunks` 保存混合检索所需的向量与原文。查询 MCP 绑定单个
32
+ `project_key`,同步 MCP 是唯一受支持的写侧入口。
33
+
34
+ ## 架构
35
+
36
+ ```text
37
+ 业务仓库 docs/**/*.md
38
+
39
+ └─ AI 发布流程 → sync_now(project_root)
40
+ │ 同步 MCP(清单、哈希、变更正文)
41
+
42
+
43
+ FastAPI /kb/sync
44
+ │ │
45
+ PostgreSQL Milvus
46
+ 结构化事实 kb2_chunks
47
+ ```
48
+
49
+ 同一项目的同步按顺序执行,清单可重复提交。Markdown 的 `docs/` 一级子目录是
50
+ 版本/分组标签;平铺文件归入 `default`,路径中名为 `prototype` 的目录不入库。
51
+ 更深层目录保留在相对路径中,中文路径按 UTF-8 处理。
52
+
53
+ ## 安装与配置
54
+
55
+ 需要 Python 3.12、可访问的 PostgreSQL 和 Milvus/Zilliz Cloud。
56
+
57
+ ```powershell
58
+ cd C:\path\to\canpoint-mcp
59
+ .\.venv\Scripts\python.exe -m pip install -r requirements.txt
60
+ ```
61
+
62
+ 复制 `.env.example` 为本机 `.env`,填写 `PG_HOST`、`PG_PORT`、`PG_USER`、
63
+ `PG_PASSWORD`、`PG_DATABASE`、`MILVUS_URI`、`MILVUS_TOKEN` 以及 embedding
64
+ 配置。真实密码、API key 只允许存在于本机环境或密钥管理器,不能提交到 Git 或
65
+ 写入文档。初始化 PostgreSQL 表和 KB2 collection:
66
+
67
+ ```powershell
68
+ .\.venv\Scripts\python.exe scripts\init_kb2.py
69
+ ```
70
+
71
+ 启动共享 HTTP 服务:
72
+
73
+ ```powershell
74
+ .\.venv\Scripts\python.exe -m uvicorn app.main:app --host 0.0.0.0 --port 8001
75
+ ```
76
+
77
+ ## HTTP 接口
78
+
79
+ | 方法与路径 | 用途 |
80
+ | --- | --- |
81
+ | `POST /kb/sync` | 提交项目完整 Markdown 清单 |
82
+ | `GET /kb/sync/{job_id}` | 查询同步和图谱任务进度 |
83
+ | `GET /kb/outline` | 按项目和可选版本读取标题树 |
84
+ | `POST /kb/search` | 项目范围内混合检索,返回父章节和行号 |
85
+ | `GET /kb/document` | 按相对路径读取全文 |
86
+ | `GET /kb/chunk/{chunk_id}` | 读取章节块、父块和子块 |
87
+ | `POST /kb/hop` | 沿项目引用边跳转相邻文档 |
88
+ | `GET /health` | 检查 PostgreSQL 与 KB2 Milvus |
89
+
90
+ 所有查询请求都必须带 `project_key`。服务端会在 PostgreSQL 和 Milvus 两侧同时
91
+ 校验项目范围,避免跨项目返回内容。
92
+
93
+ ## MCP 入口
94
+
95
+ 查询 MCP 是只读、项目绑定的 HTTP 客户端,提供 `get_outline`、`search`、
96
+ `get_document`、`get_section`、`hop`。同步 MCP 提供
97
+ `sync_now(project_root, project_key?)` 和 `sync_status(job_id)`;两个进程都只需绑定
98
+ 共享服务地址与项目标识。
99
+
100
+ 本地源码的完整 MCP 客户端配置如下;把路径与 `project_key` 换成实际值:
101
+
102
+ ```json
103
+ {
104
+ "mcpServers": {
105
+ "kb2-query-member-prototype": {
106
+ "type": "stdio",
107
+ "command": "C:\\path\\to\\canpoint-mcp\\.venv\\Scripts\\python.exe",
108
+ "args": ["C:\\path\\to\\canpoint-mcp\\query_mcp.py"],
109
+ "env": {
110
+ "KB_SERVER_URL": "http://127.0.0.1:8001",
111
+ "KB_PROJECT_KEY": "member-prototype"
112
+ }
113
+ },
114
+ "kb2-sync-member-prototype": {
115
+ "type": "stdio",
116
+ "command": "C:\\path\\to\\canpoint-mcp\\.venv\\Scripts\\python.exe",
117
+ "args": ["C:\\path\\to\\canpoint-mcp\\server.py"],
118
+ "env": {
119
+ "KB_SERVER_URL": "http://127.0.0.1:8001",
120
+ "KB_PROJECT_KEY": "member-prototype"
121
+ }
122
+ }
123
+ }
124
+ }
125
+ ```
126
+
127
+ 调用 `sync_now` 时,AI 必须传入当前业务项目的绝对 Git 根目录。工具要求该路径存在、
128
+ 就是 Git 顶层目录,且目录名与 MCP 绑定的 `project_key` 大小写不敏感地相等;随后固定
129
+ 扫描 `<project_root>/docs/**/*.md`。`docs/` 必须存在,空目录表示有意提交空的完整清单。
130
+ 可选 `project_key` 参数只作绑定断言,不能切换项目。同步不依赖业务仓库内的配置文件,
131
+ 也不提供独立命令或 Git hook 触发方式;复用的扫描与哈希代码仅是 MCP 内部实现。详见
132
+ [`clients/sync/README.md`](https://github.com/mjwyr/canpoint-mcp/blob/main/clients/sync/README.md)。
133
+
134
+ `sync_now(project_root=...)` 只等待本地扫描、上传以及服务端持久化受理,拿到 `job_id`
135
+ 后即可继续发布;
136
+ 解析、向量化和图谱在 FastAPI 后台进行。`job_id` 表示已受理而非同步完成,只有在
137
+ 知识库必须作为发布门禁时才需要调用 `sync_status(job_id)`,其终态为 `done` 或
138
+ `failed`。
139
+
140
+ 业务项目的 `AGENTS.md` 使用以下发布规则:
141
+
142
+ ```text
143
+ 发布成功后调用同步 MCP 的 sync_now(project_root="<current absolute project root>");
144
+ 取得并汇报已受理的 job_id 后继续发布流程;除非知识库索引完成是发布门禁,否则不要等待或轮询 sync_status。
145
+ ```
146
+
147
+ 两个 MCP 入口都通过共享 HTTP 服务工作,不在 MCP 进程中直接打开数据库或向量库。
148
+
149
+ ## 已发布包(PyPI / npm)
150
+
151
+ 查询与同步两个 MCP 以 `canpoint-knowledge-mcp` 为名发布(作者:若清风)。两个
152
+ 进程都通过 `KB_SERVER_URL` 与 `KB_PROJECT_KEY` 环境变量绑定共享服务地址和项目,
153
+ 不在本机打开数据库或向量库;同步 MCP 的 `sync_now(project_root)` 由 AI 传入业务
154
+ 项目的绝对 Git 根目录,固定同步 `<project_root>/docs/**/*.md`。
155
+
156
+ - **PyPI**:`pip install canpoint-knowledge-mcp` 或
157
+ `uvx canpoint-knowledge-mcp`,提供命令行入口 `kb2-query-mcp` 与
158
+ `kb2-sync-mcp`
159
+ - **npm**:`npx -y canpoint-knowledge-mcp`(查询)或
160
+ `npx -y --package canpoint-knowledge-mcp kb2-sync-mcp`(同步);
161
+ uvx 包装器,需要先安装 [uv](https://docs.astral.sh/uv/),用法见
162
+ [npm/README.md](https://github.com/mjwyr/canpoint-mcp/blob/main/npm/README.md)
163
+
164
+ 使用前提是可访问的 KB2 共享 HTTP 服务;服务端(FastAPI/PostgreSQL/Milvus)
165
+ 不在发布包内,仍需在本仓库中部署。
166
+
167
+ 维护者一键发布(版本号以 `pyproject.toml` 为准,自动同步到 `npm/package.json`):
168
+
169
+ ```powershell
170
+ # 试跑(只构建,不发布)
171
+ .\.venv\Scripts\python.exe scripts\publish.py --dry-run
172
+
173
+ # 升补丁版本并发布双平台(凭据走环境变量)
174
+ $env:PYPI_TOKEN = "<pypi token>"; $env:NPM_TOKEN = "<npm token>"
175
+ .\.venv\Scripts\python.exe scripts\publish.py --bump patch
176
+ ```
177
+
178
+ `--platform pypi|npm|all` 可选平台,`--set-version X.Y.Z` 指定版本。
179
+
180
+ ## 图谱与版本
181
+
182
+ 显式 Markdown 相对链接在同步阶段生成 `md_link` 边;LLM 抽取的业务边在后台异步
183
+ 补齐。抽取失败只记录在 `sync_jobs`,不影响文档入库和检索,服务重启后会恢复未完
184
+ 成的图谱任务。文档更新按 `project_key + rel_path` 覆盖,删除文档会级联删除其
185
+ 章节和边;版本标签来自目录,不额外维护版本快照。
186
+
187
+ 图谱抽取按块缓存并批量请求 LLM。默认每次最多 8 个块、12,000 个正文字符,并在
188
+ 全服务范围最多并发 4 个请求;可通过 `GRAPH_EXTRACT_BATCH_SIZE`、
189
+ `GRAPH_EXTRACT_BATCH_MAX_CHARS` 和 `GRAPH_EXTRACT_CONCURRENCY` 调整。修改模型、
190
+ 提示契约或 `GRAPH_EXTRACT_CACHE_VERSION` 会自动使旧缓存失效。DeepSeek V4 默认以
191
+ `LLM_ENABLE_THINKING=false` 关闭思考模式,降低结构化抽取延迟。
192
+
193
+ 新增、更新或删除文档都会在后台重新解析全项目的关系目标,但只有新增或失效的块
194
+ 会请求 LLM;缓存完整的热运行不会产生模型调用。热运行一次读取项目级缓存,复用
195
+ 预计算的文档匹配索引,并在校验目录和缓存快照后用单事务批量替换完整文档的 LLM
196
+ 边,避免关系数增长时重复扫描正文或逐边写库。`GET /kb/sync/{job_id}` 的
197
+ `graph_chunks_total`、`graph_chunks_done`、`graph_cache_hits` 和
198
+ `graph_provider_calls` 可用于查看实际进度。`/health` 只有在 PostgreSQL 迁移完成且
199
+ Milvus collection 可用时返回健康。
200
+
201
+ ## 开发检查
202
+
203
+ ```powershell
204
+ .\.venv\Scripts\python.exe -m unittest discover -s tests -v
205
+ .\.venv\Scripts\python.exe -m compileall -q app clients config controller service
206
+ ```
207
+
208
+ `frontend-next/` 是冻结的既有可视化前端,本阶段不修改其任何文件。
@@ -0,0 +1,182 @@
1
+ # KB2 产品知识库
2
+
3
+ 本仓库提供一个面向多个业务项目的 Markdown 知识库服务:AI 在业务项目发布后调用
4
+ 同步 MCP,把 `docs/` 完整清单发送到共享 HTTP 服务;PostgreSQL 保存文档、章节、层级和
5
+ 引用边,Milvus `kb2_chunks` 保存混合检索所需的向量与原文。查询 MCP 绑定单个
6
+ `project_key`,同步 MCP 是唯一受支持的写侧入口。
7
+
8
+ ## 架构
9
+
10
+ ```text
11
+ 业务仓库 docs/**/*.md
12
+
13
+ └─ AI 发布流程 → sync_now(project_root)
14
+ │ 同步 MCP(清单、哈希、变更正文)
15
+
16
+
17
+ FastAPI /kb/sync
18
+ │ │
19
+ PostgreSQL Milvus
20
+ 结构化事实 kb2_chunks
21
+ ```
22
+
23
+ 同一项目的同步按顺序执行,清单可重复提交。Markdown 的 `docs/` 一级子目录是
24
+ 版本/分组标签;平铺文件归入 `default`,路径中名为 `prototype` 的目录不入库。
25
+ 更深层目录保留在相对路径中,中文路径按 UTF-8 处理。
26
+
27
+ ## 安装与配置
28
+
29
+ 需要 Python 3.12、可访问的 PostgreSQL 和 Milvus/Zilliz Cloud。
30
+
31
+ ```powershell
32
+ cd C:\path\to\canpoint-mcp
33
+ .\.venv\Scripts\python.exe -m pip install -r requirements.txt
34
+ ```
35
+
36
+ 复制 `.env.example` 为本机 `.env`,填写 `PG_HOST`、`PG_PORT`、`PG_USER`、
37
+ `PG_PASSWORD`、`PG_DATABASE`、`MILVUS_URI`、`MILVUS_TOKEN` 以及 embedding
38
+ 配置。真实密码、API key 只允许存在于本机环境或密钥管理器,不能提交到 Git 或
39
+ 写入文档。初始化 PostgreSQL 表和 KB2 collection:
40
+
41
+ ```powershell
42
+ .\.venv\Scripts\python.exe scripts\init_kb2.py
43
+ ```
44
+
45
+ 启动共享 HTTP 服务:
46
+
47
+ ```powershell
48
+ .\.venv\Scripts\python.exe -m uvicorn app.main:app --host 0.0.0.0 --port 8001
49
+ ```
50
+
51
+ ## HTTP 接口
52
+
53
+ | 方法与路径 | 用途 |
54
+ | --- | --- |
55
+ | `POST /kb/sync` | 提交项目完整 Markdown 清单 |
56
+ | `GET /kb/sync/{job_id}` | 查询同步和图谱任务进度 |
57
+ | `GET /kb/outline` | 按项目和可选版本读取标题树 |
58
+ | `POST /kb/search` | 项目范围内混合检索,返回父章节和行号 |
59
+ | `GET /kb/document` | 按相对路径读取全文 |
60
+ | `GET /kb/chunk/{chunk_id}` | 读取章节块、父块和子块 |
61
+ | `POST /kb/hop` | 沿项目引用边跳转相邻文档 |
62
+ | `GET /health` | 检查 PostgreSQL 与 KB2 Milvus |
63
+
64
+ 所有查询请求都必须带 `project_key`。服务端会在 PostgreSQL 和 Milvus 两侧同时
65
+ 校验项目范围,避免跨项目返回内容。
66
+
67
+ ## MCP 入口
68
+
69
+ 查询 MCP 是只读、项目绑定的 HTTP 客户端,提供 `get_outline`、`search`、
70
+ `get_document`、`get_section`、`hop`。同步 MCP 提供
71
+ `sync_now(project_root, project_key?)` 和 `sync_status(job_id)`;两个进程都只需绑定
72
+ 共享服务地址与项目标识。
73
+
74
+ 本地源码的完整 MCP 客户端配置如下;把路径与 `project_key` 换成实际值:
75
+
76
+ ```json
77
+ {
78
+ "mcpServers": {
79
+ "kb2-query-member-prototype": {
80
+ "type": "stdio",
81
+ "command": "C:\\path\\to\\canpoint-mcp\\.venv\\Scripts\\python.exe",
82
+ "args": ["C:\\path\\to\\canpoint-mcp\\query_mcp.py"],
83
+ "env": {
84
+ "KB_SERVER_URL": "http://127.0.0.1:8001",
85
+ "KB_PROJECT_KEY": "member-prototype"
86
+ }
87
+ },
88
+ "kb2-sync-member-prototype": {
89
+ "type": "stdio",
90
+ "command": "C:\\path\\to\\canpoint-mcp\\.venv\\Scripts\\python.exe",
91
+ "args": ["C:\\path\\to\\canpoint-mcp\\server.py"],
92
+ "env": {
93
+ "KB_SERVER_URL": "http://127.0.0.1:8001",
94
+ "KB_PROJECT_KEY": "member-prototype"
95
+ }
96
+ }
97
+ }
98
+ }
99
+ ```
100
+
101
+ 调用 `sync_now` 时,AI 必须传入当前业务项目的绝对 Git 根目录。工具要求该路径存在、
102
+ 就是 Git 顶层目录,且目录名与 MCP 绑定的 `project_key` 大小写不敏感地相等;随后固定
103
+ 扫描 `<project_root>/docs/**/*.md`。`docs/` 必须存在,空目录表示有意提交空的完整清单。
104
+ 可选 `project_key` 参数只作绑定断言,不能切换项目。同步不依赖业务仓库内的配置文件,
105
+ 也不提供独立命令或 Git hook 触发方式;复用的扫描与哈希代码仅是 MCP 内部实现。详见
106
+ [`clients/sync/README.md`](https://github.com/mjwyr/canpoint-mcp/blob/main/clients/sync/README.md)。
107
+
108
+ `sync_now(project_root=...)` 只等待本地扫描、上传以及服务端持久化受理,拿到 `job_id`
109
+ 后即可继续发布;
110
+ 解析、向量化和图谱在 FastAPI 后台进行。`job_id` 表示已受理而非同步完成,只有在
111
+ 知识库必须作为发布门禁时才需要调用 `sync_status(job_id)`,其终态为 `done` 或
112
+ `failed`。
113
+
114
+ 业务项目的 `AGENTS.md` 使用以下发布规则:
115
+
116
+ ```text
117
+ 发布成功后调用同步 MCP 的 sync_now(project_root="<current absolute project root>");
118
+ 取得并汇报已受理的 job_id 后继续发布流程;除非知识库索引完成是发布门禁,否则不要等待或轮询 sync_status。
119
+ ```
120
+
121
+ 两个 MCP 入口都通过共享 HTTP 服务工作,不在 MCP 进程中直接打开数据库或向量库。
122
+
123
+ ## 已发布包(PyPI / npm)
124
+
125
+ 查询与同步两个 MCP 以 `canpoint-knowledge-mcp` 为名发布(作者:若清风)。两个
126
+ 进程都通过 `KB_SERVER_URL` 与 `KB_PROJECT_KEY` 环境变量绑定共享服务地址和项目,
127
+ 不在本机打开数据库或向量库;同步 MCP 的 `sync_now(project_root)` 由 AI 传入业务
128
+ 项目的绝对 Git 根目录,固定同步 `<project_root>/docs/**/*.md`。
129
+
130
+ - **PyPI**:`pip install canpoint-knowledge-mcp` 或
131
+ `uvx canpoint-knowledge-mcp`,提供命令行入口 `kb2-query-mcp` 与
132
+ `kb2-sync-mcp`
133
+ - **npm**:`npx -y canpoint-knowledge-mcp`(查询)或
134
+ `npx -y --package canpoint-knowledge-mcp kb2-sync-mcp`(同步);
135
+ uvx 包装器,需要先安装 [uv](https://docs.astral.sh/uv/),用法见
136
+ [npm/README.md](https://github.com/mjwyr/canpoint-mcp/blob/main/npm/README.md)
137
+
138
+ 使用前提是可访问的 KB2 共享 HTTP 服务;服务端(FastAPI/PostgreSQL/Milvus)
139
+ 不在发布包内,仍需在本仓库中部署。
140
+
141
+ 维护者一键发布(版本号以 `pyproject.toml` 为准,自动同步到 `npm/package.json`):
142
+
143
+ ```powershell
144
+ # 试跑(只构建,不发布)
145
+ .\.venv\Scripts\python.exe scripts\publish.py --dry-run
146
+
147
+ # 升补丁版本并发布双平台(凭据走环境变量)
148
+ $env:PYPI_TOKEN = "<pypi token>"; $env:NPM_TOKEN = "<npm token>"
149
+ .\.venv\Scripts\python.exe scripts\publish.py --bump patch
150
+ ```
151
+
152
+ `--platform pypi|npm|all` 可选平台,`--set-version X.Y.Z` 指定版本。
153
+
154
+ ## 图谱与版本
155
+
156
+ 显式 Markdown 相对链接在同步阶段生成 `md_link` 边;LLM 抽取的业务边在后台异步
157
+ 补齐。抽取失败只记录在 `sync_jobs`,不影响文档入库和检索,服务重启后会恢复未完
158
+ 成的图谱任务。文档更新按 `project_key + rel_path` 覆盖,删除文档会级联删除其
159
+ 章节和边;版本标签来自目录,不额外维护版本快照。
160
+
161
+ 图谱抽取按块缓存并批量请求 LLM。默认每次最多 8 个块、12,000 个正文字符,并在
162
+ 全服务范围最多并发 4 个请求;可通过 `GRAPH_EXTRACT_BATCH_SIZE`、
163
+ `GRAPH_EXTRACT_BATCH_MAX_CHARS` 和 `GRAPH_EXTRACT_CONCURRENCY` 调整。修改模型、
164
+ 提示契约或 `GRAPH_EXTRACT_CACHE_VERSION` 会自动使旧缓存失效。DeepSeek V4 默认以
165
+ `LLM_ENABLE_THINKING=false` 关闭思考模式,降低结构化抽取延迟。
166
+
167
+ 新增、更新或删除文档都会在后台重新解析全项目的关系目标,但只有新增或失效的块
168
+ 会请求 LLM;缓存完整的热运行不会产生模型调用。热运行一次读取项目级缓存,复用
169
+ 预计算的文档匹配索引,并在校验目录和缓存快照后用单事务批量替换完整文档的 LLM
170
+ 边,避免关系数增长时重复扫描正文或逐边写库。`GET /kb/sync/{job_id}` 的
171
+ `graph_chunks_total`、`graph_chunks_done`、`graph_cache_hits` 和
172
+ `graph_provider_calls` 可用于查看实际进度。`/health` 只有在 PostgreSQL 迁移完成且
173
+ Milvus collection 可用时返回健康。
174
+
175
+ ## 开发检查
176
+
177
+ ```powershell
178
+ .\.venv\Scripts\python.exe -m unittest discover -s tests -v
179
+ .\.venv\Scripts\python.exe -m compileall -q app clients config controller service
180
+ ```
181
+
182
+ `frontend-next/` 是冻结的既有可视化前端,本阶段不修改其任何文件。