MCPcopy Index your code
hub / github.com/modelcontextprotocol/go-sdk / withFile

Function withFile

mcp/resource.go:118–131  ·  view source on GitHub ↗

withFile calls f on the file at join(dir, rel), protecting against path traversal attacks.

(dir, rel string, f func(*os.File) error)

Source from the content-addressed store, hash-verified

116// withFile calls f on the file at join(dir, rel),
117// protecting against path traversal attacks.
118func withFile(dir, rel string, f func(*os.File) error) (err error) {
119 r, err := os.OpenRoot(dir)
120 if err != nil {
121 return err
122 }
123 defer r.Close()
124 file, err := r.Open(rel)
125 if err != nil {
126 return err
127 }
128 // Record error, in case f writes.
129 defer func() { err = errors.Join(err, file.Close()) }()
130 return f(file)
131}
132
133// fileRoots transforms the Roots obtained from the client into absolute paths on
134// the local filesystem.

Callers 1

readFileResourceFunction · 0.85

Calls 2

CloseMethod · 0.65
OpenMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…