@@ -3106,40 +3106,40 @@ export const registerToolkit: <Tools extends Record<string, Tool.Any>>(
31063106 const annotations = tool . annotations
31073107 const toolMeta = Context . getOrUndefined ( annotations , Tool . Meta )
31083108 const isDeclaredFailure = Schema . is ( tool . failureSchema )
3109- const encodeFailure = Schema . encodeUnknownEffect ( tool . failureSchema ) as (
3110- error : unknown
3111- ) => Effect . Effect < unknown , Schema . SchemaError , Tool . HandlerServices < Tools [ keyof Tools ] > >
3112- const declaredFailureResult = ( error : unknown ) =>
3113- error instanceof Error && error . message !== ""
3114- ? Effect . succeed ( toolErrorResult ( error . message ) )
3115- : Effect . map ( encodeFailure ( error ) , ( encoded ) =>
3116- new CallToolResult ( { isError : true , content : toolResultContent ( encoded ) } ) )
3117- // `structuredContent` must be JSON and match the advertised `outputSchema`, which is derived from the JSON codec.
3118- // The Toolkit has already encoded the result, so only the JSON conversion of its encoded form runs here.
3119- const encodeStructured = Schema . encodeUnknownEffect ( Schema . toCodecJson ( Schema . toEncoded ( tool . successSchema ) ) ) as (
3120- result : unknown
3121- ) => Effect . Effect < unknown , Schema . SchemaError , Tool . HandlerServices < Tools [ keyof Tools ] > >
3122- const successResult = ( result : unknown ) =>
3123- encodeStructured ( result ) . pipe (
3124- Effect . mapError ( ( cause ) =>
3109+ // MCP content is JSON, so results and declared failures go through their JSON codec.
3110+ const encodeJson = ( schema : Schema . Constraint ) => {
3111+ const encode = Schema . encodeUnknownEffect ( Schema . toCodecJson ( schema ) ) as (
3112+ value : unknown
3113+ ) => Effect . Effect < unknown , Schema . SchemaError , Tool . HandlerServices < Tools [ keyof Tools ] > >
3114+ return ( toolResult : unknown ) =>
3115+ Effect . mapError ( encode ( toolResult ) , ( cause ) =>
31253116 AiError . make ( {
31263117 module : "McpServer" ,
31273118 method : "registerToolkit" ,
31283119 reason : new AiError . ToolResultEncodingError ( {
31293120 toolName : tool . name ,
3130- toolResult : result ,
3121+ toolResult,
31313122 description : cause . message
31323123 } )
3133- } )
3134- ) ,
3135- Effect . map ( ( structuredContent ) =>
3136- new CallToolResult ( {
3137- isError : false ,
3138- structuredContent,
3139- content : toolResultContent ( structuredContent )
3140- } )
3141- )
3142- )
3124+ } ) )
3125+ }
3126+ const encodeFailure = encodeJson ( tool . failureSchema )
3127+ // Successes and "return"-mode failures arrive already encoded by the Toolkit; only their JSON conversion runs here.
3128+ const encodeReturnedFailure = encodeJson ( Schema . toEncoded ( tool . failureSchema ) )
3129+ const encodeStructured = encodeJson ( Schema . toEncoded ( tool . successSchema ) )
3130+ const failureResult = ( encoded : unknown ) =>
3131+ new CallToolResult ( { isError : true , content : toolResultContent ( encoded ) } )
3132+ const declaredFailureResult = ( error : unknown ) =>
3133+ error instanceof Error && error . message !== ""
3134+ ? Effect . succeed ( toolErrorResult ( error . message ) )
3135+ : Effect . map ( encodeFailure ( error ) , failureResult )
3136+ const successResult = ( result : unknown ) =>
3137+ Effect . map ( encodeStructured ( result ) , ( structuredContent ) =>
3138+ new CallToolResult ( {
3139+ isError : false ,
3140+ structuredContent,
3141+ content : toolResultContent ( structuredContent )
3142+ } ) )
31433143 const handleCause = ( cause : Cause . Cause < unknown > ) => {
31443144 const failure = Cause . findFail ( cause )
31453145 if ( Result . isSuccess ( failure ) ) {
@@ -3208,9 +3208,7 @@ export const registerToolkit: <Tools extends Record<string, Tool.Any>>(
32083208 }
32093209 // Declared failures return their encoded payload; anything else is classified by origin.
32103210 return result . failureOrigin === "handler"
3211- ? Effect . succeed (
3212- new CallToolResult ( { isError : true , content : toolResultContent ( result . encodedResult ) } )
3213- )
3211+ ? Effect . map ( encodeReturnedFailure ( result . encodedResult ) , failureResult )
32143212 : Effect . failCause ( Cause . annotate (
32153213 Cause . fail ( result . result ) ,
32163214 Context . make ( Toolkit . FailureOrigin , result . failureOrigin ?? "result" )
0 commit comments