[Feature Request] Agent v2: persist large MCP JSON results directly to sandbox files

Open
#46,581 1 comment 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

@CryoThrust is already working on this.

Since Nov 17, 2026.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
ai, backend

Research direction

Start by tracing the Agent v2 MCP route in api/core/workflow/nodes/agent_v2/dify_tools_builder.py, then follow result handling in api/services/agent_tool_inner_service.py and dify-agent/src/dify_agent/layers/dify_core_tools/layer.py. The change spans tool execution and sandbox file access; define a result-to-artifact contract and verify that later Python calls can read the full payload while the model sees only a bounded preview.

Written by the indexing model from the issue text.

Description

💪 enhancement
Self Checks
  • I have read the Contributing Guide and Language Policy.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • Please do not modify this template :) and fill in all the required fields.
1. Is this request related to a challenge you're experiencing? Tell me about your story.

We want to use Dify Agent v2 / the new Agent sandbox for business analytics. All data comes from our platform's custom MCP query tools. The tools return ordinary JSON; users do not upload spreadsheets, and these are not database MCP integrations.

A query can return a large dataset that Python should analyze, for example period-over-period changes or IQR/Z-score outliers. The missing capability is a runtime-owned path from the MCP result to a file readable inside the current Agent sandbox.

With the inspected core-tool path, the result becomes an observation returned to the agent. To use the full dataset in Python, having the model reproduce the JSON inside a shell command or another tool argument is costly and unreliable: it consumes input/output tokens and can truncate or alter the data. Clearing or summarizing historical results also cannot provide Python with the original dataset.

Requested behavior

When an MCP tool returns a large JSON result, Agent v2 should:

  1. Preserve the complete result in the execution layer, before model-context truncation or summarization.
  2. Write the JSON directly into the current Agent's sandbox filesystem, using a runtime-generated path.
  3. Return only that sandbox-readable path, result size, and a bounded preview to the model.
  4. Let subsequent Python/shell calls read the file without the model copying the dataset into a new tool call.
MCP query -> Dify tool execution -> runtime writes full JSON to Agent sandbox
                                           |
                                           +-> LLM: file path + bounded preview
                                           +-> Python: read and analyze full file

For example, an illustrative 5 MB JSON result could become a small observation like:

{
  "result_file": "/home/sandbox/tool-results/<tool-call-id>.json",
  "mime_type": "application/json",
  "bytes": 5242880,
  "preview": "<bounded preview>"
}

The path and fields above are illustrative, not an existing Dify API. A path on the remote MCP server or the Dify API container is insufficient unless Dify makes the contents accessible inside this Agent's sandbox.

An opt-in per-tool inline / file / auto policy with a configurable size threshold would make this usable for analytics while retaining small inline responses. Ordinary MCP text containing JSON and structured JSON results should both work without requiring each MCP server to implement a separate file-export endpoint.

Acceptance criteria

  • Python can parse the saved result and obtain all records and original values; the runtime does not ask the LLM to reconstruct them.
  • The full payload is not inserted into the model context when file mode is selected.
  • The file belongs to the current Agent sandbox/session and remains readable by later tool calls in that session.
  • A failed file write is reported clearly and does not silently substitute a truncated dataset.
2. Additional context or comments

Possible architectural cause

From source inspection, the gap appears to be at the boundary between Dify's MCP/core-tool execution and Agent v2's sandbox execution. Tool invocation has the complete result, while Python runs in the sandbox; the model-facing observation currently connects them without a general runtime-owned result-to-file bridge.

At inspected upstream revision ef7c7d6:

This architectural explanation is an inference from these paths, not a claim that separate services must be merged. A shared result/artifact contract could bridge the existing components. The request is based on source inspection and our intended integration; the 5 MB example above is illustrative, not a reported production benchmark.

Related approaches in other agents

  • Claude Code documents saving oversized successful MCP results without images to a session file and returning a file-path message to the conversation. This is the closest direct precedent.
  • Pi's Bash tool saves full output to a temporary file when truncating the model-visible output, and includes the path. This establishes the full-output-file pattern for Bash, not generic MCP support.
  • Codex Code Mode supports programmatic handling of tool results and selective emission to the model. It is a related way to keep data in the execution layer; it is not cited as proof of automatic MCP-to-file persistence.

These are distinct implementations of keeping full results accessible outside model-visible text. The specific request here is MCP JSON -> current Dify Agent sandbox file -> Python, with the LLM receiving a reference.

3. Can you help us with this feature?
  • I am interested in contributing to this feature.
Dominant language
Python
Stars
162k
Forks
25.6k
Avg merge
15h 9m
Merged PRs (30d)
606

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from langgenius/dify

All issues in langgenius/dify

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.