From f46c90f7fb200cc78d020449ed919cbeb09f0fc9 Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Sat, 21 Nov 2026 00:42:01 +0100 Subject: [PATCH] feat(32.2-02): admin consent card at /connect posts the decision and leaves for redirect_uri - /connect route without shell and without public, so login round-trips back - ConnectView ready state: client mark, fact box, catalog-order scope picker with the token-dialog * rule, Stay connected, Deny and Allow - Allow/Deny POST /mcp/consent/{request}; the browser leaves through window.location.assign after refusing javascript:, data: and vbscript: - MCPConsent, MCPConsentDecision and AccountItem type aliases - backend::lang.mcp_connect copy in en and pl; connect page title - smoke test: ticks, POST body, assign called, router never used --- admin/src/api/types.ts | 6 + admin/src/app/pageTitle.ts | 3 + admin/src/app/router.ts | 4 + admin/src/views/ConnectView.vue | 330 +++++++++++++++++++ admin/tests/smoke/connect.smoke.test.ts | 87 +++++ modules/phrasebook/backend/lang/en/lang.yaml | 23 ++ modules/phrasebook/backend/lang/pl/lang.yaml | 23 ++ 7 files changed, 476 insertions(+) create mode 100644 admin/src/views/ConnectView.vue create mode 100644 admin/tests/smoke/connect.smoke.test.ts diff --git a/admin/src/api/types.ts b/admin/src/api/types.ts index 865a17ce..97e0cbb2 100644 --- a/admin/src/api/types.ts +++ b/admin/src/api/types.ts @@ -74,6 +74,12 @@ export type AccountContribution = Schemas['cabana.AccountContributionView'] export type AccountContributionAction = Schemas['cabana.AccountContributionActionView'] /** An account contribution action's answer: the toast message and an optional URL. */ export type AccountActionResult = Schemas['cabana.AccountActionResultView'] +/** One row of a contributed My account section with an Items function (id, title, subtitle, meta). */ +export type AccountItem = Schemas['cabana.AccountItemView'] +/** GET /mcp/consent/{request}: the pending MCP connection request an administrator decides. */ +export type MCPConsent = Schemas['cabana.MCPConsentView'] +/** POST /mcp/consent/{request}: where the browser goes after Allow or Deny. */ +export type MCPConsentDecision = Schemas['cabana.MCPConsentDecisionView'] /** A declared bulk action's answer: the toast message and the affected count. */ export type BulkActionResult = Schemas['cabana.BulkActionResult'] /** Body of a widget action: the record id (absent on create) and fill values. */ diff --git a/admin/src/app/pageTitle.ts b/admin/src/app/pageTitle.ts index d31addf8..11334490 100644 --- a/admin/src/app/pageTitle.ts +++ b/admin/src/app/pageTitle.ts @@ -21,6 +21,9 @@ export function pageIndicator(route: RouteLocationNormalizedLoaded): string { if (route.name === 'plugins') { return t('backend::lang.plugins.title') } + if (route.name === 'connect') { + return t('backend::lang.mcp_connect.page_title') + } if (route.name === 'settings-form') { return settingsEntry(String(route.params.code ?? ''))?.label ?? String(route.params.code ?? '') } diff --git a/admin/src/app/router.ts b/admin/src/app/router.ts index 28a1653e..8b3ab243 100644 --- a/admin/src/app/router.ts +++ b/admin/src/app/router.ts @@ -12,6 +12,7 @@ import ReorderView from '../views/ReorderView.vue' import SettingsFormView from '../views/SettingsFormView.vue' import SettingsIndexView from '../views/SettingsIndexView.vue' import AccountView from '../views/AccountView.vue' +import ConnectView from '../views/ConnectView.vue' import PluginsView from '../views/PluginsView.vue' import NotFoundView from '../views/NotFoundView.vue' @@ -74,6 +75,9 @@ export function createAdminRouter(history: RouterHistory = createWebHistory(runt { path: '/account', name: 'account', component: AccountView, meta: { shell: true } }, // The read-only Plugins screen (Settings → System, backend.view_plugins). { path: '/system/plugins', name: 'plugins', component: PluginsView, meta: { shell: true } }, + // The MCP consent card: standalone (no shell) and not public, so an + // anonymous visitor goes through login and comes back here (D-03). + { path: '/connect', name: 'connect', component: ConnectView }, { path: '/:vendor/:plugin/:controller', name: 'list', component: ListView, meta: { shell: true } }, { path: '/:vendor/:plugin/:controller/create', name: 'create', component: FormView, meta: { shell: true } }, // The tree reorder page of a list with config_reorder.yaml (D-09). diff --git a/admin/src/views/ConnectView.vue b/admin/src/views/ConnectView.vue new file mode 100644 index 00000000..57240650 --- /dev/null +++ b/admin/src/views/ConnectView.vue @@ -0,0 +1,330 @@ + + + diff --git a/admin/tests/smoke/connect.smoke.test.ts b/admin/tests/smoke/connect.smoke.test.ts new file mode 100644 index 00000000..2e68eb48 --- /dev/null +++ b/admin/tests/smoke/connect.smoke.test.ts @@ -0,0 +1,87 @@ +// Smoke tests of the MCP consent card (Phase 32.2 UI-SPEC C1): a signed-in +// administrator opens /connect?request=…, keeps the ticks and clicks Allow, +// and the browser leaves for the server-issued redirect_uri through +// window.location.assign, never through the router. Fixtures are neutral. +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { enableAutoUnmount, flushPromises } from '@vue/test-utils' +import type { MCPConsent } from '../../src/api/types' +import { API, mountApp, requestsTo, resetState, type Route } from '../helpers' + +const REDIRECT = 'https://agent.example.test/callback?code=abc123&state=xyz' + +function consentView(overrides: Partial = {}): MCPConsent { + return { + request_id: 'req1', + client: { id: 'client-1', name: 'Acme Agent', redirect_host: 'agent.example.test', logo_url: '' }, + admin: { login: 'dev', name: 'Dana Dev', email: 'dev@example.test' }, + scopes: [ + { name: 'admin:read', label: 'Read the admin', description: 'Read every admin list.', read_only: true, known: true }, + { name: 'acme', label: 'Acme', description: 'Use the acme tools.', read_only: false, known: true }, + ], + offline_access: true, + consent_required: true, + expires_at: '2026-11-21T12:00:00Z', + ...overrides, + } +} + +function consentRoutes(view: MCPConsent = consentView(), decide?: Route): Record { + return { + [`GET ${API}/mcp/consent/req1`]: { body: { data: view, meta: {} } }, + [`POST ${API}/mcp/consent/req1`]: decide ?? { body: { data: { redirect_uri: REDIRECT }, meta: {} } }, + } +} + +let assign: ReturnType + +beforeEach(() => { + resetState() + assign = vi.fn() + vi.spyOn(window.location, 'assign').mockImplementation(assign) +}) + +afterEach(() => { + vi.restoreAllMocks() + document.body.innerHTML = '' +}) + +enableAutoUnmount(afterEach) + +describe('consent card (UI-SPEC C1)', () => { + it('Allow posts the ticked scopes and Stay connected, then leaves for redirect_uri without the router', async () => { + const { wrapper, calls, router } = await mountApp('/connect?request=req1', consentRoutes()) + const push = vi.spyOn(router, 'push') + const replace = vi.spyOn(router, 'replace') + + expect(router.currentRoute.value.name).toBe('connect') + const card = wrapper.find('[data-connect-state="ready"]') + expect(card.exists()).toBe(true) + // Standalone: no navigation shell around the card. + expect(wrapper.find('nav[aria-label="backend::lang.nav.plugins"]').exists()).toBe(false) + expect(card.find('[data-connect-host]').text()).toBe('agent.example.test') + + const scopeBoxes = card.findAll('[data-scope] input[type="checkbox"]') + expect(scopeBoxes).toHaveLength(2) + expect(scopeBoxes.map((box) => (box.element as HTMLInputElement).checked)).toEqual([true, true]) + const offline = card.find('[data-connect-offline] input[type="checkbox"]') + expect((offline.element as HTMLInputElement).checked).toBe(true) + + // Nothing is posted before the click. + expect(requestsTo(calls, 'POST', `${API}/mcp/consent/req1`)).toHaveLength(0) + + await card.trigger('submit') + await flushPromises() + + const posts = requestsTo(calls, 'POST', `${API}/mcp/consent/req1`) + expect(posts).toHaveLength(1) + expect(posts[0]!.headers.get('X-Requested-With')).toBe('XMLHttpRequest') + expect(await posts[0]!.json()).toEqual({ decision: 'allow', scopes: ['admin:read', 'acme'], offline_access: true }) + expect(assign).toHaveBeenCalledWith(REDIRECT) + for (const spy of [push, replace]) { + expect(spy).not.toHaveBeenCalledWith(REDIRECT) + for (const call of spy.mock.calls) { + expect(JSON.stringify(call)).not.toContain('agent.example.test') + } + } + }) +}) diff --git a/modules/phrasebook/backend/lang/en/lang.yaml b/modules/phrasebook/backend/lang/en/lang.yaml index 0a7f8792..944fe2c3 100644 --- a/modules/phrasebook/backend/lang/en/lang.yaml +++ b/modules/phrasebook/backend/lang/en/lang.yaml @@ -351,6 +351,29 @@ api_tokens: mcp_connect: request_not_found: This connection request has expired or does not exist. no_grantable_scopes: Choose at least one scope you can use. + page_title: Connect an agent + heading: Allow :client to use the admin as you? + subline: The agent gets your permissions, limited to the scopes you tick. It never gets more access than your own account. + signed_in_as: Signed in as + admin_with_email: ":name (:email)" + switch_account: Use another account + returns_to: Returns to + offline: Stay connected + offline_description: The agent can renew its access without asking you again. Untick it and the agent has to be allowed again when its access expires. + previously_allowed: You have allowed :client before. + allow: Allow + allowing: Allowing… + deny: Deny + returning: Returning you to :host… + continue: Continue to :host + login_hint: Sign in to finish connecting your MCP agent. + none_grantable: Your account has none of the permissions this agent asks for. Deny the request, or ask a superuser for the permissions and connect again. + expired_heading: This connection request has expired + expired_body: Connection requests are valid for a few minutes and work only once. Go back to your agent and connect again. + open_admin: Go to the admin panel + load_failed: The connection request could not be loaded. Check your connection and try again. + retry: Try again + decide_failed: Your answer could not be sent. Try again. myaccount: menu_label: My account menu_description: Edit your name, email, subtitle and password, and manage your API tokens. diff --git a/modules/phrasebook/backend/lang/pl/lang.yaml b/modules/phrasebook/backend/lang/pl/lang.yaml index 716d6e1d..2fbdaf40 100644 --- a/modules/phrasebook/backend/lang/pl/lang.yaml +++ b/modules/phrasebook/backend/lang/pl/lang.yaml @@ -373,6 +373,29 @@ api_tokens: mcp_connect: request_not_found: To żądanie połączenia wygasło albo nie istnieje. no_grantable_scopes: Wybierz co najmniej jeden zakres, którego możesz użyć. + page_title: Połącz agenta + heading: Pozwolić aplikacji :client korzystać z panelu w Twoim imieniu? + subline: Agent otrzyma Twoje uprawnienia w granicach zaznaczonych zakresów. Nigdy nie dostanie szerszego dostępu niż Twoje konto. + signed_in_as: Zalogowano jako + admin_with_email: ":name (:email)" + switch_account: Użyj innego konta + returns_to: Wrócisz do + offline: Pozostań połączony + offline_description: Agent może odnawiać dostęp bez ponownego pytania. Po odznaczeniu trzeba będzie ponownie wyrazić zgodę, gdy jego dostęp wygaśnie. + previously_allowed: Aplikacja :client ma już Twoją zgodę. + allow: Zezwól + allowing: Zezwalanie… + deny: Odmów + returning: Wracasz do :host… + continue: Przejdź do :host + login_hint: Zaloguj się, aby dokończyć łączenie agenta MCP. + none_grantable: Twoje konto nie ma żadnego z uprawnień, o które prosi ten agent. Odmów albo poproś superużytkownika o uprawnienia i połącz się ponownie. + expired_heading: Ta prośba o połączenie wygasła + expired_body: Prośba o połączenie jest ważna kilka minut i działa tylko raz. Wróć do agenta i połącz się ponownie. + open_admin: Przejdź do panelu + load_failed: Nie udało się wczytać prośby o połączenie. Sprawdź połączenie i spróbuj ponownie. + retry: Spróbuj ponownie + decide_failed: Nie udało się wysłać odpowiedzi. Spróbuj ponownie. myaccount: menu_label: Moje konto menu_description: Zmień imię, nazwisko, e-mail, podtytuł i hasło oraz zarządzaj tokenami API.