mcp: infer elicit mode from ElicitParams (#1078)

Fixes #1072
This commit is contained in:
Guglielmo Colombo 2026-07-13 11:49:15 +02:00 • committed by GitHub
commit 40f034322f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 20 additions and 10 deletions

View file

@ -63,6 +63,9 @@ func (m InputRequestMap) MarshalJSON() ([]byte, error) {
if err != nil {
return nil, err
}
if ep, ok := v.(*ElicitParams); ok {
v = ep.inferElicitMode()
}
converted[k] = &wire{Method: method, Params: v}
}
return json.Marshal(converted)
@ -2126,6 +2129,21 @@ func (x *ElicitParams) isNil() bool { return x == nil }
func (x *ElicitParams) GetProgressToken() any { return getProgressToken(x) }
func (x *ElicitParams) SetProgressToken(t any) { setProgressToken(x, t) }
// inferElicitMode returns x with Mode populated by inference if it was empty.
// Mode is inferred as "url" when URL or ElicitationID is set, otherwise "form".
func (x *ElicitParams) inferElicitMode() *ElicitParams {
if x == nil || x.Mode != "" {
return x
}
x2 := *x
if x.URL != "" || x.ElicitationID != "" {
x2.Mode = "url"
} else {
x2.Mode = "form"
}
return &x2
}
// The client's response to an elicitation/create request from the server.
type ElicitResult struct {
// This property is reserved by the protocol to allow clients and servers to

View file

@ -1639,15 +1639,7 @@ func (ss *ServerSession) Elicit(ctx context.Context, params *ElicitParams) (*Eli
return nil, fmt.Errorf("%w: params cannot be nil", jsonrpc2.ErrInvalidParams)
}
if params.Mode == "" {
params2 := *params
if params.URL != "" || params.ElicitationID != "" {
params2.Mode = "url"
} else {
params2.Mode = "form"
}
params = &params2
}
params = params.inferElicitMode()
if iparams := ss.InitializeParams(); iparams == nil || iparams.Capabilities == nil || iparams.Capabilities.Elicitation == nil {
return nil, fmt.Errorf("client does not support elicitation")

View file

@ -52,7 +52,7 @@ confirmThenGreet
"who": {
"method": "elicitation/create",
"params": {
"mode": "",
"mode": "form",
"message": "What is your name?",
"requestedSchema": {
"type": "object",