/** * stdio ↔ remote-SSE bridge. * * The MCP client (Cursor, Claude Desktop, etc.) speaks **stdio** MCP — JSON * lines on stdin, JSON lines on stdout. The Walrus Memory relayer speaks **remote * SSE** MCP at `/api/mcp/sse` + `/api/mcp/messages`. This module glues the * two together so the user only adds a `command + args` entry to their MCP * client config (no headers, no URL). * * On 401 from the relayer, we surface a clear error to the MCP client but * leave the local credentials file untouched. A naive `clearCreds()` here * was a creds-wipe DoS: anyone able to coerce a 401 response (transient WAF * rule, future http_proxy MITM, local malware racing the relayer port on * `--local`) would have wiped the user's saved seed without consent. * Re-auth requires an explicit `memwal-mcp login` from the user. */ import type { MemWalCredentials } from "./auth.js"; import { type LoginSuccessInfo } from "./messages.js"; /** Bridge mode runtime config — the URLs / label resolved at boot from * `--dev` / `--staging` / etc. Needed so `memwal_login` (re-auth) opens * the SAME dashboard the user originally signed in to, not the prod default. */ export interface BridgeConfig { relayerUrl: string; webUrl: string; label: string; /** Default memory namespace resolved at boot (`--namespace` / * `MEMWAL_NAMESPACE`). Injected into memory tool calls that omit a * namespace. Undefined → don't inject; the relayer applies its own * "default" namespace. */ namespace?: string; } /** * Inject the configured default namespace into an outbound `tools/call` * message when the agent omitted one. Mutates `msg.params.arguments` in place * and returns `msg` (so it works inline before tracking/forwarding). * * No-op when: * - no default namespace is configured (`namespace` falsy), or * - the message is not a `tools/call` for a namespace-aware memory tool, or * - the caller already supplied a non-empty `namespace` — an explicit * per-call namespace always wins over the configured default. */ /** * Name the destination this process is bound to in a `memwal_health` result: * the relayer it dialled, and the account it signs for. * * The relayer-side text can only report an origin its deployment published, and * stays silent on a self-hosted or local one, where the sidecar knows nothing * but the loopback address it dials. This side always knows the URL it * connected to — it is exactly what `--prod` / `--relayer` / `MEMWAL_SERVER_URL` * selected — so a client bound to the wrong network sees that here instead of * by noticing its memories are missing. * * Rewrites an existing `relayer=` field rather than appending a second one: when * both sides know the origin they describe the same session, and two * conflicting fields would be worse than neither. * * `account=` rides along for the same reason (WALM-639). A project-local * credentials file can point this process at a different account than the one * the user signed in with, and "which account am I writing to" was otherwise * only visible in stderr the MCP host usually hides — so the half of the * destination that decides WHOSE memory this is now shows up beside the half * that decides where it is stored. */ export declare function annotateHealthResult(result: { content?: unknown; isError?: unknown; }, relayerUrl: string, accountId?: string): void; /** * Inject a stable idempotency key into an outbound `tools/call` for a write * tool. Mutates `msg.params.arguments` in place — call BEFORE the message * is stored in `inFlight` so reconnect replays carry the same key. * * No-op when: * - the message is not a `tools/call` for a write tool, or * - the caller already supplied a `_idempotency_key`. */ export declare function applyIdempotencyKey(msg: RpcMessage): RpcMessage; export declare function applyDefaultNamespace(msg: RpcMessage, namespace?: string): RpcMessage; /** Reply for a `tools/call` naming a tool the connected relayer does not * serve. Names what IS on offer, because the agent's next move is to pick one * of those — "unknown tool" alone leaves it guessing or retrying. */ export declare function unknownToolText(name: string, available: string[]): string; interface RpcMessage { jsonrpc: "2.0"; id?: number | string | null; method?: string; params?: unknown; result?: unknown; error?: unknown; } /** Record a completed sign-in for the next tool result to carry. */ export declare function notePendingLoginSuccess(info: LoginSuccessInfo): void; /** * Open the SSE bridge and forward stdio ↔ relayer until stdin closes. * * On SSE drop (idle timeout in the Rust proxy / undici keep-alive / network * blip), we transparently reopen the stream — the relayer issues a fresh * sessionId, we route subsequent POSTs there. stdin stays open the whole * time, so the MCP client (Cursor / Claude Desktop / etc.) never sees the * reconnection. * * Two tools (`memwal_login`, `memwal_logout`) are intercepted LOCALLY and * never forwarded to the relayer — they manipulate the local credentials * file directly. They appear in `tools/list` by splicing them into the * relayer's response on the way back to the client. */ export declare function runBridge(initialCreds: MemWalCredentials, config: BridgeConfig, /** Requests the auth-required server already read off stdin before it * detected fresh credentials and handed control here (e.g. the * `memwal_recall` that triggered the hot-handoff). Replayed once the SSE * stream is up so they're served for real instead of being lost in the * mode switch — this is what removes the historical "second restart". */ pendingLines?: string[]): Promise; export {}; //# sourceMappingURL=bridge.d.ts.map