mcp: allow restricting the protocol versions a server supports (#1199)
Fixes #945
This commit is contained in:
parent
5a89b63764
commit
21c18c6229
1 changed files with 397 additions and 35 deletions
|
|
@ -63,18 +63,38 @@ var supportedProtocolVersions = []string{
|
|||
protocolVersion20241105,
|
||||
}
|
||||
|
||||
// SupportedProtocolVersions returns the protocol versions supported by this
|
||||
// version of the SDK, newest first.
|
||||
//
|
||||
// Use it to discover the values accepted by
|
||||
// [ServerOptions.SupportedProtocolVersions]. The returned slice is a copy:
|
||||
// modifying it does not change what the SDK supports.
|
||||
func SupportedProtocolVersions() []string { return slices.Clone(supportedProtocolVersions) }
|
||||
|
||||
// negotiatedVersion returns the effective protocol version to use, given a
|
||||
// client version.
|
||||
func negotiatedVersion(clientVersion string) string {
|
||||
// In general, prefer to use the clientVersion, but if we don't support the
|
||||
// client's version, use the latest version.
|
||||
// client version and the versions the server supports, newest first.
|
||||
func negotiatedVersion(clientVersion string, supported []string) string {
|
||||
// In general, prefer to use the clientVersion, but if not supported
|
||||
// by the server, use the latest version the server supports.
|
||||
//
|
||||
// Cap the supported versions at the legacy protocolVersion20251125, as this
|
||||
// method is used by the initialize method which is deprecated in
|
||||
// version protocolVersion20260728.
|
||||
if slices.Contains(supportedProtocolVersions, clientVersion) && clientVersion < protocolVersion20260728 {
|
||||
if slices.Contains(supported, clientVersion) && clientVersion < protocolVersion20260728 {
|
||||
return clientVersion
|
||||
}
|
||||
for _, v := range supported {
|
||||
if v < protocolVersion20260728 {
|
||||
return v
|
||||
}
|
||||
}
|
||||
// If the server was enforced to support only protocolVersion20260728 and
|
||||
// later, the initialize function should not be called.
|
||||
// If a client calls the initialize function, return the protocolVersion20251125.
|
||||
// As according to spec (https://modelcontextprotocol.io/specification/2025-11-25/basic/lifecycle#version-negotiation):
|
||||
// "If the server supports the requested protocol version, it MUST respond with the same version."
|
||||
// "Otherwise, the server MUST respond with another protocol version it supports."
|
||||
// "This SHOULD be the latest version supported by the server."
|
||||
return protocolVersion20251125
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue