[Feature Request] Agent v2: persist large MCP JSON results directly to sandbox files
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
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
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:
- Preserve the complete result in the execution layer, before model-context truncation or summarization.
- Write the JSON directly into the current Agent's sandbox filesystem, using a runtime-generated path.
- Return only that sandbox-readable path, result size, and a bounded preview to the model.
- 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:
- Agent v2 routes MCP providers through the core-tool path.
- The API returns messages plus a formatted observation. Existing explicit file/media results have reference handling, which might be reusable for this feature.
- The Agent core-tool layer returns
response.observation; this path does not itself persist ordinary large JSON into the sandbox.
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
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from langgenius/dify
-
OpenSearch: text_exists() queries OpenSearch _id instead of metadata.doc_id, so it always returns False for indexed chunksPossibly taken @SanemIzci claimed this 1 day ago. Open🐞 bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
langgenius/dify#47109 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Log only execution identifiers when a workflow worker starts a runPossibly taken @kinglegendzzh claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
langgenius/dify#47094 · 2 reactions ·
Maintainers usually reply within 1 day
-
Node.js SDK declares a response body for 204 conversation deletionPossibly taken @Wang-Zephyr claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
langgenius/dify#46883 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Provider IDs with a trailing newline are silently accepted and propagatedPossibly taken @Batianxieshen1 claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
langgenius/dify#46875 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Agent Python SDK polling rejects function-tool results that SSE acceptsPossibly taken @EndlessRoad-max claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
langgenius/dify#46868 · 1 reaction ·
Maintainers usually reply within 1 day
Similar issues
-
app: scripts status: backlog type: housekeeping
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
netbox-community/netbox-scripts#239 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
LmeSzinc/AzurLaneAutoScript#6194 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
micronaut-projects/micronaut-core#14585 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
langgenius/graphon#443 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
Maintainers usually reply within 1 day