Skip to content

mcp_pool

mcp_pool

Shared MCP server pool manager for the conductor.

The conductor-side MCP pool starts one process per configured MCP server and, for stdio servers, exposes that process through a multiplexed Unix socket bridge. Multiple MCP clients can connect through the proxy shim; request IDs are rewritten upstream and restored on the way back to the originating client.

Lifecycle: - start_all() — starts all configured servers on daemon startup - stop_all() — terminates all servers on daemon shutdown - health_check(name) — verifies a server process is alive - get_socket_path(name) — returns the socket path for agent access

The manager owns the stdio-to-socket bridge for transport: stdio entries. Native socket/http transports can still use their configured socket/path as a future extension.

Classes

McpServerState

Bases: str, Enum

State of a managed MCP server process.

McpPoolManager

McpPoolManager(config)

Manages MCP server process lifecycle for the conductor.

Reads McpPoolConfig and starts/stops server processes. Tracks state per server and provides socket path lookups for agent sandboxes.

Usage::

manager = McpPoolManager(config.mcp_pool)
await manager.start_all()

# During operation
if await manager.health_check("github"):
    path = manager.get_socket_path("github")
    # Bind-mount path into agent sandbox

# On shutdown
await manager.stop_all()
Source code in src/marianne/daemon/mcp_pool.py
def __init__(self, config: McpPoolConfig) -> None:
    self._config = config
    self._handles: dict[str, _ServerHandle] = {
        name: _ServerHandle(name) for name in config.servers
    }
    self._monitor_task: asyncio.Task[None] | None = None
    self._stopping = False
    self._lifecycle_lock = asyncio.Lock()
Methods:
server_names
server_names()

Return the names of all configured servers.

Source code in src/marianne/daemon/mcp_pool.py
def server_names(self) -> list[str]:
    """Return the names of all configured servers."""
    return list(self._config.servers.keys())
is_running
is_running(name)

Check if a server is currently running.

Reconciles the recorded state against the live process/bridge: a handle recorded as RUNNING whose process has exited (or whose bridge is dead) is transitioned to FAILED as a side effect, so config generation and status inventories exclude the dead server immediately.

Source code in src/marianne/daemon/mcp_pool.py
def is_running(self, name: str) -> bool:
    """Check if a server is currently running.

    Reconciles the recorded state against the live process/bridge: a handle
    recorded as RUNNING whose process has exited (or whose bridge is dead)
    is transitioned to FAILED as a side effect, so config generation and
    status inventories exclude the dead server immediately.
    """
    handle = self._handles.get(name)
    if handle is None or handle.state != McpServerState.RUNNING:
        return False
    alive = (
        handle.bridge.is_running if handle.bridge is not None
        else handle.process is not None and handle.process.returncode is None
    )
    if not alive:
        handle.state = McpServerState.FAILED
    return alive
get_status
get_status()

Get the state of all servers.

Source code in src/marianne/daemon/mcp_pool.py
def get_status(self) -> dict[str, McpServerState]:
    """Get the state of all servers."""
    for name in self._handles:
        self.is_running(name)
    return {name: handle.state for name, handle in self._handles.items()}
get_socket_path
get_socket_path(name)

Get the Unix socket path for a server.

Parameters:

Name Type Description Default
name str

Server name.

required

Returns:

Type Description
Path | None

Path to the Unix socket, or None if the server is not configured.

Source code in src/marianne/daemon/mcp_pool.py
def get_socket_path(self, name: str) -> Path | None:
    """Get the Unix socket path for a server.

    Args:
        name: Server name.

    Returns:
        Path to the Unix socket, or None if the server is not configured.
    """
    entry = self._config.servers.get(name)
    if entry is None:
        return None
    return Path(entry.socket)
get_all_socket_paths
get_all_socket_paths()

Get socket paths for all configured servers.

Source code in src/marianne/daemon/mcp_pool.py
def get_all_socket_paths(self) -> dict[str, Path]:
    """Get socket paths for all configured servers."""
    return {
        name: Path(entry.socket)
        for name, entry in self._config.servers.items()
    }
