Skip to content

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 ​

  1. Download two-turn-report-code-helper-flow.json, or take it from the samples folder on GitHub.

  2. In the studio's workflow editor, import the file (see import and export).

  3. Point Chat Model at a model that can call tools and keep to a JSON format: pull qwen2.5:7b into Ollama, or switch the provider and pick a stored key. To check the plumbing without a model, set its providerId to echo in the JSON file and import it again (the Provider list has no echo entry). Echo never calls a tool.

  4. Point MCP Tools at your MCP server: set connection.endpoint to its streamable HTTP URL (http://127.0.0.1:7001/mcp is a placeholder). toolName: "__all__" gives the agent every tool the server lists. For a token, set connection.authType to bearer and a secretRef, or register the server in the MCP server registry and set registryServerId.

  5. Install Chromium for Playwright for the PDF: pnpm exec playwright install chromium from the repo root, or set PDF_CHROMIUM_EXECUTABLE_PATH.

  6. Ask for a report, with a session id:

    bash
    curl -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"}'
  7. 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 same session_id.

  8. Read output.result. A report turn has the PDF in attachments (filename, sizeBytes and a data: URL in downloadUrl). A code turn has { language, source } in codes. When the agent needs more, status is needs_more_info and follow_up_question says what.

How it works ​

Diagram of the Two-Turn MCP Report + Code Helper workflowRuns Webhook Input, then Load Latest Report Artifact, then Agent Orchestrator, then Output Parser, then Route Intent, then PDF Output, then Build Code Response, then Build Message Response, then Save Latest Report Artifact, then Code Output, then Message Output, then Build Report Response, then Report Output. Chat Model is attached to Agent Orchestrator as its chat model; Simple Memory is attached to Agent Orchestrator as its memory; MCP Tools is attached to Agent Orchestrator as its tool.Webhook Input (webhook_input)Webhook Inputwebhook_inputLoad Latest Report Artifact (session_artifact_load)Load Latest Report Arti…session_artifact_loadAgent Orchestrator (agent_orchestrator)Agent Orchestratoragent_orchestratorChat Model (llm_call)Chat Modelllm_callSimple Memory (local_memory)Simple Memorylocal_memoryMCP Tools (mcp_tool)MCP Toolsmcp_toolOutput Parser (output_parser)Output Parseroutput_parserRoute Intent (chat_intent_router)Route Intentchat_intent_routerPDF Output (pdf_output)PDF Outputpdf_outputBuild Report Response (helper_chat_response)Build Report Responsehelper_chat_responseSave Latest Report Artifact (session_artifact_save)Save Latest Report Arti…session_artifact_saveReport Output (output)Report OutputoutputBuild Code Response (helper_chat_response)Build Code Responsehelper_chat_responseCode Output (output)Code OutputoutputBuild Message Response (helper_chat_response)Build Message Responsehelper_chat_responseMessage Output (output)Message Outputoutput
Solid arrows are the order the steps run in. Dashed lines attach a chat model, memory, tools or workers to an agent; the agent uses them while it runs.
NodeTypeRole
Webhook Inputwebhook_inputStep in the run
Load Latest Report Artifactsession_artifact_loadStep in the run
Agent Orchestratoragent_orchestratorStep in the run
Output Parseroutput_parserStep in the run
Route Intentchat_intent_routerStep in the run
PDF Outputpdf_outputStep in the run
Build Code Responsehelper_chat_responseStep in the run
Build Message Responsehelper_chat_responseStep in the run
Save Latest Report Artifactsession_artifact_saveStep in the run
Code OutputoutputStep in the run
Message OutputoutputStep in the run
Build Report Responsehelper_chat_responseStep in the run
Report OutputoutputStep in the run
Chat Modelllm_callAttached to Agent Orchestrator on its chat_model port
Simple Memorylocal_memoryAttached to Agent Orchestrator on its memory port
MCP Toolsmcp_toolAttached 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 gets session_cache_list and session_cache_get, so it can reuse an earlier list_keys answer. In our runs each report turn stored one list_keys entry 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 fields list, 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.

Further reading ​

This website (not the L2M product) uses Google Analytics, only after you accept cookies. Cookie settings