Skip to content

fix(memory): expand leading ~ in MEMORY_FILE_PATH to the home directory#4447

Open
ameyypawar wants to merge 1 commit into
modelcontextprotocol:mainfrom
ameyypawar:fix/memory-tilde-home-expansion
Open

fix(memory): expand leading ~ in MEMORY_FILE_PATH to the home directory#4447
ameyypawar wants to merge 1 commit into
modelcontextprotocol:mainfrom
ameyypawar:fix/memory-tilde-home-expansion

Conversation

@ameyypawar

Copy link
Copy Markdown

Summary

@modelcontextprotocol/server-memory ignores a MEMORY_FILE_PATH that begins with ~ (e.g. ~/memory.jsonl). MCP clients pass this variable from JSON config (Claude Desktop, Cursor, Roo Cline, VS Code), where no shell performs tilde expansion. Because path.isAbsolute("~/memory.jsonl") is false, ensureMemoryFilePath() joined the value onto the package directory, so the server persisted to a literal ~ folder inside the install instead of the user's intended location.

Addresses #1600.

Change

  • src/memory/index.ts: add an expandHome() helper and call it in ensureMemoryFilePath() before the existing absolute/relative resolution. A leading ~ or ~/ expands to os.homedir(); absolute paths, relative paths, and a ~ that is not at the start are unchanged.

The helper mirrors the one already used by the filesystem server (src/filesystem/path-utils.ts), keeping tilde handling consistent across the official servers.

Tests

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

  • expandHome unit tests: bare ~, ~/…, a ~ not followed by a separator (unchanged), and absolute/relative passthrough.
  • ensureMemoryFilePath end-to-end: ~/custom-memory.jsonl resolves under os.homedir().

Verified locally on this branch: npm test → 56/56 passing, npm run build (tsc) clean.

Scope

Tilde expansion only. The ${workspaceFolder} variable substitution discussed in #1846 is a client-side concern, and #1481 is about the JSONL storage format — neither is addressed here.

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 modelcontextprotocol#1600
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant