| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package mcp |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
"testing/synctest" |
| 13 |
|
| 14 |
"github.com/google/jsonschema-go/jsonschema" |
| 15 |
"github.com/modelcontextprotocol/go-sdk/jsonrpc" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
func TestElicitationURLMode(t *testing.T) { |
| 21 |
ctx := context.Background() |
| 22 |
clientErr := errors.New("client failed to elicit") |
| 23 |
|
| 24 |
testCases := []struct { |
| 25 |
name string |
| 26 |
handler func(context.Context, *ElicitRequest) (*ElicitResult, error) |
| 27 |
params *ElicitParams |
| 28 |
wantResultAction string |
| 29 |
wantErrMsg string |
| 30 |
wantErrCode int64 |
| 31 |
}{ |
| 32 |
{ |
| 33 |
name: "success", |
| 34 |
handler: func(ctx context.Context, req *ElicitRequest) (*ElicitResult, error) { |
| 35 |
return &ElicitResult{Action: "accept"}, nil |
| 36 |
}, |
| 37 |
params: &ElicitParams{ |
| 38 |
Mode: "url", |
| 39 |
Message: "Please provide information via URL", |
| 40 |
URL: "https://example.com/form", |
| 41 |
}, |
| 42 |
wantResultAction: "accept", |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "decline", |
| 46 |
handler: func(ctx context.Context, req *ElicitRequest) (*ElicitResult, error) { |
| 47 |
return &ElicitResult{Action: "decline"}, nil |
| 48 |
}, |
| 49 |
params: &ElicitParams{ |
| 50 |
Mode: "url", |
| 51 |
Message: "Please provide information via URL", |
| 52 |
URL: "https://example.com/form", |
| 53 |
}, |
| 54 |
wantResultAction: "decline", |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "client error", |
| 58 |
handler: func(ctx context.Context, req *ElicitRequest) (*ElicitResult, error) { |
| 59 |
return nil, clientErr |
| 60 |
}, |
| 61 |
params: &ElicitParams{ |
| 62 |
Mode: "url", |
| 63 |
Message: "This should fail", |
| 64 |
URL: "https://example.com/form", |
| 65 |
}, |
| 66 |
wantErrMsg: clientErr.Error(), |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "missing url", |
| 70 |
handler: func(ctx context.Context, req *ElicitRequest) (*ElicitResult, error) { |
| 71 |
return &ElicitResult{Action: "accept"}, nil |
| 72 |
}, |
| 73 |
params: &ElicitParams{ |
| 74 |
Mode: "url", |
| 75 |
Message: "URL is missing", |
| 76 |
}, |
| 77 |
wantErrMsg: "URL must be set for URL elicitation", |
| 78 |
wantErrCode: jsonrpc.CodeInvalidParams, |
| 79 |
}, |
| 80 |
{ |
| 81 |
name: "schema not allowed", |
| 82 |
handler: func(ctx context.Context, req *ElicitRequest) (*ElicitResult, error) { |
| 83 |
return &ElicitResult{Action: "accept"}, nil |
| 84 |
}, |
| 85 |
params: &ElicitParams{ |
| 86 |
Mode: "url", |
| 87 |
Message: "Schema is not allowed", |
| 88 |
URL: "https://example.com/form", |
| 89 |
RequestedSchema: &jsonschema.Schema{ |
| 90 |
Type: "object", |
| 91 |
}, |
| 92 |
}, |
| 93 |
wantErrMsg: "requestedSchema must not be set for URL elicitation", |
| 94 |
wantErrCode: jsonrpc.CodeInvalidParams, |
| 95 |
}, |
| 96 |
} |
| 97 |
for _, tc := range testCases { |
| 98 |
t.Run(tc.name, func(t *testing.T) { |
| 99 |
ct, st := NewInMemoryTransports() |
| 100 |
s := NewServer(testImpl, nil) |
| 101 |
ss, err := s.Connect(ctx, st, nil) |
| 102 |
if err != nil { |
| 103 |
t.Fatal(err) |
| 104 |
} |
| 105 |
defer ss.Close() |
| 106 |
|
| 107 |
c := NewClient(testImpl, &ClientOptions{ |
| 108 |
Capabilities: &ClientCapabilities{ |
| 109 |
Roots: RootCapabilities{ListChanged: true}, |
| 110 |
RootsV2: &RootCapabilities{ListChanged: true}, |
| 111 |
Elicitation: &ElicitationCapabilities{ |
| 112 |
URL: &URLElicitationCapabilities{}, |
| 113 |
}, |
| 114 |
}, |
| 115 |
ElicitationHandler: tc.handler, |
| 116 |
}) |
| 117 |
cs, err := c.Connect(ctx, ct, nil) |
| 118 |
if err != nil { |
| 119 |
t.Fatal(err) |
| 120 |
} |
| 121 |
defer cs.Close() |
| 122 |
|
| 123 |
result, err := ss.Elicit(ctx, tc.params) |
| 124 |
|
| 125 |
if tc.wantErrMsg != "" { |
| 126 |
if err == nil || !strings.Contains(err.Error(), tc.wantErrMsg) { |
| 127 |
t.Fatalf("Elicit(...): got error %v, want containing %q", err, tc.wantErrMsg) |
| 128 |
} |
| 129 |
if tc.wantErrCode != 0 { |
| 130 |
if code := errorCode(err); code != tc.wantErrCode { |
| 131 |
t.Errorf("Elicit(...): got error code %d, want %d", code, tc.wantErrCode) |
| 132 |
} |
| 133 |
} |
| 134 |
} else { |
| 135 |
if err != nil { |
| 136 |
t.Fatalf("Elicit failed: %v", err) |
| 137 |
} |
| 138 |
if result.Action != tc.wantResultAction { |
| 139 |
t.Errorf("Elicit(...): got action %q, want %q", result.Action, tc.wantResultAction) |
| 140 |
} |
| 141 |
} |
| 142 |
}) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func TestElicitationCompleteNotification(t *testing.T) { |
| 147 |
synctest.Test(t, func(t *testing.T) { |
| 148 |
ctx := context.Background() |
| 149 |
|
| 150 |
var elicitationCompleteCh = make(chan *ElicitationCompleteParams, 1) |
| 151 |
|
| 152 |
c := NewClient(testImpl, &ClientOptions{ |
| 153 |
Capabilities: &ClientCapabilities{ |
| 154 |
Roots: RootCapabilities{ListChanged: true}, |
| 155 |
RootsV2: &RootCapabilities{ListChanged: true}, |
| 156 |
Elicitation: &ElicitationCapabilities{ |
| 157 |
URL: &URLElicitationCapabilities{}, |
| 158 |
}, |
| 159 |
}, |
| 160 |
ElicitationHandler: func(context.Context, *ElicitRequest) (*ElicitResult, error) { |
| 161 |
return &ElicitResult{Action: "accept"}, nil |
| 162 |
}, |
| 163 |
ElicitationCompleteHandler: func(_ context.Context, req *ElicitationCompleteNotificationRequest) { |
| 164 |
elicitationCompleteCh <- req.Params |
| 165 |
}, |
| 166 |
}) |
| 167 |
|
| 168 |
_, ss, cleanup := basicClientServerConnection(t, c, nil, nil) |
| 169 |
defer cleanup() |
| 170 |
|
| 171 |
|
| 172 |
elicitID := "testElicitationID-123" |
| 173 |
resp, err := ss.Elicit(ctx, &ElicitParams{ |
| 174 |
Mode: "url", |
| 175 |
Message: "Please complete this form: ", |
| 176 |
URL: "https://example.com/form?id=" + elicitID, |
| 177 |
ElicitationID: elicitID, |
| 178 |
}) |
| 179 |
if err != nil { |
| 180 |
t.Fatalf("Elicit failed: %v", err) |
| 181 |
} |
| 182 |
if resp.Action != "accept" { |
| 183 |
t.Fatalf("Elicit action is %q, want %q", resp.Action, "accept") |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
err = handleNotify(ctx, notificationElicitationComplete, newServerRequest(ss, &ElicitationCompleteParams{ |
| 188 |
ElicitationID: elicitID, |
| 189 |
})) |
| 190 |
if err != nil { |
| 191 |
t.Fatalf("failed to send elicitation complete notification: %v", err) |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
gotParams := <-elicitationCompleteCh |
| 196 |
if gotParams.ElicitationID != elicitID { |
| 197 |
t.Errorf("elicitationComplete notification ID mismatch: got %q, want %q", gotParams.ElicitationID, elicitID) |
| 198 |
} |
| 199 |
}) |
| 200 |
} |
| 201 |
|
| 202 |
func TestElicitationNoValidationWithoutAccept(t *testing.T) { |
| 203 |
ctx := context.Background() |
| 204 |
|
| 205 |
schema := &jsonschema.Schema{ |
| 206 |
Type: "object", |
| 207 |
Properties: map[string]*jsonschema.Schema{ |
| 208 |
"test": {Type: "string"}, |
| 209 |
}, |
| 210 |
Required: []string{"test"}, |
| 211 |
} |
| 212 |
|
| 213 |
testCases := []struct { |
| 214 |
name string |
| 215 |
action string |
| 216 |
content map[string]any |
| 217 |
wantAction string |
| 218 |
}{ |
| 219 |
{ |
| 220 |
name: "cancel action", |
| 221 |
action: "cancel", |
| 222 |
content: nil, |
| 223 |
wantAction: "cancel", |
| 224 |
}, |
| 225 |
{ |
| 226 |
name: "decline action", |
| 227 |
action: "decline", |
| 228 |
content: map[string]any{}, |
| 229 |
wantAction: "decline", |
| 230 |
}, |
| 231 |
} |
| 232 |
|
| 233 |
for _, tc := range testCases { |
| 234 |
t.Run(tc.name, func(t *testing.T) { |
| 235 |
ct, st := NewInMemoryTransports() |
| 236 |
s := NewServer(testImpl, nil) |
| 237 |
ss, err := s.Connect(ctx, st, nil) |
| 238 |
if err != nil { |
| 239 |
t.Fatal(err) |
| 240 |
} |
| 241 |
defer ss.Close() |
| 242 |
|
| 243 |
c := NewClient(testImpl, &ClientOptions{ |
| 244 |
ElicitationHandler: func(context.Context, *ElicitRequest) (*ElicitResult, error) { |
| 245 |
return &ElicitResult{Action: tc.action, Content: tc.content}, nil |
| 246 |
}, |
| 247 |
}) |
| 248 |
cs, err := c.Connect(ctx, ct, nil) |
| 249 |
if err != nil { |
| 250 |
t.Fatal(err) |
| 251 |
} |
| 252 |
defer cs.Close() |
| 253 |
|
| 254 |
res, err := ss.Elicit(ctx, &ElicitParams{ |
| 255 |
Message: "Test bug", |
| 256 |
RequestedSchema: schema, |
| 257 |
}) |
| 258 |
|
| 259 |
if err != nil { |
| 260 |
t.Fatalf("Elicit failed: %v", err) |
| 261 |
} |
| 262 |
|
| 263 |
if res.Action != tc.wantAction { |
| 264 |
t.Errorf("Expected action %q, got %q", tc.wantAction, res.Action) |
| 265 |
} |
| 266 |
}) |
| 267 |
} |
| 268 |
} |