python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

Add constructor for McpError to allow setting `error` field

Open allenporter opened this issue 1 year ago • 0 comments

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

  • [x] 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

  • [x] I have read the MCP Documentation
  • [x] 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

allenporter avatar Dec 28 '24 18:12 allenporter