Skip to content

Commit af397af

Browse files
feat(server): PR tools are listed only where the thread's project is on a supported forge
A project with no remote, or on a host T3 has no provider for, could only get errors from the forge tools. The MCP middleware now asks PullRequestService.servesProject for the calling thread's project (cached for a minute) and hides those tools when the answer is no. The PR instruction now applies when the tools are listed, so an agent without them neither searches for them nor tells the user a tool is missing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 177cd80 commit af397af

19 files changed

Lines changed: 242 additions & 134 deletions

File tree

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

Lines changed: 113 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
OrchestratorMcpEnvironmentTarget,
1313
OrchestratorMcpFailure,
1414
PreviewTabId,
15+
ProjectId,
1516
ProviderInstanceId,
1617
ProviderInteractionMode,
1718
RuntimeMode,
@@ -1510,96 +1511,122 @@ it.effect("hides tools for links and refuses another environment while the beta
15101511
).pipe(Effect.provide(NodeHttpServer.layerTest)),
15111512
);
15121513

1513-
it.effect("lists pull request watch tools only to threads that are not delegated children", () =>
1514-
Effect.scoped(
1515-
Effect.gen(function* () {
1516-
const scopeFor = (
1517-
token: string,
1518-
subagent: boolean,
1519-
): McpInvocationContext.McpThreadInvocationScope => ({
1520-
environmentId,
1521-
requestNamespace: token,
1522-
thread: {
1523-
threadId: ThreadId.make(`thread-${token}`),
1524-
providerSessionId: token,
1525-
providerInstanceId: ProviderInstanceId.make("codex"),
1526-
subagent,
1527-
},
1528-
client: undefined,
1529-
capabilities: new Set(["pull-requests"]),
1530-
issuedAt: 1,
1531-
});
1532-
const scopes = new Map([
1533-
["parentToken", scopeFor("parentToken", false)],
1534-
["childToken", scopeFor("childToken", true)],
1535-
]);
1536-
const serverLayer = McpHttpServer.layerMcpTransport(
1537-
McpHttpServer.layerPullRequestsToolkit,
1538-
).pipe(
1539-
Layer.provide(ServerSettings.layerTest()),
1540-
Layer.provide(
1541-
Layer.mock(McpSessionRegistry.McpSessionRegistry)({
1542-
resolve: (token) => Effect.succeed(scopes.get(token)),
1543-
}),
1544-
),
1545-
Layer.provide(
1546-
Layer.mergeAll(
1547-
McpToolAccessTestkit.liveThreadsLayer,
1548-
Layer.mock(ProjectService.ProjectService)({}),
1549-
Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({}),
1550-
Layer.mock(PullRequestService.PullRequestService)({}),
1551-
Layer.mock(GitManager.GitManager)({}),
1552-
Layer.mock(Orchestrator.OrchestratorV2)({}),
1553-
Layer.mock(ProjectionStore.ProjectionStoreV2)({}),
1554-
NodeServices.layer,
1555-
NodeCrypto.layer,
1514+
it.effect(
1515+
"lists watch tools only to threads that are not delegated children, and forge tools only where the project has a forge",
1516+
() =>
1517+
Effect.scoped(
1518+
Effect.gen(function* () {
1519+
const scopeFor = (
1520+
token: string,
1521+
subagent: boolean,
1522+
): McpInvocationContext.McpThreadInvocationScope => ({
1523+
environmentId,
1524+
requestNamespace: token,
1525+
thread: {
1526+
threadId: ThreadId.make(`thread-${token}`),
1527+
providerSessionId: token,
1528+
providerInstanceId: ProviderInstanceId.make("codex"),
1529+
subagent,
1530+
},
1531+
client: undefined,
1532+
capabilities: new Set(["pull-requests"]),
1533+
issuedAt: 1,
1534+
});
1535+
const scopes = new Map([
1536+
["parentToken", scopeFor("parentToken", false)],
1537+
["childToken", scopeFor("childToken", true)],
1538+
["noForgeToken", scopeFor("noForgeToken", false)],
1539+
]);
1540+
const serverLayer = McpHttpServer.layerMcpTransport(
1541+
McpHttpServer.layerPullRequestsToolkit,
1542+
).pipe(
1543+
Layer.provide(ServerSettings.layerTest()),
1544+
Layer.provide(
1545+
Layer.mock(McpSessionRegistry.McpSessionRegistry)({
1546+
resolve: (token) => Effect.succeed(scopes.get(token)),
1547+
}),
15561548
),
1557-
),
1558-
);
1559-
yield* HttpRouter.serve(serverLayer, { disableListenLog: true, disableLogger: true }).pipe(
1560-
Layer.build,
1561-
);
1562-
const httpClient = yield* HttpClient.HttpClient;
1563-
const listedTools = (token: string) =>
1564-
Effect.gen(function* () {
1565-
const headers = {
1566-
accept: "application/json, text/event-stream",
1567-
authorization: `Bearer ${token}`,
1568-
};
1569-
const initialize = yield* httpClient.post("/mcp", {
1570-
headers,
1571-
body: HttpBody.text(
1572-
`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"probe","version":"1.0.0"}}}`,
1573-
"application/json",
1574-
),
1575-
});
1576-
const listed = yield* httpClient.post("/mcp", {
1577-
headers: {
1578-
...headers,
1579-
"mcp-session-id": initialize.headers["mcp-session-id"]!,
1580-
"mcp-protocol-version": "2025-06-18",
1581-
},
1582-
body: HttpBody.text(
1583-
`{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}`,
1584-
"application/json",
1549+
Layer.provide(
1550+
Layer.mergeAll(
1551+
McpToolAccessTestkit.liveThreadsLayer,
1552+
Layer.mock(ProjectService.ProjectService)({}),
1553+
Layer.mock(PullRequestReviewGuideService.PullRequestReviewGuideService)({}),
1554+
Layer.mock(PullRequestService.PullRequestService)({
1555+
servesProject: (projectId) => Effect.succeed(projectId !== "project-noForgeToken"),
1556+
}),
1557+
Layer.mock(GitManager.GitManager)({}),
1558+
Layer.mock(Orchestrator.OrchestratorV2)({}),
1559+
Layer.mock(ProjectionStore.ProjectionStoreV2)({
1560+
getThreadShell: (threadId) =>
1561+
Effect.succeed({
1562+
projectId: ProjectId.make(threadId.replace("thread-", "project-")),
1563+
} as never),
1564+
}),
1565+
NodeServices.layer,
1566+
NodeCrypto.layer,
15851567
),
1568+
),
1569+
);
1570+
yield* HttpRouter.serve(serverLayer, { disableListenLog: true, disableLogger: true }).pipe(
1571+
Layer.build,
1572+
);
1573+
const httpClient = yield* HttpClient.HttpClient;
1574+
const listedTools = (token: string) =>
1575+
Effect.gen(function* () {
1576+
const headers = {
1577+
accept: "application/json, text/event-stream",
1578+
authorization: `Bearer ${token}`,
1579+
};
1580+
const initialize = yield* httpClient.post("/mcp", {
1581+
headers,
1582+
body: HttpBody.text(
1583+
`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"probe","version":"1.0.0"}}}`,
1584+
"application/json",
1585+
),
1586+
});
1587+
const listed = yield* httpClient.post("/mcp", {
1588+
headers: {
1589+
...headers,
1590+
"mcp-session-id": initialize.headers["mcp-session-id"]!,
1591+
"mcp-protocol-version": "2025-06-18",
1592+
},
1593+
body: HttpBody.text(
1594+
`{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}`,
1595+
"application/json",
1596+
),
1597+
});
1598+
const body = (yield* listed.json) as {
1599+
readonly result: { readonly tools: ReadonlyArray<{ readonly name: string }> };
1600+
};
1601+
return body.result.tools.map((tool) => tool.name);
15861602
});
1587-
const body = (yield* listed.json) as {
1588-
readonly result: { readonly tools: ReadonlyArray<{ readonly name: string }> };
1589-
};
1590-
return body.result.tools.map((tool) => tool.name);
1591-
});
15921603

1593-
const parentTools = yield* listedTools("parentToken");
1594-
const childTools = yield* listedTools("childToken");
1595-
expect(parentTools).toEqual(
1596-
expect.arrayContaining(["link_pull_request", "watch_pull_request", "unwatch_pull_request"]),
1597-
);
1598-
expect(childTools).toContain("link_pull_request");
1599-
expect(childTools).not.toContain("watch_pull_request");
1600-
expect(childTools).not.toContain("unwatch_pull_request");
1601-
}),
1602-
).pipe(Effect.provide(NodeHttpServer.layerTest)),
1604+
const parentTools = yield* listedTools("parentToken");
1605+
const childTools = yield* listedTools("childToken");
1606+
expect(parentTools).toEqual(
1607+
expect.arrayContaining([
1608+
"link_pull_request",
1609+
"watch_pull_request",
1610+
"unwatch_pull_request",
1611+
]),
1612+
);
1613+
expect(childTools).toContain("link_pull_request");
1614+
expect(childTools).not.toContain("watch_pull_request");
1615+
expect(childTools).not.toContain("unwatch_pull_request");
1616+
expect(parentTools).toEqual(
1617+
expect.arrayContaining([
1618+
"pull_request_read",
1619+
"pull_request_review",
1620+
"pull_request_merge",
1621+
]),
1622+
);
1623+
const noForgeTools = yield* listedTools("noForgeToken");
1624+
expect(noForgeTools).toEqual(
1625+
expect.arrayContaining(["link_pull_request", "watch_pull_request"]),
1626+
);
1627+
expect(noForgeTools.filter((name) => name.startsWith("pull_request_"))).toEqual([]);
1628+
}),
1629+
).pipe(Effect.provide(NodeHttpServer.layerTest)),
16031630
);
16041631

16051632
it.effect(

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import * as Cache from "effect/Cache";
12
import * as Cause from "effect/Cause";
23
import * as Clock from "effect/Clock";
34
import * as Context from "effect/Context";
5+
import * as Duration from "effect/Duration";
46
import * as SecureRandom from "effect/crypto/SecureRandom";
57
import * as Effect from "effect/Effect";
68
import * as FileSystem from "effect/FileSystem";
@@ -33,6 +35,8 @@ import * as DeviceService from "../device/DeviceService.ts";
3335
import * as HtmlRender from "../htmlRender/HtmlRender.ts";
3436
import * as ThreadCommandExecutor from "../orchestration-v2/ThreadCommandExecutor.ts";
3537
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
38+
import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
39+
import * as PullRequestService from "../pullRequest/PullRequestService.ts";
3640
import * as PullRequestWatchService from "../pullRequest/PullRequestWatchService.ts";
3741
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
3842
import * as AnalyticsService from "../telemetry/AnalyticsService.ts";
@@ -238,6 +242,24 @@ const makeMcpAuthMiddleware = Effect.gen(function* () {
238242
Effect.map((settings) => settings.linkedEnvironmentsEnabled),
239243
Effect.orElseSucceed(() => false),
240244
);
245+
const pullRequests = yield* Effect.serviceOption(PullRequestService.PullRequestService);
246+
const projections = yield* Effect.serviceOption(ProjectionStore.ProjectionStoreV2);
247+
// Every request reads it, and a remote added mid-thread shows the tools within a minute.
248+
const forgeOfThread = yield* Cache.make({
249+
capacity: 1_000,
250+
timeToLive: Duration.minutes(1),
251+
lookup: (threadId: ThreadId): Effect.Effect<boolean | undefined> =>
252+
Option.isNone(pullRequests) || Option.isNone(projections)
253+
? Effect.undefined
254+
: projections.value.getThreadShell(threadId).pipe(
255+
Effect.flatMap((thread) =>
256+
thread === null
257+
? Effect.undefined
258+
: pullRequests.value.servesProject(thread.projectId),
259+
),
260+
Effect.orElseSucceed(() => undefined),
261+
),
262+
});
241263
const authenticateRequest = Effect.fn("McpHttpServer.authenticateRequest")(function* (
242264
request: HttpServerRequest.HttpServerRequest,
243265
) {
@@ -270,7 +292,11 @@ const makeMcpAuthMiddleware = Effect.gen(function* () {
270292
const limited = McpInvocationContext.withRuntimeModeCeiling(asked, runtimeModeCeiling);
271293
const linkedEnvironments = yield* linksEnabled;
272294
if (limited.client?.linked === true && !linkedEnvironments) return linkedEnvironmentsOff;
273-
return { ...limited, linkedEnvironments };
295+
const forge =
296+
limited.thread === undefined
297+
? undefined
298+
: yield* Cache.get(forgeOfThread, limited.thread.threadId);
299+
return { ...limited, linkedEnvironments, ...(forge === undefined ? {} : { forge }) };
274300
});
275301
// Untraced: a span here would wrap the tool call; the HTTP server span already covers it.
276302
return Effect.fnUntraced(function* (httpEffect) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ export interface McpInvocationScope {
141141
readonly client: McpClientCaller | undefined;
142142
/** False while the Linked environments beta is off here; `/mcp` reads it per request. */
143143
readonly linkedEnvironments?: boolean;
144+
/** False when the calling thread's project is on no forge T3 reads; `/mcp` reads it per request. */
145+
readonly forge?: boolean;
144146
}
145147

146148
/**
@@ -182,6 +184,13 @@ export const hiddenWhileLinksOff = (): boolean => {
182184
return scope?.linkedEnvironments !== false;
183185
};
184186

187+
/** `McpSchema.EnabledWhen` for tools that act on the thread's forge, hidden where it has none. */
188+
export const hiddenWithoutForge = (): boolean => {
189+
const fiber = Fiber.getCurrent();
190+
const scope = fiber && Context.getOrUndefined(fiber.context, McpInvocationContext);
191+
return scope?.forge !== false;
192+
};
193+
185194
export type McpCapabilityError<C extends McpCapability> = C extends "preview"
186195
? PreviewAutomationUnavailableError
187196
: McpCapabilityUnavailableError;

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ const ReadPullRequestTool = Tool.make("pull_request_read", {
613613
.annotate(Tool.Readonly, true)
614614
.annotate(Tool.Destructive, false)
615615
.annotate(Tool.Idempotent, true)
616-
.annotate(Tool.OpenWorld, true);
616+
.annotate(Tool.OpenWorld, true)
617+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
617618

618619
const ListPullRequestsTool = Tool.make("pull_request_list", {
619620
description: "List PRs in this thread's repository, recently updated first.",
@@ -633,7 +634,8 @@ const ListPullRequestsTool = Tool.make("pull_request_list", {
633634
.annotate(Tool.Readonly, true)
634635
.annotate(Tool.Destructive, false)
635636
.annotate(Tool.Idempotent, true)
636-
.annotate(Tool.OpenWorld, true);
637+
.annotate(Tool.OpenWorld, true)
638+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
637639

638640
const CreatePullRequestTool = Tool.make("pull_request_create", {
639641
description: "Open a PR from this thread's pushed branch and link it, or return its open PR.",
@@ -652,7 +654,8 @@ const CreatePullRequestTool = Tool.make("pull_request_create", {
652654
.annotate(Tool.Readonly, false)
653655
.annotate(Tool.Destructive, false)
654656
.annotate(Tool.Idempotent, true)
655-
.annotate(Tool.OpenWorld, true);
657+
.annotate(Tool.OpenWorld, true)
658+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
656659

657660
const UpdateNames = Schema.Array(TrimmedNonEmptyString).check(
658661
Schema.isMinLength(1),
@@ -682,7 +685,8 @@ const UpdatePullRequestTool = Tool.make("pull_request_update", {
682685
.annotate(Tool.Readonly, false)
683686
.annotate(Tool.Destructive, false)
684687
.annotate(Tool.Idempotent, true)
685-
.annotate(Tool.OpenWorld, true);
688+
.annotate(Tool.OpenWorld, true)
689+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
686690

687691
const RespondToPullRequestTool = Tool.make("pull_request_respond", {
688692
description:
@@ -701,7 +705,8 @@ const RespondToPullRequestTool = Tool.make("pull_request_respond", {
701705
.annotate(Tool.Readonly, false)
702706
.annotate(Tool.Destructive, false)
703707
.annotate(Tool.Idempotent, false)
704-
.annotate(Tool.OpenWorld, true);
708+
.annotate(Tool.OpenWorld, true)
709+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
705710

706711
export const ReviewLineComment = Schema.Struct({
707712
path: TrimmedNonEmptyString,
@@ -733,7 +738,8 @@ const ReviewPullRequestTool = Tool.make("pull_request_review", {
733738
.annotate(Tool.Readonly, false)
734739
.annotate(Tool.Destructive, false)
735740
.annotate(Tool.Idempotent, false)
736-
.annotate(Tool.OpenWorld, true);
741+
.annotate(Tool.OpenWorld, true)
742+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
737743

738744
const MergePullRequestTool = Tool.make("pull_request_merge", {
739745
description: "Merge a PR. Refuses if the head moved from expectedHeadSha (summary.headSha).",
@@ -755,7 +761,8 @@ const MergePullRequestTool = Tool.make("pull_request_merge", {
755761
.annotate(Tool.Readonly, false)
756762
.annotate(Tool.Destructive, true)
757763
.annotate(Tool.Idempotent, false)
758-
.annotate(Tool.OpenWorld, true);
764+
.annotate(Tool.OpenWorld, true)
765+
.annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenWithoutForge);
759766

760767
export const PullRequestsToolkit = Toolkit.make(
761768
LinkPullRequestTool,

0 commit comments

Comments
 (0)