|
| 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 | +} |
0 commit comments