Skip to content

Commit 2585ace

Browse files
author
rsi-pr-agent
committed
fixup! fix(mcp): hide tools refused by read-only grants (#1742)
Fork-Domain: upstream-fixes Fork-Tier: bugfix Fork-Upstreamable: yes
1 parent 59a7b46 commit 2585ace

1 file changed

Lines changed: 57 additions & 14 deletions

File tree

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

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import {
88
type ThreadId,
99
} from "@t3tools/contracts";
1010
import * as Effect from "effect/Effect";
11+
import * as Context from "effect/Context"; // fork-hook: upstream-fixes/mcp-visibility-import
12+
import * as Fiber from "effect/Fiber"; // fork-hook: upstream-fixes/mcp-visibility-import
13+
import * as HttpServerRequest from "effect/http/HttpServerRequest"; // fork-hook: upstream-fixes/mcp-visibility-import
1114
import * as Option from "effect/Option";
1215
import * as Predicate from "effect/Predicate";
1316
import * as Ref from "effect/Ref";
1417
import * as Struct from "effect/Struct";
15-
import type * as Layer from "effect/Layer";
18+
import * as Layer from "effect/Layer"; // fork-hook: upstream-fixes/mcp-visibility-layer-import
1619
import type * as Scope from "effect/Scope";
1720
import type { Tool, Toolkit } from "effect/ai";
1821

@@ -46,7 +49,8 @@ const refuseOutsideConstruction = (token: symbol) => {
4649
let declare: <P, A, E, R>(
4750
handle: (params: P) => Effect.Effect<A, E, R>,
4851
remote: RemoteCheck<P> | undefined,
49-
) => Declaration<(params: P) => Effect.Effect<A, E, R>>;
52+
writes?: boolean,
53+
) => Declaration<(params: P) => Effect.Effect<A, E, R>>; // fork-hook: upstream-fixes/mcp-visibility-declare
5054

5155
/**
5256
* What a call bound for a linked environment checks here before it goes: the
@@ -67,29 +71,37 @@ interface CheckedHandler extends Struct.Lambda {
6771
let checkedHandler: CheckedHandler;
6872

6973
let handlersLayer: <Tools extends Record<string, Tool.Any>, EX, RX>(
70-
layer: Layer.Layer<Tool.HandlersFor<Tools>, EX, RX>,
71-
) => HandlersLayer<Tools, EX, RX>;
74+
layer: Layer.Layer<Tool.HandlersFor<Tools> | ToolVisibility, EX, RX>,
75+
) => HandlersLayer<Tools, EX, RX>; // fork-hook: upstream-fixes/mcp-visibility-handlers
7276

7377
/** t3code/no-raw-mcp-registration confines Effect's unchecked registration API to McpHttpServer. */
7478
export class Declaration<out Handler> {
7579
readonly #handle: Handler;
7680
readonly #remote: RemoteCheck<never> | undefined;
81+
readonly writes: boolean; // fork-hook: upstream-fixes/mcp-visibility-writes
7782
private constructor(
7883
token: typeof builtHere,
7984
handle: Handler,
8085
remote: RemoteCheck<never> | undefined,
86+
writes: boolean, // fork-hook: upstream-fixes/mcp-visibility-constructor
8187
) {
8288
refuseOutsideConstruction(token);
8389
this.#handle = handle;
8490
this.#remote = remote;
91+
this.writes = writes; // fork-hook: upstream-fixes/mcp-visibility-writes
8592
}
8693
static {
87-
declare = (handle, remote) => new Declaration(builtHere, handle, remote);
94+
declare = (handle, remote, writes = true) => new Declaration(builtHere, handle, remote, writes); // fork-hook: upstream-fixes/mcp-visibility-declare
8895
checkedHandler = Struct.lambda<CheckedHandler>((declaration) => declaration.#handle);
8996
remoteCheckOf = (declaration) => declaration.#remote;
9097
}
9198
}
9299

100+
export class ToolVisibility extends Context.Service<
101+
ToolVisibility,
102+
Readonly<Record<string, () => boolean>>
103+
>()("t3/mcp/McpToolAccess/ToolVisibility") {} // fork-hook: upstream-fixes/mcp-visibility-service
104+
93105
const refuseReadOnlyClient = McpInvocationContext.McpInvocationContext.pipe(
94106
Effect.flatMap((scope) =>
95107
scope.client?.access === "read-only"
@@ -122,10 +134,15 @@ export const reads = <P, A, E, R>(handle: (params: P) => Effect.Effect<A, E, R>)
122134
declare(
123135
(params: P) => handle(params),
124136
() => Effect.void,
125-
);
137+
false,
138+
); // fork-hook: upstream-fixes/mcp-visibility-reads
126139

127140
export const readsAsCaller = <P, A, E, R>(handle: (params: P) => Effect.Effect<A, E, R>) =>
128-
declare((params: P) => requireThreadCaller.pipe(Effect.flatMap(() => handle(params))), undefined);
141+
declare(
142+
(params: P) => requireThreadCaller.pipe(Effect.flatMap(() => handle(params))),
143+
undefined,
144+
false,
145+
); // fork-hook: upstream-fixes/mcp-visibility-reads
129146

130147
export const actsAsCaller = <P, A, E, R>(handle: (params: P) => Effect.Effect<A, E, R>) =>
131148
declare(
@@ -278,7 +295,8 @@ export const either = <P, A, E, R, A2, E2, R2>(
278295
message: "This action acts as the calling thread, which is here. Omit environmentId.",
279296
}),
280297
),
281-
);
298+
first.writes || second.writes,
299+
); // fork-hook: upstream-fixes/mcp-visibility-either
282300
};
283301

