Add support for `data` to `McpProtocolException` by Copilot · Pull Request #1028 · modelcontextprotocol/csharp-sdk · GitHub
Skip to content

Add support for data to McpProtocolException#1028

Merged
halter73 merged 19 commits into
mainfrom
copilot/add-data-support-to-mcpprotoexception
Dec 3, 2025
Merged

Add support for data to McpProtocolException#1028
halter73 merged 19 commits into
mainfrom
copilot/add-data-support-to-mcpprotoexception

Conversation

Copilot AI commented Nov 25, 2025

Copy link
Copy Markdown
Contributor
  • 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.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Comment thread tests/ModelContextProtocol.Tests/Server/McpServerTests.cs Outdated
Comment thread tests/ModelContextProtocol.Tests/Server/McpServerTests.cs Outdated
@stephentoub stephentoub marked this pull request as ready for review November 25, 2025 16:07
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
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
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Comment thread src/ModelContextProtocol.Core/McpSessionHandler.cs
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Comment thread src/ModelContextProtocol.Core/McpSessionHandler.cs Outdated
Comment thread src/ModelContextProtocol.Core/McpSessionHandler.cs Outdated
Comment thread tests/ModelContextProtocol.Tests/Server/McpServerTests.cs Outdated
Comment thread src/ModelContextProtocol.Core/McpSessionHandler.cs Outdated
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Comment thread tests/ModelContextProtocol.Tests/McpProtocolExceptionDataTests.cs
Co-authored-by: halter73 <54385+halter73@users.noreply.github.com>

Copilot AI commented Dec 1, 2025

Copy link
Copy Markdown
Contributor Author

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Comment thread src/ModelContextProtocol.Core/McpSessionHandler.cs Outdated
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Comment thread src/ModelContextProtocol.Core/McpSessionHandler.cs Outdated
…ent wrapper

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Comment thread src/ModelContextProtocol.Core/McpSessionHandler.cs Outdated
…bility

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@stephentoub

Copy link
Copy Markdown
Contributor

@copilot, please review the CI failures and fix them

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>

Copilot AI commented Dec 2, 2025

Copy link
Copy Markdown
Contributor Author

@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