googleapis/mcp-toolbox · error

failed to retrieve updated document: %w

Error message

failed to retrieve updated document: %w

What it means

Thrown by Source.AddDocuments in the returnData branch: after successfully adding the document, a follow-up docRef.Get(ctx) to read back the freshly written document failed. This is a read failure on an already-created document, distinct from the creation failure in error 792 — the document usually exists but could not be read back.

Source

Thrown at internal/sources/firestore/firestore.go:334

	// Get the collection reference
	collection := s.FirestoreClient().Collection(collectionPath)

	// Add the document to the collection
	docRef, writeResult, err := collection.Add(ctx, documentData)
	if err != nil {
		return nil, fmt.Errorf("failed to add document: %w", err)
	}
	// Build the response
	response := map[string]any{
		"documentPath": docRef.Path,
		"createTime":   writeResult.UpdateTime.Format("2006-01-02T15:04:05.999999999Z"),
	}
	// Add document data if requested
	if returnData {
		// Fetch the updated document to return the current state
		snapshot, err := docRef.Get(ctx)
		if err != nil {
			return nil, fmt.Errorf("failed to retrieve updated document: %w", err)
		}
		// Convert the document data back to simple JSON format
		simplifiedData := FirestoreValueToJSON(snapshot.Data())
		response["documentData"] = simplifiedData
	}
	return response, nil
}

func (s *Source) UpdateDocument(ctx context.Context, documentPath string, updates []firestore.Update, documentData any, returnData bool) (map[string]any, error) {
	// Get the document reference
	docRef := s.FirestoreClient().Doc(documentPath)

	// Prepare update data
	var writeResult *firestore.WriteResult
	var writeErr error

	if len(updates) > 0 {
		writeResult, writeErr = docRef.Update(ctx, updates)

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Verify the service account can also read the collection (roles/datastore.user; check for restrictive IAM conditions).
  2. Check for TTL policies or concurrent writers deleting the collection's documents right after creation.
  3. Retry the operation — the document was created, so a re-read may succeed.
  4. Set a longer context deadline to avoid DEADLINE_EXCEEDED on the read-back.
  5. If 'not found' persists, investigate concurrent deletes on that collection.

Example fix

// before
snapshot, err := docRef.Get(ctx)
if err != nil {
    return nil, fmt.Errorf("failed to retrieve updated document: %w", err)
}
// after (caller-side)
response, err := source.AddDocuments(ctx, "docs", data)
if err != nil {
    log.Printf("document may still exist; add succeeded: %v", err)
    return fallbackWithKnownPath(response, err)
}
Defensive patterns

Strategy: retry

Validate before calling

// Go
// Pre-check read access and set an adequate deadline before the read-back
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
if _, err := source.GetDocuments(ctx, []string{knownDocPath}); err != nil {
    return fmt.Errorf("no read permission on collection; returnData will fail: %w", err)
}

Type guard

func isNotFoundErr(err error) bool {
    return err != nil && strings.Contains(err.Error(), "not found")
}

Try / catch

resp, err := source.AddDocuments(ctx, coll, data)
if err != nil {
    // The document was likely created; degrade gracefully instead of failing hard
    if strings.Contains(err.Error(), "failed to retrieve updated document") {
        log.Printf("document created but read-back failed: %v", err)
        return nil
    }
    return err
}

Prevention

When it happens

Trigger: Calling AddDocuments with returnData=true when the caller's IAM grants create but not read, the document was deleted between Add and Get (TTL policy or concurrent delete), or a transient gRPC error during the read-back.

Common situations: Fine-grained IAM where the service account only has write scopes, Firestore TTL policies deleting documents shortly after creation, replication delays in multi-region setups, transient DEADLINE_EXCEEDED on the read-back Get.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/bf02479a8a4a0152. Report an issue: GitHub.