284302
const fullAccessRequired =
@@ -329,10 +347,10 @@ export type Handlers<Tools extends Record<string, Tool.Any>> = Declarations<
329347
>;
330348

331349
export class HandlersLayer<Tools extends Record<string, Tool.Any>, EX = never, RX = never> {
332-
readonly #layer: Layer.Layer<Tool.HandlersFor<Tools>, EX, RX>;
350+
readonly #layer: Layer.Layer<Tool.HandlersFor<Tools> | ToolVisibility, EX, RX>; // fork-hook: upstream-fixes/mcp-visibility-handlers
333351
private constructor(
334352
token: typeof builtHere,
335-
layer: Layer.Layer<Tool.HandlersFor<Tools>, EX, RX>,
353+
layer: Layer.Layer<Tool.HandlersFor<Tools> | ToolVisibility, EX, RX>, // fork-hook: upstream-fixes/mcp-visibility-handlers-constructor
336354
) {
337355
refuseOutsideConstruction(token);
338356
this.#layer = layer;
@@ -415,9 +433,34 @@ export const toLayer = <Tools extends Record<string, Tool.Any>, EX = never, RX =
415433
// Checked as soon as they are given, so a forged declaration fails here.
416434
const withDeclarations = (declarations: Handlers<Tools>) =>
417435
routed(toolkit, declarations, checkedHandlers(declarations));
436+
const assemble = (declarations: Handlers<Tools>) =>
437+
Layer.mergeAll(
438+
toolkit.toLayer(withDeclarations(declarations)),
439+
Layer.succeed(
440+
ToolVisibility,
441+
Object.fromEntries(
442+
Object.entries(declarations as Readonly<Record<string, Declaration<unknown>>>).map(
443+
([name, declaration]) => [
444+
name,
445+
() => {
446+
const fiber = Fiber.getCurrent();
447+
const scope =
448+
fiber === undefined ||
449+
Context.getOrUndefined(fiber.context, HttpServerRequest.HttpServerRequest) ===
450+
undefined
451+
? undefined
452+
: Context.getOrUndefined(
453+
fiber.context,
454+
McpInvocationContext.McpInvocationContext,
455+
);
456+
return !declaration.writes || scope?.client?.access !== "read-only";
457+
},
458+
],
459+
),
460+
),
461+
),
462+
); // fork-hook: upstream-fixes/mcp-visibility-assemble
418463
return handlersLayer(
419-
toolkit.toLayer(
420-
Effect.isEffect(build) ? Effect.flatMap(build, withDeclarations) : withDeclarations(build),
421-
),
422-
);
464+
Effect.isEffect(build) ? Layer.unwrap(Effect.map(build, assemble)) : assemble(build),
465+
); // fork-hook: upstream-fixes/mcp-visibility-layer
423466
};

0 commit comments

Comments
 (0)