Skip to content

Conversation

@devcrocod
Copy link
Contributor

May throw the following exceptions: CancellationException, McpException, StreamableHttpError, and SerializationException

Motivation and Context

This makes it possible to handle errors directly without needing to inspect the cause. For example:

try {
    client.connect(transport)
} catch (e: StreamableHttpError) {
    // ...
}

How Has This Been Tested?

locally

Breaking Changes

None

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

Copy link
Contributor

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 improves error handling in the MCP client initialization by allowing specific exception types to be thrown directly instead of being wrapped in IllegalStateException. This makes it easier for users to catch and handle specific error scenarios during client connection.

Key Changes:

  • Modified the connect method's error handling to re-throw CancellationException, McpException, StreamableHttpError, and SerializationException directly
  • Added imports for McpException and SerializationException
  • Replaced if/else logic with a when expression for cleaner exception type checking

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

@kpavlov
Copy link
Contributor

kpavlov commented Nov 18, 2025

Let's add a test

@devcrocod devcrocod force-pushed the devcrocod/handling-client-exception branch from 853c334 to 0331e81 Compare November 20, 2025 17:29
Copy link
Contributor

@kpavlov kpavlov left a comment

Choose a reason for hiding this comment

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

Cool, thank you!

@devcrocod devcrocod merged commit 0f9209a into main Nov 20, 2025
4 checks passed
@devcrocod devcrocod deleted the devcrocod/handling-client-exception branch November 20, 2025 18:09
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.

3 participants