all: expose JSON-RPC error codes, and support URLElicitationRequired errors (#655)

Expose the jsonrpc.Error type to allow access to underlying JSON-RPC
error codes. Also, expose common JSON-RPC error codes in the jsonrpc
package, and MCP error codes in the mcp package.

Also, implement support for the MCP URL elicitation required error
(-32042), enabling servers to request out-of-band user authorization and
clients to automatically handle these requests with retry logic.

Server-side:
- Add CodeURLElicitationRequired constant
- Add URLElicitationRequiredError() constructor.

Client-side:
- Add urlElicitationMiddleware to intercept URLElicitationRequired
  errors and retry calls.
- Update callElicitationCompleteHandler to signal waiting operations.
- Fix capability advertisement to support both form and URL modes.

Fixes #452
Fixes #623
This commit is contained in:
Robert Findley 2025-12-01 11:29:17 -05:00 • committed by GitHub
commit 2ce44d45e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -14,7 +14,7 @@ import (
"testing"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
)
func TestApplySchema(t *testing.T) {
@ -65,8 +65,8 @@ func TestToolErrorHandling(t *testing.T) {
// Create a tool that returns a structured error
structuredErrorHandler := func(ctx context.Context, req *CallToolRequest, args map[string]any) (*CallToolResult, any, error) {
return nil, nil, &jsonrpc2.WireError{
Code: codeInvalidParams,
return nil, nil, &jsonrpc.Error{
Code: jsonrpc.CodeInvalidParams,
Message: "internal server error",
}
}
@ -106,13 +106,13 @@ func TestToolErrorHandling(t *testing.T) {
t.Fatal("expected error, got nil")
}
var wireErr *jsonrpc2.WireError
var wireErr *jsonrpc.Error
if !errors.As(err, &wireErr) {
t.Fatalf("expected WireError, got %[1]T: %[1]v", err)
t.Fatalf("expected jsonrpc.Error, got %[1]T: %[1]v", err)
}
if wireErr.Code != codeInvalidParams {
t.Errorf("expected error code %d, got %d", codeInvalidParams, wireErr.Code)
if wireErr.Code != jsonrpc.CodeInvalidParams {
t.Errorf("expected error code %d, got %d", jsonrpc.CodeInvalidParams, wireErr.Code)
}
})