Skip to content

Add add_attachment, the tool that produces an attachmentId - #242

Open
tsuda-haruni wants to merge 7 commits into
nulab:mainfrom
tsuda-haruni:add-attachment-upload
Open

tsuda-haruni wants to merge 7 commits into
nulab:mainfrom
tsuda-haruni:add-attachment-upload

Conversation

@tsuda-haruni

Copy link
Copy Markdown

Closes #241.

add_issue, update_issue and add_issue_comment all accept an attachmentId array, but no tool produced one: POST /api/v2/space/attachment was never exposed, so the parameter could be passed and never filled. This adds add_attachment, the write-side counterpart of get_issue_attachment (#221).

add_attachment({ filePath }) -> { id, name, size }

The id goes into the existing attachmentId parameters. backlog-js already had postSpaceAttachment(form), so the SDK is untouched.

Design notes

Three choices a reviewer may want to push back on:

  1. node:fs is imported at call time, not at module scope. src/lib.ts re-exports allTools and promises that nothing reachable from it touches a Node built-in on import (getIssueAttachment avoids Buffer for the same reason). A static import here would break that for every consumer, including ones that never call this tool. Deferring it moves the failure to the only place it means anything: a runtime without node:fs cannot serve a local path anyway.

  2. BACKLOG_ATTACHMENT_ROOTS bounds what the tool may read. This is the first tool that reads the host's filesystem, which makes it the first that can send a local file somewhere else. When the variable is set (path-delimiter separated), the file must resolve inside one of the listed directories; symlinks are resolved first so a link inside an allowed root cannot reach outside it. The containment test runs before the existence and regular-file checks, and everything outside the allowlist gets one error, so the messages cannot be used to map the filesystem beyond the allowed directories. Unset means no restriction — the only default that works out of the box for stdio/npx, where server and caller share a machine. Happy to flip to deny-by-default if you'd rather.

  3. openAsBlob rather than readFile. The Blob is backed by the file, so a large attachment is streamed into the request instead of being buffered once and then copied again into FormData.

Placed in the space toolset since the endpoint is space/attachment; easy to move if issue reads better.

Not included: a base64 content variant for container deployments where the client's filesystem is not visible to the server. It would work anywhere but puts the whole file through the model's context, which makes anything beyond a screenshot impractical. The README documents the bind-mount route instead.

Verification

npx vitest run     # 97 files / 573 tests
npx tsc --noEmit
npm run lint       # oxlint --type-aware --type-check
npm run build && node build/index.js --export-descriptions | grep ADD_ATTACHMENT

Also exercised against a real space: uploading a file through the built tool returned { id, name, size } as documented.

The 12 tool tests use a real temp directory rather than a mocked node:fs, since symlink resolution and containment are properties of the filesystem, not of the calls.

add_issue, update_issue and add_issue_comment all accept an attachmentId
array, but nothing in the server produced one: POST /api/v2/space/attachment
was never exposed, so the parameter could be passed and never filled. This is
the write-side counterpart of nulab#221, which added get_issue_attachment for the
same gap in the read direction.

The tool takes a server-side filePath and returns { id, name, size }.

Reading the host's filesystem is a capability the server did not previously
have, so BACKLOG_ATTACHMENT_ROOTS bounds it to a set of directories when an
operator sets it. Symlinks are resolved before the containment test, since a
link inside an allowed root would otherwise reach anything.

node:fs is imported at call time rather than at module scope: src/lib.ts
re-exports allTools and promises that nothing reachable from it touches a Node
built-in on import. openAsBlob backs the upload with the file instead of
buffering it, so a large attachment is streamed into the request.

Copilot AI 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.

🟡 Changes recommended

There are a couple of correctness/documentation issues in the new attachment upload path handling that should be addressed before merging.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds an add_attachment MCP tool that uploads a local file to Backlog via POST /api/v2/space/attachment and returns { id, name, size }, enabling existing attachmentId parameters in add_issue, update_issue, and add_issue_comment to be used as intended.

Changes:

  • Introduces add_attachment tool (space toolset) with filesystem-path validation and optional allowlist via BACKLOG_ATTACHMENT_ROOTS.
  • Adds pure string utilities (parseAttachmentRoots, isInsideRoot) and tests for root parsing/containment semantics.
  • Documents attachment uploads and the BACKLOG_ATTACHMENT_ROOTS environment variable in the README.
File summaries
File Description
src/utils/attachmentRoots.ts Adds allowlist parsing and containment helper used by attachment uploads.
src/utils/attachmentRoots.test.ts Unit tests for allowlist parsing and containment logic.
src/tools/addAttachment.ts Implements the add_attachment tool including root allowlist enforcement and upload wiring.
src/tools/addAttachment.test.ts Integration-style tests using a real temp directory, including symlink containment behavior.
src/tools/tools.ts Registers add_attachment in the space toolset.
README.md Documents the new tool and attachment upload configuration.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/tools/addAttachment.ts Resolved
Comment thread src/utils/attachmentRoots.ts Resolved
…curately

Two review points from nulab#242.

The multipart part was named after the real path, so a symlink named
`evidence.png` that points at `IMG_0522.PNG` was uploaded as `IMG_0522.PNG`.
The security checks still run against the real path — that is the file that
will be read — but the name now comes from the path as the caller wrote it,
which is the name the caller chose.

`isInsideRoot`'s comment said both inputs were real paths. They are not: when
`realpath()` fails the requested path is compared as `path.resolve`d, and the
allowlist keeps each root in its written form alongside its resolved one. The
comment now says what the function actually needs, which is absolute,
normalized paths, and why it does not normalize them itself.
@tsuda-haruni

tsuda-haruni commented Sep 8, 2026 •

Copy link
Copy Markdown
Author

Addressed both Copilot points in 3ba02dd.

  • The uploaded part is now named after the basename of the path the caller passed, while the checks still run against the real path. Added a regression test that a symlink is uploaded under the link's own name rather than the target's.
  • Reworded the isInsideRoot comment to match what it actually receives: absolute, normalized paths — real paths when the file exists, the path.resolved form otherwise.

On #251's lib.test.ts: add_attachment reaches node:fs / node:path only through call-time dynamic imports, so it stays outside that walk. Verified locally with main merged in — 603 tests pass.

nulab#258 moved the repository to Vite+, and its lint rule now rejects a bare
`vitest` import. The two test files added by this PR predate that change.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@tsuda-haruni

Copy link
Copy Markdown
Author

Checked against today's main: the branch merges cleanly and passes lint, typecheck, tests and build after the Vite+ migration (#258) — one import fix pushed in bc355fc.

If anything about the scope, toolset placement, or the allowlist default should change before review, happy to reshape it.

katayama8000 and others added 4 commits November 4, 2026 14:54
- Call backlog-js 1.x's postSpaceAttachment(client, form); tools receive a
  Client, which has no postSpaceAttachment method.
- Require BACKLOG_ATTACHMENT_ROOTS unless the transport is stdio, so remote
  HTTP callers cannot upload arbitrary server files.
- Annotate add_attachment as a write with openWorldHint, since it reads the
  server's filesystem, and list it in the annotation tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@katayama8000

Copy link
Copy Markdown
Collaborator

@tsuda-haruni
thank you for this. but let me think about this for a while.

@tsuda-haruni

Copy link
Copy Markdown
Author

Of course — take your time.

If it helps to narrow the decision, I'm happy to reshape it: stdio-only, a separate toolset that's off by default, or whatever boundary you'd prefer.

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.

No tool can upload an attachment, so attachmentId can never be obtained

3 participants