mcp: negotiate protocol version (#112)

Negotiate the protocol version properly, and set the header on HTTP
transports.

We follow the logic of the Typescript SDK.

- On initialization, the client sends the latest version it can support,
and accepts the version that the server returns if the client supports
it. If not, the connection fails.

- The server accepts the client's version unless it doesn't support it,
in which case it replies with its latest version.

Fixes #103.
This commit is contained in:
Jonathan Amsterdam 2025-07-10 11:06:55 -04:00 • committed by GitHub
commit c037ba51d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -647,10 +647,11 @@ func (ss *ServerSession) initialize(ctx context.Context, params *InitializeParam
ss.mu.Unlock()
}()
version := "2025-03-26" // preferred version
switch v := params.ProtocolVersion; v {
case "2024-11-05", "2025-03-26":
version = v
// If we support the client's version, reply with it. Otherwise, reply with our
// latest version.
version := params.ProtocolVersion
if !slices.Contains(supportedProtocolVersions, params.ProtocolVersion) {
version = latestProtocolVersion
}
return &InitializeResult{