Skip to content

Commit 0b19d51

Browse files
feat: manage skills and MCP servers in Settings → Tools
Adds a Tools settings page with Skills and MCP servers tabs, scoped to environments and projects like other settings. - MCP servers added in T3 are injected into every provider session next to the t3-code server (Claude, Codex, Cursor, OpenCode 1/2, Grok and ACP agents). Sensitive env/header values live in the secret store. - Skills can be switched off by name; Claude, Codex and OpenCode 1.x hide them from the session, and the composer drops them from its menu. - Projects override servers and skill switches per name instead of replacing the environment's list. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 04ad174 commit 0b19d51

35 files changed

Lines changed: 3099 additions & 45 deletions

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
1-
import type { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
1+
import type {
2+
EnvironmentId,
3+
ProviderInstanceId,
4+
ResolvedMcpServer,
5+
ThreadId,
6+
} from "@t3tools/contracts";
7+
8+
/**
9+
* The user's tools for one thread, resolved from Settings → Tools when the
10+
* session is prepared. Adapters add the servers next to `t3-code` and hide
11+
* the skills where their provider can.
12+
*/
13+
export interface McpProviderSessionTools {
14+
/** Enabled servers with secrets materialized, sorted by name. */
15+
readonly servers: ReadonlyArray<ResolvedMcpServer>;
16+
/** Skill names hidden from the agent, sorted. */
17+
readonly disabledSkills: ReadonlyArray<string>;
18+
/**
19+
* Changes whenever `servers` or `disabledSkills` do, without exposing a
20+
* secret. Adapters compare it to decide when a live process is stale.
21+
*/
22+
readonly fingerprint: string;
23+
}
24+
25+
export const EMPTY_MCP_PROVIDER_SESSION_TOOLS: McpProviderSessionTools = {
26+
servers: [],
27+
disabledSkills: [],
28+
fingerprint: "",
29+
};
230

331
export interface McpProviderSessionConfig {
432
readonly environmentId: EnvironmentId;
@@ -22,6 +50,13 @@ export interface McpProviderSessionConfig {
2250
* already pointed at the server's daemon; the agent never handles a token.
2351
*/
2452
readonly agentDeviceEnvironment?: Readonly<Record<string, string>>;
53+
/** Servers and skill switches from Settings → Tools; absent means none. */
54+
readonly tools?: McpProviderSessionTools;
55+
}
56+
57+
/** The thread's user tools, or none when its session has no MCP config. */
58+
export function readMcpProviderSessionTools(threadId: ThreadId): McpProviderSessionTools {
59+
return sessionsByThread.get(threadId)?.tools ?? EMPTY_MCP_PROVIDER_SESSION_TOOLS;
2560
}
2661

2762
/** Provider env with the device variables applied over `base`, or `base` untouched. */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { ProjectId, ResolvedMcpServer, ServerSettings } from "@t3tools/contracts";
2+
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
3+
4+
import type { McpProviderSessionTools } from "./McpProviderSession.ts";
5+
6+
/**
7+
* A thread's tools from settings whose secrets are already materialized:
8+
* the project's overrides applied, disabled servers dropped. The fingerprint
9+
* covers the full config so a rotated secret also counts as a change; it
10+
* stays on the server and is only ever compared, never sent or logged.
11+
*/
12+
export function resolveAgentTools(
13+
settings: ServerSettings,
14+
projectId: ProjectId | null,
15+
): McpProviderSessionTools {
16+
const effective = resolveProjectSettings(settings, projectId).settings;
17+
const servers: ResolvedMcpServer[] = Object.entries(effective.mcpServers)
18+
.filter(([, server]) => server.enabled)
19+
.map(([name, server]) => ({ name, transport: server.transport }))
20+
.toSorted((left, right) => left.name.localeCompare(right.name));
21+
const disabledSkills = [...new Set(effective.disabledSkills)].toSorted();
22+
const fingerprint =
23+
servers.length === 0 && disabledSkills.length === 0
24+
? ""
25+
: JSON.stringify({ servers, disabledSkills });
26+
return { servers, disabledSkills, fingerprint };
27+
}
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/**
2+
* Sensitive MCP server variables (env values, HTTP headers) live in the
3+
* secret store, never in settings.json or on the wire. Servers sit in two
4+
* places, the environment's `mcpServers` and each project's override entry,
5+
* so every helper here walks both.
6+
*
7+
* On disk and towards clients a sensitive variable is `{value: "",
8+
* valueRedacted: true}`. A client that sends that back keeps the stored
9+
* secret; a new value replaces it; turning `sensitive` off moves the value
10+
* back into settings.
11+
*/
12+
import type {
13+
McpServerConfig,
14+
McpServerProjectOverride,
15+
McpServerVariable,
16+
ProjectSettingsOverrides,
17+
ServerSettings,
18+
} from "@t3tools/contracts";
19+
20+
/** Where a server lives: the environment, or one project's overrides. */
21+
type McpServerOwner = { readonly kind: "environment" } | { readonly kind: "project"; id: string };
22+
23+
type ServerEntry = McpServerConfig | McpServerProjectOverride;
24+
25+
const encode = (value: string) => Buffer.from(value, "utf8").toString("base64url");
26+
27+
/** Deterministic per (owner, server, variable kind, variable name). */
28+
export function mcpServerSecretName(input: {
29+
readonly owner: McpServerOwner;
30+
readonly server: string;
31+
readonly variableKind: "env" | "header";
32+
readonly variable: string;
33+
}): string {
34+
const owner = input.owner.kind === "environment" ? "env" : `project-${encode(input.owner.id)}`;
35+
return `mcp-${owner}-${encode(input.server)}-${input.variableKind}-${encode(input.variable)}`;
36+
}
37+
38+
interface VariableSite {
39+
readonly owner: McpServerOwner;
40+
readonly server: string;
41+
readonly variableKind: "env" | "header";
42+
readonly variable: McpServerVariable;
43+
}
44+
45+
function variablesOf(entry: ServerEntry): {
46+
readonly kind: "env" | "header";
47+
readonly variables: ReadonlyArray<McpServerVariable>;
48+
} | null {
49+
const transport = entry.transport;
50+
if (transport === undefined) return null;
51+
return transport.type === "stdio"
52+
? { kind: "env", variables: transport.env }
53+
: { kind: "header", variables: transport.headers };
54+
}
55+
56+
function* variableSites(settings: ServerSettings): Generator<VariableSite> {
57+
for (const [server, entry] of Object.entries(settings.mcpServers)) {
58+
const variables = variablesOf(entry);
59+
if (variables === null) continue;
60+
for (const variable of variables.variables) {
61+
yield { owner: { kind: "environment" }, server, variableKind: variables.kind, variable };
62+
}
63+
}
64+
for (const [id, overrides] of Object.entries(settings.projectSettingsOverrides)) {
65+
for (const [server, entry] of Object.entries(overrides.mcpServers ?? {})) {
66+
const variables = variablesOf(entry);
67+
if (variables === null) continue;
68+
for (const variable of variables.variables) {
69+
yield { owner: { kind: "project", id }, server, variableKind: variables.kind, variable };
70+
}
71+
}
72+
}
73+
}
74+
75+
const siteSecretName = (site: VariableSite) =>
76+
mcpServerSecretName({
77+
owner: site.owner,
78+
server: site.server,
79+
variableKind: site.variableKind,
80+
variable: site.variable.name,
81+
});
82+
83+
/**
84+
* Rebuild every server in `settings` with `map` applied to each variable.
85+
* Returns the input unchanged when no server has variables.
86+
*/
87+
function mapVariables(
88+
settings: ServerSettings,
89+
map: (site: VariableSite) => McpServerVariable,
90+
): ServerSettings {
91+
const mapEntry = <Entry extends ServerEntry>(
92+
owner: McpServerOwner,
93+
server: string,
94+
entry: Entry,
95+
): Entry => {
96+
const transport = entry.transport;
97+
if (transport === undefined) return entry;
98+
if (transport.type === "stdio") {
99+
if (transport.env.length === 0) return entry;
100+
return {
101+
...entry,
102+
transport: {
103+
...transport,
104+
env: transport.env.map((variable) =>
105+
map({ owner, server, variableKind: "env", variable }),
106+
),
107+
},
108+
};
109+
}
110+
if (transport.headers.length === 0) return entry;
111+
return {
112+
...entry,
113+
transport: {
114+
...transport,
115+
headers: transport.headers.map((variable) =>
116+
map({ owner, server, variableKind: "header", variable }),
117+
),
118+
},
119+
};
120+
};
121+
const mcpServers = Object.fromEntries(
122+
Object.entries(settings.mcpServers).map(([server, entry]) => [
123+
server,
124+
mapEntry({ kind: "environment" }, server, entry),
125+
]),
126+
) as ServerSettings["mcpServers"];
127+
const projectSettingsOverrides = Object.fromEntries(
128+
Object.entries(settings.projectSettingsOverrides).map(([id, overrides]) => [
129+
id,
130+
overrides.mcpServers === undefined
131+
? overrides
132+
: ({
133+
...overrides,
134+
mcpServers: Object.fromEntries(
135+
Object.entries(overrides.mcpServers).map(([server, entry]) => [
136+
server,
137+
mapEntry({ kind: "project", id }, server, entry),
138+
]),
139+
),
140+
} satisfies ProjectSettingsOverrides),
141+
]),
142+
) as ServerSettings["projectSettingsOverrides"];
143+
return { ...settings, mcpServers, projectSettingsOverrides };
144+
}
145+
146+
/** What a client sees: sensitive values blanked, with a flag that one is stored. */
147+
export function redactMcpServerSecrets(settings: ServerSettings): ServerSettings {
148+
return mapVariables(settings, ({ variable }) => {
149+
if (!variable.sensitive) {
150+
const { valueRedacted: _omit, ...rest } = variable;
151+
return rest;
152+
}
153+
return {
154+
...variable,
155+
value: "",
156+
...(variable.value.length > 0 || variable.valueRedacted ? { valueRedacted: true } : {}),
157+
};
158+
});
159+
}
160+
161+
export type McpSecretChange =
162+
| { readonly kind: "write"; readonly secretName: string; readonly value: string }
163+
| { readonly kind: "remove"; readonly secretName: string };
164+
165+
/**
166+
* Plan the secret-store writes for moving from `current` to `next`, and the
167+
* settings to persist (sensitive values replaced by the redaction flag).
168+
* A sensitive variable sent back redacted with no value keeps its secret;
169+
* a value the user typed replaces it; an empty value or a variable that is
170+
* no longer sensitive (or no longer exists) removes it.
171+
*/
172+
export function planMcpServerSecrets(
173+
current: ServerSettings,
174+
next: ServerSettings,
175+
): { readonly settings: ServerSettings; readonly changes: ReadonlyArray<McpSecretChange> } {
176+
const stored = new Set<string>();
177+
for (const site of variableSites(current)) {
178+
if (site.variable.sensitive) stored.add(siteSecretName(site));
179+
}
180+
const changes: McpSecretChange[] = [];
181+
const kept = new Set<string>();
182+
const settings = mapVariables(next, (site) => {
183+
const secretName = siteSecretName(site);
184+
const { variable } = site;
185+
const { valueRedacted: _omit, ...plain } = variable;
186+
if (!variable.sensitive) return plain;
187+
if (variable.value.length === 0) {
188+
if (!variable.valueRedacted) return plain;
189+
kept.add(secretName);
190+
return { ...variable, value: "", valueRedacted: true };
191+
}
192+
kept.add(secretName);
193+
changes.push({ kind: "write", secretName, value: variable.value });
194+
return { ...variable, value: "", valueRedacted: true };
195+
});
196+
for (const secretName of stored) {
197+
if (!kept.has(secretName)) changes.push({ kind: "remove", secretName });
198+
}
199+
return { settings, changes };
200+
}
201+
202+
/** The secret names whose values `materializeMcpServerSecrets` needs. */
203+
export function mcpServerSecretNames(settings: ServerSettings): ReadonlyArray<string> {
204+
const names: string[] = [];
205+
for (const site of variableSites(settings)) {
206+
if (site.variable.sensitive && site.variable.valueRedacted) names.push(siteSecretName(site));
207+
}
208+
return names;
209+
}
210+
211+
/** Settings with stored secret values filled back in, for server-side consumers. */
212+
export function materializeMcpServerSecrets(
213+
settings: ServerSettings,
214+
secrets: ReadonlyMap<string, string>,
215+
): ServerSettings {
216+
if (secrets.size === 0) return settings;
217+
return mapVariables(settings, (site) => {
218+
if (!site.variable.sensitive || !site.variable.valueRedacted) return site.variable;
219+
return { ...site.variable, value: secrets.get(siteSecretName(site)) ?? "" };
220+
});
221+
}

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
type ProviderRequestKind,
2929
type ProviderThreadId,
3030
type ProviderUserInputAnswers,
31+
mcpServerVariableRecord,
32+
type ResolvedMcpServer,
3133
type RuntimeRequestId,
3234
type ThreadTokenUsageSnapshot,
3335
type ThreadId,
@@ -680,12 +682,45 @@ interface AcpMcpContext {
680682
readonly authorization?: string;
681683
}
682684

685+
/**
686+
* The user's servers from Settings → Tools, passed through as plain ACP
687+
* entries. A bare command name is left to the agent's PATH lookup. Http
688+
* entries are dropped later for agents that do not advertise http support.
689+
*/
690+
function acpUserMcpServers(
691+
servers: ReadonlyArray<ResolvedMcpServer>,
692+
): ReadonlyArray<EffectAcpSchema.McpServer> {
693+
return servers.map((server) =>
694+
server.transport.type === "stdio"
695+
? {
696+
// Untyped like T3's own entry: the shape every ACP version accepts.
697+
name: server.name,
698+
command: server.transport.command,
699+
args: [...server.transport.args],
700+
env: Object.entries(mcpServerVariableRecord(server.transport.env)).map(
701+
([name, value]) => ({ name, value }),
702+
),
703+
}
704+
: {
705+
type: "http" as const,
706+
name: server.name,
707+
url: server.transport.url,
708+
headers: Object.entries(mcpServerVariableRecord(server.transport.headers)).map(
709+
([name, value]) => ({ name, value }),
710+
),
711+
},
712+
);
713+
}
714+
683715
function acpMcpContext(threadId: ThreadId | null, self: SelfInvocation): AcpMcpContext {
684716
if (threadId === null) return { servers: [], acpServers: [] };
685717
const session = McpProviderSession.readMcpProviderSession(threadId);
686718
if (session === undefined) {
687719
return { servers: [], acpServers: [] };
688720
}
721+
// Both lists carry them: an agent that takes T3's server over ACP gets
722+
// only `acpServers`, so the user's servers would otherwise be lost.
723+
const userServers = acpUserMcpServers(session.tools?.servers ?? []);
689724
// Stdio is ACP's required baseline MCP transport. Agents that advertise
690725
// optional http support still routinely fail to wire injected http servers
691726
// through to their backend (codex-acp 1.2.0 and pi-acp both drop them), so
@@ -704,8 +739,9 @@ function acpMcpContext(threadId: ThreadId | null, self: SelfInvocation): AcpMcpC
704739
{ name: "T3_ACP_MCP_AUTHORIZATION", value: session.authorizationHeader },
705740
],
706741
},
742+
...userServers,
707743
],
708-
acpServers: [{ type: "acp", name: "t3-code", serverId: "t3-code" }],
744+
acpServers: [{ type: "acp", name: "t3-code", serverId: "t3-code" }, ...userServers],
709745
endpoint: session.endpoint,
710746
authorization: session.authorizationHeader,
711747
processEnvironment: {

0 commit comments

Comments
 (0)