Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 25, 2025

  • Implement server-side: serialize Exception.Data to JSON-RPC error's data field
  • Implement client-side: deserialize data from JsonRpcError to populate McpProtocolException.Data
  • Handle non-serializable data gracefully (skip non-serializable values using upfront serialization)
  • Add client/server integration tests for data propagation
  • Extract primitive values (strings, numbers, bools) from JsonElements for broader compatibility
    • Primitives work on all platforms including .NET Framework
    • Objects/arrays stored as JsonElement on .NET Core only, skipped on .NET Framework
  • Fix test failures: Skip non-serializable data tests on .NET Framework (where Exception.Data requires values to be serializable to even add them)
Original prompt

This section details on the original issue you should resolve

<issue_title>Add support for data to McpProtocolException</issue_title>
<issue_description>Is your feature request related to a problem? Please describe.

The McpProtocolException class doesn't support data. I'm trying to add the uri to the Resource not found error, as shown here.

Describe the solution you'd like

Add another exception parameter, or pass along Exception.Data.

Describe alternatives you've considered

I tried using Exception.Data, but it's not persisted.

throw new McpProtocolException(
    ReadResourceHandler_ResourceNotFound,
    (McpErrorCode)(-32002))
{
    Data =
    {
        { "uri", context.Params!.Uri }
    }
};

Additional context

See also #863.</issue_description>

<agent_instructions>Where JSON RPC errors are created from McpProtocolExceptions, please update the logic to use Exception.Data as the "data" object of the "error" object.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@stephentoub stephentoub marked this pull request as ready for review November 25, 2025 16:07
Copilot AI changed the title [WIP] Add support for data to McpProtocolException Add support for data to McpProtocolException Nov 25, 2025
Copilot AI requested a review from stephentoub November 25, 2025 16:12
Copilot finished work on behalf of stephentoub November 25, 2025 16:12
Copilot finished work on behalf of stephentoub November 25, 2025 17:07
Copilot finished work on behalf of stephentoub November 25, 2025 22:23
Copy link
Contributor Author

Copilot AI commented Dec 1, 2025

@copilot, please review the CI failures

Fixed in 23d25d8. The issue was that on .NET Framework 4.7.2, Exception.Data uses ListDictionaryInternal which requires values to be marked with [Serializable]. Since JsonElement is not serializable, setting it throws ArgumentException.

Added a try-catch around the data population to silently skip entries that fail, and updated the tests to skip data verification on .NET Framework.

Copilot finished work on behalf of stephentoub December 1, 2025 04:08
Copilot finished work on behalf of stephentoub December 1, 2025 05:06
Copilot AI requested a review from stephentoub December 1, 2025 05:06
Copilot finished work on behalf of stephentoub December 1, 2025 14:21
Copilot AI requested a review from stephentoub December 1, 2025 14:21
Copilot finished work on behalf of stephentoub December 1, 2025 20:50
Copilot AI requested a review from stephentoub December 1, 2025 21:50
Copilot finished work on behalf of stephentoub December 1, 2025 21:50
@stephentoub
Copy link
Contributor

@copilot, please review the CI failures and fix them

Copy link
Contributor Author

Copilot AI commented Dec 2, 2025

@copilot, please review the CI failures and fix them

Fixed in e5d0cbb. The issue was that on .NET Framework, Exception.Data requires values to be [Serializable], so the tests with NonSerializableObject couldn't even be set up. Added SkipUnless to those two tests so they only run on non-.NET Framework platforms.

Copilot finished work on behalf of stephentoub December 2, 2025 15:50
@halter73 halter73 merged commit db0f4d1 into main Dec 3, 2025
15 of 17 checks passed
@halter73 halter73 deleted the copilot/add-data-support-to-mcpprotoexception branch December 3, 2025 23:37
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.

Add support for data to McpProtocolException

3 participants