-
Notifications
You must be signed in to change notification settings - Fork 1.6k
v2: Errors refactor (ProtocolError, SdkError, OAuthError)
#1454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v2: Errors refactor (ProtocolError, SdkError, OAuthError)
#1454
Conversation
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
v2: Errors refactor (ProtocolError, SdkError)v2: Errors refactor (ProtocolError, SdkError, OAuthError)
mattzcarey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good
mattzcarey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
0f0a4eb
into
modelcontextprotocol:main
Error Hierarchy Refactoring
This PR refactors the SDK's error system to create a clear distinction between protocol errors that cross the wire, local SDK errors, and OAuth errors.
Motivation and Context
The SDK previously used
McpErrorwith numeric codes (ErrorCode.RequestTimeout,ErrorCode.ConnectionClosed) for local timeout/connection errors. However, these errors never cross the wire as JSON-RPC responses - they are rejected locally. Using protocol error codes for local errors was semantically inconsistent.Additionally, OAuth errors used many individual subclasses (e.g.,
InvalidClientError,InvalidGrantError,ServerError) that only differed in their error code. This was unnecessarily complex.This change introduces a structured error hierarchy:
ProtocolError(renamed fromMcpError): For errors that are serialized and sent as JSON-RPC error responsesSdkErrorwithSdkErrorCodeenum: For local errors that are thrown/rejected locally and never leave the SDKOAuthErrorwithOAuthErrorCodeenum: For OAuth-related errors (consolidated from many subclasses)classDiagram class Error { +string message +string name } class ProtocolError { +number code +string message +unknown data +toResponseObject() +fromError() } class SdkError { +SdkErrorCode code +string message +unknown data } class OAuthError { +OAuthErrorCode code +string message +string errorUri +toResponseObject() +fromResponse() } class ProtocolErrorCode { <<enumeration>> ParseError = -32700 InvalidRequest = -32600 MethodNotFound = -32601 InvalidParams = -32602 InternalError = -32603 UrlElicitationRequired = -32042 } class SdkErrorCode { <<enumeration>> NotConnected AlreadyConnected NotInitialized CapabilityNotSupported RequestTimeout ConnectionClosed SendFailed } class OAuthErrorCode { <<enumeration>> InvalidRequest InvalidClient InvalidGrant UnauthorizedClient ...17 codes total } Error <|-- ProtocolError : extends Error <|-- SdkError : extends Error <|-- OAuthError : extends ProtocolError --> ProtocolErrorCode : uses SdkError --> SdkErrorCode : uses OAuthError --> OAuthErrorCode : uses note for ProtocolError "Crosses the wire as JSON-RPC error response" note for SdkError "Local errors, never serialized" note for OAuthError "OAuth 2.0 errors per RFC 6749"How Has This Been Tested?
Breaking Changes
Yes, this is a breaking change. Users will need to update:
1. Protocol/SDK Error Changes
Imports:
McpError→ProtocolError,ErrorCode→ProtocolErrorCodeError handling for timeouts/connection errors: Use
SdkErrorwithSdkErrorCode:New
SdkErrorCodeenum values:SdkErrorCode.NotConnectedSdkErrorCode.AlreadyConnectedSdkErrorCode.NotInitializedSdkErrorCode.CapabilityNotSupportedSdkErrorCode.RequestTimeoutSdkErrorCode.ConnectionClosedSdkErrorCode.SendFailed2. OAuth Error Changes
Individual OAuth error classes replaced with single
OAuthErrorclass:Removed classes:
InvalidRequestError,InvalidClientError,InvalidGrantError,UnauthorizedClientError,UnsupportedGrantTypeError,InvalidScopeError,AccessDeniedError,ServerError,TemporarilyUnavailableError,UnsupportedResponseTypeError,UnsupportedTokenTypeError,InvalidTokenError,MethodNotAllowedError,TooManyRequestsError,InvalidClientMetadataError,InsufficientScopeError,InvalidTargetError,CustomOAuthErrorRemoved constant:
OAUTH_ERRORSTypes of changes
Checklist
Additional context
Files changed:
packages/core/src/errors/sdkErrors.ts(new) - SdkError class and SdkErrorCode enumpackages/core/src/auth/errors.ts- Refactored to single OAuthError class with OAuthErrorCode enumpackages/core/src/types/types.ts- Renamed McpError → ProtocolError, ErrorCode → ProtocolErrorCodepackages/core/src/shared/protocol.ts- Use SdkError for timeouts/connectionpackages/server/src/server/server.ts- Use SdkError for capability errorspackages/client/src/client/client.ts- Use SdkError for capability errorspackages/client/src/client/auth.ts- Updated to use OAuthError with OAuthErrorCodedocs/migration.md- Updated with error migration guidedocs/migration-SKILL.md- Updated with LLM-optimized migration tables