@@ -131,7 +131,6 @@ public Object callTool(String name, Map<String, Object> arguments) {
131131 if (!this .initialized ) {
132132 throw new IllegalStateException ("MCP client is not initialized. Please wait a moment." );
133133 }
134-
135134 try {
136135 log .info ("Calling tool: {} with arguments: {}" , name , arguments );
137136 McpSchema .CallToolResult result =
@@ -141,41 +140,54 @@ public Object callTool(String name, Map<String, Object> arguments) {
141140 log .error ("Failed to call tool '{}': result is null." , name );
142141 throw new IllegalStateException ("Failed to call tool '" + name + "': result is null." );
143142 }
143+ return processToolResult (result , name );
144+ } catch (Exception e ) {
145+ log .error ("Failed to call tool '{}' from MCP server." , name , e );
146+ throw new IllegalStateException ("Failed to call tool '" + name + "': " + e .getMessage (), e );
147+ }
148+ }
144149
145- if (result .isError () != null && result .isError ()) {
146- String errorMsg = "Tool '" + name + "' returned an error" ;
147- if (result .content () != null && !result .content ().isEmpty ()) {
148- Object errorContent = result .content ().get (0 );
149- if (errorContent instanceof McpSchema .TextContent textContent ) {
150- errorMsg += ": " + textContent .text ();
151- } else {
152- errorMsg += ": " + errorContent ;
153- }
150+ /**
151+ * Processes the tool call result and extracts the content.
152+ * Handles error cases and different content types (text, image, etc.).
153+ *
154+ * @param result The {@link McpSchema.CallToolResult} returned from the tool call.
155+ * @param name The name of the tool that was called.
156+ * @return The extracted content. For text content, returns the text as a {@link String}.
157+ * For image content, returns the {@link McpSchema.ImageContent} object.
158+ * Returns {@code null} if the tool returns empty content.
159+ * @throws IllegalStateException if the tool returns an error.
160+ */
161+ private Object processToolResult (McpSchema .CallToolResult result , String name ) {
162+ if (result .isError () != null && result .isError ()) {
163+ String errorMsg = "Tool '" + name + "' returned an error" ;
164+ if (result .content () != null && !result .content ().isEmpty ()) {
165+ Object errorContent = result .content ().get (0 );
166+ if (errorContent instanceof McpSchema .TextContent textContent ) {
167+ errorMsg += ": " + textContent .text ();
168+ } else {
169+ errorMsg += ": " + errorContent ;
154170 }
155- log .error (errorMsg );
156- throw new IllegalStateException (errorMsg );
157- }
158-
159- if (result .content () == null || result .content ().isEmpty ()) {
160- log .warn ("Tool '{}' returned empty content." , name );
161- return null ;
162171 }
172+ log .error (errorMsg );
173+ throw new IllegalStateException (errorMsg );
174+ }
163175
164- Object content = result .content ().get (0 );
165- if (content instanceof McpSchema .TextContent textContent ) {
166- log .info ("Successfully called tool '{}', result: {}" , name , textContent .text ());
167- return textContent .text ();
168- } else if (content instanceof McpSchema .ImageContent imageContent ) {
169- log .info ("Successfully called tool '{}', returned image content." , name );
170- return imageContent ;
171- } else {
172- log .info ("Successfully called tool '{}', content type: {}" , name , content .getClass ().getSimpleName ());
173- return content ;
174- }
176+ if (result .content () == null || result .content ().isEmpty ()) {
177+ log .warn ("Tool '{}' returned empty content." , name );
178+ return null ;
179+ }
175180
176- } catch (Exception e ) {
177- log .error ("Failed to call tool '{}' from MCP server." , name , e );
178- throw new IllegalStateException ("Failed to call tool '" + name + "': " + e .getMessage (), e );
181+ Object content = result .content ().get (0 );
182+ if (content instanceof McpSchema .TextContent textContent ) {
183+ log .info ("Successfully called tool '{}', result: {}" , name , textContent .text ());
184+ return textContent .text ();
185+ } else if (content instanceof McpSchema .ImageContent imageContent ) {
186+ log .info ("Successfully called tool '{}', returned image content." , name );
187+ return imageContent ;
188+ } else {
189+ log .info ("Successfully called tool '{}', content type: {}" , name , content .getClass ().getSimpleName ());
190+ return content ;
179191 }
180192 }
181193
0 commit comments