@@ -3,7 +3,7 @@ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
33import type { Config } from "../config.d.ts" ;
44import { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
55import { listResources , readResource } from "./mcp/resources.js" ;
6- import { getSession , defaultSessionId } from "./sessionManager.js" ;
6+ import { SessionManager } from "./sessionManager.js" ;
77import type { MCPTool , BrowserSession } from "./types/types.js" ;
88
99/**
@@ -16,24 +16,38 @@ import type { MCPTool, BrowserSession } from "./types/types.js";
1616export class Context {
1717 public readonly config : Config ;
1818 private server : Server ;
19- public currentSessionId : string = defaultSessionId ;
19+ private sessionManager : SessionManager ;
2020
21- constructor ( server : Server , config : Config ) {
21+ // currentSessionId is a getter that delegates to SessionManager to ensure synchronization
22+ // This prevents desync between Context and SessionManager session tracking
23+ public get currentSessionId ( ) : string {
24+ return this . sessionManager . getActiveSessionId ( ) ;
25+ }
26+
27+ constructor ( server : Server , config : Config , contextId ?: string ) {
2228 this . server = server ;
2329 this . config = config ;
30+ this . sessionManager = new SessionManager ( contextId ) ;
2431 }
2532
2633 public getServer ( ) : Server {
2734 return this . server ;
2835 }
2936
37+ public getSessionManager ( ) : SessionManager {
38+ return this . sessionManager ;
39+ }
40+
3041 /**
3142 * Gets the Stagehand instance for the current session from SessionManager
3243 */
3344 public async getStagehand (
3445 sessionId : string = this . currentSessionId ,
3546 ) : Promise < Stagehand > {
36- const session = await getSession ( sessionId , this . config ) ;
47+ const session = await this . sessionManager . getSession (
48+ sessionId ,
49+ this . config ,
50+ ) ;
3751 if ( ! session ) {
3852 throw new Error ( `No session found for ID: ${ sessionId } ` ) ;
3953 }
@@ -42,7 +56,10 @@ export class Context {
4256
4357 public async getActivePage ( ) : Promise < BrowserSession [ "page" ] | null > {
4458 // Get page from session manager
45- const session = await getSession ( this . currentSessionId , this . config ) ;
59+ const session = await this . sessionManager . getSession (
60+ this . currentSessionId ,
61+ this . config ,
62+ ) ;
4663 if ( session && session . page && ! session . page . isClosed ( ) ) {
4764 return session . page ;
4865 }
@@ -53,7 +70,7 @@ export class Context {
5370 public async getActiveBrowser (
5471 createIfMissing : boolean = true ,
5572 ) : Promise < BrowserSession [ "browser" ] | null > {
56- const session = await getSession (
73+ const session = await this . sessionManager . getSession (
5774 this . currentSessionId ,
5875 this . config ,
5976 createIfMissing ,
0 commit comments