Skip to content

MCP gateway

Each sandbox has an MCP gateway: the agent in the sandbox reaches the MCP servers the gateway serves at one URL, http://mcp-gateway.docker.internal/mcp. The servers are registered on the host with the sbx CLI (sbx mcp add), then loaded into the gateway of a sandbox on demand. This page covers the settings of the daemon, the gateway of a sandbox, and the static and dynamic gateways.

An agent can also be given MCP servers in its own configuration, without the gateway, the daemon or the CLI: a server of the host, a remote one, or a program it starts in the sandbox, registered by a mixin for claude, copilot and opencode (see MCP servers for the agent).

docs_src/client/mcp.py
import fastsandbox
from fastsandbox.models import McpServerCheck


async def main() -> None:
    client = await fastsandbox.connect()
    mode = await client.mcp.gateway_mode()
    print(f"gateway: {mode.decision}")

    # Whether the MCP policy lets `sbx mcp add` register this server.
    server = McpServerCheck.remote("docs-tools", "https://mcp.example.com/mcp")
    check = await client.mcp.check_registration(server)
    print(f"{check.name} may be registered: {check.allowed}")

    async with client.create(kit="shell") as sandbox, sandbox.running():
        # A server registered on the host with `sbx mcp add docs-tools ...`.
        await sandbox.load_mcp_server("docs-tools")
        gateway = await sandbox.mcp_gateway()
        print(f"{gateway.url} serves {gateway.servers}")
        await sandbox.unload_mcp_server("docs-tools")


if __name__ == "__main__":
    fastsandbox.run(main())

The settings of the daemon

client.mcp reaches what the daemon decides for every sandbox:

  • gateway_mode() says which gateway runs the servers, a GatewayMode: its decision, local for instance, and why;
  • check_registration(server) says whether the MCP policy lets a server be registered, before sbx mcp add does it: a local server, McpServerCheck.local(name, command), or a remote one, McpServerCheck.remote(name, url). Nothing is registered.

The gateway of a sandbox

Call What it does
mcp_gateway() the gateway, a McpGateway: its state, its url inside the sandbox, its port on the host, whether its servers are static, and the servers loaded
load_mcp_server(server) loads a registered server into the gateway; NotFoundError when it is not registered
unload_mcp_server(server) unloads it; NotFoundError when it is not loaded

The gateway answers inside the sandbox only: its port on the host answers 401 Unauthorized to a request that does not come through the proxy of the sandbox, which adds the credential of the gateway.

Static and dynamic gateways

Besides the tools of its servers, a gateway offers tools of its own. By default it is dynamic: mcp-find searches the servers registered on the host and mcp-add loads one, so the agent in the sandbox can load any of them by itself; a server added this way stays loaded, in later sessions too, until the program unloads it.

A sandbox created with a static set of servers, as sbx create --static-mcp does, offers neither mcp-find nor mcp-add: only its servers, and mcp-exec and code-mode. The program keeps the power to load and unload servers through the socket.

package = await client.package("shell")
request = package.request("my-sandbox").with_static_mcp_servers("docs-tools")
sandbox = await client.create(kit=package, request=request)

Tool names are not prefixed by server: mcp-add refuses a server whose tool names are already taken. These behaviors were measured on sbx v0.54.0 by the MCP examples (see Examples).

Credentials of remote servers

A remote server that needs a header, a bearer token for instance, is registered with a placeholder in that header, and the value is stored as the sbx secret mcp:<server>:<placeholder>. The gateway fills the header when it connects to the server: the server sees the token, the sandbox never holds it. A gateway keeps its connection: after the secret changes, unload the server and load it again for the gateway to use the new value.