Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mcp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,10 @@ func (c *Client) Connect(ctx context.Context, t Transport, opts *ClientSessionOp
Capabilities: c.capabilities(protocolVersion),
}
req := &InitializeRequest{Session: cs, Params: params}
res, err := handleSend[*InitializeResult](ctx, methodInitialize, req)
// The header must agree with params.protocolVersion: no version has been
// negotiated yet, so nothing else identifies what this request proposes.
initializeCtx := context.WithValue(ctx, protocolVersionContextKey{}, protocolVersion)
res, err := handleSend[*InitializeResult](initializeCtx, methodInitialize, req)
if err != nil {
_ = cs.Close()
return nil, err
Expand Down
12 changes: 12 additions & 0 deletions mcp/streamable_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ func (s *fakeStreamableServer) ServeHTTP(w http.ResponseWriter, req *http.Reques
if v := req.Header.Get(protocolVersionHeader); v != resp.wantProtocolVersion && resp.wantProtocolVersion != "" {
s.t.Errorf("%v: bad protocol version header: got %q, want %q", key, v, resp.wantProtocolVersion)
}
// On initialize no version has been negotiated yet, so the header carries
// what the request proposes and must agree with the body. Checked for every
// initialize rather than per-response, since it holds unconditionally.
if key.jsonrpcMethod == methodInitialize && jsonrpcReq != nil {
var params InitializeParams
if err := json.Unmarshal(jsonrpcReq.Params, &params); err != nil {
s.t.Errorf("%v: unmarshal initialize params: %v", key, err)
} else if got := req.Header.Get(protocolVersionHeader); got != params.ProtocolVersion {
s.t.Errorf("%v: protocol version header %q disagrees with body protocolVersion %q",
key, got, params.ProtocolVersion)
}
}
w.Write([]byte(body))
rc.Flush() // flush response

Expand Down