Skip to content

MCP server: fix DM thread replies, DM/GM create_post validation, and silent attachment failures - #950

Open
chesterXalan wants to merge 2 commits into
mattermost:masterfrom
chesterXalan:fix/mcp-posting-fixes
Open

MCP server: fix DM thread replies, DM/GM create_post validation, and silent attachment failures#950
chesterXalan wants to merge 2 commits into
mattermost:masterfrom
chesterXalan:fix/mcp-posting-fixes

Conversation

@chesterXalan

@chesterXalan chesterXalan commented Aug 5, 2026

Copy link
Copy Markdown

Summary

Split from #888 to keep reviews small and focused. This part contains only the bug fixes for the MCP posting tools:

  • dm and group_message accept root_id to reply in existing threads. Reply targets are resolved to the actual thread root, so any post ID in the thread works (create_post too). Models frequently pass the ID of the latest post in a thread, which the server rejects since Mattermost only supports one level of threading.
  • create_post no longer dead-ends on DM/GM channels: the display-name context check now only applies to regular team channels. DM/GM channels have no display name and no team, so the check could never pass before.
  • Attachment upload failures now abort the post instead of silently posting the message without its files. Posts are also validated before uploading, so validation failures cannot leave orphaned uploads on the server.

Note: rebased on top of #932 after it landed. The abort-on-failure semantics now also cover the unified file-resolution path introduced there:

  • inline-file and legacy-attachment upload failures both abort the post;
  • remote-mode legacy attachments return an error instead of being silently dropped from the post (defense in depth — in real flows the access:"local" schema validation rejects them before the resolver runs);
  • the combined attachment cap is checked before any upload, so a cap violation can no longer orphan already-uploaded files.

Two cases in TestPostToolsCombinedAttachmentCap were updated to pin the stricter behavior.

QA test steps:

  1. Via an MCP client, call dm with a root_id pointing at the latest reply of an existing DM thread → the message lands in that thread instead of erroring.
  2. Call create_post with a DM channel ID (channel_display_name: "Direct Message", team_display_name: "N/A") → the post is created instead of failing context validation.
  3. Call dm with a nonexistent attachment path → the tool call errors and no post is created.

Ticket Link

None. Supersedes part of #888.

Release Note

Fixed the MCP server posting tools: dm and group_message can reply in threads via root_id, create_post works on DM/GM channels, and attachment upload failures abort the post instead of posting without files.

Summary by CodeRabbit

  • New Features
    • Added threaded reply support for direct messages and group conversations.
    • Replies now link automatically to the correct thread, and direct/group channel context is supported when creating posts.
  • Bug Fixes
    • Attachment handling is more strict: combined attachment limits are validated before uploads.
    • Remote-mode legacy/unreadable attachments now fail the post creation flow instead of being skipped.
    • Upload failures now stop post creation rather than being ignored.
  • Tests
    • Added coverage for thread-root resolution and attachment/post failure scenarios.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The posting tools now support threaded replies for direct, group, and regular channels. They validate reply targets and channel context. Local attachment failures now return errors before post creation.

Changes

Post creation flow

Layer / File(s) Summary
Attachment error propagation
mcpserver/tools/file_utils.go, mcpserver/tools/file_utils_test.go, mcpserver/tools/posts_test.go
Attachment limits are checked before uploads. Unsupported remote attachments and upload failures return errors. Empty attachments remain a successful no-op.
Thread and channel validation
mcpserver/tools/posts.go, mcpserver/tools/posts_test.go
Post arguments document threaded replies. Direct and group channels use their channel context without team lookups. resolveThreadRoot validates the post ID, channel, and thread root.
Post creation integration
mcpserver/tools/posts.go
Create-post, direct-message, group-message, and development-mode flows use normalized thread roots and propagate attachment errors before creating posts.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant PostTool
  participant ThreadResolver
  participant AttachmentUploader
  participant Mattermost
  PostTool->>ThreadResolver: Validate reply target and resolve thread root
  ThreadResolver->>Mattermost: Fetch referenced post
  Mattermost-->>ThreadResolver: Return post and root ID
  ThreadResolver-->>PostTool: Return normalized root ID
  PostTool->>AttachmentUploader: Upload local attachments
  AttachmentUploader-->>PostTool: Return file IDs or error
  PostTool->>Mattermost: Create post with root ID and file IDs
  Mattermost-->>PostTool: Return created post
Loading

Possibly related PRs

Suggested labels: Setup Cloud Test Server

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main changes: DM thread replies, create_post validation, and attachment failure handling.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@mcpserver/tools/file_utils.go`:
- Around line 294-300: Update the uploadFilesForLocal error path to delete all
fileIDs uploaded before the failure, using the existing file-deletion mechanism,
before returning either upload error. Add a two-attachment test that makes the
first upload succeed and the second fail, then verifies the first file ID is
deleted and the post creation is aborted.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 1372cad7-9b3c-41c5-872d-1e2606ad61e6

📥 Commits

Reviewing files that changed from the base of the PR and between f4e52d2 and 12f7522.

📒 Files selected for processing (4)
  • mcpserver/tools/file_utils.go
  • mcpserver/tools/file_utils_test.go
  • mcpserver/tools/posts.go
  • mcpserver/tools/posts_test.go

Comment thread mcpserver/tools/file_utils.go
…ment upload failures

- dm/group_message accept root_id to reply in existing threads; reply
  targets resolve to the actual thread root so any post ID in the
  thread works (create_post too)
- create_post no longer dead-ends on DM/GM channels: the display-name
  context check only applies to regular team channels (DM channels have
  no display name and no team, so validation could never pass)
- attachment upload failures now abort the post instead of silently
  posting the message without its files; posts are validated before
  uploading so failures cannot leave orphaned uploads
@chesterXalan
chesterXalan force-pushed the fix/mcp-posting-fixes branch from 12f7522 to 0cde2d6 Compare August 5, 2026 16:25
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@mcpserver/tools/file_utils.go`:
- Around line 377-384: The resolvePostFiles function currently uploads
attachments before validating the combined file cap. Move the
checkCombinedFileCap call ahead of uploadFilesAndUrlsForLocal, using the
attachment specification count and len(files), while preserving the existing
error returns; add or update a test to verify over-cap requests send no upload
request.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 50218037-8d1e-49fc-bc06-b9766079a232

📥 Commits

Reviewing files that changed from the base of the PR and between d3a8c78 and 0cde2d6.

📒 Files selected for processing (4)
  • mcpserver/tools/file_utils.go
  • mcpserver/tools/file_utils_test.go
  • mcpserver/tools/posts.go
  • mcpserver/tools/posts_test.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • mcpserver/tools/posts_test.go
  • mcpserver/tools/posts.go
  • mcpserver/tools/file_utils_test.go

Comment thread mcpserver/tools/file_utils.go Outdated
@chesterXalan

Copy link
Copy Markdown
Author

@crspeller When you have a moment, could you take a look at this one? It rebases on top of your #932 and extends its file-resolution path with abort-on-failure semantics (details in the description), so your eyes on that interaction would be especially valuable. CodeRabbit has approved and CI is green.

@mattermost-build

Copy link
Copy Markdown
Collaborator

This PR has been automatically labelled "stale" because it hasn't had recent activity.
A core team member will check in on the status of the PR to help with questions.
Thank you for your contribution!

@chesterXalan

Copy link
Copy Markdown
Author

Still active and green — awaiting review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants