@@ -19,6 +19,7 @@ import {
1919 type SyncCreativesResponse ,
2020 type CreateMediaBuyResponse ,
2121 type MediaBuyDeliveryNotification ,
22+ type ProtocolLoggingConfig ,
2223 ADCP_STATUS ,
2324 InputRequiredError ,
2425} from '../lib' ;
@@ -127,6 +128,18 @@ const clientConfig: SingleAgentClientConfig = {
127128 logSchemaViolations : process . env . ADCP_LOG_SCHEMA_VIOLATIONS !== 'false' , // Default: true
128129 } ,
129130
131+ // Protocol logging configuration - wire-level HTTP request/response logging
132+ // Can be enabled via env var or dynamically per request
133+ protocolLogging : {
134+ enabled : process . env . ADCP_PROTOCOL_LOGGING === 'true' , // Default: false
135+ logRequests : true ,
136+ logResponses : true ,
137+ logRequestBodies : true ,
138+ logResponseBodies : true ,
139+ maxBodySize : 50000 ,
140+ redactAuthHeaders : true ,
141+ } ,
142+
130143 // Activity logging - store ALL events
131144 onActivity : ( activity : Activity ) => {
132145 storeEvent ( {
@@ -660,6 +673,89 @@ app.get('/api/sales/agents/:agentId/info', async (request, reply) => {
660673 }
661674} ) ;
662675
676+ // Query agent endpoint - supports protocol logging
677+ app . post < {
678+ Params : { agentId : string } ;
679+ Body : {
680+ brandStory ?: string ;
681+ offering ?: string | null ;
682+ agentConfig ?: AgentConfig ;
683+ protocolLogging ?: ProtocolLoggingConfig | null ;
684+ } ;
685+ } > ( '/api/sales/agents/:agentId/query' , async ( request , reply ) => {
686+ const { agentId } = request . params ;
687+ const { brandStory, offering, agentConfig, protocolLogging } = request . body ;
688+
689+ try {
690+ // Create a temporary client if custom config is provided, or use the existing client with protocol logging
691+ let agent ;
692+ let tempClient : ADCPMultiAgentClient | null = null ;
693+
694+ if ( agentConfig ) {
695+ // Custom agent configuration provided
696+ const customAgentConfig : AgentConfig = {
697+ id : agentConfig . id || agentId ,
698+ name : agentConfig . name || agentId ,
699+ agent_uri : agentConfig . agent_uri || ( agentConfig as any ) . server_url ,
700+ protocol : agentConfig . protocol || 'mcp' ,
701+ auth_token_env : agentConfig . auth_token_env ,
702+ requiresAuth : agentConfig . requiresAuth !== false ,
703+ } ;
704+
705+ // Merge protocol logging config if provided
706+ const tempClientConfig : SingleAgentClientConfig = {
707+ ...clientConfig ,
708+ ...( protocolLogging && { protocolLogging } ) ,
709+ } ;
710+
711+ tempClient = new ADCPMultiAgentClient ( [ customAgentConfig ] , tempClientConfig ) ;
712+ agent = tempClient . agent ( customAgentConfig . id ) ;
713+ } else {
714+ // Use existing client - if protocol logging is provided, create new client with that config
715+ if ( protocolLogging ) {
716+ const agentConfigs = adcpClient . getAgentConfigs ( ) ;
717+ const tempClientConfig : SingleAgentClientConfig = {
718+ ...clientConfig ,
719+ protocolLogging,
720+ } ;
721+ tempClient = new ADCPMultiAgentClient ( agentConfigs , tempClientConfig ) ;
722+ agent = tempClient . agent ( agentId ) ;
723+ } else {
724+ agent = adcpClient . agent ( agentId ) ;
725+ }
726+ }
727+
728+ if ( ! agent ) {
729+ return reply . code ( 404 ) . send ( {
730+ success : false ,
731+ error : `Agent ${ agentId } not found` ,
732+ timestamp : new Date ( ) . toISOString ( ) ,
733+ } ) ;
734+ }
735+
736+ // Call get_products by default (this is what the testing UI expects)
737+ const result = await agent . getProducts ( {
738+ brand_manifest : { name : brandStory || offering || 'Test brand' } ,
739+ ...( brandStory && { brief : brandStory } ) ,
740+ } ) ;
741+
742+ return reply . send ( {
743+ success : result . success ,
744+ data : result . data ,
745+ error : result . error ,
746+ debugLogs : result . debug_logs || [ ] ,
747+ timestamp : new Date ( ) . toISOString ( ) ,
748+ } ) ;
749+ } catch ( error ) {
750+ app . log . error ( `Failed to query agent ${ agentId } : ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
751+ return reply . code ( 500 ) . send ( {
752+ success : false ,
753+ error : error instanceof Error ? error . message : 'Failed to query agent' ,
754+ timestamp : new Date ( ) . toISOString ( ) ,
755+ } ) ;
756+ }
757+ } ) ;
758+
663759// Helper function to extract data from nested A2A/MCP responses
664760function extractResponseData ( result : any ) : any {
665761 // Check multiple possible nested structures
0 commit comments