Skip to main content
Glama

get_attachment

Download email attachments to a specified file path using the Microsoft MCP server. Input email ID, attachment ID, save path, and account ID to retrieve and store attachments efficiently.

Instructions

Download email attachment to a specified file path

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
account_idYes
attachment_idYes
email_idYes
save_pathYes

Implementation Reference

  • The core handler function for the 'get_attachment' tool. It retrieves the attachment from Microsoft Graph API using the provided email_id and attachment_id, decodes the base64-encoded contentBytes, saves the file to the specified save_path, and returns metadata including name, content_type, size, and saved path.
    def get_attachment(
        email_id: str, attachment_id: str, save_path: str, account_id: str
    ) -> dict[str, Any]:
        """Download email attachment to a specified file path"""
        result = graph.request(
            "GET", f"/me/messages/{email_id}/attachments/{attachment_id}", account_id
        )
    
        if not result:
            raise ValueError("Attachment not found")
    
        if "contentBytes" not in result:
            raise ValueError("Attachment content not available")
    
        # Save attachment to file
        path = pl.Path(save_path).expanduser().resolve()
        path.parent.mkdir(parents=True, exist_ok=True)
        content_bytes = base64.b64decode(result["contentBytes"])
        path.write_bytes(content_bytes)
    
        return {
            "name": result.get("name", "unknown"),
            "content_type": result.get("contentType", "application/octet-stream"),
            "size": result.get("size", 0),
            "saved_to": str(path),
        }
  • The server entry point imports the FastMCP instance (mcp) from tools.py where all tools including get_attachment are decorated and registered, and calls mcp.run() to start the MCP server, making the tool available.
    from .tools import mcp
    
    
    def main() -> None:
        if not os.getenv("MICROSOFT_MCP_CLIENT_ID"):
            print(
                "Error: MICROSOFT_MCP_CLIENT_ID environment variable is required",
                file=sys.stderr,
            )
            sys.exit(1)
    
        mcp.run()
  • Creates the FastMCP server instance named 'microsoft-mcp' to which all tools, including get_attachment, are registered via @mcp.tool decorators.
    from fastmcp import FastMCP
    from . import graph, auth
    
    mcp = FastMCP("microsoft-mcp")

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/elyxlz/microsoft-mcp'

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