-
Notifications
You must be signed in to change notification settings - Fork 711
feat: implement MCP elicitation support (#413) #495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b7a18c5
feat: implement MCP elicitation support (#413)
semistrict 8986c49
Address review comments and auto-format
semistrict 988dd4c
Address further minor review comments
semistrict 8f5bcf8
Add sentinel errors
semistrict 2945ac5
Revert sampling formatting changes
semistrict 1d01db9
Update elicitation response to match spec
miguelb-gk 029f247
Merge pull request #2 from gitkraken/feat/elicitation
semistrict File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/mark3labs/mcp-go/mcp" | ||
| ) | ||
|
|
||
| // ElicitationHandler defines the interface for handling elicitation requests from servers. | ||
| // Clients can implement this interface to request additional information from users. | ||
| type ElicitationHandler interface { | ||
| // Elicit handles an elicitation request from the server and returns the user's response. | ||
| // The implementation should: | ||
| // 1. Present the request message to the user | ||
| // 2. Validate input against the requested schema | ||
| // 3. Allow the user to accept, decline, or cancel | ||
| // 4. Return the appropriate response | ||
| Elicit(ctx context.Context, request mcp.ElicitationRequest) (*mcp.ElicitationResult, error) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,225 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/mark3labs/mcp-go/client/transport" | ||
| "github.com/mark3labs/mcp-go/mcp" | ||
| ) | ||
|
|
||
| // mockElicitationHandler implements ElicitationHandler for testing | ||
| type mockElicitationHandler struct { | ||
| result *mcp.ElicitationResult | ||
| err error | ||
| } | ||
|
|
||
| func (m *mockElicitationHandler) Elicit(ctx context.Context, request mcp.ElicitationRequest) (*mcp.ElicitationResult, error) { | ||
| if m.err != nil { | ||
| return nil, m.err | ||
| } | ||
| return m.result, nil | ||
| } | ||
|
|
||
| func TestClient_HandleElicitationRequest(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| handler ElicitationHandler | ||
| expectedError string | ||
| }{ | ||
| { | ||
| name: "no handler configured", | ||
| handler: nil, | ||
| expectedError: "no elicitation handler configured", | ||
| }, | ||
| { | ||
| name: "successful elicitation - accept", | ||
| handler: &mockElicitationHandler{ | ||
| result: &mcp.ElicitationResult{ | ||
| ElicitationResponse: mcp.ElicitationResponse{ | ||
| Action: mcp.ElicitationResponseActionAccept, | ||
| Content: map[string]any{ | ||
| "name": "test-project", | ||
| "framework": "react", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "successful elicitation - decline", | ||
| handler: &mockElicitationHandler{ | ||
| result: &mcp.ElicitationResult{ | ||
| ElicitationResponse: mcp.ElicitationResponse{ | ||
| Action: mcp.ElicitationResponseActionDecline, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "successful elicitation - cancel", | ||
| handler: &mockElicitationHandler{ | ||
| result: &mcp.ElicitationResult{ | ||
| ElicitationResponse: mcp.ElicitationResponse{ | ||
| Action: mcp.ElicitationResponseActionCancel, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "handler returns error", | ||
| handler: &mockElicitationHandler{ | ||
| err: fmt.Errorf("user interaction failed"), | ||
| }, | ||
| expectedError: "user interaction failed", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| client := &Client{elicitationHandler: tt.handler} | ||
|
|
||
| request := transport.JSONRPCRequest{ | ||
| ID: mcp.NewRequestId(1), | ||
| Method: string(mcp.MethodElicitationCreate), | ||
| Params: map[string]any{ | ||
| "message": "Please provide project details", | ||
| "requestedSchema": map[string]any{ | ||
| "type": "object", | ||
| "properties": map[string]any{ | ||
| "name": map[string]any{"type": "string"}, | ||
| "framework": map[string]any{"type": "string"}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| result, err := client.handleElicitationRequestTransport(context.Background(), request) | ||
|
|
||
| if tt.expectedError != "" { | ||
| if err == nil { | ||
| t.Errorf("expected error %q, got nil", tt.expectedError) | ||
| } else if err.Error() != tt.expectedError { | ||
| t.Errorf("expected error %q, got %q", tt.expectedError, err.Error()) | ||
| } | ||
| } else { | ||
| if err != nil { | ||
| t.Errorf("unexpected error: %v", err) | ||
| } | ||
| if result == nil { | ||
| t.Error("expected result, got nil") | ||
| } else { | ||
| // Verify the response is properly formatted | ||
| var elicitationResult mcp.ElicitationResult | ||
| if err := json.Unmarshal(result.Result, &elicitationResult); err != nil { | ||
| t.Errorf("failed to unmarshal result: %v", err) | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestWithElicitationHandler(t *testing.T) { | ||
| handler := &mockElicitationHandler{} | ||
| client := &Client{} | ||
|
|
||
| option := WithElicitationHandler(handler) | ||
| option(client) | ||
|
|
||
| if client.elicitationHandler != handler { | ||
| t.Error("elicitation handler not set correctly") | ||
| } | ||
| } | ||
|
|
||
| func TestClient_Initialize_WithElicitationHandler(t *testing.T) { | ||
| mockTransport := &mockElicitationTransport{ | ||
| sendRequestFunc: func(ctx context.Context, request transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) { | ||
| // Verify that elicitation capability is included | ||
| // The client internally converts the typed params to a map for transport | ||
| // So we check if we're getting the initialize request | ||
| if request.Method != "initialize" { | ||
| t.Fatalf("expected initialize method, got %s", request.Method) | ||
| } | ||
|
|
||
| // Return successful initialization response | ||
| result := mcp.InitializeResult{ | ||
| ProtocolVersion: mcp.LATEST_PROTOCOL_VERSION, | ||
| ServerInfo: mcp.Implementation{ | ||
| Name: "test-server", | ||
| Version: "1.0.0", | ||
| }, | ||
| Capabilities: mcp.ServerCapabilities{}, | ||
| } | ||
|
|
||
| resultBytes, _ := json.Marshal(result) | ||
| return &transport.JSONRPCResponse{ | ||
| ID: request.ID, | ||
| Result: json.RawMessage(resultBytes), | ||
| }, nil | ||
| }, | ||
| sendNotificationFunc: func(ctx context.Context, notification mcp.JSONRPCNotification) error { | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| handler := &mockElicitationHandler{} | ||
| client := NewClient(mockTransport, WithElicitationHandler(handler)) | ||
|
|
||
| err := client.Start(context.Background()) | ||
| if err != nil { | ||
| t.Fatalf("failed to start client: %v", err) | ||
| } | ||
|
|
||
| _, err = client.Initialize(context.Background(), mcp.InitializeRequest{ | ||
| Params: mcp.InitializeParams{ | ||
| ProtocolVersion: mcp.LATEST_PROTOCOL_VERSION, | ||
| ClientInfo: mcp.Implementation{ | ||
| Name: "test-client", | ||
| Version: "1.0.0", | ||
| }, | ||
| Capabilities: mcp.ClientCapabilities{}, | ||
| }, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("failed to initialize: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // mockElicitationTransport implements transport.Interface for testing | ||
| type mockElicitationTransport struct { | ||
| sendRequestFunc func(context.Context, transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) | ||
| sendNotificationFunc func(context.Context, mcp.JSONRPCNotification) error | ||
| } | ||
|
|
||
| func (m *mockElicitationTransport) Start(ctx context.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockElicitationTransport) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockElicitationTransport) SendRequest(ctx context.Context, request transport.JSONRPCRequest) (*transport.JSONRPCResponse, error) { | ||
| if m.sendRequestFunc != nil { | ||
| return m.sendRequestFunc(ctx, request) | ||
| } | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (m *mockElicitationTransport) SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error { | ||
| if m.sendNotificationFunc != nil { | ||
| return m.sendNotificationFunc(ctx, notification) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockElicitationTransport) SetNotificationHandler(handler func(mcp.JSONRPCNotification)) { | ||
| } | ||
|
|
||
| func (m *mockElicitationTransport) GetSessionId() string { | ||
| return "mock-session" | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.