start_all async
start_all()

Start the pool once and supervise it until stop_all().

Source code in src/marianne/daemon/mcp_pool.py
async def start_all(self) -> None:
    """Start the pool once and supervise it until stop_all()."""
    async with self._lifecycle_lock:
        if self._monitor_task is not None and not self._monitor_task.done():
            return
        self._stopping = False
        try:
            for name, handle in self._handles.items():
                handle.restart_attempts = 0
                handle.next_restart_at = 0.0
                await self._start_server(name)
        except BaseException:
            await self._stop_all()
            raise
        if self._handles:
            self._monitor_task = asyncio.create_task(self._monitor(), name="mcp-pool-monitor")
stop_all async
stop_all()

Cancel and join supervision before stopping any server.

Source code in src/marianne/daemon/mcp_pool.py
async def stop_all(self) -> None:
    """Cancel and join supervision before stopping any server."""
    async with self._lifecycle_lock:
        await self._stop_all()
health_check async
health_check(name)

Check if a server process is alive.

Parameters:

Name Type Description Default
name str

Server name.

required

Returns:

Type Description
bool

True if the server process is running, False otherwise.

Source code in src/marianne/daemon/mcp_pool.py
async def health_check(self, name: str) -> bool:
    """Check if a server process is alive.

    Args:
        name: Server name.

    Returns:
        True if the server process is running, False otherwise.
    """
    handle = self._handles.get(name)
    if handle is None:
        return False
    if handle.bridge is not None:
        return handle.bridge.is_running
    if handle.process is None:
        return False
    # returncode is None while the process is running
    return handle.process.returncode is None
generate_mcp_config_file
generate_mcp_config_file(workspace, *, server_names=None)

Generate an MCP config JSON file for MCP-native CLI instruments.

Produces a file compatible with claude-code's --mcp-config flag, pointing each server at the proxy shim which bridges to the pool's Unix sockets.

The config file is written atomically (write-to-temp + rename) to prevent partial reads by concurrent processes.

Parameters:

Name Type Description Default
workspace Path

Directory where the config file is written.

required
server_names list[str] | None

Optional subset of servers to include. If None, all running servers are included.

None

Returns:

Type Description
Path | None

Path to the generated config file, or None if no servers are

Path | None

running.

Source code in src/marianne/daemon/mcp_pool.py
def generate_mcp_config_file(
    self,
    workspace: Path,
    *,
    server_names: list[str] | None = None,
) -> Path | None:
    """Generate an MCP config JSON file for MCP-native CLI instruments.

    Produces a file compatible with claude-code's ``--mcp-config`` flag,
    pointing each server at the proxy shim which bridges to the pool's
    Unix sockets.

    The config file is written atomically (write-to-temp + rename) to
    prevent partial reads by concurrent processes.

    Args:
        workspace: Directory where the config file is written.
        server_names: Optional subset of servers to include. If None,
            all running servers are included.

    Returns:
        Path to the generated config file, or None if no servers are
        running.
    """
    names = server_names or list(self._handles.keys())

    servers_config: dict[str, object] = {}
    for name in names:
        if not self.is_running(name):
            continue
        socket_path = self.get_socket_path(name)
        if socket_path is None:
            continue
        servers_config[name] = {
            "command": sys.executable,
            "args": [
                "-m", "marianne.daemon.mcp_proxy_shim",
                str(socket_path),
            ],
        }

    if not servers_config:
        return None

    config_data = {"mcpServers": servers_config}
    config_path = workspace / ".mcp-pool-config.json"
    tmp_path = config_path.with_suffix(".tmp")
    tmp_path.parent.mkdir(parents=True, exist_ok=True)
    tmp_path.write_text(json.dumps(config_data, indent=2))
    tmp_path.rename(config_path)

    _logger.debug(
        "mcp_pool.config_file_generated",
        extra={
            "path": str(config_path),
            "servers": list(servers_config.keys()),
        },
    )
    return config_path

Functions: