Skip to content

Commit f28c74a

Browse files
RVZO6claudemaria-rcksjuliusmarminge
authored
fix(server): outside MCP clients stop being offered thread-bound tools (#32764)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 94edd64 commit f28c74a

14 files changed

Lines changed: 137 additions & 40 deletions

File tree

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,13 +604,11 @@ it.effect("refuses preview tools to a client outside a thread before they run",
604604
const click = yield* server
605605
.callTool({ name: "preview_act", arguments: { action: "click", locator: "text=Send" } })
606606
.pipe(
607+
Effect.flip,
607608
Effect.provideService(McpInvocationContext.McpInvocationContext, readOnly),
608609
Effect.provideService(McpSchema.McpServerClient, client),
609610
);
610-
expect(click.isError).toBe(true);
611-
expect(click.content).toEqual([
612-
{ type: "text", text: expect.stringContaining("needs an agent running inside T3 Code") },
613-
]);
611+
expect(click.message).toContain("Tool 'preview_act' not found");
614612
}).pipe(Effect.provide(layerTest)),
615613
);
616614

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

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

183+
/**
184+
* `McpSchema.EnabledWhen` for tools that act as the calling thread: a client
185+
* from outside has none. Discovery only; `McpToolAccess` still refuses the
186+
* call, and `toLayer` requires this on every thread-bound declaration.
187+
*/
188+
export const hiddenFromOutsideClients = (): boolean => {
189+
const fiber = Fiber.getCurrent();
190+
const scope = fiber && Context.getOrUndefined(fiber.context, McpInvocationContext);
191+
return scope === undefined || scope.thread !== undefined;
192+
};
193+
183194
/** `McpSchema.EnabledWhen` for tools that only serve links, hidden while the beta is off. */
184195
export const hiddenWhileLinksOff = (): boolean => {
185196
const fiber = Fiber.getCurrent();

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

Lines changed: 49 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,36 @@ 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("refuses to build a thread-bound tool that outside clients would be offered", () =>
499+
Effect.gen(function* () {
500+
const CallerToolkit = Toolkit.make(Tool.make("reads_as_caller", probe));
501+
const built = yield* Layer.build(
502+
McpToolAccess.HandlersLayer.layer(
503+
McpToolAccess.toLayer(CallerToolkit, {
504+
reads_as_caller: McpToolAccess.readsAsCaller(() => ran),
505+
}),
506+
),
507+
).pipe(Effect.scoped, Effect.exit);
508+
expect(Exit.isFailure(built) && Cause.pretty(built.cause)).toContain(
509+
"reads_as_caller acts as its calling thread",
510+
);
511+
}),
512+
);

‎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 {
@@ -420,8 +422,17 @@ const routed = Effect.fn("McpToolAccess.routed")(function* <Tools extends Record
420422
const handlers: Record<string, unknown> = { ...checked };
421423
for (const [name, declaration] of Object.entries<Declaration<unknown>>(declarations)) {
422424
const tool = toolkit.tools[name];
423-
if (tool === undefined || !takesLinkedEnvironment(tool)) continue;
425+
if (tool === undefined) continue;
424426
const remote = remoteCheckOf(declaration);
427+
if (
428+
remote === undefined &&
429+
Context.getOrUndefined(tool.annotations, McpSchema.EnabledWhen) !==
430+
McpInvocationContext.hiddenFromOutsideClients
431+
)
432+
return yield* Effect.die(
433+
`${name} acts as its calling thread, so outside clients must not be offered it.`,
434+
);
435+
if (!takesLinkedEnvironment(tool)) continue;
425436
const forwarded = forwardedOf(declaration) as
426437
| Forwarded<object, unknown, Tool.Failure<Tools[keyof Tools]>, never>
427438
| undefined;

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

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

967-
it.effect("refuses act-as-caller tools to a client caller", () =>
967+
it.effect("hides act-as-caller tools from a client caller", () =>
968968
Effect.gen(function* () {
969969
const server = yield* McpServer.McpServer;
970-
const result = yield* server
970+
const error = yield* server
971971
.callTool({
972972
name: "delegate_task",
973973
arguments: { task: "Review", mode: "async" },
974974
})
975975
.pipe(
976+
Effect.flip,
976977
Effect.provideService(
977978
McpInvocationContext.McpInvocationContext,
978979
clientScope("full-access"),
979980
),
980981
Effect.provideService(McpSchema.McpServerClient, client),
981982
);
982-
expect(declaredFailure(result)).toMatchObject({ code: "thread_credential_required" });
983+
expect(error.message).toContain("Tool 'delegate_task' not found");
983984
}).pipe(
984985
Effect.provide(
985986
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/message/tools.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { OrchestratorMcpFailure } from "@t3tools/contracts";
22
import { SEND_TO_USER_MAX_LENGTH, SEND_TO_USER_TOOL_NAME } from "@t3tools/shared/toolOutput";
33
import * as Schema from "effect/Schema";
4-
import { Tool, Toolkit } from "effect/ai";
4+
import { McpSchema, Tool, Toolkit } from "effect/ai";
55

66
import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
77
import * as McpInvocationContext from "../../McpInvocationContext.ts";
@@ -33,6 +33,7 @@ const SendToUserTool = Tool.make(SEND_TO_USER_TOOL_NAME, {
3333
.annotate(Tool.Destructive, false)
3434
.annotate(Tool.Idempotent, false)
3535
.annotate(Tool.OpenWorld, false)
36+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients)
3637
// Claude Code defers MCP tools behind ToolSearch; Anthropic says to declare this from the first request.
3738
.annotate(Tool.Meta, { "anthropic/alwaysLoad": true });
3839

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ const DelegateTaskTool = Tool.make("delegate_task", {
9797
})
9898
.annotate(Tool.Title, "Delegate a child task")
9999
.annotate(Tool.Destructive, true)
100-
.annotate(Tool.OpenWorld, true);
100+
.annotate(Tool.OpenWorld, true)
101+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
101102

102103
const TaskStatusTool = Tool.make("task_status", {
103104
description:
@@ -111,7 +112,8 @@ const TaskStatusTool = Tool.make("task_status", {
111112
.annotate(Tool.Title, "Get delegated task status")
112113
.annotate(Tool.Readonly, false)
113114
.annotate(Tool.Destructive, false)
114-
.annotate(Tool.Idempotent, true);
115+
.annotate(Tool.Idempotent, true)
116+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
115117

116118
const TaskCancelTool = Tool.make("task_cancel", {
117119
description:
@@ -123,7 +125,8 @@ const TaskCancelTool = Tool.make("task_cancel", {
123125
dependencies,
124126
})
125127
.annotate(Tool.Title, "Cancel delegated task")
126-
.annotate(Tool.Destructive, true);
128+
.annotate(Tool.Destructive, true)
129+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
127130

128131
const WorkflowStopTool = Tool.make("t3_workflow_stop", {
129132
description:
@@ -161,7 +164,8 @@ const BackgroundWaitTool = Tool.make("t3_background_wait", {
161164
.annotate(Tool.Title, "Wait on background work")
162165
// Only marks which tasks keep the thread Working; Claude Plan mode blocks tools without this.
163166
.annotate(Tool.Readonly, true)
164-
.annotate(Tool.Destructive, false);
167+
.annotate(Tool.Destructive, false)
168+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
165169

166170
const ScheduleTaskTool = Tool.make("schedule_task", {
167171
description:
@@ -214,7 +218,8 @@ const RequestSecretTool = Tool.make("request_secret", {
214218
.annotate(Tool.Title, "Request a secret from the user")
215219
.annotate(Tool.Destructive, false)
216220
// Claude Code defers MCP tools behind ToolSearch; agents that never load this one ask in chat.
217-
.annotate(Tool.Meta, { "anthropic/alwaysLoad": true });
221+
.annotate(Tool.Meta, { "anthropic/alwaysLoad": true })
222+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
218223

219224
const CreateThreadsTool = Tool.make("create_threads", {
220225
description:
@@ -227,7 +232,8 @@ const CreateThreadsTool = Tool.make("create_threads", {
227232
})
228233
.annotate(Tool.Title, "Create T3 threads")
229234
.annotate(Tool.Destructive, true)
230-
.annotate(Tool.OpenWorld, true);
235+
.annotate(Tool.OpenWorld, true)
236+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
231237

232238
const ThreadListTool = Tool.make("t3_thread_list", {
233239
description:

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

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

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

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

4851
const safeBrowserTool = <T extends Tool.Any>(tool: T): T =>
49-
tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, false) as T;
52+
tool
53+
.annotate(Tool.OpenWorld, true)
54+
.annotate(Tool.Destructive, false)
55+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients) as T;
5056

5157
const readonlyBrowserTool = <T extends Tool.Any>(tool: T): T =>
5258
safeBrowserTool(tool).annotate(Tool.Readonly, true).annotate(Tool.Idempotent, true) as T;
@@ -62,7 +68,8 @@ const PreviewStatusTool = Tool.make("preview_status", {
6268
.annotate(Tool.Title, "Get preview status")
6369
.annotate(Tool.Readonly, true)
6470
.annotate(Tool.Destructive, false)
65-
.annotate(Tool.Idempotent, true);
71+
.annotate(Tool.Idempotent, true)
72+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
6673

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

0 commit comments

Comments
 (0)