Skip to main content

Overview

Sessions represent independent conversation contexts with their own history and state. Each session has a unique ID and maintains its own conversation history, working directory, and MCP server connections.

Creating Sessions

The newSession method creates a new conversation session:
string
Optional working directory for the session
MCPServerConfig[]
MCP servers to connect to for this session
string
required
A unique identifier for the new session
Mode[]
Optional list of available modes for this session
string
The initial mode for this session
See protocol docs: Session Setup

Example

May throw an auth_required error if the agent requires authentication before creating sessions.

Loading Sessions

The optional loadSession method loads an existing session to resume a previous conversation:
string
required
The session ID to load
MCPServerConfig[]
MCP servers to connect to for this session
Mode[]
Optional list of available modes for this session
string
The current mode for this session
This method is only available if the agent advertises the loadSession capability during initialization. The agent should:
  • Restore the session context and conversation history
  • Connect to the specified MCP servers
  • Stream the entire conversation history back to the client via sessionUpdate notifications
See protocol docs: Loading Sessions

Example

Session Modes

Session modes allow switching between different agent behaviors (e.g., “ask”, “architect”, “code”) that affect system prompts, tool availability, and permission behaviors.
string
required
The session ID to update
string
required
The mode to switch to (must be one of the advertised modes)
The mode must be one of the modes advertised in availableModes during session creation or loading. Agents may also change modes autonomously and notify the client via current_mode_update notifications. This method can be called at any time during a session, whether the agent is idle or actively generating a turn. See protocol docs: Session Modes

Example

Forking and Resuming Sessions (Unstable)

These methods are marked as UNSTABLE and may be removed or changed at any point.

Forking Sessions

The unstable_forkSession method creates a new session based on an existing one:
string
required
The session ID to fork from
MCPServerConfig[]
MCP servers to connect to for the new session
string
required
The ID of the newly created session
This allows operations like generating summaries without affecting the original session’s history.

Example

Resuming Sessions

The unstable_resumeSession method resumes an existing session without replaying the message history:
string
required
The session ID to resume
MCPServerConfig[]
MCP servers to connect to for this session
Unlike loadSession, this method does not stream the conversation history to the client.

Example

Session Persistence

To support session loading, you’ll need to persist session data:

Best Practices

Generate Unique IDs

Use crypto.randomUUID() or similar to generate unique session IDs

Clean Up Resources

Disconnect from MCP servers and clean up resources when sessions end

Persist State

Save session state after each prompt to support session loading

Validate Modes

Validate that requested modes are in your list of available modes