okf-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.
- okf_mcp-0.1.0/LICENSE +201 -0
- okf_mcp-0.1.0/PKG-INFO +72 -0
- okf_mcp-0.1.0/README.md +57 -0
- okf_mcp-0.1.0/okf_mcp/__init__.py +5 -0
- okf_mcp-0.1.0/okf_mcp/registry.py +151 -0
- okf_mcp-0.1.0/okf_mcp/server.py +139 -0
- okf_mcp-0.1.0/okf_mcp.egg-info/PKG-INFO +72 -0
- okf_mcp-0.1.0/okf_mcp.egg-info/SOURCES.txt +13 -0
- okf_mcp-0.1.0/okf_mcp.egg-info/dependency_links.txt +1 -0
- okf_mcp-0.1.0/okf_mcp.egg-info/entry_points.txt +2 -0
- okf_mcp-0.1.0/okf_mcp.egg-info/requires.txt +2 -0
- okf_mcp-0.1.0/okf_mcp.egg-info/top_level.txt +1 -0
- okf_mcp-0.1.0/pyproject.toml +26 -0
- okf_mcp-0.1.0/setup.cfg +4 -0
- okf_mcp-0.1.0/tests/test_okf_mcp.py +117 -0
okf_mcp-0.1.0/LICENSE
ADDED
|
@@ -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 Derivative
|
|
95
|
+
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 2026 okf-ingest contributors
|
|
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.
|
okf_mcp-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: okf-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MCP server for Open Knowledge Format (OKF) bundles — expose okf-ingest's deterministic consume verbs (search, context, graph impact, diff, doctor) as agent tools.
|
|
5
|
+
Author-email: Travis Jakel <travis.s.jakel@gmail.com>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/travisjakel/okf-mcp
|
|
8
|
+
Keywords: okf,open-knowledge-format,mcp,model-context-protocol,knowledge-graph,duckdb
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: okf-ingest>=0.7.0
|
|
13
|
+
Requires-Dist: mcp>=1.2
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# okf-mcp
|
|
17
|
+
|
|
18
|
+
**MCP server for [Open Knowledge Format](https://github.com/GoogleCloudPlatform/knowledge-catalog) bundles** — expose [okf-ingest](https://github.com/travisjakel/okf-ingest)'s deterministic consume verbs as tools any MCP client (Claude Code, Cursor, custom agents) can call.
|
|
19
|
+
|
|
20
|
+
Existing OKF MCP servers hand agents the markdown *files*. This one hands them
|
|
21
|
+
the **queryable catalog and concept graph**: index-first context assembly,
|
|
22
|
+
impact/backlink analysis, SQL over frontmatter, drift diffs, and health checks —
|
|
23
|
+
all deterministic okf-ingest code, no model calls in the server.
|
|
24
|
+
|
|
25
|
+
## Tools
|
|
26
|
+
|
|
27
|
+
| Tool | What the agent gets |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `okf_list_bundles` | which bundles this server exposes |
|
|
30
|
+
| `okf_search` | concepts whose title/body match a term |
|
|
31
|
+
| `okf_get_concept` | one concept's frontmatter + full body |
|
|
32
|
+
| `okf_context` | **the flagship** — index.md + a concept's link-neighborhood as one curated markdown blob (the author's graph, not fuzzy matches) |
|
|
33
|
+
| `okf_impact` | inbound / outbound / transitive links — "what breaks if X changes" |
|
|
34
|
+
| `okf_sql` | read-only SELECT over the DuckDB catalog (`okf_concept`, `okf_link`, `okf_validation`) |
|
|
35
|
+
| `okf_diff` | what changed on disk since the server loaded the bundle — an agent's memory-refresh between looks |
|
|
36
|
+
| `okf_refresh` | re-ingest a directory bundle after `okf_diff` shows changes |
|
|
37
|
+
| `okf_doctor` | health score + per-rule findings before trusting a bundle |
|
|
38
|
+
|
|
39
|
+
## Install & run
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install okf-mcp # depends on okf-ingest >= 0.7.0
|
|
43
|
+
|
|
44
|
+
# one or more bundles: directories and/or okf-ingest .duckdb catalogs,
|
|
45
|
+
# optionally named (first one is the default target)
|
|
46
|
+
okf-mcp ~/my-bundle
|
|
47
|
+
okf-mcp wiki=~/wiki notes=~/vault snapshot=./kb.duckdb
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Register with Claude Code:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
claude mcp add okf -- okf-mcp ~/my-bundle
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Or in any MCP client config:
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{ "mcpServers": { "okf": { "command": "okf-mcp", "args": ["wiki=/path/to/bundle"] } } }
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Design notes
|
|
63
|
+
|
|
64
|
+
- **Read-only.** Directory bundles are ingested into disposable in-memory
|
|
65
|
+
DuckDB catalogs at startup; `.duckdb` sources open read-only; `okf_sql`
|
|
66
|
+
accepts SELECT/WITH only. The server never writes to your bundle.
|
|
67
|
+
- **Deterministic.** Every tool is plain okf-ingest code — same input, same
|
|
68
|
+
answer; the only nondeterminism an agent sees is its own.
|
|
69
|
+
- **`[[wikilinks]]` work.** Vault-style bundles (Obsidian/Logseq/Foam) resolve
|
|
70
|
+
by id / alias / title / stem, same as okf-ingest 0.6.0+.
|
|
71
|
+
|
|
72
|
+
Apache-2.0. Sibling project: [okf-ingest](https://github.com/travisjakel/okf-ingest) (the R + Python ingestion tool this wraps).
|
okf_mcp-0.1.0/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# okf-mcp
|
|
2
|
+
|
|
3
|
+
**MCP server for [Open Knowledge Format](https://github.com/GoogleCloudPlatform/knowledge-catalog) bundles** — expose [okf-ingest](https://github.com/travisjakel/okf-ingest)'s deterministic consume verbs as tools any MCP client (Claude Code, Cursor, custom agents) can call.
|
|
4
|
+
|
|
5
|
+
Existing OKF MCP servers hand agents the markdown *files*. This one hands them
|
|
6
|
+
the **queryable catalog and concept graph**: index-first context assembly,
|
|
7
|
+
impact/backlink analysis, SQL over frontmatter, drift diffs, and health checks —
|
|
8
|
+
all deterministic okf-ingest code, no model calls in the server.
|
|
9
|
+
|
|
10
|
+
## Tools
|
|
11
|
+
|
|
12
|
+
| Tool | What the agent gets |
|
|
13
|
+
|---|---|
|
|
14
|
+
| `okf_list_bundles` | which bundles this server exposes |
|
|
15
|
+
| `okf_search` | concepts whose title/body match a term |
|
|
16
|
+
| `okf_get_concept` | one concept's frontmatter + full body |
|
|
17
|
+
| `okf_context` | **the flagship** — index.md + a concept's link-neighborhood as one curated markdown blob (the author's graph, not fuzzy matches) |
|
|
18
|
+
| `okf_impact` | inbound / outbound / transitive links — "what breaks if X changes" |
|
|
19
|
+
| `okf_sql` | read-only SELECT over the DuckDB catalog (`okf_concept`, `okf_link`, `okf_validation`) |
|
|
20
|
+
| `okf_diff` | what changed on disk since the server loaded the bundle — an agent's memory-refresh between looks |
|
|
21
|
+
| `okf_refresh` | re-ingest a directory bundle after `okf_diff` shows changes |
|
|
22
|
+
| `okf_doctor` | health score + per-rule findings before trusting a bundle |
|
|
23
|
+
|
|
24
|
+
## Install & run
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install okf-mcp # depends on okf-ingest >= 0.7.0
|
|
28
|
+
|
|
29
|
+
# one or more bundles: directories and/or okf-ingest .duckdb catalogs,
|
|
30
|
+
# optionally named (first one is the default target)
|
|
31
|
+
okf-mcp ~/my-bundle
|
|
32
|
+
okf-mcp wiki=~/wiki notes=~/vault snapshot=./kb.duckdb
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Register with Claude Code:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
claude mcp add okf -- okf-mcp ~/my-bundle
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Or in any MCP client config:
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{ "mcpServers": { "okf": { "command": "okf-mcp", "args": ["wiki=/path/to/bundle"] } } }
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Design notes
|
|
48
|
+
|
|
49
|
+
- **Read-only.** Directory bundles are ingested into disposable in-memory
|
|
50
|
+
DuckDB catalogs at startup; `.duckdb` sources open read-only; `okf_sql`
|
|
51
|
+
accepts SELECT/WITH only. The server never writes to your bundle.
|
|
52
|
+
- **Deterministic.** Every tool is plain okf-ingest code — same input, same
|
|
53
|
+
answer; the only nondeterminism an agent sees is its own.
|
|
54
|
+
- **`[[wikilinks]]` work.** Vault-style bundles (Obsidian/Logseq/Foam) resolve
|
|
55
|
+
by id / alias / title / stem, same as okf-ingest 0.6.0+.
|
|
56
|
+
|
|
57
|
+
Apache-2.0. Sibling project: [okf-ingest](https://github.com/travisjakel/okf-ingest) (the R + Python ingestion tool this wraps).
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""okf-mcp — bundle registry and tool logic.
|
|
2
|
+
|
|
3
|
+
Plain functions over a BundleRegistry so the logic is testable without an MCP
|
|
4
|
+
transport; okf_mcp.server wraps these as MCP tools. All operations are
|
|
5
|
+
READ-ONLY with respect to the source bundles: directory sources are ingested
|
|
6
|
+
into disposable in-memory DuckDB catalogs at startup (refresh re-ingests),
|
|
7
|
+
.duckdb sources are opened read-only. Everything is deterministic okf-ingest
|
|
8
|
+
code — no model calls.
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from typing import Any, Optional
|
|
15
|
+
|
|
16
|
+
import duckdb
|
|
17
|
+
import okf
|
|
18
|
+
from okf.diff import diff as okf_diff
|
|
19
|
+
from okf.doctor import doctor as okf_doctor
|
|
20
|
+
from okf.graph import backlinks as okf_backlinks, impact as okf_impact
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class Bundle:
|
|
25
|
+
name: str
|
|
26
|
+
source: str # directory or .duckdb path
|
|
27
|
+
kind: str # "dir" | "catalog"
|
|
28
|
+
con: Any # duckdb connection
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class BundleRegistry:
|
|
32
|
+
"""Named OKF bundles the server exposes. The first added is the default."""
|
|
33
|
+
|
|
34
|
+
def __init__(self) -> None:
|
|
35
|
+
self.bundles: dict[str, Bundle] = {}
|
|
36
|
+
|
|
37
|
+
def add(self, name: str, source: str) -> Bundle:
|
|
38
|
+
if name in self.bundles:
|
|
39
|
+
raise ValueError(f"duplicate bundle name: {name}")
|
|
40
|
+
source = os.path.abspath(source)
|
|
41
|
+
if source.endswith(".duckdb") and os.path.isfile(source):
|
|
42
|
+
con = duckdb.connect(source, read_only=True)
|
|
43
|
+
b = Bundle(name, source, "catalog", con)
|
|
44
|
+
elif os.path.isdir(source):
|
|
45
|
+
con, _ = okf.ingest(source) # in-memory catalog
|
|
46
|
+
b = Bundle(name, source, "dir", con)
|
|
47
|
+
else:
|
|
48
|
+
raise ValueError(f"not a bundle dir or .duckdb catalog: {source}")
|
|
49
|
+
self.bundles[name] = b
|
|
50
|
+
return b
|
|
51
|
+
|
|
52
|
+
def get(self, name: Optional[str] = None) -> Bundle:
|
|
53
|
+
if not self.bundles:
|
|
54
|
+
raise RuntimeError("no bundles loaded")
|
|
55
|
+
if name is None:
|
|
56
|
+
return next(iter(self.bundles.values()))
|
|
57
|
+
if name not in self.bundles:
|
|
58
|
+
raise ValueError(
|
|
59
|
+
f"unknown bundle {name!r}; available: {', '.join(self.bundles)}")
|
|
60
|
+
return self.bundles[name]
|
|
61
|
+
|
|
62
|
+
def refresh(self, name: Optional[str] = None) -> dict:
|
|
63
|
+
b = self.get(name)
|
|
64
|
+
if b.kind != "dir":
|
|
65
|
+
raise ValueError(f"bundle {b.name!r} is a static catalog; nothing to refresh")
|
|
66
|
+
old = b.con
|
|
67
|
+
b.con, summary = okf.ingest(b.source)
|
|
68
|
+
old.close()
|
|
69
|
+
keep = ("n_concepts", "conformant", "links_total", "links_broken")
|
|
70
|
+
return {"bundle": b.name, **{k: v for k, v in summary.items() if k in keep}}
|
|
71
|
+
|
|
72
|
+
def close(self) -> None:
|
|
73
|
+
for b in self.bundles.values():
|
|
74
|
+
try:
|
|
75
|
+
b.con.close()
|
|
76
|
+
except Exception:
|
|
77
|
+
pass
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# ---- tool logic -------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
def _rows(cur) -> list[dict]:
|
|
83
|
+
cols = [d[0] for d in cur.description]
|
|
84
|
+
return [dict(zip(cols, r)) for r in cur.fetchall()]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def list_bundles(reg: BundleRegistry) -> list[dict]:
|
|
88
|
+
out = []
|
|
89
|
+
for b in reg.bundles.values():
|
|
90
|
+
n = b.con.execute(
|
|
91
|
+
"SELECT count(*) FROM okf_concept WHERE reserved = FALSE").fetchone()[0]
|
|
92
|
+
out.append({"name": b.name, "source": b.source, "kind": b.kind,
|
|
93
|
+
"n_concepts": n, "default": b is next(iter(reg.bundles.values()))})
|
|
94
|
+
return out
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def search(reg: BundleRegistry, term: str, bundle: Optional[str] = None,
|
|
98
|
+
limit: int = 20) -> list[dict]:
|
|
99
|
+
b = reg.get(bundle)
|
|
100
|
+
return _rows(b.con.execute(
|
|
101
|
+
"SELECT path, type, title, description FROM okf_concept "
|
|
102
|
+
"WHERE body ILIKE ? OR title ILIKE ? ORDER BY path LIMIT ?",
|
|
103
|
+
[f"%{term}%", f"%{term}%", int(limit)]))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def sql(reg: BundleRegistry, query: str, bundle: Optional[str] = None) -> list[dict]:
|
|
107
|
+
q = query.lstrip().lower()
|
|
108
|
+
if not (q.startswith("select") or q.startswith("with")):
|
|
109
|
+
raise ValueError("only SELECT/WITH queries are allowed")
|
|
110
|
+
b = reg.get(bundle)
|
|
111
|
+
return _rows(b.con.execute(query))
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def get_concept(reg: BundleRegistry, path: str, bundle: Optional[str] = None) -> dict:
|
|
115
|
+
b = reg.get(bundle)
|
|
116
|
+
rows = _rows(b.con.execute(
|
|
117
|
+
"SELECT path, type, title, description, tags, timestamp, body "
|
|
118
|
+
"FROM okf_concept WHERE path = ?", [path]))
|
|
119
|
+
if not rows:
|
|
120
|
+
raise ValueError(f"concept not found: {path}")
|
|
121
|
+
return rows[0]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def context(reg: BundleRegistry, start: Optional[str] = None, depth: int = 1,
|
|
125
|
+
max_tokens: int = 8000, bundle: Optional[str] = None) -> dict:
|
|
126
|
+
b = reg.get(bundle)
|
|
127
|
+
return okf.context(b.con, start=start, depth=depth, max_tokens=max_tokens)
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def impact(reg: BundleRegistry, concept: str, bundle: Optional[str] = None) -> dict:
|
|
131
|
+
b = reg.get(bundle)
|
|
132
|
+
return okf_impact(b.con, concept)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def backlinks(reg: BundleRegistry, concept: str, bundle: Optional[str] = None) -> list:
|
|
136
|
+
b = reg.get(bundle)
|
|
137
|
+
return okf_backlinks(b.con, concept)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def diff(reg: BundleRegistry, bundle: Optional[str] = None) -> dict:
|
|
141
|
+
b = reg.get(bundle)
|
|
142
|
+
if b.kind != "dir":
|
|
143
|
+
raise ValueError(
|
|
144
|
+
f"bundle {b.name!r} is a static catalog; drift-diff needs a directory source")
|
|
145
|
+
return okf_diff(b.con, b.source)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def doctor(reg: BundleRegistry, bundle: Optional[str] = None,
|
|
149
|
+
stale_days: Optional[int] = None, now: Optional[str] = None) -> dict:
|
|
150
|
+
b = reg.get(bundle)
|
|
151
|
+
return okf_doctor(b.con, now=now, stale_days=stale_days)
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""okf-mcp — MCP server exposing okf-ingest's consume verbs as agent tools.
|
|
2
|
+
|
|
3
|
+
Usage:
|
|
4
|
+
okf-mcp <bundle> [<bundle> ...]
|
|
5
|
+
|
|
6
|
+
Each <bundle> is a directory (an OKF bundle of markdown files) or a .duckdb
|
|
7
|
+
catalog produced by `okf ingest`, optionally prefixed with a name:
|
|
8
|
+
okf-mcp ~/wiki notes=~/vault snapshot=./kb.duckdb
|
|
9
|
+
|
|
10
|
+
Runs on stdio (the standard MCP local-server transport). Directory bundles are
|
|
11
|
+
ingested into disposable in-memory catalogs at startup; sources are never
|
|
12
|
+
written to. Register with an MCP client, e.g.:
|
|
13
|
+
claude mcp add okf -- okf-mcp ~/my-bundle
|
|
14
|
+
"""
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import datetime
|
|
18
|
+
import sys
|
|
19
|
+
from typing import Optional
|
|
20
|
+
|
|
21
|
+
from mcp.server.fastmcp import FastMCP
|
|
22
|
+
|
|
23
|
+
from . import registry as R
|
|
24
|
+
|
|
25
|
+
mcp = FastMCP("okf")
|
|
26
|
+
reg = R.BundleRegistry()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@mcp.tool()
|
|
30
|
+
def okf_list_bundles() -> list[dict]:
|
|
31
|
+
"""List the knowledge bundles this server exposes (name, source, concept
|
|
32
|
+
count). Call this first when unsure which bundle to target; every other
|
|
33
|
+
tool takes an optional `bundle` name and defaults to the first bundle."""
|
|
34
|
+
return R.list_bundles(reg)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@mcp.tool()
|
|
38
|
+
def okf_search(term: str, bundle: Optional[str] = None, limit: int = 20) -> list[dict]:
|
|
39
|
+
"""Find concepts whose title or body contains `term` (case-insensitive
|
|
40
|
+
substring). Call this to locate relevant concepts before reading them —
|
|
41
|
+
returns path/type/title/description; follow up with okf_get_concept or
|
|
42
|
+
okf_context on a returned path."""
|
|
43
|
+
return R.search(reg, term, bundle, limit)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@mcp.tool()
|
|
47
|
+
def okf_get_concept(path: str, bundle: Optional[str] = None) -> dict:
|
|
48
|
+
"""Read one concept (its frontmatter fields and full markdown body) by its
|
|
49
|
+
bundle-relative path, e.g. 'ops/backups.md'. Use after okf_search when you
|
|
50
|
+
need the actual content of a single concept."""
|
|
51
|
+
return R.get_concept(reg, path, bundle)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@mcp.tool()
|
|
55
|
+
def okf_context(start: Optional[str] = None, depth: int = 1,
|
|
56
|
+
max_tokens: int = 8000, bundle: Optional[str] = None) -> dict:
|
|
57
|
+
"""Assemble a curated, index-first context blob: index.md plus the concept
|
|
58
|
+
at `start` and everything within `depth` links of it, as one markdown
|
|
59
|
+
string (capped near `max_tokens`). This follows the links the bundle's
|
|
60
|
+
author wrote — prefer it over search when you want the full neighborhood
|
|
61
|
+
of a topic rather than keyword matches. Omit `start` to pack the whole
|
|
62
|
+
bundle."""
|
|
63
|
+
return R.context(reg, start, depth, max_tokens, bundle)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@mcp.tool()
|
|
67
|
+
def okf_impact(concept: str, bundle: Optional[str] = None) -> dict:
|
|
68
|
+
"""Report what links to and from a concept: outbound links, inbound links
|
|
69
|
+
(backlinks), and the full transitive set of concepts reachable from it.
|
|
70
|
+
Call this to answer 'what depends on X' or 'what breaks if X changes'."""
|
|
71
|
+
return R.impact(reg, concept, bundle)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@mcp.tool()
|
|
75
|
+
def okf_sql(query: str, bundle: Optional[str] = None) -> list[dict]:
|
|
76
|
+
"""Run a read-only SQL (SELECT/WITH) query against the bundle's DuckDB
|
|
77
|
+
catalog. Tables: okf_concept (path, type, title, description, tags,
|
|
78
|
+
timestamp, body, content_hash), okf_link (src_path, dst_raw, dst_path,
|
|
79
|
+
resolved), okf_validation (path, severity, rule, message). Use for
|
|
80
|
+
structured questions the other tools don't cover, e.g. counting concepts
|
|
81
|
+
by type or listing everything tagged 'x'."""
|
|
82
|
+
return R.sql(reg, query, bundle)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@mcp.tool()
|
|
86
|
+
def okf_diff(bundle: Optional[str] = None) -> dict:
|
|
87
|
+
"""Report what changed in a directory-backed bundle since this server
|
|
88
|
+
loaded it (or since the last okf_refresh): concepts added/removed/changed,
|
|
89
|
+
type/title changes, links added/removed and newly broken/fixed. Call this
|
|
90
|
+
to re-sync your understanding after files may have changed; if it shows
|
|
91
|
+
changes, call okf_refresh to load them."""
|
|
92
|
+
return R.diff(reg, bundle)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
@mcp.tool()
|
|
96
|
+
def okf_refresh(bundle: Optional[str] = None) -> dict:
|
|
97
|
+
"""Re-ingest a directory-backed bundle so the catalog reflects the current
|
|
98
|
+
files. Call after okf_diff reports changes, or when you know the bundle
|
|
99
|
+
was edited since the server started."""
|
|
100
|
+
return reg.refresh(bundle)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@mcp.tool()
|
|
104
|
+
def okf_doctor(bundle: Optional[str] = None, stale_days: Optional[int] = None) -> dict:
|
|
105
|
+
"""Health report for a bundle: score (0-100 = % of concepts with zero
|
|
106
|
+
findings), error/warning counts, and per-rule counts (broken links,
|
|
107
|
+
orphans, missing fields, duplicate titles; timestamps older than
|
|
108
|
+
`stale_days` if given). Call before relying heavily on a bundle, or to
|
|
109
|
+
answer 'is this knowledge base healthy'."""
|
|
110
|
+
now = None
|
|
111
|
+
if stale_days is not None:
|
|
112
|
+
now = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
113
|
+
return R.doctor(reg, bundle, stale_days, now)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def main(argv: Optional[list[str]] = None) -> int:
|
|
117
|
+
args = sys.argv[1:] if argv is None else argv
|
|
118
|
+
if not args or args[0] in ("-h", "--help"):
|
|
119
|
+
print(__doc__)
|
|
120
|
+
return 0 if args else 2
|
|
121
|
+
for a in args:
|
|
122
|
+
if "=" in a and not a.split("=", 1)[0].startswith((".", "/", "\\")) \
|
|
123
|
+
and ":" not in a.split("=", 1)[0]:
|
|
124
|
+
name, source = a.split("=", 1)
|
|
125
|
+
else:
|
|
126
|
+
name, source = None, a
|
|
127
|
+
if name is None:
|
|
128
|
+
base = source.rstrip("/\\").replace("\\", "/").rsplit("/", 1)[-1]
|
|
129
|
+
name = base[:-7] if base.endswith(".duckdb") else base
|
|
130
|
+
reg.add(name, source)
|
|
131
|
+
try:
|
|
132
|
+
mcp.run() # stdio transport
|
|
133
|
+
finally:
|
|
134
|
+
reg.close()
|
|
135
|
+
return 0
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
if __name__ == "__main__":
|
|
139
|
+
sys.exit(main())
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: okf-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MCP server for Open Knowledge Format (OKF) bundles — expose okf-ingest's deterministic consume verbs (search, context, graph impact, diff, doctor) as agent tools.
|
|
5
|
+
Author-email: Travis Jakel <travis.s.jakel@gmail.com>
|
|
6
|
+
License: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/travisjakel/okf-mcp
|
|
8
|
+
Keywords: okf,open-knowledge-format,mcp,model-context-protocol,knowledge-graph,duckdb
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: okf-ingest>=0.7.0
|
|
13
|
+
Requires-Dist: mcp>=1.2
|
|
14
|
+
Dynamic: license-file
|
|
15
|
+
|
|
16
|
+
# okf-mcp
|
|
17
|
+
|
|
18
|
+
**MCP server for [Open Knowledge Format](https://github.com/GoogleCloudPlatform/knowledge-catalog) bundles** — expose [okf-ingest](https://github.com/travisjakel/okf-ingest)'s deterministic consume verbs as tools any MCP client (Claude Code, Cursor, custom agents) can call.
|
|
19
|
+
|
|
20
|
+
Existing OKF MCP servers hand agents the markdown *files*. This one hands them
|
|
21
|
+
the **queryable catalog and concept graph**: index-first context assembly,
|
|
22
|
+
impact/backlink analysis, SQL over frontmatter, drift diffs, and health checks —
|
|
23
|
+
all deterministic okf-ingest code, no model calls in the server.
|
|
24
|
+
|
|
25
|
+
## Tools
|
|
26
|
+
|
|
27
|
+
| Tool | What the agent gets |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `okf_list_bundles` | which bundles this server exposes |
|
|
30
|
+
| `okf_search` | concepts whose title/body match a term |
|
|
31
|
+
| `okf_get_concept` | one concept's frontmatter + full body |
|
|
32
|
+
| `okf_context` | **the flagship** — index.md + a concept's link-neighborhood as one curated markdown blob (the author's graph, not fuzzy matches) |
|
|
33
|
+
| `okf_impact` | inbound / outbound / transitive links — "what breaks if X changes" |
|
|
34
|
+
| `okf_sql` | read-only SELECT over the DuckDB catalog (`okf_concept`, `okf_link`, `okf_validation`) |
|
|
35
|
+
| `okf_diff` | what changed on disk since the server loaded the bundle — an agent's memory-refresh between looks |
|
|
36
|
+
| `okf_refresh` | re-ingest a directory bundle after `okf_diff` shows changes |
|
|
37
|
+
| `okf_doctor` | health score + per-rule findings before trusting a bundle |
|
|
38
|
+
|
|
39
|
+
## Install & run
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install okf-mcp # depends on okf-ingest >= 0.7.0
|
|
43
|
+
|
|
44
|
+
# one or more bundles: directories and/or okf-ingest .duckdb catalogs,
|
|
45
|
+
# optionally named (first one is the default target)
|
|
46
|
+
okf-mcp ~/my-bundle
|
|
47
|
+
okf-mcp wiki=~/wiki notes=~/vault snapshot=./kb.duckdb
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Register with Claude Code:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
claude mcp add okf -- okf-mcp ~/my-bundle
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Or in any MCP client config:
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{ "mcpServers": { "okf": { "command": "okf-mcp", "args": ["wiki=/path/to/bundle"] } } }
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Design notes
|
|
63
|
+
|
|
64
|
+
- **Read-only.** Directory bundles are ingested into disposable in-memory
|
|
65
|
+
DuckDB catalogs at startup; `.duckdb` sources open read-only; `okf_sql`
|
|
66
|
+
accepts SELECT/WITH only. The server never writes to your bundle.
|
|
67
|
+
- **Deterministic.** Every tool is plain okf-ingest code — same input, same
|
|
68
|
+
answer; the only nondeterminism an agent sees is its own.
|
|
69
|
+
- **`[[wikilinks]]` work.** Vault-style bundles (Obsidian/Logseq/Foam) resolve
|
|
70
|
+
by id / alias / title / stem, same as okf-ingest 0.6.0+.
|
|
71
|
+
|
|
72
|
+
Apache-2.0. Sibling project: [okf-ingest](https://github.com/travisjakel/okf-ingest) (the R + Python ingestion tool this wraps).
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
okf_mcp/__init__.py
|
|
5
|
+
okf_mcp/registry.py
|
|
6
|
+
okf_mcp/server.py
|
|
7
|
+
okf_mcp.egg-info/PKG-INFO
|
|
8
|
+
okf_mcp.egg-info/SOURCES.txt
|
|
9
|
+
okf_mcp.egg-info/dependency_links.txt
|
|
10
|
+
okf_mcp.egg-info/entry_points.txt
|
|
11
|
+
okf_mcp.egg-info/requires.txt
|
|
12
|
+
okf_mcp.egg-info/top_level.txt
|
|
13
|
+
tests/test_okf_mcp.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
okf_mcp
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "okf-mcp"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "MCP server for Open Knowledge Format (OKF) bundles — expose okf-ingest's deterministic consume verbs (search, context, graph impact, diff, doctor) as agent tools."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "Apache-2.0" }
|
|
11
|
+
authors = [{ name = "Travis Jakel", email = "travis.s.jakel@gmail.com" }]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
dependencies = [
|
|
14
|
+
"okf-ingest>=0.7.0",
|
|
15
|
+
"mcp>=1.2",
|
|
16
|
+
]
|
|
17
|
+
keywords = ["okf", "open-knowledge-format", "mcp", "model-context-protocol", "knowledge-graph", "duckdb"]
|
|
18
|
+
|
|
19
|
+
[project.urls]
|
|
20
|
+
Homepage = "https://github.com/travisjakel/okf-mcp"
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
okf-mcp = "okf_mcp.server:main"
|
|
24
|
+
|
|
25
|
+
[tool.setuptools]
|
|
26
|
+
packages = ["okf_mcp"]
|
okf_mcp-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""Offline tests: registry/tool logic on a tiny inline bundle, plus an
|
|
2
|
+
in-process MCP handshake (client <-> server over memory streams)."""
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
import textwrap
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
|
|
9
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
10
|
+
from okf_mcp import registry as R # noqa: E402
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def make_bundle(tmp_path):
|
|
14
|
+
d = tmp_path / "bundle"
|
|
15
|
+
d.mkdir()
|
|
16
|
+
(d / "index.md").write_text(textwrap.dedent("""\
|
|
17
|
+
---
|
|
18
|
+
type: Index
|
|
19
|
+
title: Home
|
|
20
|
+
---
|
|
21
|
+
# Home
|
|
22
|
+
- [Alpha](alpha.md)
|
|
23
|
+
- [Beta](beta.md)
|
|
24
|
+
"""), encoding="utf-8")
|
|
25
|
+
(d / "alpha.md").write_text(textwrap.dedent("""\
|
|
26
|
+
---
|
|
27
|
+
type: Signal
|
|
28
|
+
title: Alpha
|
|
29
|
+
description: The alpha concept.
|
|
30
|
+
---
|
|
31
|
+
# Alpha
|
|
32
|
+
Depends on [Beta](beta.md). Mentions quicksilver.
|
|
33
|
+
"""), encoding="utf-8")
|
|
34
|
+
(d / "beta.md").write_text(textwrap.dedent("""\
|
|
35
|
+
---
|
|
36
|
+
type: Runbook
|
|
37
|
+
title: Beta
|
|
38
|
+
description: The beta concept.
|
|
39
|
+
---
|
|
40
|
+
# Beta
|
|
41
|
+
Body of beta.
|
|
42
|
+
"""), encoding="utf-8")
|
|
43
|
+
return str(d)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@pytest.fixture()
|
|
47
|
+
def reg(tmp_path):
|
|
48
|
+
r = R.BundleRegistry()
|
|
49
|
+
r.add("test", make_bundle(tmp_path))
|
|
50
|
+
yield r
|
|
51
|
+
r.close()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_list_and_search(reg):
|
|
55
|
+
bundles = R.list_bundles(reg)
|
|
56
|
+
assert bundles[0]["name"] == "test" and bundles[0]["n_concepts"] == 2
|
|
57
|
+
hits = R.search(reg, "quicksilver")
|
|
58
|
+
assert [h["path"] for h in hits] == ["alpha.md"]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_get_concept_and_context(reg):
|
|
62
|
+
c = R.get_concept(reg, "alpha.md")
|
|
63
|
+
assert c["type"] == "Signal" and "quicksilver" in c["body"]
|
|
64
|
+
ctx = R.context(reg, start="alpha.md", depth=1)
|
|
65
|
+
assert "Alpha" in ctx["text"] and "beta.md" in ctx["included"]
|
|
66
|
+
with pytest.raises(ValueError):
|
|
67
|
+
R.get_concept(reg, "nope.md")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_impact_and_sql(reg):
|
|
71
|
+
im = R.impact(reg, "beta.md")
|
|
72
|
+
assert "alpha.md" in im["inbound"]
|
|
73
|
+
rows = R.sql(reg, "SELECT count(*) AS n FROM okf_concept WHERE reserved = FALSE")
|
|
74
|
+
assert rows[0]["n"] == 2
|
|
75
|
+
with pytest.raises(ValueError):
|
|
76
|
+
R.sql(reg, "DELETE FROM okf_concept")
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_diff_refresh_doctor(reg, tmp_path):
|
|
80
|
+
assert R.diff(reg)["identical"] is True
|
|
81
|
+
# mutate the source dir -> drift shows, refresh clears it
|
|
82
|
+
(tmp_path / "bundle" / "gamma.md").write_text(
|
|
83
|
+
"---\ntype: Tool\ntitle: Gamma\n---\n# Gamma\n", encoding="utf-8")
|
|
84
|
+
d = R.diff(reg)
|
|
85
|
+
assert d["added"] == ["gamma.md"] and d["identical"] is False
|
|
86
|
+
reg.refresh()
|
|
87
|
+
assert R.diff(reg)["identical"] is True
|
|
88
|
+
rep = R.doctor(reg)
|
|
89
|
+
assert 0 <= rep["score"] <= 100
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@pytest.mark.anyio
|
|
93
|
+
async def test_mcp_handshake(tmp_path):
|
|
94
|
+
"""Real MCP round-trip: server + client over in-memory streams."""
|
|
95
|
+
from mcp.shared.memory import create_connected_server_and_client_session
|
|
96
|
+
from okf_mcp import server as S
|
|
97
|
+
|
|
98
|
+
S.reg.close()
|
|
99
|
+
S.reg.bundles.clear()
|
|
100
|
+
S.reg.add("test", make_bundle(tmp_path))
|
|
101
|
+
try:
|
|
102
|
+
async with create_connected_server_and_client_session(
|
|
103
|
+
S.mcp._mcp_server) as session:
|
|
104
|
+
tools = await session.list_tools()
|
|
105
|
+
names = {t.name for t in tools.tools}
|
|
106
|
+
assert {"okf_search", "okf_context", "okf_diff", "okf_doctor"} <= names
|
|
107
|
+
res = await session.call_tool("okf_search", {"term": "quicksilver"})
|
|
108
|
+
assert not res.isError
|
|
109
|
+
assert "alpha.md" in res.content[0].text
|
|
110
|
+
finally:
|
|
111
|
+
S.reg.close()
|
|
112
|
+
S.reg.bundles.clear()
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@pytest.fixture
|
|
116
|
+
def anyio_backend():
|
|
117
|
+
return "asyncio"
|