Skip to content

Commit 43dfaa3

Browse files
donjor-agent[bot]donjor
authored andcommitted
fix(server): external MCP threads inherit the scoped default model and options (#1646)
## Problem External MCP `create_thread` read the project row's legacy `defaultModelSelection`. After the settings fold, the UI reads `projectSettingsOverrides` and the environment default instead, so external threads launched with a stale model and lost saved options such as `effort: high`. Live example, project `t3code-hyprws`: | Source | Selection | | --- | --- | | Legacy row (external MCP) | `codex` `gpt-5.6-sol`, reasoning high | | Folded override (UI) | `claudeAgent` `claude-opus-5-5`, effort high, 1m | ## Change `ExternalMcpService` reads `ServerSettingsService.getSettings` on each create and resolves the default with `resolveProjectSettings(settings, project.id, project)`, the same call web `useHandleNewThread` makes. Saved options pass through unchanged. Like the composer, an inherited default whose instance cannot run falls back to the first usable instance instead of failing. | Boundary | State | | --- | --- | | `target.options` | still `invalid_request` | | explicit `target.providerInstanceId` | still strict | | Auth, DPoP grant, project policy | untouched | ## Scope and approval Owner-requested fix to the fork's own external MCP feature (`device-auth`, #1636): external launches must inherit the UI's saved project or environment default. ## Verification ```text vp test run apps/server/src/mcp/external/ → 24 passed base resolver restored → 4 failed, 20 passed fallback guard removed → 2 failed, 22 passed apps/server tsc --noEmit → exit 0 vp lint apps/server/src/mcp/external/ → exit 0 vp run fork:ci → exit 0 ``` | New test | Proves | | --- | --- | | environment effort | inherited, re-read per launch | | project override | its provider and all options win | | options target | still rejected | | unusable default (x2) | missing or signed out: falls back | | orchestrator (x2) | high effort persisted, both scopes | Not checked: a live external client against a deployed build; the served nightly predates this change. Prepared by a Codex task (`fix/external-mcp-scoped-defaults`); adopted, extended with the fallback, re-verified, and published by Claude Opus 5.5 in Claude Code, hosted by T3 Code. Independent review: GPT 6.1 Sol (high) via Pi, two rounds. ## Fork trailers Fork-Domain: device-auth Fork-Tier: core Co-authored-by: donjor <[email protected]>
1 parent 633b107 commit 43dfaa3

5 files changed

Lines changed: 208 additions & 9 deletions

‎apps/server/src/mcp/external/ExternalMcpServer.auth.fork.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import * as ThreadManagementService from "../../orchestration-v2/ThreadManagemen
3232
import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
3333
import * as ProjectService from "../../project/ProjectService.ts";
3434
import * as ProviderRegistry from "../../provider/Services/ProviderRegistry.ts";
35+
import * as ServerSettings from "../../serverSettings.ts";
3536
import * as ExternalMcpServer from "./ExternalMcpServer.fork.ts";
3637

3738
// A web `Request` carries no Host header, so the server derives `localhost`.
@@ -74,6 +75,7 @@ const dependencies = Layer.mergeAll(
7475
Layer.mock(ProjectService.ProjectService)({}),
7576
Layer.mock(ProviderRegistry.ProviderRegistry)({}),
7677
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({}),
78+
ServerSettings.layerTest(),
7779
).pipe(
7880
Layer.provideMerge(
7981
HttpPlatform.layer.pipe(Layer.provideMerge(NodeServices.layer), Layer.provide(Etag.layerWeak)),

‎apps/server/src/mcp/external/ExternalMcpServer.fork.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as ThreadManagementService from "../../orchestration-v2/ThreadManagemen
1919
import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
2020
import * as ProjectService from "../../project/ProjectService.ts";
2121
import * as ProviderRegistry from "../../provider/Services/ProviderRegistry.ts";
22+
import * as ServerSettings from "../../serverSettings.ts";
2223
import * as ExternalMcpServer from "./ExternalMcpServer.fork.ts";
2324

2425
const policy: ExternalMcpGrant.ExternalMcpPolicy = {
@@ -88,6 +89,7 @@ const dependencies = Layer.mergeAll(
8889
Layer.mock(ProjectService.ProjectService)({}),
8990
Layer.mock(ProviderRegistry.ProviderRegistry)({}),
9091
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({}),
92+
ServerSettings.layerTest(),
9193
HttpPlatform.layer.pipe(Layer.provideMerge(NodeServices.layer), Layer.provide(Etag.layerWeak)),
9294
SqlitePersistenceMemory,
9395
);

‎apps/server/src/mcp/external/ExternalMcpService.fork.test.ts‎

Lines changed: 154 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { assert, describe, it } from "@effect/vitest";
22
import {
33
AuthSessionId,
4+
DEFAULT_SERVER_SETTINGS,
5+
type ModelSelection,
46
type OrchestrationProjectShell,
57
type OrchestrationV2ServerCommand,
68
type OrchestrationV2ThreadProjection,
79
ProjectId,
10+
ProviderDriverKind,
811
ProviderInstanceId,
912
RunId,
1013
type ServerProvider,
14+
type ServerSettings as ServerSettingsValue,
1115
ThreadId,
1216
} from "@t3tools/contracts";
1317
import * as Effect from "effect/Effect";
@@ -20,19 +24,23 @@ import * as ProjectService from "../../project/ProjectService.ts";
2024
import * as ProviderRegistry from "../../provider/Services/ProviderRegistry.ts";
2125
import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
2226
import type { ExternalMcpPolicy } from "../../auth/ExternalMcpGrant.fork.ts";
27+
import * as ServerSettings from "../../serverSettings.ts";
2328
import * as ExternalMcpService from "./ExternalMcpService.fork.ts";
2429

2530
const granted = ProjectId.make("project:granted");
2631
const other = ProjectId.make("project:other");
2732
const instanceId = ProviderInstanceId.make("codex");
2833
const modelSelection = { instanceId, model: "gpt-test" };
2934

30-
const projectShell = (id: ProjectId) =>
35+
const projectShell = (
36+
id: ProjectId,
37+
defaultModelSelection: ModelSelection | null = modelSelection,
38+
) =>
3139
({
3240
id,
3341
title: String(id),
3442
workspaceRoot: `/work/${id}`,
35-
defaultModelSelection: modelSelection,
43+
defaultModelSelection,
3644
}) as unknown as OrchestrationProjectShell;
3745

3846
const provider = {
@@ -63,11 +71,15 @@ const principal = (
6371
});
6472

6573
/** Threads live in a map keyed by id; `thread.create` adds one, so a reload finds it. */
66-
const makeHarness = () => {
74+
const makeHarness = (
75+
settings: Partial<ServerSettingsValue> = {},
76+
legacySelection: ModelSelection | null = modelSelection,
77+
providers: ReadonlyArray<ServerProvider> = [provider],
78+
) => {
6779
const dispatched: Array<OrchestrationV2ServerCommand> = [];
6880
const sent: Array<ThreadManagementService.ThreadManagementSendInput> = [];
6981
// Flipped to make a send fail after it, as a thread whose run has ended would.
70-
const state = { sendFails: false };
82+
const state = { sendFails: false, settings: { ...DEFAULT_SERVER_SETTINGS, ...settings } };
7183
const threads = new Map<
7284
string,
7385
{ projectId: ProjectId; runtimeMode: string; interactionMode: string }
@@ -137,12 +149,16 @@ const makeHarness = () => {
137149
}),
138150
}),
139151
Layer.mock(ProjectService.ProjectService)({
140-
getShell: (projectId) => Effect.succeed(Option.some(projectShell(projectId))),
152+
getShell: (projectId) =>
153+
Effect.succeed(Option.some(projectShell(projectId, legacySelection))),
141154
listShells: () => Effect.succeed([projectShell(granted), projectShell(other)]),
142155
}),
143-
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([provider]) }),
156+
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed(providers) }),
144157
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
145-
list: () => Effect.succeed([instanceId]),
158+
list: () => Effect.succeed(providers.map((entry) => entry.instanceId)),
159+
}),
160+
Layer.mock(ServerSettings.ServerSettingsService)({
161+
getSettings: Effect.sync(() => state.settings),
146162
}),
147163
Layer.fresh(SqlitePersistenceMemory),
148164
),
@@ -160,6 +176,137 @@ const run = <A, E>(
160176
);
161177

