@@ -7,7 +7,6 @@ import type { StreamEvent } from '@langchain/core/tracers/log_stream'
77import type { AgentFinish , AgentStep } from 'langchain/agents'
88
99import type { ZodSchema } from 'zod'
10- import { zodToJsonSchema } from 'zod-to-json-schema'
1110import type { MCPClient } from '../client.js'
1211import type { BaseConnector } from '../connectors/base.js'
1312import type { MCPSession } from '../session.js'
@@ -25,6 +24,7 @@ import {
2524 AgentExecutor ,
2625 createToolCallingAgent ,
2726} from 'langchain/agents'
27+ import { zodToJsonSchema } from 'zod-to-json-schema'
2828import { LangChainAdapter } from '../adapters/langchain_adapter.js'
2929import { logger } from '../logging.js'
3030import { ServerManager } from '../managers/server_manager.js'
@@ -368,7 +368,7 @@ export class MCPAgent {
368368 // Fallback: use the same LLM but we'll handle structure in our helper method
369369 structuredLlm = this . llm
370370 }
371- schemaDescription = JSON . stringify ( zodToJsonSchema ( outputSchema ) , null , 2 ) ;
371+ schemaDescription = JSON . stringify ( zodToJsonSchema ( outputSchema ) , null , 2 )
372372 }
373373
374374 try {
@@ -804,10 +804,10 @@ export class MCPAgent {
804804 // Get detailed schema information for better prompting
805805 const maxRetries = 3
806806 let lastError : string = ''
807-
807+
808808 for ( let attempt = 1 ; attempt <= maxRetries ; attempt ++ ) {
809809 logger . info ( `🔄 Structured output attempt ${ attempt } /${ maxRetries } ` )
810-
810+
811811 let formatPrompt = `
812812 Please format the following information according to the EXACT schema specified below.
813813 You must use the exact field names and types as shown in the schema.
@@ -842,21 +842,21 @@ export class MCPAgent {
842842 const validatedResult = this . _validateStructuredResult ( structuredResult , outputSchema )
843843 logger . info ( `✅ Structured output successful on attempt ${ attempt } ` )
844844 return validatedResult
845-
846- } catch ( e ) {
845+ }
846+ catch ( e ) {
847847 lastError = e instanceof Error ? e . message : String ( e )
848848 logger . warn ( `⚠️ Structured output attempt ${ attempt } failed: ${ lastError } ` )
849-
849+
850850 if ( attempt === maxRetries ) {
851851 logger . error ( `❌ All ${ maxRetries } structured output attempts failed` )
852852 throw new Error ( `Failed to generate valid structured output after ${ maxRetries } attempts. Last error: ${ lastError } ` )
853853 }
854-
854+
855855 // Continue to next attempt
856856 continue
857857 }
858858 }
859-
859+
860860 // This should never be reached, but TypeScript requires it
861861 throw new Error ( 'Unexpected error in structured output generation' )
862862 }
@@ -866,42 +866,42 @@ export class MCPAgent {
866866 */
867867 private _validateStructuredResult < T > ( structuredResult : any , outputSchema : ZodSchema < T > ) : T {
868868 // Use Zod to validate the structured result
869- try {
869+ try {
870870 // Use Zod to validate the structured result
871871 const validatedResult = outputSchema . parse ( structuredResult )
872872
873- // Additional validation for required fields
874- const schemaType = outputSchema as any
875- if ( schemaType . _def && schemaType . _def . shape ) {
876- for ( const [ fieldName , fieldSchema ] of Object . entries ( schemaType . _def . shape ) ) {
877- const field = fieldSchema as any
878- const isOptional = field . isOptional ?.( ) ?? field . _def ?. typeName === 'ZodOptional'
879- const isNullable = field . isNullable ?.( ) ?? field . _def ?. typeName === 'ZodNullable'
880- if ( ! isOptional && ! isNullable ) {
881- const value = ( validatedResult as any ) [ fieldName ]
882- if ( value === null || value === undefined
883- || ( typeof value === 'string' && ! value . trim ( ) )
884- || ( Array . isArray ( value ) && value . length === 0 ) ) {
885- throw new Error ( `Required field '${ fieldName } ' is missing or empty` )
873+ // Additional validation for required fields
874+ const schemaType = outputSchema as any
875+ if ( schemaType . _def && schemaType . _def . shape ) {
876+ for ( const [ fieldName , fieldSchema ] of Object . entries ( schemaType . _def . shape ) ) {
877+ const field = fieldSchema as any
878+ const isOptional = field . isOptional ?.( ) ?? field . _def ?. typeName === 'ZodOptional'
879+ const isNullable = field . isNullable ?.( ) ?? field . _def ?. typeName === 'ZodNullable'
880+ if ( ! isOptional && ! isNullable ) {
881+ const value = ( validatedResult as any ) [ fieldName ]
882+ if ( value === null || value === undefined
883+ || ( typeof value === 'string' && ! value . trim ( ) )
884+ || ( Array . isArray ( value ) && value . length === 0 ) ) {
885+ throw new Error ( `Required field '${ fieldName } ' is missing or empty` )
886+ }
886887 }
887888 }
888889 }
889- }
890890
891- return validatedResult
892- }
893- catch ( e ) {
894- logger . debug ( `Validation details: ${ e } ` )
895- throw e // Re-raise to trigger retry logic
896- }
891+ return validatedResult
892+ }
893+ catch ( e ) {
894+ logger . debug ( `Validation details: ${ e } ` )
895+ throw e // Re-raise to trigger retry logic
896+ }
897897 }
898898
899899 /**
900900 * Enhance the query with schema information to make the agent aware of required fields.
901901 */
902902 private _enhanceQueryWithSchema < T > ( query : string , outputSchema : ZodSchema < T > ) : string {
903903 try {
904- const schemaDescription = JSON . stringify ( zodToJsonSchema ( outputSchema ) , null , 2 ) ;
904+ const schemaDescription = JSON . stringify ( zodToJsonSchema ( outputSchema ) , null , 2 )
905905
906906 // Enhance the query with schema awareness
907907 const enhancedQuery = `
0 commit comments