feat(server): add V2 create_threads to the T3 MCP #11795
Closedt3dotgg(GitHub profile, opens in a new tab)wants to merge 7 commits into
main fromfeat/mcp-start-threadopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/mcp/toolkits/threads/handlers.ts line 92OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +72,21 @@72+ Effect.orDie,73+ Effect.map((uuid) => CommandId.make(`server:${tag}:${threadId}:${uuid}`)),74+ );75+76+ return ThreadsToolkit.of({77+ start_thread: (input) =>78+ Effect.gen(function* () {79+ const scope = yield* McpInvocationContext.requireMcpCapability("threads");80+ const parent = yield* snapshots81+ .getThreadShellById(scope.threadId)82+ .pipe(Effect.catchCause(failed));83+ if (Option.isNone(parent)) {84+ return yield* new StartThreadCallerNotFoundError({ threadId: scope.threadId });85+ }86+87+ const threadId = yield* newThreadId(scope, input);88+ const existing =89+ input.clientRequestId === undefined90+ ? Option.none()91+ : yield* snapshots.getThreadShellById(threadId).pipe(Effect.catchCause(failed));92+ if (Option.isSome(existing)) {apps/server/src/mcp/toolkits/threads/handlers.ts line 108OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +88,21 @@88+ const existing =89+ input.clientRequestId === undefined90+ ? Option.none()91+ : yield* snapshots.getThreadShellById(threadId).pipe(Effect.catchCause(failed));92+ if (Option.isSome(existing)) {93+ return { threadId, title: existing.value.title, alreadyStarted: true };94+ }95+96+ const title = input.title ?? DEFAULT_THREAD_TITLE;97+ const createdAt = DateTime.formatIso(yield* DateTime.now);98+ yield* engine99+ .dispatch({100+ type: "thread.create",101+ commandId: yield* commandId("mcp-thread-create", threadId),102+ threadId,103+ title,104+ createdAt,105+ ...inheritedThreadSettings(parent.value, input),106+ })107+ .pipe(Effect.catchCause(failed));108+ yield* engineapps/server/src/mcp/toolkits/threads/handlers.ts line 92OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +72,21 @@72+ Effect.orDie,73+ Effect.map((uuid) => CommandId.make(`server:${tag}:${threadId}:${uuid}`)),74+ );75+76+ return ThreadsToolkit.of({77+ start_thread: (input) =>78+ Effect.gen(function* () {79+ const scope = yield* McpInvocationContext.requireMcpCapability("threads");80+ const parent = yield* snapshots81+ .getThreadShellById(scope.threadId)82+ .pipe(Effect.catchCause(failed));83+ if (Option.isNone(parent)) {84+ return yield* new StartThreadCallerNotFoundError({ threadId: scope.threadId });85+ }86+87+ const threadId = yield* newThreadId(scope, input);88+ const existing =89+ input.clientRequestId === undefined90+ ? Option.none()91+ : yield* snapshots.getThreadShellById(threadId).pipe(Effect.catchCause(failed));92+ if (Option.isSome(existing)) {
- apps/server/src/mcp/toolkits/threads/handlers.ts lines 92 to 93OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +73,21 @@73+ Effect.map((uuid) => CommandId.make(`server:${tag}:${threadId}:${uuid}`)),74+ );75+76+ return ThreadsToolkit.of({77+ start_thread: (input) =>78+ Effect.gen(function* () {79+ const scope = yield* McpInvocationContext.requireMcpCapability("threads");80+ const parent = yield* snapshots81+ .getThreadShellById(scope.threadId)82+ .pipe(Effect.catchCause(failed));83+ if (Option.isNone(parent)) {84+ return yield* new StartThreadCallerNotFoundError({ threadId: scope.threadId });85+ }86+87+ const threadId = yield* newThreadId(scope, input);88+ const existing =89+ input.clientRequestId === undefined90+ ? Option.none()91+ : yield* snapshots.getThreadShellById(threadId).pipe(Effect.catchCause(failed));92+ if (Option.isSome(existing)) {93+ return { threadId, title: existing.value.title, alreadyStarted: true };
- apps/server/src/mcp/toolkits/threads/handlers.ts line 133OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +113,21 @@113+ .pipe(Effect.catchCause(failed));114+ yield* engine115+ .dispatch({116+ type: "thread.turn.start",117+ commandId: turnCommandId,118+ threadId,119+ message: {120+ messageId: MessageId.make(`mcp-start:${threadId}`),121+ role: "user",122+ text: input.prompt,123+ attachments: [],124+ },125+ // The reactor generates a title from the first turn unless the126+ // agent named the thread. Seeding with the given title keeps it.127+ ...(input.title !== undefined ? { titleSeed: input.title } : {}),128+ runtimeMode: input.runtimeMode ?? parent.value.runtimeMode,129+ interactionMode: input.interactionMode ?? parent.value.interactionMode,130+ createdAt,131+ })132+ .pipe(Effect.catchCause(failed));133+ return { threadId, title };
t3dotgg(GitHub profile, opens in a new tab) changed the title
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/mcp/toolkits/orchestrator/handlers.ts lines 29 to 29OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +9,21 @@9+ OrchestratorMcpFailure,10+ type OrchestratorMcpInteractionMode,11+ type OrchestratorMcpRuntimeMode,12+ type OrchestratorMcpTarget,13+ ORCHESTRATOR_MCP_MAX_BATCH_THREADS,14+ type ProviderInteractionMode,15+ type ProviderOptionDescriptor,16+ type ProviderOptionSelection,17+ type RuntimeMode,18+ type ServerProvider,19+ ThreadId,20+} from "@t3tools/contracts";21+import * as Cause from "effect/Cause";22+import * as Crypto from "effect/Crypto";23+import * as DateTime from "effect/DateTime";24+import * as Effect from "effect/Effect";25+import * as Option from "effect/Option";26+27+import * as OrchestrationEngine from "../../../orchestration/Services/OrchestrationEngine.ts";28+import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";29+import { ProviderRegistry } from "../../../provider/Services/ProviderRegistry.ts";apps/server/src/mcp/toolkits/orchestrator/tools.ts lines 12 to 12OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +1,12 @@1+import {2+ OrchestratorMcpCapabilitiesResult,3+ OrchestratorMcpCreateThreadsInput,4+ OrchestratorMcpCreateThreadsResult,5+ OrchestratorMcpFailure,6+} from "@t3tools/contracts";7+import { Tool, Toolkit } from "effect/unstable/ai";8+9+import * as McpInvocationContext from "../../McpInvocationContext.ts";10+import * as OrchestrationEngine from "../../../orchestration/Services/OrchestrationEngine.ts";11+import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";12+import { ProviderRegistry } from "../../../provider/Services/ProviderRegistry.ts";apps/server/src/mcp/toolkits/orchestrator/handlers.ts lines 290 to 295OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +275,21 @@275+ case undefined:276+ return started ? "starting" : "idle";277+ }278+}279+280+const hex = (bytes: Uint8Array) =>281+ Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");282+283+const make = Effect.gen(function* () {284+ const engine = yield* OrchestrationEngine.OrchestrationEngineService;285+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;286+ const providerRegistry = yield* ProviderRegistry;287+ const crypto = yield* Crypto.Crypto;288+ const encoder = new TextEncoder();289+290+ const orchestrationError =291+ (context: string) =>292+ <E>(cause: Cause.Cause<E>): Effect.Effect<never, OrchestratorMcpFailure> =>293+ Cause.hasInterruptsOnly(cause)294+ ? Effect.failCause(cause as Cause.Cause<never>)295+ : Effect.fail(failure("orchestration_error", `${context}: ${errorMessage(cause)}`));
- apps/server/src/mcp/toolkits/orchestrator/handlers.ts lines 36 to 36Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +16,21 @@16+ type ProviderOptionSelection,17+ type RuntimeMode,18+ type ServerProvider,19+ ThreadId,20+} from "@t3tools/contracts";21+import * as Cause from "effect/Cause";22+import * as Crypto from "effect/Crypto";23+import * as DateTime from "effect/DateTime";24+import * as Effect from "effect/Effect";25+import * as Option from "effect/Option";26+27+import * as OrchestrationEngine from "../../../orchestration/Services/OrchestrationEngine.ts";28+import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";29+import * as ProviderRegistry from "../../../provider/Services/ProviderRegistry.ts";30+import * as McpInvocationContext from "../../McpInvocationContext.ts";31+import { OrchestratorToolkit } from "./tools.ts";32+33+// Ports of the matching Orchestrator V2 helpers (#2829,34+// apps/server/src/mcp/OrchestratorMcpService.ts). Keep them in step until35+// V2 replaces this file.36+apps/server/src/mcp/toolkits/orchestrator/handlers.ts lines 293 to 295OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +275,21 @@275+ case undefined:276+ return started ? "starting" : "idle";277+ }278+}279+280+const hex = (bytes: Uint8Array) =>281+ Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");282+283+const make = Effect.gen(function* () {284+ const engine = yield* OrchestrationEngine.OrchestrationEngineService;285+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;286+ const providerRegistry = yield* ProviderRegistry.ProviderRegistry;287+ const crypto = yield* Crypto.Crypto;288+ const encoder = new TextEncoder();289+290+ // A returned failure is a normal tool result, so McpServer never logs it.291+ // Log the cause here. The agent gets the message, the same text V2 returns.292+ const orchestrationError =293+ (context: string) =>294+ <E>(cause: Cause.Cause<E>): Effect.Effect<never, OrchestratorMcpFailure> =>295+ Cause.hasInterruptsOnly(cause)
- apps/server/src/mcp/toolkits/orchestrator/handlers.ts lines 311 to 314Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -0,0 +294,21 @@294+const hex = (bytes: Uint8Array) =>295+ Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");296+297+const make = Effect.gen(function* () {298+ const engine = yield* OrchestrationEngine.OrchestrationEngineService;299+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;300+ const providerRegistry = yield* ProviderRegistry.ProviderRegistry;301+ const crypto = yield* Crypto.Crypto;302+ const encoder = new TextEncoder();303+304+ // A returned failure is a normal tool result, so McpServer never logs it.305+ // Log the error here, and give the agent a message built from its tag.306+ const orchestrationError =307+ (context: string) =>308+ (error: OrchestrationDispatchError): Effect.Effect<never, OrchestratorMcpFailure> =>309+ Effect.logWarning(`create_threads: ${context}`, error).pipe(310+ Effect.andThen(311+ Effect.fail(312+ new OrchestratorMcpFailure({313+ code: "orchestration_error",314+ message: `${context}: ${orchestrationErrorReason(error)}`,
- @@ -0,0 +298,21 @@298+ const engine = yield* OrchestrationEngine.OrchestrationEngineService;299+ const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;300+ const providerRegistry = yield* ProviderRegistry.ProviderRegistry;301+ const crypto = yield* Crypto.Crypto;302+ const encoder = new TextEncoder();303+304+ // A returned failure is a normal tool result, so McpServer never logs it.305+ // Log the error here, and give the agent a message built from its tag.306+ const orchestrationError =307+ (context: string) =>308+ (error: OrchestrationDispatchError): Effect.Effect<never, OrchestratorMcpFailure> =>309+ Effect.logWarning(`create_threads: ${context}`, error).pipe(310+ Effect.andThen(311+ Effect.fail(312+ new OrchestratorMcpFailure({313+ code: "orchestration_error",314+ message: `${context}: ${orchestrationErrorReason(error)}`,315+ }),316+ ),317+ ),318+ );
juliusmarminge added labelsmacroscope-review
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed