@studiometa/forge-mcp 0.2.2 → 0.2.3
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.
- package/dist/auth.d.ts +12 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +19 -1
- package/dist/auth.js.map +1 -1
- package/dist/crypto.d.ts +56 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +110 -0
- package/dist/crypto.js.map +1 -0
- package/dist/{http-BMOiJdyw.js → http-CK8WsamV.js} +16 -4
- package/dist/http-CK8WsamV.js.map +1 -0
- package/dist/http.d.ts +1 -1
- package/dist/http.d.ts.map +1 -1
- package/dist/http.js +2 -2
- package/dist/index.js +1 -1
- package/dist/oauth.d.ts +118 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +571 -0
- package/dist/oauth.js.map +1 -0
- package/dist/server.d.ts +24 -6
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +33 -9
- package/dist/server.js.map +1 -1
- package/dist/{version-D3OFS3DQ.js → version-BTMdX8xQ.js} +2 -2
- package/dist/{version-D3OFS3DQ.js.map → version-BTMdX8xQ.js.map} +1 -1
- package/package.json +9 -1
- package/dist/http-BMOiJdyw.js.map +0 -1
package/dist/oauth.js
ADDED
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
import { createAuthCode, decodeAuthCode } from "./crypto.js";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { defineEventHandler, getQuery, getRequestHeader, readBody, sendRedirect, setResponseHeader, setResponseStatus } from "h3";
|
|
4
|
+
/**
|
|
5
|
+
* OAuth 2.1 endpoints for Claude Desktop integration
|
|
6
|
+
*
|
|
7
|
+
* Implements OAuth 2.1 with PKCE as specified in the MCP authorization spec.
|
|
8
|
+
* Uses stateless encrypted tokens — no server-side storage required.
|
|
9
|
+
*
|
|
10
|
+
* Spec: https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization
|
|
11
|
+
*
|
|
12
|
+
* Flow:
|
|
13
|
+
* 1. Claude redirects user to /authorize with OAuth params (including PKCE)
|
|
14
|
+
* 2. User enters their Forge API token in a login form
|
|
15
|
+
* 3. Server encrypts the token + PKCE challenge into an authorization code
|
|
16
|
+
* 4. Redirects back to Claude with the code
|
|
17
|
+
* 5. Claude exchanges code for access token via /token (with code_verifier)
|
|
18
|
+
* 6. Server validates PKCE and returns base64-encoded access token
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Create a base64-encoded access token from a Forge API token.
|
|
22
|
+
* The access token is simply base64(apiToken) so that parseAuthHeader
|
|
23
|
+
* can decode it on every request without any server-side lookup.
|
|
24
|
+
*/
|
|
25
|
+
function createAccessToken(apiToken) {
|
|
26
|
+
return Buffer.from(apiToken).toString("base64");
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* OAuth metadata for discovery (RFC 8414)
|
|
30
|
+
* GET /.well-known/oauth-authorization-server
|
|
31
|
+
*
|
|
32
|
+
* MCP clients MUST check this endpoint first for server capabilities.
|
|
33
|
+
*/
|
|
34
|
+
const oauthMetadataHandler = defineEventHandler((event) => {
|
|
35
|
+
const host = getRequestHeader(event, "host") || "localhost:3000";
|
|
36
|
+
const baseUrl = `${getRequestHeader(event, "x-forwarded-proto") || "http"}://${host}`;
|
|
37
|
+
setResponseHeader(event, "Content-Type", "application/json");
|
|
38
|
+
setResponseHeader(event, "Cache-Control", "public, max-age=3600");
|
|
39
|
+
return {
|
|
40
|
+
issuer: baseUrl,
|
|
41
|
+
authorization_endpoint: `${baseUrl}/authorize`,
|
|
42
|
+
token_endpoint: `${baseUrl}/token`,
|
|
43
|
+
response_types_supported: ["code"],
|
|
44
|
+
grant_types_supported: ["authorization_code", "refresh_token"],
|
|
45
|
+
code_challenge_methods_supported: ["S256"],
|
|
46
|
+
token_endpoint_auth_methods_supported: ["none"],
|
|
47
|
+
registration_endpoint: `${baseUrl}/register`,
|
|
48
|
+
scopes_supported: ["forge"],
|
|
49
|
+
service_documentation: "https://github.com/studiometa/forge-tools"
|
|
50
|
+
};
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* Protected resource metadata (RFC 9728)
|
|
54
|
+
* GET /.well-known/oauth-protected-resource
|
|
55
|
+
*
|
|
56
|
+
* Tells MCP clients where to find the OAuth authorization server.
|
|
57
|
+
*/
|
|
58
|
+
const protectedResourceHandler = defineEventHandler((event) => {
|
|
59
|
+
const host = getRequestHeader(event, "host") || "localhost:3000";
|
|
60
|
+
const baseUrl = `${getRequestHeader(event, "x-forwarded-proto") || "http"}://${host}`;
|
|
61
|
+
setResponseHeader(event, "Content-Type", "application/json");
|
|
62
|
+
setResponseHeader(event, "Cache-Control", "public, max-age=3600");
|
|
63
|
+
return {
|
|
64
|
+
resource: `${baseUrl}/mcp`,
|
|
65
|
+
authorization_servers: [baseUrl],
|
|
66
|
+
bearer_methods_supported: ["header"],
|
|
67
|
+
scopes_supported: ["forge"]
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
/**
|
|
71
|
+
* Dynamic Client Registration endpoint (RFC 7591)
|
|
72
|
+
* POST /register
|
|
73
|
+
*
|
|
74
|
+
* MCP servers SHOULD support DCR to allow clients to register automatically.
|
|
75
|
+
* Since we use stateless tokens, we accept any registration and return
|
|
76
|
+
* a generated client_id.
|
|
77
|
+
*/
|
|
78
|
+
const registerHandler = defineEventHandler(async (event) => {
|
|
79
|
+
setResponseHeader(event, "Content-Type", "application/json");
|
|
80
|
+
let body;
|
|
81
|
+
try {
|
|
82
|
+
body = await readBody(event);
|
|
83
|
+
} catch {
|
|
84
|
+
setResponseStatus(event, 400);
|
|
85
|
+
return {
|
|
86
|
+
error: "invalid_request",
|
|
87
|
+
error_description: "Invalid JSON body"
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const clientName = body.client_name || "MCP Client";
|
|
91
|
+
const redirectUris = body.redirect_uris || [];
|
|
92
|
+
const clientId = Buffer.from(JSON.stringify({
|
|
93
|
+
name: clientName,
|
|
94
|
+
ts: Date.now()
|
|
95
|
+
})).toString("base64url");
|
|
96
|
+
setResponseStatus(event, 201);
|
|
97
|
+
return {
|
|
98
|
+
client_id: clientId,
|
|
99
|
+
client_name: clientName,
|
|
100
|
+
redirect_uris: redirectUris,
|
|
101
|
+
token_endpoint_auth_method: "none",
|
|
102
|
+
grant_types: ["authorization_code", "refresh_token"],
|
|
103
|
+
response_types: ["code"]
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
/**
|
|
107
|
+
* Authorization endpoint — shows login form
|
|
108
|
+
* GET /authorize
|
|
109
|
+
*/
|
|
110
|
+
const authorizeGetHandler = defineEventHandler((event) => {
|
|
111
|
+
const query = getQuery(event);
|
|
112
|
+
const redirectUri = query.redirect_uri;
|
|
113
|
+
const state = query.state;
|
|
114
|
+
const codeChallenge = query.code_challenge;
|
|
115
|
+
const codeChallengeMethod = query.code_challenge_method;
|
|
116
|
+
if (!redirectUri) {
|
|
117
|
+
setResponseHeader(event, "Content-Type", "text/html; charset=utf-8");
|
|
118
|
+
setResponseStatus(event, 400);
|
|
119
|
+
return renderErrorPage("Missing required parameter: redirect_uri");
|
|
120
|
+
}
|
|
121
|
+
if (!codeChallenge) {
|
|
122
|
+
const errorUrl = new URL(redirectUri);
|
|
123
|
+
errorUrl.searchParams.set("error", "invalid_request");
|
|
124
|
+
errorUrl.searchParams.set("error_description", "code_challenge is required");
|
|
125
|
+
if (state) errorUrl.searchParams.set("state", state);
|
|
126
|
+
return sendRedirect(event, errorUrl.toString(), 302);
|
|
127
|
+
}
|
|
128
|
+
if (codeChallengeMethod && codeChallengeMethod !== "S256") {
|
|
129
|
+
const errorUrl = new URL(redirectUri);
|
|
130
|
+
errorUrl.searchParams.set("error", "invalid_request");
|
|
131
|
+
errorUrl.searchParams.set("error_description", "Only S256 code_challenge_method is supported");
|
|
132
|
+
if (state) errorUrl.searchParams.set("state", state);
|
|
133
|
+
return sendRedirect(event, errorUrl.toString(), 302);
|
|
134
|
+
}
|
|
135
|
+
setResponseHeader(event, "Content-Type", "text/html; charset=utf-8");
|
|
136
|
+
return renderLoginForm({
|
|
137
|
+
redirectUri,
|
|
138
|
+
state,
|
|
139
|
+
codeChallenge,
|
|
140
|
+
codeChallengeMethod: codeChallengeMethod || "S256"
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
/**
|
|
144
|
+
* Authorization endpoint — process login
|
|
145
|
+
* POST /authorize
|
|
146
|
+
*/
|
|
147
|
+
const authorizePostHandler = defineEventHandler(async (event) => {
|
|
148
|
+
const { apiToken, redirectUri, state, codeChallenge, codeChallengeMethod } = await readBody(event);
|
|
149
|
+
if (!redirectUri) {
|
|
150
|
+
setResponseHeader(event, "Content-Type", "text/html; charset=utf-8");
|
|
151
|
+
setResponseStatus(event, 400);
|
|
152
|
+
return renderErrorPage("Missing redirect_uri parameter");
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
const uri = new URL(redirectUri);
|
|
156
|
+
const isLocalhost = uri.hostname === "localhost" || uri.hostname === "127.0.0.1";
|
|
157
|
+
const isHttps = uri.protocol === "https:";
|
|
158
|
+
if (!isLocalhost && !isHttps) {
|
|
159
|
+
setResponseStatus(event, 400);
|
|
160
|
+
return renderErrorPage("redirect_uri must be HTTPS or localhost");
|
|
161
|
+
}
|
|
162
|
+
} catch {
|
|
163
|
+
setResponseStatus(event, 400);
|
|
164
|
+
return renderErrorPage("Invalid redirect_uri format");
|
|
165
|
+
}
|
|
166
|
+
if (!apiToken) {
|
|
167
|
+
setResponseHeader(event, "Content-Type", "text/html; charset=utf-8");
|
|
168
|
+
return renderLoginForm({
|
|
169
|
+
redirectUri,
|
|
170
|
+
state,
|
|
171
|
+
codeChallenge,
|
|
172
|
+
codeChallengeMethod,
|
|
173
|
+
error: "Forge API Token is required"
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
const code = createAuthCode({
|
|
177
|
+
apiToken,
|
|
178
|
+
codeChallenge,
|
|
179
|
+
codeChallengeMethod: codeChallengeMethod || "S256"
|
|
180
|
+
});
|
|
181
|
+
const redirectUrl = new URL(redirectUri);
|
|
182
|
+
redirectUrl.searchParams.set("code", code);
|
|
183
|
+
if (state) redirectUrl.searchParams.set("state", state);
|
|
184
|
+
setResponseHeader(event, "Content-Type", "text/html; charset=utf-8");
|
|
185
|
+
return renderSuccessPage(redirectUrl.toString());
|
|
186
|
+
});
|
|
187
|
+
/**
|
|
188
|
+
* Token endpoint — exchange code for access token
|
|
189
|
+
* POST /token
|
|
190
|
+
*
|
|
191
|
+
* Supports:
|
|
192
|
+
* - authorization_code grant (with PKCE validation)
|
|
193
|
+
* - refresh_token grant
|
|
194
|
+
*/
|
|
195
|
+
const tokenHandler = defineEventHandler(async (event) => {
|
|
196
|
+
setResponseHeader(event, "Content-Type", "application/json");
|
|
197
|
+
const { grant_type, code, code_verifier, refresh_token } = await readBody(event);
|
|
198
|
+
if (grant_type === "refresh_token") return handleRefreshToken(event, refresh_token);
|
|
199
|
+
if (grant_type !== "authorization_code") {
|
|
200
|
+
setResponseStatus(event, 400);
|
|
201
|
+
return {
|
|
202
|
+
error: "unsupported_grant_type",
|
|
203
|
+
error_description: "Supported grant types: authorization_code, refresh_token"
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
if (!code) {
|
|
207
|
+
setResponseStatus(event, 400);
|
|
208
|
+
return {
|
|
209
|
+
error: "invalid_request",
|
|
210
|
+
error_description: "Missing authorization code"
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
if (!code_verifier) {
|
|
214
|
+
setResponseStatus(event, 400);
|
|
215
|
+
return {
|
|
216
|
+
error: "invalid_request",
|
|
217
|
+
error_description: "Missing code_verifier (PKCE required)"
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
try {
|
|
221
|
+
const payload = decodeAuthCode(code);
|
|
222
|
+
if (payload.codeChallenge) {
|
|
223
|
+
if (createS256Challenge(code_verifier) !== payload.codeChallenge) {
|
|
224
|
+
setResponseStatus(event, 400);
|
|
225
|
+
return {
|
|
226
|
+
error: "invalid_grant",
|
|
227
|
+
error_description: "Invalid code_verifier"
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
access_token: createAccessToken(payload.apiToken),
|
|
233
|
+
token_type: "Bearer",
|
|
234
|
+
expires_in: 3600,
|
|
235
|
+
refresh_token: createAuthCode({ apiToken: payload.apiToken }, 86400 * 30)
|
|
236
|
+
};
|
|
237
|
+
} catch (error) {
|
|
238
|
+
setResponseStatus(event, 400);
|
|
239
|
+
return {
|
|
240
|
+
error: "invalid_grant",
|
|
241
|
+
error_description: error instanceof Error ? error.message : "Invalid authorization code"
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
/**
|
|
246
|
+
* Handle refresh token grant
|
|
247
|
+
*/
|
|
248
|
+
function handleRefreshToken(event, refreshToken) {
|
|
249
|
+
if (!refreshToken) {
|
|
250
|
+
setResponseStatus(event, 400);
|
|
251
|
+
return {
|
|
252
|
+
error: "invalid_request",
|
|
253
|
+
error_description: "Missing refresh_token"
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
try {
|
|
257
|
+
const payload = decodeAuthCode(refreshToken);
|
|
258
|
+
return {
|
|
259
|
+
access_token: createAccessToken(payload.apiToken),
|
|
260
|
+
token_type: "Bearer",
|
|
261
|
+
expires_in: 3600,
|
|
262
|
+
refresh_token: createAuthCode({ apiToken: payload.apiToken }, 86400 * 30)
|
|
263
|
+
};
|
|
264
|
+
} catch (error) {
|
|
265
|
+
setResponseStatus(event, 400);
|
|
266
|
+
return {
|
|
267
|
+
error: "invalid_grant",
|
|
268
|
+
error_description: error instanceof Error ? error.message : "Invalid refresh token"
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Create S256 PKCE challenge from verifier
|
|
274
|
+
* SHA256(code_verifier) encoded as base64url
|
|
275
|
+
*/
|
|
276
|
+
function createS256Challenge(codeVerifier) {
|
|
277
|
+
return createHash("sha256").update(codeVerifier).digest("base64url");
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Escape HTML special characters
|
|
281
|
+
*/
|
|
282
|
+
function escapeHtml(str) {
|
|
283
|
+
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Render the login form HTML
|
|
287
|
+
*/
|
|
288
|
+
function renderLoginForm(params) {
|
|
289
|
+
const { redirectUri, state, codeChallenge, codeChallengeMethod, error } = params;
|
|
290
|
+
return `<!DOCTYPE html>
|
|
291
|
+
<html lang="en">
|
|
292
|
+
<head>
|
|
293
|
+
<meta charset="UTF-8">
|
|
294
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
295
|
+
<title>Connect to Laravel Forge</title>
|
|
296
|
+
<style>
|
|
297
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
298
|
+
body {
|
|
299
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
300
|
+
background: linear-gradient(135deg, #18b69b 0%, #0e7460 100%);
|
|
301
|
+
min-height: 100vh;
|
|
302
|
+
display: flex;
|
|
303
|
+
align-items: center;
|
|
304
|
+
justify-content: center;
|
|
305
|
+
padding: 20px;
|
|
306
|
+
}
|
|
307
|
+
.container {
|
|
308
|
+
background: white;
|
|
309
|
+
border-radius: 16px;
|
|
310
|
+
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
|
311
|
+
padding: 40px;
|
|
312
|
+
width: 100%;
|
|
313
|
+
max-width: 420px;
|
|
314
|
+
}
|
|
315
|
+
.logo {
|
|
316
|
+
text-align: center;
|
|
317
|
+
margin-bottom: 24px;
|
|
318
|
+
}
|
|
319
|
+
.logo svg { width: 48px; height: 48px; }
|
|
320
|
+
h1 {
|
|
321
|
+
text-align: center;
|
|
322
|
+
color: #1a1a2e;
|
|
323
|
+
font-size: 24px;
|
|
324
|
+
margin-bottom: 8px;
|
|
325
|
+
}
|
|
326
|
+
.subtitle {
|
|
327
|
+
text-align: center;
|
|
328
|
+
color: #666;
|
|
329
|
+
font-size: 14px;
|
|
330
|
+
margin-bottom: 32px;
|
|
331
|
+
}
|
|
332
|
+
.error {
|
|
333
|
+
background: #fee2e2;
|
|
334
|
+
border: 1px solid #fecaca;
|
|
335
|
+
color: #dc2626;
|
|
336
|
+
padding: 12px 16px;
|
|
337
|
+
border-radius: 8px;
|
|
338
|
+
margin-bottom: 24px;
|
|
339
|
+
font-size: 14px;
|
|
340
|
+
}
|
|
341
|
+
.notice {
|
|
342
|
+
background: #f0fdf4;
|
|
343
|
+
border: 1px solid #bbf7d0;
|
|
344
|
+
color: #166534;
|
|
345
|
+
padding: 12px 16px;
|
|
346
|
+
border-radius: 8px;
|
|
347
|
+
margin-bottom: 24px;
|
|
348
|
+
font-size: 13px;
|
|
349
|
+
line-height: 1.4;
|
|
350
|
+
}
|
|
351
|
+
.form-group { margin-bottom: 20px; }
|
|
352
|
+
label {
|
|
353
|
+
display: block;
|
|
354
|
+
font-size: 14px;
|
|
355
|
+
font-weight: 500;
|
|
356
|
+
color: #374151;
|
|
357
|
+
margin-bottom: 6px;
|
|
358
|
+
}
|
|
359
|
+
input {
|
|
360
|
+
width: 100%;
|
|
361
|
+
padding: 12px 16px;
|
|
362
|
+
border: 1px solid #d1d5db;
|
|
363
|
+
border-radius: 8px;
|
|
364
|
+
font-size: 16px;
|
|
365
|
+
transition: border-color 0.2s, box-shadow 0.2s;
|
|
366
|
+
}
|
|
367
|
+
input:focus {
|
|
368
|
+
outline: none;
|
|
369
|
+
border-color: #18b69b;
|
|
370
|
+
box-shadow: 0 0 0 3px rgba(24, 182, 155, 0.2);
|
|
371
|
+
}
|
|
372
|
+
input::placeholder { color: #9ca3af; }
|
|
373
|
+
.help-text {
|
|
374
|
+
font-size: 12px;
|
|
375
|
+
color: #6b7280;
|
|
376
|
+
margin-top: 4px;
|
|
377
|
+
}
|
|
378
|
+
.help-text a { color: #18b69b; text-decoration: none; }
|
|
379
|
+
.help-text a:hover { text-decoration: underline; }
|
|
380
|
+
button {
|
|
381
|
+
width: 100%;
|
|
382
|
+
padding: 14px 24px;
|
|
383
|
+
background: linear-gradient(135deg, #18b69b 0%, #0e7460 100%);
|
|
384
|
+
color: white;
|
|
385
|
+
border: none;
|
|
386
|
+
border-radius: 8px;
|
|
387
|
+
font-size: 16px;
|
|
388
|
+
font-weight: 600;
|
|
389
|
+
cursor: pointer;
|
|
390
|
+
transition: transform 0.2s, box-shadow 0.2s;
|
|
391
|
+
}
|
|
392
|
+
button:hover {
|
|
393
|
+
transform: translateY(-1px);
|
|
394
|
+
box-shadow: 0 4px 12px rgba(24, 182, 155, 0.4);
|
|
395
|
+
}
|
|
396
|
+
button:active { transform: translateY(0); }
|
|
397
|
+
.footer {
|
|
398
|
+
text-align: center;
|
|
399
|
+
margin-top: 24px;
|
|
400
|
+
font-size: 12px;
|
|
401
|
+
color: #9ca3af;
|
|
402
|
+
}
|
|
403
|
+
.footer a { color: #18b69b; text-decoration: none; }
|
|
404
|
+
.footer a:hover { text-decoration: underline; }
|
|
405
|
+
</style>
|
|
406
|
+
</head>
|
|
407
|
+
<body>
|
|
408
|
+
<div class="container">
|
|
409
|
+
<div class="logo">
|
|
410
|
+
<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
411
|
+
<path d="M12 2L2 7L12 12L22 7L12 2Z" stroke="#18b69b" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
|
412
|
+
<path d="M2 17L12 22L22 17" stroke="#0e7460" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
|
413
|
+
<path d="M2 12L12 17L22 12" stroke="#18b69b" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
|
|
414
|
+
</svg>
|
|
415
|
+
</div>
|
|
416
|
+
<h1>Connect to Laravel Forge</h1>
|
|
417
|
+
<p class="subtitle">Enter your Forge API token to connect with Claude</p>
|
|
418
|
+
|
|
419
|
+
${error ? `<div class="error">${escapeHtml(error)}</div>` : ""}
|
|
420
|
+
|
|
421
|
+
<div class="notice">
|
|
422
|
+
<strong>Your token is not stored on this server.</strong> It is encrypted into the authorization code and sent back to Claude Desktop, which holds it in its own secure storage.
|
|
423
|
+
</div>
|
|
424
|
+
|
|
425
|
+
<form method="POST" action="/authorize">
|
|
426
|
+
<input type="hidden" name="redirectUri" value="${escapeHtml(redirectUri || "")}">
|
|
427
|
+
<input type="hidden" name="state" value="${escapeHtml(state || "")}">
|
|
428
|
+
<input type="hidden" name="codeChallenge" value="${escapeHtml(codeChallenge || "")}">
|
|
429
|
+
<input type="hidden" name="codeChallengeMethod" value="${escapeHtml(codeChallengeMethod || "S256")}">
|
|
430
|
+
|
|
431
|
+
<div class="form-group">
|
|
432
|
+
<label for="apiToken">Forge API Token *</label>
|
|
433
|
+
<input type="password" id="apiToken" name="apiToken" required placeholder="Enter your Forge API token">
|
|
434
|
+
<p class="help-text">
|
|
435
|
+
Generate at <a href="https://forge.laravel.com/user-profile/api" target="_blank" rel="noopener">forge.laravel.com → API Tokens</a>
|
|
436
|
+
</p>
|
|
437
|
+
</div>
|
|
438
|
+
|
|
439
|
+
<button type="submit">Connect to Forge</button>
|
|
440
|
+
</form>
|
|
441
|
+
|
|
442
|
+
<p class="footer">
|
|
443
|
+
Powered by <a href="https://github.com/studiometa/forge-tools" target="_blank" rel="noopener">forge-tools</a>
|
|
444
|
+
</p>
|
|
445
|
+
</div>
|
|
446
|
+
</body>
|
|
447
|
+
</html>`;
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Render success page with auto-redirect
|
|
451
|
+
*/
|
|
452
|
+
function renderSuccessPage(redirectUrl) {
|
|
453
|
+
return `<!DOCTYPE html>
|
|
454
|
+
<html lang="en">
|
|
455
|
+
<head>
|
|
456
|
+
<meta charset="UTF-8">
|
|
457
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
458
|
+
<meta http-equiv="refresh" content="2;url=${escapeHtml(redirectUrl)}">
|
|
459
|
+
<title>Connected — Forge MCP</title>
|
|
460
|
+
<style>
|
|
461
|
+
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
462
|
+
body {
|
|
463
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
464
|
+
background: linear-gradient(135deg, #18b69b 0%, #0e7460 100%);
|
|
465
|
+
min-height: 100vh;
|
|
466
|
+
display: flex;
|
|
467
|
+
align-items: center;
|
|
468
|
+
justify-content: center;
|
|
469
|
+
padding: 20px;
|
|
470
|
+
}
|
|
471
|
+
.container {
|
|
472
|
+
background: white;
|
|
473
|
+
border-radius: 16px;
|
|
474
|
+
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
|
475
|
+
padding: 40px;
|
|
476
|
+
width: 100%;
|
|
477
|
+
max-width: 420px;
|
|
478
|
+
text-align: center;
|
|
479
|
+
}
|
|
480
|
+
.success-icon {
|
|
481
|
+
width: 64px;
|
|
482
|
+
height: 64px;
|
|
483
|
+
margin: 0 auto 24px;
|
|
484
|
+
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
|
|
485
|
+
border-radius: 50%;
|
|
486
|
+
display: flex;
|
|
487
|
+
align-items: center;
|
|
488
|
+
justify-content: center;
|
|
489
|
+
}
|
|
490
|
+
.success-icon svg { width: 32px; height: 32px; stroke: white; }
|
|
491
|
+
h1 { color: #1a1a2e; font-size: 24px; margin-bottom: 8px; }
|
|
492
|
+
.message { color: #666; font-size: 14px; margin-bottom: 24px; }
|
|
493
|
+
.spinner {
|
|
494
|
+
width: 24px;
|
|
495
|
+
height: 24px;
|
|
496
|
+
border: 3px solid #e5e7eb;
|
|
497
|
+
border-top-color: #18b69b;
|
|
498
|
+
border-radius: 50%;
|
|
499
|
+
animation: spin 1s linear infinite;
|
|
500
|
+
margin: 0 auto 16px;
|
|
501
|
+
}
|
|
502
|
+
@keyframes spin { to { transform: rotate(360deg); } }
|
|
503
|
+
.redirect-text { color: #9ca3af; font-size: 13px; margin-bottom: 16px; }
|
|
504
|
+
.manual-link { color: #18b69b; text-decoration: none; font-size: 14px; }
|
|
505
|
+
.manual-link:hover { text-decoration: underline; }
|
|
506
|
+
</style>
|
|
507
|
+
</head>
|
|
508
|
+
<body>
|
|
509
|
+
<div class="container">
|
|
510
|
+
<div class="success-icon">
|
|
511
|
+
<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
512
|
+
<path d="M20 6L9 17L4 12" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/>
|
|
513
|
+
</svg>
|
|
514
|
+
</div>
|
|
515
|
+
<h1>Successfully Connected!</h1>
|
|
516
|
+
<p class="message">Your Forge API token has been encrypted and transmitted securely.</p>
|
|
517
|
+
<div class="spinner"></div>
|
|
518
|
+
<p class="redirect-text">Redirecting to Claude Desktop...</p>
|
|
519
|
+
<a href="${escapeHtml(redirectUrl)}" class="manual-link">Click here if not redirected automatically</a>
|
|
520
|
+
</div>
|
|
521
|
+
<script>
|
|
522
|
+
setTimeout(function() {
|
|
523
|
+
window.location.href = ${JSON.stringify(redirectUrl)};
|
|
524
|
+
}, 2000);
|
|
525
|
+
<\/script>
|
|
526
|
+
</body>
|
|
527
|
+
</html>`;
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Render error page
|
|
531
|
+
*/
|
|
532
|
+
function renderErrorPage(message) {
|
|
533
|
+
return `<!DOCTYPE html>
|
|
534
|
+
<html lang="en">
|
|
535
|
+
<head>
|
|
536
|
+
<meta charset="UTF-8">
|
|
537
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
538
|
+
<title>Error — Forge MCP</title>
|
|
539
|
+
<style>
|
|
540
|
+
body {
|
|
541
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
542
|
+
background: #f3f4f6;
|
|
543
|
+
min-height: 100vh;
|
|
544
|
+
display: flex;
|
|
545
|
+
align-items: center;
|
|
546
|
+
justify-content: center;
|
|
547
|
+
padding: 20px;
|
|
548
|
+
}
|
|
549
|
+
.container {
|
|
550
|
+
background: white;
|
|
551
|
+
border-radius: 16px;
|
|
552
|
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
|
553
|
+
padding: 40px;
|
|
554
|
+
text-align: center;
|
|
555
|
+
max-width: 400px;
|
|
556
|
+
}
|
|
557
|
+
h1 { color: #dc2626; margin-bottom: 16px; }
|
|
558
|
+
p { color: #6b7280; }
|
|
559
|
+
</style>
|
|
560
|
+
</head>
|
|
561
|
+
<body>
|
|
562
|
+
<div class="container">
|
|
563
|
+
<h1>Error</h1>
|
|
564
|
+
<p>${escapeHtml(message)}</p>
|
|
565
|
+
</div>
|
|
566
|
+
</body>
|
|
567
|
+
</html>`;
|
|
568
|
+
}
|
|
569
|
+
export { authorizeGetHandler, authorizePostHandler, createAccessToken, createS256Challenge, oauthMetadataHandler, protectedResourceHandler, registerHandler, tokenHandler };
|
|
570
|
+
|
|
571
|
+
//# sourceMappingURL=oauth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth.js","names":[],"sources":["../src/oauth.ts"],"sourcesContent":["/**\n * OAuth 2.1 endpoints for Claude Desktop integration\n *\n * Implements OAuth 2.1 with PKCE as specified in the MCP authorization spec.\n * Uses stateless encrypted tokens — no server-side storage required.\n *\n * Spec: https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization\n *\n * Flow:\n * 1. Claude redirects user to /authorize with OAuth params (including PKCE)\n * 2. User enters their Forge API token in a login form\n * 3. Server encrypts the token + PKCE challenge into an authorization code\n * 4. Redirects back to Claude with the code\n * 5. Claude exchanges code for access token via /token (with code_verifier)\n * 6. Server validates PKCE and returns base64-encoded access token\n */\n\nimport {\n defineEventHandler,\n getQuery,\n getRequestHeader,\n readBody,\n sendRedirect,\n setResponseHeader,\n setResponseStatus,\n type H3Event,\n} from \"h3\";\nimport { createHash } from \"node:crypto\";\n\nimport { createAuthCode, decodeAuthCode } from \"./crypto.ts\";\n\n/**\n * Create a base64-encoded access token from a Forge API token.\n * The access token is simply base64(apiToken) so that parseAuthHeader\n * can decode it on every request without any server-side lookup.\n */\nexport function createAccessToken(apiToken: string): string {\n return Buffer.from(apiToken).toString(\"base64\");\n}\n\n/**\n * OAuth metadata for discovery (RFC 8414)\n * GET /.well-known/oauth-authorization-server\n *\n * MCP clients MUST check this endpoint first for server capabilities.\n */\nexport const oauthMetadataHandler = defineEventHandler((event: H3Event) => {\n const host = getRequestHeader(event, \"host\") || \"localhost:3000\";\n const protocol = getRequestHeader(event, \"x-forwarded-proto\") || \"http\";\n const baseUrl = `${protocol}://${host}`;\n\n setResponseHeader(event, \"Content-Type\", \"application/json\");\n setResponseHeader(event, \"Cache-Control\", \"public, max-age=3600\");\n\n return {\n // Required fields per RFC 8414\n issuer: baseUrl,\n authorization_endpoint: `${baseUrl}/authorize`,\n token_endpoint: `${baseUrl}/token`,\n response_types_supported: [\"code\"],\n\n // OAuth 2.1 / MCP requirements\n grant_types_supported: [\"authorization_code\", \"refresh_token\"],\n code_challenge_methods_supported: [\"S256\"],\n token_endpoint_auth_methods_supported: [\"none\"], // Public client\n\n // Optional but useful\n registration_endpoint: `${baseUrl}/register`,\n scopes_supported: [\"forge\"],\n service_documentation: \"https://github.com/studiometa/forge-tools\",\n };\n});\n\n/**\n * Protected resource metadata (RFC 9728)\n * GET /.well-known/oauth-protected-resource\n *\n * Tells MCP clients where to find the OAuth authorization server.\n */\nexport const protectedResourceHandler = defineEventHandler((event: H3Event) => {\n const host = getRequestHeader(event, \"host\") || \"localhost:3000\";\n const protocol = getRequestHeader(event, \"x-forwarded-proto\") || \"http\";\n const baseUrl = `${protocol}://${host}`;\n\n setResponseHeader(event, \"Content-Type\", \"application/json\");\n setResponseHeader(event, \"Cache-Control\", \"public, max-age=3600\");\n\n return {\n resource: `${baseUrl}/mcp`,\n authorization_servers: [baseUrl],\n bearer_methods_supported: [\"header\"],\n scopes_supported: [\"forge\"],\n };\n});\n\n/**\n * Dynamic Client Registration endpoint (RFC 7591)\n * POST /register\n *\n * MCP servers SHOULD support DCR to allow clients to register automatically.\n * Since we use stateless tokens, we accept any registration and return\n * a generated client_id.\n */\nexport const registerHandler = defineEventHandler(async (event: H3Event) => {\n setResponseHeader(event, \"Content-Type\", \"application/json\");\n\n let body: Record<string, unknown>;\n try {\n body = (await readBody(event)) as Record<string, unknown>;\n } catch {\n setResponseStatus(event, 400);\n return {\n error: \"invalid_request\",\n error_description: \"Invalid JSON body\",\n };\n }\n\n // Extract client metadata\n const clientName = (body.client_name as string) || \"MCP Client\";\n const redirectUris = (body.redirect_uris as string[]) || [];\n\n // Generate a client_id based on the registration\n // Since we're stateless, we encode minimal info in the client_id\n const clientId = Buffer.from(\n JSON.stringify({\n name: clientName,\n ts: Date.now(),\n }),\n ).toString(\"base64url\");\n\n setResponseStatus(event, 201);\n return {\n client_id: clientId,\n client_name: clientName,\n redirect_uris: redirectUris,\n token_endpoint_auth_method: \"none\",\n grant_types: [\"authorization_code\", \"refresh_token\"],\n response_types: [\"code\"],\n };\n});\n\n/**\n * Authorization endpoint — shows login form\n * GET /authorize\n */\nexport const authorizeGetHandler = defineEventHandler((event: H3Event) => {\n const query = getQuery(event);\n\n // Extract OAuth parameters\n const redirectUri = query.redirect_uri as string;\n const state = query.state as string;\n const codeChallenge = query.code_challenge as string;\n const codeChallengeMethod = query.code_challenge_method as string;\n\n // Validate required parameters per OAuth 2.1\n if (!redirectUri) {\n setResponseHeader(event, \"Content-Type\", \"text/html; charset=utf-8\");\n setResponseStatus(event, 400);\n return renderErrorPage(\"Missing required parameter: redirect_uri\");\n }\n\n // PKCE is REQUIRED for public clients per MCP spec\n if (!codeChallenge) {\n const errorUrl = new URL(redirectUri);\n errorUrl.searchParams.set(\"error\", \"invalid_request\");\n errorUrl.searchParams.set(\"error_description\", \"code_challenge is required\");\n if (state) errorUrl.searchParams.set(\"state\", state);\n return sendRedirect(event, errorUrl.toString(), 302);\n }\n\n if (codeChallengeMethod && codeChallengeMethod !== \"S256\") {\n const errorUrl = new URL(redirectUri);\n errorUrl.searchParams.set(\"error\", \"invalid_request\");\n errorUrl.searchParams.set(\"error_description\", \"Only S256 code_challenge_method is supported\");\n if (state) errorUrl.searchParams.set(\"state\", state);\n return sendRedirect(event, errorUrl.toString(), 302);\n }\n\n setResponseHeader(event, \"Content-Type\", \"text/html; charset=utf-8\");\n\n return renderLoginForm({\n redirectUri,\n state,\n codeChallenge,\n codeChallengeMethod: codeChallengeMethod || \"S256\",\n });\n});\n\n/**\n * Authorization endpoint — process login\n * POST /authorize\n */\nexport const authorizePostHandler = defineEventHandler(async (event: H3Event) => {\n const body = (await readBody(event)) as Record<string, string>;\n\n const { apiToken, redirectUri, state, codeChallenge, codeChallengeMethod } = body;\n\n // Validate redirect URI first (security requirement)\n if (!redirectUri) {\n setResponseHeader(event, \"Content-Type\", \"text/html; charset=utf-8\");\n setResponseStatus(event, 400);\n return renderErrorPage(\"Missing redirect_uri parameter\");\n }\n\n // Validate redirect URI format (must be HTTPS or localhost)\n try {\n const uri = new URL(redirectUri);\n const isLocalhost = uri.hostname === \"localhost\" || uri.hostname === \"127.0.0.1\";\n const isHttps = uri.protocol === \"https:\";\n if (!isLocalhost && !isHttps) {\n setResponseStatus(event, 400);\n return renderErrorPage(\"redirect_uri must be HTTPS or localhost\");\n }\n } catch {\n setResponseStatus(event, 400);\n return renderErrorPage(\"Invalid redirect_uri format\");\n }\n\n // Validate required credentials\n if (!apiToken) {\n setResponseHeader(event, \"Content-Type\", \"text/html; charset=utf-8\");\n return renderLoginForm({\n redirectUri,\n state,\n codeChallenge,\n codeChallengeMethod,\n error: \"Forge API Token is required\",\n });\n }\n\n // Create encrypted authorization code with PKCE challenge\n const code = createAuthCode({\n apiToken,\n codeChallenge,\n codeChallengeMethod: codeChallengeMethod || \"S256\",\n });\n\n // Build redirect URL with authorization code\n const redirectUrl = new URL(redirectUri);\n redirectUrl.searchParams.set(\"code\", code);\n if (state) {\n redirectUrl.searchParams.set(\"state\", state);\n }\n\n // Show success page with auto-redirect\n setResponseHeader(event, \"Content-Type\", \"text/html; charset=utf-8\");\n return renderSuccessPage(redirectUrl.toString());\n});\n\n/**\n * Token endpoint — exchange code for access token\n * POST /token\n *\n * Supports:\n * - authorization_code grant (with PKCE validation)\n * - refresh_token grant\n */\nexport const tokenHandler = defineEventHandler(async (event: H3Event) => {\n setResponseHeader(event, \"Content-Type\", \"application/json\");\n\n // h3 auto-parses both JSON and URL-encoded bodies into objects\n const body = (await readBody(event)) as Record<string, string>;\n\n const { grant_type, code, code_verifier, refresh_token } = body;\n\n // Handle refresh token grant\n if (grant_type === \"refresh_token\") {\n return handleRefreshToken(event, refresh_token);\n }\n\n // Validate authorization code grant\n if (grant_type !== \"authorization_code\") {\n setResponseStatus(event, 400);\n return {\n error: \"unsupported_grant_type\",\n error_description: \"Supported grant types: authorization_code, refresh_token\",\n };\n }\n\n if (!code) {\n setResponseStatus(event, 400);\n return {\n error: \"invalid_request\",\n error_description: \"Missing authorization code\",\n };\n }\n\n if (!code_verifier) {\n setResponseStatus(event, 400);\n return {\n error: \"invalid_request\",\n error_description: \"Missing code_verifier (PKCE required)\",\n };\n }\n\n try {\n // Decode the authorization code\n const payload = decodeAuthCode(code);\n\n // Validate PKCE: SHA256(code_verifier) must equal code_challenge\n if (payload.codeChallenge) {\n const expectedChallenge = createS256Challenge(code_verifier);\n if (expectedChallenge !== payload.codeChallenge) {\n setResponseStatus(event, 400);\n return {\n error: \"invalid_grant\",\n error_description: \"Invalid code_verifier\",\n };\n }\n }\n\n // Create access token: base64(apiToken) — decodable on every request\n const accessToken = createAccessToken(payload.apiToken);\n\n // Create refresh token (encrypted credentials, longer expiry)\n const refreshToken = createAuthCode(\n { apiToken: payload.apiToken },\n 86400 * 30, // 30 days\n );\n\n return {\n access_token: accessToken,\n token_type: \"Bearer\",\n expires_in: 3600, // 1 hour\n refresh_token: refreshToken,\n };\n } catch (error) {\n setResponseStatus(event, 400);\n return {\n error: \"invalid_grant\",\n error_description: error instanceof Error ? error.message : \"Invalid authorization code\",\n };\n }\n});\n\n/**\n * Handle refresh token grant\n */\nfunction handleRefreshToken(event: H3Event, refreshToken: string | undefined) {\n if (!refreshToken) {\n setResponseStatus(event, 400);\n return {\n error: \"invalid_request\",\n error_description: \"Missing refresh_token\",\n };\n }\n\n try {\n // Decode refresh token (it's just an encrypted auth code with longer expiry)\n const payload = decodeAuthCode(refreshToken);\n\n // Create new access token\n const accessToken = createAccessToken(payload.apiToken);\n\n // Create new refresh token (rotate for security)\n const newRefreshToken = createAuthCode(\n { apiToken: payload.apiToken },\n 86400 * 30, // 30 days\n );\n\n return {\n access_token: accessToken,\n token_type: \"Bearer\",\n expires_in: 3600,\n refresh_token: newRefreshToken,\n };\n } catch (error) {\n setResponseStatus(event, 400);\n return {\n error: \"invalid_grant\",\n error_description: error instanceof Error ? error.message : \"Invalid refresh token\",\n };\n }\n}\n\n/**\n * Create S256 PKCE challenge from verifier\n * SHA256(code_verifier) encoded as base64url\n */\nexport function createS256Challenge(codeVerifier: string): string {\n return createHash(\"sha256\").update(codeVerifier).digest(\"base64url\");\n}\n\n/**\n * Escape HTML special characters\n */\nfunction escapeHtml(str: string): string {\n return str\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/'/g, \"'\");\n}\n\n/**\n * Render the login form HTML\n */\nfunction renderLoginForm(params: {\n redirectUri?: string;\n state?: string;\n codeChallenge?: string;\n codeChallengeMethod?: string;\n error?: string;\n}): string {\n const { redirectUri, state, codeChallenge, codeChallengeMethod, error } = params;\n\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>Connect to Laravel Forge</title>\n <style>\n * { box-sizing: border-box; margin: 0; padding: 0; }\n body {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;\n background: linear-gradient(135deg, #18b69b 0%, #0e7460 100%);\n min-height: 100vh;\n display: flex;\n align-items: center;\n justify-content: center;\n padding: 20px;\n }\n .container {\n background: white;\n border-radius: 16px;\n box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);\n padding: 40px;\n width: 100%;\n max-width: 420px;\n }\n .logo {\n text-align: center;\n margin-bottom: 24px;\n }\n .logo svg { width: 48px; height: 48px; }\n h1 {\n text-align: center;\n color: #1a1a2e;\n font-size: 24px;\n margin-bottom: 8px;\n }\n .subtitle {\n text-align: center;\n color: #666;\n font-size: 14px;\n margin-bottom: 32px;\n }\n .error {\n background: #fee2e2;\n border: 1px solid #fecaca;\n color: #dc2626;\n padding: 12px 16px;\n border-radius: 8px;\n margin-bottom: 24px;\n font-size: 14px;\n }\n .notice {\n background: #f0fdf4;\n border: 1px solid #bbf7d0;\n color: #166534;\n padding: 12px 16px;\n border-radius: 8px;\n margin-bottom: 24px;\n font-size: 13px;\n line-height: 1.4;\n }\n .form-group { margin-bottom: 20px; }\n label {\n display: block;\n font-size: 14px;\n font-weight: 500;\n color: #374151;\n margin-bottom: 6px;\n }\n input {\n width: 100%;\n padding: 12px 16px;\n border: 1px solid #d1d5db;\n border-radius: 8px;\n font-size: 16px;\n transition: border-color 0.2s, box-shadow 0.2s;\n }\n input:focus {\n outline: none;\n border-color: #18b69b;\n box-shadow: 0 0 0 3px rgba(24, 182, 155, 0.2);\n }\n input::placeholder { color: #9ca3af; }\n .help-text {\n font-size: 12px;\n color: #6b7280;\n margin-top: 4px;\n }\n .help-text a { color: #18b69b; text-decoration: none; }\n .help-text a:hover { text-decoration: underline; }\n button {\n width: 100%;\n padding: 14px 24px;\n background: linear-gradient(135deg, #18b69b 0%, #0e7460 100%);\n color: white;\n border: none;\n border-radius: 8px;\n font-size: 16px;\n font-weight: 600;\n cursor: pointer;\n transition: transform 0.2s, box-shadow 0.2s;\n }\n button:hover {\n transform: translateY(-1px);\n box-shadow: 0 4px 12px rgba(24, 182, 155, 0.4);\n }\n button:active { transform: translateY(0); }\n .footer {\n text-align: center;\n margin-top: 24px;\n font-size: 12px;\n color: #9ca3af;\n }\n .footer a { color: #18b69b; text-decoration: none; }\n .footer a:hover { text-decoration: underline; }\n </style>\n</head>\n<body>\n <div class=\"container\">\n <div class=\"logo\">\n <svg viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M12 2L2 7L12 12L22 7L12 2Z\" stroke=\"#18b69b\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n <path d=\"M2 17L12 22L22 17\" stroke=\"#0e7460\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n <path d=\"M2 12L12 17L22 12\" stroke=\"#18b69b\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n </svg>\n </div>\n <h1>Connect to Laravel Forge</h1>\n <p class=\"subtitle\">Enter your Forge API token to connect with Claude</p>\n\n ${error ? `<div class=\"error\">${escapeHtml(error)}</div>` : \"\"}\n\n <div class=\"notice\">\n <strong>Your token is not stored on this server.</strong> It is encrypted into the authorization code and sent back to Claude Desktop, which holds it in its own secure storage.\n </div>\n\n <form method=\"POST\" action=\"/authorize\">\n <input type=\"hidden\" name=\"redirectUri\" value=\"${escapeHtml(redirectUri || \"\")}\">\n <input type=\"hidden\" name=\"state\" value=\"${escapeHtml(state || \"\")}\">\n <input type=\"hidden\" name=\"codeChallenge\" value=\"${escapeHtml(codeChallenge || \"\")}\">\n <input type=\"hidden\" name=\"codeChallengeMethod\" value=\"${escapeHtml(codeChallengeMethod || \"S256\")}\">\n\n <div class=\"form-group\">\n <label for=\"apiToken\">Forge API Token *</label>\n <input type=\"password\" id=\"apiToken\" name=\"apiToken\" required placeholder=\"Enter your Forge API token\">\n <p class=\"help-text\">\n Generate at <a href=\"https://forge.laravel.com/user-profile/api\" target=\"_blank\" rel=\"noopener\">forge.laravel.com → API Tokens</a>\n </p>\n </div>\n\n <button type=\"submit\">Connect to Forge</button>\n </form>\n\n <p class=\"footer\">\n Powered by <a href=\"https://github.com/studiometa/forge-tools\" target=\"_blank\" rel=\"noopener\">forge-tools</a>\n </p>\n </div>\n</body>\n</html>`;\n}\n\n/**\n * Render success page with auto-redirect\n */\nfunction renderSuccessPage(redirectUrl: string): string {\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <meta http-equiv=\"refresh\" content=\"2;url=${escapeHtml(redirectUrl)}\">\n <title>Connected — Forge MCP</title>\n <style>\n * { box-sizing: border-box; margin: 0; padding: 0; }\n body {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;\n background: linear-gradient(135deg, #18b69b 0%, #0e7460 100%);\n min-height: 100vh;\n display: flex;\n align-items: center;\n justify-content: center;\n padding: 20px;\n }\n .container {\n background: white;\n border-radius: 16px;\n box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);\n padding: 40px;\n width: 100%;\n max-width: 420px;\n text-align: center;\n }\n .success-icon {\n width: 64px;\n height: 64px;\n margin: 0 auto 24px;\n background: linear-gradient(135deg, #10b981 0%, #059669 100%);\n border-radius: 50%;\n display: flex;\n align-items: center;\n justify-content: center;\n }\n .success-icon svg { width: 32px; height: 32px; stroke: white; }\n h1 { color: #1a1a2e; font-size: 24px; margin-bottom: 8px; }\n .message { color: #666; font-size: 14px; margin-bottom: 24px; }\n .spinner {\n width: 24px;\n height: 24px;\n border: 3px solid #e5e7eb;\n border-top-color: #18b69b;\n border-radius: 50%;\n animation: spin 1s linear infinite;\n margin: 0 auto 16px;\n }\n @keyframes spin { to { transform: rotate(360deg); } }\n .redirect-text { color: #9ca3af; font-size: 13px; margin-bottom: 16px; }\n .manual-link { color: #18b69b; text-decoration: none; font-size: 14px; }\n .manual-link:hover { text-decoration: underline; }\n </style>\n</head>\n<body>\n <div class=\"container\">\n <div class=\"success-icon\">\n <svg viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n <path d=\"M20 6L9 17L4 12\" stroke-width=\"2.5\" stroke-linecap=\"round\" stroke-linejoin=\"round\"/>\n </svg>\n </div>\n <h1>Successfully Connected!</h1>\n <p class=\"message\">Your Forge API token has been encrypted and transmitted securely.</p>\n <div class=\"spinner\"></div>\n <p class=\"redirect-text\">Redirecting to Claude Desktop...</p>\n <a href=\"${escapeHtml(redirectUrl)}\" class=\"manual-link\">Click here if not redirected automatically</a>\n </div>\n <script>\n setTimeout(function() {\n window.location.href = ${JSON.stringify(redirectUrl)};\n }, 2000);\n </script>\n</body>\n</html>`;\n}\n\n/**\n * Render error page\n */\nfunction renderErrorPage(message: string): string {\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>Error — Forge MCP</title>\n <style>\n body {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;\n background: #f3f4f6;\n min-height: 100vh;\n display: flex;\n align-items: center;\n justify-content: center;\n padding: 20px;\n }\n .container {\n background: white;\n border-radius: 16px;\n box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);\n padding: 40px;\n text-align: center;\n max-width: 400px;\n }\n h1 { color: #dc2626; margin-bottom: 16px; }\n p { color: #6b7280; }\n </style>\n</head>\n<body>\n <div class=\"container\">\n <h1>Error</h1>\n <p>${escapeHtml(message)}</p>\n </div>\n</body>\n</html>`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAoCA,SAAgB,kBAAkB,UAA0B;AAC1D,QAAO,OAAO,KAAK,SAAS,CAAC,SAAS,SAAS;;;;;;;;AASjD,MAAa,uBAAuB,oBAAoB,UA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1E,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAiIH,QAAQ,sBAAsB,WAAW,MAAM,CAAC,UAAU,GAAG;;;;;;;uDAOZ,WAAW,eAAe,GAAG,CAAC;iDACpC,WAAW,SAAS,GAAG,CAAC;yDAChB,WAAW,iBAAiB,GAAG,CAAC;+DAC1B,WAAW,uBAAuB,OAAO,CAAC;;;;;;;;;;;;;;;;;;;;;;;AAwBzG,SAAS,kBAAkB,aAA6B;AACtD,QAAO;;;;;8CAKqC,WAAW,YAAY,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;eA6DvD,WAAW,YAAY,CAAC;;;;+BAIR,KAAK,UAAU,YAAY,CAAC;;;;;;;;;AAU3D,SAAS,gBAAgB,SAAyB;AAChD,QAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SA+BA,WAAW,QAAQ,CAAC"}
|
package/dist/server.d.ts
CHANGED
|
@@ -3,22 +3,40 @@
|
|
|
3
3
|
* Forge MCP Server - HTTP Transport (Streamable HTTP)
|
|
4
4
|
*
|
|
5
5
|
* Implements the official MCP Streamable HTTP transport specification.
|
|
6
|
-
*
|
|
6
|
+
* Supports both raw Bearer tokens and OAuth 2.1 with PKCE for
|
|
7
|
+
* Claude Desktop compatibility.
|
|
7
8
|
*
|
|
8
|
-
* Token
|
|
9
|
+
* Token formats:
|
|
10
|
+
* - Raw Forge API token (Bearer <token>)
|
|
11
|
+
* - Base64-encoded token from OAuth flow (Bearer base64(<token>))
|
|
12
|
+
*
|
|
13
|
+
* Environment variables:
|
|
14
|
+
* PORT - HTTP port (default: 3000)
|
|
15
|
+
* HOST - Bind address (default: 0.0.0.0)
|
|
16
|
+
* FORGE_READ_ONLY - Disable write operations (default: false)
|
|
17
|
+
* OAUTH_SECRET - AES-256-GCM encryption key for OAuth tokens.
|
|
18
|
+
* Required in production. A default is used if unset.
|
|
9
19
|
*
|
|
10
20
|
* Usage:
|
|
11
21
|
* forge-mcp-server
|
|
12
22
|
* forge-mcp-server --read-only
|
|
13
|
-
*
|
|
23
|
+
* OAUTH_SECRET=my-secret forge-mcp-server
|
|
14
24
|
* PORT=3000 forge-mcp-server
|
|
15
25
|
*
|
|
16
26
|
* Endpoints:
|
|
17
|
-
* POST /mcp
|
|
18
|
-
* GET /mcp
|
|
19
|
-
* DELETE /mcp
|
|
27
|
+
* POST /mcp - MCP Streamable HTTP (JSON-RPC messages)
|
|
28
|
+
* GET /mcp - MCP Streamable HTTP (SSE stream for server notifications)
|
|
29
|
+
* DELETE /mcp - MCP Streamable HTTP (session termination)
|
|
20
30
|
* GET / - Service info
|
|
21
31
|
* GET /health - Health check
|
|
32
|
+
*
|
|
33
|
+
* OAuth 2.1 endpoints (for Claude Desktop):
|
|
34
|
+
* GET /.well-known/oauth-authorization-server - OAuth metadata (RFC 8414)
|
|
35
|
+
* GET /.well-known/oauth-protected-resource - Protected resource (RFC 9728)
|
|
36
|
+
* POST /register - Dynamic client registration (RFC 7591)
|
|
37
|
+
* GET /authorize - Login form
|
|
38
|
+
* POST /authorize - Process login
|
|
39
|
+
* POST /token - Token exchange (PKCE)
|
|
22
40
|
*/
|
|
23
41
|
import { type Server } from "node:http";
|
|
24
42
|
/**
|