Skip to content

Commit 4b985de

Browse files
committed
refactor: update protocol version negotiation to select the highest matching version from descending order list
1 parent 3747e9f commit 4b985de

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

mcp/client.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,18 +358,22 @@ func (c *Client) discover(ctx context.Context, cs *ClientSession) (*InitializeRe
358358
return nil, false, err
359359
}
360360

361-
// Pick the highest protocol version that both the server and this SDK
362-
// support. If there is no overlap, fall back to initialize so version
363-
// negotiation can happen via the legacy path.
364-
negotiated := ""
365-
for _, v := range res.SupportedVersions {
366-
if slices.Contains(supportedProtocolVersions, v) && v > negotiated {
361+
// Pick the highest protocol version that both the server and this SDK support.
362+
// Since supportedProtocolVersions is defined in descending order (newest to oldest),
363+
// the first match we find is the highest supported version.
364+
var negotiated string
365+
for _, v := range supportedProtocolVersions {
366+
if slices.Contains(res.SupportedVersions, v) {
367367
negotiated = v
368+
break
368369
}
369370
}
370371
if negotiated == "" {
372+
// If there is no overlap, fall back to initialize so version
373+
// negotiation can happen via the legacy path.
371374
return nil, true, nil
372375
}
376+
373377
return &InitializeResult{
374378
Capabilities: res.Capabilities,
375379
Instructions: res.Instructions,

0 commit comments

Comments
 (0)