162178
describe("ExternalMcpServiceFork", () => {
179+
it.effect(
180+
"inherits environment effort for a migrated project and reads changes on each launch",
181+
() => {
182+
const high = {
183+
instanceId,
184+
model: "gpt-6.1-sol",
185+
options: [{ id: "reasoningEffort", value: "high" }],
186+
} satisfies ModelSelection;
187+
const medium = { ...high, options: [{ id: "reasoningEffort", value: "medium" }] };
188+
const harness = makeHarness(
189+
{ defaultModelSelection: medium, projectSettingsFolded: true },
190+
null,
191+
[
192+
{
193+
...provider,
194+
models: [{ slug: high.model, name: high.model, isCustom: false, capabilities: null }],
195+
},
196+
],
197+
);
198+
return run(harness, (service) =>
199+
Effect.gen(function* () {
200+
yield* service.createThread(principal(), {
201+
projectId: granted,
202+
clientRequestId: "medium",
203+
});
204+
harness.state.settings = { ...harness.state.settings, defaultModelSelection: high };
205+
yield* service.createThread(principal(), { projectId: granted, clientRequestId: "high" });
206+
const selections = harness.dispatched.flatMap((command) =>
207+
command.type === "thread.create" ? [command.modelSelection] : [],
208+
);
209+
assert.deepEqual(selections, [medium, high]);
210+
}),
211+
);
212+
},
213+
);
214+
215+
it.effect(
216+
"uses the current project provider and all its options over environment and legacy defaults",
217+
() => {
218+
const claudeId = ProviderInstanceId.make("claudeAgent");
219+
const high = {
220+
instanceId: claudeId,
221+
model: "claude-opus-5-5",
222+
options: [
223+
{ id: "effort", value: "high" },
224+
{ id: "contextWindow", value: "200k" },
225+
],
226+
} satisfies ModelSelection;
227+
const harness = makeHarness(
228+
{
229+
defaultModelSelection: modelSelection,
230+
projectSettingsFolded: true,
231+
projectSettingsOverrides: { [granted]: { defaultModelSelection: high } },
232+
},
233+
modelSelection,
234+
[
235+
provider,
236+
{
237+
...provider,
238+
instanceId: claudeId,
239+
driver: ProviderDriverKind.make("claudeAgent"),
240+
models: [{ slug: high.model, name: high.model, isCustom: false, capabilities: null }],
241+
},
242+
],
243+
);
244+
return run(harness, (service) =>
245+
Effect.gen(function* () {
246+
yield* service.createThread(principal(), {
247+
projectId: granted,
248+
clientRequestId: "project",
249+
});
250+
const command = harness.dispatched[0];
251+
assert.equal(command?.type, "thread.create");
252+
if (command?.type !== "thread.create") return;
253+
assert.deepEqual(command.modelSelection, high);
254+
}),
255+
);
256+
},
257+
);
258+
259+
it.effect("keeps external model option overrides unsupported", () => {
260+
const harness = makeHarness();
261+
return run(harness, (service) =>
262+
Effect.gen(function* () {
263+
const error = yield* service
264+
.createThread(principal(), {
265+
projectId: granted,
266+
clientRequestId: "options",
267+
target: { options: [{ id: "reasoningEffort", value: "high" }] },
268+
})
269+
.pipe(Effect.flip);
270+
assert.equal(error.code, "invalid_request");
271+
assert.equal(harness.dispatched.length, 0);
272+
}),
273+
);
274+
});
275+
276+
it.effect.each(["missing", "signed out"] as const)(
277+
"falls back to a usable provider when the inherited default's provider is %s",
278+
(state) => {
279+
const claudeId = ProviderInstanceId.make("claudeAgent");
280+
const harness = makeHarness(
281+
{
282+
defaultModelSelection: { instanceId: claudeId, model: "claude-opus-5-5" },
283+
projectSettingsFolded: true,
284+
},
285+
null,
286+
state === "missing"
287+
? [provider]
288+
: [
289+
provider,
290+
{
291+
...provider,
292+
instanceId: claudeId,
293+
driver: ProviderDriverKind.make("claudeAgent"),
294+
auth: { status: "unauthenticated" },
295+
} as ServerProvider,
296+
],
297+
);
298+
return run(harness, (service) =>
299+
Effect.gen(function* () {
300+
yield* service.createThread(principal(), { projectId: granted, clientRequestId: "gone" });
301+
const command = harness.dispatched[0];
302+
assert.equal(command?.type, "thread.create");
303+
if (command?.type !== "thread.create") return;
304+
assert.deepEqual(command.modelSelection, modelSelection);
305+
}),
306+
);
307+
},
308+
);
309+
163310
it.effect("reaches only the projects and threads the grant names", () => {
164311
const harness = makeHarness();
165312
return run(harness, (service) =>

‎apps/server/src/mcp/external/ExternalMcpService.fork.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
RunId,
3232
ThreadId,
3333
} from "@t3tools/contracts";
34+
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
3435
import * as Context from "effect/Context";
3536
import * as DateTime from "effect/DateTime";
3637
import * as Effect from "effect/Effect";
@@ -47,6 +48,7 @@ import * as ProviderAdapterRegistry from "../../orchestration-v2/ProviderAdapter
4748
import * as ThreadManagementService from "../../orchestration-v2/ThreadManagementService.ts";
4849
import * as ProjectService from "../../project/ProjectService.ts";
4950
import * as ProviderRegistry from "../../provider/Services/ProviderRegistry.ts";
51+
import * as ServerSettings from "../../serverSettings.ts";
5052
import {
5153
listItemFromShellFork,
5254
resolveInteractionMode,
@@ -256,6 +258,7 @@ const make = Effect.gen(function* () {
256258
const projects = yield* ProjectService.ProjectService;
257259
const providerRegistry = yield* ProviderRegistry.ProviderRegistry;
258260
const providerAdapters = yield* ProviderAdapterRegistry.ProviderAdapterRegistryV2;
261+
const serverSettings = yield* ServerSettings.ServerSettingsService;
259262
const ledger = yield* makeExternalMcpRequestLedger;
260263

261264
const loadProject = (principal: ExternalMcpPrincipal, projectId: ProjectId) =>
@@ -368,7 +371,17 @@ const make = Effect.gen(function* () {
368371
isProviderAvailable(provider) &&
369372
provider.auth.status !== "unauthenticated",
370373
);
371-
const projectDefault = project.defaultModelSelection;
374+
const settings = yield* serverSettings.getSettings.pipe(
375+
Effect.mapError((error) => failure("orchestration_error", errorMessage(error))),
376+
);
377+
// Like the composer, a default whose instance cannot run falls back to a usable one.
378+
const scopedDefault = resolveProjectSettings(settings, project.id, project).settings
379+
.defaultModelSelection;
380+
const projectDefault =
381+
scopedDefault !== null &&
382+
usable.some((provider) => provider.instanceId === scopedDefault.instanceId)
383+
? scopedDefault
384+
: null;
372385
const instanceId =
373386
target?.providerInstanceId ??
374387
(target?.driverKind === undefined

‎apps/server/src/mcp/external/ExternalMcpService.orchestrator.fork.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
RunAttemptId,
2222
type RunId,
2323
type ServerProvider,
24+
type ServerSettings as ServerSettingsValue,
2425
ThreadId,
2526
} from "@t3tools/contracts";
2627
import * as DateTime from "effect/DateTime";
@@ -47,6 +48,7 @@ import { makeOrchestratorV2ReplayLayerWithRegistry } from "../../orchestration-v
4748
import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
4849
import * as ProjectService from "../../project/ProjectService.ts";
4950
import { makeProviderRegistryLayer } from "../../provider/testUtils/providerRegistryMock.ts";
51+
import * as ServerSettings from "../../serverSettings.ts";
5052
import * as ExternalMcpService from "./ExternalMcpService.fork.ts";
5153

5254
const projectId = ProjectId.make("project:external-granted");
@@ -277,6 +279,7 @@ const harness = <A, E>(
277279
step: Effect.Effect<void>,
278280
) => void;
279281
}) => Effect.Effect<A, E>,
282+
settings: Partial<ServerSettingsValue> = {},
280283
) =>
281284
Effect.scoped(
282285
Effect.gen(function* () {
@@ -303,7 +306,7 @@ const harness = <A, E>(
303306
id: projectId,
304307
title: "granted",
305308
workspaceRoot: cwd,
306-
defaultModelSelection: modelSelection,
309+
defaultModelSelection: null,
307310
} as unknown as OrchestrationProjectShell;
308311
let pending:
309312
| {
@@ -343,6 +346,9 @@ const harness = <A, E>(
343346
),
344347
Layer.provide(registryLayer),
345348
Layer.provide(makeProviderRegistryLayer([provider])),
349+
Layer.provide(
350+
ServerSettings.layerTest({ defaultModelSelection: modelSelection, ...settings }),
351+
),
346352
Layer.provideMerge(SqlitePersistenceMemory),
347353
),
348354
).pipe(Layer.provide(NodeServices.layer));
@@ -368,6 +374,35 @@ const harness = <A, E>(
368374
);
369375

370376
describe("ExternalMcpServiceFork on the orchestrator", () => {
377+
it.live.each(["environment", "project"] as const)(
378+
"persists inherited high effort from %s settings when no target is supplied",
379+
(scope) => {
380+
const high = {
381+
...modelSelection,
382+
options: [{ id: "reasoningEffort", value: "high" }],
383+
} satisfies ModelSelection;
384+
return harness(
385+
`external-mcp-${scope}-default`,
386+
({ service, orchestrator }) =>
387+
Effect.gen(function* () {
388+
const created = yield* service.createThread(principal, {
389+
projectId,
390+
clientRequestId: "inherited-effort",
391+
});
392+
const persisted = yield* orchestrator.getThreadProjection(created.threadId);
393+
assert.deepEqual(persisted.thread.modelSelection, high);
394+
assert.isNull(created.runId);
395+
}),
396+
{
397+
projectSettingsFolded: true,
398+
...(scope === "environment"
399+
? { defaultModelSelection: high }
400+
: { projectSettingsOverrides: { [projectId]: { defaultModelSelection: high } } }),
401+
},
402+
);
403+
},
404+
);
405+
371406
it.live("never prompts a created thread the owner has since raised", () =>
372407
harness("external-mcp-create-replay", ({ service, orchestrator, provider }) =>
373408
Effect.gen(function* () {

0 commit comments

Comments
 (0)