Skip to content

mcp_proxy_shim

mcp_proxy_shim

stdio-to-socket MCP proxy shim.

A lightweight bridge that forwards MCP JSON-RPC messages between a CLI instrument's stdin/stdout and the conductor's shared MCP pool Unix socket.

The proxy is spawned as a subprocess by MCP-native CLI instruments (e.g. claude-code via --mcp-config). It reads JSON-RPC from stdin, forwards to the pool's Unix socket, reads responses from the socket, and writes them to stdout.

Usage::

python -m marianne.daemon.mcp_proxy_shim /tmp/mzt/mcp/github.sock

Or invoked automatically via MCP config JSON::

{"mcpServers": {"github": {
    "command": "python3",
    "args": ["-m", "marianne.daemon.mcp_proxy_shim",
             "/tmp/mzt/mcp/github.sock"]
}}}

Design constraints: - Minimal dependencies (stdlib only — no Pydantic, no Marianne imports) - Async I/O throughout for non-blocking forwarding - Graceful error handling (log to stderr, exit non-zero on failure) - Bidirectional: stdin -> socket AND socket -> stdout run concurrently

Functions:

forward_stdin_to_socket async

forward_stdin_to_socket(reader, writer)

Forward data from stdin to the Unix socket.

Reads chunks from stdin and writes them to the socket connection. Closes the socket write side when stdin is exhausted (EOF).

Parameters:

Name Type Description Default
reader StreamReader

asyncio StreamReader wrapping stdin.

required
writer StreamWriter

asyncio StreamWriter connected to the Unix socket.

required
Source code in src/marianne/daemon/mcp_proxy_shim.py
async def forward_stdin_to_socket(
    reader: asyncio.StreamReader,
    writer: asyncio.StreamWriter,
) -> None:
    """Forward data from stdin to the Unix socket.

    Reads chunks from stdin and writes them to the socket connection.
    Closes the socket write side when stdin is exhausted (EOF).

    Args:
        reader: asyncio StreamReader wrapping stdin.
        writer: asyncio StreamWriter connected to the Unix socket.
    """
    try:
        while True:
            data = await reader.read(65536)
            if not data:
                if writer.can_write_eof():
                    writer.write_eof()
                break
            writer.write(data)
            await writer.drain()
    except (ConnectionResetError, BrokenPipeError):
        pass
    except Exception as exc:  # noqa: BLE001 — proxy task reports the stream failure on stderr
        print(f"mcp_proxy_shim: stdin->socket error: {exc}", file=sys.stderr)

forward_socket_to_stdout async

forward_socket_to_stdout(reader, stdout_writer)

Forward data from the Unix socket to stdout.

Reads chunks from the socket connection and writes them to stdout.

Parameters:

Name Type Description Default
reader StreamReader

asyncio StreamReader connected to the Unix socket.

required
stdout_writer StreamWriter

asyncio StreamWriter wrapping stdout.

required
Source code in src/marianne/daemon/mcp_proxy_shim.py
async def forward_socket_to_stdout(
    reader: asyncio.StreamReader,
    stdout_writer: asyncio.StreamWriter,
) -> None:
    """Forward data from the Unix socket to stdout.

    Reads chunks from the socket connection and writes them to stdout.

    Args:
        reader: asyncio StreamReader connected to the Unix socket.
        stdout_writer: asyncio StreamWriter wrapping stdout.
    """
    try:
        while True:
            data = await reader.read(65536)
            if not data:
                break
            stdout_writer.write(data)
            await stdout_writer.drain()
    except (ConnectionResetError, BrokenPipeError):
        pass
    except Exception as exc:  # noqa: BLE001 — proxy task reports the stream failure on stderr
        print(f"mcp_proxy_shim: socket->stdout error: {exc}", file=sys.stderr)

run_proxy async

run_proxy(socket_path)

Run the stdio-to-socket proxy.

Connects to the Unix socket at socket_path, then concurrently forwards stdin->socket and socket->stdout until either side closes.

Parameters:

Name Type Description Default
socket_path str

Path to the Unix domain socket.

required

Returns:

Type Description
int

Exit code: 0 on success, 1 on connection failure.

Source code in src/marianne/daemon/mcp_proxy_shim.py
async def run_proxy(socket_path: str) -> int:
    """Run the stdio-to-socket proxy.

    Connects to the Unix socket at ``socket_path``, then concurrently
    forwards stdin->socket and socket->stdout until either side closes.

    Args:
        socket_path: Path to the Unix domain socket.

    Returns:
        Exit code: 0 on success, 1 on connection failure.
    """
    path = Path(socket_path)

    if not path.exists():
        print(
            f"mcp_proxy_shim: socket not found: {socket_path}",
            file=sys.stderr,
        )
        return 1

    try:
        sock_reader, sock_writer = await asyncio.open_unix_connection(
            str(path),
        )
    except (ConnectionRefusedError, FileNotFoundError, OSError) as exc:
        print(
            f"mcp_proxy_shim: cannot connect to {socket_path}: {exc}",
            file=sys.stderr,
        )
        return 1

    # Wrap stdin/stdout as async streams
    loop = asyncio.get_running_loop()
    stdin_reader = asyncio.StreamReader()
    await loop.connect_read_pipe(
        lambda: asyncio.StreamReaderProtocol(stdin_reader),
        sys.stdin.buffer,
    )

    stdout_transport, stdout_protocol = await loop.connect_write_pipe(
        asyncio.streams.FlowControlMixin,
        sys.stdout.buffer,
    )
    stdout_writer = asyncio.StreamWriter(
        stdout_transport,
        stdout_protocol,
        None,
        loop,
    )

    # Run both directions concurrently
    stdin_task = asyncio.create_task(
        forward_stdin_to_socket(stdin_reader, sock_writer),
    )
    stdout_task = asyncio.create_task(
        forward_socket_to_stdout(sock_reader, stdout_writer),
    )

    try:
        done, _pending = await asyncio.wait(
            {stdin_task, stdout_task},
            return_when=asyncio.ALL_COMPLETED,
        )
    finally:
        sock_writer.close()
        try:
            await sock_writer.wait_closed()
        except Exception:  # noqa: BLE001 — teardown of an already-closing socket; the copy loops reported their own errors
            pass

    for task in done:
        task_exc = task.exception()
        if task_exc is not None:
            print(
                f"mcp_proxy_shim: forwarding error: {task_exc}",
                file=sys.stderr,
            )
            return 1

    return 0

main

main()

Entry point for the proxy shim.

Source code in src/marianne/daemon/mcp_proxy_shim.py
def main() -> None:
    """Entry point for the proxy shim."""
    if len(sys.argv) != 2:
        print(
            "Usage: python -m marianne.daemon.mcp_proxy_shim <socket_path>",
            file=sys.stderr,
        )
        sys.exit(2)

    exit_code = asyncio.run(run_proxy(sys.argv[1]))
    sys.exit(exit_code)