-
Notifications
You must be signed in to change notification settings - Fork 696
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
Open
semistrict
wants to merge
1
commit into
mark3labs:main
Choose a base branch
from
semistrict:feat/elicitation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,296
−38
Open
Changes from all commits
Commits
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{ | ||
Response: mcp.ElicitationResponse{ | ||
Type: mcp.ElicitationResponseTypeAccept, | ||
Value: map[string]interface{}{ | ||
"name": "test-project", | ||
"framework": "react", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "successful elicitation - decline", | ||
handler: &mockElicitationHandler{ | ||
result: &mcp.ElicitationResult{ | ||
Response: mcp.ElicitationResponse{ | ||
Type: mcp.ElicitationResponseTypeDecline, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "successful elicitation - cancel", | ||
handler: &mockElicitationHandler{ | ||
result: &mcp.ElicitationResult{ | ||
Response: mcp.ElicitationResponse{ | ||
Type: mcp.ElicitationResponseTypeCancel, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
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]interface{}{ | ||
"message": "Please provide project details", | ||
"requestedSchema": map[string]interface{}{ | ||
"type": "object", | ||
"properties": map[string]interface{}{ | ||
"name": map[string]interface{}{"type": "string"}, | ||
"framework": map[string]interface{}{"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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Enhance test to verify elicitation capability declaration.
The test sets up client initialization properly but doesn't actually verify that elicitation capability is declared. The comment on line 140 mentions this verification, but it's not implemented.
This ensures the test actually verifies the capability declaration functionality.
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes no sense to do this check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JBUinfo I understand your perspective. You're right to push back on that suggestion. The test appears to be focused on verifying that client initialization works properly when an elicitation handler is configured, rather than testing the detailed capability negotiation mechanics. The current test serves its purpose by ensuring the initialization flow completes successfully with the handler present.
Thank you for the clarification!