feat(mcp): add support for MCP elicitation -32042 error handling #1745
Open
Christian-kam wants to merge 2 commits intostrands-agents:mainfrom
Open
feat(mcp): add support for MCP elicitation -32042 error handling #1745Christian-kam wants to merge 2 commits intostrands-agents:mainfrom
-32042 error handling #1745Christian-kam wants to merge 2 commits intostrands-agents:mainfrom
Conversation
Add handling for MCP elicitation required errors (error code -32042) in MCPClient. When an MCP tool requires user authorization via URL, the client now parses the elicitation data and returns a user-friendly error message with clear instructions and the authorization URL(s). The implementation gracefully falls back to generic error handling if the elicitation data cannot be parsed, ensuring robustness. Changes: - Enhanced _handle_tool_execution_error to detect and parse elicitation errors - Added imports for ElicitationRequiredErrorData and McpError - Added comprehensive test coverage for various elicitation scenarios - Follows structured logging and error handling patterns
Add handling for MCP elicitation required errors (error code -32042) in MCPClient. When an MCP tool requires user authorization via URL, the client now parses the elicitation data and returns a user-friendly error message with clear instructions and the authorization URL(s). The implementation gracefully falls back to generic error handling if the elicitation data cannot be parsed, ensuring robustness. Changes: - Enhanced _handle_tool_execution_error to detect and parse elicitation errors - Added imports for ElicitationRequiredErrorData and McpError - Added comprehensive test coverage for various elicitation scenarios - Follows structured logging and error handling patterns
Author
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
When an MCP server like AgentCore Gateway returns a
-32042(URLElicitationRequiredError) ontools/call, theMCP Python SDK raises
McpError. The currentMCPClient._handle_tool_execution_errorcatches it and returns a generic error string, losing the authorization URL(s) embedded in
error.data.elicitations. This breaks OAuth consent flows that rely on URL mode elicitation(e.g., AgentCore Gateway 3LO, or any MCP server using the 2025-11-25 spec).
This PR modifies
_handle_tool_execution_errorto detectMcpErrorwith code-32042,parse the error data using the MCP SDK's
ElicitationRequiredErrorDatamodel, and includethe authorization URL(s) in the tool result text with a
URL_ELICITATION_REQUIREDprefix.The LLM can then present the URL to the user and retry the tool call after consent completes.
Changes
File:
strands/tools/mcp/mcp_client.pyMcpErrorfrommcp.shared.exceptions,ElicitationRequiredErrorDatafrommcp.typesMCPClient._handle_tool_execution_error: added a check at the top of the method for-32042errors, parsesElicitationRequiredErrorData, extracts URL(s) and message(s), and returns them in the tool result. Falls through to original behavior for all non-32042 errors.How the two elicitation paths work together
The MCP 2025-11-25 spec defines two ways a server can request user authorization:
elicitation/createrequest to clientelicitation_callbackonMCPClient(existing, unchanged)-32042error ontools/call_handle_tool_execution_error(this PR)Both paths can coexist. The
elicitation_callbackhandles case 1 via the MCP Python SDK'sClientSession. This PR handles case 2, where the error is raised asMcpErrorand caughtby
call_tool_async/call_tool_sync.Related Issues
Closes #1742
Specifically addresses the "URL Mode Elicitation" item under Major Changes in the MCP 2025-11-25 spec:
-32042)Documentation PR
N/A
Type of Change
New feature
Testing
Unit tests
-32042with validElicitationRequiredErrorDataMcpError(ErrorData(code=-32042, data={"elicitations": [{"mode": "url", "url": "https://...", "message": "Please authorize"}]}))MCPToolResultwithURL_ELICITATION_REQUIREDand the URL-32042with multiple URLs-32042with malformed dataMcpError(ErrorData(code=-32042, data="invalid"))"Tool execution failed: ..."-32042with no URLs in elicitationsMcpError(ErrorData(code=-32042, data={"elicitations": [{"mode": "url"}]}))"Tool execution failed: ..."McpError(ErrorData(code=-32600, ...))"Tool execution failed: ..."(no change)RuntimeError("connection lost")"Tool execution failed: ..."(no change)Integration test
Tested with AgentCore Gateway using LinkedIn as a 3LO OAuth target:
LinkedInAuthCode___getUserInfo-32042with authorization URLhatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.