@@ -3,6 +3,8 @@ import { ConnectorManager } from "../connectors/manager.js";
33import { createToolSuccessResponse , createToolErrorResponse } from "../utils/response-formatter.js" ;
44import type { Connector } from "../connectors/interface.js" ;
55import { quoteQualifiedIdentifier } from "../utils/identifier-quoter.js" ;
6+ import { requestStore } from "../requests/index.js" ;
7+ import { getClientIdentifier } from "../utils/client-identifier.js" ;
68
79/**
810 * Object types that can be searched
@@ -454,7 +456,7 @@ async function searchIndexes(
454456 * Create a search_database_objects tool handler
455457 */
456458export function createSearchDatabaseObjectsToolHandler ( sourceId ?: string ) {
457- return async ( args : any , _extra : any ) => {
459+ return async ( args : any , extra : any ) => {
458460 const {
459461 object_type,
460462 pattern = "%" ,
@@ -471,6 +473,11 @@ export function createSearchDatabaseObjectsToolHandler(sourceId?: string) {
471473 limit : number ;
472474 } ;
473475
476+ const startTime = Date . now ( ) ;
477+ const effectiveSourceId = sourceId || "default" ;
478+ let success = true ;
479+ let errorMessage : string | undefined ;
480+
474481 try {
475482 const connector = ConnectorManager . getCurrentConnector ( sourceId ) ;
476483
@@ -479,27 +486,24 @@ export function createSearchDatabaseObjectsToolHandler(sourceId?: string) {
479486 // Validate table parameter
480487 if ( table ) {
481488 if ( ! schema ) {
482- return createToolErrorResponse (
483- "The 'table' parameter requires 'schema' to be specified" ,
484- "SCHEMA_REQUIRED"
485- ) ;
489+ success = false ;
490+ errorMessage = "The 'table' parameter requires 'schema' to be specified" ;
491+ return createToolErrorResponse ( errorMessage , "SCHEMA_REQUIRED" ) ;
486492 }
487493 if ( ! [ "column" , "index" ] . includes ( object_type ) ) {
488- return createToolErrorResponse (
489- `The 'table' parameter only applies to object_type 'column' or 'index', not '${ object_type } '` ,
490- "INVALID_TABLE_FILTER"
491- ) ;
494+ success = false ;
495+ errorMessage = `The 'table' parameter only applies to object_type 'column' or 'index', not '${ object_type } '` ;
496+ return createToolErrorResponse ( errorMessage , "INVALID_TABLE_FILTER" ) ;
492497 }
493498 }
494499
495500 // Validate schema if provided
496501 if ( schema ) {
497502 const schemas = await connector . getSchemas ( ) ;
498503 if ( ! schemas . includes ( schema ) ) {
499- return createToolErrorResponse (
500- `Schema '${ schema } ' does not exist. Available schemas: ${ schemas . join ( ", " ) } ` ,
501- "SCHEMA_NOT_FOUND"
502- ) ;
504+ success = false ;
505+ errorMessage = `Schema '${ schema } ' does not exist. Available schemas: ${ schemas . join ( ", " ) } ` ;
506+ return createToolErrorResponse ( errorMessage , "SCHEMA_NOT_FOUND" ) ;
503507 }
504508 }
505509
@@ -523,10 +527,9 @@ export function createSearchDatabaseObjectsToolHandler(sourceId?: string) {
523527 results = await searchIndexes ( connector , pattern , schema , table , detail_level , limit ) ;
524528 break ;
525529 default :
526- return createToolErrorResponse (
527- `Unsupported object_type: ${ object_type } ` ,
528- "INVALID_OBJECT_TYPE"
529- ) ;
530+ success = false ;
531+ errorMessage = `Unsupported object_type: ${ object_type } ` ;
532+ return createToolErrorResponse ( errorMessage , "INVALID_OBJECT_TYPE" ) ;
530533 }
531534
532535 return createToolSuccessResponse ( {
@@ -540,10 +543,25 @@ export function createSearchDatabaseObjectsToolHandler(sourceId?: string) {
540543 truncated : results . length === limit ,
541544 } ) ;
542545 } catch ( error ) {
546+ success = false ;
547+ errorMessage = ( error as Error ) . message ;
543548 return createToolErrorResponse (
544- `Error searching database objects: ${ ( error as Error ) . message } ` ,
549+ `Error searching database objects: ${ errorMessage } ` ,
545550 "SEARCH_ERROR"
546551 ) ;
552+ } finally {
553+ // Track the request
554+ requestStore . add ( {
555+ id : crypto . randomUUID ( ) ,
556+ timestamp : new Date ( ) . toISOString ( ) ,
557+ sourceId : effectiveSourceId ,
558+ toolName : effectiveSourceId === "default" ? "search_objects" : `search_objects_${ effectiveSourceId } ` ,
559+ sql : `search_objects(object_type=${ object_type } , pattern=${ pattern } , schema=${ schema || "all" } , table=${ table || "all" } , detail_level=${ detail_level } )` ,
560+ durationMs : Date . now ( ) - startTime ,
561+ client : getClientIdentifier ( extra ) ,
562+ success,
563+ error : errorMessage ,
564+ } ) ;
547565 }
548566 } ;
549567}
0 commit comments