|
| 1 | +import type { ProjectWorkspaceDto } from './flowApi' |
| 2 | + |
| 3 | +export type McpPermission = |
| 4 | + | 'project.read' |
| 5 | + | 'project.write' |
| 6 | + | 'library.read' |
| 7 | + | 'run.read' |
| 8 | + | 'debug.read' |
| 9 | + | 'flow.write' |
| 10 | + | 'debug.control' |
| 11 | + | 'flow.publish' |
| 12 | + | 'flow.rollback' |
| 13 | + | 'script.compile' |
| 14 | + | 'library.import' |
| 15 | + | 'library.manage' |
| 16 | + | 'mcp.keys.manage' |
| 17 | + | 'sensitive.read' |
| 18 | + |
| 19 | +export interface McpApiKeyDto { |
| 20 | + id: string |
| 21 | + projectId?: string | null |
| 22 | + name: string |
| 23 | + keyPrefix: string |
| 24 | + permissions: McpPermission[] |
| 25 | + createdAt: string |
| 26 | + expiresAt?: string | null |
| 27 | + revokedAt?: string | null |
| 28 | + lastUsedAt?: string | null |
| 29 | + isAdministrator: boolean |
| 30 | +} |
| 31 | + |
| 32 | +export interface CreatedMcpApiKeyDto { |
| 33 | + key: McpApiKeyDto |
| 34 | + secret: string |
| 35 | +} |
| 36 | + |
| 37 | +export interface RotatedMcpApiKeyDto { |
| 38 | + revokedKeyId: string |
| 39 | + key: McpApiKeyDto |
| 40 | + secret?: string | null |
| 41 | +} |
| 42 | + |
| 43 | +export interface CreateMcpApiKeyRequestDto { |
| 44 | + projectId?: string | null |
| 45 | + name: string |
| 46 | + permissions: McpPermission[] |
| 47 | + expiresAt?: string | null |
| 48 | + isAdministrator?: boolean |
| 49 | +} |
| 50 | + |
| 51 | +export interface McpKeyProjectOption { |
| 52 | + id: string |
| 53 | + name: string |
| 54 | +} |
| 55 | + |
| 56 | +const apiBaseUrl = (import.meta.env.VITE_API_BASE_URL ?? '').replace(/\/$/, '') |
| 57 | +const sessionCredentialKey = 'sereinflow.mcp.management-key' |
| 58 | +const rememberedCredentialKey = 'sereinflow.mcp.management-key.remembered' |
| 59 | + |
| 60 | +export function getMcpManagementCredential(): string | undefined { |
| 61 | + if (typeof window === 'undefined') return undefined |
| 62 | + try { |
| 63 | + return window.sessionStorage.getItem(sessionCredentialKey) |
| 64 | + ?? window.localStorage.getItem(rememberedCredentialKey) |
| 65 | + ?? undefined |
| 66 | + } catch { |
| 67 | + return undefined |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export function hasRememberedMcpManagementCredential(): boolean { |
| 72 | + if (typeof window === 'undefined') return false |
| 73 | + try { |
| 74 | + return Boolean(window.localStorage.getItem(rememberedCredentialKey)) |
| 75 | + } catch { |
| 76 | + return false |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export function storeMcpManagementCredential(secret: string, remember: boolean): void { |
| 81 | + if (typeof window === 'undefined') return |
| 82 | + try { |
| 83 | + window.sessionStorage.setItem(sessionCredentialKey, secret) |
| 84 | + if (remember) window.localStorage.setItem(rememberedCredentialKey, secret) |
| 85 | + else window.localStorage.removeItem(rememberedCredentialKey) |
| 86 | + } catch { |
| 87 | + // Storage may be disabled by the browser; the current request still succeeds. |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export function clearMcpManagementCredential(): void { |
| 92 | + if (typeof window === 'undefined') return |
| 93 | + try { |
| 94 | + window.sessionStorage.removeItem(sessionCredentialKey) |
| 95 | + window.localStorage.removeItem(rememberedCredentialKey) |
| 96 | + } catch { |
| 97 | + // Ignore storage cleanup failures. |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export async function setupMcpApiKey(): Promise<CreatedMcpApiKeyDto> { |
| 102 | + return request<CreatedMcpApiKeyDto>('/api/environment/settings/mcp-keys/setup', { method: 'POST' }) |
| 103 | +} |
| 104 | + |
| 105 | +export async function listMcpApiKeys(secret: string): Promise<McpApiKeyDto[]> { |
| 106 | + return request<McpApiKeyDto[]>('/api/environment/settings/mcp-keys', { secret }) |
| 107 | +} |
| 108 | + |
| 109 | +export async function createMcpApiKey( |
| 110 | + secret: string, |
| 111 | + body: CreateMcpApiKeyRequestDto, |
| 112 | +): Promise<CreatedMcpApiKeyDto> { |
| 113 | + return request<CreatedMcpApiKeyDto>('/api/environment/settings/mcp-keys', { |
| 114 | + method: 'POST', |
| 115 | + secret, |
| 116 | + body, |
| 117 | + }) |
| 118 | +} |
| 119 | + |
| 120 | +export async function rotateMcpApiKey(secret: string, keyId: string): Promise<RotatedMcpApiKeyDto> { |
| 121 | + return request<RotatedMcpApiKeyDto>(`/api/environment/settings/mcp-keys/${encodeURIComponent(keyId)}/rotate`, { |
| 122 | + method: 'POST', |
| 123 | + secret, |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +export async function revokeMcpApiKey(secret: string, keyId: string): Promise<McpApiKeyDto> { |
| 128 | + return request<McpApiKeyDto>(`/api/environment/settings/mcp-keys/${encodeURIComponent(keyId)}`, { |
| 129 | + method: 'DELETE', |
| 130 | + secret, |
| 131 | + }) |
| 132 | +} |
| 133 | + |
| 134 | +export function mcpKeyProjectOptions(workspaces: ProjectWorkspaceDto[]): McpKeyProjectOption[] { |
| 135 | + return workspaces |
| 136 | + .filter((workspace) => workspace.project.status !== 'archived') |
| 137 | + .map((workspace) => ({ id: workspace.project.id, name: workspace.project.name })) |
| 138 | +} |
| 139 | + |
| 140 | +async function request<T>( |
| 141 | + path: string, |
| 142 | + options: { method?: 'POST' | 'DELETE'; secret?: string; body?: unknown } = {}, |
| 143 | +): Promise<T> { |
| 144 | + const headers: Record<string, string> = {} |
| 145 | + if (options.body !== undefined) headers['content-type'] = 'application/json' |
| 146 | + if (options.secret) headers.authorization = `Bearer ${options.secret}` |
| 147 | + const response = await fetch(`${apiBaseUrl}${path}`, { |
| 148 | + method: options.method, |
| 149 | + headers: Object.keys(headers).length > 0 ? headers : undefined, |
| 150 | + body: options.body === undefined ? undefined : JSON.stringify(options.body), |
| 151 | + }) |
| 152 | + if (response.ok) { |
| 153 | + const payload = await response.text() |
| 154 | + return (payload ? JSON.parse(payload) : undefined) as T |
| 155 | + } |
| 156 | + |
| 157 | + const problem = await response.json().catch(() => ({})) as { detail?: string; title?: string } |
| 158 | + throw new McpApiError(response.status, problem.detail ?? problem.title ?? `MCP request failed (${response.status}).`) |
| 159 | +} |
| 160 | + |
| 161 | +export class McpApiError extends Error { |
| 162 | + public readonly status: number |
| 163 | + |
| 164 | + constructor(status: number, message: string) { |
| 165 | + super(message) |
| 166 | + this.status = status |
| 167 | + this.name = 'McpApiError' |
| 168 | + } |
| 169 | +} |
0 commit comments