@@ -3,13 +3,13 @@ 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" ;
7- import type { MCPTool , BrowserSession } from "./types/types.js" ;
6+ import { store } from "./tools/helpers/session .js" ;
7+ import type { MCPTool , StagehandSession } from "./types/types.js" ;
88
99export class Context {
1010 public readonly config : Config ;
1111 private server : Server ;
12- public currentSessionId : string = defaultSessionId ;
12+ private _currentSession : StagehandSession | null = null ;
1313
1414 constructor ( server : Server , config : Config ) {
1515 this . server = server ;
@@ -21,40 +21,35 @@ export class Context {
2121 }
2222
2323 /**
24- * Gets the Stagehand instance for the current session from SessionManager
24+ * Gets the Stagehand instance for the current session
2525 */
26- public async getStagehand (
27- sessionId : string = this . currentSessionId ,
28- ) : Promise < Stagehand > {
29- const session = await getSession ( sessionId , this . config ) ;
30- if ( ! session ) {
31- throw new Error ( `No session found for ID: ${ sessionId } ` ) ;
32- }
26+ public async getStagehand ( ) : Promise < Stagehand > {
27+ const session = await this . getCurrentSession ( ) ;
3328 return session . stagehand ;
3429 }
3530
36- public async getActivePage ( ) : Promise < BrowserSession [ "page" ] | null > {
37- // Get page from session manager
38- const session = await getSession ( this . currentSessionId , this . config ) ;
39- if ( session && session . page && ! session . page . isClosed ( ) ) {
40- return session . page ;
31+ public async getActivePage ( ) : Promise < StagehandSession [ "page" ] | null > {
32+ try {
33+ const session = await this . getCurrentSession ( ) ;
34+ if ( session . page && ! session . page . isClosed ( ) ) {
35+ return session . page ;
36+ }
37+ } catch {
38+ // Session not available
4139 }
42-
4340 return null ;
4441 }
4542
46- public async getActiveBrowser (
47- createIfMissing : boolean = true ,
48- ) : Promise < BrowserSession [ "browser" ] | null > {
49- const session = await getSession (
50- this . currentSessionId ,
51- this . config ,
52- createIfMissing ,
53- ) ;
54- if ( ! session || ! session . browser || ! session . browser . isConnected ( ) ) {
55- return null ;
43+ public async getActiveBrowser ( ) : Promise < StagehandSession [ "browser" ] | null > {
44+ try {
45+ const session = await this . getCurrentSession ( ) ;
46+ if ( session . browser && session . browser . isConnected ( ) ) {
47+ return session . browser ;
48+ }
49+ } catch {
50+ // Session not available
5651 }
57- return session . browser ;
52+ return null ;
5853 }
5954
6055 async run ( tool : MCPTool , args : unknown ) : Promise < CallToolResult > {
@@ -122,4 +117,15 @@ export class Context {
122117 readResource ( uri : string ) {
123118 return readResource ( uri ) ;
124119 }
120+
121+ /**
122+ * Gets or creates the current session
123+ */
124+ private async getCurrentSession ( ) : Promise < StagehandSession > {
125+ if ( ! this . _currentSession ) {
126+ const sessionStore = store ( this . config ) ;
127+ this . _currentSession = await sessionStore . create ( ) ;
128+ }
129+ return this . _currentSession ;
130+ }
125131}
0 commit comments