Skip to content

Commit 40f0343

Browse files
mcp: infer elicit mode from ElicitParams (#1078)
Fixes #1072
1 parent ce393ac commit 40f0343

3 files changed

Lines changed: 20 additions & 10 deletions

File tree

mcp/protocol.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func (m InputRequestMap) MarshalJSON() ([]byte, error) {
6363
if err != nil {
6464
return nil, err
6565
}
66+
if ep, ok := v.(*ElicitParams); ok {
67+
v = ep.inferElicitMode()
68+
}
6669
converted[k] = &wire{Method: method, Params: v}
6770
}
6871
return json.Marshal(converted)
@@ -2126,6 +2129,21 @@ func (x *ElicitParams) isNil() bool { return x == nil }
21262129
func (x *ElicitParams) GetProgressToken() any { return getProgressToken(x) }
21272130
func (x *ElicitParams) SetProgressToken(t any) { setProgressToken(x, t) }
21282131

2132+
// inferElicitMode returns x with Mode populated by inference if it was empty.
2133+
// Mode is inferred as "url" when URL or ElicitationID is set, otherwise "form".
2134+
func (x *ElicitParams) inferElicitMode() *ElicitParams {
2135+
if x == nil || x.Mode != "" {
2136+
return x
2137+
}
2138+
x2 := *x
2139+
if x.URL != "" || x.ElicitationID != "" {
2140+
x2.Mode = "url"
2141+
} else {
2142+
x2.Mode = "form"
2143+
}
2144+
return &x2
2145+
}
2146+
21292147
// The client's response to an elicitation/create request from the server.
21302148
type ElicitResult struct {
21312149
// This property is reserved by the protocol to allow clients and servers to

mcp/server.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,15 +1639,7 @@ func (ss *ServerSession) Elicit(ctx context.Context, params *ElicitParams) (*Eli
16391639
return nil, fmt.Errorf("%w: params cannot be nil", jsonrpc2.ErrInvalidParams)
16401640
}
16411641

1642-
if params.Mode == "" {
1643-
params2 := *params
1644-
if params.URL != "" || params.ElicitationID != "" {
1645-
params2.Mode = "url"
1646-
} else {
1647-
params2.Mode = "form"
1648-
}
1649-
params = &params2
1650-
}
1642+
params = params.inferElicitMode()
16511643

16521644
if iparams := ss.InitializeParams(); iparams == nil || iparams.Capabilities == nil || iparams.Capabilities.Elicitation == nil {
16531645
return nil, fmt.Errorf("client does not support elicitation")

mcp/testdata/conformance/server/mrtr.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ confirmThenGreet
5252
"who": {
5353
"method": "elicitation/create",
5454
"params": {
55-
"mode": "",
55+
"mode": "form",
5656
"message": "What is your name?",
5757
"requestedSchema": {
5858
"type": "object",

0 commit comments

Comments
 (0)