11package io .quarkiverse .langchain4j .mcp .test ;
22
33import static org .assertj .core .api .Assertions .assertThat ;
4+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
45
56import java .util .Map ;
67
1213
1314import dev .langchain4j .agent .tool .ToolExecutionRequest ;
1415import dev .langchain4j .agent .tool .ToolSpecification ;
16+ import dev .langchain4j .exception .ToolArgumentsException ;
17+ import dev .langchain4j .exception .ToolExecutionException ;
1518import dev .langchain4j .mcp .client .McpClient ;
1619import dev .langchain4j .model .chat .request .json .JsonBooleanSchema ;
1720import dev .langchain4j .model .chat .request .json .JsonIntegerSchema ;
@@ -87,9 +90,8 @@ public void executeToolWithWrongArgumentType() {
8790 .name ("echoString" )
8891 .arguments ("{\" input\" : 1}" ) // wrong argument type
8992 .build ();
90- String toolExecutionResultString = executor .execute (toolExecutionRequest , null );
91- assertThat (toolExecutionResultString )
92- .isEqualTo ("There was an error executing the tool. Message: Internal error. Code: -32603" );
93+ assertThatThrownBy (() -> executor .execute (toolExecutionRequest , null ))
94+ .isInstanceOf (ToolExecutionException .class );
9395 }
9496
9597 @ Test
@@ -100,20 +102,18 @@ public void executeNonExistentTool() {
100102 .name ("THIS-TOOL-DOES-NOT-EXIST" )
101103 .arguments ("{\" input\" : 1}" )
102104 .build ();
103- String toolExecutionResultString = executor .execute (toolExecutionRequest , null );
104- assertThat (toolExecutionResultString )
105- .isEqualTo ("There was an error executing the tool. "
106- + "Message: Invalid tool name: THIS-TOOL-DOES-NOT-EXIST. Code: -32602" );
105+ assertThatThrownBy (() -> executor .execute (toolExecutionRequest , null ))
106+ .isInstanceOf (ToolArgumentsException .class )
107+ .hasMessageContaining ("THIS-TOOL-DOES-NOT-EXIST" );
107108 }
108109
109110 @ Test
110111 public void executeToolThatThrowsBusinessError () {
111112 ToolProviderResult toolProviderResult = toolProvider .provideTools (null );
112113 ToolExecutor executor = toolProviderResult .toolExecutorByName ("error" );
113114 ToolExecutionRequest toolExecutionRequest = ToolExecutionRequest .builder ().name ("error" ).arguments ("{}" ).build ();
114- String toolExecutionResultString = executor .execute (toolExecutionRequest , null );
115- assertThat (toolExecutionResultString )
116- .isEqualTo ("There was an error executing the tool. Message: Internal error. Code: -32603" );
115+ assertThatThrownBy (() -> executor .execute (toolExecutionRequest , null ))
116+ .isInstanceOf (ToolExecutionException .class );
117117 }
118118
119119 @ Test
@@ -124,9 +124,8 @@ public void executeToolThatReturnsError() {
124124 .name ("errorResponse" )
125125 .arguments ("{}" )
126126 .build ();
127- String toolExecutionResultString = executor .execute (toolExecutionRequest , null );
128- assertThat (toolExecutionResultString )
129- .isEqualTo ("There was an error executing the tool. The tool returned: This is an actual error" );
127+ assertThatThrownBy (() -> executor .execute (toolExecutionRequest , null ))
128+ .isInstanceOf (ToolExecutionException .class );
130129 }
131130
132131 @ Test
0 commit comments