-
Notifications
You must be signed in to change notification settings - Fork 176
Fix: Return CallToolResult with isError for tool errors to conform with MCP spec #354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,7 +21,6 @@ import kotlinx.serialization.json.buildJsonArray | |||||
| import kotlinx.serialization.json.buildJsonObject | ||||||
| import kotlinx.serialization.json.put | ||||||
| import org.junit.jupiter.api.Test | ||||||
| import org.junit.jupiter.api.assertThrows | ||||||
| import java.text.DecimalFormat | ||||||
| import java.text.DecimalFormatSymbols | ||||||
| import java.util.Locale | ||||||
|
|
@@ -527,16 +526,18 @@ abstract class AbstractToolIntegrationTest : KotlinTestBase() { | |||||
| "message" to "My exception message", | ||||||
| ) | ||||||
|
|
||||||
| val exception = assertThrows<IllegalStateException> { | ||||||
| runBlocking { | ||||||
| client.callTool(errorToolName, exceptionArgs) | ||||||
| } | ||||||
| } | ||||||
| val exceptionResult = client.callTool(errorToolName, exceptionArgs) as CallToolResultBase | ||||||
|
||||||
|
|
||||||
| val msg = exception.message ?: "" | ||||||
| val expectedMessage = "JSONRPCError(code=InternalError, message=My exception message, data={})" | ||||||
| assertTrue(exceptionResult.isError ?: false, "isError should be true for exception") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Elvis operator looks crypric in asserts. |
||||||
|
|
||||||
| assertEquals(expectedMessage, msg, "Unexpected error message for exception") | ||||||
| val exceptionContent = exceptionResult.content.firstOrNull { it is TextContent } as? TextContent | ||||||
| assertNotNull(exceptionContent, "Error content should be present in the result") | ||||||
|
|
||||||
| val exceptionText = exceptionContent.text ?: "" | ||||||
| assertTrue( | ||||||
| exceptionText.contains("Error executing tool") && exceptionText.contains("My exception message"), | ||||||
| "Error message should contain the exception details", | ||||||
| ) | ||||||
|
Comment on lines
+533
to
+540
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO it would be more readable with kotest assertions |
||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
|
|
@@ -770,15 +771,21 @@ abstract class AbstractToolIntegrationTest : KotlinTestBase() { | |||||
| val nonExistentToolName = "non-existent-tool" | ||||||
| val arguments = mapOf("text" to "Test") | ||||||
|
|
||||||
| val exception = assertThrows<IllegalStateException> { | ||||||
| runBlocking { | ||||||
| client.callTool(nonExistentToolName, arguments) | ||||||
| } | ||||||
| val result = runBlocking { | ||||||
| client.callTool(nonExistentToolName, arguments) | ||||||
| } | ||||||
|
|
||||||
| val msg = exception.message ?: "" | ||||||
| val expectedMessage = "JSONRPCError(code=InternalError, message=Tool not found: non-existent-tool, data={})" | ||||||
| assertNotNull(result, "Tool call result should not be null") | ||||||
| val callResult = result as CallToolResult | ||||||
| assertTrue(callResult.isError ?: false, "isError should be true for non-existent tool") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||||||
|
|
||||||
| assertEquals(expectedMessage, msg, "Unexpected error message for non-existent tool") | ||||||
| val textContent = callResult.content.firstOrNull { it is TextContent } as? TextContent | ||||||
| assertNotNull(textContent, "Error content should be present in the result") | ||||||
|
|
||||||
| val errorText = textContent.text ?: "" | ||||||
| assertTrue( | ||||||
| errorText.contains("non-existent-tool") && errorText.contains("not found"), | ||||||
| "Error message should indicate the tool was not found", | ||||||
| ) | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent error message format. The log message uses 'Tool not found: ${request.name}' (line 524), but the returned content uses 'Tool ${request.name} not found'. These should match for consistency. Consider changing to 'Tool not found: ${request.name}' to align with the logging and other error messages in the codebase (e.g., 'Prompt not found: ${request.name}' on line 556).