Skip to content

[BUG] MCP STDIO Transport Fails: console.log in db.ts breaks JSON-RPC protocol #104

@kishan0725

Description

@kishan0725

What happened?

When using OpenMemory as an MCP server via STDIO transport (npx openmemory-js mcp), the connection fails immediately with a JSON parsing error. The root cause is that database initialization logs are written to stdout instead of stderr, violating the MCP STDIO protocol requirement that stdout must contain only valid JSON-RPC messages.

Expected Behavior

  • OpenMemory MCP server starts successfully
  • STDIO transport establishes connection
  • MCP tools are available for use
  • All diagnostic logs go to stderr, not stdout

Actual Behavior

Error message:

Stdio connection lost for "openmemory": 
Unexpected token 'D', "[DB] Using "... is not valid JSON

What's Happening:

The database initialization in db.ts writes to stdout:

[DB] Using Postgres VectorStore with table: "public"."openmemory_vectors"

The MCP client reads stdout expecting JSON-RPC, tries to parse this string as JSON, and fails.

MCP STDIO Protocol Requirements

From the MCP specification:

  • stdout = JSON-RPC messages ONLY (machine-readable)
  • stderr = Diagnostic messages, logs (human-readable)

Any non-JSON text on stdout breaks the protocol.

Why This Happens

The database module (db.ts) initializes eagerly when imported, before the MCP STDIO transport is ready:

// In mcp.ts
import { q, vector_store } from "../core/db";  // ← Triggers db.ts init immediately

The console.log statements execute during module initialization, polluting stdout before MCP can even start.

Proposed Solution

Change all console.log to console.error in database initialization code. Audit other files for similar stdout logging.

Steps to Reproduce

  1. Configure OpenMemory with PostgreSQL:
{
  "mcpServers": {
    "openmemory": {
      "command": "npx",
      "args": ["-y", "openmemory-js", "mcp"],
      "env": {
        "OM_METADATA_BACKEND": "postgres",
        "OM_PG_HOST": "localhost",
        "OM_PG_DB": "openmemory_test",
        "OM_EMBEDDINGS": "aws",
        "AWS_REGION": "us-west-2",
        // ... other config
      }
    }
  }
}
  1. Start MCP client (e.g., Claude Desktop, custom MCP client)

  2. Observe immediate connection failure

Component

AI/LLM Integration

Environment

  • OpenMemory Version: Latest (as of 2025-01-23)

  • Node Version: v23.11.0

  • MCP Client: Any MCP client using STDIO transport

  • Backend: PostgreSQL

  • OS: MacOS

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions