Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

agentTools.ts

218 lines8.8 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3​
4import {
5 ForwardCompatibleArray,
6 ProjectId,
7 ThreadId,
8 TrimmedNonEmptyString,
9} from "./baseSchemas.ts";
10import { ProviderInstanceId } from "./providerInstance.ts";
11​
12/**
13 * MCP servers and skill switches that T3 applies to every provider session.
14 *
15 * A server here is injected into each session next to the built-in `t3-code`
16 * server; T3 never writes a provider's own config files. Servers a provider
17 * already loads from its own config keep working untouched.
18 */
19​
20/**
21 * Lowercase letters, digits, `-` and `_`, starting with a letter: the subset
22 * every provider accepts as a server key and tool prefix (Claude names tools
23 * `mcp__<server>__<tool>`, OpenCode `<server>_<tool>`).
24 */
25const MCP_SERVER_NAME_PATTERN = /^[a-z][a-z0-9_-]*$/;
26const MCP_SERVER_NAME_MAX_CHARS = 48;
27/** T3's own server, which every session already gets. */
28const T3_MCP_SERVER_NAME = "t3-code";
29/** Reserved for T3's own servers: Codex also gets a direct-only `t3-code-direct`. */
30const RESERVED_MCP_SERVER_NAMES: ReadonlySet<string> = new Set([
31 T3_MCP_SERVER_NAME,
32 "t3-code-direct",
33]);
34​
35export const McpServerName = TrimmedNonEmptyString.check(
36 Schema.isMaxLength(MCP_SERVER_NAME_MAX_CHARS),
37 Schema.isPattern(MCP_SERVER_NAME_PATTERN),
38 Schema.makeFilter((name: string) =>
39 RESERVED_MCP_SERVER_NAMES.has(name) ? `"${name}" is reserved for T3 Code.` : true,
40 ),
41);
42export type McpServerName = typeof McpServerName.Type;
43​
44/**
45 * An environment variable or HTTP header. A `sensitive` value moves to the secret store;
46 * clients see it empty with `valueRedacted`, and sending that back keeps it. After a rename
47 * the client sends `storedAs`, the names it was saved under, so the save moves the secret.
48 */
49export const McpServerVariable = Schema.Struct({
50 name: TrimmedNonEmptyString,
51 value: Schema.String.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
52 sensitive: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
53 valueRedacted: Schema.optionalKey(Schema.Boolean),
54 storedAs: Schema.optionalKey(
55 Schema.Struct({ server: TrimmedNonEmptyString, variable: TrimmedNonEmptyString }),
56 ),
57});
58export type McpServerVariable = typeof McpServerVariable.Type;
59​
60export const McpStdioTransport = Schema.Struct({
61 type: Schema.Literal("stdio"),
62 command: TrimmedNonEmptyString,
63 args: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
64 env: Schema.Array(McpServerVariable).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
65});
66export type McpStdioTransport = typeof McpStdioTransport.Type;
67​
68/**
69 * A client the user registered with the server's OAuth provider, for servers such as Google's
70 * that don't let a client register itself. Its secret is kept like a variable's value.
71 */
72export const McpServerOAuth = Schema.Struct({
73 clientId: TrimmedNonEmptyString,
74 clientSecret: Schema.optionalKey(
75 Schema.Struct({
76 value: Schema.String.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
77 sensitive: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
78 valueRedacted: Schema.optionalKey(Schema.Boolean),
79 /** The server it was saved under, after a rename. */
80 storedAs: Schema.optionalKey(Schema.Struct({ server: TrimmedNonEmptyString })),
81 }),
82 ),
83});
84export type McpServerOAuth = typeof McpServerOAuth.Type;
85​
86export const McpHttpTransport = Schema.Struct({
87 type: Schema.Literal("http"),
88 url: TrimmedNonEmptyString,
89 headers: Schema.Array(McpServerVariable).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
90 oauth: Schema.optionalKey(McpServerOAuth),
91});
92export type McpHttpTransport = typeof McpHttpTransport.Type;
93​
94export const McpServerTransport = Schema.Union([McpStdioTransport, McpHttpTransport]);
95export type McpServerTransport = typeof McpServerTransport.Type;
96​
97export const McpServerConfig = Schema.Struct({
98 enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
99 transport: McpServerTransport,
100});
101export type McpServerConfig = typeof McpServerConfig.Type;
102​
103/**
104 * One server in a project's overrides. With a transport it adds a server to
105 * the project, or replaces the environment's server of the same name. Without
106 * one it only switches the inherited server on or off for the project.
107 */
108export const McpServerProjectOverride = Schema.Struct({
109 enabled: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(true))),
110 transport: Schema.optionalKey(McpServerTransport),
111});
112export type McpServerProjectOverride = typeof McpServerProjectOverride.Type;
113​
114export const McpServers = Schema.Record(McpServerName, McpServerConfig);
115export type McpServers = typeof McpServers.Type;
116​
117export const McpServerProjectOverrides = Schema.Record(McpServerName, McpServerProjectOverride);
118export type McpServerProjectOverrides = typeof McpServerProjectOverrides.Type;
119​
120/**
121 * Skills switched off on the environment, by the name agents invoke them
122 * with. A name rather than a path: the same skill is reached through several
123 * folders (symlinked into each provider's directory, or a worktree's copy of
124 * the repo), and a provider only ever sees one of them.
125 */
126export const DisabledSkills = ForwardCompatibleArray(TrimmedNonEmptyString);
127export type DisabledSkills = typeof DisabledSkills.Type;
128​
129/**
130 * A project's per-skill switches, keyed by skill name. `true` turns a skill
131 * off for the project; `false` turns one the environment disabled back on.
132 * An absent name inherits.
133 */
134export const DisabledSkillsProjectOverride = Schema.Record(TrimmedNonEmptyString, Schema.Boolean);
135export type DisabledSkillsProjectOverride = typeof DisabledSkillsProjectOverride.Type;
136​
137/**
138 * A thread's own switches, by name. `true` turns a server or skill on for the
139 * thread even where settings have it off; `false` turns it off. An absent
140 * name follows settings, and a name this environment doesn't define is
141 * skipped: a thread switches servers, it never defines one.
142 */
143export const ThreadToolOverrides = Schema.Struct({
144 mcpServers: Schema.optionalKey(Schema.Record(TrimmedNonEmptyString, Schema.Boolean)),
145 skills: Schema.optionalKey(Schema.Record(TrimmedNonEmptyString, Schema.Boolean)),
146});
147export type ThreadToolOverrides = typeof ThreadToolOverrides.Type;
148​
149/** An MCP server as a provider session receives it: secrets materialized, enabled only. */
150export interface ResolvedMcpServer {
151 readonly name: McpServerName;
152 readonly transport: McpServerTransport;
154​
155/** Variables as a provider needs them; a redacted value that never materialized is dropped. */
156export function mcpServerVariableRecord(
157 variables: ReadonlyArray<McpServerVariable>,
158): Record<string, string> {
159 const record: Record<string, string> = {};
160 for (const variable of variables) {
161 if (variable.valueRedacted === true && variable.value.length === 0) continue;
162 record[variable.name] = variable.value;
163 }
164 return record;
166​
167/** What a row shows for a server: its command line or URL, never a secret. */
168export function mcpServerTransportSummary(transport: McpServerTransport): string {
169 return transport.type === "stdio"
170 ? [transport.command, ...transport.args].join(" ")
171 : transport.url;
173​
174// ── Listing ──────────────────────────────────────────────────────────
175​
176export const AgentToolsListInput = Schema.Struct({
177 /** Report what this thread's sessions get, its overrides included. */
178 threadId: Schema.optional(ThreadId),
179 /** Without a thread: what a new thread in this project gets. */
180 projectId: Schema.optional(ProjectId),
181});
182export type AgentToolsListInput = typeof AgentToolsListInput.Type;
183​
184export const AgentToolsMcpServer = Schema.Struct({
185 name: McpServerName,
186 /** Its command line or URL, never a secret. */
187 summary: Schema.String,
188 definedBy: Schema.Literals(["settings", "project"]),
189 /** On before the thread's overrides apply. */
190 settingsEnabled: Schema.Boolean,
191 /** On for the thread's sessions. */
192 enabled: Schema.Boolean,
193});
194export type AgentToolsMcpServer = typeof AgentToolsMcpServer.Type;
195​
196export const AgentToolsSkill = Schema.Struct({
197 name: Schema.String,
198 description: Schema.optionalKey(Schema.String),
199 /** The provider instances whose agents can load it. */
200 providers: Schema.Array(ProviderInstanceId),
201 settingsEnabled: Schema.Boolean,
202 enabled: Schema.Boolean,
203});
204export type AgentToolsSkill = typeof AgentToolsSkill.Type;
205​
206export const AgentToolsList = Schema.Struct({
207 mcpServers: Schema.Array(AgentToolsMcpServer),
208 skills: Schema.Array(AgentToolsSkill),
209});
210export type AgentToolsList = typeof AgentToolsList.Type;
211​
212export class AgentToolsListError extends Schema.TaggedError<AgentToolsListError>()(
213 "AgentToolsListError",
214 {
215 message: TrimmedNonEmptyString,
216 cause: Schema.optional(Schema.Defect()),
217 },
218) {}