@marlinjai/email-mcp 1.2.6 → 1.2.8

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.
package/dist/index.js CHANGED
@@ -8398,33 +8398,24 @@ var init_adapter3 = __esm({
8398
8398
  throw formatImapError(error48, `Failed to open folder "${folder}"`);
8399
8399
  }
8400
8400
  try {
8401
- let realMessageCount = -1;
8402
- try {
8403
- const status = await this.client.status(folder, { messages: true });
8404
- realMessageCount = status.messages ?? -1;
8405
- } catch {
8406
- }
8407
- const mailboxExists = this.client.mailbox?.exists ?? -1;
8408
- const effectiveCount = Math.max(realMessageCount, mailboxExists);
8409
- if (effectiveCount <= 0) {
8401
+ const mailboxExists = this.client.mailbox?.exists ?? 0;
8402
+ if (mailboxExists === 0) {
8410
8403
  return [];
8411
8404
  }
8412
8405
  const criteria = this.buildSearchCriteria(query);
8413
8406
  const hasCriteria = Object.keys(criteria).length > 0;
8414
- let allUids;
8407
+ let allUids = [];
8415
8408
  try {
8416
8409
  const searchResult = await this.client.search(
8417
8410
  hasCriteria ? criteria : { all: true },
8418
8411
  { uid: true }
8419
8412
  );
8420
8413
  allUids = Array.isArray(searchResult) ? searchResult : [];
8421
- } catch (searchError) {
8422
- const errorMsg = String(searchError?.responseText || searchError?.message || "").toLowerCase();
8423
- const isInvalidMessage = errorMsg.includes("invalid message");
8424
- if (isInvalidMessage || !hasCriteria) {
8414
+ } catch {
8415
+ try {
8425
8416
  allUids = await this.collectUidsViaFetch(query);
8426
- } else {
8427
- throw searchError;
8417
+ } catch {
8418
+ return [];
8428
8419
  }
8429
8420
  }
8430
8421
  const offset = query.offset || 0;
@@ -8447,39 +8438,39 @@ var init_adapter3 = __esm({
8447
8438
  async collectUidsViaFetch(query) {
8448
8439
  if (!this.client) return [];
8449
8440
  const uids = [];
8450
- try {
8451
- for await (const msg of this.client.fetch("1:*", { uid: true, flags: true })) {
8452
- if (query.unreadOnly && msg.flags?.has("\\Seen")) continue;
8453
- if (query.starredOnly && !msg.flags?.has("\\Flagged")) continue;
8454
- uids.push(msg.uid);
8455
- }
8456
- return uids;
8457
- } catch {
8458
- const exists = this.client.mailbox?.exists ?? 0;
8459
- if (exists > 0) {
8460
- try {
8461
- for await (const msg of this.client.fetch(`1:${exists}`, { uid: true, flags: true })) {
8462
- if (query.unreadOnly && msg.flags?.has("\\Seen")) continue;
8463
- if (query.starredOnly && !msg.flags?.has("\\Flagged")) continue;
8464
- uids.push(msg.uid);
8465
- }
8466
- return uids;
8467
- } catch {
8441
+ const exists = this.client.mailbox?.exists ?? 0;
8442
+ const safeFetch = async (range) => {
8443
+ try {
8444
+ const messages = await this.client.fetchAll(range, { uid: true, flags: true });
8445
+ for (const msg of messages) {
8446
+ if (query.unreadOnly && msg.flags?.has("\\Seen")) continue;
8447
+ if (query.starredOnly && !msg.flags?.has("\\Flagged")) continue;
8448
+ uids.push(msg.uid);
8468
8449
  }
8450
+ return uids.length > 0;
8451
+ } catch {
8452
+ return false;
8469
8453
  }
8470
- const count = exists > 0 ? exists : 50;
8471
- for (let seq = 1; seq <= count; seq++) {
8472
- try {
8473
- for await (const msg of this.client.fetch(String(seq), { uid: true, flags: true })) {
8474
- if (query.unreadOnly && msg.flags?.has("\\Seen")) continue;
8475
- if (query.starredOnly && !msg.flags?.has("\\Flagged")) continue;
8476
- uids.push(msg.uid);
8477
- }
8478
- } catch {
8454
+ };
8455
+ if (await safeFetch("1:*")) return uids;
8456
+ uids.length = 0;
8457
+ if (exists > 0) {
8458
+ if (await safeFetch(`1:${exists}`)) return uids;
8459
+ uids.length = 0;
8460
+ }
8461
+ const count = exists > 0 ? exists : 50;
8462
+ for (let seq = 1; seq <= count; seq++) {
8463
+ try {
8464
+ const msgs = await this.client.fetchAll(String(seq), { uid: true, flags: true });
8465
+ for (const msg of msgs) {
8466
+ if (query.unreadOnly && msg.flags?.has("\\Seen")) continue;
8467
+ if (query.starredOnly && !msg.flags?.has("\\Flagged")) continue;
8468
+ uids.push(msg.uid);
8479
8469
  }
8470
+ } catch {
8480
8471
  }
8481
- return uids;
8482
8472
  }
8473
+ return uids;
8483
8474
  }
8484
8475
  /**
8485
8476
  * Fetches email data for a set of UIDs, either with full body or lightweight headers.
@@ -8487,19 +8478,27 @@ var init_adapter3 = __esm({
8487
8478
  async fetchEmails(uids, folder, returnBody) {
8488
8479
  if (!this.client) return [];
8489
8480
  const emails = [];
8490
- if (returnBody) {
8491
- for await (const msg of this.client.fetch(uids, { source: true, uid: true, flags: true })) {
8481
+ const fetchOpts = returnBody ? { source: true, uid: true, flags: true } : { envelope: true, uid: true, flags: true, bodyStructure: true };
8482
+ const uidRange = uids.join(",");
8483
+ let messages;
8484
+ try {
8485
+ messages = await this.client.fetchAll(uidRange, fetchOpts, { uid: true });
8486
+ } catch {
8487
+ messages = [];
8488
+ for (const uid of uids) {
8489
+ try {
8490
+ const batch = await this.client.fetchAll(String(uid), fetchOpts, { uid: true });
8491
+ messages.push(...batch);
8492
+ } catch {
8493
+ }
8494
+ }
8495
+ }
8496
+ for (const msg of messages) {
8497
+ if (returnBody) {
8492
8498
  const parsed = await simpleParser(msg.source);
8493
8499
  parsed.flags = msg.flags;
8494
8500
  emails.push(mapParsedEmail(parsed, folder, this.accountId, msg.uid));
8495
- }
8496
- } else {
8497
- for await (const msg of this.client.fetch(uids, {
8498
- envelope: true,
8499
- uid: true,
8500
- flags: true,
8501
- bodyStructure: true
8502
- })) {
8501
+ } else {
8503
8502
  const env = msg.envelope;
8504
8503
  emails.push({
8505
8504
  id: String(msg.uid),