Skip to content

Commit 649af58

Browse files
ameyypawarolaservo
andauthored
fix(memory): expand leading ~ in MEMORY_FILE_PATH to the home directory (#4447)
* fix(memory): expand leading ~ in MEMORY_FILE_PATH to the home directory MCP clients pass MEMORY_FILE_PATH from JSON config, where no shell expands a leading "~". Because path.isAbsolute("~/memory.jsonl") is false, the value was joined onto the package directory, so the server persisted to a literal "~" folder inside the install instead of the user's intended location. Add an expandHome() helper that mirrors the one already used by the filesystem server (src/filesystem/path-utils.ts) and apply it in ensureMemoryFilePath() before the existing absolute/relative resolution. Absolute paths, relative paths, and a "~" not at the start are unaffected. Adds unit tests for expandHome plus an end-to-end test through ensureMemoryFilePath. Addresses #1600 * test(memory): use a literal ~/ path in the tilde expansion test path.join('~', ...) produces a backslash on Windows, which expandHome does not match, so the test failed there. --------- Co-authored-by: olaservo <[email protected]>
1 parent 242751e commit 649af58

2 files changed

Lines changed: 56 additions & 5 deletions

File tree

‎src/memory/__tests__/file-path.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
22
import { promises as fs } from 'fs';
33
import path from 'path';
4+
import os from 'os';
45
import { fileURLToPath } from 'url';
5-
import { ensureMemoryFilePath, defaultMemoryPath } from '../index.js';
6+
import { ensureMemoryFilePath, defaultMemoryPath, expandHome } from '../index.js';
67

78
describe('ensureMemoryFilePath', () => {
89
const testDir = path.dirname(fileURLToPath(import.meta.url));
@@ -72,6 +73,15 @@ describe('ensureMemoryFilePath', () => {
7273
expect(path.isAbsolute(result)).toBe(true);
7374
}
7475
});
76+
77+
it('should expand a leading "~/" to the home directory', async () => {
78+
process.env.MEMORY_FILE_PATH = '~/custom-memory.jsonl';
79+
80+
const result = await ensureMemoryFilePath();
81+
82+
expect(result).toBe(path.join(os.homedir(), 'custom-memory.jsonl'));
83+
expect(path.isAbsolute(result)).toBe(true);
84+
});
7585
});
7686

7787
describe('without MEMORY_FILE_PATH environment variable', () => {
@@ -154,3 +164,29 @@ describe('ensureMemoryFilePath', () => {
154164
});
155165
});
156166
});
167+
168+
describe('expandHome', () => {
169+
it('expands a bare "~" to the home directory', () => {
170+
expect(expandHome('~')).toBe(os.homedir());
171+
});
172+
173+
it('expands a leading "~/" to the home directory', () => {
174+
expect(expandHome('~/notes/memory.jsonl')).toBe(
175+
path.join(os.homedir(), 'notes/memory.jsonl')
176+
);
177+
});
178+
179+
it('leaves a "~" not followed by a separator unchanged', () => {
180+
expect(expandHome('~backup.jsonl')).toBe('~backup.jsonl');
181+
});
182+
183+
it('leaves absolute paths unchanged', () => {
184+
expect(expandHome('/var/data/memory.jsonl')).toBe('/var/data/memory.jsonl');
185+
});
186+
187+
it('leaves relative paths unchanged', () => {
188+
expect(expandHome(path.join('data', 'memory.jsonl'))).toBe(
189+
path.join('data', 'memory.jsonl')
190+
);
191+
});
192+
});

‎src/memory/index.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,35 @@ import { SubscribeRequestSchema, UnsubscribeRequestSchema } from "@modelcontextp
66
import { z } from "zod";
77
import { promises as fs } from 'fs';
88
import path from 'path';
9+
import os from 'os';
910
import { randomBytes } from 'crypto';
1011
import { fileURLToPath } from 'url';
1112
import { SERVER_VERSION } from './version.js';
1213

1314
// Define memory file path using environment variable with fallback
1415
export const defaultMemoryPath = path.join(path.dirname(fileURLToPath(import.meta.url)), 'memory.jsonl');
1516

17+
// Expand a leading "~" to the user's home directory. MCP clients pass
18+
// MEMORY_FILE_PATH from JSON config, where no shell performs tilde expansion,
19+
// so an unexpanded "~" would otherwise be treated as a relative path and
20+
// joined onto the package directory. Mirrors the helper of the same name in
21+
// the filesystem server (src/filesystem/path-utils.ts).
22+
export function expandHome(filepath: string): string {
23+
if (filepath.startsWith('~/') || filepath === '~') {
24+
return path.join(os.homedir(), filepath.slice(1));
25+
}
26+
return filepath;
27+
}
28+
1629
// Handle backward compatibility: migrate memory.json to memory.jsonl if needed
1730
export async function ensureMemoryFilePath(): Promise<string> {
1831
if (process.env.MEMORY_FILE_PATH) {
19-
// Custom path provided, use it as-is (with absolute path resolution)
20-
return path.isAbsolute(process.env.MEMORY_FILE_PATH)
21-
? process.env.MEMORY_FILE_PATH
22-
: path.join(path.dirname(fileURLToPath(import.meta.url)), process.env.MEMORY_FILE_PATH);
32+
// Custom path provided. Expand a leading "~" first, then resolve relative
33+
// paths against the package directory (absolute paths are used as-is).
34+
const customPath = expandHome(process.env.MEMORY_FILE_PATH);
35+
return path.isAbsolute(customPath)
36+
? customPath
37+
: path.join(path.dirname(fileURLToPath(import.meta.url)), customPath);
2338
}
2439

2540
// No custom path set, check for backward compatibility migration

0 commit comments

Comments
 (0)