github/github-mcp-server · error
failed to read file content: %w
Error message
failed to read file content: %w
What it means
Thrown by the repo:// resource read handler (pkg/github/repository_resource.go) after GitHub's raw content endpoint already returned HTTP 200: io.ReadAll failed while streaming the body. The wrapped error is almost always a transport failure (connection reset, unexpected EOF, deadline exceeded) that hit mid-download, after status and headers were received. The file exists and is reachable; only the body transfer did not complete.
Source
Thrown at pkg/github/repository_resource.go:213
return nil, fmt.Errorf("failed to get raw content: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()
// If the raw content is not found, we will fall back to the GitHub API (in case it is a directory)
switch {
case resp.StatusCode == http.StatusOK:
ext := filepath.Ext(path)
mimeType := resp.Header.Get("Content-Type")
if ext == ".md" {
mimeType = "text/markdown"
} else if mimeType == "" {
mimeType = mime.TypeByExtension(ext)
}
content, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read file content: %w", err)
}
switch {
case strings.HasPrefix(mimeType, "text"), strings.HasPrefix(mimeType, "application"):
return &mcp.ReadResourceResult{
Contents: []*mcp.ResourceContents{
{
URI: request.Params.URI,
MIMEType: mimeType,
Text: string(content),
},
},
}, nil
default:
var buf bytes.Buffer
base64Encoder := base64.NewEncoder(base64.StdEncoding, &buf)
_, err := base64Encoder.Write(content)
if err != nil {View on GitHub (pinned to 0ea1f775a7)
Solutions
- Retry the read_resource call once - mid-body interruptions are usually transient and succeed on a second attempt
- Check the file size first (e.g. via get_file_contents metadata) and avoid streaming files above your reliability limit
- Raise the raw client's HTTP timeout or pass a context with a longer deadline when reading large files
- If it reproduces only on one network, inspect proxy/firewall treatment of raw.githubusercontent.com
Example fix
// before
content, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read file content: %w", err)
}
// after - bound the read and signal transiency to callers
const maxRawBytes = 64 << 20
content, err := io.ReadAll(io.LimitReader(resp.Body, maxRawBytes))
if err != nil {
return nil, fmt.Errorf("failed to read file content (transfer interrupted, likely transient): %w", err)
} Defensive patterns
Strategy: retry
Validate before calling
// Before read_resource on a repo:// URI, confirm the blob is small enough to stream reliably
meta, _, err := client.Repositories.GetContents(ctx, owner, repo, path, nil)
if err == nil && meta != nil && meta.GetSize() > 50<<20 {
return fmt.Errorf("file %s is %d bytes; fetch it with git instead of read_resource", path, meta.GetSize())
} Type guard
func isTransientBodyErr(err error) bool {
var netErr net.Error
if errors.As(err, &netErr) {
return true
}
return errors.Is(err, io.ErrUnexpectedEOF) ||
errors.Is(err, syscall.ECONNRESET) ||
strings.Contains(err.Error(), "http2: stream closed")
} Try / catch
var result *mcp.ReadResourceResult
err := retry(ctx, 3, 500*time.Millisecond, func() error {
var e error
result, e = readResource(ctx, repoURI)
if e != nil && isTransientBodyErr(e) {
return e // retryable
}
return nil
})
if err != nil {
return fmt.Errorf("raw file read failed after retries: %w", err)
} Prevention
- Avoid read_resource on large binaries; use git clone or the contents API with a raw media type instead
- Set generous timeouts on raw file reads proportional to file size
- Retry once on unexpected EOF or connection reset before escalating to the user
When it happens
Trigger: Reading a repo://raw/... resource URI when the connection drops between headers and the last body byte: rawClient.GetRawContent returned 200, then a proxy or raw.githubusercontent.com reset the stream, the client context deadline expired during a large file, or the TLS connection broke mid-transfer.
Common situations: Reading very large files (hundreds of MB) through read_resource; aggressive HTTP client timeouts on the raw client; corporate proxies that cut long transfers; flaky CI or mobile networks.
Related errors
- failed to read response body: %w
- failed to read response body: %w
- failed to fetch raw content: %s
- failed to read response body: %w
- failed to read response body: %w
AI-assisted analysis of github/github-mcp-server@0ea1f775a7 (2026-08-15).
Data as JSON: /api/errors/09e81f7ae6c489cd.
Report an issue: GitHub.