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:
Jonathan Amsterdam 2025-06-09 07:11:18 -04:00
commit d9a7859c3b
2 changed files with 17 additions and 20 deletions

View file

@ -16,6 +16,7 @@ import (
"strings"
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/internal/util"
)
// 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.
func fileRoot(root *Root) (_ string, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("root %q: %w", root.URI, err)
}
}()
defer util.Wrapf(&err, "root %q", root.URI)
// Convert to absolute file path.
rurl, err := url.Parse(root.URI)

View file

@ -18,6 +18,7 @@ import (
"sync"
"github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
"github.com/modelcontextprotocol/go-sdk/internal/util"
)
const DefaultPageSize = 1000
@ -296,11 +297,7 @@ func fileResourceHandler(dir string) ResourceHandler {
panic(err)
}
return func(ctx context.Context, ss *ServerSession, params *ReadResourceParams) (_ *ReadResourceResult, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("reading resource %s: %w", params.URI, err)
}
}()
defer util.Wrapf(&err, "reading resource %s", params.URI)
// TODO: use a memoizing API here.
rootRes, err := ss.ListRoots(ctx, nil)