Skip to content

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

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

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.
CopilotAI review requested due to automatic review settings June 3, 2026 22:41

CopilotAI left a comment

Copy link
Copy Markdown

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
FileDescription
src/mcp/server/mcpserver/resources/types.pyChain underlying exceptions for resource/file/directory read/list errors.
src/mcp/server/mcpserver/resources/templates.pyChain underlying exceptions when template-based resource creation fails.
src/mcp/server/mcpserver/resources/resource_manager.pyChain underlying exceptions when resolving resources via templates fails.
src/mcp/server/mcpserver/prompts/base.pyChain underlying exceptions when prompt rendering fails.
src/mcp/client/session.pyChain schema/validation exceptions when validating tool structured output.
src/mcp/client/auth/utils.pyChain validation exceptions when parsing OAuth registration/token responses fails.

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

Comment threadsrc/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 threadsrc/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 threadsrc/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
@maxisbey

Copy link
Copy Markdown
Contributor

Thanks for the PR. Tracking this in #2565 instead. Closing as part of a general backlog cleanup ahead of the v2 release. If this is still relevant against current main, feel free to reopen.

AI Disclaimer

Sign up for freeto 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.

3 participants

@kiankhooban@maxisbey