mcp-kb-sqlite 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.
@@ -0,0 +1,32 @@
1
+ name: CI
2
+
3
+ on:
4
+ pull_request:
5
+ branches: [main]
6
+
7
+ jobs:
8
+ check-version:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v4
12
+ - name: Check version has no pre-release suffix
13
+ run: |
14
+ VERSION=$(grep '^version' pyproject.toml | cut -d'"' -f2)
15
+ echo "Version: $VERSION"
16
+ if ! echo "$VERSION" | grep -qE '^[0-9]+(\.[0-9]+)*$'; then
17
+ echo "ERROR: Version '$VERSION' is not a clean release version (only digits and dots allowed). Remove any pre-release suffix before merging to main."
18
+ exit 1
19
+ fi
20
+ echo "OK: Version '$VERSION' is a clean release version."
21
+
22
+ check-lock:
23
+ runs-on: ubuntu-latest
24
+ steps:
25
+ - uses: actions/checkout@v4
26
+ - uses: astral-sh/setup-uv@v5
27
+ - name: Verify uv.lock is up to date
28
+ run: uv lock --check
29
+
30
+ test:
31
+ needs: check-lock
32
+ uses: ./.github/workflows/test.yml
@@ -0,0 +1,51 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+
7
+ jobs:
8
+ test:
9
+ uses: ./.github/workflows/test.yml
10
+
11
+ publish:
12
+ needs: test
13
+ runs-on: ubuntu-latest
14
+ environment: pypi
15
+ permissions:
16
+ id-token: write
17
+ contents: write
18
+ steps:
19
+ - uses: actions/checkout@v4
20
+ with:
21
+ fetch-depth: 2
22
+ - name: Check version changed
23
+ id: version
24
+ run: |
25
+ VERSION=$(grep '^version' pyproject.toml | cut -d'"' -f2)
26
+ if git show HEAD~1:pyproject.toml > /dev/null 2>&1; then
27
+ PREV=$(git diff HEAD~1 pyproject.toml | grep '^\-version' | cut -d'"' -f2)
28
+ else
29
+ PREV="$VERSION" # pyproject.toml is new, so this is always a version change
30
+ fi
31
+ if [ -z "$PREV" ]; then
32
+ echo "changed=false" >> $GITHUB_OUTPUT
33
+ else
34
+ echo "changed=true" >> $GITHUB_OUTPUT
35
+ echo "version=$VERSION" >> $GITHUB_OUTPUT
36
+ fi
37
+ - uses: astral-sh/setup-uv@v5
38
+ if: steps.version.outputs.changed == 'true'
39
+ - name: Build
40
+ if: steps.version.outputs.changed == 'true'
41
+ run: uv build
42
+ - name: Publish to PyPI
43
+ if: steps.version.outputs.changed == 'true'
44
+ uses: pypa/gh-action-pypi-publish@release/v1
45
+ - name: Tag release
46
+ if: steps.version.outputs.changed == 'true'
47
+ run: |
48
+ git config user.name "github-actions[bot]"
49
+ git config user.email "github-actions[bot]@users.noreply.github.com"
50
+ git tag "v${{ steps.version.outputs.version }}"
51
+ git push origin "v${{ steps.version.outputs.version }}"
@@ -0,0 +1,18 @@
1
+ name: Test
2
+
3
+ on:
4
+ workflow_call:
5
+
6
+ jobs:
7
+ test:
8
+ runs-on: ubuntu-latest
9
+ strategy:
10
+ matrix:
11
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+ - uses: astral-sh/setup-uv@v5
15
+ with:
16
+ enable-cache: true
17
+ - name: Run tests
18
+ run: uv run --python ${{ matrix.python-version }} pytest
@@ -0,0 +1,221 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ # Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ # poetry.lock
109
+ # poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ # pdm.lock
116
+ # pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ # pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # Redis
135
+ *.rdb
136
+ *.aof
137
+ *.pid
138
+
139
+ # RabbitMQ
140
+ mnesia/
141
+ rabbitmq/
142
+ rabbitmq-data/
143
+
144
+ # ActiveMQ
145
+ activemq-data/
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .envrc
153
+ .venv
154
+ env/
155
+ venv/
156
+ ENV/
157
+ env.bak/
158
+ venv.bak/
159
+
160
+ # Spyder project settings
161
+ .spyderproject
162
+ .spyproject
163
+
164
+ # Rope project settings
165
+ .ropeproject
166
+
167
+ # mkdocs documentation
168
+ /site
169
+
170
+ # mypy
171
+ .mypy_cache/
172
+ .dmypy.json
173
+ dmypy.json
174
+
175
+ # Pyre type checker
176
+ .pyre/
177
+
178
+ # pytype static type analyzer
179
+ .pytype/
180
+
181
+ # Cython debug symbols
182
+ cython_debug/
183
+
184
+ # PyCharm
185
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
186
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
187
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
188
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
189
+ # .idea/
190
+
191
+ # Abstra
192
+ # Abstra is an AI-powered process automation framework.
193
+ # Ignore directories containing user credentials, local state, and settings.
194
+ # Learn more at https://abstra.io/docs
195
+ .abstra/
196
+
197
+ # Visual Studio Code
198
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
201
+ # you could uncomment the following to ignore the entire vscode folder
202
+ # .vscode/
203
+ # Temporary file for partial code execution
204
+ tempCodeRunnerFile.py
205
+
206
+ # Ruff stuff:
207
+ .ruff_cache/
208
+
209
+ # PyPI configuration file
210
+ .pypirc
211
+
212
+ # Marimo
213
+ marimo/_static/
214
+ marimo/_lsp/
215
+ __marimo__/
216
+
217
+ # Streamlit
218
+ .streamlit/secrets.toml
219
+
220
+ /.idea/
221
+ /.qlty/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Eugene Kosyakov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,147 @@
1
+ Metadata-Version: 2.5
2
+ Name: mcp-kb-sqlite
3
+ Version: 0.1.0
4
+ Summary: Local long-term memory MCP server backed by SQLite FTS5
5
+ Project-URL: Homepage, https://github.com/eukos/mcp-kb-sqlite
6
+ Author-email: Eugene Kosyakov <eukos@yandex.by>
7
+ License: MIT
8
+ License-File: LICENSE
9
+ Keywords: ai,claude,fts5,knowledge-base,llm,mcp,memory,opencode,sqlite
10
+ Classifier: Development Status :: 4 - Beta
11
+ Requires-Python: >=3.10
12
+ Requires-Dist: mcp[cli]<3,>=2
13
+ Requires-Dist: pydantic>=2
14
+ Description-Content-Type: text/markdown
15
+
16
+ # mcp-kb-sqlite
17
+
18
+ Simple, fast persistent memory for your coding agent — cross-session, cross-project. One knowledge
19
+ base shared across every repo you work in, so what you learn in one project is there the next time
20
+ you open another. Entries can be linked to each other (`relate`), so related facts stay connected
21
+ instead of scattered.
22
+
23
+ Start a conversation with `/kb-use` to load relevant context before you begin. When you learn
24
+ something worth keeping — an architecture decision, a gotcha, a debugging finding — save it with
25
+ `/kb-update`.
26
+
27
+ Under the hood: a plain SQLite database with an FTS5 full-text index, and a small tool surface for
28
+ agents to search, save, and link entries. No server to run, no external service, no schema to
29
+ manage by hand.
30
+
31
+ ## Install — MCP server
32
+
33
+ The database lives at `~/.ai-memory/kb.db` unless you override it with `DB_PATH` (shown below,
34
+ optional). The schema is created and migrated automatically on first connection.
35
+
36
+ ### Claude Code
37
+
38
+ ```sh
39
+ claude mcp add kb -- uv run --directory /path/to/mcp-kb-sqlite mcp-kb-sqlite
40
+
41
+ # with a custom DB_PATH:
42
+ claude mcp add kb --env DB_PATH=/path/to/kb.db -- uv run --directory /path/to/mcp-kb-sqlite mcp-kb-sqlite
43
+ ```
44
+
45
+ Or add it directly to `.mcp.json` (project-local) or `~/.claude.json` (user-scoped, under the
46
+ top-level `mcpServers` key):
47
+
48
+ ```json
49
+ {
50
+ "mcpServers": {
51
+ "kb": {
52
+ "type": "stdio",
53
+ "command": "uv",
54
+ "args": ["run", "--directory", "/path/to/mcp-kb-sqlite", "mcp-kb-sqlite"],
55
+ "env": { "DB_PATH": "/path/to/kb.db" }
56
+ }
57
+ }
58
+ }
59
+ ```
60
+
61
+ `env` is optional — drop it to use the default `~/.ai-memory/kb.db`.
62
+
63
+ ### OpenCode
64
+
65
+ Add to `opencode.json` (global at `~/.config/opencode/opencode.json`, or project-local):
66
+
67
+ ```json
68
+ {
69
+ "mcp": {
70
+ "kb": {
71
+ "type": "local",
72
+ "command": [
73
+ "uv",
74
+ "run",
75
+ "--directory",
76
+ "/path/to/mcp-kb-sqlite",
77
+ "mcp-kb-sqlite"
78
+ ],
79
+ "enabled": true,
80
+ "environment": { "DB_PATH": "/path/to/kb.db" }
81
+ }
82
+ }
83
+ }
84
+ ```
85
+
86
+ `environment` is optional — drop it to use the default `~/.ai-memory/kb.db`.
87
+
88
+ ## Install — skills
89
+
90
+ The `/kb-use` and `/kb-update` workflows ship as skills for both Claude Code and OpenCode. The two
91
+ flavors are hand-kept in sync — there's no generator, so edit both when instructions change.
92
+
93
+ ### Claude Code
94
+
95
+ Copy or symlink `skills/claude/commands/*.md` into `~/.claude/commands/`.
96
+
97
+ ### OpenCode
98
+
99
+ Copy or symlink each `skills/opencode/<name>/` directory into `~/.config/opencode/skills/<name>/`
100
+ (or a project-local `.opencode/skills/`). OpenCode skills are auto-invoked by description match via
101
+ the agent's `skill` tool, but can also be triggered directly as `/<name>` in chat — this prepends
102
+ the SKILL.md text and appends whatever you typed after the command.
103
+
104
+ ## Tools
105
+
106
+ | Tool | Purpose |
107
+ | ---------------------------------------------------------- | ------------------------------------------------------------- |
108
+ | `save(id?, ns?, key?, title?, description?, tags?, data?)` | Create or update an entry — see below |
109
+ | `get(id, include_data=False)` | Fetch one entry; `include_data=True` returns the payload |
110
+ | `search(query, ns?, limit=10, offset=0)` | FTS5/BM25 over title + description + tags |
111
+ | `list(ns?, limit=20, offset=0)` | Entries by recency, metadata only |
112
+ | `list_namespaces()` | Namespaces with entry counts and last-updated date |
113
+ | `relate(from_id, to_id, rel?)` | Link two entries; omit `rel` to remove all links between them |
114
+ | `get_relations(id)` | Links in both directions |
115
+ | `delete(id)` | Remove an entry (cascades to its relations) |
116
+
117
+ Entries are addressed by `ns` (namespace, e.g. `project/subsystem`) plus `key` (a slug unique within the
118
+ namespace). `ns` filters are prefix matches, so `ns="project"` covers every subsystem under it.
119
+
120
+ Only `title`, `description`, and `tags` are FTS-indexed. `data` is the payload — put schemas, code, configs
121
+ and traces there, and make sure anything you need to _find_ also appears in one of the indexed fields.
122
+
123
+ ### `save`: create vs. update
124
+
125
+ Every parameter is optional; the presence of `id` picks the mode.
126
+
127
+ **Update** — pass `id` plus only the fields you want to change:
128
+
129
+ ```python
130
+ save(id=42, description="new search hints") # title, tags, data untouched
131
+ ```
132
+
133
+ Omitted fields keep their current value. Pass `""` (or `[]` for `tags`) to clear `description`, `tags`, or
134
+ `data`. `ns`, `key`, and `title` can be changed — that's how you rename or move an entry — but not cleared.
135
+
136
+ **Create** — omit `id`; `ns`, `key`, and `title` are all required:
137
+
138
+ ```python
139
+ save(ns="project/db", key="schema", title="Schema layout", tags=["db","schema"], data="…")
140
+ ```
141
+
142
+ There is no upsert. Creating over an existing `(ns, key)` is an error that reports the existing id, so an
143
+ accidental full overwrite isn't possible:
144
+
145
+ ```
146
+ Error: project/db/schema already exists (id=42) — pass id=42 to update it
147
+ ```
@@ -0,0 +1,132 @@
1
+ # mcp-kb-sqlite
2
+
3
+ Simple, fast persistent memory for your coding agent — cross-session, cross-project. One knowledge
4
+ base shared across every repo you work in, so what you learn in one project is there the next time
5
+ you open another. Entries can be linked to each other (`relate`), so related facts stay connected
6
+ instead of scattered.
7
+
8
+ Start a conversation with `/kb-use` to load relevant context before you begin. When you learn
9
+ something worth keeping — an architecture decision, a gotcha, a debugging finding — save it with
10
+ `/kb-update`.
11
+
12
+ Under the hood: a plain SQLite database with an FTS5 full-text index, and a small tool surface for
13
+ agents to search, save, and link entries. No server to run, no external service, no schema to
14
+ manage by hand.
15
+
16
+ ## Install — MCP server
17
+
18
+ The database lives at `~/.ai-memory/kb.db` unless you override it with `DB_PATH` (shown below,
19
+ optional). The schema is created and migrated automatically on first connection.
20
+
21
+ ### Claude Code
22
+
23
+ ```sh
24
+ claude mcp add kb -- uv run --directory /path/to/mcp-kb-sqlite mcp-kb-sqlite
25
+
26
+ # with a custom DB_PATH:
27
+ claude mcp add kb --env DB_PATH=/path/to/kb.db -- uv run --directory /path/to/mcp-kb-sqlite mcp-kb-sqlite
28
+ ```
29
+
30
+ Or add it directly to `.mcp.json` (project-local) or `~/.claude.json` (user-scoped, under the
31
+ top-level `mcpServers` key):
32
+
33
+ ```json
34
+ {
35
+ "mcpServers": {
36
+ "kb": {
37
+ "type": "stdio",
38
+ "command": "uv",
39
+ "args": ["run", "--directory", "/path/to/mcp-kb-sqlite", "mcp-kb-sqlite"],
40
+ "env": { "DB_PATH": "/path/to/kb.db" }
41
+ }
42
+ }
43
+ }
44
+ ```
45
+
46
+ `env` is optional — drop it to use the default `~/.ai-memory/kb.db`.
47
+
48
+ ### OpenCode
49
+
50
+ Add to `opencode.json` (global at `~/.config/opencode/opencode.json`, or project-local):
51
+
52
+ ```json
53
+ {
54
+ "mcp": {
55
+ "kb": {
56
+ "type": "local",
57
+ "command": [
58
+ "uv",
59
+ "run",
60
+ "--directory",
61
+ "/path/to/mcp-kb-sqlite",
62
+ "mcp-kb-sqlite"
63
+ ],
64
+ "enabled": true,
65
+ "environment": { "DB_PATH": "/path/to/kb.db" }
66
+ }
67
+ }
68
+ }
69
+ ```
70
+
71
+ `environment` is optional — drop it to use the default `~/.ai-memory/kb.db`.
72
+
73
+ ## Install — skills
74
+
75
+ The `/kb-use` and `/kb-update` workflows ship as skills for both Claude Code and OpenCode. The two
76
+ flavors are hand-kept in sync — there's no generator, so edit both when instructions change.
77
+
78
+ ### Claude Code
79
+
80
+ Copy or symlink `skills/claude/commands/*.md` into `~/.claude/commands/`.
81
+
82
+ ### OpenCode
83
+
84
+ Copy or symlink each `skills/opencode/<name>/` directory into `~/.config/opencode/skills/<name>/`
85
+ (or a project-local `.opencode/skills/`). OpenCode skills are auto-invoked by description match via
86
+ the agent's `skill` tool, but can also be triggered directly as `/<name>` in chat — this prepends
87
+ the SKILL.md text and appends whatever you typed after the command.
88
+
89
+ ## Tools
90
+
91
+ | Tool | Purpose |
92
+ | ---------------------------------------------------------- | ------------------------------------------------------------- |
93
+ | `save(id?, ns?, key?, title?, description?, tags?, data?)` | Create or update an entry — see below |
94
+ | `get(id, include_data=False)` | Fetch one entry; `include_data=True` returns the payload |
95
+ | `search(query, ns?, limit=10, offset=0)` | FTS5/BM25 over title + description + tags |
96
+ | `list(ns?, limit=20, offset=0)` | Entries by recency, metadata only |
97
+ | `list_namespaces()` | Namespaces with entry counts and last-updated date |
98
+ | `relate(from_id, to_id, rel?)` | Link two entries; omit `rel` to remove all links between them |
99
+ | `get_relations(id)` | Links in both directions |
100
+ | `delete(id)` | Remove an entry (cascades to its relations) |
101
+
102
+ Entries are addressed by `ns` (namespace, e.g. `project/subsystem`) plus `key` (a slug unique within the
103
+ namespace). `ns` filters are prefix matches, so `ns="project"` covers every subsystem under it.
104
+
105
+ Only `title`, `description`, and `tags` are FTS-indexed. `data` is the payload — put schemas, code, configs
106
+ and traces there, and make sure anything you need to _find_ also appears in one of the indexed fields.
107
+
108
+ ### `save`: create vs. update
109
+
110
+ Every parameter is optional; the presence of `id` picks the mode.
111
+
112
+ **Update** — pass `id` plus only the fields you want to change:
113
+
114
+ ```python
115
+ save(id=42, description="new search hints") # title, tags, data untouched
116
+ ```
117
+
118
+ Omitted fields keep their current value. Pass `""` (or `[]` for `tags`) to clear `description`, `tags`, or
119
+ `data`. `ns`, `key`, and `title` can be changed — that's how you rename or move an entry — but not cleared.
120
+
121
+ **Create** — omit `id`; `ns`, `key`, and `title` are all required:
122
+
123
+ ```python
124
+ save(ns="project/db", key="schema", title="Schema layout", tags=["db","schema"], data="…")
125
+ ```
126
+
127
+ There is no upsert. Creating over an existing `(ns, key)` is an error that reports the existing id, so an
128
+ accidental full overwrite isn't possible:
129
+
130
+ ```
131
+ Error: project/db/schema already exists (id=42) — pass id=42 to update it
132
+ ```
File without changes
@@ -0,0 +1,38 @@
1
+ import os
2
+ import sqlite3
3
+ from contextlib import contextmanager
4
+ from pathlib import Path
5
+
6
+ from mcp_kb_sqlite.db.migrations import run_migrations
7
+
8
+ _db_path: Path | None = None
9
+
10
+
11
+ def get_db_path() -> Path:
12
+ global _db_path
13
+ if _db_path is None:
14
+ raw = os.environ.get("DB_PATH")
15
+ if raw:
16
+ _db_path = Path(raw)
17
+ else:
18
+ _db_path = Path.home() / ".ai-memory" / "kb.db"
19
+ os.makedirs(_db_path.parent, exist_ok=True)
20
+ return _db_path
21
+
22
+
23
+ @contextmanager
24
+ def get_conn():
25
+ conn = sqlite3.connect(get_db_path(), timeout=30)
26
+ conn.row_factory = sqlite3.Row
27
+ conn.execute("PRAGMA journal_mode = WAL")
28
+ conn.execute("PRAGMA foreign_keys = ON")
29
+ try:
30
+ yield conn
31
+ conn.commit()
32
+ finally:
33
+ conn.close()
34
+
35
+
36
+ def init_db() -> None:
37
+ with get_conn() as conn:
38
+ run_migrations(conn)