internal/mcp/internal/util: add Wrapf
Add Wrapf, a function that helps with error wrapping. Change-Id: I102695f75e4d217e09f828f4723cbfcbbe83f887 Reviewed-on: https://go-review.googlesource.com/c/tools/+/679177 Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Sam Thanawalla <samthanawalla@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
30af200d5e
commit
d9a7859c3b
6 changed files with 17 additions and 20 deletions
|
|
@ -6,6 +6,7 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"cmp"
|
"cmp"
|
||||||
|
"fmt"
|
||||||
"iter"
|
"iter"
|
||||||
"reflect"
|
"reflect"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
@ -73,3 +74,10 @@ func FieldJSONInfo(f reflect.StructField) JSONInfo {
|
||||||
}
|
}
|
||||||
return info
|
return info
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrapf wraps *errp with the given formatted message if *errp is not nil.
|
||||||
|
func Wrapf(errp *error, format string, args ...any) {
|
||||||
|
if *errp != nil {
|
||||||
|
*errp = fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), *errp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,8 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/modelcontextprotocol/go-sdk/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -69,7 +71,7 @@ func parseJSONPointer(ptr string) (segments []string, err error) {
|
||||||
// This implementation suffices for JSON Schema: pointers are applied only to Schemas,
|
// This implementation suffices for JSON Schema: pointers are applied only to Schemas,
|
||||||
// and refer only to Schemas.
|
// and refer only to Schemas.
|
||||||
func dereferenceJSONPointer(s *Schema, sptr string) (_ *Schema, err error) {
|
func dereferenceJSONPointer(s *Schema, sptr string) (_ *Schema, err error) {
|
||||||
defer wrapf(&err, "JSON Pointer %q", sptr)
|
defer util.Wrapf(&err, "JSON Pointer %q", sptr)
|
||||||
|
|
||||||
segments, err := parseJSONPointer(sptr)
|
segments, err := parseJSONPointer(sptr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -282,10 +282,3 @@ func assert(cond bool, msg string) {
|
||||||
panic("assertion failed: " + msg)
|
panic("assertion failed: " + msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// wrapf wraps *errp with the given formatted message if *errp is not nil.
|
|
||||||
func wrapf(errp *error, format string, args ...any) {
|
|
||||||
if *errp != nil {
|
|
||||||
*errp = fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), *errp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ type state struct {
|
||||||
|
|
||||||
// validate validates the reflected value of the instance.
|
// validate validates the reflected value of the instance.
|
||||||
func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *annotations) (err error) {
|
func (st *state) validate(instance reflect.Value, schema *Schema, callerAnns *annotations) (err error) {
|
||||||
defer wrapf(&err, "validating %s", schema)
|
defer util.Wrapf(&err, "validating %s", schema)
|
||||||
|
|
||||||
// Maintain a stack for dynamic schema resolution.
|
// Maintain a stack for dynamic schema resolution.
|
||||||
st.stack = append(st.stack, schema) // push
|
st.stack = append(st.stack, schema) // push
|
||||||
|
|
@ -607,7 +607,7 @@ func (rs *Resolved) ApplyDefaults(instancep any) error {
|
||||||
// Leave this as a potentially recursive helper function, because we'll surely want
|
// Leave this as a potentially recursive helper function, because we'll surely want
|
||||||
// to apply defaults on sub-schemas someday.
|
// to apply defaults on sub-schemas someday.
|
||||||
func (st *state) applyDefaults(instancep reflect.Value, schema *Schema) (err error) {
|
func (st *state) applyDefaults(instancep reflect.Value, schema *Schema) (err error) {
|
||||||
defer wrapf(&err, "applyDefaults: schema %s, instance %v", schema, instancep)
|
defer util.Wrapf(&err, "applyDefaults: schema %s, instance %v", schema, instancep)
|
||||||
|
|
||||||
instance := instancep.Elem()
|
instance := instancep.Elem()
|
||||||
if instance.Kind() == reflect.Map || instance.Kind() == reflect.Struct {
|
if instance.Kind() == reflect.Map || instance.Kind() == reflect.Struct {
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
|
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
|
||||||
|
"github.com/modelcontextprotocol/go-sdk/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A ServerResource associates a Resource with its handler.
|
// A ServerResource associates a Resource with its handler.
|
||||||
|
|
@ -122,11 +123,7 @@ func fileRoots(rawRoots []*Root) ([]string, error) {
|
||||||
|
|
||||||
// fileRoot returns the absolute path for Root.
|
// fileRoot returns the absolute path for Root.
|
||||||
func fileRoot(root *Root) (_ string, err error) {
|
func fileRoot(root *Root) (_ string, err error) {
|
||||||
defer func() {
|
defer util.Wrapf(&err, "root %q", root.URI)
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("root %q: %w", root.URI, err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Convert to absolute file path.
|
// Convert to absolute file path.
|
||||||
rurl, err := url.Parse(root.URI)
|
rurl, err := url.Parse(root.URI)
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
|
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
|
||||||
|
"github.com/modelcontextprotocol/go-sdk/internal/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DefaultPageSize = 1000
|
const DefaultPageSize = 1000
|
||||||
|
|
@ -296,11 +297,7 @@ func fileResourceHandler(dir string) ResourceHandler {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return func(ctx context.Context, ss *ServerSession, params *ReadResourceParams) (_ *ReadResourceResult, err error) {
|
return func(ctx context.Context, ss *ServerSession, params *ReadResourceParams) (_ *ReadResourceResult, err error) {
|
||||||
defer func() {
|
defer util.Wrapf(&err, "reading resource %s", params.URI)
|
||||||
if err != nil {
|
|
||||||
err = fmt.Errorf("reading resource %s: %w", params.URI, err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// TODO: use a memoizing API here.
|
// TODO: use a memoizing API here.
|
||||||
rootRes, err := ss.ListRoots(ctx, nil)
|
rootRes, err := ss.ListRoots(ctx, nil)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue