@@ -3,13 +3,13 @@ import { Server } from "@modelcontextprotocol/sdk/server/index.js";
3
3
import type { Config } from "../config.d.ts" ;
4
4
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
5
5
import { 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" ;
8
8
9
9
export class Context {
10
10
public readonly config : Config ;
11
11
private server : Server ;
12
- public currentSessionId : string = defaultSessionId ;
12
+ private _currentSession : StagehandSession | null = null ;
13
13
14
14
constructor ( server : Server , config : Config ) {
15
15
this . server = server ;
@@ -21,40 +21,35 @@ export class Context {
21
21
}
22
22
23
23
/**
24
- * Gets the Stagehand instance for the current session from SessionManager
24
+ * Gets the Stagehand instance for the current session
25
25
*/
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 ( ) ;
33
28
return session . stagehand ;
34
29
}
35
30
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
41
39
}
42
-
43
40
return null ;
44
41
}
45
42
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
56
51
}
57
- return session . browser ;
52
+ return null ;
58
53
}
59
54
60
55
async run ( tool : MCPTool , args : unknown ) : Promise < CallToolResult > {
@@ -122,4 +117,15 @@ export class Context {
122
117
readResource ( uri : string ) {
123
118
return readResource ( uri ) ;
124
119
}
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
+ }
125
131
}
0 commit comments