@@ -12,6 +12,7 @@ import {
1212 ToolRegistrationOptions ,
1313 ToolExecutionResult ,
1414} from "./types.js" ;
15+ import { RequestContext } from "../auth/RequestContext.js" ;
1516
1617export class ToolRegistry {
1718 private tools : Map < string , RegisteredTool > = new Map ( ) ;
@@ -200,6 +201,131 @@ export class ToolRegistry {
200201 }
201202 }
202203
204+ /**
205+ * Execute a tool with request context (for authenticated requests)
206+ */
207+ async executeToolWithContext (
208+ name : string ,
209+ args : Record < string , unknown > ,
210+ context : RequestContext ,
211+ ) : Promise < ToolExecutionResult > {
212+ const startTime = Date . now ( ) ;
213+ const tool = this . tools . get ( name ) ;
214+
215+ if ( ! tool ) {
216+ return {
217+ success : false ,
218+ error : `Tool not found: ${ name } ` ,
219+ executionTime : Date . now ( ) - startTime ,
220+ } ;
221+ }
222+
223+ try {
224+ this . logger . debug ( `Executing tool with context: ${ name } ` , {
225+ ...context . toLogContext ( ) ,
226+ argCount : Object . keys ( args ) . length ,
227+ } ) ;
228+
229+ // For context-aware execution, we need to create tool instances with the context's service
230+ // This requires updating the tool registration to support context-aware executors
231+ const result = await this . executeToolWithService ( name , args , context ) ;
232+
233+ // Update tool metrics
234+ tool . callCount ++ ;
235+ tool . lastCalled = new Date ( ) ;
236+ const executionTime = Date . now ( ) - startTime ;
237+
238+ // Update average execution time
239+ tool . averageExecutionTime =
240+ ( tool . averageExecutionTime * ( tool . callCount - 1 ) + executionTime ) / tool . callCount ;
241+
242+ this . logger . info ( `Tool executed successfully with context: ${ name } ` , {
243+ ...context . toLogContext ( ) ,
244+ executionTime,
245+ callCount : tool . callCount ,
246+ } ) ;
247+
248+ return {
249+ ...result ,
250+ executionTime,
251+ } ;
252+ } catch ( error ) {
253+ const executionTime = Date . now ( ) - startTime ;
254+ this . logger . error ( `Tool execution failed with context: ${ name } ` , error as Error , {
255+ ...context . toLogContext ( ) ,
256+ } ) ;
257+
258+ return {
259+ success : false ,
260+ error : ( error as Error ) . message ,
261+ executionTime,
262+ } ;
263+ }
264+ }
265+
266+ /**
267+ * Execute tool with service from context
268+ */
269+ private async executeToolWithService (
270+ name : string ,
271+ args : Record < string , unknown > ,
272+ context : RequestContext ,
273+ ) : Promise < ToolExecutionResult > {
274+ // Create tool instance with context's service
275+ switch ( name ) {
276+ case "lighthouse_upload_file" : {
277+ const { LighthouseUploadFileTool } = await import ( "../tools/LighthouseUploadFileTool.js" ) ;
278+ const tool = new LighthouseUploadFileTool ( context . service , this . logger ) ;
279+ return await tool . execute ( args ) ;
280+ }
281+ case "lighthouse_fetch_file" : {
282+ const { LighthouseFetchFileTool } = await import ( "../tools/LighthouseFetchFileTool.js" ) ;
283+ const tool = new LighthouseFetchFileTool ( context . service , this . logger ) ;
284+ return await tool . execute ( args ) ;
285+ }
286+ case "lighthouse_create_dataset" : {
287+ const { LighthouseCreateDatasetTool } = await import (
288+ "../tools/LighthouseCreateDatasetTool.js"
289+ ) ;
290+ const tool = new LighthouseCreateDatasetTool ( context . service , this . logger ) ;
291+ return await tool . execute ( args ) ;
292+ }
293+ case "lighthouse_list_datasets" : {
294+ const { LighthouseListDatasetsTool } = await import (
295+ "../tools/LighthouseListDatasetsTool.js"
296+ ) ;
297+ const tool = new LighthouseListDatasetsTool ( context . service , this . logger ) ;
298+ return await tool . execute ( args ) ;
299+ }
300+ case "lighthouse_get_dataset" : {
301+ const { LighthouseGetDatasetTool } = await import ( "../tools/LighthouseGetDatasetTool.js" ) ;
302+ const tool = new LighthouseGetDatasetTool ( context . service , this . logger ) ;
303+ return await tool . execute ( args ) ;
304+ }
305+ case "lighthouse_update_dataset" : {
306+ const { LighthouseUpdateDatasetTool } = await import (
307+ "../tools/LighthouseUpdateDatasetTool.js"
308+ ) ;
309+ const tool = new LighthouseUpdateDatasetTool ( context . service , this . logger ) ;
310+ return await tool . execute ( args ) ;
311+ }
312+ case "lighthouse_generate_key" : {
313+ const { LighthouseGenerateKeyTool } = await import ( "../tools/LighthouseGenerateKeyTool.js" ) ;
314+ const tool = new LighthouseGenerateKeyTool ( context . service , this . logger ) ;
315+ return await tool . execute ( args ) ;
316+ }
317+ case "lighthouse_setup_access_control" : {
318+ const { LighthouseSetupAccessControlTool } = await import (
319+ "../tools/LighthouseSetupAccessControlTool.js"
320+ ) ;
321+ const tool = new LighthouseSetupAccessControlTool ( context . service , this . logger ) ;
322+ return await tool . execute ( args ) ;
323+ }
324+ default :
325+ throw new Error ( `Unknown tool: ${ name } ` ) ;
326+ }
327+ }
328+
203329 /**
204330 * Get registry metrics
205331 */
0 commit comments