mcp: change CompleteParams.Context from pointer to value type#746
mcp: change CompleteParams.Context from pointer to value type#746SamMorrowDrums wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
CompleteContext contains only a map field which is safe at its zero value. A pointer type forces unnecessary nil checks and has caused panics in production when clients omit the optional context field.
15e5fba to
3e8e800
Compare
Sorry about the extra null check. |
|
@jba I'd have been inclined to agree except that omitempty handles empty json object Perhaps we could remove the omitempty and leave it as a pointer? |
Do you mean:
I'm not sure I see the problem. Don't all our struct pointers have the same issue? |
|
@jba I'm very much ok to drop this, but tried to clarify my thinking below for posterity. I was only thinking it's a little bit strange that the SDK internal representation would be temperamental to varying client implementations of parts the spec itself, so when I develop with one client I might get different structs than another. User input is different of course. One could argue that's a schema validation issue and if the schema proscribed one or another way, then it could be handled as a validation error to be fair. I think I'd be more comfortable with json being marshalled so that an empty object is nil than occasionally being an empty struct depending on input. We take full responsibility for not handling a potentially nil struct properly, that part I'm not attributing to the SDK at all. My reason to keep responding was only a philosophical one. I want the SDK spec fields to be deterministic, so regardless of client I get consistent go structs from the SDK. But, again it's a pointer so always handle nil is fair answer. |
mcp: change CompleteParams.Context from pointer to value type
The
CompleteContextstruct contains only a single field (Arguments map[string]string)which already handles the nil/empty case gracefully. Making
Contexta pointer typeforces all consumers to perform nil checks before accessing
Arguments, which iserror-prone and has caused nil pointer panics in production (see github/github-mcp-server#1754).
With a value type, consumers can safely access
req.Params.Context.Argumentsdirectly:Argumentswill benil(safe to range over, check length, etc.)Argumentswill be an empty mapThis follows the Go idiom of preferring value types for simple structs where
the zero value is useful, similar to how
time.Timeis typically used as a value.Before (requires nil check):
After (safe direct access):