@@ -167,32 +167,70 @@ func filterDisabledTools(cfg *config.ConfigStore, mcpName string, tools []*Tool)
167167 return filtered
168168}
169169
170−// ensureBase64 checks if data is valid base64 and returns it as-is if so,
171−// otherwise encodes the raw binary data to base64.
170+// ensureBase64 normalizes valid base64 input and guarantees padded
171+// base64.StdEncoding output; otherwise it encodes raw binary data.
172172func ensureBase64(data []byte) []byte {
173− // Check if the data is already valid base64 by attempting to decode it.
174− // Valid base64 should only contain ASCII characters (A-Z, a-z, 0-9, +, /, =).
175− if isValidBase64(data) {
173+ if len(data) == 0 {
176174 return data
177175 }
178− // Data is raw binary, encode it to base64.
176+
177+ normalized := normalizeBase64Input(data)
178+ if decoded, ok := decodeBase64(normalized); ok {
179+ encoded := make([]byte, base64.StdEncoding.EncodedLen(len(decoded)))
180+ base64.StdEncoding.Encode(encoded, decoded)
181+ return encoded
182+ }
183+
179184 encoded := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
180185 base64.StdEncoding.Encode(encoded, data)
181186 return encoded
182187}
183188
189+func normalizeBase64Input(data []byte) []byte {
190+ normalized := strings.Join(strings.Fields(string(data)), "")
191+ return []byte(normalized)
192+}
193+
194+func decodeBase64(data []byte) ([]byte, bool) {
195+ if len(data) == 0 {
196+ return data, true
197+ }
198+
199+ for _, b := range data {
200+ if b > 127 {
201+ return nil, false
202+ }
203+ }
204+
205+ s := string(data)
206+ decoded, err := base64.StdEncoding.DecodeString(s)
207+ if err == nil {
208+ return decoded, true
209+ }
210+ decoded, err = base64.RawStdEncoding.DecodeString(s)
211+ if err == nil {
212+ return decoded, true
213+ }
214+ return nil, false
215+}
216+
184217// isValidBase64 checks if the data appears to be valid base64-encoded content.
185218func isValidBase64(data []byte) bool {
186219 if len(data) == 0 {
187220 return true
188221 }
222+
189223 // Base64 strings should only contain ASCII characters.
190224 for _, b := range data {
191225 if b > 127 {
192226 return false
193227 }
194228 }
195− // Try to decode to verify it's valid base64.
196− _, err := base64.StdEncoding.DecodeString(string(data))
229+
230+ s := string(data)
231+ if _, err := base64.StdEncoding.DecodeString(s); err == nil {
232+ return true
233+ }
234+ _, err := base64.RawStdEncoding.DecodeString(s)
197235 return err == nil
198236}