@prenta/admin 2.15.0 → 2.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  'use client'
2
2
 
3
- import { useEffect, useState } from 'react'
3
+ import { useEffect, useRef, useState } from 'react'
4
4
  import { Loader2 } from 'lucide-react'
5
5
  import { cmsApi } from '../lib/api.js'
6
6
  import { parseRouteQuery } from '../router/index.js'
@@ -10,13 +10,17 @@ type ConsentDetails = {
10
10
  clientId: string
11
11
  redirectHost: string
12
12
  scope: string
13
+ autoApprove?: boolean
14
+ autoApproveReason?: 'trusted_client' | 'existing_grant' | null
13
15
  }
14
16
 
15
17
  export function McpConsent({ currentPath }: { currentPath: string }) {
16
18
  const txn = parseRouteQuery(currentPath).get('txn') ?? ''
17
19
  const [details, setDetails] = useState<ConsentDetails | null>(null)
20
+ const [autoApproving, setAutoApproving] = useState(false)
18
21
  const [error, setError] = useState('')
19
22
  const [busy, setBusy] = useState(false)
23
+ const autoSubmittedTxnRef = useRef<string | null>(null)
20
24
 
21
25
  useEffect(() => {
22
26
  if (!txn) {
@@ -24,14 +28,31 @@ export function McpConsent({ currentPath }: { currentPath: string }) {
24
28
  return
25
29
  }
26
30
  let cancelled = false
27
- cmsApi<ConsentDetails>(`/mcp/oauth/transactions/${encodeURIComponent(txn)}`).then((res) => {
28
- if (cancelled) return
29
- if (res.error || !res.data) {
30
- setError(res.error ?? 'This connection request expired.')
31
- return
32
- }
33
- setDetails(res.data)
34
- })
31
+ cmsApi<ConsentDetails>(`/mcp/oauth/transactions/${encodeURIComponent(txn)}`).then(
32
+ async (res) => {
33
+ if (cancelled) return
34
+ if (res.error || !res.data) {
35
+ setError(res.error ?? 'This connection request expired.')
36
+ return
37
+ }
38
+ setDetails(res.data)
39
+ if (res.data.autoApprove && autoSubmittedTxnRef.current !== txn) {
40
+ autoSubmittedTxnRef.current = txn
41
+ setAutoApproving(true)
42
+ const consentRes = await cmsApi<{ redirectUrl: string }>('/mcp/oauth/consent', {
43
+ method: 'POST',
44
+ body: JSON.stringify({ txn, allow: true, auto: true }),
45
+ })
46
+ if (cancelled) return
47
+ if (consentRes.error || !consentRes.data?.redirectUrl) {
48
+ setAutoApproving(false)
49
+ setError(consentRes.error ?? 'Could not auto-approve connection.')
50
+ return
51
+ }
52
+ window.location.assign(consentRes.data.redirectUrl)
53
+ }
54
+ },
55
+ )
35
56
  return () => {
36
57
  cancelled = true
37
58
  }
@@ -62,8 +83,14 @@ export function McpConsent({ currentPath }: { currentPath: string }) {
62
83
  Loading the request
63
84
  </p>
64
85
  )}
86
+ {details && autoApproving && (
87
+ <p className="text-muted-foreground mt-4 flex items-center gap-2 text-sm">
88
+ <Loader2 className="animate-spin" size={16} aria-hidden="true" />
89
+ Connecting <span className="text-foreground font-medium">{details.clientName}</span>…
90
+ </p>
91
+ )}
65
92
  {error && <p className="text-destructive mt-4 text-sm">{error}</p>}
66
- {details && (
93
+ {details && !autoApproving && (
67
94
  <div className="mt-4 space-y-4">
68
95
  <p className="text-foreground text-base">
69
96
  <span className="font-medium">{details.clientName}</span> wants to edit content on
@@ -280,7 +280,7 @@ export function Settings({
280
280
  className: 'space-y-4',
281
281
  },
282
282
  users: { body: <UsersView onNavigate={onNavigate} embedded currentPath={currentPath} /> },
283
- mcp: { body: <McpConnectTab onNavigate={onNavigate} /> },
283
+ mcp: { body: <McpConnectTab onNavigate={onNavigate} role={session?.user?.role} /> },
284
284
  'api-keys': { body: <ApiKeysView onNavigate={onNavigate} embedded /> },
285
285
  updates: { body: <UpdatesTab role={session?.user?.role} />, className: 'space-y-4' },
286
286
  }
@@ -1,12 +1,13 @@
1
1
  'use client'
2
2
 
3
3
  import * as Tabs from '@radix-ui/react-tabs'
4
- import { Copy, Loader2 } from 'lucide-react'
5
- import { useEffect, useState } from 'react'
4
+ import { Copy, Loader2, Plus, Trash2 } from 'lucide-react'
5
+ import { useEffect, useState, type FormEvent } from 'react'
6
6
  import { toast } from 'sonner'
7
7
 
8
+ import { Switch } from '../../components/ui/Toggle.js'
8
9
  import { cmsApi } from '../../lib/api.js'
9
- import { SettingsCard } from './components.js'
10
+ import { SettingsCard, SettingsControlRow } from './components.js'
10
11
  import { mcpClientGuides, type McpClientGuide, type McpClientId } from './mcp-connect.js'
11
12
 
12
13
  type Grant = {
@@ -16,6 +17,38 @@ type Grant = {
16
17
  revokedAt: string | null
17
18
  }
18
19
 
20
+ type TrustedClientEntry = {
21
+ value: string
22
+ source: 'config' | 'settings'
23
+ }
24
+
25
+ type McpSettingsData = {
26
+ autoApproveReconnect: boolean
27
+ autoApproveReconnectFromConfig: boolean
28
+ trustedClients: TrustedClientEntry[]
29
+ }
30
+
31
+ function isClientMatchedByTrusted(clientId: string, entries: TrustedClientEntry[]): boolean {
32
+ let clientOrigin = ''
33
+ try {
34
+ clientOrigin = new URL(clientId).origin
35
+ } catch {
36
+ return false
37
+ }
38
+ const normalizedClient = clientId.replace(/\/+$/, '')
39
+ return entries.some((entry) => {
40
+ try {
41
+ const entryUrl = new URL(entry.value)
42
+ if (entryUrl.pathname === '/' && entry.value === entryUrl.origin) {
43
+ return clientOrigin === entryUrl.origin
44
+ }
45
+ return normalizedClient === entry.value
46
+ } catch {
47
+ return false
48
+ }
49
+ })
50
+ }
51
+
19
52
  async function copyText(value: string, label: string) {
20
53
  try {
21
54
  await navigator.clipboard.writeText(value)
@@ -92,11 +125,25 @@ function ClientPanel({ guide, onCreateKey }: { guide: McpClientGuide; onCreateKe
92
125
  * there" setup other site builders use. The URL comes from the server so the
93
126
  * Host header is never trusted.
94
127
  */
95
- export function McpConnectTab({ onNavigate }: { onNavigate?: (path: string) => void }) {
128
+ export function McpConnectTab({
129
+ onNavigate,
130
+ role,
131
+ }: {
132
+ onNavigate?: (path: string) => void
133
+ role?: string
134
+ }) {
135
+ const canManage = role === 'SUPER_ADMIN' || role === 'ADMIN'
96
136
  const [mcpUrl, setMcpUrl] = useState('')
97
137
  const [loadError, setLoadError] = useState<string | null>(null)
98
138
  const [loading, setLoading] = useState(true)
99
139
  const [grants, setGrants] = useState<Grant[]>([])
140
+ const [settings, setSettings] = useState<McpSettingsData>({
141
+ autoApproveReconnect: true,
142
+ autoApproveReconnectFromConfig: false,
143
+ trustedClients: [],
144
+ })
145
+ const [newTrustedInput, setNewTrustedInput] = useState('')
146
+ const [savingSettings, setSavingSettings] = useState(false)
100
147
  const [client, setClient] = useState<McpClientId>('claude-code')
101
148
 
102
149
  function loadGrants() {
@@ -105,6 +152,12 @@ export function McpConnectTab({ onNavigate }: { onNavigate?: (path: string) => v
105
152
  })
106
153
  }
107
154
 
155
+ function loadSettings() {
156
+ void cmsApi<McpSettingsData>('/mcp/oauth/settings').then((res) => {
157
+ if (res.data) setSettings(res.data)
158
+ })
159
+ }
160
+
108
161
  useEffect(() => {
109
162
  let cancelled = false
110
163
  void cmsApi<{ mcpUrl: string }>('/mcp/oauth/setup').then((res) => {
@@ -119,11 +172,64 @@ export function McpConnectTab({ onNavigate }: { onNavigate?: (path: string) => v
119
172
  setMcpUrl(res.data.mcpUrl)
120
173
  })
121
174
  loadGrants()
175
+ loadSettings()
122
176
  return () => {
123
177
  cancelled = true
124
178
  }
125
179
  }, [])
126
180
 
181
+ async function persistMcpSettings(nextReconnect: boolean, nextDbTrusted: string[]) {
182
+ setSavingSettings(true)
183
+ const res = await cmsApi<McpSettingsData>('/mcp/oauth/settings', {
184
+ method: 'PUT',
185
+ body: JSON.stringify({
186
+ autoApproveReconnect: nextReconnect,
187
+ trustedClients: nextDbTrusted,
188
+ }),
189
+ })
190
+ setSavingSettings(false)
191
+ if (res.error || !res.data) {
192
+ toast.error(res.error ?? 'Could not save MCP settings')
193
+ return false
194
+ }
195
+ setSettings(res.data)
196
+ toast.success('MCP settings saved')
197
+ return true
198
+ }
199
+
200
+ function dbTrustedValues(): string[] {
201
+ return settings.trustedClients.filter((e) => e.source === 'settings').map((e) => e.value)
202
+ }
203
+
204
+ async function handleToggleReconnect(next: boolean) {
205
+ if (!canManage || savingSettings) return
206
+ await persistMcpSettings(next, dbTrustedValues())
207
+ }
208
+
209
+ async function handleAddTrusted(e: FormEvent) {
210
+ e.preventDefault()
211
+ const trimmed = newTrustedInput.trim()
212
+ if (!trimmed || !canManage || savingSettings) return
213
+ const ok = await persistMcpSettings(settings.autoApproveReconnect, [
214
+ ...dbTrustedValues(),
215
+ trimmed,
216
+ ])
217
+ if (ok) setNewTrustedInput('')
218
+ }
219
+
220
+ async function handleRemoveTrusted(value: string) {
221
+ if (!canManage || savingSettings) return
222
+ await persistMcpSettings(
223
+ settings.autoApproveReconnect,
224
+ dbTrustedValues().filter((v) => v !== value),
225
+ )
226
+ }
227
+
228
+ async function handleTrustConnectedClient(clientId: string) {
229
+ if (!canManage || savingSettings) return
230
+ await persistMcpSettings(settings.autoApproveReconnect, [...dbTrustedValues(), clientId])
231
+ }
232
+
127
233
  async function revoke(id: string) {
128
234
  const res = await cmsApi<{ note: string }>(`/mcp/oauth/grants/${encodeURIComponent(id)}`, {
129
235
  method: 'DELETE',
@@ -185,6 +291,93 @@ export function McpConnectTab({ onNavigate }: { onNavigate?: (path: string) => v
185
291
  </Tabs.Root>
186
292
  )}
187
293
 
294
+ <SettingsCard
295
+ title="Auto-approve"
296
+ description="Skip the manual Allow click for reconnects or explicitly trusted HTTPS client origins and CIMD URLs."
297
+ >
298
+ <div className="space-y-5">
299
+ <SettingsControlRow
300
+ label="Auto-approve reconnects"
301
+ description={
302
+ settings.autoApproveReconnectFromConfig
303
+ ? 'Managed in prenta.config.ts (mcp.autoApproveReconnect).'
304
+ : 'Automatically allow an app when you have already approved it and have not revoked its access.'
305
+ }
306
+ control={
307
+ <Switch
308
+ aria-label="Auto-approve reconnects"
309
+ checked={settings.autoApproveReconnect}
310
+ disabled={!canManage || savingSettings || settings.autoApproveReconnectFromConfig}
311
+ onCheckedChange={(checked) => void handleToggleReconnect(checked)}
312
+ />
313
+ }
314
+ />
315
+
316
+ <div className="border-border space-y-3 border-t pt-4">
317
+ <div>
318
+ <h3 className="text-foreground text-sm font-medium">Trusted clients</h3>
319
+ <p className="text-muted-foreground mt-0.5 text-xs">
320
+ Enter an HTTPS origin (e.g. <span className="font-mono">https://claude.ai</span>) or
321
+ an exact client metadata URL to grant editorial access without a consent click.
322
+ </p>
323
+ </div>
324
+
325
+ {settings.trustedClients.length === 0 ? (
326
+ <p className="text-muted-foreground text-sm">No trusted clients configured.</p>
327
+ ) : (
328
+ <ul className="space-y-2">
329
+ {settings.trustedClients.map((entry) => (
330
+ <li
331
+ key={`${entry.source}:${entry.value}`}
332
+ className="border-border flex items-center justify-between gap-3 rounded-md border px-3 py-2 text-sm"
333
+ >
334
+ <span className="min-w-0 truncate font-mono text-xs">{entry.value}</span>
335
+ {entry.source === 'config' ? (
336
+ <span className="bg-muted text-muted-foreground shrink-0 rounded px-2 py-0.5 text-xs font-medium whitespace-nowrap">
337
+ Config
338
+ </span>
339
+ ) : canManage ? (
340
+ <button
341
+ type="button"
342
+ disabled={savingSettings}
343
+ aria-label={`Remove ${entry.value}`}
344
+ onClick={() => void handleRemoveTrusted(entry.value)}
345
+ className="text-muted-foreground hover:text-destructive inline-flex shrink-0 items-center gap-1 text-xs font-medium disabled:opacity-50"
346
+ >
347
+ <Trash2 size={13} aria-hidden="true" />
348
+ Remove
349
+ </button>
350
+ ) : null}
351
+ </li>
352
+ ))}
353
+ </ul>
354
+ )}
355
+
356
+ {canManage && (
357
+ <form onSubmit={(e) => void handleAddTrusted(e)} className="flex flex-wrap gap-2">
358
+ <input
359
+ type="url"
360
+ value={newTrustedInput}
361
+ onChange={(e) => setNewTrustedInput(e.target.value)}
362
+ placeholder="https://claude.ai"
363
+ aria-label="Trusted client HTTPS URL"
364
+ disabled={savingSettings}
365
+ className="border-border bg-input-background text-foreground focus-visible:ring-ring min-w-[240px] flex-1 rounded-md border px-3 py-1.5 font-mono text-xs focus-visible:ring-2 focus-visible:outline-none disabled:opacity-50"
366
+ />
367
+ <button
368
+ type="submit"
369
+ disabled={!newTrustedInput.trim() || savingSettings}
370
+ className="border-border text-foreground hover:bg-accent inline-flex items-center gap-1.5 rounded-md border px-3 py-1.5 text-xs font-medium transition-colors disabled:cursor-not-allowed disabled:opacity-50"
371
+ >
372
+ <Plus size={14} aria-hidden="true" />
373
+ Add trusted client
374
+ </button>
375
+ </form>
376
+ )}
377
+ </div>
378
+ </div>
379
+ </SettingsCard>
380
+
188
381
  <SettingsCard
189
382
  title="Connected apps"
190
383
  description="Revoke stops new requests immediately. An access token already issued can keep working for up to 15 minutes."
@@ -193,18 +386,41 @@ export function McpConnectTab({ onNavigate }: { onNavigate?: (path: string) => v
193
386
  <p className="text-muted-foreground text-sm">No apps connected yet.</p>
194
387
  ) : (
195
388
  <ul className="space-y-2">
196
- {grants.map((grant) => (
197
- <li key={grant.id} className="flex items-center justify-between gap-3 text-sm">
198
- <span className="min-w-0 truncate">{grant.clientId}</span>
199
- <button
200
- type="button"
201
- className="text-destructive focus-visible:ring-ring shrink-0 font-medium hover:underline focus-visible:ring-2 focus-visible:outline-none"
202
- onClick={() => void revoke(grant.id)}
203
- >
204
- Revoke
205
- </button>
206
- </li>
207
- ))}
389
+ {grants.map((grant) => {
390
+ const alreadyTrusted = isClientMatchedByTrusted(
391
+ grant.clientId,
392
+ settings.trustedClients,
393
+ )
394
+ return (
395
+ <li key={grant.id} className="flex items-center justify-between gap-3 text-sm">
396
+ <span className="min-w-0 truncate font-mono text-xs">{grant.clientId}</span>
397
+ <div className="flex shrink-0 items-center gap-3">
398
+ {alreadyTrusted ? (
399
+ <span className="bg-muted text-muted-foreground rounded px-2 py-0.5 text-xs font-medium whitespace-nowrap">
400
+ Trusted
401
+ </span>
402
+ ) : canManage ? (
403
+ <button
404
+ type="button"
405
+ disabled={savingSettings}
406
+ aria-label={`Trust ${grant.clientId}`}
407
+ onClick={() => void handleTrustConnectedClient(grant.clientId)}
408
+ className="text-primary focus-visible:ring-ring font-medium hover:underline focus-visible:ring-2 focus-visible:outline-none disabled:opacity-50"
409
+ >
410
+ Trust
411
+ </button>
412
+ ) : null}
413
+ <button
414
+ type="button"
415
+ className="text-destructive focus-visible:ring-ring font-medium hover:underline focus-visible:ring-2 focus-visible:outline-none"
416
+ onClick={() => void revoke(grant.id)}
417
+ >
418
+ Revoke
419
+ </button>
420
+ </div>
421
+ </li>
422
+ )
423
+ })}
208
424
  </ul>
209
425
  )}
210
426
  </SettingsCard>