This project combines a LangChain-based email assistant agent with a React/Next.js chat UI for LangGraph. The agent uses dynamic middleware, prompt switching, tool gating, and human-in-the-loop controls to safely manage email workflows and support long-running conversations.
-
email_agent.py- Defines an authenticated email assistant agent.
- Uses dynamic tool selection based on authentication state.
- Implements middleware for prompt switching and tool gating.
- Exports a Graph-compatible
agentobject.
-
agent-chat-ui/- Next.js frontend chat app for talking to a LangGraph assistant.
- Connects to a LangGraph server using
apiUrl,assistantId, and optional API key. - Supports thread history, long conversations, and artifact rendering.
Watch a short demo of the Agent Chat UI and Email Assistant in action:
The core Python agent is defined in 3.5_email_agent.py:
EmailContextstores runtime context values such as email and password.AuthenticatedStateis anAgentStateschema with anauthenticatedboolean.authenticate,check_inbox, andsend_emailare LangChain tools.dynamic_tool_callmiddleware switches available tools at runtime:- When unauthenticated, only
authenticateis available. - After successful authentication,
check_inboxandsend_emailbecome available.
- When unauthenticated, only
dynamic_prompt_funcmiddleware selects the system prompt based on current state.HumanInTheLoopMiddlewareinterrupts thesend_emailtool so that email dispatch must be explicitly approved by a human before the assistant can continue.
This project uses two middleware capabilities:
@dynamic_promptto dynamically generate the assistant prompt based onauthenticatedstate.@wrap_model_callto dynamically override the tool list before each model invocation.
These features together create a dynamic agent that can:
- adapt its instructions based on authentication status
- enforce a strict zero-trust workflow
- switch available capabilities during a conversation
The email assistant is designed for safe operational behavior:
- Authentication is required before any inbox or send actions.
check_inboxis allowed once authenticated.send_emailis protected byHumanInTheLoopMiddleware.- This means the agent can draft or propose an email, but a human review step is required before a final send occurs.
The UI and agent support long-running conversations by preserving thread history and state:
- The chat frontend enables thread selection and conversation history.
fetchStateHistory: trueis configured in the stream provider so the UI can load past messages and maintain session continuity.- LangGraph state storage preserves the agent's
authenticatedstatus and any message history across requests.
- User opens the chat UI and connects to a LangGraph server.
- The assistant starts in an unauthenticated state.
- The agent asks for email credentials via the
authenticatetool. - On success, middleware switches prompt and activates email tools.
- The agent first reads the inbox using
check_inbox. - The user can request a reply;
send_emailis gated behind human approval.
- Node.js 18+ and
pnpm - Python 3.11+ or compatible
- A LangGraph-compatible backend or graph runner
GROQ_API_KEYfor the configured Groq model provider (used by3.5_email_agent.py)
Create a Python environment and install dependencies required by the agent. At a minimum, install:
pip install python-dotenv langchain langgraphThen make sure the environment can access the model provider:
export GROQ_API_KEY="your_groq_api_key"On Windows PowerShell:
$env:GROQ_API_KEY = "your_groq_api_key"From agent-chat-ui/:
cd agent-chat-ui
pnpm install
pnpm devThe UI will start on http://localhost:3000 by default.
The frontend supports optional environment variables defined from .env:
NEXT_PUBLIC_API_URL=http://localhost:2024
NEXT_PUBLIC_ASSISTANT_ID=agentCopy agent-chat-ui/.env.example to .env if you want to hardcode values.
Run the frontend and point it at your LangGraph deployment.
Deployment URL: URL of the LangGraph serverAssistant/Graph ID: graph or assistant ID, typicallyagentLangSmith API Key: optional for deployed LangGraph hosts
- Ask the assistant to read emails or send a message.
- The agent should respond that it must authenticate first.
- Provide the email and password and verify the
authenticatetool runs.
- After authentication, the agent should immediately check the inbox with
check_inbox. - Confirm the inbox summary appears and the assistant can reference recent email content.
- Ask the assistant to send an email.
- The
send_emailtool is configured to interrupt underHumanInTheLoopMiddleware. - Confirm that final send requires explicit approval or review before it executes.
- Send multiple messages and continue the same thread.
- Reload the UI or revisit the thread.
- Confirm the previous messages and state are reloaded correctly.
authenticate(email, password, runtime)returns aCommandthat updates the agent state and sends a tool message.check_inbox()simulates reading recent email content.send_email(to, subject, body)returns a confirmation string.
There are two prompt modes:
unauthenticated_prompt: instructs the assistant to authenticate before performing email actions.authenticated_prompt: instructs the assistant to manage inbox and email tasks securely.
The dynamic prompt is selected at runtime based on whether authenticated is True.
The agent middleware pipeline includes:
dynamic_tool_call: to decide which tools are available per request.dynamic_prompt_func: to choose the correct system prompt based on state.HumanInTheLoopMiddleware: to require human confirmation for sensitive actions.