feat(mcp): add support for MCP elicitation -32042 error handling#1745
feat(mcp): add support for MCP elicitation -32042 error handling#1745mkmeral merged 7 commits intostrands-agents:mainfrom
Conversation
|
Bumping. This is a pretty important feature for 3LO or return of control for MCP tools. |
|
/strands review and check if this is the only code that we need to keep track from mcp |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
MCP Elicitation Tracking AssessmentRegarding the question of whether this is the only code needed to track from MCP for elicitation: Yes, this PR covers the essential error-based elicitation path. Per the MCP 2025-11-25 spec, there are two elicitation mechanisms:
The implementation follows a valid "manual retry" approach where the LLM receives URLs and instructs the user to complete authorization, then retry. Future enhancement opportunity (not blocking):
This could be added as a follow-up enhancement if automatic retry is desired. |
|
/strands review |
|
Assessment: Request Changes The PR has critical issues that will cause failures: Blocking Issues
Please fix these issues before this can be merged. See inline comments for details. |
|
/strands review |
|
Assessment: Approve ✅ The previously flagged issues have been addressed: Issues Fixed
Test CoverageAll scenarios from the PR test matrix are now covered:
Implementation correctly handles MCP Good work on the fixes! 👍 |
-32042 error handling |
Ensure this feature gets also implemented in the Typescript SDK |
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
1. -32042 with no URLs in elicitations → "Falls through to generic error" 2. Different McpError code (e.g., -32600) → "Falls through to generic error"
more generic elicitation. Error data could carry any elicitation type eg. url or form
updated the test
|
@agent-of-mkmeral see what is the current state of the TS SDK, and if we should create a ticket/issue there as a feature request for this feature. If so create an issue |
|
@mkmeral I checked the current state of the TypeScript SDK. Here's the summary: TypeScript SDK — MCP Elicitation Status
The TS SDK currently has zero elicitation support. When an MCP server returns Existing TS SDK issues
Neither tracks the error-based ✅ Created new issueI've created a feature request in the TS SDK with full implementation details, test matrix, and reference to this PR: 👉 strands-agents/sdk-typescript#760 — feat(mcp): Add support for MCP -32042 error-based URL elicitation handling cc @mangelino |
…ands-agents#1745) Co-authored-by: Christian Kamwangala <kamwang@amazon.fr>
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.