feat(server): agents list and create threads via the t3-code MCP toolkit #11864
Closedsaphid(GitHub profile, opens in a new tab)wants to merge 12 commits into
main fromrefs/pull/11864/headopened 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:XL
- apps/web/src/components/ChatView.tsx line 2891Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2863,3 +2865,12 @@28632865 [threadActivities],28642866 );28652867 const workLogEntries = useMemo(() => deriveWorkLogEntries(threadActivities), [threadActivities]);2868+ const agentCreatedThreads = useMemo(2869+ () => deriveAgentCreatedThreads(workLogEntries),2870+ [workLogEntries],2871+ );2872+ useEffect(() => {2873+ if (agentCreatedThreads.length === 0) {2874+ return;2875+ }2876+ notifyAgentCreatedThreads({
- apps/server/src/mcp/toolkits/threads/tools.ts line 27OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +7,21 @@7+} from "@t3tools/contracts";8+import * as Crypto from "effect/Crypto";9+import { Tool, Toolkit } from "effect/unstable/ai";10+11+import * as McpInvocationContext from "../../McpInvocationContext.ts";12+import * as OrchestrationEngine from "../../../orchestration/Services/OrchestrationEngine.ts";13+import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";14+15+const listDependencies = [16+ McpInvocationContext.McpInvocationContext,17+ ProjectionSnapshotQuery.ProjectionSnapshotQuery,18+];19+20+const createDependencies = [21+ McpInvocationContext.McpInvocationContext,22+ ProjectionSnapshotQuery.ProjectionSnapshotQuery,23+ OrchestrationEngine.OrchestrationEngineService,24+ Crypto.Crypto,25+];26+27+export const ThreadsListTool = Tool.make("threads_list", {apps/web/src/agentCreatedThreadToast.ts lines 40 to 42OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +22,21 @@22+ }23+ recentAgentThreadIds.add(thread.threadId);24+ toastManager.add(25+ stackedThreadToast({26+ type: "success",27+ title: "New thread created",28+ description: thread.title,29+ actionProps: {30+ children: "Open",31+ onClick: () => {32+ input.navigate(scopeThreadRef(input.environmentId, thread.threadId));33+ },34+ },35+ }),36+ );37+ }38+}39+40+export function resetAgentCreatedThreadToastsForTests(): void {41+ recentAgentThreadIds.clear();42+}apps/web/src/components/ChatView.tsx lines 2868 to 2886OutdatedResolvedby saphid(GitHub profile, opens in a new tab)@@ -2864,2 +2866,21 @@28642866 );28652867 const workLogEntries = useMemo(() => deriveWorkLogEntries(threadActivities), [threadActivities]);2868+ const agentCreatedThreads = useMemo(2869+ () => deriveAgentCreatedThreads(workLogEntries),2870+ [workLogEntries],2871+ );2872+ useEffect(() => {2873+ if (agentCreatedThreads.length === 0) {2874+ return;2875+ }2876+ notifyAgentCreatedThreads({2877+ environmentId,2878+ threads: agentCreatedThreads,2879+ navigate: (threadRef) => {2880+ void navigate({2881+ to: "/$environmentId/$threadId",2882+ params: buildThreadRouteParams(threadRef),2883+ });2884+ },2885+ });2886+ }, [agentCreatedThreads, environmentId, navigate]);
- apps/server/src/mcp/toolkits/threads/handlers.ts lines 23 to 26OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +6,21 @@6+ type ThreadsCreateResult,7+ type ThreadsListInput,8+ type ThreadsListResult,9+ type ThreadsSurfaceError,10+ type ThreadsListItem,11+ THREADS_SURFACE_LIST_DEFAULT_LIMIT,12+ THREADS_SURFACE_LIST_MAX_LIMIT,13+} from "@t3tools/contracts";14+import * as DateTime from "effect/DateTime";15+import * as Crypto from "effect/Crypto";16+import * as Effect from "effect/Effect";17+18+import * as McpInvocationContext from "../../McpInvocationContext.ts";19+import * as OrchestrationEngine from "../../../orchestration/Services/OrchestrationEngine.ts";20+import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";21+import { ThreadsToolkit } from "./tools.ts";22+23+const fail = (detail: string) =>24+ Effect.fail<ThreadsSurfaceError>({ _tag: "ThreadsSurfaceError", detail });25+26+const failFrom = (error: { readonly message: string }) => fail(error.message);packages/contracts/src/threadsSurface.ts lines 11 to 11OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,11 @@1+/**2+ * ThreadsSurface - Schemas for the agent-facing threads toolkit.3+ *4+ * Tools in this surface let a running agent list and create threads in the5+ * current environment. The payloads are deliberately small: thread items carry6+ * ids and titles only, never message content, so thread data stays out of the7+ * model. Clients resolve rendering (and navigation) locally from these ids.8+ *9+ * @module ThreadsSurface10+ */11+import { Effect, Schema } from "effect";
- packages/contracts/src/threadsSurface.ts line 30Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +10,21 @@10+ */11+import { Effect, Schema } from "effect";12+import {13+ IsoDateTime,14+ PositiveInt,15+ ProjectId,16+ ThreadId,17+ TrimmedNonEmptyString,18+} from "./baseSchemas.ts";19+20+export const THREADS_SURFACE_TOOL_NAMES = ["threads_list", "threads_create"] as const;21+export type ThreadsSurfaceToolName = (typeof THREADS_SURFACE_TOOL_NAMES)[number];22+23+/**24+ * Adapters surface toolkit tools under different names: Codex keeps the bare25+ * tool name (`item.tool`), while Claude/OpenCode wrap it as26+ * `mcp__<server>__<tool>`. Matches both forms for the `t3-code` server only,27+ * using the same server-name aliases as the preview tool matcher.28+ */29+const THREADS_SURFACE_QUALIFIED_NAME =30+ /^(?:(?:mcp__)?t3[-_]?code_{1,2})?(threads_list|threads_create)$/;
- apps/server/src/mcp/toolkits/threads/handlers.ts lines 23 to 24OutdatedResolvedby saphid(GitHub profile, opens in a new tab)@@ -0,0 +4,21 @@4+ type OrchestrationReadModel,5+ type ThreadsCreateInput,6+ type ThreadsCreateResult,7+ type ThreadsListInput,8+ type ThreadsListResult,9+ ThreadsSurfaceError,10+ type ThreadsListItem,11+ THREADS_SURFACE_LIST_DEFAULT_LIMIT,12+ THREADS_SURFACE_LIST_MAX_LIMIT,13+} from "@t3tools/contracts";14+import * as DateTime from "effect/DateTime";15+import * as Crypto from "effect/Crypto";16+import * as Effect from "effect/Effect";17+18+import * as McpInvocationContext from "../../McpInvocationContext.ts";19+import * as OrchestrationEngine from "../../../orchestration/Services/OrchestrationEngine.ts";20+import * as ProjectionSnapshotQuery from "../../../orchestration/Services/ProjectionSnapshotQuery.ts";21+import { ThreadsToolkit } from "./tools.ts";22+23+const fail = (operation: ThreadsSurfaceError["operation"], detail: string) =>24+ Effect.fail(new ThreadsSurfaceError({ operation, detail }));
- apps/server/src/mcp/toolkits/threads/handlers.ts line 81OutdatedResolvedby saphid(GitHub profile, opens in a new tab)@@ -0,0 +61,21 @@61+ });62+63+const threadsCreate = (input: ThreadsCreateInput) =>64+ Effect.gen(function* () {65+ const scope = yield* McpInvocationContext.McpInvocationContext;66+ const query = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;67+ const readModel = yield* query68+ .getCommandReadModel()69+ .pipe(failFrom("threads_create", "Failed to load the environment's threads."));70+71+ const callingThread = readModel.threads.find((thread) => thread.id === scope.threadId);72+ if (!callingThread) {73+ return yield* Effect.fail(74+ new ThreadsSurfaceError({75+ operation: "threads_create",76+ detail: "Calling thread no longer exists; cannot derive the target project.",77+ }),78+ );79+ }80+ const projectId = input.projectId ?? callingThread.projectId;81+ if (!readModel.projects.some((project) => project.id === projectId)) {
- apps/server/src/mcp/toolkits/threads/handlers.ts lines 47 to 47OutdatedResolvedby saphid(GitHub profile, opens in a new tab)@@ -0,0 +27,21 @@27+ thread.settledOverride === "settled" ||28+ (thread.settledOverride === null && thread.settledAt !== null);29+30+const liveThreads = (readModel: OrchestrationReadModel) =>31+ readModel.threads.filter((thread) => thread.deletedAt === null && thread.archivedAt === null);32+33+const threadsList = (input: ThreadsListInput) =>34+ Effect.gen(function* () {35+ const query = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;36+ const readModel = yield* query37+ .getCommandReadModel()38+ .pipe(failFrom("threads_list", "Failed to load the environment's threads."));39+40+ const threads = liveThreads(readModel)41+ .filter((thread) => input.projectId === undefined || thread.projectId === input.projectId)42+ .filter((thread) => {43+ if (input.filter === "settled") return isThreadSettled(thread);44+ if (input.filter === "active") return !isThreadSettled(thread);45+ return true;46+ })47+ .sort((a, b) => (a.updatedAt < b.updatedAt ? 1 : a.updatedAt > b.updatedAt ? -1 : 0))
- apps/server/src/mcp/toolkits/threads/handlers.ts lines 47 to 47OutdatedResolvedby saphid(GitHub profile, opens in a new tab)@@ -0,0 +27,21 @@27+ thread.settledOverride === "settled" ||28+ (thread.settledOverride === null && thread.settledAt !== null);29+30+const liveThreads = (readModel: OrchestrationReadModel) =>31+ readModel.threads.filter((thread) => thread.deletedAt === null && thread.archivedAt === null);32+33+const threadsList = (input: ThreadsListInput) =>34+ Effect.gen(function* () {35+ const query = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;36+ const readModel = yield* query37+ .getCommandReadModel()38+ .pipe(failFrom("threads_list", "Failed to load the environment's threads."));39+40+ const threads = liveThreads(readModel)41+ .filter((thread) => input.projectId === undefined || thread.projectId === input.projectId)42+ .filter((thread) => {43+ if (input.filter === "settled") return isThreadSettled(thread);44+ if (input.filter === "active") return !isThreadSettled(thread);45+ return true;46+ })47+ .sort((a, b) => Date.parse(b.updatedAt) - Date.parse(a.updatedAt))
juliusmarminge closed this
Sign in to join this conversation.



macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed