Two-turn report and code helper with session artifacts
What you get: A chat endpoint that builds a PDF report from MCP data on one turn, saves it for the session, and writes a Python script from that saved report on a later turn.
Needs: A model with tool calling (local Ollama as shipped) and an MCP server · Time: 20 minutes · Nodes: 16
A Webhook Input at POST /webhook/two-turn-report-code-helper takes a user_prompt and a session_id. Session Artifact Load reads the report saved earlier in that session, if any. An Agent Orchestrator with a chat model, a Simple Memory and every tool of one MCP server answers with one JSON object: status, intent, final_html, python_code, follow_up_question, chart_data and artifact_context. An Output Parser checks it, and a Chat Intent Router picks a branch. A report turn prints final_html to PDF, saves the report with Session Artifact Save and returns it with the PDF attached. A code turn returns the script. Anything else returns a message. As shipped, the chat model is qwen2.5:7b on a local Ollama server, the MCP endpoint is a placeholder, and the webhook has no authentication.
Steps
Download two-turn-report-code-helper-flow.json, or take it from the samples folder on GitHub.
In the studio's workflow editor, import the file (see import and export).
Point Chat Model at a model that can call tools and keep to a JSON format: pull
qwen2.5:7binto Ollama, or switch the provider and pick a stored key. To check the plumbing without a model, set itsproviderIdtoechoin the JSON file and import it again (the Provider list has no echo entry). Echo never calls a tool.Point MCP Tools at your MCP server: set
connection.endpointto its streamable HTTP URL (http://127.0.0.1:7001/mcpis a placeholder).toolName: "__all__"gives the agent every tool the server lists. For a token, setconnection.authTypetobearerand asecretRef, or register the server in the MCP server registry and setregistryServerId.Install Chromium for Playwright for the PDF:
pnpm exec playwright install chromiumfrom the repo root, or setPDF_CHROMIUM_EXECUTABLE_PATH.Ask for a report, with a session id:
bashcurl -X POST http://localhost:4000/webhook/two-turn-report-code-helper \ -H "Content-Type: application/json" \ -d '{"user_prompt": "Build a PDF report of our keys by cloud", "session_id": "team-a-1"}'Ask for the code in the same session: send
"user_prompt": "Now give me a Python script that regenerates it nightly without AI"with the samesession_id.Read
output.result. A report turn has the PDF inattachments(filename,sizeBytesand adata:URL indownloadUrl). A code turn has{ language, source }incodes. When the agent needs more,statusisneeds_more_infoandfollow_up_questionsays what.
How it works
| Node | Type | Role |
|---|---|---|
| Webhook Input | webhook_input | Step in the run |
| Load Latest Report Artifact | session_artifact_load | Step in the run |
| Agent Orchestrator | agent_orchestrator | Step in the run |
| Output Parser | output_parser | Step in the run |
| Route Intent | chat_intent_router | Step in the run |
| PDF Output | pdf_output | Step in the run |
| Build Code Response | helper_chat_response | Step in the run |
| Build Message Response | helper_chat_response | Step in the run |
| Save Latest Report Artifact | session_artifact_save | Step in the run |
| Code Output | output | Step in the run |
| Message Output | output | Step in the run |
| Build Report Response | helper_chat_response | Step in the run |
| Report Output | output | Step in the run |
| Chat Model | llm_call | Attached to Agent Orchestrator on its chat_model port |
| Simple Memory | local_memory | Attached to Agent Orchestrator on its memory port |
| MCP Tools | mcp_tool | Attached to Agent Orchestrator on its tool port |
12 node types. Each type links to its config fields in the node reference.
What a run does
We ran it on a local install with two stand-ins: a local MCP server whose one tool, list_keys, returned 12 keys, and a scripted OpenAI-compatible model written to follow the system prompt. A real model's answers depend on the model.
Turn 1, "Build a PDF report of our keys by cloud", session t1. The Load node found nothing. The agent was offered session_cache_list, session_cache_get, agent_clarify and http_mcp__list_keys, called list_keys, then answered with a report object. The parser accepted it, and the router took the agent's own intent: "report". PDF Output printed a one-page A4 file, mcp-report-t1.pdf (15,717 bytes), the Save node stored five fields as latest_report, and the reply carried the PDF in attachments. The run took about 0.7 seconds.
Turn 2, "Now give me a Python script that regenerates it nightly without AI", same session. The Load node found the report, and the agent's prompt held it as JSON. Memory added the first turn's question and answer, but not the tool result (Persist Tool Messages is off). The agent called no tool and answered with intent: "code"; the reply carried the script in codes, and the PDF and Save nodes were skipped.
A new session asking for code had no saved report, so the stand-in returned status: "needs_more_info" with a question, as the system prompt asks.
With the echo provider, the agent's answer is its own prompt. The parser asked once more (maxRetries: 1), then gave parsed: null, and the router fell back to keywords: "report" picked the report branch. The PDF held only the request text, and every field of the saved report was null. On the router, intentSource: "explicit" means the agent's intent was used, keyword means the fallback matched a keyword, and default means nothing matched.
Memory, artifacts and the tool cache
The workflow keeps three things per session, under the namespace two-turn-report-code-helper:
- Memory: the last 20 messages (
maxMessages), what the user asked and what the agent answered. - The artifact: one exact JSON value under the key
latest_report, which the next turn sees word for word instead of a summary. Each report turn replaces it. - The session tool cache: each tool result from the agent's turns. With a
session_id, the agent getssession_cache_listandsession_cache_get, so it can reuse an earlierlist_keysanswer. In our runs each report turn stored onelist_keysentry for its session.
All three need a session_id; without one, nothing is loaded or saved. They stay in the L2M database until you set AGENT_MEMORY_RETENTION_DAYS (by default they are kept): see data retention and agent memory.
How the router decides
The Chat Intent Router reads parsed.intent first: report, code, message or a synonym such as pdf or python. If that is missing, it searches the request for its keywords: code words (python, script, nightly ...) beat report words (report, pdf, chart ...), and neither means a message.
With requireArtifactForCode on, a code request with no saved report takes a fourth branch, missing_context. The sample does not wire it, and with nothing there the run ends with no output, so add a reply node first. The router finds the report through artifactPath, results.load_report_artifact.latest_report, because its own input comes from the parser.
What to check before you rely on it
The agent writes final_html, and Chromium renders it with scripts on; the URLs it loads go through the outbound URL checks. The agent also writes the Python script. The prompt asks for direct REST calls, secrets from environment variables and no invented endpoints, but treat the script as a draft and read it before you run it.
If the MCP server is down, the run does not fail. With our server stopped, the agent saw only the three built-in tools, its list_keys call came back as Tool 'list_keys' does not exist, and it answered without data. Check _mcpTools on the Agent node when a report comes back empty.
Make it yours
- Use your own data: the prompt names no product, so any MCP server with read tools works. Edit its report rules to name the tools and fields you want.
- Change what is kept: edit the Save node's
fieldslist, and the next turn sees exactly those. - Build the report from fixed nodes instead of model-written HTML: the branded PDF inventory report draws charts from an MCP tool's answer with no model in the data path.
Make it safer
These controls are optional; the recipe runs without them.
- Secure the webhook before anyone else can reach it, with a bearer token or an HMAC signature: see webhook auth modes.
- The agent gets every tool the server lists. Give it a token that can only read, and optionally hold tool calls for a person: see human approval for agent tool calls.
- Turn on
OUTBOUND_DENY_PRIVATE(off by default) so model-written HTML cannot load internal URLs: see outbound URL checks. - Cap spend with a workflow budget.