@@ -183,9 +183,12 @@ export class VirtualMCPStorage implements VirtualMCPStoragePort {
183183 . where ( "dependency_mode" , "=" , "direct" )
184184 . execute ( ) ;
185185
186+ const lastUsedMap = await this . fetchLastUsed ( [ id ] ) ;
187+
186188 return this . deserializeVirtualMCPEntity (
187189 row as unknown as RawConnectionRow ,
188190 aggregationRows as RawAggregationRow [ ] ,
191+ lastUsedMap . get ( id ) ,
189192 ) ;
190193 }
191194
@@ -227,10 +230,13 @@ export class VirtualMCPStorage implements VirtualMCPStoragePort {
227230 aggregationsByParent . set ( agg . parent_connection_id , existing ) ;
228231 }
229232
233+ const lastUsedMap = await this . fetchLastUsed ( virtualMcpIds ) ;
234+
230235 return rows . map ( ( row ) =>
231236 this . deserializeVirtualMCPEntity (
232237 row as unknown as RawConnectionRow ,
233238 aggregationsByParent . get ( row . id ) ?? [ ] ,
239+ lastUsedMap . get ( row . id ) ,
234240 ) ,
235241 ) ;
236242 }
@@ -483,12 +489,47 @@ export class VirtualMCPStorage implements VirtualMCPStoragePort {
483489 }
484490 }
485491
492+ /**
493+ * Returns the most recent thread per agent (by created_at) with the user who started it.
494+ */
495+ private async fetchLastUsed (
496+ ids : string [ ] ,
497+ ) : Promise < Map < string , { last_used_at : string ; last_used_by : string } > > {
498+ if ( ids . length === 0 ) return new Map ( ) ;
499+
500+ const rows = await this . db
501+ . selectFrom ( "threads" )
502+ . distinctOn ( "virtual_mcp_id" )
503+ . select ( [ "virtual_mcp_id" , "created_by" , "created_at" ] )
504+ . where ( "virtual_mcp_id" , "in" , ids )
505+ . orderBy ( "virtual_mcp_id" )
506+ . orderBy ( "created_at" , "desc" )
507+ . execute ( ) ;
508+
509+ const result = new Map <
510+ string ,
511+ { last_used_at : string ; last_used_by : string }
512+ > ( ) ;
513+ for ( const row of rows ) {
514+ const at =
515+ row . created_at instanceof Date
516+ ? row . created_at . toISOString ( )
517+ : String ( row . created_at ) ;
518+ result . set ( row . virtual_mcp_id , {
519+ last_used_at : at ,
520+ last_used_by : row . created_by ,
521+ } ) ;
522+ }
523+ return result ;
524+ }
525+
486526 /**
487527 * Deserialize connection row with aggregations to VirtualMCPEntity
488528 */
489529 private deserializeVirtualMCPEntity (
490530 row : RawConnectionRow ,
491531 aggregationRows : RawAggregationRow [ ] ,
532+ lastUsed ?: { last_used_at : string ; last_used_by : string } ,
492533 ) : VirtualMCPEntity {
493534 // Convert Date to ISO string if needed
494535 const createdAt =
@@ -518,6 +559,8 @@ export class VirtualMCPStorage implements VirtualMCPStoragePort {
518559 updated_at : updatedAt ,
519560 created_by : row . created_by ,
520561 updated_by : row . updated_by ?? undefined ,
562+ last_used_at : lastUsed ?. last_used_at ,
563+ last_used_by : lastUsed ?. last_used_by ,
521564 metadata : {
522565 ...metadata ,
523566 instructions : metadata ?. instructions ?? null ,
0 commit comments