-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(mcp): expose attachment upload and send #10564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
juliusmarminge
merged 6 commits into
agents/mcp-thin/projects
from
agents/mcp-thin/attachments
Sep 7, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc42153
feat(mcp): expose existing attachment upload and send services
juliusmarminge 3fe65d5
fix(mcp): describe the existing attachment upload method
juliusmarminge 7891d7a
fix(mcp): retain actionable attachment claim messages
juliusmarminge 3701a41
fix(mcp): reuse stored attachment metadata
juliusmarminge 62a515b
fix(mcp): reject archived attachment targets before claiming
juliusmarminge 71fa7c9
refactor(mcp): use shared attachment send intake
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { type ChatAttachment, MessageId, OrchestratorMcpFailure } from "@t3tools/contracts"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Upload from "../../../assets/AttachmentUpload.ts"; | ||
| import * as Claims from "../../../orchestration-v2/AttachmentClaims.ts"; | ||
| import * as ThreadMessageIntake from "../../../orchestration-v2/ThreadMessageIntake.ts"; | ||
| import { | ||
| newCommandId, | ||
| readMutationCaller, | ||
| readWritableThread, | ||
| unavailable, | ||
| } from "../../threadAccess.ts"; | ||
| import { AttachmentToolkit } from "./tools.ts"; | ||
|
|
||
| export function resolveAttachmentReferences( | ||
| requested: ReadonlyArray<ChatAttachment>, | ||
| stored: ReadonlyArray<ChatAttachment>, | ||
| ) { | ||
| const owned = new Map(stored.map((attachment) => [attachment.id, attachment])); | ||
| return Effect.forEach(requested, (attachment) => { | ||
| const canonical = Claims.attachmentIsPendingUpload(attachment) | ||
| ? attachment | ||
| : owned.get(attachment.id); | ||
| return canonical === undefined | ||
| ? Effect.fail( | ||
| new OrchestratorMcpFailure({ | ||
| code: "invalid_request", | ||
| message: "Attachments must be pending uploads or already belong to the target thread.", | ||
| }), | ||
| ) | ||
| : Effect.succeed(canonical); | ||
| }); | ||
| } | ||
|
|
||
| export const AttachmentHandlersLive = AttachmentToolkit.toLayer({ | ||
| t3_attachment_prepare_upload: (input) => | ||
| Effect.gen(function* () { | ||
| yield* readMutationCaller(); | ||
| return yield* Upload.issueAttachmentUploadUrl(input.upload).pipe( | ||
| Effect.mapError(unavailable), | ||
| ); | ||
| }), | ||
| t3_attachment_discard: (input) => | ||
| Effect.gen(function* () { | ||
| yield* readMutationCaller(); | ||
| yield* Upload.deletePendingAttachment(input.attachmentId); | ||
| return {}; | ||
| }), | ||
| t3_thread_send_attachments: (input) => | ||
| Effect.gen(function* () { | ||
| const { caller, projection } = yield* readWritableThread(input.threadId); | ||
| if (projection.thread.archivedAt !== null) | ||
| return yield* new OrchestratorMcpFailure({ | ||
| code: "invalid_request", | ||
| message: "Unarchive the target thread before sending attachments.", | ||
| }); | ||
| const attachments = yield* resolveAttachmentReferences( | ||
| input.attachments, | ||
| projection.messages.flatMap((message) => message.attachments), | ||
| ); | ||
| const commandId = yield* newCommandId(); | ||
| const messageId = MessageId.make(commandId); | ||
| const result = yield* ThreadMessageIntake.sendToThread({ | ||
| projectId: caller.projectId, | ||
| threadId: projection.thread.id, | ||
| commandId, | ||
| messageId, | ||
| text: input.message ?? "", | ||
| attachments, | ||
| mode: "auto", | ||
| createdBy: "agent", | ||
| creationSource: "mcp", | ||
| }).pipe( | ||
| Effect.mapError((error) => | ||
| error._tag === "AttachmentClaimError" | ||
| ? new OrchestratorMcpFailure({ code: "orchestration_error", message: error.message }) | ||
| : unavailable(), | ||
| ), | ||
| ); | ||
| return { | ||
| threadId: projection.thread.id, | ||
| messageId, | ||
| runId: result.run.id, | ||
| status: result.run.status, | ||
| }; | ||
| }), | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { | ||
| AttachmentCreateUploadUrlInput, | ||
| AttachmentCreateUploadUrlResult, | ||
| AttachmentDeleteInput, | ||
| ChatImageAttachment, | ||
| ChatFileAttachment, | ||
| MessageId, | ||
| RunId, | ||
| ThreadId, | ||
| OrchestrationV2RunStatus, | ||
| OrchestratorMcpFailure, | ||
| } from "@t3tools/contracts"; | ||
| import * as Crypto from "effect/Crypto"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Schema from "effect/Schema"; | ||
| import { Tool, Toolkit } from "effect/unstable/ai"; | ||
| import { ServerSecretStore } from "../../../auth/ServerSecretStore.ts"; | ||
| import { ServerConfig } from "../../../config.ts"; | ||
| import { ThreadManagementService } from "../../../orchestration-v2/ThreadManagementService.ts"; | ||
| import { McpInvocationContext } from "../../McpInvocationContext.ts"; | ||
|
|
||
| const shared = { | ||
| failure: OrchestratorMcpFailure, | ||
| failureMode: "return" as const, | ||
| dependencies: [ | ||
| McpInvocationContext, | ||
| ThreadManagementService, | ||
| ServerConfig, | ||
| ServerSecretStore, | ||
| FileSystem.FileSystem, | ||
| Crypto.Crypto, | ||
| ], | ||
| }; | ||
| export const AttachmentUploadTool = Tool.make("t3_attachment_prepare_upload", { | ||
| ...shared, | ||
| description: | ||
| "Create the app's signed upload URL. POST the exact bytes to relativeUrl on this MCP server's HTTP origin, then pass the attachment metadata and returned ID to t3_thread_send_attachments. Upload is separate from sending; provider attachment support is decided by its adapter.", | ||
| parameters: Schema.Struct({ upload: AttachmentCreateUploadUrlInput }), | ||
| success: AttachmentCreateUploadUrlResult, | ||
| }).annotate(Tool.Destructive, true); | ||
| export const AttachmentDiscardTool = Tool.make("t3_attachment_discard", { | ||
| ...shared, | ||
| description: | ||
| "Discard a pending upload. Already-delivered thread attachments are never deleted by this operation.", | ||
| parameters: AttachmentDeleteInput, | ||
| success: Schema.Struct({}), | ||
| }).annotate(Tool.Destructive, true); | ||
| export const AttachmentSendTool = Tool.make("t3_thread_send_attachments", { | ||
| ...shared, | ||
| description: | ||
| "Send uploaded attachments to this thread or another thread in the calling project. Each call is a new message, without a retry key. Acceptance does not mean the provider can consume the attachment or has finished the turn. The target cannot have broader permission modes than the caller; failures retain claimed files when dispatch outcome is uncertain.", | ||
| parameters: Schema.Struct({ | ||
| threadId: Schema.optional(ThreadId), | ||
| message: Schema.optional(Schema.String.check(Schema.isMaxLength(120000))), | ||
| attachments: Schema.Array(Schema.Union([ChatImageAttachment, ChatFileAttachment])).check( | ||
| Schema.isMinLength(1), | ||
| Schema.isMaxLength(8), | ||
| ), | ||
| }), | ||
| success: Schema.Struct({ | ||
| threadId: ThreadId, | ||
| messageId: MessageId, | ||
| runId: RunId, | ||
| status: OrchestrationV2RunStatus, | ||
| }), | ||
| }) | ||
| .annotate(Tool.Destructive, true) | ||
| .annotate(Tool.OpenWorld, true); | ||
| export const AttachmentToolkit = Toolkit.make( | ||
| AttachmentUploadTool, | ||
| AttachmentDiscardTool, | ||
| AttachmentSendTool, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.