MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / readFileResource

Function readFileResource

mcp/resource.go:53–69  ·  view source on GitHub ↗

readFileResource reads from the filesystem at a URI relative to dirFilepath, respecting the roots. dirFilepath and rootFilepaths are absolute filesystem paths.

(rawURI, dirFilepath string, rootFilepaths []string)

Source from the content-addressed store, hash-verified

51// the roots.
52// dirFilepath and rootFilepaths are absolute filesystem paths.
53func readFileResource(rawURI, dirFilepath string, rootFilepaths []string) ([]byte, error) {
54 uriFilepath, err := computeURIFilepath(rawURI, dirFilepath, rootFilepaths)
55 if err != nil {
56 return nil, err
57 }
58
59 var data []byte
60 err = withFile(dirFilepath, uriFilepath, func(f *os.File) error {
61 var err error
62 data, err = io.ReadAll(f)
63 return err
64 })
65 if os.IsNotExist(err) {
66 err = ResourceNotFoundError(rawURI)
67 }
68 return data, err
69}
70
71// computeURIFilepath returns a path relative to dirFilepath.
72// The dirFilepath and rootFilepaths are absolute file paths.

Callers 2

fileResourceHandlerFunction · 0.85
TestReadFileResourceFunction · 0.85

Calls 3

computeURIFilepathFunction · 0.85
withFileFunction · 0.85
ResourceNotFoundErrorFunction · 0.85

Tested by 1

TestReadFileResourceFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…