Skip to main content
Glama
jahfer

JMAP MCP Server

by jahfer

search_emails

Search emails in JMAP mailboxes using filters like date ranges, keywords, attachments, sender, recipient, subject, and content to find specific messages.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
inputSchemaYes

Implementation Reference

  • The handler function for the 'search_emails' tool. It destructures input parameters, constructs a JMAP filter object based on the provided search criteria, performs batched JMAP requests to query thread exemplar email IDs, fetch thread IDs, get emails in threads, and retrieve email details (id, subject, from, receivedAt, preview, mailboxIds), then returns the email details as JSON text content.
      async (inputs) => {
        try {
          const {
            mailboxId,
            limit,
            excludeMailboxIds,
            receivedBefore,
            receivedAfter,
            hasKeyword,
            notKeyword,
            hasAttachment,
            searchText,
            searchFrom,
            searchTo,
            searchCc,
            searchBcc,
            searchSubject,
            searchBody
          } = inputs;
    
          const session = await jam.session;
          const accountId = session.primaryAccounts["urn:ietf:params:jmap:mail"];
    
          // Construct the filter object based on provided inputs
          const filter = { inMailbox: mailboxId };
          if (excludeMailboxIds && excludeMailboxIds.length > 0) {
            filter.inMailboxOtherThan = excludeMailboxIds;
          }
          if (receivedBefore) {
            filter.before = receivedBefore;
          }
          if (receivedAfter) {
            filter.after = receivedAfter;
          }
          if (hasKeyword) {
            filter.hasKeyword = hasKeyword;
          }
          if (notKeyword) {
            filter.notKeyword = notKeyword;
          }
          if (typeof hasAttachment === 'boolean') {
            filter.hasAttachment = hasAttachment;
          }
          if (searchText) {
            filter.text = searchText;
          }
          if (searchFrom) {
            filter.from = searchFrom;
          }
          if (searchTo) {
            filter.to = searchTo;
          }
          if (searchCc) {
            filter.cc = searchCc;
          }
          if (searchBcc) {
            filter.bcc = searchBcc;
          }
          if (searchSubject) {
            filter.subject = searchSubject;
          }
          if (searchBody) {
            filter.body = searchBody;
          }
    
          const [results] = await jam.requestMany(b => {
            // 1. Query for the latest thread exemplar email IDs
            const queryEmailsDraft = b.Email.query({
              accountId,
              filter: filter, // Use the constructed filter object
              sort: [{ property: "receivedAt", isAscending: false }],
              position: 0,
              collapseThreads: true, // Collapse to get thread exemplars
              limit: limit // Limit the number of threads
            });
    
            // 2. Fetch the threadId of each of those exemplar messages
            const getThreadIdsDraft = b.Email.get({
              accountId,
              ids: queryEmailsDraft.$ref('/ids'), // Use $ref on the draft variable
              properties: ["threadId"]
            });
    
            // 3. Get the emailIds of the messages in those threads
            const getThreadEmailsDraft = b.Thread.get({
              accountId,
              ids: getThreadIdsDraft.$ref('/list/*/threadId'), // Use $ref on the draft variable
            });
    
            // 4. Finally we get the data for all those emails
            const getEmailDetailsDraft = b.Email.get({
              accountId,
              ids: getThreadEmailsDraft.$ref('/list/*/emailIds'), // Use $ref on the draft variable
              properties: ["id", "subject", "from", "receivedAt", "preview", "mailboxIds"]
            });
    
            return {
              queryEmails: queryEmailsDraft,
              getThreadIds: getThreadIdsDraft,
              getThreadEmails: getThreadEmailsDraft,
              getEmailDetails: getEmailDetailsDraft
            };
          });
    
          return {
            content: [{
              type: "text",
              text: JSON.stringify(results.getEmailDetails.list, null, 2)
            }]
          };
        } catch (error) {
          console.error("Error in fetch_latest_emails:", error);
          return {
            content: [{
              type: "text",
              text: `Error fetching emails: ${JSON.stringify(error) || 'Unknown error'}`
            }],
            isError: true
          };
        }
      }
    );
  • Zod input schema defining parameters for searching emails, including required mailboxId and various optional filters like dates, keywords, attachments, and search fields.
    inputSchema: z.object({
      mailboxId: z.string().describe("The mailbox to search in."),
      limit: z.number().default(15).describe("Max number of threads/emails to return."),
      excludeMailboxIds: z.array(z.string()).optional().describe("Mailboxes to exclude."),
      receivedBefore: z.string().datetime().optional().describe("Only emails received before this date (ISO 8601)."),
      receivedAfter: z.string().datetime().optional().describe("Only emails received after this date (ISO 8601)."),
      hasKeyword: z.string().optional().describe("Only emails with this keyword."),
      notKeyword: z.string().optional().describe("Only emails without this keyword."),
      hasAttachment: z.boolean().optional().describe("Only emails with/without attachments."),
      searchText: z.string().optional().describe("Full-text search."),
      searchFrom: z.string().optional().describe("Filter by sender."),
      searchTo: z.string().optional().describe("Filter by recipient (To)."),
      searchCc: z.string().optional().describe("Filter by recipient (Cc)."),
      searchBcc: z.string().optional().describe("Filter by recipient (Bcc)."),
      searchSubject: z.string().optional().describe("Filter by subject."),
      searchBody: z.string().optional().describe("Filter by body content.")
    })
  • index.js:83-84 (registration)
    MCP server registration of the 'search_emails' tool, specifying the tool name, description, input schema, and handler function.
    server.tool(
      "search_emails",
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Tool has no description.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness1/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Tool has no description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Tool has no description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Tool has no description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose1/5

Does the description clearly state what the tool does and how it differs from similar tools?

Tool has no description.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Tool has no description.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jahfer/jmap-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server