Skip to content

amri369/mistral-mcp-temporal-logfire

Repository files navigation

Durable Stateful Agents with Mistral + Temporal + MCP

LLM agents can break on multi-step workflows. An API timeout halfway through kills all progress. Conversation state gets lost between calls. And writing manual retry logic is brittle and painful to maintain.

This architecture addresses all of that:

  • Mistral Agent API manages conversation state server-side (no history passing)
  • Temporal orchestrates durable execution with automatic retries
  • MCP provides standardized tool interfaces (swap providers without code changes)
  • Logfire instruments the entire request path for production observability

Built for critical workflows where tasks can take minutes and failures are expensive.

System Architecture

Evolution from Previous Work

Long-running LLM agent workflows can be subjected to failures, network issues, and third-party API exceptions. In many situations, manual intervention is needed to identify issues and debug them. For production systems, this can be prohibitively expensive. This is the main reason many companies rely on Temporal for orchestrating agents in production.

The other challenge that can arise is agent status management and short memory for conversational agents.

To address these challenges, I extended my previous durable agents with Temporal project with:

  • Stateful agents – Mistral manages agent definitions and conversation history server-side (beta feature)
  • Enhanced retriesHTTP retry mechanism distinguishes retriable vs. terminal failures
  • API gateway – FastAPI server triggers workflows and polls results for frontend clients
  • Full observability – Logfire traces capture MCP/FastAPI server transactions, agent status, token usage, and retry behavior
  • MCP tools – Dedicated MCP server with Yahoo Finance integration

Architecture

Core Stack:

  • Mistral AI Agent API – Server-side stateful agents. Register agents on Mistral's infrastructure, update their configurations, and run multi-turn conversations with persistent state managed by Mistral.
  • Temporal Workflows – Durable orchestration. Activities automatically retry on failure; workflows resume from last checkpoint.
  • MCP Server – Exposes tools and prompts over HTTP/SSE. Agents discover capabilities at runtime.
  • Logfire (optional) – OpenTelemetry-native observability. Auto-instruments FastAPI endpoints and Mistral agent calls with full request/response traces.

Key Design Decisions:

  1. Mistral hosts agent state server-side – Unlike traditional stateless LLM APIs, Mistral's Agent API maintains agent definitions and conversation history on their servers. You register agents once, then interact via agent_id.
  2. Temporal workflows orchestrate agent interactions – Workflow state persists locally while Mistral manages agent and conversation state remotely. Workflows survive crashes and coordinate multi-step agent tasks.
  3. Prompt and tools access via MCP server – Temporal activities create agents with instructions fetched from a local MCP server. Instruction prompts are separated from code. Agents can run available tools server side.
  4. FastAPI orchestrates requests – Receives user queries, triggers Temporal workflows, and polls workflow results using Temporal query handles.
  5. Logfire instruments the entire request path – Captures FastAPI route traces, Mistral API calls with token usage, and correlates them in a unified trace view.

Data Flow:

User Request → FastAPI → Temporal Workflow → Mistral Agent API (stateful)
      ↓            ↓              ↓                  ↓
   Logfire ←──────┴──────────────┴──────────────────┘
                                         ↓
                                 MCP Server (tools/prompts)
                                         ↓
                                 Execute tool activities
                                         ↓
                                 Stream response → User

Quick Start

Prerequisites

  • Python 3.11+ with uv
  • Temporal CLI (brew install temporal)
  • Mistral API key
  • Logfire API key

1. Environment Setup

uv sync
source .venv/bin/activate

2. Launch Services (in separate terminals)

Terminal 1: MCP Server

export PYTHONPATH=.
uv run uvicorn mcp_server.main:app --reload --port 9000

To inspect and interact with the server, run:

export PYTHONPATH=.
mcp dev mcp_server/financial_research_server.py

Terminal 2: Temporal Dev Server

temporal server start-dev

Terminal 3: Temporal Worker Executes workflow and activity tasks.

export PYTHONPATH=.
uv run --env-file .env tasks/worker.py

Terminal 4: FastAPI Gateway

export PYTHONPATH=.
uv run uvicorn api.main:app --reload

3. Test the Agent

export PYTHONPATH=.
uv run --env-file .env examples/main.py

API Documentation: http://localhost:8000/docs

Observability in Action

Logfire Traces

The screenshot above shows a financial query about Nvidia triggered via FastAPI. Logfire captures the entire execution path:

  • MCP prompt fetching
  • FastAPI request lifecycle
  • Mistral agent execution status and metadata (including token usage)
  • Automatic retry behavior: One Mistral API call failed due to a structured output parsing error. Temporal automatically retried until it succeeded.

Temporal Event History

The Temporal UI shows the workflow's event history, including the start_conversation_activity that failed once (Attempt 2) before succeeding. This demonstrates how Temporal's durable execution handles transient failures without developer intervention.

Why This Stack?

Mistral's stateful agents are unique among LLM providers—agents are registered and managed server-side, eliminating the need to pass full conversation history with every request.

Temporal handles transient failures (API timeouts, rate limits) with automatic retries and orchestrates complex multi-agent workflows. Workflow state persists across process restarts.

MCP decouples tool implementation from agent logic. Swap tool providers without changing agent code. Standardized interface for tool discovery and execution, and prompts.

Logfire provides a unified view of workflow executions, LLM calls (with prompts, completions, token counts), and FastAPI request lifecycle—critical for debugging production issues.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •