Skip to content

Commit 2b7c993

Browse files
authored
mcp: align resource not found error code with sep-2164 (#931)
Change the error code returned by ResourceNotFoundError from -32002 to -32602 (jsonrpc.CodeInvalidParams), aligning with [SEP-2164](modelcontextprotocol/modelcontextprotocol#2164). * Not gated with a protocol version check, because the previous error code was never formally specified and the behaviour varied across SDKs. * In case clients which rely on the old error code exist, the previous behaviour can be restored with `MCPGODEBUG=customresnotfounderrcode=1`. * `CodeResourceNotFound` deprecated since its only purpose going forward is backward compatibility. The new code should use `jsonrpc.CodeInvalidParams` directly. * `CodeResourceNotFound` changed to `var` so it can stay in sync with `ResourceNotFoundError` and existing code doing `code == mcp.CodeResourceNotFound` keeps working in both the default and compat modes.
1 parent ec54d41 commit 2b7c993

5 files changed

Lines changed: 62 additions & 2 deletions

File tree

docs/mcpgodebug.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818

1919
## `MCPGODEBUG` history
2020

21+
### 1.7.0
22+
23+
Options listed below were added and will be removed in the 1.9.0 version of the SDK.
24+
25+
- `customresnotfounderrcode` added. If set to `1`, `ResourceNotFoundError` will
26+
use the custom error code `-32002` instead of the standard `-32602` (Invalid
27+
Params), restoring the previous behavior. The default behavior was changed to
28+
align with SEP-2164 and the JSON-RPC specification.
29+
2130
### 1.6.0
2231

2332
Options listed below were added and will be removed in the 1.8.0 version of the SDK.

internal/docs/mcpgodebug.src.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717

1818
## `MCPGODEBUG` history
1919

20+
### 1.7.0
21+
22+
Options listed below were added and will be removed in the 1.9.0 version of the SDK.
23+
24+
- `customresnotfounderrcode` added. If set to `1`, `ResourceNotFoundError` will
25+
use the custom error code `-32002` instead of the standard `-32602` (Invalid
26+
Params), restoring the previous behavior. The default behavior was changed to
27+
align with SEP-2164 and the JSON-RPC specification.
28+
2029
### 1.6.0
2130

2231
Options listed below were added and will be removed in the 1.8.0 version of the SDK.

mcp/error_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ func TestServerErrors(t *testing.T) {
111111
}
112112
}
113113

114+
// TestResourceNotFoundErrorCode verifies ResourceNotFoundError code and
115+
// CodeResourceNotFound are -32602 (InvalidParams) per SEP-2164.
116+
func TestResourceNotFoundErrorCode(t *testing.T) {
117+
err := ResourceNotFoundError("file:///test.txt")
118+
var rpcErr *jsonrpc.Error
119+
if !errors.As(err, &rpcErr) {
120+
t.Fatalf("got error type %T, want *jsonrpc.Error", err)
121+
}
122+
if rpcErr.Code != jsonrpc.CodeInvalidParams {
123+
t.Errorf("got error code %d, want %d (InvalidParams)", rpcErr.Code, jsonrpc.CodeInvalidParams)
124+
}
125+
if CodeResourceNotFound != jsonrpc.CodeInvalidParams {
126+
t.Errorf("CodeResourceNotFound = %d, want %d", CodeResourceNotFound, jsonrpc.CodeInvalidParams)
127+
}
128+
}
129+
114130
// TestInputValidationToolError validates that input validation errors (missing
115131
// required params, wrong types) are returned as tool results with IsError=true,
116132
// not as JSON-RPC errors. This allows LLMs to see the error and self-correct.

mcp/resource.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"path/filepath"
1616
"strings"
1717

18+
"github.com/modelcontextprotocol/go-sdk/internal/mcpgodebug"
1819
"github.com/modelcontextprotocol/go-sdk/internal/util"
1920
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
2021
"github.com/yosida95/uritemplate/v3"
@@ -37,8 +38,25 @@ type serverResourceTemplate struct {
3738
// If it cannot find the resource, it should return the result of calling [ResourceNotFoundError].
3839
type ResourceHandler func(context.Context, *ReadResourceRequest) (*ReadResourceResult, error)
3940

41+
// customresnotfounderrcode is a compatibility parameter that restores the
42+
// pre-1.7.0 behavior of [ResourceNotFoundError] and [CodeResourceNotFound],
43+
// where the error code was a custom -32002. See the documentation for the mcpgodebug
44+
// package for instructions on how to enable it.
45+
// The option will be removed in the future version of the SDK.
46+
var customresnotfounderrcode = mcpgodebug.Value("customresnotfounderrcode")
47+
48+
func init() {
49+
if customresnotfounderrcode == "1" {
50+
CodeResourceNotFound = -32002
51+
}
52+
}
53+
4054
// ResourceNotFoundError returns an error indicating that a resource being read could
4155
// not be found.
56+
//
57+
// By default, the error code is -32602 (Invalid Params), as specified in the
58+
// MCP specification (SEP-2164). To restore the pre-1.7.0 release behavior where the
59+
// error code was -32002, set MCPGODEBUG=customresnotfounderrcode=1.
4260
func ResourceNotFoundError(uri string) error {
4361
return &jsonrpc.Error{
4462
Code: CodeResourceNotFound,

mcp/shared.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,22 @@ const (
347347
// CodeHeaderMismatch indicates that HTTP headers do not match the corresponding values
348348
// in the request body, or that required headers are missing or malformed.
349349
CodeHeaderMismatch = -32001
350-
// CodeResourceNotFound indicates that a requested resource could not be found.
351-
CodeResourceNotFound = -32002
352350
// CodeURLElicitationRequired indicates that the server requires URL elicitation
353351
// before processing the request. The client should execute the elicitation handler
354352
// with the elicitations provided in the error data.
355353
CodeURLElicitationRequired = -32042
356354
)
357355

356+
// CodeResourceNotFound indicates that a requested resource could not be found.
357+
//
358+
// By default, the value is -32602 (Invalid Params), as specified in the
359+
// MCP specification (SEP-2164). To restore the pre-1.7.0 release behavior where the
360+
// error code was -32002, set MCPGODEBUG=customresnotfounderrcode=1.
361+
//
362+
// Deprecated: Use [jsonrpc.CodeInvalidParams] directly. This variable will be
363+
// removed in a future version.
364+
var CodeResourceNotFound int64 = jsonrpc.CodeInvalidParams
365+
358366
// URLElicitationRequiredError returns an error indicating that URL elicitation is required
359367
// before the request can be processed. The elicitations parameter should contain the
360368
// elicitation requests that must be completed.

0 commit comments

Comments
 (0)