mcp-basic-memory 0.0.0-stage → 0.1.0
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 +21 -0
- package/README.md +26 -2
- package/cli.js +31 -0
- package/package.json +17 -4
- package/proxy.js +191 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kucharskill
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
-
#
|
|
1
|
+
# mcp-basic-memory
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This stdio proxy starts [Basic Memory](https://github.com/basicmachines-co/basic-memory) on demand with `uvx basic-memory@latest mcp`. It requires Node.js 22+ and `uvx` on `PATH`.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npx -y --prefer-online mcp-basic-memory@latest
|
|
7
|
+
npx -y --prefer-online mcp-basic-memory@latest --version
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
A Codex `config.toml` entry:
|
|
11
|
+
|
|
12
|
+
```toml
|
|
13
|
+
[mcp_servers.basic_memory]
|
|
14
|
+
command = "npx"
|
|
15
|
+
args = ["-y", "--prefer-online", "mcp-basic-memory@latest"]
|
|
16
|
+
startup_timeout_sec = 180
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The proxy forwards Basic Memory's MCP messages. It keeps the client connection open when the upstream exits and repeats the MCP handshake on the next request. The latest request selects the idle timeout: one minute for initialization and tool discovery, or thirty minutes for other requests. Outstanding requests keep the upstream running.
|
|
20
|
+
|
|
21
|
+
At shutdown, the proxy closes upstream stdin and gives Basic Memory up to one minute to finish cleanup. If the launcher remains running, the proxy terminates its process tree. The same shutdown applies when the client disconnects.
|
|
22
|
+
|
|
23
|
+
Basic Memory owns project paths, configuration, indexes, model selection and credentials. Existing `BASIC_MEMORY_*` settings are inherited. The proxy supplies a default `FASTEMBED_CACHE_PATH` only when that variable is unset. Basic Memory's explicit `semantic_embedding_cache_dir` takes precedence. The fallback directory is `basic-memory/fastembed` under the platform's user cache (`%LOCALAPPDATA%` on Windows, `~/Library/Caches` on macOS, and `$XDG_CACHE_HOME` or `~/.cache` on Linux). `uv` manages its own environment cache.
|
|
24
|
+
|
|
25
|
+
Basic Memory watches files while its process is running. Downloads, remote embedding providers and cloud access follow the user's Basic Memory settings. The proxy adds no hosted service or per-request fee.
|
|
26
|
+
|
|
27
|
+
Run `npm run check` for automated checks or `npm run release:prepare` to verify and pack an npm archive. GitHub Releases publish through npm Trusted Publishing.
|
package/cli.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { isAbsolute, join } from 'node:path';
|
|
5
|
+
import { StdioProxy } from './proxy.js';
|
|
6
|
+
|
|
7
|
+
const args = process.argv.slice(2);
|
|
8
|
+
if (args.length === 1 && ['--version', '-v'].includes(args[0])) {
|
|
9
|
+
const metadata = JSON.parse(await readFile(new URL('./package.json', import.meta.url), 'utf8'));
|
|
10
|
+
console.log(metadata.version);
|
|
11
|
+
} else if (args.length === 1 && ['--help', '-h'].includes(args[0])) {
|
|
12
|
+
console.log('Usage: mcp-basic-memory [--help | --version]\n\nStart the Basic Memory stdio proxy. Requires Node.js 22+ and uvx on PATH.');
|
|
13
|
+
} else if (args.length) {
|
|
14
|
+
console.error('Unknown argument. Run mcp-basic-memory --help.');
|
|
15
|
+
process.exitCode = 2;
|
|
16
|
+
} else {
|
|
17
|
+
const env = { ...process.env };
|
|
18
|
+
if (!env.FASTEMBED_CACHE_PATH) {
|
|
19
|
+
let cache;
|
|
20
|
+
if (process.platform === 'win32') cache = env.LOCALAPPDATA || join(homedir(), 'AppData', 'Local');
|
|
21
|
+
else if (process.platform === 'darwin') cache = join(homedir(), 'Library', 'Caches');
|
|
22
|
+
else cache = env.XDG_CACHE_HOME && isAbsolute(env.XDG_CACHE_HOME) ? env.XDG_CACHE_HOME : join(homedir(), '.cache');
|
|
23
|
+
env.FASTEMBED_CACHE_PATH = join(cache, 'basic-memory', 'fastembed');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const proxy = new StdioProxy({ command: 'uvx', args: ['basic-memory@latest', 'mcp'], env });
|
|
27
|
+
proxy.connect(process.stdin, process.stdout);
|
|
28
|
+
for (const signal of ['SIGINT', 'SIGTERM']) {
|
|
29
|
+
process.once(signal, () => { process.stdin.destroy(); void proxy.close(); });
|
|
30
|
+
}
|
|
31
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-basic-memory",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "On-demand stdio proxy for Basic Memory with idle shutdown.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {"type": "git", "url": "git+https://github.com/kucharskill/mcp-basic-memory.git"},
|
|
8
|
+
"homepage": "https://github.com/kucharskill/mcp-basic-memory#readme",
|
|
9
|
+
"bugs": {"url": "https://github.com/kucharskill/mcp-basic-memory/issues"},
|
|
10
|
+
"bin": {"mcp-basic-memory": "cli.js"},
|
|
11
|
+
"files": ["cli.js", "proxy.js", "README.md", "LICENSE"],
|
|
12
|
+
"engines": {"node": ">=22"},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"check": "node --check cli.js && node --check proxy.js && node --test test/*.test.js",
|
|
15
|
+
"release:prepare": "npm run check && node scripts/prepare-release.mjs",
|
|
16
|
+
"release:publish": "node scripts/publish-release.mjs"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {"access": "public"}
|
|
19
|
+
}
|
package/proxy.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { once } from 'node:events';
|
|
4
|
+
import { createInterface } from 'node:readline';
|
|
5
|
+
|
|
6
|
+
const discoveryMethods = new Set(['initialize', 'tools/list']);
|
|
7
|
+
const report = message => process.stderr.write(`mcp-basic-memory: ${message}\n`);
|
|
8
|
+
|
|
9
|
+
function killTree(child) {
|
|
10
|
+
if (!child.pid || child.exitCode !== null || child.signalCode !== null) return;
|
|
11
|
+
if (process.platform !== 'win32') {
|
|
12
|
+
try { process.kill(-child.pid, 'SIGKILL'); }
|
|
13
|
+
catch (error) { if (error.code !== 'ESRCH') report(error.message); }
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const killer = spawn('taskkill.exe', ['/PID', String(child.pid), '/T', '/F'], {
|
|
17
|
+
windowsHide: true, stdio: 'ignore',
|
|
18
|
+
});
|
|
19
|
+
killer.on('error', error => {
|
|
20
|
+
report(error.message);
|
|
21
|
+
child.kill();
|
|
22
|
+
});
|
|
23
|
+
killer.on('exit', code => {
|
|
24
|
+
if (code && child.exitCode === null && child.signalCode === null) {
|
|
25
|
+
report(`taskkill exited with code ${code}`);
|
|
26
|
+
child.kill();
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class StdioProxy {
|
|
32
|
+
constructor({ command, args = [], env = process.env, cwd, discoveryMs = 60_000,
|
|
33
|
+
idleMs = 1_800_000, startupMs = 120_000, shutdownMs = 60_000 }) {
|
|
34
|
+
Object.assign(this, { command, args, env, cwd, discoveryMs, idleMs, startupMs, shutdownMs });
|
|
35
|
+
this.pending = new Set();
|
|
36
|
+
this.serverPending = new Set();
|
|
37
|
+
this.delay = discoveryMs;
|
|
38
|
+
this.queued = 0;
|
|
39
|
+
this.queue = Promise.resolve();
|
|
40
|
+
this.closed = false;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
connect(input, output) {
|
|
44
|
+
this.output = output;
|
|
45
|
+
const lines = createInterface({ input, crlfDelay: Infinity });
|
|
46
|
+
lines.on('line', line => {
|
|
47
|
+
this.queued++;
|
|
48
|
+
this.queue = this.queue.then(() => this.receive(line)).catch(error => report(error.message))
|
|
49
|
+
.finally(() => { this.queued--; this.arm(); });
|
|
50
|
+
});
|
|
51
|
+
lines.on('close', () => { void this.close(); });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
sendError(id, code, message) {
|
|
55
|
+
if (!this.closed) this.output.write(JSON.stringify({ jsonrpc: '2.0', id, error: { code, message } }) + '\n');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async receive(line) {
|
|
59
|
+
let message;
|
|
60
|
+
try { message = JSON.parse(line); }
|
|
61
|
+
catch { this.sendError(null, -32700, 'Parse error'); return; }
|
|
62
|
+
if (this.closed) return;
|
|
63
|
+
if (message === null || typeof message !== 'object' || Array.isArray(message)) {
|
|
64
|
+
this.sendError(null, -32600, 'Invalid request');
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const hasId = Object.hasOwn(message, 'id');
|
|
68
|
+
const request = message.method && hasId;
|
|
69
|
+
if (request) {
|
|
70
|
+
clearTimeout(this.timer);
|
|
71
|
+
this.timer = null;
|
|
72
|
+
this.delay = discoveryMethods.has(message.method) ? this.discoveryMs : this.idleMs;
|
|
73
|
+
if (message.method === 'initialize') this.initialize = message;
|
|
74
|
+
}
|
|
75
|
+
if (message.method === 'notifications/initialized') this.initialized = line;
|
|
76
|
+
if (!request && (!this.child || this.stopping)) return;
|
|
77
|
+
try {
|
|
78
|
+
await this.ensureStarted(message.method === 'initialize');
|
|
79
|
+
if (this.closed) return;
|
|
80
|
+
if (!this.child) throw new Error('Basic Memory exited before accepting the request');
|
|
81
|
+
if (request) this.pending.add(message.id);
|
|
82
|
+
else if (hasId) this.serverPending.delete(message.id);
|
|
83
|
+
this.child.stdin.write(line + '\n');
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (request) {
|
|
86
|
+
this.pending.delete(message.id);
|
|
87
|
+
this.sendError(message.id, -32603, error.message);
|
|
88
|
+
} else throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async ensureStarted(initializing) {
|
|
93
|
+
if (this.stopping) await this.stopping;
|
|
94
|
+
if (this.closed || this.child) return;
|
|
95
|
+
const child = spawn(this.command, this.args, {
|
|
96
|
+
cwd: this.cwd, env: this.env, stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true,
|
|
97
|
+
detached: process.platform !== 'win32',
|
|
98
|
+
});
|
|
99
|
+
this.child = child;
|
|
100
|
+
child.stdin.on('error', () => {});
|
|
101
|
+
child.stderr.pipe(process.stderr, { end: false });
|
|
102
|
+
let failure;
|
|
103
|
+
child.on('error', error => { failure = error.message; });
|
|
104
|
+
child.once('close', (code, signal) => this.disconnected(child, failure || `Basic Memory exited (${signal ?? code})`));
|
|
105
|
+
const lines = createInterface({ input: child.stdout, crlfDelay: Infinity });
|
|
106
|
+
lines.on('line', line => this.fromUpstream(line));
|
|
107
|
+
await once(child, 'spawn');
|
|
108
|
+
|
|
109
|
+
if (initializing || !this.initialize) return;
|
|
110
|
+
if (this.closed || this.child !== child) throw new Error('Basic Memory exited during startup');
|
|
111
|
+
const id = `proxy-initialize-${randomUUID()}`;
|
|
112
|
+
this.bootstrapId = id;
|
|
113
|
+
let timer;
|
|
114
|
+
try {
|
|
115
|
+
await new Promise((resolve, reject) => {
|
|
116
|
+
timer = setTimeout(() => reject(new Error('Basic Memory initialization timed out')), this.startupMs);
|
|
117
|
+
this.bootstrap = { id, resolve, reject };
|
|
118
|
+
child.stdin.write(JSON.stringify({ ...this.initialize, id }) + '\n');
|
|
119
|
+
});
|
|
120
|
+
if (this.initialized) child.stdin.write(this.initialized + '\n');
|
|
121
|
+
} catch (error) {
|
|
122
|
+
void this.stop();
|
|
123
|
+
throw error;
|
|
124
|
+
} finally {
|
|
125
|
+
clearTimeout(timer);
|
|
126
|
+
this.bootstrap = null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
fromUpstream(line) {
|
|
131
|
+
let message;
|
|
132
|
+
try { message = JSON.parse(line); }
|
|
133
|
+
catch { report('discarded non-JSON upstream output'); return; }
|
|
134
|
+
if (!message || typeof message !== 'object' || Array.isArray(message)) return;
|
|
135
|
+
if (!message.method && message.id === this.bootstrapId && this.bootstrapId) {
|
|
136
|
+
if (message.error) this.bootstrap?.reject(new Error(message.error.message));
|
|
137
|
+
else this.bootstrap?.resolve();
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (Object.hasOwn(message, 'id')) {
|
|
141
|
+
if (message.method) this.serverPending.add(message.id);
|
|
142
|
+
else this.pending.delete(message.id);
|
|
143
|
+
}
|
|
144
|
+
if (!this.closed) this.output.write(line + '\n');
|
|
145
|
+
this.arm();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
disconnected(child, reason) {
|
|
149
|
+
if (child !== this.child) return;
|
|
150
|
+
this.child = null;
|
|
151
|
+
clearTimeout(this.timer);
|
|
152
|
+
this.timer = null;
|
|
153
|
+
this.bootstrap?.reject(new Error(reason));
|
|
154
|
+
this.bootstrapId = null;
|
|
155
|
+
for (const id of this.pending) this.sendError(id, -32603, reason);
|
|
156
|
+
this.pending.clear();
|
|
157
|
+
this.serverPending.clear();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
arm() {
|
|
161
|
+
if (this.timer || this.closed || this.stopping || !this.child || this.queued ||
|
|
162
|
+
this.pending.size || this.serverPending.size || this.bootstrap) return;
|
|
163
|
+
this.timer = setTimeout(() => {
|
|
164
|
+
this.timer = null;
|
|
165
|
+
if (this.queued || this.pending.size || this.serverPending.size) this.arm();
|
|
166
|
+
else void this.stop();
|
|
167
|
+
}, this.delay);
|
|
168
|
+
this.timer.unref();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async stop() {
|
|
172
|
+
if (this.stopping) return this.stopping;
|
|
173
|
+
const child = this.child;
|
|
174
|
+
if (!child) return;
|
|
175
|
+
clearTimeout(this.timer);
|
|
176
|
+
this.timer = null;
|
|
177
|
+
this.stopping = new Promise(resolve => {
|
|
178
|
+
const timer = setTimeout(() => killTree(child), this.shutdownMs);
|
|
179
|
+
child.once('exit', () => clearTimeout(timer));
|
|
180
|
+
child.once('close', () => { clearTimeout(timer); resolve(); });
|
|
181
|
+
child.stdin.end();
|
|
182
|
+
});
|
|
183
|
+
try { await this.stopping; }
|
|
184
|
+
finally { this.stopping = null; }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async close() {
|
|
188
|
+
this.closed = true;
|
|
189
|
+
await this.stop();
|
|
190
|
+
}
|
|
191
|
+
}
|