Skip to content

Upload document images from the MCP server - #848

Merged
pikann merged 3 commits into
Paca-AI:masterfrom
sebathi:feat/mcp-doc-images
Dec 2, 2026
Merged

pikann merged 3 commits into
Paca-AI:masterfrom
sebathi:feat/mcp-doc-images

Conversation

@sebathi

@sebathi sebathi commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The MCP server cannot put an image in a document. push of a Markdown file that shows ![P1](png/P1.png) drops the line (a relative link has no meaning in Paca), and no tool calls the doc file routes the web editor uploads through, so a folder of wireframes or screenshots reaches Paca as text only. The only way to add the images was to drag them into the editor one by one.

Change

Only apps/mcp. The API and the web app are untouched: this uses the existing doc file routes (initiate-upload, complete-upload, download-url, DELETE) and the docfile://<project>/<document>/<file> URL the web editor already writes in image blocks.

push / push_docs upload the local images a Markdown file shows on a line of their own and put their docfile:// URIs in the document.

  • The Markdown file is never rewritten; it keeps png/P1.png.
  • .paca/push.json records each image's sha256 and file id under a new optional images key. The same bytes are not uploaded twice, an image whose bytes changed is uploaded again, and a second push of an unchanged file still prints unchanged.
  • A file can only belong to a document that exists, so a new document is written first with a line of text in place of each image, gets its images, and is written again with If-Match.
  • A missing or empty image, or one outside the allowed folders (working directory, system temp folder, PACA_MCP_FILE_ROOTS), stops the push before anything is sent, naming the image and its line.
  • --dry-run lists what would be uploaded and what is already in Paca.
  • An image an earlier push uploaded and the file no longer shows stays in Paca and the push says so; --prune-images (pruneImages) deletes those.

pull --images / pull_docs with images: true downloads the images of the documents it writes to .paca-assets/<document id>/<file name> next to each mirror file and names them by that path in the Markdown. .paca-sync.json records path → file id and sha256 under a new optional assets key, so pushing the mirror file back maps the path to the same file and uploads nothing. A mirror file can also show a new local image, which push uploads. An image the document no longer shows leaves the mirror on the pull that brings that change, unless its bytes were changed locally or the mirror file still names it (a file pull kept or left conflicted). Without the option the output is byte for byte what it was.

New tools

  • upload_doc_file — a local file to a document by docId or path; returns the URI and a ready Markdown line, and insertMarkdown: true appends it to the document.
  • read_doc_file — an image (up to 5 MB) back to the agent as image content, by uri or docId + fileId. A larger file is refused from its Content-Length, or as soon as the limit is passed, without being read into memory.
  • delete_doc_file.

The initiate / PUT (or multipart) / complete flow moved out of views-client.ts into api/upload.ts and is shared by task attachments and doc files; uploadTaskAttachment behaves as before. assertAllowedPath moved to utils/allowed-paths.ts so the CLI and the tools apply the same rule.

Presigned URLs are never printed or put in error messages.

Choices worth a look

  • Assets live in .paca-assets/<document id>/, not in a folder named after the document. A renamed document keeps its Markdown unchanged, and a moved one takes its images along.
  • No size limit. InitiateDocUpload sets none, so push sets none either. Each image is read into memory to be hashed and sent.
  • No list_doc_files. The API has no route that lists a document's files.
  • Existing state files keep working. Both new keys are optional and are only written when a document has images.

Tests

bun run lint, bun run build and bun run test pass in apps/mcp (1395 tests; lint reports the 4 warnings that are on master).

New: push-images.test.ts, mirror-images.test.ts, doc-client-files.test.ts, doc-files.test.ts, doc-file-tools.test.ts, with additions to dispatch.test.ts and doc-mirror-tools.test.ts. They cover the single PUT and multipart upload, reuse by sha256, the two writes of a new document, a document deleted between the write and the upload (a conflict, not a crash), dry run, pruning (also on an otherwise unchanged file), missing / empty / out-of-roots images, a path with a space, pull with and without --images, a document moved to another folder, an image removed from the document in Paca, the read limit, and that no output contains a presigned URL.

Also run against the stack from deploy/docker-compose.e2e.yml:

  • push of a Markdown file with three PNGs (one in a folder with a space): created, images: 3 uploaded. The second push prints unchanged. The stored document has three image blocks with docfile:// URLs, each file downloads with the sha256 recorded in .paca/push.json, and the images render in the web editor.
  • After changing one PNG's bytes: 1 uploaded, 2 already in Paca. After removing an image line: reported as left in Paca, then deleted by --prune-images.
  • pull --images, then a push from a mirror file with a new local image and a later text-only edit: one upload, then none.
  • upload_doc_file with insertMarkdown, read_doc_file, and a refused path outside the allowed folders, through the MCP server over stdio.

Not covered

  • Only an image on a line of its own is uploaded. An image inside a sentence is dropped by the Markdown conversion, as it is today, and reference-style images (![alt][ref]) are not read.
  • If the second write of a push fails (a conflict, say), the files already uploaded stay attached to the document without being shown; the next push uploads them again because the state was not saved.
  • Replacing the bytes of an image in a pull mirror without changing its Markdown line is not detected.
  • pull --images localizes the documents that pull writes; a document unchanged since the last pull keeps its docfile:// URIs until it changes.
  • Two image blocks in a row are written by read_doc and pull on a single line (![a](…)![b](…)). That is the converter's existing output and reads back as two image blocks, so I left it alone.

push and push_docs upload the local images a Markdown file shows on a
line of their own and put their docfile:// URIs in the document. The
Markdown file is never rewritten. .paca/push.json records the sha256 and
file id of each image, so unchanged bytes are not uploaded again and a
second push of an unchanged file still prints "unchanged". A new
document is written first, gets its images, and is written again with
If-Match.

