ai-devkit icon indicating copy to clipboard operation
ai-devkit copied to clipboard

memory: MCP server exits silently when started via `npx @ai-devkit/memory` (bin symlink fails main-module check)

Open PhucChuHoang opened this issue 2 months ago • 0 comments

Summary

On macOS, the memory MCP server set up by ai-devkit init (and documented in the @ai-devkit/memory README) never starts. The process exits with code 0 and no output, so the MCP client just reports the connection as closed (Claude Code: Failed to reconnect to memory: CONNECTION_CLOSED).

Cause

packages/memory/src/index.ts only starts the server when the file is the main module:

if (process.argv[1] === fileURLToPath(import.meta.url)) {
  runServer()...
}

npx and global installs launch the package through the ai-devkit-memory bin symlink (node_modules/.bin/ai-devkit-memory → ../@ai-devkit/memory/dist/index.js). process.argv[1] is the symlink path, while import.meta.url is the real file path, because Node resolves symlinks for the main module. They never match, so runServer() is never called.

Steps to reproduce

INIT='{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"t","version":"1"}}}'

# Through the bin symlink: no output, exits immediately
(echo "$INIT"; sleep 5) | npx -y @ai-devkit/memory

# Through the real file: responds as expected
npm i -g @ai-devkit/memory
(echo "$INIT"; sleep 5) | node "$(npm root -g)/@ai-devkit/memory/dist/index.js"
# {"result":{"protocolVersion":"2025-06-18","capabilities":{"tools":{}},"serverInfo":{"name":"ai-devkit-memory",...}},"jsonrpc":"2.0","id":1}

Expected

npx @ai-devkit/memory starts the stdio MCP server, as the README and ai-devkit init config assume.

Suggested fix

Compare real paths:

import { realpathSync } from "fs";
import { fileURLToPath } from "url";

const isMain =
  !!process.argv[1] &&
  realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));

if (isMain) { runServer()... }

Or move the bin to a separate entry file (e.g. dist/cli.js) that always calls runServer(), and keep index.js as the library entry.

Workaround

Point the MCP config at the real file instead of npx:

{ "command": "node", "args": ["<npm root -g>/@ai-devkit/memory/dist/index.js"] }

Environment

  • @ai-devkit/memory 0.20.1 (also seen on 0.12.0)
  • ai-devkit 0.65.0
  • Node v22.20.0 (nvm)
  • macOS 14.3.1
  • MCP client: Claude Code

Possibly related: #40

PhucChuHoang avatar Sep 29 '26 14:09 PhucChuHoang