@@ -28,42 +28,16 @@ async function handleCreateContext(
28
28
context : Context ,
29
29
params : CreateContextInput
30
30
) : Promise < ToolResult > {
31
- const action = async ( ) : Promise < ToolActionResult > => {
32
- try {
33
- const config = context . config ;
34
-
35
- if ( ! config . browserbaseApiKey || ! config . browserbaseProjectId ) {
36
- throw new Error ( "Browserbase API Key or Project ID is missing in the configuration" ) ;
37
- }
38
-
39
- const bb = new Browserbase ( {
40
- apiKey : config . browserbaseApiKey ,
41
- } ) ;
42
-
43
- console . error ( "Creating new Browserbase context" ) ;
44
- const bbContext = await bb . contexts . create ( {
45
- projectId : config . browserbaseProjectId ,
46
- } ) ;
47
-
48
- console . error ( `Successfully created context: ${ bbContext . id } ` ) ;
49
-
50
- // Store context ID with optional name if provided
51
- const contextName = params . name || bbContext . id ;
52
- contexts . set ( contextName , bbContext . id ) ;
53
-
54
- return {
55
- content : [
56
- {
57
- type : "text" ,
58
- text : `Created new Browserbase context with ID: ${ bbContext . id } ${ params . name ? ` and name: ${ params . name } ` : '' } ` ,
59
- } ,
60
- ] ,
61
- } ;
62
- } catch ( error : any ) {
63
- console . error ( `CreateContext handle failed: ${ error . message || error } ` ) ;
64
- throw new Error ( `Failed to create Browserbase context: ${ error . message || error } ` ) ;
31
+ try {
32
+ const config = context . config ;
33
+
34
+ if ( ! config . browserbaseApiKey || ! config . browserbaseProjectId ) {
35
+ throw new Error ( "Browserbase API Key or Project ID is missing in the configuration" ) ;
65
36
}
66
- } ;
37
+
38
+ const bb = new Browserbase ( {
39
+ apiKey : config . browserbaseApiKey ,
40
+ } ) ;
67
41
68
42
console . error ( "Creating new Browserbase context" ) ;
69
43
const bbContext = await bb . contexts . create ( {
@@ -124,70 +98,64 @@ async function handleDeleteContext(
124
98
context : Context ,
125
99
params : DeleteContextInput
126
100
) : Promise < ToolResult > {
127
- const action = async ( ) : Promise < ToolActionResult > => {
128
- try {
129
- const config = context . config ;
130
-
131
- if ( ! config . browserbaseApiKey ) {
132
- throw new Error ( "Browserbase API Key is missing in the configuration" ) ;
133
- }
134
-
135
- if ( ! params . contextId && ! params . name ) {
136
- throw new Error ( "Missing required argument: either contextId or name must be provided" ) ;
137
- }
101
+ try {
102
+ const config = context . config ;
103
+
104
+ if ( ! config . browserbaseApiKey ) {
105
+ throw new Error ( "Browserbase API Key is missing in the configuration" ) ;
106
+ }
107
+
108
+ if ( ! params . contextId && ! params . name ) {
109
+ throw new Error ( "Missing required argument: either contextId or name must be provided" ) ;
110
+ }
138
111
139
- // Resolve context ID either directly or by name
140
- let contextId = params . contextId ;
141
- if ( ! contextId && params . name ) {
142
- contextId = contexts . get ( params . name ) ;
143
- if ( ! contextId ) {
144
- throw new Error ( `Context with name "${ params . name } " not found` ) ;
145
- }
112
+ // Resolve context ID either directly or by name
113
+ let contextId = params . contextId ;
114
+ if ( ! contextId && params . name ) {
115
+ contextId = contexts . get ( params . name ) ;
116
+ if ( ! contextId ) {
117
+ throw new Error ( `Context with name "${ params . name } " not found` ) ;
146
118
}
119
+ }
147
120
148
- console . error ( `Deleting Browserbase context: ${ contextId } ` ) ;
149
-
150
- // Delete from Browserbase API
151
- // The SDK may not have a delete method directly, so we use the REST API
152
- const response = await fetch ( `https://api.browserbase.com/v1/contexts/${ contextId } ` , {
153
- method : 'DELETE' ,
154
- headers : {
155
- 'X-BB-API-Key' : config . browserbaseApiKey ,
156
- } ,
157
- } ) ;
158
-
159
- if ( response . status !== 204 ) {
160
- const errorText = await response . text ( ) ;
161
- throw new Error ( `Failed to delete context with status ${ response . status } : ${ errorText } ` ) ;
162
- }
163
-
164
- // Remove from local store
165
- if ( params . name ) {
166
- contexts . delete ( params . name ) ;
167
- }
168
-
169
- // Delete by ID too (in case it was stored multiple ways)
170
- for ( const [ name , id ] of contexts . entries ( ) ) {
171
- if ( id === contextId ) {
172
- contexts . delete ( name ) ;
173
- }
121
+ console . error ( `Deleting Browserbase context: ${ contextId } ` ) ;
122
+
123
+ // Delete from Browserbase API
124
+ // The SDK may not have a delete method directly, so we use the REST API
125
+ const response = await fetch ( `https://api.browserbase.com/v1/contexts/${ contextId } ` , {
126
+ method : 'DELETE' ,
127
+ headers : {
128
+ 'X-BB-API-Key' : config . browserbaseApiKey ,
129
+ } ,
130
+ } ) ;
131
+
132
+ if ( response . status !== 204 ) {
133
+ const errorText = await response . text ( ) ;
134
+ throw new Error ( `Failed to delete context with status ${ response . status } : ${ errorText } ` ) ;
135
+ }
136
+
137
+ // Remove from local store
138
+ if ( params . name ) {
139
+ contexts . delete ( params . name ) ;
140
+ }
141
+
142
+ // Delete by ID too (in case it was stored multiple ways)
143
+ for ( const [ name , id ] of contexts . entries ( ) ) {
144
+ if ( id === contextId ) {
145
+ contexts . delete ( name ) ;
174
146
}
175
-
176
- console . error ( `Successfully deleted context: ${ contextId } ` ) ;
177
-
178
- return {
179
- content : [
180
- {
181
- type : "text" ,
182
- text : `Deleted Browserbase context with ID: ${ contextId } ` ,
183
- } ,
184
- ] ,
185
- } ;
186
- } catch ( error : any ) {
187
- console . error ( `DeleteContext handle failed: ${ error . message || error } ` ) ;
188
- throw new Error ( `Failed to delete Browserbase context: ${ error . message || error } ` ) ;
189
147
}
190
- } ;
148
+
149
+ console . error ( `Successfully deleted context: ${ contextId } ` ) ;
150
+
151
+ const result : ToolActionResult = {
152
+ content : [
153
+ {
154
+ type : "text" ,
155
+ text : `Deleted Browserbase context with ID: ${ contextId } ` ,
156
+ } ,
157
+ ] ,
158
+ } ;
191
159
192
160
return {
193
161
resultOverride : result ,
0 commit comments