-
Notifications
You must be signed in to change notification settings - Fork 302
mcp: don't treat pointers differently given an OutputSchema override #692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -359,21 +359,14 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan | |
| // Pointers are treated equivalently to non-pointers when deriving the schema. | ||
| // If an indirection occurred to derive the schema, a non-nil zero value is | ||
| // returned to be used in place of the typed nil zero value. | ||
| // | ||
| // Note that if sfield already holds a schema, zero will be nil even if T is a | ||
| // pointer: if the user provided the schema, they may have intentionally | ||
| // derived it from the pointer type, and handling of zero values is up to them. | ||
| // | ||
| // TODO(rfindley): we really shouldn't ever return 'null' results. Maybe we | ||
| // should have a jsonschema.Zero(schema) helper? | ||
| func setSchema[T any](sfield *any, rfield **jsonschema.Resolved) (zero any, err error) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add some more documentation around I am also not familiar with the I could be missing some context around how this function is used, or conventions around these names. |
||
| rt := reflect.TypeFor[T]() | ||
| if rt.Kind() == reflect.Pointer { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm on the fence, should we put a comment here explaining the importance of setting |
||
| rt = rt.Elem() | ||
| zero = reflect.Zero(rt).Interface() | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Somewhat related to my previous comment, should we check if our handling of zero/nil for pointer types is "interfering" with the user provided schema? It sounds like actually the user should never be including the ability to return typed nils in their schema, so maybe this is something we can check at validation time? its also OK if the answer is "no" and this is too much added complexity, I just wanted to double check if there's any corner cases we could easily identify and flag for the user to make their UX better |
||
| var internalSchema *jsonschema.Schema | ||
| if *sfield == nil { | ||
| rt := reflect.TypeFor[T]() | ||
| if rt.Kind() == reflect.Pointer { | ||
| rt = rt.Elem() | ||
| zero = reflect.Zero(rt).Interface() | ||
| } | ||
| // TODO: we should be able to pass nil opts here. | ||
| internalSchema, err = jsonschema.ForType(rt, &jsonschema.ForOptions{}) | ||
| if err == nil { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t1is a confusing name here since it is index1or2.tnmight be slightly better.maybe it would be even clearer if we iterated over adjacent pairs in this array, then we could use names like
iandjand avoid giving explicit indices.