Skip to content

fix: add raise ... from e in all except handlers#2777

Open
kiankhooban wants to merge 2 commits into
modelcontextprotocol:mainfrom
kiankhooban:fix/raise-without-from
Open

fix: add raise ... from e in all except handlers#2777
kiankhooban wants to merge 2 commits into
modelcontextprotocol:mainfrom
kiankhooban:fix/raise-without-from

Conversation

@kiankhooban
Copy link
Copy Markdown

Follow-up to #2542.

Adds from e to all remaining raise statements inside except ... as e: blocks that were missing exception chaining. This ensures the original traceback is preserved when exceptions are re-raised, making debugging significantly easier.

Files changed (11 sites across 6 files):

  • src/mcp/client/session.py (2)
  • src/mcp/client/auth/utils.py (2)
  • src/mcp/server/mcpserver/resources/types.py (4)
  • src/mcp/server/mcpserver/resources/resource_manager.py (1)
  • src/mcp/server/mcpserver/resources/templates.py (1)
  • src/mcp/server/mcpserver/prompts/base.py (1)

Preserves exception chaining so the original traceback is always
visible when exceptions are re-raised inside except blocks.

Fixes 11 sites across 6 files — follow-up to modelcontextprotocol#2542.
Copilot AI review requested due to automatic review settings June 3, 2026 22:41
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Improve debuggability by preserving original exceptions when re-raising errors across resources, prompts, and client/auth flows.

Changes:

  • Add explicit exception chaining (raise ... from e) to preserve original tracebacks.
  • Apply the same pattern across resource reading/creation, prompt rendering, tool result validation, and OAuth response parsing.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/mcp/server/mcpserver/resources/types.py Chain underlying exceptions for resource/file/directory read/list errors.
src/mcp/server/mcpserver/resources/templates.py Chain underlying exceptions when template-based resource creation fails.
src/mcp/server/mcpserver/resources/resource_manager.py Chain underlying exceptions when resolving resources via templates fails.
src/mcp/server/mcpserver/prompts/base.py Chain underlying exceptions when prompt rendering fails.
src/mcp/client/session.py Chain schema/validation exceptions when validating tool structured output.
src/mcp/client/auth/utils.py Chain validation exceptions when parsing OAuth registration/token responses fails.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/mcp/client/session.py Outdated
Comment on lines +320 to +321
except SchemaError as e: # pragma: no cover
raise RuntimeError(f"Invalid schema for tool {name}: {e}") # pragma: no cover
raise RuntimeError(f"Invalid schema for tool {name}: {e}") from e # pragma: no cover
Comment thread src/mcp/client/auth/utils.py Outdated
return client_info
except ValidationError as e: # pragma: no cover
raise OAuthRegistrationError(f"Invalid registration response: {e}")
raise OAuthRegistrationError(f"Invalid registration response: {e}") from e
Comment thread src/mcp/client/auth/utils.py Outdated
return token_response
except ValidationError as e: # pragma: no cover
raise OAuthTokenError(f"Invalid token response: {e}")
raise OAuthTokenError(f"Invalid token response: {e}") from e
- Remove duplicate pragma: no cover from raise line in session.py
- Strip {e} from OAuth error messages to avoid leaking token data;
  the chained exception already carries full details for debugging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants