Skip to content

Deprecate McpErrorCode.ResourceNotFound (-32002) and use McpErrorCode.InvalidParams (-32602) per SEP-2164#1558

Open
jayaraman-venkatesan wants to merge 5 commits into
modelcontextprotocol:mainfrom
jayaraman-venkatesan:feature/sep-2164-resource-not-found-error-code
Open

Deprecate McpErrorCode.ResourceNotFound (-32002) and use McpErrorCode.InvalidParams (-32602) per SEP-2164#1558
jayaraman-venkatesan wants to merge 5 commits into
modelcontextprotocol:mainfrom
jayaraman-venkatesan:feature/sep-2164-resource-not-found-error-code

Conversation

@jayaraman-venkatesan
Copy link
Copy Markdown
Contributor

@jayaraman-venkatesan jayaraman-venkatesan commented May 4, 2026

Fixes #1540

SEP-2164 standardizes the error code for "resource not found" from the MCP-specific -32002 to the standard JSON-RPC -32602 (Invalid Params), as part of the 2026-06-30 MCP spec release.

How Has This Been Tested?

  • Existing test suite passes locally (dotnet test) — no new tests were needed since the existing resource routing, resource builder, and exception propagation tests already cover the error code path; their assertions were updated to expect -32602.

Breaking Changes

McpErrorCode.ResourceNotFound is marked [Obsolete] but retained at its original value -32002 for backward compatibility with pre-2026-06-30 clients and servers that may still send or check for that code. New code should use McpErrorCode.InvalidParams (-32602).

Server implementors who currently throw McpErrorCode.ResourceNotFound will see a compiler warning directing them to migrate to McpErrorCode.InvalidParams.

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

  • McpErrorCode.ResourceNotFound = -32002 is marked [Obsolete] with a message pointing to InvalidParams
  • The default ReadResource handler in McpServerImpl now throws InvalidParams
  • McpErrorCode.InvalidParams docs updated to list Resources as a covered context
  • All internal usages (server, test servers, tests) migrated to InvalidParams

…rceNotFound (-32002) and use InvalidParams (-32602) per SEP-2164
@jayaraman-venkatesan
Copy link
Copy Markdown
Contributor Author

jayaraman-venkatesan commented May 6, 2026

@mikekistler , can you please help reviewing this.
I'm not able to request review.

@mikekistler
Copy link
Copy Markdown
Contributor

It seems like we could make this backward compatible if we conditioned the return code value on the protocol version. If the version is "DRAFT-2026-06-v1" (soon to be renamed to the actual spec date, currently expected to be 2026-06-30) or later, the return -32602, and otherwise return -32002. I realize this is going to be more complicated, but it would avoid potentially breaking clients using 2025-11-25 and earlier spec versions.

@jayaraman-venkatesan
Copy link
Copy Markdown
Contributor Author

@mikekistler, thanks for the review.

I was able to add a branching logic looking at the negotiated protocol version.

/// </remarks>
internal static bool UseSep2164ResourceErrors(string? protocolVersion)
{
const string MinSep2164ProtocolVersion = "2026-06-30";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for updating this. Now that #1553 has been merged, you can add this logic to McpHeaders.cs and reference the DRAFT-2026-v1 constant there. We might have to rename the constant to something a little more generic.

return false;
}

return string.Compare(protocolVersion, MinSep2164ProtocolVersion, StringComparison.Ordinal) >= 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For now, only use the InvalidParams error codes if there's an exact ordinal match with "DRAFT-2026-v1". We shouldn't be doing a >= comparison on strings.

/// draft string <c>"DRAFT-2026-06-v1"</c> because <c>'D'</c> sorts greater than <c>'2'</c>
/// in ASCII.
/// </remarks>
internal static bool UseSep2164ResourceErrors(string? protocolVersion)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
internal static bool UseSep2164ResourceErrors(string? protocolVersion)
internal static bool UseInvalidParamsForMissingResource(string? protocolVersion)

I think we should remove any mention of "SEP-2164" or SEPs in general from method names and comments. I know it's internal, but the individual SEPs won't matter much after this PR is merged.

{
var resource = McpServerResource.Create(options: new() { UriTemplate = uriTemplate }, method: method);
var client = await CreateClientWithResourcesAsync(resource);
var client = await CreateClientWithResourcesAndServerVersionAsync("2026-06-30", resource);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think we should update the generic "AssertNoMatchAsync" test helper method to use a draft protocol version. That should be explicitly opted into with theory data or at least a literal in the top-level test.

// Literal template braces in URI should not match (template literal is not a valid URI)
var mcpEx = await Assert.ThrowsAsync<McpProtocolException>(async () => await client.ReadResourceAsync("test://params{?a1,a2,a3}", null, TestContext.Current.CancellationToken));
Assert.Equal(McpErrorCode.ResourceNotFound, mcpEx.ErrorCode);
Assert.Equal(McpErrorCode.InvalidParams, mcpEx.ErrorCode);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's not update the generic tests to use the draft protocol version just yet. We can do this later. ResourceNotFound_ErrorCode_IsVersionGated should give us enough coverage for the InvalidParams logic.

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.

SEP-2164: Standardize resource not found error code (-32602)

4 participants