Fix MCP server startup for Claude Code compatibility

- Suppress stderr output (Claude Code rejects servers with stderr)
- Make SyncEngine.start() non-blocking (blocking sync caused 15s+ timeout)
- Skip Content-Length header lines gracefully (NDJSON transport)
- Silently ignore malformed JSON lines instead of sending error responses
This commit is contained in:
Viktor Barzin 2026-03-14 16:05:35 +00:00
parent be9e6352c3
commit a52afe050d
No known key found for this signature in database
GPG key ID: 0EB088298288D958
2 changed files with 8 additions and 18 deletions

View file

@ -600,19 +600,11 @@ class MemoryServer:
try:
for line in sys.stdin:
line = line.strip()
if not line:
if not line or line.startswith("Content-Length:"):
continue
try:
message = json.loads(line)
except json.JSONDecodeError as e:
print(
json.dumps({
"jsonrpc": "2.0",
"id": None,
"error": {"code": -32700, "message": f"Parse error: {e}"},
}),
flush=True,
)
except json.JSONDecodeError:
continue
response = self.process_message(message)
if response is not None:
@ -623,6 +615,11 @@ class MemoryServer:
def main() -> None:
# Suppress all stderr output — MCP clients (e.g. Claude Code) may treat
# any stderr as a fatal error and refuse to load the server.
sys.stderr = open(os.devnull, "w")
logging.disable(logging.CRITICAL)
server = MemoryServer()
server.run()

View file

@ -87,14 +87,7 @@ class SyncEngine:
return self._last_sync_success
def start(self) -> None:
"""Run initial sync (blocking), then start background thread."""
try:
self._sync_once()
self._last_sync_success = True
except Exception:
logger.warning("Initial sync failed, starting in offline mode")
self._last_sync_success = False
"""Start background sync thread (non-blocking)."""
self._thread = threading.Thread(target=self._sync_loop, daemon=True)
self._thread.start()