MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / forcedMcpConsent

Function forcedMcpConsent

e2e/targets/selfhost.ts:56–102  ·  view source on GitHub ↗
(baseUrl: string, credentials: { readonly email: string; readonly password: string })

Source from the content-addressed store, hash-verified

54// then POST the same `/api/auth/oauth2/consent` grant the Allow button fires.
55export const forcedMcpConsent =
56 (baseUrl: string, credentials: { readonly email: string; readonly password: string }) =>
57 async ({ authorizationUrl }: { authorizationUrl: string }): Promise<{ code: string }> => {
58 const origin = new URL(baseUrl).origin;
59 const { cookieHeader } = await signInSession(baseUrl, credentials);
60
61 const authorize = await fetch(authorizationUrl, {
62 headers: { cookie: cookieHeader },
63 redirect: "manual",
64 });
65 const location = authorize.headers.get("location");
66 if (!location) {
67 throw new Error(`forcedMcpConsent: authorize did not redirect (status ${authorize.status})`);
68 }
69 // The consent redirect is relative (`/mcp-consent?...`) — resolve it against
70 // the instance origin. If the server issued a code directly (consent not
71 // forced), use it; otherwise complete the forced approval below.
72 const redirect = new URL(location, baseUrl);
73 const direct = redirect.searchParams.get("code");
74 if (direct) return { code: direct };
75 const consentCode = redirect.searchParams.get("consent_code");
76 if (!consentCode) {
77 throw new Error(`forcedMcpConsent: no consent_code in authorize redirect: ${location}`);
78 }
79
80 const decision = await fetch(new URL("/api/auth/oauth2/consent", baseUrl), {
81 method: "POST",
82 headers: { "content-type": "application/json", origin, cookie: cookieHeader },
83 body: JSON.stringify({ accept: true, consent_code: consentCode }),
84 });
85 if (!decision.ok) {
86 throw new Error(`forcedMcpConsent: consent grant failed (status ${decision.status})`);
87 }
88 const decisionText = await decision.text();
89 if (!decisionText) {
90 throw new Error(
91 `forcedMcpConsent: consent grant returned an empty body (status ${decision.status})`,
92 );
93 }
94 const body = JSON.parse(decisionText) as { redirectURI?: string };
95 const code = body.redirectURI ? new URL(body.redirectURI).searchParams.get("code") : null;
96 if (!code) {
97 throw new Error(
98 `forcedMcpConsent: no code in consent redirect: ${body.redirectURI ?? "(none)"}`,
99 );
100 }
101 return { code };
102 };
103
104// A second, distinct member of the (single) selfhost org: admin invite →
105// invited email signup → Better Auth session. What multi-user scenarios use

Callers 2

selfhostDockerTargetFunction · 0.90
selfhostTargetFunction · 0.85

Calls 4

signInSessionFunction · 0.85
getMethod · 0.65
textMethod · 0.65
fetchFunction · 0.50

Tested by

no test coverage detected