Skip to content

Commit bd0fd6e

Browse files
RVZO6claude
andcommitted
fix(server): outside MCP clients stop being offered thread-bound tools
tools/list returned the same 68 tools to every caller, so a client signed in from outside a thread saw 28 tools it could only fail, such as request_secret. Hide them with EnabledWhen, and make McpToolAccess.toLayer refuse to build a thread-bound declaration that outside clients would still be offered. Authorization does not change: the handlers keep refusing with thread_credential_required, and a test now covers that behind discovery. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 09a54f0 commit bd0fd6e

13 files changed

Lines changed: 287 additions & 38 deletions

File tree

‎apps/server/src/mcp/McpHttpServer.test.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,11 @@ it.effect("refuses preview tools to a client outside a thread before they run",
531531
const click = yield* server
532532
.callTool({ name: "preview_act", arguments: { action: "click", locator: "text=Send" } })
533533
.pipe(
534+
Effect.flip,
534535
Effect.provideService(McpInvocationContext.McpInvocationContext, readOnly),
535536
Effect.provideService(McpSchema.McpServerClient, client),
536537
);
537-
expect(click.isError).toBe(true);
538-
expect(click.content).toEqual([
539-
{ type: "text", text: expect.stringContaining("needs an agent running inside T3 Code") },
540-
]);
538+
expect(click.message).toContain("Tool 'preview_act' not found");
541539
}).pipe(Effect.provide(layerTest)),
542540
);
543541

‎apps/server/src/mcp/McpInvocationContext.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ export const hiddenFromSubagents = (): boolean => {
161161
return scope?.thread?.subagent !== true;
162162
};
163163

