@hydradb/mcp 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +191 -0
- package/README.md +190 -0
- package/dist/client.js +144 -0
- package/dist/context.js +150 -0
- package/dist/descriptions.js +92 -0
- package/dist/index.js +29 -0
- package/dist/logger.js +66 -0
- package/dist/server.js +322 -0
- package/dist/tool-names.js +11 -0
- package/dist/types.js +2 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2025 HydraDB Contributors
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
# Hydra DB — MCP Server
|
|
2
|
+
|
|
3
|
+
MCP (Model Context Protocol) server for [Hydra DB](https://hydradb.com), the state-of-the-art agentic memory. Provides tools for storing, recalling, and managing memories with knowledge-graph enriched context.
|
|
4
|
+
|
|
5
|
+
## Available Tools
|
|
6
|
+
|
|
7
|
+
### **hydra_db_search**
|
|
8
|
+
|
|
9
|
+
Search through Hydra DB memories. Returns relevant chunks with graph-enriched context including entity paths and knowledge graph relations.
|
|
10
|
+
|
|
11
|
+
### **hydra_db_store**
|
|
12
|
+
|
|
13
|
+
Save important information to Hydra DB memory. Hydra DB automatically extracts insights, preferences, and builds a knowledge graph from the stored content.
|
|
14
|
+
|
|
15
|
+
### **hydra_db_ingest_conversation**
|
|
16
|
+
|
|
17
|
+
Ingest user-assistant conversation turns into Hydra DB memory. Hydra DB extracts insights, preferences, and knowledge graph entities from the conversation.
|
|
18
|
+
|
|
19
|
+
### **hydra_db_list_memories**
|
|
20
|
+
|
|
21
|
+
List all stored user memories in Hydra DB. Returns memory IDs and their content.
|
|
22
|
+
|
|
23
|
+
### **hydra_db_delete_memory**
|
|
24
|
+
|
|
25
|
+
Delete a specific user memory from Hydra DB by its memory ID.
|
|
26
|
+
|
|
27
|
+
### **hydra_db_fetch_content**
|
|
28
|
+
|
|
29
|
+
Fetch the full content of a specific source by its source ID.
|
|
30
|
+
|
|
31
|
+
### **hydra_db_list_sources**
|
|
32
|
+
|
|
33
|
+
List all ingested sources in Hydra DB memory.
|
|
34
|
+
|
|
35
|
+
## Configuration
|
|
36
|
+
|
|
37
|
+
### Get Your Credentials
|
|
38
|
+
|
|
39
|
+
1. Get your Hydra DB API Key from [Hydra DB](https://app.hydradb.com)
|
|
40
|
+
2. Get your Tenant ID from the Hydra DB dashboard
|
|
41
|
+
|
|
42
|
+
### Environment Variables
|
|
43
|
+
|
|
44
|
+
| Variable | Description | Default |
|
|
45
|
+
| ------------------------ | ----------------------------------- | -------------- |
|
|
46
|
+
| `HYDRA_DB_API_KEY` | Your Hydra-DB API key | *Required* |
|
|
47
|
+
| `HYDRA_DB_TENANT_ID` | Your Hydra-DB tenant identifier | *Required* |
|
|
48
|
+
| `HYDRA_DB_SUB_TENANT_ID` | Sub-tenant for data partitioning | `hydra-db-mcp` |
|
|
49
|
+
| `HYDRA_DB_LOG_LEVEL` | Log level: DEBUG, INFO, WARN, ERROR | `ERROR` |
|
|
50
|
+
|
|
51
|
+
### Claude Desktop
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"mcpServers": {
|
|
56
|
+
"hydradb": {
|
|
57
|
+
"command": "npx",
|
|
58
|
+
"args": ["-y", "@hydra_db/mcp@0.1.1"],
|
|
59
|
+
"env": {
|
|
60
|
+
"HYDRA_DB_API_KEY": "your-api-key",
|
|
61
|
+
"HYDRA_DB_TENANT_ID": "your-tenant-id"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Cursor & Windsurf
|
|
69
|
+
|
|
70
|
+
| Client | Config File |
|
|
71
|
+
| -------- | --------------------------------------- |
|
|
72
|
+
| Cursor | `~/.cursor/mcp.json` |
|
|
73
|
+
| Windsurf | `~/.codeium/windsurf/mcp_config.json` |
|
|
74
|
+
|
|
75
|
+
```json
|
|
76
|
+
{
|
|
77
|
+
"mcpServers": {
|
|
78
|
+
"hydradb": {
|
|
79
|
+
"command": "npx",
|
|
80
|
+
"args": ["-y", "@hydra_db/mcp@0.1.1"],
|
|
81
|
+
"env": {
|
|
82
|
+
"HYDRA_DB_API_KEY": "your-api-key",
|
|
83
|
+
"HYDRA_DB_TENANT_ID": "your-tenant-id"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### VS Code
|
|
91
|
+
|
|
92
|
+
Add to `.vscode/mcp.json`:
|
|
93
|
+
|
|
94
|
+
```json
|
|
95
|
+
{
|
|
96
|
+
"servers": {
|
|
97
|
+
"hydradb": {
|
|
98
|
+
"type": "stdio",
|
|
99
|
+
"command": "npx",
|
|
100
|
+
"args": ["-y", "@hydra_db/mcp@0.1.1"],
|
|
101
|
+
"env": {
|
|
102
|
+
"HYDRA_DB_API_KEY": "your-api-key",
|
|
103
|
+
"HYDRA_DB_TENANT_ID": "your-tenant-id"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Custom Sub-Tenant
|
|
111
|
+
|
|
112
|
+
To partition data, set the `HYDRA_DB_SUB_TENANT_ID` environment variable:
|
|
113
|
+
|
|
114
|
+
```json
|
|
115
|
+
{
|
|
116
|
+
"mcpServers": {
|
|
117
|
+
"hydradb": {
|
|
118
|
+
"command": "npx",
|
|
119
|
+
"args": ["-y", "@hydra_db/mcp@0.1.1"],
|
|
120
|
+
"env": {
|
|
121
|
+
"HYDRA_DB_API_KEY": "your-api-key",
|
|
122
|
+
"HYDRA_DB_TENANT_ID": "your-tenant-id",
|
|
123
|
+
"HYDRA_DB_SUB_TENANT_ID": "my-project"
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## How It Works
|
|
131
|
+
|
|
132
|
+
- **hydra_db_search** queries `/recall/recall_preferences` for relevant memories and returns graph-enriched context (entity paths, chunk relations, extra context).
|
|
133
|
+
- **hydra_db_store** sends text to `/memories/add_memory` with `infer: true` and `upsert: true`. Hydra-DB extracts insights and builds a knowledge graph automatically.
|
|
134
|
+
- **hydra_db_ingest_conversation** sends user-assistant pairs to `/memories/add_memory` as conversation turns, grouped by `source_id`.
|
|
135
|
+
- **hydra_db_list_memories** and **hydra_db_list_sources** query `/list/data` to browse stored data.
|
|
136
|
+
- **hydra_db_delete_memory** calls `DELETE /memories/delete_memory` to remove a specific memory.
|
|
137
|
+
- **hydra_db_fetch_content** calls `/fetch/content` to retrieve the original ingested content.
|
|
138
|
+
|
|
139
|
+
## Development
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
npm install
|
|
143
|
+
npm run build
|
|
144
|
+
HYDRA_DB_API_KEY=your-key HYDRA_DB_TENANT_ID=your-tenant npm start
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
For development with auto-reload:
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
HYDRA_DB_API_KEY=your-key HYDRA_DB_TENANT_ID=your-tenant npm run dev
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Testing
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
# unit tests (mocked HTTP)
|
|
157
|
+
npm test
|
|
158
|
+
|
|
159
|
+
# live integration test against Hydra DB
|
|
160
|
+
RUN_LIVE_TESTS=true HYDRA_DB_API_KEY=your-key HYDRA_DB_TENANT_ID=your-tenant npm run test:integration
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Troubleshooting
|
|
164
|
+
|
|
165
|
+
- **API Key Issues**: Ensure `HYDRA_DB_API_KEY` is set correctly
|
|
166
|
+
- **Connection Errors**: Check your internet connection and API key validity
|
|
167
|
+
- **Tool Not Found**: Make sure the package is installed and the command path is correct
|
|
168
|
+
- **Debug Logging**: Set `HYDRA_DB_LOG_LEVEL=DEBUG` for verbose output
|
|
169
|
+
|
|
170
|
+
## Contributing / Developer Setup
|
|
171
|
+
|
|
172
|
+
Get up and running quickly with the bootstrap script:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
git clone https://github.com/usecortex/hydradb-mcp.git
|
|
176
|
+
cd hydradb-mcp
|
|
177
|
+
make bootstrap
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
This will install dependencies, build the project, and create a `.env` file from `.env.example`. Edit `.env` with your HydraDB credentials, then:
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
make dev # Start MCP server in dev mode (auto-reload)
|
|
184
|
+
make test # Run unit tests
|
|
185
|
+
make test-all # Run unit + integration tests
|
|
186
|
+
make check-types # Type-check without emitting
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Run `make help` to see all available targets.
|
|
190
|
+
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { logger } from "./logger.js";
|
|
2
|
+
const API_BASE = "https://api.hydradb.com";
|
|
3
|
+
const INGEST_INSTRUCTIONS = "Focus on extracting user preferences, habits, opinions, likes, dislikes, " +
|
|
4
|
+
"goals, and recurring themes. Capture any stated or implied personal context " +
|
|
5
|
+
"that would help personalise future interactions.";
|
|
6
|
+
export class HydraDBClient {
|
|
7
|
+
constructor(apiKey, tenantId, subTenantId) {
|
|
8
|
+
this.apiKey = apiKey;
|
|
9
|
+
this.tenantId = tenantId;
|
|
10
|
+
this.subTenantId = subTenantId;
|
|
11
|
+
logger.info(`Hydra DB connected (tenant=${tenantId}, sub=${subTenantId})`);
|
|
12
|
+
}
|
|
13
|
+
headers() {
|
|
14
|
+
return {
|
|
15
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
16
|
+
"Content-Type": "application/json",
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
async post(path, body) {
|
|
20
|
+
const url = `${API_BASE}${path}`;
|
|
21
|
+
logger.debug("POST " + path, { body: body });
|
|
22
|
+
const res = await fetch(url, {
|
|
23
|
+
method: "POST",
|
|
24
|
+
headers: this.headers(),
|
|
25
|
+
body: JSON.stringify(body),
|
|
26
|
+
});
|
|
27
|
+
if (!res.ok) {
|
|
28
|
+
const text = await res.text().catch(() => "");
|
|
29
|
+
throw new Error(`Hydra DB ${path} → ${res.status}: ${text}`);
|
|
30
|
+
}
|
|
31
|
+
return res.json();
|
|
32
|
+
}
|
|
33
|
+
async del(path, params) {
|
|
34
|
+
const qs = new URLSearchParams(params).toString();
|
|
35
|
+
const url = `${API_BASE}${path}?${qs}`;
|
|
36
|
+
logger.debug("DELETE " + path, params);
|
|
37
|
+
const res = await fetch(url, {
|
|
38
|
+
method: "DELETE",
|
|
39
|
+
headers: this.headers(),
|
|
40
|
+
});
|
|
41
|
+
if (!res.ok) {
|
|
42
|
+
const text = await res.text().catch(() => "");
|
|
43
|
+
throw new Error(`Hydra DB ${path} → ${res.status}: ${text}`);
|
|
44
|
+
}
|
|
45
|
+
return res.json();
|
|
46
|
+
}
|
|
47
|
+
// --- Ingest ---
|
|
48
|
+
async ingestConversation(turns, sourceId, userName) {
|
|
49
|
+
const payload = {
|
|
50
|
+
memories: [
|
|
51
|
+
{
|
|
52
|
+
user_assistant_pairs: turns,
|
|
53
|
+
infer: true,
|
|
54
|
+
source_id: sourceId,
|
|
55
|
+
user_name: userName ?? "User",
|
|
56
|
+
custom_instructions: INGEST_INSTRUCTIONS,
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
tenant_id: this.tenantId,
|
|
60
|
+
sub_tenant_id: this.subTenantId,
|
|
61
|
+
upsert: true,
|
|
62
|
+
};
|
|
63
|
+
return this.post("/memories/add_memory", payload);
|
|
64
|
+
}
|
|
65
|
+
async ingestText(text, opts) {
|
|
66
|
+
const shouldInfer = opts?.infer ?? true;
|
|
67
|
+
const payload = {
|
|
68
|
+
memories: [
|
|
69
|
+
{
|
|
70
|
+
text,
|
|
71
|
+
infer: shouldInfer,
|
|
72
|
+
is_markdown: opts?.isMarkdown ?? false,
|
|
73
|
+
...(shouldInfer && {
|
|
74
|
+
custom_instructions: opts?.customInstructions ?? INGEST_INSTRUCTIONS,
|
|
75
|
+
}),
|
|
76
|
+
...(opts?.sourceId && { source_id: opts.sourceId }),
|
|
77
|
+
...(opts?.title && { title: opts.title }),
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
tenant_id: this.tenantId,
|
|
81
|
+
sub_tenant_id: this.subTenantId,
|
|
82
|
+
upsert: true,
|
|
83
|
+
};
|
|
84
|
+
return this.post("/memories/add_memory", payload);
|
|
85
|
+
}
|
|
86
|
+
// --- Recall ---
|
|
87
|
+
async recall(query, opts) {
|
|
88
|
+
const payload = {
|
|
89
|
+
tenant_id: this.tenantId,
|
|
90
|
+
sub_tenant_id: this.subTenantId,
|
|
91
|
+
query,
|
|
92
|
+
max_results: opts?.maxResults ?? 10,
|
|
93
|
+
mode: opts?.mode ?? "thinking",
|
|
94
|
+
alpha: 0.8,
|
|
95
|
+
recency_bias: opts?.recencyBias ?? 0,
|
|
96
|
+
graph_context: opts?.graphContext ?? true,
|
|
97
|
+
};
|
|
98
|
+
return this.post("/recall/recall_preferences", payload);
|
|
99
|
+
}
|
|
100
|
+
// --- List ---
|
|
101
|
+
async listMemories() {
|
|
102
|
+
const payload = {
|
|
103
|
+
tenant_id: this.tenantId,
|
|
104
|
+
sub_tenant_id: this.subTenantId,
|
|
105
|
+
kind: "memories",
|
|
106
|
+
};
|
|
107
|
+
return this.post("/list/data", payload);
|
|
108
|
+
}
|
|
109
|
+
async listSources(sourceIds) {
|
|
110
|
+
const payload = {
|
|
111
|
+
tenant_id: this.tenantId,
|
|
112
|
+
sub_tenant_id: this.subTenantId,
|
|
113
|
+
kind: "knowledge",
|
|
114
|
+
...(sourceIds && { source_ids: sourceIds }),
|
|
115
|
+
};
|
|
116
|
+
return this.post("/list/data", payload);
|
|
117
|
+
}
|
|
118
|
+
// --- Delete ---
|
|
119
|
+
async deleteMemory(memoryId) {
|
|
120
|
+
return this.del("/memories/delete_memory", {
|
|
121
|
+
tenant_id: this.tenantId,
|
|
122
|
+
memory_id: memoryId,
|
|
123
|
+
sub_tenant_id: this.subTenantId,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
// --- Fetch Content ---
|
|
127
|
+
async fetchContent(sourceId, mode = "content") {
|
|
128
|
+
const payload = {
|
|
129
|
+
tenant_id: this.tenantId,
|
|
130
|
+
sub_tenant_id: this.subTenantId,
|
|
131
|
+
source_id: sourceId,
|
|
132
|
+
mode,
|
|
133
|
+
};
|
|
134
|
+
return this.post("/fetch/content", payload);
|
|
135
|
+
}
|
|
136
|
+
// --- Accessors ---
|
|
137
|
+
getTenantId() {
|
|
138
|
+
return this.tenantId;
|
|
139
|
+
}
|
|
140
|
+
getSubTenantId() {
|
|
141
|
+
return this.subTenantId;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=client.js.map
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
function formatTriplet(triplet) {
|
|
2
|
+
const src = triplet.source?.name ?? "?";
|
|
3
|
+
const rel = triplet.relation;
|
|
4
|
+
const predicate = rel?.raw_predicate ?? rel?.canonical_predicate ?? "related to";
|
|
5
|
+
const tgt = triplet.target?.name ?? "?";
|
|
6
|
+
const ctx = rel?.context ? ` [${rel.context}]` : "";
|
|
7
|
+
return ` (${src}) —[${predicate}]→ (${tgt})${ctx}`;
|
|
8
|
+
}
|
|
9
|
+
export function buildRecalledContext(response, opts) {
|
|
10
|
+
const minScore = opts?.minEvidenceScore ?? 0.4;
|
|
11
|
+
const maxGroupOccurrences = opts?.maxGroupOccurrences;
|
|
12
|
+
const chunks = response.chunks ?? [];
|
|
13
|
+
const graphCtx = response.graph_context ?? {
|
|
14
|
+
query_paths: [],
|
|
15
|
+
chunk_relations: [],
|
|
16
|
+
chunk_id_to_group_ids: {},
|
|
17
|
+
};
|
|
18
|
+
const extraContextMap = response.additional_context ?? {};
|
|
19
|
+
const rawRelations = graphCtx.chunk_relations ?? [];
|
|
20
|
+
const relationIndex = {};
|
|
21
|
+
for (let idx = 0; idx < rawRelations.length; idx++) {
|
|
22
|
+
const relation = rawRelations[idx];
|
|
23
|
+
if ((relation.relevancy_score ?? 0) < minScore)
|
|
24
|
+
continue;
|
|
25
|
+
const groupId = relation.group_id ?? `p_${idx}`;
|
|
26
|
+
relationIndex[groupId] = relation;
|
|
27
|
+
}
|
|
28
|
+
const chunkToGroupIds = graphCtx.chunk_id_to_group_ids ?? {};
|
|
29
|
+
const consumedExtraIds = new Set();
|
|
30
|
+
const groupOccurrenceCounts = {};
|
|
31
|
+
const chunkSections = [];
|
|
32
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
33
|
+
const chunk = chunks[i];
|
|
34
|
+
const lines = [];
|
|
35
|
+
lines.push(`Chunk ${i + 1}`);
|
|
36
|
+
const meta = chunk.document_metadata ?? {};
|
|
37
|
+
const title = chunk.source_title || meta.title;
|
|
38
|
+
if (title) {
|
|
39
|
+
lines.push(`Source: ${title}`);
|
|
40
|
+
}
|
|
41
|
+
lines.push(chunk.chunk_content ?? "");
|
|
42
|
+
const chunkUuid = chunk.chunk_uuid;
|
|
43
|
+
const linkedGroupIds = chunkToGroupIds[chunkUuid] ?? [];
|
|
44
|
+
const matchedRelations = [];
|
|
45
|
+
// Track whether the primary lookup found any candidate groups
|
|
46
|
+
// (even if all were capped) to avoid incorrectly falling through
|
|
47
|
+
// to the fallback path
|
|
48
|
+
const hasLinkedGroups = linkedGroupIds.some((gid) => !!relationIndex[gid]);
|
|
49
|
+
for (const gid of linkedGroupIds) {
|
|
50
|
+
if (relationIndex[gid]) {
|
|
51
|
+
const occurrences = groupOccurrenceCounts[gid] ?? 0;
|
|
52
|
+
if (maxGroupOccurrences == null ||
|
|
53
|
+
occurrences < maxGroupOccurrences) {
|
|
54
|
+
matchedRelations.push(relationIndex[gid]);
|
|
55
|
+
groupOccurrenceCounts[gid] = occurrences + 1;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (matchedRelations.length === 0 && !hasLinkedGroups) {
|
|
60
|
+
for (const [gid, rel] of Object.entries(relationIndex)) {
|
|
61
|
+
const triplets = rel.triplets ?? [];
|
|
62
|
+
const hasChunk = triplets.some((t) => t.relation?.chunk_id === chunkUuid);
|
|
63
|
+
if (hasChunk) {
|
|
64
|
+
const occurrences = groupOccurrenceCounts[gid] ?? 0;
|
|
65
|
+
if (maxGroupOccurrences == null ||
|
|
66
|
+
occurrences < maxGroupOccurrences) {
|
|
67
|
+
matchedRelations.push(rel);
|
|
68
|
+
groupOccurrenceCounts[gid] = occurrences + 1;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const relationLines = [];
|
|
74
|
+
for (const rel of matchedRelations) {
|
|
75
|
+
const triplets = rel.triplets ?? [];
|
|
76
|
+
if (triplets.length > 0) {
|
|
77
|
+
for (const triplet of triplets) {
|
|
78
|
+
relationLines.push(formatTriplet(triplet));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if (rel.combined_context) {
|
|
82
|
+
relationLines.push(` ${rel.combined_context}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (relationLines.length > 0) {
|
|
86
|
+
lines.push("Graph Relations:");
|
|
87
|
+
lines.push(...relationLines);
|
|
88
|
+
}
|
|
89
|
+
const extraIds = chunk.extra_context_ids ?? [];
|
|
90
|
+
if (extraIds.length > 0 && Object.keys(extraContextMap).length > 0) {
|
|
91
|
+
const extraLines = [];
|
|
92
|
+
for (const ctxId of extraIds) {
|
|
93
|
+
if (consumedExtraIds.has(ctxId))
|
|
94
|
+
continue;
|
|
95
|
+
const extraChunk = extraContextMap[ctxId];
|
|
96
|
+
if (extraChunk) {
|
|
97
|
+
consumedExtraIds.add(ctxId);
|
|
98
|
+
const extraContent = extraChunk.chunk_content ?? "";
|
|
99
|
+
const extraTitle = extraChunk.source_title ?? "";
|
|
100
|
+
if (extraTitle) {
|
|
101
|
+
extraLines.push(` Related Context (${extraTitle}): ${extraContent}`);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
extraLines.push(` Related Context: ${extraContent}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (extraLines.length > 0) {
|
|
109
|
+
lines.push("Extra Context:");
|
|
110
|
+
lines.push(...extraLines);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
chunkSections.push(lines.join("\n"));
|
|
114
|
+
}
|
|
115
|
+
const entityPathLines = [];
|
|
116
|
+
const rawPaths = graphCtx.query_paths ?? [];
|
|
117
|
+
for (const path of rawPaths) {
|
|
118
|
+
if (path.combined_context) {
|
|
119
|
+
entityPathLines.push(path.combined_context);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
const triplets = path.triplets ?? [];
|
|
123
|
+
const segments = [];
|
|
124
|
+
for (const pt of triplets) {
|
|
125
|
+
const s = pt.source?.name;
|
|
126
|
+
const rel = pt.relation;
|
|
127
|
+
const p = rel?.raw_predicate ??
|
|
128
|
+
rel?.canonical_predicate ??
|
|
129
|
+
"related to";
|
|
130
|
+
const t = pt.target?.name;
|
|
131
|
+
segments.push(`(${s} -> ${p} -> ${t})`);
|
|
132
|
+
}
|
|
133
|
+
if (segments.length > 0) {
|
|
134
|
+
entityPathLines.push(segments.join(" -> "));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const output = [];
|
|
139
|
+
if (entityPathLines.length > 0) {
|
|
140
|
+
output.push("=== ENTITY PATHS ===");
|
|
141
|
+
output.push(entityPathLines.join("\n"));
|
|
142
|
+
output.push("");
|
|
143
|
+
}
|
|
144
|
+
if (chunkSections.length > 0) {
|
|
145
|
+
output.push("=== CONTEXT ===");
|
|
146
|
+
output.push(chunkSections.join("\n\n---\n\n"));
|
|
147
|
+
}
|
|
148
|
+
return output.join("\n");
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool descriptions for the Hydra DB MCP server.
|
|
3
|
+
* Kept in a separate file for easy editing and localisation.
|
|
4
|
+
*/
|
|
5
|
+
import { TOOL_NAMES } from "./tool-names.js";
|
|
6
|
+
export const TOOL_DESCRIPTIONS = {
|
|
7
|
+
[TOOL_NAMES.SEARCH]: {
|
|
8
|
+
title: "Search Hydra DB Memory",
|
|
9
|
+
description: "Search through Hydra DB State-of-the-art agentic memories. Returns relevant chunks with " +
|
|
10
|
+
"graph-enriched context including entity paths and knowledge graph relations. " +
|
|
11
|
+
"Use this to find previously stored information, past conversations, user preferences, " +
|
|
12
|
+
"or any knowledge that has been ingested into Hydra DB memory. " +
|
|
13
|
+
"Supports both fast semantic search and deeper thinking mode with graph traversal.",
|
|
14
|
+
params: {
|
|
15
|
+
query: "The search query to find relevant memories",
|
|
16
|
+
max_results: "Maximum number of memory chunks to return (1-50, default: 10)",
|
|
17
|
+
mode: "Recall mode: 'fast' for quick semantic search, 'thinking' for deeper personalised recall with graph traversal (default: 'thinking')",
|
|
18
|
+
graph_context: "Whether to include knowledge graph relations in results (default: true)",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
[TOOL_NAMES.STORE]: {
|
|
22
|
+
title: "Store to Hydra DB Memory",
|
|
23
|
+
description: "Save important information to Hydra DB State-of-the-art agentic memory. Use this to persist " +
|
|
24
|
+
"facts, preferences, decisions, notes, or any text the user wants remembered across " +
|
|
25
|
+
"sessions. Hydra DB automatically extracts insights, preferences, and builds a knowledge " +
|
|
26
|
+
"graph from the stored content. Supports plain text and markdown.",
|
|
27
|
+
params: {
|
|
28
|
+
text: "The information to store in memory",
|
|
29
|
+
title: "Optional title for the memory entry (default: 'MCP Memory')",
|
|
30
|
+
source_id: "Optional source identifier to group related memories together. You can use this as " +
|
|
31
|
+
"your session ID or any other unique identifier for a conversation",
|
|
32
|
+
infer: "Whether Hydra DB should extract insights and build knowledge graph from this text (default: true)",
|
|
33
|
+
is_markdown: "Whether the text is in markdown format (default: false)",
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
[TOOL_NAMES.INGEST_CONVERSATION]: {
|
|
37
|
+
title: "Ingest Conversation",
|
|
38
|
+
description: "Ingest one or more user-assistant conversation turns into Hydra DB memory. " +
|
|
39
|
+
"Hydra DB will extract insights, preferences, and knowledge graph entities from the " +
|
|
40
|
+
"conversation. Use this to store conversation history so it can be recalled later. " +
|
|
41
|
+
"Each turn is a pair of user message and assistant response.",
|
|
42
|
+
params: {
|
|
43
|
+
turns: "Array of conversation turns, each with a 'user' and 'assistant' field",
|
|
44
|
+
source_id: "Source identifier to group all turns from the same session together",
|
|
45
|
+
user_name: "Optional name of the user for personalisation (default: 'User')",
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
[TOOL_NAMES.LIST_MEMORIES]: {
|
|
49
|
+
title: "List Memories",
|
|
50
|
+
description: "List all stored user memories in Hydra DB. Returns memory IDs and their content. " +
|
|
51
|
+
"Use this to browse what has been stored, verify memories exist, or find memory IDs " +
|
|
52
|
+
"for deletion.",
|
|
53
|
+
},
|
|
54
|
+
[TOOL_NAMES.DELETE_MEMORY]: {
|
|
55
|
+
title: "Delete Memory",
|
|
56
|
+
description: "Delete a specific user memory from Hydra DB by its memory ID. " +
|
|
57
|
+
`Use ${TOOL_NAMES.LIST_MEMORIES} first to find the memory ID you want to delete. ` +
|
|
58
|
+
"This action is irreversible.",
|
|
59
|
+
params: {
|
|
60
|
+
memory_id: "The ID of the memory to delete",
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
[TOOL_NAMES.FETCH_CONTENT]: {
|
|
64
|
+
title: "Fetch Source Content",
|
|
65
|
+
description: "Fetch the full content of a specific source by its source ID from Hydra DB. " +
|
|
66
|
+
"Returns the original text content that was ingested. Use this to retrieve " +
|
|
67
|
+
"the complete content of a previously stored source.",
|
|
68
|
+
params: {
|
|
69
|
+
source_id: "The source ID to fetch content for",
|
|
70
|
+
mode: "Fetch mode: 'content' for text, 'url' for presigned URL, 'both' for both (default: 'content')",
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
[TOOL_NAMES.LIST_SOURCES]: {
|
|
74
|
+
title: "List Sources",
|
|
75
|
+
description: "List all ingested sources in Hydra DB memory. Returns source IDs, titles, types, " +
|
|
76
|
+
"and metadata. Use this to see what data sources have been ingested and to find " +
|
|
77
|
+
"source IDs for fetching content.",
|
|
78
|
+
params: {
|
|
79
|
+
source_ids: "Optional array of specific source IDs to filter by. If omitted, lists all sources.",
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
export const SERVER_INSTRUCTIONS = "Hydra DB MCP server for State-of-the-art agentic memory management. " +
|
|
84
|
+
`Use ${TOOL_NAMES.SEARCH} to find relevant memories and knowledge graph context. ` +
|
|
85
|
+
`Use ${TOOL_NAMES.STORE} to save important information for future recall. ` +
|
|
86
|
+
`Use ${TOOL_NAMES.INGEST_CONVERSATION} to store conversation history. ` +
|
|
87
|
+
`Use ${TOOL_NAMES.LIST_MEMORIES} to browse stored memories. ` +
|
|
88
|
+
`Use ${TOOL_NAMES.DELETE_MEMORY} to remove specific memories. ` +
|
|
89
|
+
`Use ${TOOL_NAMES.FETCH_CONTENT} to retrieve full source content. ` +
|
|
90
|
+
`Use ${TOOL_NAMES.LIST_SOURCES} to see all ingested data sources. ` +
|
|
91
|
+
"All tools require a valid Hydra DB API key and tenant ID configured via environment variables.";
|
|
92
|
+
//# sourceMappingURL=descriptions.js.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
|
+
import { createHydraDBServer } from "./server.js";
|
|
4
|
+
const HYDRA_DB_API_KEY = process.env.HYDRA_DB_API_KEY;
|
|
5
|
+
if (!HYDRA_DB_API_KEY) {
|
|
6
|
+
console.error("Error: HYDRA_DB_API_KEY environment variable is required");
|
|
7
|
+
process.exit(1);
|
|
8
|
+
}
|
|
9
|
+
const HYDRA_DB_TENANT_ID = process.env.HYDRA_DB_TENANT_ID;
|
|
10
|
+
if (!HYDRA_DB_TENANT_ID) {
|
|
11
|
+
console.error("Error: HYDRA_DB_TENANT_ID environment variable is required");
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
async function main() {
|
|
15
|
+
try {
|
|
16
|
+
const server = createHydraDBServer();
|
|
17
|
+
const transport = new StdioServerTransport();
|
|
18
|
+
await server.connect(transport);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
console.error("Fatal error running server:", error);
|
|
22
|
+
process.exit(1);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
main().catch((error) => {
|
|
26
|
+
console.error("Fatal error running server:", error);
|
|
27
|
+
process.exit(1);
|
|
28
|
+
});
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
package/dist/logger.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structured logger for Hydra DB MCP Server.
|
|
3
|
+
* Outputs to stderr to avoid interfering with STDIO transport.
|
|
4
|
+
*/
|
|
5
|
+
export var LogLevel;
|
|
6
|
+
(function (LogLevel) {
|
|
7
|
+
LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
|
|
8
|
+
LogLevel[LogLevel["INFO"] = 1] = "INFO";
|
|
9
|
+
LogLevel[LogLevel["WARN"] = 2] = "WARN";
|
|
10
|
+
LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
|
|
11
|
+
})(LogLevel || (LogLevel = {}));
|
|
12
|
+
const LOG_LEVEL_NAMES = {
|
|
13
|
+
[LogLevel.DEBUG]: "DEBUG",
|
|
14
|
+
[LogLevel.INFO]: "INFO",
|
|
15
|
+
[LogLevel.WARN]: "WARN",
|
|
16
|
+
[LogLevel.ERROR]: "ERROR",
|
|
17
|
+
};
|
|
18
|
+
function getLogLevel() {
|
|
19
|
+
const level = process.env.HYDRA_DB_LOG_LEVEL?.toUpperCase();
|
|
20
|
+
switch (level) {
|
|
21
|
+
case "DEBUG":
|
|
22
|
+
return LogLevel.DEBUG;
|
|
23
|
+
case "INFO":
|
|
24
|
+
return LogLevel.INFO;
|
|
25
|
+
case "WARN":
|
|
26
|
+
return LogLevel.WARN;
|
|
27
|
+
case "ERROR":
|
|
28
|
+
return LogLevel.ERROR;
|
|
29
|
+
default:
|
|
30
|
+
return LogLevel.ERROR;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const currentLogLevel = getLogLevel();
|
|
34
|
+
function formatMessage(level, message, meta) {
|
|
35
|
+
const timestamp = new Date().toISOString();
|
|
36
|
+
const levelName = LOG_LEVEL_NAMES[level];
|
|
37
|
+
if (meta && Object.keys(meta).length > 0) {
|
|
38
|
+
try {
|
|
39
|
+
return `[${timestamp}] [hydradb-mcp] ${levelName}: ${message} ${JSON.stringify(meta)}`;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return `[${timestamp}] [hydradb-mcp] ${levelName}: ${message} [unstringifiable]`;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return `[${timestamp}] [hydradb-mcp] ${levelName}: ${message}`;
|
|
46
|
+
}
|
|
47
|
+
function log(level, message, meta) {
|
|
48
|
+
if (level >= currentLogLevel) {
|
|
49
|
+
console.error(formatMessage(level, message, meta));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export const logger = {
|
|
53
|
+
debug(message, meta) {
|
|
54
|
+
log(LogLevel.DEBUG, message, meta);
|
|
55
|
+
},
|
|
56
|
+
info(message, meta) {
|
|
57
|
+
log(LogLevel.INFO, message, meta);
|
|
58
|
+
},
|
|
59
|
+
warn(message, meta) {
|
|
60
|
+
log(LogLevel.WARN, message, meta);
|
|
61
|
+
},
|
|
62
|
+
error(message, meta) {
|
|
63
|
+
log(LogLevel.ERROR, message, meta);
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
//# sourceMappingURL=logger.js.map
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { HydraDBClient } from "./client.js";
|
|
4
|
+
import { buildRecalledContext } from "./context.js";
|
|
5
|
+
import { TOOL_DESCRIPTIONS, SERVER_INSTRUCTIONS } from "./descriptions.js";
|
|
6
|
+
import { logger } from "./logger.js";
|
|
7
|
+
import { TOOL_NAMES } from "./tool-names.js";
|
|
8
|
+
const DEFAULT_SUB_TENANT = "hydra-db-mcp";
|
|
9
|
+
function getConfig() {
|
|
10
|
+
const apiKey = process.env.HYDRA_DB_API_KEY;
|
|
11
|
+
if (!apiKey) {
|
|
12
|
+
throw new Error("HYDRA_DB_API_KEY environment variable is required");
|
|
13
|
+
}
|
|
14
|
+
const tenantId = process.env.HYDRA_DB_TENANT_ID;
|
|
15
|
+
if (!tenantId) {
|
|
16
|
+
throw new Error("HYDRA_DB_TENANT_ID environment variable is required");
|
|
17
|
+
}
|
|
18
|
+
const subTenantId = process.env.HYDRA_DB_SUB_TENANT_ID || DEFAULT_SUB_TENANT;
|
|
19
|
+
return { apiKey, tenantId, subTenantId };
|
|
20
|
+
}
|
|
21
|
+
export function createHydraDBServer() {
|
|
22
|
+
const server = new McpServer({
|
|
23
|
+
name: "hydradb-mcp",
|
|
24
|
+
version: "1.0.0",
|
|
25
|
+
}, {
|
|
26
|
+
instructions: SERVER_INSTRUCTIONS,
|
|
27
|
+
});
|
|
28
|
+
const config = getConfig();
|
|
29
|
+
const client = new HydraDBClient(config.apiKey, config.tenantId, config.subTenantId);
|
|
30
|
+
// --- Search ---
|
|
31
|
+
const desc = TOOL_DESCRIPTIONS[TOOL_NAMES.SEARCH];
|
|
32
|
+
server.registerTool(TOOL_NAMES.SEARCH, {
|
|
33
|
+
title: desc.title,
|
|
34
|
+
description: desc.description,
|
|
35
|
+
inputSchema: {
|
|
36
|
+
query: z.string().describe(desc.params.query),
|
|
37
|
+
max_results: z
|
|
38
|
+
.number()
|
|
39
|
+
.min(1)
|
|
40
|
+
.max(50)
|
|
41
|
+
.optional()
|
|
42
|
+
.describe(desc.params.max_results),
|
|
43
|
+
mode: z
|
|
44
|
+
.enum(["fast", "thinking"])
|
|
45
|
+
.optional()
|
|
46
|
+
.describe(desc.params.mode),
|
|
47
|
+
graph_context: z
|
|
48
|
+
.boolean()
|
|
49
|
+
.optional()
|
|
50
|
+
.describe(desc.params.graph_context),
|
|
51
|
+
},
|
|
52
|
+
annotations: {
|
|
53
|
+
readOnlyHint: true,
|
|
54
|
+
openWorldHint: true,
|
|
55
|
+
idempotentHint: true,
|
|
56
|
+
},
|
|
57
|
+
}, async (args) => {
|
|
58
|
+
const { query, max_results, mode, graph_context, } = args;
|
|
59
|
+
logger.debug(`${TOOL_NAMES.SEARCH}: "${query}"`);
|
|
60
|
+
const res = await client.recall(query, {
|
|
61
|
+
maxResults: max_results ?? 10,
|
|
62
|
+
mode: mode ?? "thinking",
|
|
63
|
+
graphContext: graph_context ?? true,
|
|
64
|
+
});
|
|
65
|
+
if (!res.chunks || res.chunks.length === 0) {
|
|
66
|
+
return {
|
|
67
|
+
content: [
|
|
68
|
+
{
|
|
69
|
+
type: "text",
|
|
70
|
+
text: "No relevant memories found in Hydra DB.",
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const contextStr = buildRecalledContext(res);
|
|
76
|
+
const summary = res.chunks.slice(0, 10).map((c, i) => {
|
|
77
|
+
const score = c.relevancy_score != null
|
|
78
|
+
? ` (${Math.round(c.relevancy_score * 100)}%)`
|
|
79
|
+
: "";
|
|
80
|
+
const snippet = c.chunk_content.length > 150
|
|
81
|
+
? `${c.chunk_content.slice(0, 150)}...`
|
|
82
|
+
: c.chunk_content;
|
|
83
|
+
return `${i + 1}. ${snippet}${score}`;
|
|
84
|
+
});
|
|
85
|
+
return {
|
|
86
|
+
content: [
|
|
87
|
+
{
|
|
88
|
+
type: "text",
|
|
89
|
+
text: `Found ${res.chunks.length} memories:\n\n${summary.join("\n")}\n\n---\nFull context:\n${contextStr}`,
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
};
|
|
93
|
+
});
|
|
94
|
+
// --- Store ---
|
|
95
|
+
const storeDesc = TOOL_DESCRIPTIONS[TOOL_NAMES.STORE];
|
|
96
|
+
server.registerTool(TOOL_NAMES.STORE, {
|
|
97
|
+
title: storeDesc.title,
|
|
98
|
+
description: storeDesc.description,
|
|
99
|
+
inputSchema: {
|
|
100
|
+
text: z.string().describe(storeDesc.params.text),
|
|
101
|
+
title: z
|
|
102
|
+
.string()
|
|
103
|
+
.optional()
|
|
104
|
+
.describe(storeDesc.params.title),
|
|
105
|
+
source_id: z
|
|
106
|
+
.string()
|
|
107
|
+
.optional()
|
|
108
|
+
.describe(storeDesc.params.source_id),
|
|
109
|
+
infer: z
|
|
110
|
+
.boolean()
|
|
111
|
+
.optional()
|
|
112
|
+
.describe(storeDesc.params.infer),
|
|
113
|
+
is_markdown: z
|
|
114
|
+
.boolean()
|
|
115
|
+
.optional()
|
|
116
|
+
.describe(storeDesc.params.is_markdown),
|
|
117
|
+
},
|
|
118
|
+
}, async (args) => {
|
|
119
|
+
const { text, title, source_id, infer, is_markdown } = args;
|
|
120
|
+
logger.debug(`${TOOL_NAMES.STORE}: "${text.slice(0, 50)}..."`);
|
|
121
|
+
const res = await client.ingestText(text, {
|
|
122
|
+
sourceId: source_id,
|
|
123
|
+
title: title ?? "MCP Memory",
|
|
124
|
+
infer: infer ?? true,
|
|
125
|
+
isMarkdown: is_markdown ?? false,
|
|
126
|
+
});
|
|
127
|
+
const preview = text.length > 80 ? `${text.slice(0, 80)}...` : text;
|
|
128
|
+
return {
|
|
129
|
+
content: [
|
|
130
|
+
{
|
|
131
|
+
type: "text",
|
|
132
|
+
text: `Saved to Hydra DB (${res.success_count} success, ${res.failed_count} failed): "${preview}"`,
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
// --- Ingest Conversation ---
|
|
138
|
+
const ingestDesc = TOOL_DESCRIPTIONS[TOOL_NAMES.INGEST_CONVERSATION];
|
|
139
|
+
const turnSchema = z.object({
|
|
140
|
+
user: z.string().describe("The user's message"),
|
|
141
|
+
assistant: z.string().describe("The assistant's response"),
|
|
142
|
+
});
|
|
143
|
+
server.registerTool(TOOL_NAMES.INGEST_CONVERSATION, {
|
|
144
|
+
title: ingestDesc.title,
|
|
145
|
+
description: ingestDesc.description,
|
|
146
|
+
inputSchema: {
|
|
147
|
+
turns: z
|
|
148
|
+
.array(turnSchema)
|
|
149
|
+
.min(1)
|
|
150
|
+
.describe(ingestDesc.params.turns),
|
|
151
|
+
source_id: z
|
|
152
|
+
.string()
|
|
153
|
+
.describe(ingestDesc.params.source_id),
|
|
154
|
+
user_name: z
|
|
155
|
+
.string()
|
|
156
|
+
.optional()
|
|
157
|
+
.describe(ingestDesc.params.user_name),
|
|
158
|
+
},
|
|
159
|
+
}, async (args) => {
|
|
160
|
+
const { turns, source_id, user_name } = args;
|
|
161
|
+
logger.debug(`${TOOL_NAMES.INGEST_CONVERSATION}: ${turns.length} turns -> ${source_id}`);
|
|
162
|
+
const res = await client.ingestConversation(turns, source_id, user_name);
|
|
163
|
+
return {
|
|
164
|
+
content: [
|
|
165
|
+
{
|
|
166
|
+
type: "text",
|
|
167
|
+
text: `Ingested ${turns.length} conversation turn(s) into Hydra DB (source: ${source_id}, success: ${res.success_count}, failed: ${res.failed_count})`,
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
// --- List Memories ---
|
|
173
|
+
const listDesc = TOOL_DESCRIPTIONS[TOOL_NAMES.LIST_MEMORIES];
|
|
174
|
+
server.registerTool(TOOL_NAMES.LIST_MEMORIES, {
|
|
175
|
+
title: listDesc.title,
|
|
176
|
+
description: listDesc.description,
|
|
177
|
+
inputSchema: {},
|
|
178
|
+
annotations: {
|
|
179
|
+
readOnlyHint: true,
|
|
180
|
+
idempotentHint: true,
|
|
181
|
+
},
|
|
182
|
+
}, async () => {
|
|
183
|
+
logger.debug(TOOL_NAMES.LIST_MEMORIES);
|
|
184
|
+
const res = await client.listMemories();
|
|
185
|
+
const memories = res.user_memories ?? [];
|
|
186
|
+
if (memories.length === 0) {
|
|
187
|
+
return {
|
|
188
|
+
content: [
|
|
189
|
+
{
|
|
190
|
+
type: "text",
|
|
191
|
+
text: "No memories stored yet.",
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
const lines = memories.map((m, i) => `${i + 1}. [${m.memory_id}] ${m.memory_content.slice(0, 150)}`);
|
|
197
|
+
return {
|
|
198
|
+
content: [
|
|
199
|
+
{
|
|
200
|
+
type: "text",
|
|
201
|
+
text: `${memories.length} memories:\n\n${lines.join("\n")}`,
|
|
202
|
+
},
|
|
203
|
+
],
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
// --- Delete Memory ---
|
|
207
|
+
const deleteDesc = TOOL_DESCRIPTIONS[TOOL_NAMES.DELETE_MEMORY];
|
|
208
|
+
server.registerTool(TOOL_NAMES.DELETE_MEMORY, {
|
|
209
|
+
title: deleteDesc.title,
|
|
210
|
+
description: deleteDesc.description,
|
|
211
|
+
inputSchema: {
|
|
212
|
+
memory_id: z.string().describe(deleteDesc.params.memory_id),
|
|
213
|
+
},
|
|
214
|
+
}, async (args) => {
|
|
215
|
+
const { memory_id } = args;
|
|
216
|
+
logger.debug(`${TOOL_NAMES.DELETE_MEMORY}: ${memory_id}`);
|
|
217
|
+
const res = await client.deleteMemory(memory_id);
|
|
218
|
+
if (res.user_memory_deleted) {
|
|
219
|
+
return {
|
|
220
|
+
content: [
|
|
221
|
+
{
|
|
222
|
+
type: "text",
|
|
223
|
+
text: `Deleted memory: ${memory_id}`,
|
|
224
|
+
},
|
|
225
|
+
],
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
content: [
|
|
230
|
+
{
|
|
231
|
+
type: "text",
|
|
232
|
+
text: `Memory ${memory_id} was not found or already deleted.`,
|
|
233
|
+
},
|
|
234
|
+
],
|
|
235
|
+
};
|
|
236
|
+
});
|
|
237
|
+
// --- Fetch Content ---
|
|
238
|
+
const fetchDesc = TOOL_DESCRIPTIONS[TOOL_NAMES.FETCH_CONTENT];
|
|
239
|
+
server.registerTool(TOOL_NAMES.FETCH_CONTENT, {
|
|
240
|
+
title: fetchDesc.title,
|
|
241
|
+
description: fetchDesc.description,
|
|
242
|
+
inputSchema: {
|
|
243
|
+
source_id: z.string().describe(fetchDesc.params.source_id),
|
|
244
|
+
mode: z
|
|
245
|
+
.enum(["content", "url", "both"])
|
|
246
|
+
.optional()
|
|
247
|
+
.describe(fetchDesc.params.mode),
|
|
248
|
+
},
|
|
249
|
+
annotations: {
|
|
250
|
+
readOnlyHint: true,
|
|
251
|
+
idempotentHint: true,
|
|
252
|
+
},
|
|
253
|
+
}, async (args) => {
|
|
254
|
+
const { source_id, mode } = args;
|
|
255
|
+
logger.debug(`${TOOL_NAMES.FETCH_CONTENT}: ${source_id}`);
|
|
256
|
+
const res = await client.fetchContent(source_id, mode ?? "content");
|
|
257
|
+
if (!res.success || res.error) {
|
|
258
|
+
return {
|
|
259
|
+
content: [
|
|
260
|
+
{
|
|
261
|
+
type: "text",
|
|
262
|
+
text: `Could not fetch source ${source_id}: ${res.error ?? "unknown error"}`,
|
|
263
|
+
},
|
|
264
|
+
],
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
const content = res.content ?? res.content_base64 ?? "(no text content)";
|
|
268
|
+
return {
|
|
269
|
+
content: [
|
|
270
|
+
{
|
|
271
|
+
type: "text",
|
|
272
|
+
text: `Source: ${source_id}\n\n${content}`,
|
|
273
|
+
},
|
|
274
|
+
],
|
|
275
|
+
};
|
|
276
|
+
});
|
|
277
|
+
// --- List Sources ---
|
|
278
|
+
const sourcesDesc = TOOL_DESCRIPTIONS[TOOL_NAMES.LIST_SOURCES];
|
|
279
|
+
server.registerTool(TOOL_NAMES.LIST_SOURCES, {
|
|
280
|
+
title: sourcesDesc.title,
|
|
281
|
+
description: sourcesDesc.description,
|
|
282
|
+
inputSchema: {
|
|
283
|
+
source_ids: z
|
|
284
|
+
.array(z.string())
|
|
285
|
+
.optional()
|
|
286
|
+
.describe(sourcesDesc.params.source_ids),
|
|
287
|
+
},
|
|
288
|
+
annotations: {
|
|
289
|
+
readOnlyHint: true,
|
|
290
|
+
idempotentHint: true,
|
|
291
|
+
},
|
|
292
|
+
}, async (args) => {
|
|
293
|
+
const { source_ids } = args;
|
|
294
|
+
logger.debug(TOOL_NAMES.LIST_SOURCES);
|
|
295
|
+
const res = await client.listSources(source_ids);
|
|
296
|
+
if (!res.sources || res.sources.length === 0) {
|
|
297
|
+
return {
|
|
298
|
+
content: [
|
|
299
|
+
{
|
|
300
|
+
type: "text",
|
|
301
|
+
text: "No sources found.",
|
|
302
|
+
},
|
|
303
|
+
],
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
const lines = res.sources.map((s, i) => {
|
|
307
|
+
const title = s.title ? ` — ${s.title}` : "";
|
|
308
|
+
const type = s.type ? ` (${s.type})` : "";
|
|
309
|
+
return `${i + 1}. [${s.id}]${title}${type}`;
|
|
310
|
+
});
|
|
311
|
+
return {
|
|
312
|
+
content: [
|
|
313
|
+
{
|
|
314
|
+
type: "text",
|
|
315
|
+
text: `${res.total} sources:\n\n${lines.join("\n")}`,
|
|
316
|
+
},
|
|
317
|
+
],
|
|
318
|
+
};
|
|
319
|
+
});
|
|
320
|
+
return server.server;
|
|
321
|
+
}
|
|
322
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Single source of truth for MCP tool names.
|
|
2
|
+
export const TOOL_NAMES = {
|
|
3
|
+
SEARCH: "hydra_db_search",
|
|
4
|
+
STORE: "hydra_db_store",
|
|
5
|
+
INGEST_CONVERSATION: "hydra_db_ingest_conversation",
|
|
6
|
+
LIST_MEMORIES: "hydra_db_list_memories",
|
|
7
|
+
DELETE_MEMORY: "hydra_db_delete_memory",
|
|
8
|
+
FETCH_CONTENT: "hydra_db_fetch_content",
|
|
9
|
+
LIST_SOURCES: "hydra_db_list_sources",
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=tool-names.js.map
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hydradb/mcp",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "MCP server for Hydra DB: State-of-the-art agentic memory with recall, ingest, and knowledge graph context",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "dist/index.js",
|
|
8
|
+
"bin": {
|
|
9
|
+
"hydradb-mcp": "dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist/*.js",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"start": "node dist/index.js",
|
|
18
|
+
"dev": "tsx src/index.ts",
|
|
19
|
+
"check-types": "tsc --noEmit",
|
|
20
|
+
"test": "npm run test:unit",
|
|
21
|
+
"test:unit": "tsx --test tests/**/*.test.ts",
|
|
22
|
+
"test:integration": "tsx --test tests/**/*.integration.spec.ts",
|
|
23
|
+
"test:all": "npm run test:unit && npm run test:integration"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@modelcontextprotocol/sdk": "^1.21.1",
|
|
27
|
+
"zod": "^3.25.46"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^20",
|
|
31
|
+
"tsx": "^4.19.4",
|
|
32
|
+
"typescript": "^5.9.3"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|