github/github-mcp-server · info

failed to base64 encode content: %w

Error message

failed to base64 encode content: %w

What it means

Returned when base64Encoder.Write(content) fails while encoding a non-text (blob) file for a repo:// resource. The encoder writes into a bytes.Buffer, whose Write method can never return an error (it grows on demand), so this branch is effectively unreachable defensive code that exists only because the encoder API returns an error. If you ever see it, treat it as a bug report, not an environmental problem.

Source

Thrown at pkg/github/repository_resource.go:232

			}

			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 {
					return nil, fmt.Errorf("failed to base64 encode content: %w", err)
				}
				if err := base64Encoder.Close(); err != nil {
					return nil, fmt.Errorf("failed to close base64 encoder: %w", err)
				}

				return &mcp.ReadResourceResult{
					Contents: []*mcp.ResourceContents{
						{
							URI:      request.Params.URI,
							MIMEType: mimeType,
							Blob:     buf.Bytes(),
						},
					},
				}, nil
			}
		case resp.StatusCode != http.StatusNotFound:
			// If we got a response but it is not 200 OK, we return an error
			body, err := io.ReadAll(resp.Body)

View on GitHub (pinned to 0ea1f775a7)

Solutions

  1. No runtime action needed - the path is unreachable with a bytes.Buffer sink
  2. Simplify the handler by replacing the streaming encoder with base64.StdEncoding.EncodeToString(content), which removes both this and the Close error branch

Example fix

// before
var buf bytes.Buffer
enc := base64.NewEncoder(base64.StdEncoding, &buf)
if _, err := enc.Write(content); err != nil {
    return nil, fmt.Errorf("failed to base64 encode content: %w", err)
}
// after
blob := base64.StdEncoding.EncodeToString(content)
Defensive patterns

Strategy: try-catch

Try / catch

if err != nil && strings.Contains(err.Error(), "failed to base64 encode content") {
    // Unreachable with a bytes.Buffer sink: treat as a bug, not an environment problem
    log.Error("unexpected base64 encode failure", "err", err)
}

Prevention

When it happens

Trigger: Only reachable when the MIME type of a fetched file is neither text/* nor application/*, so the blob path runs - and even then bytes.Buffer.Write cannot fail in practice. No realistic API call produces this error.

Common situations: Not observed in practice; developers encounter it while reading the code and wondering about the error path.

Related errors


AI-assisted analysis of github/github-mcp-server@0ea1f775a7 (2026-08-15). Data as JSON: /api/errors/e4218d647e478dad. Report an issue: GitHub.