fix(agent): enable MCP file preview via doc_id (#15399)

## Summary
MCP-wrapped agents could only force-download files looked up by
`doc_id`. This adds an explicit preview path and inline response headers
for previewable file types.

- **New** `GET /api/v1/agents/attachments/{attachment_id}/preview` —
inline preview for PDFs, images, and other safe types (pass `ext` and/or
`mime_type`)
- **Improved** `GET /api/v1/documents/{doc_id}/preview` — sets inline
disposition using the document filename
- **Improved** attachment download routing — resolves `mime_type` /
`ext` query params (no default `markdown`), supports
`disposition=inline`
- **DocGenerator output** — includes URL-encoded `preview_url` for MCP
clients
- **Legacy `/document/download/...` aliases** — still use download
semantics; MCP clients should call `/preview` explicitly

Fixes #15398

## Test plan
- [x] `pytest test/unit_test/api/utils/test_file_response_headers.py`
(6/6)

---------

Co-authored-by: MkDev11 <mkdev11@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Ling Qin <qinling0210@163.com>
This commit is contained in:
monsterDavid
2026-07-03 04:56:01 -07:00
committed by GitHub
parent 0f4f2135f3
commit 7da4f200e5
8 changed files with 295 additions and 106 deletions

View File

@@ -280,6 +280,24 @@ export default {
cancelDataflow: (id: string) => `${restAPIv1}/tasks/${id}/cancel`,
getAttachmentFileDownload: (docId: string) =>
`${restAPIv1}/agents/attachments/${docId}/download`,
getAttachmentFilePreview: ({
docId,
ext,
mimeType,
filename,
}: {
docId: string;
ext?: string;
mimeType?: string;
filename?: string;
}) => {
const params = new URLSearchParams();
if (ext) params.set('ext', ext);
if (mimeType) params.set('mime_type', mimeType);
if (filename) params.set('filename', filename);
const query = params.toString();
return `${restAPIv1}/agents/attachments/${docId}/preview${query ? `?${query}` : ''}`;
},
downloadFile: `${restAPIv1}/agents/download`,
testWebhook: (id: string) => `${restAPIv1}/agents/${id}/webhook/test`,
fetchWebhookTrace: (id: string) => `${restAPIv1}/agents/${id}/webhook/logs`,