|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/mark3labs/mcp-go/client/transport" |
| 10 | + "github.com/mark3labs/mcp-go/mcp" |
| 11 | +) |
| 12 | + |
| 13 | +// mockElicitationHandler implements ElicitationHandler for testing |
| 14 | +type mockElicitationHandler struct { |
| 15 | + result *mcp.ElicitationResult |
| 16 | + err error |
| 17 | +} |
| 18 | + |
| 19 | +func (m *mockElicitationHandler) Elicit(ctx context.Context, request mcp.ElicitationRequest) (*mcp.ElicitationResult, error) { |
| 20 | + if m.err != nil { |
| 21 | + return nil, m.err |
| 22 | + } |
| 23 | + return m.result, nil |
| 24 | +} |
| 25 | + |
| 26 | +func TestClient_HandleElicitationRequest(t *testing.T) { |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + handler ElicitationHandler |
| 30 | + expectedError string |
| 31 | + }{ |
| 32 | + { |
| 33 | + name: "no handler configured", |
| 34 | + handler: nil, |
| 35 | + expectedError: "no elicitation handler configured", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "successful elicitation - accept", |
| 39 | + handler: &mockElicitationHandler{ |
| 40 | + result: &mcp.ElicitationResult{ |
| 41 | + Response: mcp.ElicitationResponse{ |
| 42 | + Type: mcp.ElicitationResponseTypeAccept, |
| 43 | + Value: map[string]interface{}{ |
| 44 | + "name": "test-project", |
| 45 | + "framework": "react", |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "successful elicitation - decline", |
| 53 | + handler: &mockElicitationHandler{ |
| 54 | + result: &mcp.ElicitationResult{ |
| 55 | + Response: mcp.ElicitationResponse{ |
| 56 | + Type: mcp.ElicitationResponseTypeDecline, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "successful elicitation - cancel", |
| 63 | + handler: &mockElicitationHandler{ |
| 64 | + result: &mcp.ElicitationResult{ |
| 65 | + Response: mcp.ElicitationResponse{ |
| 66 | + Type: mcp.ElicitationResponseTypeCancel, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "handler returns error", |
| 73 | + handler: &mockElicitationHandler{ |
| 74 | + err: fmt.Errorf("user interaction failed"), |
| 75 | + }, |
| 76 | + expectedError: "user interaction failed", |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + client := &Client{elicitationHandler: tt.handler} |
| 83 | + |
| 84 | + request := transport.JSONRPCRequest{ |
| 85 | + ID: mcp.NewRequestId(1), |
| 86 | + Method: string(mcp.MethodElicitationCreate), |
| 87 | + Params: map[string]interface{}{ |
| 88 | + "message": "Please provide project details", |
| 89 | + "requestedSchema": map[string]interface{}{ |
| 90 | + "type": "object", |
| 91 | + "properties": map[string]interface{}{ |
| 92 | + "name": map[string]interface{}{"type": "string"}, |
| 93 | + "framework": map[string]interface{}{"type": "string"}, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + result, err := client.handleElicitationRequestTransport(context.Background(), request) |
| 100 | + |
| 101 | + if tt.expectedError != "" { |
| 102 | + if err == nil { |
| 103 | + t.Errorf("expected error %q, got nil", tt.expectedError) |
| 104 | + } else if err.Error() != tt.expectedError { |
| 105 | + t.Errorf("expected error %q, got %q", tt.expectedError, err.Error()) |
| 106 | + } |
| 107 | + } else { |
| 108 | + if err != nil { |
| 109 | + t.Errorf("unexpected error: %v", err) |
| 110 | + } |
| 111 | + if result == nil { |
| 112 | + t.Error("expected result, got nil") |
| 113 | + } else { |
| 114 | + // Verify the response is properly formatted |
| 115 | + var elicitationResult mcp.ElicitationResult |
| 116 | + if err := json.Unmarshal(result.Result, &elicitationResult); err != nil { |
| 117 | + t.Errorf("failed to unmarshal result: %v", err) |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestWithElicitationHandler(t *testing.T) { |
| 126 | + handler := &mockElicitationHandler{} |
| 127 | + client := &Client{} |
| 128 | + |
| 129 | + option := WithElicitationHandler(handler) |
| 130 | + option(client) |
| 131 | + |
| 132 | + if client.elicitationHandler != handler { |
| 133 | + t.Error("elicitation handler not set correctly") |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestClient_Initialize_WithElicitationHandler(t *testing.T) { |
| 138 | + mockTransport := &mockElicitationTransport{ |
| 139 | + sendRequestFunc: func(ctx context.Context, request transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) { |
| 140 | + // Verify that elicitation capability is included |
| 141 | + // The client internally converts the typed params to a map for transport |
| 142 | + // So we check if we're getting the initialize request |
| 143 | + if request.Method != "initialize" { |
| 144 | + t.Fatalf("expected initialize method, got %s", request.Method) |
| 145 | + } |
| 146 | + |
| 147 | + // Return successful initialization response |
| 148 | + result := mcp.InitializeResult{ |
| 149 | + ProtocolVersion: mcp.LATEST_PROTOCOL_VERSION, |
| 150 | + ServerInfo: mcp.Implementation{ |
| 151 | + Name: "test-server", |
| 152 | + Version: "1.0.0", |
| 153 | + }, |
| 154 | + Capabilities: mcp.ServerCapabilities{}, |
| 155 | + } |
| 156 | + |
| 157 | + resultBytes, _ := json.Marshal(result) |
| 158 | + return &transport.JSONRPCResponse{ |
| 159 | + ID: request.ID, |
| 160 | + Result: json.RawMessage(resultBytes), |
| 161 | + }, nil |
| 162 | + }, |
| 163 | + sendNotificationFunc: func(ctx context.Context, notification mcp.JSONRPCNotification) error { |
| 164 | + return nil |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + handler := &mockElicitationHandler{} |
| 169 | + client := NewClient(mockTransport, WithElicitationHandler(handler)) |
| 170 | + |
| 171 | + err := client.Start(context.Background()) |
| 172 | + if err != nil { |
| 173 | + t.Fatalf("failed to start client: %v", err) |
| 174 | + } |
| 175 | + |
| 176 | + _, err = client.Initialize(context.Background(), mcp.InitializeRequest{ |
| 177 | + Params: mcp.InitializeParams{ |
| 178 | + ProtocolVersion: mcp.LATEST_PROTOCOL_VERSION, |
| 179 | + ClientInfo: mcp.Implementation{ |
| 180 | + Name: "test-client", |
| 181 | + Version: "1.0.0", |
| 182 | + }, |
| 183 | + Capabilities: mcp.ClientCapabilities{}, |
| 184 | + }, |
| 185 | + }) |
| 186 | + |
| 187 | + if err != nil { |
| 188 | + t.Fatalf("failed to initialize: %v", err) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +// mockElicitationTransport implements transport.Interface for testing |
| 193 | +type mockElicitationTransport struct { |
| 194 | + sendRequestFunc func(context.Context, transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) |
| 195 | + sendNotificationFunc func(context.Context, mcp.JSONRPCNotification) error |
| 196 | +} |
| 197 | + |
| 198 | +func (m *mockElicitationTransport) Start(ctx context.Context) error { |
| 199 | + return nil |
| 200 | +} |
| 201 | + |
| 202 | +func (m *mockElicitationTransport) Close() error { |
| 203 | + return nil |
| 204 | +} |
| 205 | + |
| 206 | +func (m *mockElicitationTransport) SendRequest(ctx context.Context, request transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) { |
| 207 | + if m.sendRequestFunc != nil { |
| 208 | + return m.sendRequestFunc(ctx, request) |
| 209 | + } |
| 210 | + return nil, nil |
| 211 | +} |
| 212 | + |
| 213 | +func (m *mockElicitationTransport) SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error { |
| 214 | + if m.sendNotificationFunc != nil { |
| 215 | + return m.sendNotificationFunc(ctx, notification) |
| 216 | + } |
| 217 | + return nil |
| 218 | +} |
| 219 | + |
| 220 | +func (m *mockElicitationTransport) SetNotificationHandler(handler func(mcp.JSONRPCNotification)) { |
| 221 | +} |
| 222 | + |
| 223 | +func (m *mockElicitationTransport) GetSessionId() string { |
| 224 | + return "mock-session" |
| 225 | +} |
0 commit comments