github/github-mcp-server · info

failed to close base64 encoder: %w

Error message

failed to close base64 encoder: %w

What it means

Returned when base64Encoder.Close() fails after encoding binary content for a repo:// resource. Close flushes the final base64 block into the same bytes.Buffer; since bytes.Buffer writes never fail, this error is effectively unreachable defensive code. Seeing it would indicate memory exhaustion or a corrupted runtime rather than any GitHub condition.

Source

Thrown at pkg/github/repository_resource.go:235

			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)
			if err != nil {
				return nil, fmt.Errorf("failed to read response body: %w", err)
			}

View on GitHub (pinned to 0ea1f775a7)

Solutions

  1. No runtime action needed - the path is unreachable with a bytes.Buffer sink
  2. Replace the encoder with base64.StdEncoding.EncodeToString(content) to delete both error branches and make the invariant explicit

Example fix

// before
if err := base64Encoder.Close(); err != nil {
    return nil, fmt.Errorf("failed to close base64 encoder: %w", err)
}
// after - no encoder, no close
blob := base64.StdEncoding.EncodeToString(content)
Defensive patterns

Strategy: try-catch

Try / catch

if err != nil && strings.Contains(err.Error(), "failed to close base64 encoder") {
    // Unreachable with a bytes.Buffer sink: report upstream
    log.Error("unexpected base64 close failure", "err", err)
}

Prevention

When it happens

Trigger: Same blob path as the Write error (non-text MIME type); Close on an encoder over a bytes.Buffer has no failure mode in practice. No realistic API call produces this error.

Common situations: Not observed in practice; encountered only during code review of the encoding branch.

Related errors


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