|
1 | | -import { OrchestratorMcpFailure, type ServerSettings } from "@t3tools/contracts"; |
| 1 | +import { |
| 2 | + type McpServerConfig, |
| 3 | + type McpServerName, |
| 4 | + type McpServerVariable, |
| 5 | + OrchestratorMcpFailure, |
| 6 | + type ProjectId, |
| 7 | + type SecretRef, |
| 8 | + type ServerSettings, |
| 9 | +} from "@t3tools/contracts"; |
2 | 10 | import * as Effect from "effect/Effect"; |
3 | 11 | import * as Environment from "../../../environment/ServerEnvironment.ts"; |
4 | 12 | import * as HostResources from "../../../resourceTelemetry/HostResources.ts"; |
5 | 13 | import * as ThreadCommandExecutor from "../../../orchestration-v2/ThreadCommandExecutor.ts"; |
| 14 | +import * as SecretRequests from "../../../secrets/SecretRequests.ts"; |
6 | 15 | import * as Settings from "../../../serverSettings.ts"; |
7 | 16 | import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
8 | 17 | import * as McpToolAccess from "../../McpToolAccess.ts"; |
@@ -30,6 +39,117 @@ export function preferences(settings: ServerSettings) { |
30 | 39 | }, |
31 | 40 | }; |
32 | 41 | } |
| 42 | +/** The environment's MCP servers as `t3_mcp_servers_read` shows them: a secret only as `{ stored }`. */ |
| 43 | +function mcpServersView(settings: ServerSettings) { |
| 44 | + const values = (variables: ReadonlyArray<McpServerVariable>) => |
| 45 | + Object.fromEntries( |
| 46 | + variables.map((variable) => [ |
| 47 | + variable.name, |
| 48 | + variable.sensitive ? { stored: variable.value !== "" } : variable.value, |
| 49 | + ]), |
| 50 | + ); |
| 51 | + return { |
| 52 | + servers: Object.fromEntries( |
| 53 | + Object.entries(settings.mcpServers).map(([name, { enabled, transport }]) => [ |
| 54 | + name, |
| 55 | + transport.type === "stdio" |
| 56 | + ? { |
| 57 | + enabled, |
| 58 | + command: transport.command, |
| 59 | + args: transport.args, |
| 60 | + env: values(transport.env), |
| 61 | + } |
| 62 | + : { enabled, url: transport.url, headers: values(transport.headers) }, |
| 63 | + ]), |
| 64 | + ), |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +type McpValue = |
| 69 | + | string |
| 70 | + | { readonly secretRef: SecretRef; readonly prefix?: string } |
| 71 | + | { readonly stored: true }; |
| 72 | +interface McpServerInput { |
| 73 | + readonly enabled?: boolean; |
| 74 | + readonly command?: string; |
| 75 | + readonly args?: ReadonlyArray<string>; |
| 76 | + readonly env?: { readonly [name: string]: McpValue }; |
| 77 | + readonly url?: string; |
| 78 | + readonly headers?: { readonly [name: string]: McpValue }; |
| 79 | +} |
| 80 | +const invalid = (message: string) => |
| 81 | + new OrchestratorMcpFailure({ code: "invalid_request", message }); |
| 82 | + |
| 83 | +const mcpServerConfig = Effect.fn("EnvironmentToolkit.mcpServerConfig")(function* ( |
| 84 | + server: McpServerInput, |
| 85 | + existing: McpServerConfig | undefined, |
| 86 | + projectId: ProjectId | undefined, |
| 87 | +) { |
| 88 | + if ((server.command === undefined) === (server.url === undefined)) { |
| 89 | + return yield* invalid("Give either `command` or `url`."); |
| 90 | + } |
| 91 | + if (server.command !== undefined && server.headers !== undefined) { |
| 92 | + return yield* invalid("`headers` go with `url`."); |
| 93 | + } |
| 94 | + if (server.url !== undefined && (server.args !== undefined || server.env !== undefined)) { |
| 95 | + return yield* invalid("`args` and `env` go with `command`."); |
| 96 | + } |
| 97 | + const entries = Object.entries( |
| 98 | + (server.command === undefined ? server.headers : server.env) ?? {}, |
| 99 | + ); |
| 100 | + // Keys are stored per transport (env or header), so one survives only on the same transport. |
| 101 | + const sameTransport = |
| 102 | + existing !== undefined && |
| 103 | + (server.command !== undefined) === (existing.transport.type === "stdio"); |
| 104 | + const stored = new Set( |
| 105 | + (!sameTransport |
| 106 | + ? [] |
| 107 | + : existing.transport.type === "stdio" |
| 108 | + ? existing.transport.env |
| 109 | + : existing.transport.headers |
| 110 | + ).flatMap((variable) => (variable.sensitive && variable.value !== "" ? [variable.name] : [])), |
| 111 | + ); |
| 112 | + // Checked before any secretRef is used: each one is good for a single save. |
| 113 | + for (const [name, value] of entries) { |
| 114 | + if (typeof value === "string") continue; |
| 115 | + if ("stored" in value && !stored.has(name)) { |
| 116 | + return yield* invalid(`No secret is stored for ${name}.`); |
| 117 | + } |
| 118 | + if ("secretRef" in value && projectId === undefined) { |
| 119 | + return yield* invalid("A `secretRef` can only be used from the thread that asked for it."); |
| 120 | + } |
| 121 | + } |
| 122 | + const secretRequests = yield* SecretRequests.SecretRequests; |
| 123 | + const variables = yield* Effect.forEach( |
| 124 | + entries, |
| 125 | + ([name, value]): Effect.Effect<McpServerVariable, OrchestratorMcpFailure> => |
| 126 | + typeof value === "string" |
| 127 | + ? Effect.succeed({ name, value, sensitive: false }) |
| 128 | + : "stored" in value |
| 129 | + ? Effect.succeed({ name, value: "", sensitive: true, valueRedacted: true }) |
| 130 | + : secretRequests.consume({ ref: value.secretRef, projectId: projectId! }).pipe( |
| 131 | + Effect.mapError((error) => invalid(error.message)), |
| 132 | + Effect.map((secret) => ({ |
| 133 | + name, |
| 134 | + value: (value.prefix ?? "") + secret, |
| 135 | + sensitive: true, |
| 136 | + })), |
| 137 | + ), |
| 138 | + ); |
| 139 | + return { |
| 140 | + enabled: server.enabled ?? true, |
| 141 | + transport: |
| 142 | + server.command === undefined |
| 143 | + ? { type: "http" as const, url: server.url!, headers: variables } |
| 144 | + : { |
| 145 | + type: "stdio" as const, |
| 146 | + command: server.command, |
| 147 | + args: server.args ?? [], |
| 148 | + env: variables, |
| 149 | + }, |
| 150 | + } satisfies McpServerConfig; |
| 151 | +}); |
| 152 | + |
33 | 153 | const GB = 1e9; |
34 | 154 | const gigabytes = (bytes: number) => Math.round((bytes / GB) * 10) / 10; |
35 | 155 |
|
@@ -100,4 +220,34 @@ export const layer = McpToolAccess.toLayer(EnvironmentToolkit, { |
100 | 220 | : executor.withLock(scope.thread.threadId, update); |
101 | 221 | }), |
102 | 222 | ), |
| 223 | + t3_mcp_servers_read: McpToolAccess.reads(() => |
| 224 | + Effect.gen(function* () { |
| 225 | + const { settings } = yield* access; |
| 226 | + return mcpServersView(yield* settings.getSettings.pipe(Effect.mapError(unavailable))); |
| 227 | + }), |
| 228 | + ), |
| 229 | + t3_mcp_server_update: McpToolAccess.writesEnvironment(({ name, server }, check) => |
| 230 | + Effect.gen(function* () { |
| 231 | + const scope = yield* McpInvocationContext.McpInvocationContext; |
| 232 | + const executor = yield* ThreadCommandExecutor.ThreadCommandExecutor; |
| 233 | + const update = Effect.gen(function* () { |
| 234 | + const { caller } = yield* check; |
| 235 | + const { settings } = yield* access; |
| 236 | + const current = yield* settings.getSettings.pipe(Effect.mapError(unavailable)); |
| 237 | + const config = |
| 238 | + server === null |
| 239 | + ? null |
| 240 | + : yield* mcpServerConfig(server, current.mcpServers[name], caller?.projectId); |
| 241 | + const patch: { readonly [key in McpServerName]: McpServerConfig | null } = { |
| 242 | + [name]: config, |
| 243 | + }; |
| 244 | + return mcpServersView( |
| 245 | + yield* settings.updateSettings({ mcpServers: patch }).pipe(Effect.mapError(unavailable)), |
| 246 | + ); |
| 247 | + }); |
| 248 | + return yield* scope.thread === undefined |
| 249 | + ? update |
| 250 | + : executor.withLock(scope.thread.threadId, update); |
| 251 | + }), |
| 252 | + ), |
103 | 253 | }); |
0 commit comments