pull --images (pull_docs with images: true) downloads the images of the
documents it writes to .paca-assets/<document id>/ and names them by
that path, so a pulled file can be pushed back without re-uploading.

New tools: upload_doc_file, read_doc_file and delete_doc_file. The
initiate / PUT / complete upload flow moves to api/upload.ts and is
shared with task attachments.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Minor suggestions only — nothing blocking; three small points inline.

Approach: sound — it reuses the existing doc-file routes and the docfile:// URI the web editor already writes, and keeps sync idempotent with sha256 values recorded in the state files the CLI already owns, rather than adding a new mechanism or touching the API.

Reviewed changes

  • api/upload.ts extraction — the initiate/PUT/complete flow moved out of views-client.ts verbatim and is now shared by task attachments (uploadTaskAttachment behaves as before) and document files, including the multipart 5 MiB path.
  • push / push_docs image upload — local images on a line of their own are uploaded and replaced by docfile:// URIs only in what is sent; the Markdown file is untouched. Reuse is keyed by repo-relative path + sha256 in .paca/push.json, a first push writes placeholders then the content with If-Match, and missing/empty/out-of-root images abort before any request. --prune-images / pruneImages deletes the files the file no longer shows.
  • pull --images / images: true — names each document image .paca-assets/<docId>/<file name>, localizes the Markdown, renames the folder with a moved document, removes it when the document is gone, records assets so a mirror push maps the path back to the same file id, and reports failed downloads for the next run.
  • New tools — upload_doc_file (by docId or path, optional insertMarkdown append guarded by If-Match), read_doc_file (images only, 5 MB) and delete_doc_file, all gated by the shared assertAllowedPath.
  • Path/type helper moves — assertAllowedPath/realish to utils/allowed-paths.ts so the CLI and the tools apply one rule, and guessContentType to utils/doc-files.ts for reuse by the new tools.
  • Docs and tests — README/ALL_TOOLS/CHANGELOG updates plus five new test files covering single/multipart upload, reuse, dry run, pruning, document-deleted conflicts, path/missing-file errors, pull --images round trips, a moved document and that no output leaks a presigned URL.

I verified the docfile://{project}/{doc}/{file} shape against the web editor's resolveFileUrl, the upload contract against the Go handlers (InitiateDocUpload sets no size limit; the multipart threshold and DefaultPartSize are 5 MiB, matching putMultipart), and ran the package suite: 61 files / 1385 tests pass, with lint and typecheck clean for the touched files.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Comment thread apps/mcp/README.md Resolved
Comment thread apps/mcp/src/cli/pull.ts Resolved
Comment thread apps/mcp/src/tools/doc-file-tools.ts Resolved
pull kept the .paca-sync.json entry and the file under .paca-assets of
an image that had left the document, so a later pull could download it
again. A fetched document now keeps only the images it still shows, and
the file is removed unless its bytes were changed locally.

read_doc_file checked its 5 MB limit after the whole file was in memory.
downloadDocFile takes a byte limit, refuses a larger Content-Length
before reading, and stops reading once the limit is passed.

The README overview counts 84 tools.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

The new stale-image pruning can delete a file that a kept mirror file still references. Details inline.

Reviewed changes

The second commit addresses the three prior notes and adds the pruning and read-cap behavior:

  • Capped read_doc_file reads — downloadDocFile now takes maxBytes; readUpTo refuses a file from its declared Content-Length or as soon as the streamed total passes the limit, cancelling the body instead of buffering it, and the tool turns FileTooLargeError into a text error.
  • Pruned stale mirror images — shownAssets filters a fetched document's assets to the file ids its content still shows; syncAssetFiles then deletes the matching files under .paca-assets/<docId>/ (keeping ones whose bytes were edited locally), drops empty maps and prunes empty folders.
  • README and tests — the headline tool count is corrected to 84, the asset-removal behavior is documented, and tests cover the size cap and removed-image pruning.

The full apps/mcp suite passes (61 files / 1392 tests); tsc reports only the pre-existing member-tools.ts errors.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Comment thread apps/mcp/src/cli/pull.ts Resolved
The stale-image cleanup ran before pull decided whether to rewrite the
mirror file, so a file it kept or left conflicted could lose an image it
still referenced, entry and bytes. The cleanup now runs once the files
are in place and lets go of an image only when the document no longer
shows it and the mirror file no longer names it.

Naming new images against the full previous set also stops a new image
from taking the path of a removed one whose local file was edited.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Approach: sound — the fix moves stale-image pruning to after the mirror files are in place, so it can tell an image a kept file still references from one the document genuinely dropped, and it seeds new-image naming with the full previous set so a same-named image no longer takes a locally edited file's path.

Reviewed changes

The third commit (4105ab2) addresses the pruning regression from the prior review; I read the changed pull.ts paths and ran the package suite.

  • Prune moved after the write — stale-image cleanup now runs once next[id].file is on disk, keeping an asset when either the fetched content still shows it or the mirror file still names its path.
  • Same-name images keep their suffix — nameNewAssets is seeded with the full previous assets map, so a new image sharing a removed image's file name takes the <fileId8>- path instead of overwriting a locally edited file.
  • Regression tests — merged, conflicted, and same-name cases added to mirror-images.test.ts; README updated to match.

The full apps/mcp suite passes (61 files / 1395 tests), and the changed files are lint- and typecheck-clean.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pikann pikann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you for the contribution! 🚀

@pikann
pikann merged commit 9a8d913 into Paca-AI:master Dec 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants