# server_session

> **Function** in `deepagents_code`

📖 [View in docs](https://reference.langchain.com/python/deepagents-code/client/launch/server_manager/server_session)

Async context manager that starts a server and guarantees cleanup.

Wraps `start_server_and_get_agent` so callers don't need to duplicate the
try/finally pattern for stopping the server.

## Signature

```python
server_session(
    *,
    assistant_id: str,
    model_name: str | None = None,
    model_params: dict[str, Any] | None = None,
    profile_overrides: dict[str, Any] | None = None,
    auto_approve: bool = False,
    interrupt_shell_only: bool = False,
    shell_allow_list: list[str] | None = None,
    sandbox_type: str = 'none',
    sandbox_id: str | None = None,
    sandbox_snapshot_name: str | None = None,
    sandbox_setup: str | None = None,
    enable_shell: bool = True,
    enable_ask_user: bool = False,
    enable_interpreter: bool | None = None,
    interpreter_ptc: str | list[str] | None = None,
    interpreter_ptc_acknowledge_unsafe: bool = False,
    allow_fs_tools: list[FsToolName] | None = None,
    rubric_model: str | None = None,
    rubric_max_iterations: int | None = None,
    mcp_config_path: str | None = None,
    no_mcp: bool = False,
    trust_project_mcp: bool | None = None,
    interactive: bool = True,
    host: str = '127.0.0.1',
    port: int = _EPHEMERAL_PORT,
) -> AsyncIterator[tuple[RemoteAgent, ServerProcess]]
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `assistant_id` | `str` | Yes | Agent identifier. |
| `model_name` | `str \| None` | No | Model spec string. (default: `None`) |
| `model_params` | `dict[str, Any] \| None` | No | Extra model kwargs. (default: `None`) |
| `profile_overrides` | `dict[str, Any] \| None` | No | Model profile metadata overrides. (default: `None`) |
| `auto_approve` | `bool` | No | Auto-approve all tools. (default: `False`) |
| `interrupt_shell_only` | `bool` | No | Validate shell commands via middleware instead of HITL. (default: `False`) |
| `shell_allow_list` | `list[str] \| None` | No | Restrictive shell allow-list for `ShellAllowListMiddleware`. (default: `None`) |
| `sandbox_type` | `str` | No | Sandbox type. (default: `'none'`) |
| `sandbox_id` | `str \| None` | No | Existing sandbox ID to reuse. (default: `None`) |
| `sandbox_snapshot_name` | `str \| None` | No | Snapshot (langsmith) or blueprint (runloop) name. (default: `None`) |
| `sandbox_setup` | `str \| None` | No | Path to setup script for the sandbox. (default: `None`) |
| `enable_shell` | `bool` | No | Enable shell execution tools. (default: `True`) |
| `enable_ask_user` | `bool` | No | Enable ask_user tool. (default: `False`) |
| `enable_interpreter` | `bool \| None` | No | Enable the JS interpreter (`js_eval`) middleware on the main agent. `None` uses the sandbox-aware default. (default: `None`) |
| `interpreter_ptc` | `str \| list[str] \| None` | No | Override for `settings.interpreter_ptc` (PTC allowlist). (default: `None`) |
| `interpreter_ptc_acknowledge_unsafe` | `bool` | No | Explicit acknowledgement for `interpreter_ptc="all"` outside of `auto_approve`. (default: `False`) |
| `allow_fs_tools` | `list[FsToolName] \| None` | No | Allowlist for `FilesystemMiddleware`'s `tools` param.  `None` leaves the SDK default (all tools). (default: `None`) |
| `rubric_model` | `str \| None` | No | Grader model spec; `None` reuses the main model. (default: `None`) |
| `rubric_max_iterations` | `int \| None` | No | Explicit grader iterations per rubric attempt; `None` uses the SDK default. (default: `None`) |
| `mcp_config_path` | `str \| None` | No | Path to MCP config. (default: `None`) |
| `no_mcp` | `bool` | No | Disable MCP. (default: `False`) |
| `trust_project_mcp` | `bool \| None` | No | Trust project MCP servers. (default: `None`) |
| `interactive` | `bool` | No | Whether the agent is interactive. (default: `True`) |
| `host` | `str` | No | Server host. (default: `'127.0.0.1'`) |
| `port` | `int` | No | Server port. Defaults to `_EPHEMERAL_PORT` (0), letting the server pick a free ephemeral port instead of the well-known `langgraph dev` port 2024. (default: `_EPHEMERAL_PORT`) |

---

[View source on GitHub](https://github.com/langchain-ai/deepagents/blob/7794b61a6e76230e8c7a49bdce808b3728305914/libs/code/deepagents_code/client/launch/server_manager.py#L449)