164+
/**
165+
* `McpSchema.EnabledWhen` for tools that act as the calling thread: a client
166+
* from outside has none. Discovery only; `McpToolAccess` still refuses the
167+
* call, and `toLayer` requires this on every thread-bound declaration.
168+
*/
169+
export const hiddenFromOutsideClients = (): boolean => {
170+
const fiber = Fiber.getCurrent();
171+
const scope = fiber && Context.getOrUndefined(fiber.context, McpInvocationContext);
172+
return scope === undefined || scope.thread !== undefined;
173+
};
174+
164175
/** `McpSchema.EnabledWhen` for tools that only serve links, hidden while the beta is off. */
165176
export const hiddenWhileLinksOff = (): boolean => {
166177
const fiber = Fiber.getCurrent();

‎apps/server/src/mcp/McpToolAccess.test.ts‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import * as Effect from "effect/Effect";
1313
import * as Exit from "effect/Exit";
1414
import * as Layer from "effect/Layer";
1515
import * as Schema from "effect/Schema";
16+
import * as Sink from "effect/Sink";
17+
import * as Stream from "effect/Stream";
1618
import { McpSchema, McpServer, Tool, Toolkit } from "effect/ai";
1719

1820
import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts";
@@ -113,8 +115,14 @@ const probe = {
113115
};
114116
const ProbeToolkit = Toolkit.make(
115117
Tool.make("reads", probe),
116-
Tool.make("reads_as_caller", probe),
117-
Tool.make("acts_as_caller", probe),
118+
Tool.make("reads_as_caller", probe).annotate(
119+
McpSchema.EnabledWhen,
120+
McpInvocationContext.hiddenFromOutsideClients,
121+
),
122+
Tool.make("acts_as_caller", probe).annotate(
123+
McpSchema.EnabledWhen,
124+
McpInvocationContext.hiddenFromOutsideClients,
125+
),
118126
Tool.make("writes", probe),
119127
Tool.make("writes_with_capability", probe),
120128
Tool.make("writes_threads", {
@@ -254,15 +262,13 @@ it.effect.each([
254262
["writes_environment", readOnlyClient, {}, "capability_denied"],
255263

256264
["reads_as_caller", ended, {}, "ran"],
257-
["reads_as_caller", fullAccessClient, {}, "thread_credential_required"],
258265
["acts_as_caller", supervised, {}, "ran"],
259266
["acts_as_caller", ended, {}, "parent_not_active"],
260267
["acts_as_caller", background, {}, "ran"],
261268
["acts_as_caller", oldProviderBackground, {}, "parent_not_active"],
262269
["acts_as_caller", mismatchedProvider, {}, "parent_not_active"],
263270
["acts_as_caller", archivedBackground, {}, "parent_not_active"],
264271
["acts_as_caller", unknownOwnerBackground, {}, "parent_not_active"],
265-
["acts_as_caller", fullAccessClient, {}, "thread_credential_required"],
266272

267273
["writes", supervised, {}, "ran"],
268274
["writes", ended, {}, "parent_not_active"],
@@ -455,7 +461,12 @@ it.effect.each([
455461

456462
it.effect("refuses a linked environment to a tool that acts as its calling thread", () =>
457463
Effect.gen(function* () {
458-
const CallerToolkit = Toolkit.make(Tool.make("acts_as_caller", linkedProbe));
464+
const CallerToolkit = Toolkit.make(
465+
Tool.make("acts_as_caller", linkedProbe).annotate(
466+
McpSchema.EnabledWhen,
467+
McpInvocationContext.hiddenFromOutsideClients,
468+
),
469+
);
459470
const built = yield* Layer.build(
460471
McpToolAccess.HandlersLayer.layer(
461472
McpToolAccess.toLayer(CallerToolkit, {
@@ -466,3 +477,49 @@ it.effect("refuses a linked environment to a tool that acts as its calling threa
466477
expect(Exit.isFailure(built) && Cause.hasDies(built.cause)).toBe(true);
467478
}),
468479
);
480+
481+
// Discovery hides these tools. The handler must refuse on its own if that ever changes.
482+
it.effect.each(["reads_as_caller", "acts_as_caller"] as const)(
483+
"%s refuses an outside client behind discovery",
484+
(tool) =>
485+
Effect.gen(function* () {
486+
const built = yield* ProbeToolkit;
487+
const { result } = yield* built
488+
.handle(tool, {})
489+
.pipe(Stream.unwrap, Stream.run(Sink.last()), Effect.flatMap(Effect.fromOption));
490+
expect(result).toMatchObject({ code: "thread_credential_required" });
491+
}).pipe(
492+
Effect.provide(McpToolAccess.HandlersLayer.layer(layerProbeHandlers)),
493+
Effect.provideService(McpInvocationContext.McpInvocationContext, fullAccessClient),
494+
Effect.provide(Layer.mock(ThreadManagement.ThreadManagementService)({})),
495+
),
496+
);
497+
498+
it.effect.each(["reads_as_caller", "acts_as_caller"] as const)(
499+
"%s is not found by an outside client",
500+
(tool) =>
501+
McpServer.McpServer.pipe(
502+
Effect.flatMap((server) => server.callTool({ name: tool, arguments: {} })),
503+
Effect.flip,
504+
Effect.map((error) => expect(error.message).toContain(`Tool '${tool}' not found`)),
505+
Effect.provideService(McpInvocationContext.McpInvocationContext, fullAccessClient),
506+
Effect.provideService(McpSchema.McpServerClient, mcpClient),
507+
Effect.provide(probeServer(Layer.mock(ThreadManagement.ThreadManagementService)({}))),
508+
),
509+
);
510+
511+
it.effect("refuses to build a thread-bound tool that outside clients would be offered", () =>
512+
Effect.gen(function* () {
513+
const CallerToolkit = Toolkit.make(Tool.make("reads_as_caller", probe));
514+
const built = yield* Layer.build(
515+
McpToolAccess.HandlersLayer.layer(
516+
McpToolAccess.toLayer(CallerToolkit, {
517+
reads_as_caller: McpToolAccess.readsAsCaller(() => ran),
518+
}),
519+
),
520+
).pipe(Effect.scoped, Effect.exit);
521+
expect(Exit.isFailure(built) && Cause.pretty(built.cause)).toContain(
522+
"reads_as_caller acts as its calling thread",
523+
);
524+
}),
525+
);

‎apps/server/src/mcp/McpToolAccess.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import {
77
type RuntimeMode,
88
type ThreadId,
99
} from "@t3tools/contracts";
10+
import * as Context from "effect/Context";
1011
import * as Effect from "effect/Effect";
1112
import * as Option from "effect/Option";
1213
import * as Predicate from "effect/Predicate";
1314
import * as Ref from "effect/Ref";
1415
import * as Struct from "effect/Struct";
1516
import type * as Layer from "effect/Layer";
1617
import type * as Scope from "effect/Scope";
18+
import { McpSchema } from "effect/ai";
1719
import type { Tool, Toolkit } from "effect/ai";
1820

1921
import {
@@ -385,8 +387,17 @@ const routed = Effect.fn("McpToolAccess.routed")(function* <Tools extends Record
385387
const handlers: Record<string, unknown> = { ...checked };
386388
for (const [name, declaration] of Object.entries<Declaration<unknown>>(declarations)) {
387389
const tool = toolkit.tools[name];
388-
if (tool === undefined || !takesLinkedEnvironment(tool)) continue;
390+
if (tool === undefined) continue;
389391
const remote = remoteCheckOf(declaration);
392+
if (
393+
remote === undefined &&
394+
Context.getOrUndefined(tool.annotations, McpSchema.EnabledWhen) !==
395+
McpInvocationContext.hiddenFromOutsideClients
396+
)
397+
return yield* Effect.die(
398+
`${name} acts as its calling thread, so outside clients must not be offered it.`,
399+
);
400+
if (!takesLinkedEnvironment(tool)) continue;
390401
if (remote === undefined)
391402
return yield* Effect.die(
392403
`${name} takes a linked environment, but it acts as its calling thread, which is here.`,

‎apps/server/src/mcp/toolkits/core.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -650,22 +650,23 @@ it.effect("a read-only client reads threads and is refused every write before it
650650
);
651651
const dispatched: Array<string> = [];
652652

653-
it.effect("refuses act-as-caller tools to a client caller", () =>
653+
it.effect("hides act-as-caller tools from a client caller", () =>
654654
Effect.gen(function* () {
655655
const server = yield* McpServer.McpServer;
656-
const result = yield* server
656+
const error = yield* server
657657
.callTool({
658658
name: "delegate_task",
659659
arguments: { task: "Review", mode: "async" },
660660
})
661661
.pipe(
662+
Effect.flip,
662663
Effect.provideService(
663664
McpInvocationContext.McpInvocationContext,
664665
clientScope("full-access"),
665666
),
666667
Effect.provideService(McpSchema.McpServerClient, client),
667668
);
668-
expect(declaredFailure(result)).toMatchObject({ code: "thread_credential_required" });
669+
expect(error.message).toContain("Tool 'delegate_task' not found");
669670
}).pipe(
670671
Effect.provide(
671672
layerOrchestratorToolkit.pipe(

‎apps/server/src/mcp/toolkits/device/tools.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as Schema from "effect/Schema";
1212
import * as FileSystem from "effect/FileSystem";
1313
import * as Path from "effect/Path";
1414
import * as ServerConfig from "../../../config.ts";
15-
import { Tool, Toolkit } from "effect/ai";
15+
import { McpSchema, Tool, Toolkit } from "effect/ai";
1616

1717
import * as McpInvocationContext from "../../McpInvocationContext.ts";
1818
import * as DeviceService from "../../../device/DeviceService.ts";
@@ -45,7 +45,8 @@ const DeviceListTool = Tool.make("device_list", {
4545
.annotate(Tool.Readonly, true)
4646
.annotate(Tool.Destructive, false)
4747
.annotate(Tool.Idempotent, true)
48-
.annotate(Tool.OpenWorld, false);
48+
.annotate(Tool.OpenWorld, false)
49+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
4950

5051
const DeviceOpenTool = Tool.make("device_open", {
5152
description:
@@ -66,7 +67,8 @@ const DeviceOpenTool = Tool.make("device_open", {
6667
.annotate(Tool.Readonly, false)
6768
.annotate(Tool.Destructive, false)
6869
.annotate(Tool.Idempotent, true)
69-
.annotate(Tool.OpenWorld, true);
70+
.annotate(Tool.OpenWorld, true)
71+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
7072

7173
export const DeviceScreenshotTool = Tool.make("device_screenshot", {
7274
description:
@@ -80,7 +82,8 @@ export const DeviceScreenshotTool = Tool.make("device_screenshot", {
8082
.annotate(Tool.Readonly, true)
8183
.annotate(Tool.Destructive, false)
8284
.annotate(Tool.Idempotent, true)
83-
.annotate(Tool.OpenWorld, true);
85+
.annotate(Tool.OpenWorld, true)
86+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
8487

8588
const DeviceCloseTool = Tool.make("device_close", {
8689
description:
@@ -94,7 +97,8 @@ const DeviceCloseTool = Tool.make("device_close", {
9497
.annotate(Tool.Readonly, false)
9598
.annotate(Tool.Destructive, true)
9699
.annotate(Tool.Idempotent, true)
97-
.annotate(Tool.OpenWorld, true);
100+
.annotate(Tool.OpenWorld, true)
101+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
98102

99103
export const DeviceStandardToolkit = Toolkit.make(DeviceListTool, DeviceOpenTool, DeviceCloseTool);
100104

‎apps/server/src/mcp/toolkits/html/tools.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
HTML_RENDER_TOOL_NAME,
1111
} from "@t3tools/shared/htmlRender";
1212
import * as Schema from "effect/Schema";
13-
import { Tool, Toolkit } from "effect/ai";
13+
import { McpSchema, Tool, Toolkit } from "effect/ai";
1414

1515
import * as HtmlRender from "../../../htmlRender/HtmlRender.ts";
1616
import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
@@ -72,7 +72,8 @@ export const HtmlPreviewTool = Tool.make("html_preview", {
7272
.annotate(Tool.Readonly, true)
7373
.annotate(Tool.Destructive, false)
7474
.annotate(Tool.Idempotent, true)
75-
.annotate(Tool.OpenWorld, true);
75+
.annotate(Tool.OpenWorld, true)
76+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
7677

7778
// This affects only the caller's thread, not its workspace, so read-only sandboxes and plan mode may use it.
7879
const HtmlRenderTool = Tool.make(HTML_RENDER_TOOL_NAME, {
@@ -109,7 +110,8 @@ const HtmlRenderTool = Tool.make(HTML_RENDER_TOOL_NAME, {
109110
.annotate(Tool.Readonly, true)
110111
.annotate(Tool.Destructive, false)
111112
.annotate(Tool.Idempotent, false)
112-
.annotate(Tool.OpenWorld, true);
113+
.annotate(Tool.OpenWorld, true)
114+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
113115

114116
export const HtmlPreviewToolkit = Toolkit.make(HtmlPreviewTool);
115117

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ const DelegateTaskTool = Tool.make("delegate_task", {
9494
})
9595
.annotate(Tool.Title, "Delegate a child task")
9696
.annotate(Tool.Destructive, true)
97-
.annotate(Tool.OpenWorld, true);
97+
.annotate(Tool.OpenWorld, true)
98+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
9899

99100
const TaskStatusTool = Tool.make("task_status", {
100101
description:
@@ -108,7 +109,8 @@ const TaskStatusTool = Tool.make("task_status", {
108109
.annotate(Tool.Title, "Get delegated task status")
109110
.annotate(Tool.Readonly, false)
110111
.annotate(Tool.Destructive, false)
111-
.annotate(Tool.Idempotent, true);
112+
.annotate(Tool.Idempotent, true)
113+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
112114

113115
const TaskCancelTool = Tool.make("task_cancel", {
114116
description:
@@ -120,7 +122,8 @@ const TaskCancelTool = Tool.make("task_cancel", {
120122
dependencies,
121123
})
122124
.annotate(Tool.Title, "Cancel delegated task")
123-
.annotate(Tool.Destructive, true);
125+
.annotate(Tool.Destructive, true)
126+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
124127

125128
const WorkflowStopTool = Tool.make("t3_workflow_stop", {
126129
description:
@@ -154,7 +157,9 @@ const BackgroundWaitTool = Tool.make("t3_background_wait", {
154157
failure: OrchestratorMcpFailure,
155158
failureMode: "return",
156159
dependencies,
157-
}).annotate(Tool.Title, "Wait on background work");
160+
})
161+
.annotate(Tool.Title, "Wait on background work")
162+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
158163

159164
const ScheduleTaskTool = Tool.make("schedule_task", {
160165
description:
@@ -207,7 +212,8 @@ const RequestSecretTool = Tool.make("request_secret", {
207212
.annotate(Tool.Title, "Request a secret from the user")
208213
.annotate(Tool.Destructive, false)
209214
// Claude Code defers MCP tools behind ToolSearch; agents that never load this one ask in chat.
210-
.annotate(Tool.Meta, { "anthropic/alwaysLoad": true });
215+
.annotate(Tool.Meta, { "anthropic/alwaysLoad": true })
216+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
211217

212218
const CreateThreadsTool = Tool.make("create_threads", {
213219
description:
@@ -220,7 +226,8 @@ const CreateThreadsTool = Tool.make("create_threads", {
220226
})
221227
.annotate(Tool.Title, "Create T3 threads")
222228
.annotate(Tool.Destructive, true)
223-
.annotate(Tool.OpenWorld, true);
229+
.annotate(Tool.OpenWorld, true)
230+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
224231

225232
const ThreadListTool = Tool.make("t3_thread_list", {
226233
description:

‎apps/server/src/mcp/toolkits/preview/tools.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from "@t3tools/contracts";
2323
import * as Schema from "effect/Schema";
2424
import * as FileSystem from "effect/FileSystem";
25-
import { Tool, Toolkit } from "effect/ai";
25+
import { McpSchema, Tool, Toolkit } from "effect/ai";
2626

2727
import * as McpInvocationContext from "../../McpInvocationContext.ts";
2828
import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
@@ -42,10 +42,16 @@ const presentationFields = { toolIcon: Schema.optional(ToolActivityIcon) };
4242
const PreviewActionResult = Schema.Struct(presentationFields);
4343

4444
const browserTool = <T extends Tool.Any>(tool: T): T =>
45-
tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T;
45+
tool
46+
.annotate(Tool.OpenWorld, true)
47+
.annotate(Tool.Destructive, true)
48+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients) as T;
4649

4750
const safeBrowserTool = <T extends Tool.Any>(tool: T): T =>
48-
tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, false) as T;
51+
tool
52+
.annotate(Tool.OpenWorld, true)
53+
.annotate(Tool.Destructive, false)
54+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients) as T;
4955

5056
const readonlyBrowserTool = <T extends Tool.Any>(tool: T): T =>
5157
safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T;
@@ -61,7 +67,8 @@ const PreviewStatusTool = Tool.make("preview_status", {
6167
.annotate(Tool.Title, "Get preview status")
6268
.annotate(Tool.Readonly, true)
6369
.annotate(Tool.Destructive, false)
64-
.annotate(Tool.Idempotent, true);
70+
.annotate(Tool.Idempotent, true)
71+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
6572

6673
// `url` already covers websites, so open only adds the dev-server target.
6774
const EnvironmentPortTarget = BrowserNavigationTarget.members[1];

0 commit comments

Comments
 (0)