Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6
7import {
8 PI_T3_MCP_EXTENSION_FILENAME,
9 T3_MCP_BEARER_ENV,
10 T3_MCP_URL_ENV,
11 T3_PI_RUNTIME_MODE_ENV,
12} from "./mcpExtensionSource.ts";
13import {
14 buildPiRpcLaunch,
15 materializePiT3McpExtension,
16 resolvePiLaunchArgs,
17} from "./mcpInjection.ts";
18
19const threadId = ThreadId.make("thread-pi-t3-mcp");
20
21const mcpSession = {
22 environmentId: EnvironmentId.make("environment-pi-t3-mcp"),
23 threadId,
24 providerSessionId: "mcp-session-pi",
25 providerInstanceId: ProviderInstanceId.make("pi"),
26 endpoint: "http://127.0.0.1:43123/mcp",
27 authorizationHeader: "Bearer secret-pi-token",
28 browserToolsAvailable: true,
29};
30
31describe("pi T3 MCP injection", () => {
32 it("always adds the permission bridge and configures MCP when available", () => {
33 const resolvedArgs = resolvePiLaunchArgs(
34 "--extension=/home/user/.pi/agent/extensions/demo.ts --session-dir=/tmp/pi-sessions --provider=anthropic --model=claude-sonnet --tools='' --name=-review --extension-flag=kept",
35 );
36 assert.isTrue(resolvedArgs.ok);
37 if (!resolvedArgs.ok) return;
38 const launch = buildPiRpcLaunch({
39 launchArgs: resolvedArgs.args,
40 environment: { PATH: "/usr/bin" },
41 mcpSession,
42 extensionPath: "/tmp/cache/pi-t3-mcp-extension.ts",
43 runtimeMode: "approval-required",
44 });
45 assert.deepEqual(launch.args, [
46 "--mode",
47 "rpc",
48 "--extension",
49 "/home/user/.pi/agent/extensions/demo.ts",
50 "--session-dir",
51 "/tmp/pi-sessions",
52 "--provider",
53 "anthropic",
54 "--model",
55 "claude-sonnet",
56 "--tools",
57 "",
58 "--name",
59 "-review",
60 "--extension-flag=kept",
61 "--extension",
62 "/tmp/cache/pi-t3-mcp-extension.ts",
63 ]);
64 assert.notInclude(launch.args, "--no-extensions");
65 assert.equal(launch.env[T3_MCP_URL_ENV], "http://127.0.0.1:43123/mcp");
66 assert.equal(launch.env[T3_MCP_BEARER_ENV], "secret-pi-token");
67 assert.equal(launch.env[T3_PI_RUNTIME_MODE_ENV], "approval-required");
68
69 const permissionOnly = buildPiRpcLaunch({
70 launchArgs: [],
71 environment: {
72 [T3_MCP_URL_ENV]: "http://127.0.0.1:9999/stale",
73 [T3_MCP_BEARER_ENV]: "stale-token",
74 },
75 mcpSession: undefined,
76 extensionPath: "/tmp/cache/pi-t3-mcp-extension.ts",
77 runtimeMode: "auto-accept-edits",
78 });
79 assert.deepEqual(permissionOnly.args, [
80 "--mode",
81 "rpc",
82 "--extension",
83 "/tmp/cache/pi-t3-mcp-extension.ts",
84 ]);
85 assert.isFalse(permissionOnly.hasT3Mcp);
86 assert.isUndefined(permissionOnly.env[T3_MCP_URL_ENV]);
87 assert.isUndefined(permissionOnly.env[T3_MCP_BEARER_ENV]);
88 assert.equal(permissionOnly.env[T3_PI_RUNTIME_MODE_ENV], "auto-accept-edits");
89 });
90
91 it("falls back to Pi's first supported mode for legacy auto threads", () => {
92 const launch = buildPiRpcLaunch({
93 launchArgs: [],
94 environment: {},
95 mcpSession: undefined,
96 extensionPath: "/tmp/cache/pi-t3-mcp-extension.ts",
97 runtimeMode: "auto",
98 });
99
100 assert.equal(launch.env[T3_PI_RUNTIME_MODE_ENV], "approval-required");
101 });
102
103 it("forces tools and user extensions off for unattended text generation", () => {
104 const launch = buildPiRpcLaunch({
105 launchArgs: [
106 "--tools",
107 "read,write",
108 "--extension",
109 "/home/user/.pi/agent/extensions/demo.ts",
110 "--extension=./second.ts",
111 "--provider",
112 "anthropic",
113 ],
114 environment: {},
115 mcpSession,
116 extensionPath: "/tmp/cache/pi-t3-mcp-extension.ts",
117 ephemeral: true,
118 disableExtensions: true,
119 disableTools: true,
120 });
121 assert.deepEqual(launch.args, [
122 "--mode",
123 "rpc",
124 "--no-session",
125 "--provider",
126 "anthropic",
127 "--no-extensions",
128 "--no-tools",
129 ]);
130 assert.isFalse(launch.hasT3Mcp);
131 assert.deepInclude(resolvePiLaunchArgs("--mode text"), {
132 ok: false,
133 message: "Pi launch argument '--mode' is controlled by T3 Code and cannot be overridden.",
134 });
135 assert.deepInclude(resolvePiLaunchArgs("--session old.jsonl"), { ok: false });
136 assert.deepInclude(resolvePiLaunchArgs("prompt pi immediately"), { ok: false });
137 assert.deepInclude(resolvePiLaunchArgs("--plan @instructions.md"), { ok: false });
138 });
139
140 it("rejects --provider without --model, which Pi 1.0 refuses at startup", () => {
141 const rejection = {
142 ok: false,
143 message: "Pi launch argument '--provider' requires '--model'.",
144 };
145 assert.deepInclude(resolvePiLaunchArgs("--provider openrouter"), rejection);
146 assert.deepInclude(resolvePiLaunchArgs("--provider=openrouter --models gpt-6"), rejection);
147 assert.isTrue(resolvePiLaunchArgs("--provider openrouter --model=deepseek/v4").ok);
148 assert.isTrue(resolvePiLaunchArgs("--model deepseek/v4").ok);
149 });
150
151 it.effect("materializes the MCP bridge with namespaced tool registration", () =>
152 Effect.gen(function* () {
153 const fs = yield* FileSystem.FileSystem;
154 const cacheDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pi-extensions-" });
155 const mcpDest = yield* materializePiT3McpExtension(cacheDir);
156 assert.isTrue(mcpDest.endsWith(PI_T3_MCP_EXTENSION_FILENAME));
157 const mcpSource = yield* fs.readFileString(mcpDest);
158 assert.include(mcpSource, "export default async function t3McpExtension");
159 assert.include(mcpSource, "before_agent_start");
160 assert.include(mcpSource, 'pi.on("tool_call"');
161 assert.include(mcpSource, "Allow ${event.toolName}?");
162 assert.include(mcpSource, '"mcp-protocol-version"');
163 assert.include(mcpSource, '"tools/call"');
164 assert.include(mcpSource, "mcp__t3-code__");
165 assert.include(mcpSource, "mcp__t3_code__");
166 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
167 );
168});