MCP
By
Tendem Team
Give Your AI Agent a Human Fallback: A Tendem MCP Code Walkthrough
A hands-on look at Tendem's MCP examples repository – how to wire a human escalation path into your own agent with four tools, using a real OCR pipeline as the example.
Adapted from a screen-recorded walkthrough by Nikita, an ML engineer on the Tendem team, of the tendem-mcp examples repository. Commands and flags are summarized for clarity; confirm exact syntax against the repository README before running.
If you are building your own AI agent, the pattern in this walkthrough solves a specific problem: what happens when your agent hits something it cannot do reliably? Most agents either guess and move on, or stop and wait for you. Tendem's MCP gives your agent a third option – hand the task to a vetted human expert, without leaving your pipeline.
The example used throughout is an OCR pipeline that extracts fields from invoices. Most invoices are handled by the model alone. A couple, deliberately included as bad examples, are too blurry for the model to read confidently – and that is exactly when the pipeline escalates to a human.
Two ways to wire in a human
The repository ships two versions of the same pipeline, built to be interchangeable with each other:
Agentic flow | Scripted flow | |
Who decides to escalate | Your LLM, via tool calls | Your code, based on a confidence threshold |
How much code you write | A short prompt plus the tool list | A full control-flow script that calls the same four functions directly |
Best for | Getting a working escalation path quickly | Deterministic cost control, dedupe guards, and production reliability |
Both versions call the same underlying tools. The difference is who is driving: the LLM, or your own control flow.
Setting up the example
Clone the repository and open the OCR example folder. Inside, you will find a README that walks through setup in full – here is the short version.
1. Create the environment
From the OCR example directory, sync the virtual environment. The repo uses uv rather than pip, which resolves dependencies noticeably faster:
cd examples/ocruv sync
2. Configure your credentials
Copy the example environment file and fill in two things: an OpenAI-compatible LLM endpoint (your agent needs a model to run on), and a Tendem API key.
cp .env.example .env
Once you’re in the product, the full MCP connection instructions live at https://agent.tendem.ai/mcp – under the Agent builders tab.
3. Generate sample documents
A make-samples command creates a local set of test invoices, including one intentionally bad, blurry scan the model should not be confident about:
uv run make samples
Flow 1: the agentic approach
In the agentic flow, you plug Tendem's tools into your own agent framework (the example uses LangChain) and let the LLM itself decide when to call them. Your agent gets five tools in total: one specific to this use case, and four generic ones for working with a human.
The use-case-specific tool is a report tool: it takes the model's output and writes it to the results file, including a confidence score, notes, the source, and the extracted fields.
The four generic tools are what make the human escalation possible:
Tool | Type | What it does |
| Synchronous, not idempotent | Sends the task description and any local file paths to Tendem; uploads the files. Calling it twice creates two separate tasks. |
| Asynchronous, polls internally | Launches deterministic polling internally that checks task status periodically. For the agent it looks like a long-running tool. Does not require tokens while executed. Exits as soon as there is an update. Safe to relaunch several times. |
| Synchronous, text only | Sends your answer to a clarifying question back into the chat with Tendem's agent. May trigger a new or updated quote. |
| Asynchronous, polls internally | Similar to the check_human_task, but is used on a later stage when the task is already approved and is waiting for the result to be given back. Exits when the QA'd result is ready. Safe to launch several times. |
Notice the shape of that sequence: create, then poll for a quote or a question, then reply if needed, then wait for the finished result. That loop is the entire mechanism, regardless of which framework you plug it into.
Running it
You can run the agentic script with a spending cap and verbose logging, so you can watch what it decides in real time:
uv run ocr_agentic --max-price <cap> --watch --verbose
What happens when you run it
The --watch flag enables launching the pipeline for each new image that appears in the document folder. The model reads through them and, for the two it cannot confidently parse, creates a Tendem task for each. Creating a task does not commit you to anything yet: there is a separate quoting and approval step before a human actually starts work, and the max-price cap means a quote above your limit is never auto-approved.
Flow 2: the scripted approach
The scripted version directly calls the tools. It may use the same four tool or go on a lower abstraction method and use less abstract underlying tools.
If the confidence clears a threshold you set (via an environment variable), the pipeline writes the result directly, the same way the agentic report tool would. If it does not, an escalate function takes over and talks to Tendem – calling create_task and a separate upload_file step directly, an even lower-level entry point than the agentic tools use.
A guard worth copying into your own pipeline
Every approved task is billed against your API key, both versions keep a small local ledger, mapping each image to the Tendem task ID it already created. Before escalating, it checks whether that image has already been sent. Without this guard, retrying the pipeline on the same input would create duplicate tasks and duplicate charges.
The rest of the escalate function is a polling loop that deterministically handles every state a task can be in: a quote arrived, a result arrived, a clarifying question came back, or the quote came in over budget.
What happens on Tendem's side while you wait
Once a task is created, an internal Tendem agent estimates the scope, matches it to the right expert specialization, and prices it. In the recorded demo, a task capped at $5 came back with a quote of $3 and proceeded automatically. From there, a vetted human expert is matched – typically within about an hour – completes the work, and it passes through an internal QA step before the result is returned to your pipeline. None of that matching or QA work needs to be modeled in your code; it happens behind the check_human_task and wait_for_human_result calls.
When the result comes back, your local agent calls the same report tool used for everything else, so escalated and non-escalated items land in the same output file, in the same shape.
Which flow should you use
Start agentic if you want a working escalation path fast: wire in the four tools, write a short prompt, and let the model decide when it is out of its depth. Move to the scripted approach when you need tighter control over cost – explicit budget checks and a fully deterministic path through every state a task can reach. Nothing stops you from starting agentic and hardening specific steps into scripted code later; the four underlying MCP tools are the same either way.
Give your agent a human fallback.
Clone the examples repository, generate a Tendem API key from your account, and wire the four tools into whatever you are already building – LangChain agent or plain Python.

