From 559e41a91fc4ea98ec4b73e94d4f4857de30c8c3 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 20 Jun 2026 22:14:07 -0700 Subject: [PATCH] Require MCP registry for V2 provider sessions --- .../src/mcp/McpSessionRegistry.testkit.ts | 27 ++++++++++++++ .../AcpRegistryOrchestratorV2.live.test.ts | 2 + .../CursorOrchestratorV2.live.test.ts | 2 + .../GrokOrchestratorV2.live.test.ts | 2 + .../ProviderSessionManager.test.ts | 16 ++++++-- .../ProviderSessionManager.ts | 37 ++++++++++++------- .../src/orchestration-v2/runtimeLayer.test.ts | 2 + .../testkit/ProviderReplayHarness.ts | 17 +++++++-- apps/server/src/server.test.ts | 18 +++++---- apps/server/src/server.ts | 8 ++-- 10 files changed, 101 insertions(+), 30 deletions(-) create mode 100644 apps/server/src/mcp/McpSessionRegistry.testkit.ts diff --git a/apps/server/src/mcp/McpSessionRegistry.testkit.ts b/apps/server/src/mcp/McpSessionRegistry.testkit.ts new file mode 100644 index 0000000000..8c9818ce03 --- /dev/null +++ b/apps/server/src/mcp/McpSessionRegistry.testkit.ts @@ -0,0 +1,27 @@ +import { EnvironmentId } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as Layer from "effect/Layer"; + +import { McpSessionRegistry } from "./McpSessionRegistry.ts"; + +export const layer = Layer.succeed( + McpSessionRegistry, + McpSessionRegistry.of({ + issue: ({ threadId, providerInstanceId }) => + Effect.succeed({ + config: { + environmentId: EnvironmentId.make("environment:mcp-test"), + threadId, + providerSessionId: `mcp-test:${threadId}`, + providerInstanceId, + endpoint: "http://127.0.0.1/mcp", + authorizationHeader: `Bearer mcp-test:${threadId}`, + }, + expiresAt: Number.MAX_SAFE_INTEGER, + }), + resolve: () => Effect.succeed(undefined), + revokeProviderSession: () => Effect.void, + revokeThread: () => Effect.void, + revokeAll: Effect.succeed(undefined), + }), +); diff --git a/apps/server/src/orchestration-v2/AcpRegistryOrchestratorV2.live.test.ts b/apps/server/src/orchestration-v2/AcpRegistryOrchestratorV2.live.test.ts index 159b2069fa..0fedf0b7a3 100644 --- a/apps/server/src/orchestration-v2/AcpRegistryOrchestratorV2.live.test.ts +++ b/apps/server/src/orchestration-v2/AcpRegistryOrchestratorV2.live.test.ts @@ -29,6 +29,7 @@ import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; import * as VcsProcess from "../vcs/VcsProcess.ts"; import { OrchestratorV2 } from "./Orchestrator.ts"; import { OrchestrationV2LayerLive } from "./runtimeLayer.ts"; +import { layer as mcpSessionRegistryTestLayer } from "../mcp/McpSessionRegistry.testkit.ts"; const liveAgentId = process.env.T3_ACP_REGISTRY_LIVE_AGENT_ID?.trim() || "devin"; const liveCommandPath = process.env.T3_ACP_REGISTRY_LIVE_COMMAND?.trim(); @@ -77,6 +78,7 @@ const providerInstanceRegistryLayer = ProviderInstanceRegistryHydrationLive.pipe ); const liveLayer = OrchestrationV2LayerLive.pipe( + Layer.provide(mcpSessionRegistryTestLayer), Layer.provide(SqlitePersistenceMemory), Layer.provide(checkpointStoreLayer), Layer.provide(serverConfigLayer), diff --git a/apps/server/src/orchestration-v2/CursorOrchestratorV2.live.test.ts b/apps/server/src/orchestration-v2/CursorOrchestratorV2.live.test.ts index 59ff981f89..56abf680dd 100644 --- a/apps/server/src/orchestration-v2/CursorOrchestratorV2.live.test.ts +++ b/apps/server/src/orchestration-v2/CursorOrchestratorV2.live.test.ts @@ -27,6 +27,7 @@ import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; import * as VcsProcess from "../vcs/VcsProcess.ts"; import { OrchestratorV2 } from "./Orchestrator.ts"; import { OrchestrationV2LayerLive } from "./runtimeLayer.ts"; +import { layer as mcpSessionRegistryTestLayer } from "../mcp/McpSessionRegistry.testkit.ts"; import { CURSOR_MODEL_SELECTION } from "./testkit/fixtures/shared.ts"; const serverConfigLayer = ServerConfig.layerTest(process.cwd(), { @@ -60,6 +61,7 @@ const providerInstanceRegistryLayer = ProviderInstanceRegistryHydrationLive.pipe ); const liveLayer = OrchestrationV2LayerLive.pipe( + Layer.provide(mcpSessionRegistryTestLayer), Layer.provide(SqlitePersistenceMemory), Layer.provide(checkpointStoreLayer), Layer.provide(serverConfigLayer), diff --git a/apps/server/src/orchestration-v2/GrokOrchestratorV2.live.test.ts b/apps/server/src/orchestration-v2/GrokOrchestratorV2.live.test.ts index 5e5c0aa33e..6c8ac93777 100644 --- a/apps/server/src/orchestration-v2/GrokOrchestratorV2.live.test.ts +++ b/apps/server/src/orchestration-v2/GrokOrchestratorV2.live.test.ts @@ -27,6 +27,7 @@ import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; import * as VcsProcess from "../vcs/VcsProcess.ts"; import { OrchestratorV2 } from "./Orchestrator.ts"; import { OrchestrationV2LayerLive } from "./runtimeLayer.ts"; +import { layer as mcpSessionRegistryTestLayer } from "../mcp/McpSessionRegistry.testkit.ts"; import { GROK_MODEL_SELECTION } from "./testkit/fixtures/shared.ts"; const serverConfigLayer = ServerConfig.layerTest(process.cwd(), { @@ -60,6 +61,7 @@ const providerInstanceRegistryLayer = ProviderInstanceRegistryHydrationLive.pipe ); const liveLayer = OrchestrationV2LayerLive.pipe( + Layer.provide(mcpSessionRegistryTestLayer), Layer.provide(SqlitePersistenceMemory), Layer.provide(checkpointStoreLayer), Layer.provide(serverConfigLayer), diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts index cb92f17d0a..888d8c083b 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.test.ts @@ -304,9 +304,16 @@ function makeTestLayer(input: { TestStoresLayer, TestEventSinkLayer, idAllocatorLayer, + TestMcpRegistryLayer, providerSessionManagerLayerWithOptions({ idleTimeoutMs: input.idleTimeoutMs }).pipe( Layer.provide( - Layer.mergeAll(registryLayer, TestEventSinkLayer, idAllocatorLayer, TestStoresLayer), + Layer.mergeAll( + registryLayer, + TestEventSinkLayer, + idAllocatorLayer, + TestMcpRegistryLayer, + TestStoresLayer, + ), ), ), ); @@ -322,7 +329,10 @@ const fakeEnvironment = ServerEnvironment.of({ getDescriptor: Effect.die("unused"), }); -const TestMcpRegistryLayer = McpSessionRegistry.layer.pipe( +const TestMcpRegistryLayer = Layer.effect( + McpSessionRegistry.McpSessionRegistry, + McpSessionRegistry.__testing.make(), +).pipe( Layer.provide(Layer.succeed(HttpServer.HttpServer, fakeHttpServer)), Layer.provide(Layer.succeed(ServerEnvironment, fakeEnvironment)), Layer.provide(NodeServices.layer), @@ -530,7 +540,7 @@ it.effect( state, idleTimeoutMs: 1_000, mcpConfigs, - }).pipe(Layer.provideMerge(TestMcpRegistryLayer)), + }), ), ); }), diff --git a/apps/server/src/orchestration-v2/ProviderSessionManager.ts b/apps/server/src/orchestration-v2/ProviderSessionManager.ts index 6f909cc338..695b020faa 100644 --- a/apps/server/src/orchestration-v2/ProviderSessionManager.ts +++ b/apps/server/src/orchestration-v2/ProviderSessionManager.ts @@ -190,6 +190,8 @@ type ProviderSessionEventSignal = export interface ProviderSessionManagerV2LayerOptions { readonly idleTimeoutMs?: number; + /** Test replay harnesses can omit T3's MCP server from provider protocol fixtures. */ + readonly configureMcp?: boolean; } function releaseStatusFor( @@ -236,12 +238,17 @@ export const layerWithOptions = ( ): Layer.Layer< ProviderSessionManagerV2, never, - EventSinkV2 | IdAllocatorV2 | ProjectionStoreV2 | ProviderAdapterRegistryV2 + | EventSinkV2 + | IdAllocatorV2 + | McpSessionRegistry.McpSessionRegistry + | ProjectionStoreV2 + | ProviderAdapterRegistryV2 > => Layer.effect( ProviderSessionManagerV2, Effect.gen(function* () { const registry = yield* ProviderAdapterRegistryV2; + const mcpSessionRegistry = yield* McpSessionRegistry.McpSessionRegistry; const eventSink = yield* EventSinkV2; const idAllocator = yield* IdAllocatorV2; const projectionStore = yield* ProjectionStoreV2; @@ -251,20 +258,22 @@ export const layerWithOptions = ( const openSemaphore = yield* Semaphore.make(1); const idleTimeoutMs = Math.max(1, options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS); const prepareMcpSession = (threadId: ThreadId, providerInstanceId: ProviderInstanceId) => - McpSessionRegistry.issueActiveMcpCredential({ - threadId, - providerInstanceId, - }).pipe( - Effect.tap((credential) => - credential === undefined - ? Effect.void - : Effect.sync(() => McpProviderSession.setMcpProviderSession(credential.config)), - ), - ); + options.configureMcp === false + ? Effect.sync(() => McpProviderSession.clearMcpProviderSession(threadId)) + : mcpSessionRegistry.revokeThread(threadId).pipe( + Effect.andThen(mcpSessionRegistry.issue({ threadId, providerInstanceId })), + Effect.tap((credential) => + Effect.sync(() => McpProviderSession.setMcpProviderSession(credential.config)), + ), + ); const clearMcpSession = (threadId: ThreadId) => - McpSessionRegistry.revokeActiveMcpThread(threadId).pipe( - Effect.tap(() => Effect.sync(() => McpProviderSession.clearMcpProviderSession(threadId))), - ); + mcpSessionRegistry + .revokeThread(threadId) + .pipe( + Effect.tap(() => + Effect.sync(() => McpProviderSession.clearMcpProviderSession(threadId)), + ), + ); const publishToSubscribers = ( subscribers: Ref.Ref>>, diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index 5f34d0d07a..c71edc1ccc 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -17,6 +17,7 @@ import { ServerConfig } from "../config.ts"; import { GitCoreLive } from "../git/Layers/GitCore.ts"; import { SqlitePersistenceMemory } from "../persistence/Layers/Sqlite.ts"; import { ServerSettingsService } from "../serverSettings.ts"; +import { layer as mcpSessionRegistryTestLayer } from "../mcp/McpSessionRegistry.testkit.ts"; import { ProviderInstanceRegistry } from "../provider/Services/ProviderInstanceRegistry.ts"; import type { ProviderInstance } from "../provider/ProviderDriver.ts"; import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts"; @@ -72,6 +73,7 @@ const TestProviderInstanceRegistry = Layer.succeed(ProviderInstanceRegistry, { }); const TestLayer = OrchestrationV2LayerLive.pipe( + Layer.provide(mcpSessionRegistryTestLayer), Layer.provide(SqlitePersistenceMemory), Layer.provide(CheckpointStoreTestLayer), Layer.provide(ServerConfigLayer), diff --git a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts index 41cf250160..80d6437606 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts @@ -13,6 +13,7 @@ import * as CheckpointStore from "../../checkpointing/CheckpointStore.ts"; import { ServerConfig } from "../../config.ts"; import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts"; import { ServerSettingsService } from "../../serverSettings.ts"; +import { layer as mcpSessionRegistryTestLayer } from "../../mcp/McpSessionRegistry.testkit.ts"; import * as VcsDriverRegistry from "../../vcs/VcsDriverRegistry.ts"; import * as VcsProcess from "../../vcs/VcsProcess.ts"; import { layer as checkpointCaptureServiceLayer } from "../CheckpointCaptureService.ts"; @@ -35,7 +36,7 @@ import { layer as projectionStoreLayer } from "../ProjectionStore.ts"; import type { OrchestratorV2, OrchestratorV2Error } from "../Orchestrator.ts"; import { ProviderAdapterRegistryV2 } from "../ProviderAdapterRegistry.ts"; import { layer as providerEventIngestorLayer } from "../ProviderEventIngestor.ts"; -import { layer as providerSessionManagerLayer } from "../ProviderSessionManager.ts"; +import { layerWithOptions as providerSessionManagerLayerWithOptions } from "../ProviderSessionManager.ts"; import { layer as providerSwitchServiceLayer } from "../ProviderSwitchService.ts"; import { layer as providerTurnControlServiceLayer } from "../ProviderTurnControlService.ts"; import { layer as providerTurnStartServiceLayer } from "../ProviderTurnStartService.ts"; @@ -262,8 +263,18 @@ export function makeOrchestratorV2ReplayLayerWithRegistry( idAllocatorLayer, providerEventIngestorProvided, ); - const providerSessionManagerProvided = providerSessionManagerLayer.pipe( - Layer.provide(Layer.mergeAll(registryLayer, eventSinkProvided, idAllocatorLayer, storesLayer)), + const providerSessionManagerProvided = providerSessionManagerLayerWithOptions({ + configureMcp: false, + }).pipe( + Layer.provide( + Layer.mergeAll( + registryLayer, + eventSinkProvided, + idAllocatorLayer, + mcpSessionRegistryTestLayer, + storesLayer, + ), + ), ); const providerSwitchServiceProvided = providerSwitchServiceLayer.pipe( Layer.provide(registryLayer), diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index 4db8ffdd9b..e4019c336e 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -128,6 +128,7 @@ import * as T3ProjectFileLoader from "./project/T3ProjectFileLoader.ts"; import * as ProjectSetupScriptRunner from "./project/ProjectSetupScriptRunner.ts"; import * as RepositoryIdentityResolver from "./project/RepositoryIdentityResolver.ts"; import * as ServerEnvironment from "./environment/ServerEnvironment.ts"; +import { layer as mcpSessionRegistryTestLayer } from "./mcp/McpSessionRegistry.testkit.ts"; import * as WorkspaceEntries from "./workspace/WorkspaceEntries.ts"; import * as WorkspaceFileSystem from "./workspace/WorkspaceFileSystem.ts"; import * as WorkspacePaths from "./workspace/WorkspacePaths.ts"; @@ -572,14 +573,17 @@ const buildAppUnderTest = (options?: { disableLogger: true, }).pipe( Layer.provide( - Layer.mock(Keybindings.Keybindings)({ - loadConfigState: Effect.succeed({ - keybindings: [], - issues: [], + Layer.merge( + mcpSessionRegistryTestLayer, + Layer.mock(Keybindings.Keybindings)({ + loadConfigState: Effect.succeed({ + keybindings: [], + issues: [], + }), + streamChanges: Stream.empty, + ...options?.layers?.keybindings, }), - streamChanges: Stream.empty, - ...options?.layers?.keybindings, - }), + ), ), Layer.provide( Layer.mock(ProviderRegistry.ProviderRegistry)({ diff --git a/apps/server/src/server.ts b/apps/server/src/server.ts index f1d234bbac..d99423a3ee 100644 --- a/apps/server/src/server.ts +++ b/apps/server/src/server.ts @@ -445,7 +445,9 @@ export const makeRoutesLayer = Layer.mergeAll( staticAndDevRouteLayer, websocketRpcRouteLayer, ), - McpHttpServer.layer.pipe(Layer.provide(McpSessionRegistry.layer)), + // The MCP session registry is provided globally (shared with V2 provider + // sessions) rather than inline here. + McpHttpServer.layer, ).pipe( // Both transports consume the same service instance, so caches single-flight across clients // and mutations observed on WebSocket invalidate patches subsequently read over HTTP. @@ -659,8 +661,8 @@ export const makeServerLayer = Layer.unwrap( ); return serverApplicationLayer.pipe( - Layer.provideMerge(runtimeServicesLive), - Layer.provide(activationLayer), + Layer.provideMerge(RuntimeServicesLive), + Layer.provideMerge(McpSessionRegistry.layer.pipe(Layer.provide(ServerEnvironment.layer))), Layer.provideMerge(serverRelayBrokerTracingLayer), Layer.provideMerge(HttpServerLive), Layer.provide(ApplicationObservabilityLive),