Admin tools for Claude: an MCP server, connected with an admin's approval
- services/mcp: the MCP server at /mcp (Streamable HTTP, stateless): the admin's reading tools, the daily tasks and the forum's moderation; every tool calls the site's API with the caller's token, and members' e-mail addresses and names are masked in the answers. - Backend: OAuth 2.1 for MCP clients (registration with Claude's or a local return address only, PKCE, rotating refresh tokens, revocation); the admin guards accept the token for reading and the writes in MCP_WRITES only. - Forum: an admin's token reaches the moderator endpoints only. - Frontend: the approval page (/oauth/engedely), Admin → Claude to revoke a connection, the authorization server's metadata at its standard address. - Deploy: the mcp service in compose, Caddy, CI; the worker removes old codes, tokens and clients. docs/mcp.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -14,6 +14,11 @@ const config: NextConfig = {
|
||||
"/kep/fooldal.png": ["./src/assets/og/*.ttf"],
|
||||
"/hires-emberek/[slug]/kep/[file]": ["./src/assets/og/*.ttf"],
|
||||
},
|
||||
// OAuth for the MCP server's clients (docs/mcp.md): the authorization
|
||||
// server's metadata, from the backend, at its standard address.
|
||||
async rewrites() {
|
||||
return [{ source: "/.well-known/oauth-authorization-server", destination: "/api/v1/oauth/metadata" }];
|
||||
},
|
||||
images: {
|
||||
// Only the famous people's photos (served by the backend through the
|
||||
// /api/v1 proxy, one stored version each) go through the optimizer.
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import { confirmDialog } from "@/components/Dialogs";
|
||||
import { api, errorText } from "@/lib/api";
|
||||
import { formatDate } from "@/lib/astro";
|
||||
import type { OauthGrant } from "@/lib/types";
|
||||
|
||||
/** The MCP clients connected to the admin tools, and revoking them. */
|
||||
export function Connections() {
|
||||
const [grants, setGrants] = useState<OauthGrant[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
setGrants((await api<{ grants: OauthGrant[] }>("/admin/oauth/grants")).grants);
|
||||
setError(null);
|
||||
} catch (e) {
|
||||
setError(errorText(e));
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
load();
|
||||
}, [load]);
|
||||
|
||||
async function revoke(grant: OauthGrant) {
|
||||
const ok = await confirmDialog({
|
||||
title: `Visszavonod: ${grant.client_name}?`,
|
||||
text: "Az alkalmazás azonnal elveszíti a hozzáférést. Újra összekötni Claude-ban lehet.",
|
||||
confirm: "Visszavonás",
|
||||
danger: true,
|
||||
});
|
||||
if (!ok) return;
|
||||
try {
|
||||
await api(`/admin/oauth/grants/${grant.id}`, { method: "DELETE" });
|
||||
await load();
|
||||
} catch (e) {
|
||||
setError(errorText(e));
|
||||
}
|
||||
}
|
||||
|
||||
if (error) return <p className="msg msg-error">{error}</p>;
|
||||
if (!grants) return <p className="muted">Betöltés…</p>;
|
||||
if (grants.length === 0) return <p className="muted">Nincs kapcsolt alkalmazás.</p>;
|
||||
return (
|
||||
<div className="table-wrap">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Alkalmazás</th>
|
||||
<th>Admin</th>
|
||||
<th>Összekötve</th>
|
||||
<th>Utoljára használta</th>
|
||||
<th aria-label="Műveletek" />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{grants.map((g) => (
|
||||
<tr key={g.id}>
|
||||
<td>
|
||||
{g.client_name}
|
||||
{g.redirect_host && <span className="hint"> · {g.redirect_host}</span>}
|
||||
</td>
|
||||
<td>{g.user_email}</td>
|
||||
<td>{formatDate(g.created_at, true)}</td>
|
||||
<td>{formatDate(g.last_used_at, true)}</td>
|
||||
<td>
|
||||
<button type="button" className="btn btn-sm btn-danger" onClick={() => revoke(g)}>
|
||||
Visszavonás
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import type { Metadata } from "next";
|
||||
import { notFound } from "next/navigation";
|
||||
|
||||
import { AdminNav } from "@/components/AdminNav";
|
||||
import { requireMe } from "@/lib/server";
|
||||
|
||||
import { Connections } from "./Connections";
|
||||
|
||||
export const metadata: Metadata = { title: "Claude" };
|
||||
|
||||
export default async function AdminConnectionsPage() {
|
||||
const me = await requireMe("/admin/kapcsolatok");
|
||||
if (!me.user.roles.includes("admin")) notFound();
|
||||
return (
|
||||
<main className="page">
|
||||
<AdminNav current="/admin/kapcsolatok" />
|
||||
<div className="page-head">
|
||||
<h1>Claude</h1>
|
||||
<p className="muted">
|
||||
Az admin eszközökhöz kapcsolt alkalmazások. Claude-ban az összekötő címe:{" "}
|
||||
<code>https://sorsfonalak.hu/mcp</code>. Az összekötéskor admin fiókkal kell belépni és engedélyezni.
|
||||
</p>
|
||||
</div>
|
||||
<Connections />
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -1931,3 +1931,8 @@ span.tarot-fan-card { cursor: default; }
|
||||
.spark-axis { margin-left: 0; }
|
||||
.funnel li { grid-template-columns: minmax(0, 1fr); gap: 4px; }
|
||||
}
|
||||
|
||||
/* The OAuth approval page (/oauth/engedely). */
|
||||
.oauth-scope { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 8px; }
|
||||
.oauth-actions { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
|
||||
.oauth-actions .msg { flex-basis: 100%; }
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
|
||||
import { api, errorText } from "@/lib/api";
|
||||
|
||||
export type OauthParams = Partial<
|
||||
Record<
|
||||
| "response_type"
|
||||
| "client_id"
|
||||
| "redirect_uri"
|
||||
| "code_challenge"
|
||||
| "code_challenge_method"
|
||||
| "scope"
|
||||
| "state"
|
||||
| "resource",
|
||||
string
|
||||
>
|
||||
>;
|
||||
|
||||
/** The admin's answer: the backend gives the address back to the client (with a code, or a refusal). */
|
||||
export function Approve({ params }: { params: OauthParams }) {
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
async function answer(deny: boolean) {
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const r = await api<{ redirect_to: string }>("/oauth/authorize", { body: { ...params, deny } });
|
||||
window.location.assign(r.redirect_to);
|
||||
} catch (e) {
|
||||
setError(errorText(e));
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="oauth-actions">
|
||||
<button type="button" className="btn btn-primary" disabled={busy} onClick={() => answer(false)}>
|
||||
Engedélyezem
|
||||
</button>
|
||||
<button type="button" className="btn" disabled={busy} onClick={() => answer(true)}>
|
||||
Mégse
|
||||
</button>
|
||||
{error && (
|
||||
<p className="msg msg-error" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import type { Metadata } from "next";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { BACKEND_URL, getMe } from "@/lib/server";
|
||||
|
||||
import { Approve, type OauthParams } from "./Approve";
|
||||
|
||||
export const metadata: Metadata = { title: "Claude összekötése", robots: { index: false } };
|
||||
|
||||
type Props = { searchParams: Promise<Record<string, string | string[] | undefined>> };
|
||||
|
||||
const PARAMS = [
|
||||
"response_type",
|
||||
"client_id",
|
||||
"redirect_uri",
|
||||
"code_challenge",
|
||||
"code_challenge_method",
|
||||
"scope",
|
||||
"state",
|
||||
"resource",
|
||||
] as const;
|
||||
|
||||
interface Check {
|
||||
client_name: string;
|
||||
redirect_host: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The OAuth approval page (docs/mcp.md): Claude sends the admin here to
|
||||
* connect the admin tools. The backend checks the request first; only an
|
||||
* admin may approve it.
|
||||
*/
|
||||
export default async function OauthApprovalPage({ searchParams }: Props) {
|
||||
const raw = await searchParams;
|
||||
const params: OauthParams = {};
|
||||
const query = new URLSearchParams();
|
||||
for (const name of PARAMS) {
|
||||
const value = Array.isArray(raw[name]) ? raw[name][0] : raw[name];
|
||||
if (value !== undefined) {
|
||||
params[name] = value;
|
||||
query.set(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
let check: Check | null = null;
|
||||
let problem = "A kérés nem érkezett meg rendesen.";
|
||||
try {
|
||||
const res = await fetch(`${BACKEND_URL}/v1/oauth/authorize?${query}`, { cache: "no-store" });
|
||||
const body = await res.json();
|
||||
if (res.ok) check = body as Check;
|
||||
else problem = body?.error?.message ?? problem;
|
||||
} catch {
|
||||
problem = "A szerver most nem érhető el. Próbáld újra.";
|
||||
}
|
||||
|
||||
if (!check) {
|
||||
return (
|
||||
<main className="page" style={{ maxWidth: 560 }}>
|
||||
<div className="page-head">
|
||||
<h1>Az összekötés nem sikerült</h1>
|
||||
<p className="muted">{problem}</p>
|
||||
<p className="hint">Próbáld újra az összekötést Claude-ban.</p>
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
const me = await getMe();
|
||||
if (!me) redirect(`/belepes?vissza=${encodeURIComponent(`/oauth/engedely?${query}`)}`);
|
||||
if (!me.user.roles.includes("admin")) {
|
||||
return (
|
||||
<main className="page" style={{ maxWidth: 560 }}>
|
||||
<div className="page-head">
|
||||
<h1>Ehhez admin fiók kell</h1>
|
||||
<p className="muted">Az admin eszközöket csak admin fiókkal lehet Claude-dal összekötni.</p>
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<main className="page" style={{ maxWidth: 560 }}>
|
||||
<div className="page-head">
|
||||
<span className="eyebrow">Admin eszközök</span>
|
||||
<h1>{check.client_name} hozzáférést kér</h1>
|
||||
</div>
|
||||
<section className="card">
|
||||
<p>
|
||||
Ha engedélyezed, a(z) <strong>{check.client_name}</strong> a te admin jogoddal használhatja a Sorsfonalak
|
||||
admin eszközeit.
|
||||
</p>
|
||||
<ul className="oauth-scope">
|
||||
<li>
|
||||
Lekérdezheti a statisztikát, a visszajelzéseket, az értékeléseket, a hibanaplót, a háttérfeladatokat, a
|
||||
tagokat, a promókódokat és a meghívókat. A tagok e-mail-címe és neve rejtve marad.
|
||||
</li>
|
||||
<li>
|
||||
Elvégezheti a napi teendőket: promókód készítése, visszajelzés megválaszolása, feladat újraindítása, hiba
|
||||
törlése, meghívó küldése, fórumbejelentés lezárása, kitiltás. Minden ilyen lépés előtt rákérdez.
|
||||
</li>
|
||||
<li>Szerepkört, kreditet, csomagot, promptot és modellt nem módosíthat: ezek az admin oldalon maradnak.</li>
|
||||
</ul>
|
||||
{check.redirect_host && <p className="hint">Engedélyezés után visszakerülsz ide: {check.redirect_host}.</p>}
|
||||
<p className="hint">A kapcsolatot bármikor visszavonhatod: Admin → Claude.</p>
|
||||
<Approve params={params} />
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -15,6 +15,7 @@ const PAGES = [
|
||||
{ href: "/admin/promptok", label: "Promptok" },
|
||||
{ href: "/admin/modellek", label: "Modellek" },
|
||||
{ href: "/admin/hibak", label: "Hibanapló" },
|
||||
{ href: "/admin/kapcsolatok", label: "Claude" },
|
||||
];
|
||||
|
||||
/** May manage the famous people (an admin or a creator). */
|
||||
|
||||
@@ -952,3 +952,13 @@ export interface AdminStats {
|
||||
content: { kind: string; done: number; failed: number; cost_usd: number }[];
|
||||
weekly: { week_start: string; readers: number; emails: number }[];
|
||||
}
|
||||
|
||||
/** An MCP client an admin connected to the admin tools (docs/mcp.md). */
|
||||
export interface OauthGrant {
|
||||
id: string;
|
||||
client_name: string;
|
||||
redirect_host: string | null;
|
||||
user_email: string;
|
||||
created_at: string;
|
||||
last_used_at: string | null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user