-
Notifications
You must be signed in to change notification settings - Fork 649
fix: align tool error handling with MCP specification #540
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
Closed
tzolov
wants to merge
3
commits into
modelcontextprotocol:main
from
tzolov:improve-tool-call-error-handling
+353
−78
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,9 @@ | |
import io.modelcontextprotocol.spec.McpError; | ||
import io.modelcontextprotocol.spec.McpSchema; | ||
import io.modelcontextprotocol.spec.McpSchema.CallToolResult; | ||
import io.modelcontextprotocol.spec.McpSchema.JSONRPCResponse; | ||
import io.modelcontextprotocol.spec.McpSchema.ResourceTemplate; | ||
import io.modelcontextprotocol.spec.McpSchema.TextContent; | ||
import io.modelcontextprotocol.spec.McpSchema.Tool; | ||
import io.modelcontextprotocol.spec.McpStatelessServerTransport; | ||
import io.modelcontextprotocol.util.Assert; | ||
|
@@ -249,6 +251,11 @@ public Mono<CallToolResult> apply(McpTransportContext transportContext, McpSchem | |
|
||
return this.delegateHandler.apply(transportContext, request).map(result -> { | ||
|
||
if (result.isError() != null && result.isError()) { | ||
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. nit: can simplify to |
||
// If the tool call resulted in an error, skip further validation | ||
return result; | ||
} | ||
|
||
if (outputSchema == null) { | ||
if (result.structuredContent() != null) { | ||
logger.warn( | ||
|
@@ -375,11 +382,11 @@ private McpStatelessRequestHandler<CallToolResult> toolsCallRequestHandler() { | |
.findAny(); | ||
|
||
if (toolSpecification.isEmpty()) { | ||
return Mono.error(new McpError("Tool not found: " + callToolRequest.name())); | ||
return Mono.error(new McpError(new JSONRPCResponse.JSONRPCError(McpSchema.ErrorCodes.INVALID_PARAMS, | ||
"Unknown tool: invalid_tool_name", "Tool not found: " + callToolRequest.name()))); | ||
} | ||
|
||
return toolSpecification.map(tool -> tool.callHandler().apply(ctx, callToolRequest)) | ||
.orElse(Mono.error(new McpError("Tool not found: " + callToolRequest.name()))); | ||
return toolSpecification.get().callHandler().apply(ctx, callToolRequest); | ||
}; | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import java.net.URI; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Miscellaneous utility methods. | ||
|
@@ -107,4 +108,19 @@ private static boolean isUnderBaseUri(URI baseUri, URI endpointUri) { | |
return endpointPath.startsWith(basePath); | ||
} | ||
|
||
/** | ||
* Finds the root cause of the given throwable by traversing the cause chain. | ||
* @param throwable The throwable to analyze | ||
* @return The root cause throwable | ||
* @throws NullPointerException if the provided throwable is null | ||
*/ | ||
public static Throwable findRootCause(Throwable throwable) { | ||
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. We don't need this any more |
||
Objects.requireNonNull(throwable); | ||
Throwable rootCause = throwable; | ||
while (rootCause.getCause() != null && rootCause.getCause() != rootCause) { | ||
rootCause = rootCause.getCause(); | ||
} | ||
return rootCause; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: can simplify to
Boolean.TRUE.equals(result.isError())