diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 092d9f324106..95e269c64c97 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -240,13 +240,13 @@ export class OrchestratorMcpService extends Context.Service< OrchestratorMcpServiceShape >()("t3/mcp/OrchestratorMcpService") {} -const isThreadManagementError = Schema.is(ThreadManagementService.ThreadManagementError); +export const isThreadManagementError = Schema.is(ThreadManagementService.ThreadManagementError); function failure(code: OrchestratorMcpFailure["code"], message: string): OrchestratorMcpFailure { return new OrchestratorMcpFailure({ code, message }); } -function threadManagementFailure(error: unknown): OrchestratorMcpFailure { +export function threadManagementFailure(error: unknown): OrchestratorMcpFailure { if (!isThreadManagementError(error)) return failure("orchestration_error", errorMessage(error)); switch (error._tag) { case "ThreadManagementThreadNotFoundError": diff --git a/apps/server/src/mcp/toolkits/attachment/handlers.ts b/apps/server/src/mcp/toolkits/attachment/handlers.ts index 032ab3a1d0ed..25d0ac7fad15 100644 --- a/apps/server/src/mcp/toolkits/attachment/handlers.ts +++ b/apps/server/src/mcp/toolkits/attachment/handlers.ts @@ -12,7 +12,8 @@ import * as Upload from "../../../assets/AttachmentUpload.ts"; import * as Claims from "../../../orchestration-v2/AttachmentClaims.ts"; import * as ThreadMessageIntake from "../../../orchestration-v2/ThreadMessageIntake.ts"; import * as McpToolAccess from "../../McpToolAccess.ts"; -import { newCommandId, readThread, unavailable } from "../../threadAccess.ts"; +import { isThreadManagementError, threadManagementFailure } from "../../OrchestratorMcpService.ts"; +import { dispatchFailure, newCommandId, readThread, unavailable } from "../../threadAccess.ts"; import { AttachmentToolkit } from "./tools.ts"; export function resolveAttachmentReferences( @@ -118,7 +119,9 @@ export const layer = McpToolAccess.toLayer( code: "orchestration_error", message: error.message, }) - : unavailable(), + : isThreadManagementError(error) + ? threadManagementFailure(error) + : dispatchFailure(error), ), ); return { diff --git a/apps/server/src/mcp/toolkits/core.test.ts b/apps/server/src/mcp/toolkits/core.test.ts index a520e74fd836..af2501b295c5 100644 --- a/apps/server/src/mcp/toolkits/core.test.ts +++ b/apps/server/src/mcp/toolkits/core.test.ts @@ -1420,6 +1420,100 @@ it.effect("only the caller that prepared a pending upload can discard it", () => ), ); +const sendTargetId = ThreadId.make("send-target-thread"); +const sendCommand = { + commandId: CommandId.make("mcp-core-send"), + commandType: "message.dispatch", +}; + +it.effect.each([ + { + outcome: "says that the missing workspace could not be recreated", + refusal: new ThreadManagement.ThreadManagementWorktreeRestoreError({ + threadId: sendTargetId, + cause: new Error("The branch no longer exists."), + }), + reply: { + _tag: "OrchestratorMcpFailure", + code: "thread_not_sendable", + message: + "Couldn't recreate the missing workspace of thread send-target-thread, so the message was not sent. The branch no longer exists.", + }, + }, + { + outcome: "passes on the text reason of an orchestrator rejection", + refusal: new OrchestratorCommandRejectedError({ + ...sendCommand, + cause: "Thread send-target-thread is deleted.", + }), + reply: { + _tag: "OrchestratorMcpFailure", + code: "orchestration_error", + message: "Thread send-target-thread is deleted.", + }, + }, + { + outcome: "keeps an internal dispatch cause private", + refusal: new OrchestratorDispatchError({ + ...sendCommand, + cause: new Error("private-storage-path"), + }), + reply: { + _tag: "OrchestratorMcpFailure", + code: "orchestration_error", + message: "The operation could not be completed.", + }, + }, +])("t3_thread_send_attachments $outcome", ({ refusal, reply }) => { + const stored = ChatImageAttachment.make({ + type: "image", + id: "owned-image", + name: "original.png", + mimeType: "image/png", + sizeBytes: 12, + }); + return Effect.gen(function* () { + const server = yield* McpServer.McpServer; + const result = yield* server + .callTool({ + name: "t3_thread_send_attachments", + arguments: { threadId: sendTargetId, message: "See the file.", attachments: [stored] }, + }) + .pipe( + Effect.provideService(McpInvocationContext.McpInvocationContext, scope), + Effect.provideService(McpSchema.McpServerClient, client), + ); + expect(declaredFailure(result)).toEqual(reply); + }).pipe( + Effect.provide( + McpHttpServer.layerAttachmentToolkit.pipe( + Layer.provideMerge(McpHttpServer.layerInProcessMcpServer), + Layer.provide( + Layer.mock(ThreadManagement.ThreadManagementService)({ + getThreadShell: (id) => Effect.succeed(McpToolAccessTestkit.liveThreadShell(id)), + getProjectThreadRecords: () => + Effect.succeed({ + ...McpToolAccessTestkit.idleThreadProjection( + McpToolAccessTestkit.liveThreadShell(sendTargetId), + ), + messages: [{ attachments: [stored] }], + } as never), + sendToThread: () => Effect.fail(refusal), + }), + ), + Layer.provide(AssetSigner.layer.pipe(Layer.provideMerge(ServerSecretStore.layer))), + Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-mcp-attachment-" })), + Layer.provide( + DocumentText.layer.pipe( + Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), + ), + ), + Layer.provide(NodeCrypto.layer), + ), + ), + ); +}); + it.effect( "t3_thread_list settled:false with an empty statuses list lists the unsettled threads", () => {