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](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/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.
This commit is contained in:
parent
ec54d41a17
commit
2b7c9937e6
1 changed files with 62 additions and 2 deletions
|
|
@ -111,6 +111,22 @@ func TestServerErrors(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestResourceNotFoundErrorCode verifies ResourceNotFoundError code and
|
||||
// CodeResourceNotFound are -32602 (InvalidParams) per SEP-2164.
|
||||
func TestResourceNotFoundErrorCode(t *testing.T) {
|
||||
err := ResourceNotFoundError("file:///test.txt")
|
||||
var rpcErr *jsonrpc.Error
|
||||
if !errors.As(err, &rpcErr) {
|
||||
t.Fatalf("got error type %T, want *jsonrpc.Error", err)
|
||||
}
|
||||
if rpcErr.Code != jsonrpc.CodeInvalidParams {
|
||||
t.Errorf("got error code %d, want %d (InvalidParams)", rpcErr.Code, jsonrpc.CodeInvalidParams)
|
||||
}
|
||||
if CodeResourceNotFound != jsonrpc.CodeInvalidParams {
|
||||
t.Errorf("CodeResourceNotFound = %d, want %d", CodeResourceNotFound, jsonrpc.CodeInvalidParams)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInputValidationToolError validates that input validation errors (missing
|
||||
// required params, wrong types) are returned as tool results with IsError=true,
|
||||
// not as JSON-RPC errors. This allows LLMs to see the error and self-correct.
|
||||
|
|
|
|||
Loading…
Reference in a new issue