Connect LlamaIndex agents to their own email inbox

Checked · Markdown

Short answer

Install llama-index-tools-mcp, create a BasicMCPClient for https://agentline.nz/mcp with your Agentline NZ API key in an Authorization: Bearer header, and pass McpToolSpec(client=client).to_tool_list_async() to a FunctionAgent. The agent can then send and read email from its own @agentline.nz inbox and wait for one-time codes sent by email.

Before you start

  • An Agentline NZ API key from Dashboard → API keys, exported as AGENTLINE_KEY. No credit card needed to sign up. Start free with an email inbox, or try Starter or Pro free for 7 days.
  • Your @agentline.nz email inbox works straight away. Texting and phone numbers are coming soon.
  • An LLM for the agent. The example uses OpenAI, with OPENAI_API_KEY set; any LlamaIndex LLM that supports tool calling works.

Steps

  1. Install the MCP tools package. llama-index-tools-mcp brings in llama-index-core. Install the LLM package you want as well.
    pip install llama-index-tools-mcp llama-index-llms-openai
  2. Connect and build the agent. A URL that doesn't end in /sse selects Streamable HTTP. headers is sent with every request, and timeout is in seconds.
    import asyncio, os
    from llama_index.core.agent.workflow import FunctionAgent
    from llama_index.llms.openai import OpenAI
    from llama_index.tools.mcp import BasicMCPClient, McpToolSpec
    
    async def main():
        client = BasicMCPClient(
            "https://agentline.nz/mcp",
            headers={"Authorization": f"Bearer {os.environ['AGENTLINE_KEY']}"},
            timeout=60,
        )
        tools = await McpToolSpec(client=client).to_tool_list_async()
        agent = FunctionAgent(
            name="Assistant",
            description="Sends and reads email and picks up one-time codes with Agentline NZ.",
            tools=tools,
            llm=OpenAI(model="gpt-4o"),
            system_prompt="Use Agentline NZ to send and read email and wait for one-time codes.",
        )
        print(await agent.run("What's my Agentline NZ email address?"))
    
    asyncio.run(main())
  3. Run it. Run the script. The agent should call get_my_number and print your inbox address.

Limit the agent to some tools

Pass allowed_tools to keep an agent read-only, for example:

spec = McpToolSpec(
    client=client,
    allowed_tools=["get_my_number", "list_emails", "read_email", "wait_for_code"],
)

The sending tool is send_email. wait_for_code doesn't send anything, but it isn't strictly read-only: it consumes each code it returns, so the code isn't returned again.

Using the REST API instead

If you'd rather not use MCP, wrap a few REST calls as FunctionTools. The API is at https://agentline.nz/api/v1 with the same Authorization: Bearer header:

import os, httpx
from llama_index.core.tools import FunctionTool

API = "https://agentline.nz/api/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['AGENTLINE_KEY']}"}

def send_email(to: str, subject: str, text: str) -> dict:
    """Send an email from the agent's @agentline.nz address."""
    return httpx.post(f"{API}/emails", headers=HEADERS, json={"to": to, "subject": subject, "text": text}).json()

def list_inbound_emails(limit: int = 20) -> dict:
    """List received emails, newest first."""
    return httpx.get(f"{API}/emails", headers=HEADERS, params={"direction": "in", "limit": limit}).json()

def wait_for_code(timeout_seconds: int = 45) -> dict:
    """Wait for a one-time code to arrive by email."""
    return httpx.post(
        f"{API}/wait-for-code",
        headers=HEADERS,
        json={"channel": "email", "timeout_seconds": timeout_seconds},
        timeout=60,
    ).json()

tools = [FunctionTool.from_defaults(fn) for fn in (send_email, list_inbound_emails, wait_for_code)]

POST /emails returns {"email": {…}}, GET /emails returns {"emails": [...]}, and POST /wait-for-code returns {"found": true, "code": "…", …} or {"found": false}. Errors come back as {"error": {"code": "…", "message": "…"}}, which the agent can read. The docs list every endpoint.

Good to know

  • Keep timeout at 60 seconds or more. wait_for_code can wait up to 55 seconds, and BasicMCPClient's default is 30.
  • Until you're on a paid plan, your agent can only email your own verified email address. Anything else returns recipient_not_allowed.
  • Texting and phone numbers are coming soon. To hear when they're ready, join the waitlist.
  • BasicMCPClient opens a new session for each call, so you don't need to close it.

FAQ

Can I use aget_tools_from_mcp_url instead?

Yes. Pass the same BasicMCPClient as client, for example await aget_tools_from_mcp_url('https://agentline.nz/mcp', client=client). It returns the same tool list as McpToolSpec and also takes allowed_tools.

What does Agentline NZ cost?

Starter is NZ$29/month and Pro is NZ$79/month, billed in NZ dollars. No credit card needed to sign up. Start free with an email inbox, or try Starter or Pro free for 7 days. You can cancel anytime, and your plan stops at the end of the billing period you've paid for. Refunds follow section 5 of our Terms: fees already paid aren't refunded, except where the Terms or the law say so. Agentline NZ is not GST-registered, so no GST is charged.

When does the inbox work, and can my assistant send texts yet?

Your @agentline.nz email inbox works straight away. Texting and phone numbers are coming soon. To hear when texting is ready, join the waitlist.

Can my assistant send marketing or bulk email?

No. Transactional and assistant messages only: replies, bookings, confirmations, reminders people asked for, and codes. Marketing, promotions, cold messages and bulk messages aren't allowed. See What we don't allow.

Start free · No credit card needed to sign up. Start free with an email inbox, or try Starter or Pro free for 7 days.