Skip to content

Add constructor for McpError to allow setting error field#116

Closed
allenporter wants to merge 1 commit into
modelcontextprotocol:mainfrom
allenporter:mcp-error
Closed

Add constructor for McpError to allow setting error field#116
allenporter wants to merge 1 commit into
modelcontextprotocol:mainfrom
allenporter:mcp-error

Conversation

@allenporter

Copy link
Copy Markdown
Contributor

Motivation and Context

Fix an issue where exception handling code fails with AttributeError: 'McpError' object has no attribute 'error'.

          |   File ".../Development/mcp-python-sdk/src/mcp/client/session.py", line 114, in list_resources
          |     return await self.send_request(
          |            ^^^^^^^^^^^^^^^^^^^^^^^^
          |     ...<6 lines>...
          |     )
          |     ^
          |   File ".../mcp-python-sdk/src/mcp/shared/session.py", line 175, in send_request
          |     raise McpError(response_or_error.error)
          | mcp.shared.exceptions.McpError: code=-32601 message='Method not found' data=None
          | 
          | During handling of the above exception, another exception occurred:
          | 
          | Traceback (most recent call last):
          |   File ".../mcp-python-sdk/src/mcp/server/lowlevel/server.py", line 453, in run
          |     response = err.error
          |                ^^^^^^^^^
          | AttributeError: 'McpError' object has no attribute 'error'

The current callers of McpError seem to assume that it has a constructor that accepts an error_data but currently these get passed to the parent as the error message args (e.g. as if it were a pydantic object). This correctly sets the error code argument and sets the message string for the parent class.

How Has This Been Tested?

Tested manually running an sse_client connected to a service that returns errors for unsupported methods when called.

Breaking Changes

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

dsp-ant pushed a commit that referenced this pull request Jan 2, 2025
Backport of #116 by @allenporter to v1.1.x branch.

Fixes an issue where exception handling code fails with AttributeError
when accessing the error field of McpError.
@dsp-ant

dsp-ant commented Jan 2, 2025

Copy link
Copy Markdown
Member

This was merged as b89cabc. I tried to update your branch to be based on v1.1.x so we can keep the PR workflow going, but i got permission denied. So I'll close this ,but it's actually merged and attributed to you! Thank you!

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