fix: reject duplicate initialize requests (#962)
## Summary - reject a second `initialize` request on an already initialized server session - keep the original `ServerSession.InitializeParams()` instead of replacing it with later client parameters - add a raw JSON-RPC regression test for the duplicate-initialize path Fixes #961. ## To verify - `go test ./mcp -run TestServerRejectsDuplicateInitialize -count=1` - `go test ./mcp -count=1` - `go test ./...` --------- Co-authored-by: guglielmoc <guglielmoc@google.com>
This commit is contained in:
parent
bf69179066
commit
bed83e104b
1 changed files with 95 additions and 1 deletions
|
|
@ -1488,9 +1488,17 @@ func (ss *ServerSession) initialize(ctx context.Context, params *InitializeParam
|
|||
if params == nil {
|
||||
return nil, fmt.Errorf("%w: \"params\" must be be provided", jsonrpc2.ErrInvalidParams)
|
||||
}
|
||||
var wasInit bool
|
||||
ss.updateState(func(state *ServerSessionState) {
|
||||
state.InitializeParams = params
|
||||
wasInit = state.InitializeParams != nil
|
||||
if !wasInit {
|
||||
state.InitializeParams = params
|
||||
}
|
||||
})
|
||||
if wasInit {
|
||||
ss.server.opts.Logger.Error("duplicate initialize request")
|
||||
return nil, fmt.Errorf("duplicate %q received", methodInitialize)
|
||||
}
|
||||
|
||||
s := ss.server
|
||||
return &InitializeResult{
|
||||
|
|
|
|||
Loading…
Reference in a new issue