Open History to see the last commit
1import type { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
2
3export interface McpProviderSessionConfig {
4 readonly environmentId: EnvironmentId;
5 readonly threadId: ThreadId;
6 readonly providerSessionId: string;
7 readonly providerInstanceId: ProviderInstanceId;
8 readonly endpoint: string;
9 readonly authorizationHeader: string;
10 /** Capabilities the credential grants ("preview", "device"). */
11 readonly capabilities: ReadonlySet<string>;
12 /**
13 * Set when the session may drive devices. Adapters spread this into the
14 * provider subprocess environment so the `agent-device` CLI is on PATH and
15 * already pointed at the server's daemon; the agent never handles a token.
16 */
17 readonly agentDeviceEnvironment?: Readonly<Record<string, string>>;
18}
19
20/** Provider env with the device variables applied over `base`, or `base` untouched. */
21export function withAgentDeviceEnvironment(
22 base: NodeJS.ProcessEnv,
23 config: Pick<McpProviderSessionConfig, "agentDeviceEnvironment"> | undefined,
24): NodeJS.ProcessEnv {
25 const extra = config?.agentDeviceEnvironment;
26 if (!extra) return base;
27 const separator = extra.PATH_SEPARATOR ?? ":";
28 const basePath = base.PATH ?? base.Path;
29 const { PATH: shimDir, PATH_SEPARATOR: _separator, ...rest } = extra;
30 return {
31 ...base,
32 ...rest,
33 ...(shimDir ? { PATH: basePath ? `${shimDir}${separator}${basePath}` : shimDir } : {}),
34 };
35}
36
37const sessionsByThread = new Map<ThreadId, McpProviderSessionConfig>();
38
39export function setMcpProviderSession(config: McpProviderSessionConfig): void {
40 sessionsByThread.set(config.threadId, config);
41}
42
43export function readMcpProviderSession(threadId: ThreadId): McpProviderSessionConfig | undefined {
44 return sessionsByThread.get(threadId);
45}
46
47export function clearMcpProviderSession(threadId: ThreadId): void {
48 sessionsByThread.delete(threadId);
49}
50
51export function clearAllMcpProviderSessions(): void {
52 sessionsByThread.clear();
53}