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
| Name | Required | Description | Default |
|---|---|---|---|
| attachment_id | Yes | ||
| inbox_id | Yes | ||
| message_id | Yes |
Implementation Reference
- node/src/functions.ts:43-69 (handler)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/src/tools.ts:73-77 (registration)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/src/agentmail_toolkit/tools.py:75-80 (registration)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/src/schemas.ts:35-39 (schema)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, })