@cubicecho/mcp-daemon
v0.1.1
Published
One bin, three run modes for an MCP app: serve over HTTP, run as a local daemon, or act as a stdio MCP server that bridges to the daemon.
Downloads
463
Maintainers
Readme
@cubicecho/mcp-daemon
One bin, three run modes for an MCP app that keeps its own store (a database, a data dir):
| Command | Does |
| --- | --- |
| (none) or stdio | An MCP server on stdin/stdout that forwards to the local daemon, starting it if none runs |
| serve | The app's HTTP server in the foreground (--host, --port) |
| daemon start / stop / status | The same server, detached, on 127.0.0.1 and a free port |
Every stdio session, in every MCP client, shares one daemon, so only one process ever opens the store. That makes an in-process database such as PGlite or SQLite safe behind a stdio MCP server, which would otherwise be started once per session.
MCP client A ─stdio─► bridge ─┐
MCP client B ─stdio─► bridge ─┼─ HTTP /mcp ─► daemon (the app's server) ─► the store
MCP client C ─stdio─► bridge ─┘Using it
The app's bin hands runCli its version, its own flags, where to keep the daemon's files, and how to start its
HTTP server:
#!/usr/bin/env node
import { defaultStateDir, runCli } from '@cubicecho/mcp-daemon';
const code = await runCli({
name: 'my-app',
version: VERSION,
options: { 'data-dir': { type: 'string' } },
stateDir: (values) => defaultStateDir('my-app', dataDirOf(values)),
serve: ({ mode, host, port, bridgeToken }, values) => startServer({ dataDir: dataDirOf(values), host, port, bridgeToken }),
});
process.exit(code);serve must:
- listen on
hostandportwhen given (the daemon passes127.0.0.1and0), and resolve with the port it took and aclose(); - accept
Authorization: Bearer <bridgeToken>on its MCP endpoint, beside whatever auth it already has.
An MCP client config then runs the bin with no command:
{ "mcpServers": { "my-app": { "command": "my-app", "args": ["--data-dir", "/path/to/data"] } } }--url <endpoint> --key <key> makes the bridge forward to a server elsewhere instead, starting nothing.
How it works
- A lock per state dir.
serveand the daemon both takedaemon.lock(a file holding the pid, hard-linked into place) before starting the app, so a foregroundserveand a daemon cannot both open the store. A dead owner's lock is taken over, by one process at a time. - A state file. Once the server listens,
daemon.jsonrecords its pid, URL, version, mode and a random bridge token. It is written owner-only (0600) in an owner-only dir. - Finding or starting the daemon. A bridge reads the state file and checks the process is alive and its server
answers HTTP (any status). If not, it spawns
daemon rundetached, its output appended todaemon.log, and waits for the state file. Two bridges starting at once both spawn; the lock lets one run and the other exit. An owner that is shutting down is waited for, and a start that fails is reported at once with the end of the log. - Upgrades. A new session whose version differs from a running daemon's stops it and starts its own. Sessions
already running move to the new daemon and never replace one. A foreground
serveis used as it is. - The bridge passes JSON-RPC messages unchanged between the SDK's stdio server transport and its streamable HTTP client transport, setting the negotiated protocol version on the HTTP side. Server-to-client notifications reach the client through the HTTP side's SSE stream when the app serves live sessions.
- Reconnecting. When the daemon crashes, stops or is replaced, the bridge connects to the next one and repeats the
client's handshake, resource subscriptions and log level, then sends
list_changedand resource updates so the client fetches again. It looks at the state file every second, and an idle bridge starts nothing: afterdaemon stop, the next request does. Requests under way are sent again once, except a tool call the daemon may have taken: the client gets an error, because the call may have had its effect. With--url, a bridge reconnects when a send fails.
defaultStateDir(app, key) puts the state dir under $XDG_STATE_HOME (else ~/.local/state), named by a hash of
the key. Keep it out of the data dir: that may be a network share, where a pid in a lock file means nothing.
The timeouts and paths are in DAEMON_DEFAULTS; override any of them with settings.
Not yet
- The daemon runs until
daemon stop; it does not exit when idle. - With
--url, a server lost while a request is under way is not noticed until the next send. - After a crash, whichever session sends first starts its own version of the daemon, which may be the older one.
- Windows is untested (detached spawn and signals differ).
