Skip to main content
Glama

getAttachment

Retrieve specific email attachments by ID from an AgentMail inbox using inbox, message, and attachment IDs for efficient AI agent workflows.

Instructions

Get attachment by ID

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
attachment_idYes
inbox_idYes
message_idYes

Implementation Reference

  • Node.js handler function for getAttachment tool: fetches attachment by thread_id and attachment_id, detects MIME type, extracts text from PDF or DOCX using pdf-parse and mammoth, returns text or error.
    export async function getAttachment(client: AgentMailClient, args: Args): Promise<Attachment> { const { thread_id, attachment_id } = args const response = await client.threads.getAttachment(thread_id, attachment_id) const fileBytes = Buffer.from(await response.arrayBuffer()) const fileKind = await fileTypeFromBuffer(fileBytes) const fileType = fileKind?.mime let text = undefined if (fileType === 'application/pdf') { const parser = new PDFParse({ data: fileBytes, CanvasFactory }) const pdfData = await parser.getText() text = pdfData.text } else if (fileType === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') { const result = await mammoth.extractRawText({ buffer: fileBytes }) text = result.value } else { return { error: `Unsupported file type: ${fileType || 'unknown'}`, file_type: fileType, } } return { text, file_type: fileType } }
  • Python handler function for get_attachment tool: fetches attachment, detects type with filetype, extracts text from PDF using pymupdf or DOCX using docx, returns Attachment model with text or error.
    def get_attachment(client: AgentMail, kwargs: Kwargs): it = client.threads.get_attachment( thread_id=kwargs["thread_id"], attachment_id=kwargs["attachment_id"] ) file_bytes = b"".join(it) file_kind = filetype.guess(file_bytes) file_type = file_kind.mime if file_kind else None text = "" if file_type == "application/pdf": for page in pymupdf.Document(stream=file_bytes): text += page.get_text() + "\n" elif ( file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ): for paragraph in docx.Document(io.BytesIO(file_bytes)).paragraphs: text += paragraph.text + "\n" else: return Attachment( error=f"Unsupported file type: {file_type or 'unknown'}", file_type=file_type, ) return Attachment(text=text, file_type=file_type)
  • Node.js tool registration for 'get_attachment' using GetAttachmentParams schema and getAttachment handler.
    name: 'get_attachment', description: 'Get attachment', params_schema: GetAttachmentParams, func: getAttachment, },
  • Python tool registration for 'get_attachment' using GetAttachmentParams schema and get_attachment handler.
    Tool( name="get_attachment", description="Get attachment", params_schema=GetAttachmentParams, func=get_attachment, ),
  • Node.js Zod schema for getAttachment parameters: inbox_id, thread_id, attachment_id.
    export const GetAttachmentParams = z.object({ inbox_id: InboxIdSchema, thread_id: ThreadIdSchema, attachment_id: AttachmentIdSchema, })

Other Tools

Related 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/agentmail-to/agentmail-toolkit'

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