@marlinjai/email-mcp 1.2.6 → 1.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1622,34 +1622,28 @@ var ImapAdapter = class {
1622
1622
  throw formatImapError(error, `Failed to open folder "${folder}"`);
1623
1623
  }
1624
1624
  try {
1625
- let realMessageCount = -1;
1625
+ const mailboxExists = this.client.mailbox?.exists ?? 0;
1626
+ let statusCount = 0;
1626
1627
  try {
1627
1628
  const status = await this.client.status(folder, { messages: true });
1628
- realMessageCount = status.messages ?? -1;
1629
+ statusCount = status.messages ?? 0;
1629
1630
  } catch {
1630
1631
  }
1631
- const mailboxExists = this.client.mailbox?.exists ?? -1;
1632
- const effectiveCount = Math.max(realMessageCount, mailboxExists);
1633
- if (effectiveCount <= 0) {
1632
+ const effectiveCount = Math.max(mailboxExists, statusCount);
1633
+ if (effectiveCount === 0) {
1634
1634
  return [];
1635
1635
  }
1636
1636
  const criteria = this.buildSearchCriteria(query);
1637
1637
  const hasCriteria = Object.keys(criteria).length > 0;
1638
- let allUids;
1638
+ let allUids = [];
1639
1639
  try {
1640
1640
  const searchResult = await this.client.search(
1641
1641
  hasCriteria ? criteria : { all: true },
1642
1642
  { uid: true }
1643
1643
  );
1644
1644
  allUids = Array.isArray(searchResult) ? searchResult : [];
1645
- } catch (searchError) {
1646
- const errorMsg = String(searchError?.responseText || searchError?.message || "").toLowerCase();
1647
- const isInvalidMessage = errorMsg.includes("invalid message");
1648
- if (isInvalidMessage || !hasCriteria) {
1649
- allUids = await this.collectUidsViaFetch(query);
1650
- } else {
1651
- throw searchError;
1652
- }
1645
+ } catch {
1646
+ allUids = await this.collectUidsViaFetch(query);
1653
1647
  }
1654
1648
  const offset = query.offset || 0;
1655
1649
  const slicedUids = query.limit ? allUids.slice(offset, offset + query.limit) : allUids.slice(offset);
@@ -1671,39 +1665,39 @@ var ImapAdapter = class {
1671
1665
  async collectUidsViaFetch(query) {
1672
1666
  if (!this.client) return [];
1673
1667
  const uids = [];
1674
- try {
1675
- for await (const msg of this.client.fetch("1:*", { uid: true, flags: true })) {
1676
- if (query.unreadOnly && msg.flags?.has("\\Seen")) continue;
1677
- if (query.starredOnly && !msg.flags?.has("\\Flagged")) continue;
1678
- uids.push(msg.uid);
1679
- }
1680
- return uids;
1681
- } catch {
1682
- const exists = this.client.mailbox?.exists ?? 0;
1683
- if (exists > 0) {
1684
- try {
1685
- for await (const msg of this.client.fetch(`1:${exists}`, { uid: true, flags: true })) {
1686
- if (query.unreadOnly && msg.flags?.has("\\Seen")) continue;
1687
- if (query.starredOnly && !msg.flags?.has("\\Flagged")) continue;
1688
- uids.push(msg.uid);
1689
- }
1690
- return uids;
1691
- } catch {
1668
+ const exists = this.client.mailbox?.exists ?? 0;
1669
+ const safeFetch = async (range) => {
1670
+ try {
1671
+ const messages = await this.client.fetchAll(range, { uid: true, flags: true });
1672
+ for (const msg of messages) {
1673
+ if (query.unreadOnly && msg.flags?.has("\\Seen")) continue;
1674
+ if (query.starredOnly && !msg.flags?.has("\\Flagged")) continue;
1675
+ uids.push(msg.uid);
1692
1676
  }
1677
+ return uids.length > 0;
1678
+ } catch {
1679
+ return false;
1693
1680
  }
1694
- const count = exists > 0 ? exists : 50;
1695
- for (let seq = 1; seq <= count; seq++) {
1696
- try {
1697
- for await (const msg of this.client.fetch(String(seq), { uid: true, flags: true })) {
1698
- if (query.unreadOnly && msg.flags?.has("\\Seen")) continue;
1699
- if (query.starredOnly && !msg.flags?.has("\\Flagged")) continue;
1700
- uids.push(msg.uid);
1701
- }
1702
- } catch {
1681
+ };
1682
+ if (await safeFetch("1:*")) return uids;
1683
+ uids.length = 0;
1684
+ if (exists > 0) {
1685
+ if (await safeFetch(`1:${exists}`)) return uids;
1686
+ uids.length = 0;
1687
+ }
1688
+ const count = exists > 0 ? exists : 50;
1689
+ for (let seq = 1; seq <= count; seq++) {
1690
+ try {
1691
+ const msgs = await this.client.fetchAll(String(seq), { uid: true, flags: true });
1692
+ for (const msg of msgs) {
1693
+ if (query.unreadOnly && msg.flags?.has("\\Seen")) continue;
1694
+ if (query.starredOnly && !msg.flags?.has("\\Flagged")) continue;
1695
+ uids.push(msg.uid);
1703
1696
  }
1697
+ } catch {
1704
1698
  }
1705
- return uids;
1706
1699
  }
1700
+ return uids;
1707
1701
  }
1708
1702
  /**
1709
1703
  * Fetches email data for a set of UIDs, either with full body or lightweight headers.