Skip to content

feat: unified error type #308

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

Merged
merged 3 commits into from
Jul 9, 2025

Conversation

4t145
Copy link
Collaborator

@4t145 4t145 commented Jul 7, 2025

Provide a unified error type.

  1. create an unified error type named RmcpError, which can be converted from every possible error type when you create and operate a rmcp service.
  2. remove error generice type of ClientInitializeError and ServerInitializeError, and use a new type called DynamicTransportError, witch include the transport type name.
  3. make the transport error variant in ServiceError use DynamicTransportError too.
  4. remove some redundant bound, precisely, all the From<std::io::Error> bound.

Motivation and Context

#299

How Has This Been Tested?

Breaking Changes

There is a little breaking change, I cancelled the error generic type in InitializeError. User can still fetch the inner transport error by using DynamicTransportError::downcast and DynamicTransportError::is. If user didn't match the inner type before, that would not be a problem.

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

This change could make user confuse about the rmcp::Error and rmcp::RmcpError, so I added an deprecate marker on rmcp::Error.
And in 0.3.x, rmcp::RmcpError would become the rmcp::Error. The current rmcp::Error would become rmcp::ErrorData.

@github-actions github-actions bot added T-documentation Documentation improvements T-test Testing related changes T-core Core library changes T-examples Example code changes T-handler Handler implementation changes T-macros Macro changes T-model Model/data structure changes T-service Service layer changes labels Jul 7, 2025
@4t145 4t145 requested review from Copilot and jokemanfire July 7, 2025 07:59
Copy link
Contributor

@Copilot 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

This PR unifies error handling by introducing a central RmcpError and a dynamic transport error type, DynamicTransportError, removing former generic error parameters and redundant bounds.

  • Created RmcpError for all service, initialization, runtime, and transport-creation errors
  • Replaced generic ClientInitializeError<E>/ServerInitializeError<E> with concrete types using DynamicTransportError
  • Updated examples, tests, service modules, handlers, and macros to use ErrorData/RmcpError and removed old Error generics

Reviewed Changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
examples/servers/src/common/counter.rs Swapped alias Error as McpErrorErrorData as McpError
examples/clients/src/git_stdio.rs Changed main to return Result<(), RmcpError> & added map_err
crates/rmcp/tests/test_progress_subscriber.rs Updated return type from ErrorErrorData
crates/rmcp/tests/test_notification.rs Updated return type from ErrorErrorData
crates/rmcp/tests/test_complex_schema.rs Swapped Error as McpErrorErrorData as McpError
crates/rmcp/tests/common/handlers.rs Swapped Error as McpErrorErrorData as McpError
crates/rmcp/src/transport.rs Added Transport::name & DynamicTransportError type
crates/rmcp/src/service/tower.rs Switched S::Error: Into<Error>Into<ErrorData>
crates/rmcp/src/service/server.rs Removed generic ServerInitializeError<E>, added helper
crates/rmcp/src/service/client.rs Removed generic ClientInitializeError<E>, added helper
crates/rmcp/src/service.rs Replaced Error generics, updated ServiceError variant
crates/rmcp/src/model/content.rs Changed json methods to return ErrorData
crates/rmcp/src/lib.rs Re-exported Error, ErrorData, RmcpError, deprecated old alias
crates/rmcp/src/handler/server/tool.rs Updated parse_json_object & traits to use ErrorData
crates/rmcp/src/handler/server/router/tool.rs Updated return types to ErrorData
crates/rmcp/src/handler/server/router.rs Updated handler signatures to ErrorData
crates/rmcp/src/handler/server.rs Swapped import Error as McpErrorErrorData as McpError
crates/rmcp/src/handler/client.rs Swapped import Error as McpErrorErrorData as McpError
crates/rmcp/src/error.rs Added RmcpError enum and transport_creation helper
crates/rmcp/README.md Updated example to use ErrorData as McpError
crates/rmcp-macros/src/tool_handler.rs Updated generated handlers to return ErrorData
crates/rmcp-macros/src/lib.rs Updated doc comments to rmcp::ErrorData
crates/rmcp-macros/README.md Updated macro examples to use ErrorData
Comments suppressed due to low confidence (4)

examples/servers/src/common/counter.rs:5

  • [nitpick] Aliasing ErrorData as McpError can be misleading; consider using a direct alias like ErrorData or the unified RmcpError for clarity.
    ErrorData as McpError, RoleServer, ServerHandler,

crates/rmcp/src/transport.rs:248

  • Consider adding unit tests for DynamicTransportError::is and downcast methods to ensure transport error conversion works as expected.
#[derive(Debug, thiserror::Error)]

crates/rmcp/README.md:18

  • [nitpick] Update this example to use the new RmcpError alias instead of ErrorData for consistency with the unified error type.
use rmcp::{ErrorData as McpError, ServiceExt, model::*, tool, tool_router, transport::stdio, handler::server::tool::ToolCallContext, handler::server::router::tool::ToolRouter};

crates/rmcp/src/lib.rs:93

  • [nitpick] Review whether the #[allow(deprecated)] on the re-export of Error is still necessary and remove it if the deprecation period is complete.
#[allow(deprecated)]

@@ -48,7 +48,6 @@ pub enum ServiceError {
Timeout { timeout: Duration },
}
Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

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

This empty impl ServiceError {} block is redundant and can be removed to reduce clutter.

Copilot uses AI. Check for mistakes.

jokemanfire
jokemanfire previously approved these changes Jul 8, 2025
@jokemanfire
Copy link
Collaborator

conflict

@4t145
Copy link
Collaborator Author

4t145 commented Jul 8, 2025

@jokemanfire resolved

@4t145
Copy link
Collaborator Author

4t145 commented Jul 8, 2025

cc @ssddOnTop please check this

@ssddOnTop
Copy link
Contributor

Thanks @4t145 I'll review this in a bit

Copy link
Contributor

@ssddOnTop ssddOnTop left a comment

Choose a reason for hiding this comment

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

There's some overall feedback at #299 (comment)

But the PR itself looks good to me.

@4t145 4t145 merged commit e615300 into modelcontextprotocol:main Jul 9, 2025
11 checks passed
@4t145 4t145 deleted the feature-unified-error branch July 9, 2025 03:23
@github-actions github-actions bot mentioned this pull request Jul 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-core Core library changes T-documentation Documentation improvements T-examples Example code changes T-handler Handler implementation changes T-macros Macro changes T-model Model/data structure changes T-service Service layer changes T-test Testing related changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants