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
ThenewSession 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
Example
May throw an
auth_required error if the agent requires authentication before creating sessions.Loading Sessions
The optionalloadSession 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
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
sessionUpdatenotifications
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)
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)
Forking Sessions
Theunstable_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
Example
Resuming Sessions
Theunstable_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
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 IDsClean 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