nucladb 0.3.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.
- nucladb-0.3.0/PKG-INFO +261 -0
- nucladb-0.3.0/README.md +245 -0
- nucladb-0.3.0/nucladb/__init__.py +21 -0
- nucladb-0.3.0/nucladb/cli.py +175 -0
- nucladb-0.3.0/nucladb/client.py +341 -0
- nucladb-0.3.0/nucladb/errors.py +91 -0
- nucladb-0.3.0/nucladb/mcp_server.py +229 -0
- nucladb-0.3.0/nucladb/nucladb_pb2.py +125 -0
- nucladb-0.3.0/nucladb/nucladb_pb2.pyi +347 -0
- nucladb-0.3.0/nucladb/nucladb_pb2_grpc.py +740 -0
- nucladb-0.3.0/nucladb/rest.py +204 -0
- nucladb-0.3.0/nucladb.egg-info/PKG-INFO +261 -0
- nucladb-0.3.0/nucladb.egg-info/SOURCES.txt +17 -0
- nucladb-0.3.0/nucladb.egg-info/dependency_links.txt +1 -0
- nucladb-0.3.0/nucladb.egg-info/entry_points.txt +3 -0
- nucladb-0.3.0/nucladb.egg-info/requires.txt +7 -0
- nucladb-0.3.0/nucladb.egg-info/top_level.txt +1 -0
- nucladb-0.3.0/pyproject.toml +30 -0
- nucladb-0.3.0/setup.cfg +4 -0
nucladb-0.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nucladb
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Python client for NuclaDB, a vector search engine
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/Rakshit-gen/NuclaDB
|
|
7
|
+
Project-URL: Documentation, https://github.com/Rakshit-gen/NuclaDB/tree/main/clients/python
|
|
8
|
+
Requires-Python: >=3.9
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: grpcio>=1.84
|
|
11
|
+
Requires-Dist: grpcio-health-checking>=1.60
|
|
12
|
+
Requires-Dist: protobuf>=7.35.1
|
|
13
|
+
Provides-Extra: mcp
|
|
14
|
+
Requires-Dist: mcp<3,>=2.3; extra == "mcp"
|
|
15
|
+
Requires-Dist: sentence-transformers>=3; extra == "mcp"
|
|
16
|
+
|
|
17
|
+
# nucladb (Python client)
|
|
18
|
+
|
|
19
|
+
A thin Python client for [NuclaDB](../../README.md), generated from the
|
|
20
|
+
same `proto/nucladb.proto` gRPC contract the Go server implements,
|
|
21
|
+
wrapped in a small pythonic layer (a context manager, plain dicts/lists
|
|
22
|
+
instead of protobuf messages) plus a `nucladb` command-line tool with
|
|
23
|
+
the same commands as the Go `nucladb-cli` (see `../../docs/cli.md`).
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
pip install ./clients/python
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
That's the one command: it puts both the `nucladb` library and the
|
|
32
|
+
`nucladb` CLI on your `PATH` (via the `[project.scripts]` entry point in
|
|
33
|
+
`pyproject.toml`), no separate build step. (Named `nucladb`, not
|
|
34
|
+
`nucladb-cli`, to avoid colliding with the Go binary of that name if
|
|
35
|
+
both are ever installed on the same machine.)
|
|
36
|
+
|
|
37
|
+
## CLI
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
$ export NUCLADB_ADDR=localhost:9090 # or pass --addr
|
|
41
|
+
$ nucladb ping
|
|
42
|
+
ok
|
|
43
|
+
$ nucladb insert --id 1 --vector 1,0,0,0 --meta team=search
|
|
44
|
+
inserted id=1
|
|
45
|
+
$ nucladb search --vector 1,0,0,0 --top-k 3
|
|
46
|
+
1 score=0.000000 {'team': 'search'}
|
|
47
|
+
$ nucladb count
|
|
48
|
+
1
|
|
49
|
+
$ nucladb get --id 1
|
|
50
|
+
1 1,0,0,0 {'team': 'search'}
|
|
51
|
+
$ nucladb delete --id 1
|
|
52
|
+
deleted=true
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
`list` prints every stored id, one per line.
|
|
56
|
+
|
|
57
|
+
Run `nucladb <command> --help` for a command's full flag list. Every
|
|
58
|
+
data command also accepts `--tenant` at the top level, same semantics
|
|
59
|
+
as the Go CLI: omit it for the reserved `default` tenant. For a server
|
|
60
|
+
run with `-api-keys`, set `NUCLADB_API_KEY`; set `NUCLADB_TLS=1` if it
|
|
61
|
+
serves TLS.
|
|
62
|
+
|
|
63
|
+
## Library
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
from nucladb import Client, DistanceMetric
|
|
67
|
+
|
|
68
|
+
with Client("localhost:9090", tenant_id="docs") as db:
|
|
69
|
+
db.insert("1", [1.0, 0.0, 0.0, 0.0], metadata={"team": "search", "year": "2025"})
|
|
70
|
+
for match in db.search([1.0, 0.0, 0.0, 0.0], top_k=3):
|
|
71
|
+
print(match.id, match.score, match.metadata)
|
|
72
|
+
|
|
73
|
+
# where clauses: (key, op) or (key, op, value); all must match
|
|
74
|
+
db.search([1.0, 0.0, 0.0, 0.0], where=[("year", "gte", 2024), ("team", "in", ["search", "ads"])])
|
|
75
|
+
|
|
76
|
+
db.get("1") # (vector, metadata), or None
|
|
77
|
+
db.update_metadata("1", {"team": "infra"}) # replaces all metadata
|
|
78
|
+
ids = list(db.list()) # pages through every id
|
|
79
|
+
db.count()
|
|
80
|
+
|
|
81
|
+
# tenant admin (needs an admin key when the server uses -api-keys)
|
|
82
|
+
db.create_tenant("acme", dim=384, metric=DistanceMetric.COSINE, max_vectors=1_000_000)
|
|
83
|
+
db.set_quota("acme", max_qps=50)
|
|
84
|
+
db.list_tenants()
|
|
85
|
+
db.delete_tenant("acme")
|
|
86
|
+
|
|
87
|
+
# API keys (needs an admin key; the server must require keys)
|
|
88
|
+
key, info = db.create_api_key(["acme"], name="ci", ttl_seconds=30 * 86400)
|
|
89
|
+
db.list_api_keys() # ids and details, never the keys
|
|
90
|
+
db.revoke_api_key(info.id)
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`create_api_key` is the only time a key is returned; the server stores a
|
|
94
|
+
hash of it.
|
|
95
|
+
|
|
96
|
+
`Client(address, tenant_id="", api_key="", tls=False, timeout=30)`:
|
|
97
|
+
`tenant_id` scopes every data call, `api_key` is sent as a bearer token,
|
|
98
|
+
`tls=True` connects with the system's root certificates, and `timeout` is
|
|
99
|
+
each call's deadline in seconds (`None` waits forever). A key sent without
|
|
100
|
+
TLS to anything but localhost logs a warning.
|
|
101
|
+
|
|
102
|
+
### Errors
|
|
103
|
+
|
|
104
|
+
`Client` and `RestClient` raise the same exceptions, all subclasses of
|
|
105
|
+
`nucladb.NucladbError`, which carries `.code` (the gRPC status name, such
|
|
106
|
+
as `"NOT_FOUND"`), `.message`, and `.status` (the HTTP status, for
|
|
107
|
+
`RestClient`):
|
|
108
|
+
|
|
109
|
+
| Exception | When | Retry? |
|
|
110
|
+
|---|---|---|
|
|
111
|
+
| `InvalidArgument` | wrong vector length, bad filter, bad id | no |
|
|
112
|
+
| `NotFound` | tenant missing (a missing vector makes `get` return `None`) | no |
|
|
113
|
+
| `AlreadyExists` | `create_tenant` with a taken id | no |
|
|
114
|
+
| `Unauthenticated` | no key, or an unknown or expired one | no |
|
|
115
|
+
| `PermissionDenied` | the key doesn't cover the tenant or call | no |
|
|
116
|
+
| `QuotaExceeded` | the tenant is at `max_vectors` | no; delete or raise the quota |
|
|
117
|
+
| `RateLimited` | the tenant is over `max_qps` | yes, after a second |
|
|
118
|
+
| `Unavailable` | server or shard unreachable; a write may or may not have landed | yes, upserts are idempotent |
|
|
119
|
+
|
|
120
|
+
```python
|
|
121
|
+
from nucladb import QuotaExceeded
|
|
122
|
+
|
|
123
|
+
try:
|
|
124
|
+
db.insert("2", vector)
|
|
125
|
+
except QuotaExceeded:
|
|
126
|
+
...
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`RestClient` retries `RateLimited` itself, waiting for the server's
|
|
130
|
+
`Retry-After`. `RestError` is kept as another name for `NucladbError`.
|
|
131
|
+
|
|
132
|
+
## MCP server
|
|
133
|
+
|
|
134
|
+
`nucladb-mcp` lets an agent (Claude Code, Claude Desktop, anything that
|
|
135
|
+
speaks MCP) store and search text in a NuclaDB tenant. NuclaDB only takes
|
|
136
|
+
vectors, so the server turns text into vectors itself with a small local
|
|
137
|
+
model, `all-MiniLM-L6-v2` (384 numbers per text, runs on a laptop CPU, no
|
|
138
|
+
API key). The text is kept in each vector's metadata so results come back
|
|
139
|
+
readable.
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
pip install './clients/python[mcp]'
|
|
143
|
+
./bin/nucladbd -data-dir=./data &
|
|
144
|
+
NUCLADB_TENANT=memory nucladb-mcp
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
The first start downloads the model (about 90 MB, roughly a minute);
|
|
148
|
+
after that it is ready in about 7 seconds. If the tenant doesn't exist,
|
|
149
|
+
it is created with 384 dimensions and the cosine metric. If it exists
|
|
150
|
+
with a different size, the server refuses to start and says so, rather
|
|
151
|
+
than failing on the first write.
|
|
152
|
+
|
|
153
|
+
### Claude Code
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
claude mcp add nucladb -e NUCLADB_ADDR=localhost:9090 -e NUCLADB_TENANT=memory -- nucladb-mcp
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### A hosted server
|
|
160
|
+
|
|
161
|
+
A hosted server, like the one behind the NuclaDB console, only exposes
|
|
162
|
+
REST, so point `NUCLADB_URL` at it instead of `NUCLADB_ADDR`. With a
|
|
163
|
+
console key, set up the index with 384 dimensions (the model's size) and
|
|
164
|
+
use the `tenant_id` the console shows:
|
|
165
|
+
|
|
166
|
+
```
|
|
167
|
+
claude mcp add nucladb \
|
|
168
|
+
-e NUCLADB_URL=https://nucladb-demo.onrender.com \
|
|
169
|
+
-e NUCLADB_TENANT=u-... -e NUCLADB_API_KEY=nk_... -- nucladb-mcp
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Over REST, `add_texts` sends 10 texts per request and waits out HTTP 429
|
|
173
|
+
answers, so it works within a tenant's rate limit: 256 texts take about
|
|
174
|
+
26 seconds at 10 per second.
|
|
175
|
+
|
|
176
|
+
### Claude Desktop
|
|
177
|
+
|
|
178
|
+
Add this to `claude_desktop_config.json`. Desktop apps often don't see
|
|
179
|
+
the shell's `PATH`, so give the full path from `which nucladb-mcp`:
|
|
180
|
+
|
|
181
|
+
```json
|
|
182
|
+
{
|
|
183
|
+
"mcpServers": {
|
|
184
|
+
"nucladb": {
|
|
185
|
+
"command": "/full/path/to/nucladb-mcp",
|
|
186
|
+
"env": { "NUCLADB_ADDR": "localhost:9090", "NUCLADB_TENANT": "memory" }
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Tools
|
|
193
|
+
|
|
194
|
+
| Tool | Does | Notes |
|
|
195
|
+
|---|---|---|
|
|
196
|
+
| `add_texts(texts, metadata?)` | Stores up to 256 texts | The id is a hash of the text, so adding the same text again updates it instead of storing a copy. `metadata` goes on every text in the call; the key `text` is reserved |
|
|
197
|
+
| `search(query, top_k=5, filters?)` | Closest texts to `query` | `top_k` is 1 to 50. `filters` is key=value pairs that all have to match. `score` is a distance, so lower means closer |
|
|
198
|
+
| `get(id)` | One text and its metadata | `null` if the id isn't stored |
|
|
199
|
+
| `delete(id)` | Forgets one text | Deleting an unknown id is not an error |
|
|
200
|
+
| `count()` | How many texts are stored | |
|
|
201
|
+
|
|
202
|
+
`search`, `get` and `count` are marked read-only, and `delete` is marked
|
|
203
|
+
destructive, so clients that read those hints can skip the approval
|
|
204
|
+
prompt for lookups. When the server rejects a call, the agent sees the
|
|
205
|
+
reason (`nucladb: INVALID_ARGUMENT: ...` over gRPC, `nucladb: HTTP 400: ...`
|
|
206
|
+
over REST), not just "Error executing tool". A text over 12 KiB is refused
|
|
207
|
+
with a message to split it, since the server caps a vector's metadata at
|
|
208
|
+
16 KiB.
|
|
209
|
+
|
|
210
|
+
### Settings
|
|
211
|
+
|
|
212
|
+
All environment variables. `NUCLADB_ADDR`, `NUCLADB_TENANT`,
|
|
213
|
+
`NUCLADB_API_KEY`, `NUCLADB_TLS` and `NUCLADB_CA` are shared with the `nucladb` CLI:
|
|
214
|
+
|
|
215
|
+
| Variable | Default | |
|
|
216
|
+
|---|---|---|
|
|
217
|
+
| `NUCLADB_ADDR` | `localhost:9090` | gRPC address |
|
|
218
|
+
| `NUCLADB_URL` | none | REST base URL, e.g. `https://nucladb-demo.onrender.com`. When set, it is used instead of `NUCLADB_ADDR` |
|
|
219
|
+
| `NUCLADB_TENANT` | the `default` tenant | Created on first start if missing (needs an admin key) |
|
|
220
|
+
| `NUCLADB_API_KEY` | none | With a tenant-scoped key, the startup size check runs one search instead of listing tenants |
|
|
221
|
+
| `NUCLADB_TLS` | off | `1` to connect over TLS (gRPC only; for REST use an `https://` URL) |
|
|
222
|
+
| `NUCLADB_CA` | none | CA certificate file to trust instead of the system's, such as a cluster's `ca.pem`; turns TLS on (gRPC only) |
|
|
223
|
+
| `NUCLADB_MCP_MODEL` | `all-MiniLM-L6-v2` | Any sentence-transformers model. A different size needs a tenant made for it |
|
|
224
|
+
|
|
225
|
+
The `default` tenant takes the server's `-dim`, which is 128 unless you
|
|
226
|
+
set it, so it won't fit the model. Set `NUCLADB_TENANT` to a new name
|
|
227
|
+
instead.
|
|
228
|
+
|
|
229
|
+
## Regenerating the protobuf stubs
|
|
230
|
+
|
|
231
|
+
`nucladb/nucladb_pb2.py` and `nucladb/nucladb_pb2_grpc.py` are generated,
|
|
232
|
+
not hand-written; regenerate them after changing `proto/nucladb.proto`:
|
|
233
|
+
|
|
234
|
+
```
|
|
235
|
+
python -m grpc_tools.protoc -I ../../proto \
|
|
236
|
+
--python_out=nucladb --grpc_python_out=nucladb --pyi_out=nucladb \
|
|
237
|
+
../../proto/nucladb.proto
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
`nucladb_pb2_grpc.py`'s generated `import nucladb_pb2` needs to stay a
|
|
241
|
+
relative import (`from . import nucladb_pb2`) for the package to import
|
|
242
|
+
correctly: the generator doesn't know it's producing a package, so fix
|
|
243
|
+
that line back up after regenerating.
|
|
244
|
+
|
|
245
|
+
## Tests
|
|
246
|
+
|
|
247
|
+
`tests/smoke_test.py` is a real end-to-end check, not a mock: it builds
|
|
248
|
+
the actual `nucladbd` Go binary, runs it as a subprocess, and drives it
|
|
249
|
+
through insert/batch_upsert/search/filter/delete via this package's own
|
|
250
|
+
`Client`, the same pattern `test/chaos/kill_test.go` uses on the Go
|
|
251
|
+
side. Run from a checkout with Go available:
|
|
252
|
+
|
|
253
|
+
```
|
|
254
|
+
python tests/smoke_test.py
|
|
255
|
+
python tests/mcp_test.py
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
`tests/mcp_test.py` does the same for `nucladb-mcp`: every tool, the
|
|
259
|
+
refusals, a server-side error reaching the agent, and the startup tenant
|
|
260
|
+
check, over gRPC and then over REST with a scoped key and a 10/s tenant. It swaps the real model for a stand-in that counts four words, so
|
|
261
|
+
it needs the `mcp` package but not torch or a model download.
|
nucladb-0.3.0/README.md
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# nucladb (Python client)
|
|
2
|
+
|
|
3
|
+
A thin Python client for [NuclaDB](../../README.md), generated from the
|
|
4
|
+
same `proto/nucladb.proto` gRPC contract the Go server implements,
|
|
5
|
+
wrapped in a small pythonic layer (a context manager, plain dicts/lists
|
|
6
|
+
instead of protobuf messages) plus a `nucladb` command-line tool with
|
|
7
|
+
the same commands as the Go `nucladb-cli` (see `../../docs/cli.md`).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
pip install ./clients/python
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
That's the one command: it puts both the `nucladb` library and the
|
|
16
|
+
`nucladb` CLI on your `PATH` (via the `[project.scripts]` entry point in
|
|
17
|
+
`pyproject.toml`), no separate build step. (Named `nucladb`, not
|
|
18
|
+
`nucladb-cli`, to avoid colliding with the Go binary of that name if
|
|
19
|
+
both are ever installed on the same machine.)
|
|
20
|
+
|
|
21
|
+
## CLI
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
$ export NUCLADB_ADDR=localhost:9090 # or pass --addr
|
|
25
|
+
$ nucladb ping
|
|
26
|
+
ok
|
|
27
|
+
$ nucladb insert --id 1 --vector 1,0,0,0 --meta team=search
|
|
28
|
+
inserted id=1
|
|
29
|
+
$ nucladb search --vector 1,0,0,0 --top-k 3
|
|
30
|
+
1 score=0.000000 {'team': 'search'}
|
|
31
|
+
$ nucladb count
|
|
32
|
+
1
|
|
33
|
+
$ nucladb get --id 1
|
|
34
|
+
1 1,0,0,0 {'team': 'search'}
|
|
35
|
+
$ nucladb delete --id 1
|
|
36
|
+
deleted=true
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
`list` prints every stored id, one per line.
|
|
40
|
+
|
|
41
|
+
Run `nucladb <command> --help` for a command's full flag list. Every
|
|
42
|
+
data command also accepts `--tenant` at the top level, same semantics
|
|
43
|
+
as the Go CLI: omit it for the reserved `default` tenant. For a server
|
|
44
|
+
run with `-api-keys`, set `NUCLADB_API_KEY`; set `NUCLADB_TLS=1` if it
|
|
45
|
+
serves TLS.
|
|
46
|
+
|
|
47
|
+
## Library
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from nucladb import Client, DistanceMetric
|
|
51
|
+
|
|
52
|
+
with Client("localhost:9090", tenant_id="docs") as db:
|
|
53
|
+
db.insert("1", [1.0, 0.0, 0.0, 0.0], metadata={"team": "search", "year": "2025"})
|
|
54
|
+
for match in db.search([1.0, 0.0, 0.0, 0.0], top_k=3):
|
|
55
|
+
print(match.id, match.score, match.metadata)
|
|
56
|
+
|
|
57
|
+
# where clauses: (key, op) or (key, op, value); all must match
|
|
58
|
+
db.search([1.0, 0.0, 0.0, 0.0], where=[("year", "gte", 2024), ("team", "in", ["search", "ads"])])
|
|
59
|
+
|
|
60
|
+
db.get("1") # (vector, metadata), or None
|
|
61
|
+
db.update_metadata("1", {"team": "infra"}) # replaces all metadata
|
|
62
|
+
ids = list(db.list()) # pages through every id
|
|
63
|
+
db.count()
|
|
64
|
+
|
|
65
|
+
# tenant admin (needs an admin key when the server uses -api-keys)
|
|
66
|
+
db.create_tenant("acme", dim=384, metric=DistanceMetric.COSINE, max_vectors=1_000_000)
|
|
67
|
+
db.set_quota("acme", max_qps=50)
|
|
68
|
+
db.list_tenants()
|
|
69
|
+
db.delete_tenant("acme")
|
|
70
|
+
|
|
71
|
+
# API keys (needs an admin key; the server must require keys)
|
|
72
|
+
key, info = db.create_api_key(["acme"], name="ci", ttl_seconds=30 * 86400)
|
|
73
|
+
db.list_api_keys() # ids and details, never the keys
|
|
74
|
+
db.revoke_api_key(info.id)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`create_api_key` is the only time a key is returned; the server stores a
|
|
78
|
+
hash of it.
|
|
79
|
+
|
|
80
|
+
`Client(address, tenant_id="", api_key="", tls=False, timeout=30)`:
|
|
81
|
+
`tenant_id` scopes every data call, `api_key` is sent as a bearer token,
|
|
82
|
+
`tls=True` connects with the system's root certificates, and `timeout` is
|
|
83
|
+
each call's deadline in seconds (`None` waits forever). A key sent without
|
|
84
|
+
TLS to anything but localhost logs a warning.
|
|
85
|
+
|
|
86
|
+
### Errors
|
|
87
|
+
|
|
88
|
+
`Client` and `RestClient` raise the same exceptions, all subclasses of
|
|
89
|
+
`nucladb.NucladbError`, which carries `.code` (the gRPC status name, such
|
|
90
|
+
as `"NOT_FOUND"`), `.message`, and `.status` (the HTTP status, for
|
|
91
|
+
`RestClient`):
|
|
92
|
+
|
|
93
|
+
| Exception | When | Retry? |
|
|
94
|
+
|---|---|---|
|
|
95
|
+
| `InvalidArgument` | wrong vector length, bad filter, bad id | no |
|
|
96
|
+
| `NotFound` | tenant missing (a missing vector makes `get` return `None`) | no |
|
|
97
|
+
| `AlreadyExists` | `create_tenant` with a taken id | no |
|
|
98
|
+
| `Unauthenticated` | no key, or an unknown or expired one | no |
|
|
99
|
+
| `PermissionDenied` | the key doesn't cover the tenant or call | no |
|
|
100
|
+
| `QuotaExceeded` | the tenant is at `max_vectors` | no; delete or raise the quota |
|
|
101
|
+
| `RateLimited` | the tenant is over `max_qps` | yes, after a second |
|
|
102
|
+
| `Unavailable` | server or shard unreachable; a write may or may not have landed | yes, upserts are idempotent |
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
from nucladb import QuotaExceeded
|
|
106
|
+
|
|
107
|
+
try:
|
|
108
|
+
db.insert("2", vector)
|
|
109
|
+
except QuotaExceeded:
|
|
110
|
+
...
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
`RestClient` retries `RateLimited` itself, waiting for the server's
|
|
114
|
+
`Retry-After`. `RestError` is kept as another name for `NucladbError`.
|
|
115
|
+
|
|
116
|
+
## MCP server
|
|
117
|
+
|
|
118
|
+
`nucladb-mcp` lets an agent (Claude Code, Claude Desktop, anything that
|
|
119
|
+
speaks MCP) store and search text in a NuclaDB tenant. NuclaDB only takes
|
|
120
|
+
vectors, so the server turns text into vectors itself with a small local
|
|
121
|
+
model, `all-MiniLM-L6-v2` (384 numbers per text, runs on a laptop CPU, no
|
|
122
|
+
API key). The text is kept in each vector's metadata so results come back
|
|
123
|
+
readable.
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
pip install './clients/python[mcp]'
|
|
127
|
+
./bin/nucladbd -data-dir=./data &
|
|
128
|
+
NUCLADB_TENANT=memory nucladb-mcp
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The first start downloads the model (about 90 MB, roughly a minute);
|
|
132
|
+
after that it is ready in about 7 seconds. If the tenant doesn't exist,
|
|
133
|
+
it is created with 384 dimensions and the cosine metric. If it exists
|
|
134
|
+
with a different size, the server refuses to start and says so, rather
|
|
135
|
+
than failing on the first write.
|
|
136
|
+
|
|
137
|
+
### Claude Code
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
claude mcp add nucladb -e NUCLADB_ADDR=localhost:9090 -e NUCLADB_TENANT=memory -- nucladb-mcp
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### A hosted server
|
|
144
|
+
|
|
145
|
+
A hosted server, like the one behind the NuclaDB console, only exposes
|
|
146
|
+
REST, so point `NUCLADB_URL` at it instead of `NUCLADB_ADDR`. With a
|
|
147
|
+
console key, set up the index with 384 dimensions (the model's size) and
|
|
148
|
+
use the `tenant_id` the console shows:
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
claude mcp add nucladb \
|
|
152
|
+
-e NUCLADB_URL=https://nucladb-demo.onrender.com \
|
|
153
|
+
-e NUCLADB_TENANT=u-... -e NUCLADB_API_KEY=nk_... -- nucladb-mcp
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Over REST, `add_texts` sends 10 texts per request and waits out HTTP 429
|
|
157
|
+
answers, so it works within a tenant's rate limit: 256 texts take about
|
|
158
|
+
26 seconds at 10 per second.
|
|
159
|
+
|
|
160
|
+
### Claude Desktop
|
|
161
|
+
|
|
162
|
+
Add this to `claude_desktop_config.json`. Desktop apps often don't see
|
|
163
|
+
the shell's `PATH`, so give the full path from `which nucladb-mcp`:
|
|
164
|
+
|
|
165
|
+
```json
|
|
166
|
+
{
|
|
167
|
+
"mcpServers": {
|
|
168
|
+
"nucladb": {
|
|
169
|
+
"command": "/full/path/to/nucladb-mcp",
|
|
170
|
+
"env": { "NUCLADB_ADDR": "localhost:9090", "NUCLADB_TENANT": "memory" }
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Tools
|
|
177
|
+
|
|
178
|
+
| Tool | Does | Notes |
|
|
179
|
+
|---|---|---|
|
|
180
|
+
| `add_texts(texts, metadata?)` | Stores up to 256 texts | The id is a hash of the text, so adding the same text again updates it instead of storing a copy. `metadata` goes on every text in the call; the key `text` is reserved |
|
|
181
|
+
| `search(query, top_k=5, filters?)` | Closest texts to `query` | `top_k` is 1 to 50. `filters` is key=value pairs that all have to match. `score` is a distance, so lower means closer |
|
|
182
|
+
| `get(id)` | One text and its metadata | `null` if the id isn't stored |
|
|
183
|
+
| `delete(id)` | Forgets one text | Deleting an unknown id is not an error |
|
|
184
|
+
| `count()` | How many texts are stored | |
|
|
185
|
+
|
|
186
|
+
`search`, `get` and `count` are marked read-only, and `delete` is marked
|
|
187
|
+
destructive, so clients that read those hints can skip the approval
|
|
188
|
+
prompt for lookups. When the server rejects a call, the agent sees the
|
|
189
|
+
reason (`nucladb: INVALID_ARGUMENT: ...` over gRPC, `nucladb: HTTP 400: ...`
|
|
190
|
+
over REST), not just "Error executing tool". A text over 12 KiB is refused
|
|
191
|
+
with a message to split it, since the server caps a vector's metadata at
|
|
192
|
+
16 KiB.
|
|
193
|
+
|
|
194
|
+
### Settings
|
|
195
|
+
|
|
196
|
+
All environment variables. `NUCLADB_ADDR`, `NUCLADB_TENANT`,
|
|
197
|
+
`NUCLADB_API_KEY`, `NUCLADB_TLS` and `NUCLADB_CA` are shared with the `nucladb` CLI:
|
|
198
|
+
|
|
199
|
+
| Variable | Default | |
|
|
200
|
+
|---|---|---|
|
|
201
|
+
| `NUCLADB_ADDR` | `localhost:9090` | gRPC address |
|
|
202
|
+
| `NUCLADB_URL` | none | REST base URL, e.g. `https://nucladb-demo.onrender.com`. When set, it is used instead of `NUCLADB_ADDR` |
|
|
203
|
+
| `NUCLADB_TENANT` | the `default` tenant | Created on first start if missing (needs an admin key) |
|
|
204
|
+
| `NUCLADB_API_KEY` | none | With a tenant-scoped key, the startup size check runs one search instead of listing tenants |
|
|
205
|
+
| `NUCLADB_TLS` | off | `1` to connect over TLS (gRPC only; for REST use an `https://` URL) |
|
|
206
|
+
| `NUCLADB_CA` | none | CA certificate file to trust instead of the system's, such as a cluster's `ca.pem`; turns TLS on (gRPC only) |
|
|
207
|
+
| `NUCLADB_MCP_MODEL` | `all-MiniLM-L6-v2` | Any sentence-transformers model. A different size needs a tenant made for it |
|
|
208
|
+
|
|
209
|
+
The `default` tenant takes the server's `-dim`, which is 128 unless you
|
|
210
|
+
set it, so it won't fit the model. Set `NUCLADB_TENANT` to a new name
|
|
211
|
+
instead.
|
|
212
|
+
|
|
213
|
+
## Regenerating the protobuf stubs
|
|
214
|
+
|
|
215
|
+
`nucladb/nucladb_pb2.py` and `nucladb/nucladb_pb2_grpc.py` are generated,
|
|
216
|
+
not hand-written; regenerate them after changing `proto/nucladb.proto`:
|
|
217
|
+
|
|
218
|
+
```
|
|
219
|
+
python -m grpc_tools.protoc -I ../../proto \
|
|
220
|
+
--python_out=nucladb --grpc_python_out=nucladb --pyi_out=nucladb \
|
|
221
|
+
../../proto/nucladb.proto
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
`nucladb_pb2_grpc.py`'s generated `import nucladb_pb2` needs to stay a
|
|
225
|
+
relative import (`from . import nucladb_pb2`) for the package to import
|
|
226
|
+
correctly: the generator doesn't know it's producing a package, so fix
|
|
227
|
+
that line back up after regenerating.
|
|
228
|
+
|
|
229
|
+
## Tests
|
|
230
|
+
|
|
231
|
+
`tests/smoke_test.py` is a real end-to-end check, not a mock: it builds
|
|
232
|
+
the actual `nucladbd` Go binary, runs it as a subprocess, and drives it
|
|
233
|
+
through insert/batch_upsert/search/filter/delete via this package's own
|
|
234
|
+
`Client`, the same pattern `test/chaos/kill_test.go` uses on the Go
|
|
235
|
+
side. Run from a checkout with Go available:
|
|
236
|
+
|
|
237
|
+
```
|
|
238
|
+
python tests/smoke_test.py
|
|
239
|
+
python tests/mcp_test.py
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
`tests/mcp_test.py` does the same for `nucladb-mcp`: every tool, the
|
|
243
|
+
refusals, a server-side error reaching the agent, and the startup tenant
|
|
244
|
+
check, over gRPC and then over REST with a scoped key and a 10/s tenant. It swaps the real model for a stand-in that counts four words, so
|
|
245
|
+
it needs the `mcp` package but not torch or a model download.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from .client import APIKey, Client, DistanceMetric, ScoredVector, Tenant
|
|
2
|
+
from .errors import (
|
|
3
|
+
AlreadyExists,
|
|
4
|
+
FailedPrecondition,
|
|
5
|
+
InvalidArgument,
|
|
6
|
+
NotFound,
|
|
7
|
+
NucladbError,
|
|
8
|
+
PermissionDenied,
|
|
9
|
+
QuotaExceeded,
|
|
10
|
+
RateLimited,
|
|
11
|
+
RestError,
|
|
12
|
+
Unauthenticated,
|
|
13
|
+
Unavailable,
|
|
14
|
+
)
|
|
15
|
+
from .rest import RestClient
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"APIKey", "AlreadyExists", "Client", "DistanceMetric", "FailedPrecondition", "InvalidArgument",
|
|
19
|
+
"NotFound", "NucladbError", "PermissionDenied", "QuotaExceeded", "RateLimited", "RestClient",
|
|
20
|
+
"RestError", "ScoredVector", "Tenant", "Unauthenticated", "Unavailable",
|
|
21
|
+
]
|