poe-code 4.0.21 → 4.0.22

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "poe-code",
3
- "version": "4.0.21",
3
+ "version": "4.0.22",
4
4
  "description": "CLI tool to configure Poe API for developer workflows.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -14,7 +14,9 @@ export interface TinyHttpMcpServerOAuthOptions extends ProtectedResourceMetadata
14
14
  }
15
15
  export type HttpTransportOptions = ServerOptions & StreamableHttpTransportOptions & {
16
16
  oauth?: TinyHttpMcpServerOAuthOptions;
17
+ requestHandler?: HttpAdditionalRequestHandler;
17
18
  };
19
+ export type HttpAdditionalRequestHandler = (request: IncomingMessage, response: ServerResponse) => boolean | Promise<boolean>;
18
20
  export interface HttpListenOptions {
19
21
  port?: number;
20
22
  hostname?: string;
@@ -157,6 +157,9 @@ export function createHttpServer(options) {
157
157
  const nodeServer = http.createServer(async (req, res) => {
158
158
  const requestUrl = new URL(req.url ?? "/", "http://127.0.0.1");
159
159
  try {
160
+ if (options.requestHandler !== undefined && (await options.requestHandler(req, res))) {
161
+ return;
162
+ }
160
163
  if (protectedResourceMetadataBody !== undefined &&
161
164
  req.method === "GET" &&
162
165
  getProtectedResourceMetadataPaths(path).includes(requestUrl.pathname)) {
@@ -3,7 +3,7 @@ export type { Server, TypedSchema, ImageContent, AudioContent, EmbeddedResource,
3
3
  export { createExpressMiddleware, createExpressOAuthHandlers, createProtectedResourceMetadataRouter } from "./express-middleware.js";
4
4
  export type { CreateExpressOAuthHandlersOptions } from "./express-middleware.js";
5
5
  export { createHttpServer, createProtectedResourceMetadataDocument } from "./http-server.js";
6
- export type { HttpToolContext, HttpToolHandler, HttpListenOptions, HttpServer, HttpServerHandle, HttpTransportOptions, ProtectedResourceMetadataOptions, TinyHttpMcpServerOAuthOptions } from "./http-server.js";
6
+ export type { HttpToolContext, HttpToolHandler, HttpAdditionalRequestHandler, HttpListenOptions, HttpServer, HttpServerHandle, HttpTransportOptions, ProtectedResourceMetadataOptions, TinyHttpMcpServerOAuthOptions } from "./http-server.js";
7
7
  export { TokenVerificationError } from "./auth.js";
8
8
  export type { RequestAuthInfo, TokenVerifier, VerifiedAccessToken } from "./auth.js";
9
9
  export { StreamableHttpTransport } from "./http-transport.js";
@@ -1,5 +1,5 @@
1
1
  export { createHttpServer, createProtectedResourceMetadataDocument } from "./http-server.js";
2
- export type { HttpToolContext, HttpToolHandler, HttpListenOptions, HttpServer, HttpServerHandle, HttpTransportOptions, ProtectedResourceMetadataOptions, TinyHttpMcpServerOAuthOptions } from "./http-server.js";
2
+ export type { HttpToolContext, HttpToolHandler, HttpAdditionalRequestHandler, HttpListenOptions, HttpServer, HttpServerHandle, HttpTransportOptions, ProtectedResourceMetadataOptions, TinyHttpMcpServerOAuthOptions } from "./http-server.js";
3
3
  export { TokenVerificationError } from "./auth.js";
4
4
  export type { RequestAuthInfo, TokenVerifier, VerifiedAccessToken } from "./auth.js";
5
5
  export { StreamableHttpTransport } from "./http-transport.js";
@@ -0,0 +1,44 @@
1
+ import { hostedOAuth } from "./http-hosted-oauth.js";
2
+ const ignoredHostedOAuth = hostedOAuth({
3
+ publicUrl: "https://calendar.example/mcp",
4
+ storage: ignoredStorage,
5
+ provider: {
6
+ name: "Skylight",
7
+ login: { fields: ["email", "password"] },
8
+ async connect({ email, password, signal }) {
9
+ const ignoredEmail = email;
10
+ const ignoredPassword = password;
11
+ const ignoredSignal = signal;
12
+ return {
13
+ accountId: ignoredEmail,
14
+ credential: `${ignoredPassword}:${ignoredSignal.aborted}`
15
+ };
16
+ },
17
+ services({ credentials }) {
18
+ return { skylight: { credential: () => credentials.read() } };
19
+ }
20
+ }
21
+ });
22
+ void ignoredHostedOAuth;
23
+ const ignoredRedirectHostedOAuth = hostedOAuth({
24
+ publicUrl: "https://calendar.example/mcp",
25
+ storage: ignoredStorage,
26
+ provider: {
27
+ name: "Skylight",
28
+ services: ({ credentials }) => ({
29
+ skylight: { credential: () => credentials.read() }
30
+ })
31
+ },
32
+ advanced: {
33
+ interaction: {
34
+ paths: ["/oauth/skylight/callback"],
35
+ start: () => Response.redirect("https://login.example/authorize"),
36
+ handle: async ({ complete }) => complete({
37
+ transactionId: "transaction-id",
38
+ accountId: "account-id",
39
+ credential: "provider-session"
40
+ })
41
+ }
42
+ }
43
+ });
44
+ void ignoredRedirectHostedOAuth;
@@ -0,0 +1,113 @@
1
+ import { type JWK } from "jose";
2
+ import { type AuthorizationServerStore, type AuthorizationTransactionRecord, type OAuthAuthorizationServerSigningKey } from "../../mcp-oauth-server/dist/index.js";
3
+ import type { HttpAdditionalRequestHandler, TinyHttpMcpServerOAuthOptions } from "../../tiny-http-mcp-server/dist/server.js";
4
+ export type HostedOAuthLoginFieldName = "email" | "password" | "apiKey" | (string & {});
5
+ export interface HostedOAuthLoginField {
6
+ name: HostedOAuthLoginFieldName;
7
+ label?: string;
8
+ type?: "text" | "email" | "password";
9
+ }
10
+ export interface HostedOAuthCredentialStore<TCredential = unknown> {
11
+ get(subject: string): Promise<TCredential | undefined>;
12
+ set(subject: string, credential: TCredential): Promise<void>;
13
+ delete(subject: string): Promise<void>;
14
+ update(subject: string, update: (credential: TCredential) => Promise<TCredential> | TCredential): Promise<TCredential>;
15
+ }
16
+ export interface HostedOAuthStorageCapabilities {
17
+ durable: boolean;
18
+ encryptedCredentials: boolean;
19
+ stableKeys: boolean;
20
+ shared: boolean;
21
+ }
22
+ export interface HostedOAuthInteractionStore {
23
+ set(transaction: AuthorizationTransactionRecord): Promise<void>;
24
+ get(transactionId: string): Promise<AuthorizationTransactionRecord | undefined>;
25
+ delete(transactionId: string): Promise<void>;
26
+ }
27
+ export interface HostedOAuthStorage<TCredential = unknown> {
28
+ authorizationServer: AuthorizationServerStore;
29
+ interactions: HostedOAuthInteractionStore;
30
+ credentials: HostedOAuthCredentialStore<TCredential>;
31
+ capabilities: HostedOAuthStorageCapabilities;
32
+ signingKey(): Promise<OAuthAuthorizationServerSigningKey>;
33
+ resolveSubject(providerName: string, accountId: string): Promise<string>;
34
+ cleanup?(now?: number): Promise<void>;
35
+ }
36
+ export interface HostedOAuthCredentialAccess<TCredential = unknown> {
37
+ read(): Promise<TCredential>;
38
+ update(update: (credential: TCredential) => Promise<TCredential> | TCredential): Promise<TCredential>;
39
+ delete(): Promise<void>;
40
+ }
41
+ export interface HostedOAuthProvider<TCredential = unknown, TServices extends object = object> {
42
+ name: string;
43
+ login?: {
44
+ fields: readonly (HostedOAuthLoginFieldName | HostedOAuthLoginField)[];
45
+ };
46
+ connect?(fields: Readonly<Record<string, string>> & {
47
+ signal: AbortSignal;
48
+ }): Promise<{
49
+ accountId: string;
50
+ credential: TCredential;
51
+ }>;
52
+ services(input: {
53
+ credentials: HostedOAuthCredentialAccess<TCredential>;
54
+ }): Promise<Partial<TServices>> | Partial<TServices>;
55
+ }
56
+ export interface HostedOAuthInteractionAdapter<TCredential = unknown> {
57
+ paths: readonly string[];
58
+ start(context: {
59
+ request: Request;
60
+ transaction: AuthorizationTransactionRecord;
61
+ }): Promise<Response> | Response;
62
+ handle(context: {
63
+ request: Request;
64
+ complete(input: {
65
+ transactionId: string;
66
+ accountId: string;
67
+ credential: TCredential;
68
+ }): Promise<Response>;
69
+ }): Promise<Response> | Response;
70
+ }
71
+ export interface HostedOAuthAdvancedOptions<TCredential = unknown> {
72
+ scopes?: readonly string[];
73
+ branding?: {
74
+ title?: string;
75
+ };
76
+ accessTokenTtlSeconds?: number;
77
+ authorizationCodeTtlSeconds?: number;
78
+ authorizationTransactionTtlSeconds?: number;
79
+ refreshTokenTtlSeconds?: number;
80
+ additionalPublicJwks?: readonly JWK[];
81
+ interaction?: HostedOAuthInteractionAdapter<TCredential>;
82
+ }
83
+ export interface HostedOAuthOptions<TCredential = unknown, TServices extends object = object> {
84
+ publicUrl: string;
85
+ storage: HostedOAuthStorage<TCredential>;
86
+ provider: HostedOAuthProvider<TCredential, TServices>;
87
+ advanced?: HostedOAuthAdvancedOptions<TCredential>;
88
+ }
89
+ export interface PreparedHostedOAuth {
90
+ publicUrl: URL;
91
+ issuer: URL;
92
+ scopes: readonly string[];
93
+ }
94
+ export interface HostedOAuthConfiguration<TCredential = unknown, TServices extends object = object> extends HostedOAuthOptions<TCredential, TServices> {
95
+ readonly kind: "hosted";
96
+ prepare(options?: {
97
+ production?: boolean;
98
+ }): Promise<PreparedHostedOAuth>;
99
+ }
100
+ export declare function isHostedOAuthConfiguration(value: unknown): value is HostedOAuthConfiguration<unknown, object>;
101
+ export declare function hostedOAuth<TCredential = unknown, TServices extends object = object>(options: HostedOAuthOptions<TCredential, TServices>): HostedOAuthConfiguration<TCredential, TServices>;
102
+ export declare class HostedOAuthLoginError extends Error {
103
+ constructor(message: string);
104
+ }
105
+ export interface HostedOAuthRuntime<TServices extends object = object> {
106
+ mcpPath: string;
107
+ oauth: TinyHttpMcpServerOAuthOptions;
108
+ requestHandler: HttpAdditionalRequestHandler;
109
+ requestServices(subject: string): Promise<Partial<TServices>>;
110
+ }
111
+ export declare function createInMemoryHostedOAuthStorage<TCredential = unknown>(options: {
112
+ development: true;
113
+ }): HostedOAuthStorage<TCredential>;
@@ -0,0 +1,422 @@
1
+ import { createHmac, generateKeyPairSync, randomBytes } from "node:crypto";
2
+ import { exportJWK } from "jose";
3
+ import { createAuthorizationInteractionSecurity, createInMemoryAuthorizationServerStore, createOAuthAuthorizationServer, verifyAuthorizationInteractionCsrf } from "mcp-oauth-server";
4
+ export function isHostedOAuthConfiguration(value) {
5
+ return typeof value === "object" && value !== null && "kind" in value && value.kind === "hosted";
6
+ }
7
+ function normalizePublicUrl(value) {
8
+ const url = new URL(value);
9
+ if (url.hash.length > 0 || url.search.length > 0) {
10
+ throw new Error("hosted OAuth publicUrl must not contain a query or fragment.");
11
+ }
12
+ if (url.username.length > 0 || url.password.length > 0) {
13
+ throw new Error("hosted OAuth publicUrl must not contain credentials.");
14
+ }
15
+ return url;
16
+ }
17
+ function configurationErrors(config, production) {
18
+ const errors = [];
19
+ const scopes = config.advanced?.scopes ?? ["mcp", "offline_access"];
20
+ if (!scopes.includes("mcp"))
21
+ errors.push("scopes containing required mcp scope");
22
+ if (scopes.some((scope) => scope.length === 0 || scope.trim() !== scope || scope.includes(" "))) {
23
+ errors.push("valid space-free scope names");
24
+ }
25
+ if (production) {
26
+ if (normalizePublicUrl(config.publicUrl).protocol !== "https:")
27
+ errors.push("HTTPS publicUrl");
28
+ if (!config.storage.capabilities.durable)
29
+ errors.push("durable storage");
30
+ if (!config.storage.capabilities.encryptedCredentials)
31
+ errors.push("encrypted credentials");
32
+ if (!config.storage.capabilities.stableKeys)
33
+ errors.push("stable signing and subject keys");
34
+ }
35
+ return errors;
36
+ }
37
+ export function hostedOAuth(options) {
38
+ if (options.provider.name.trim().length === 0)
39
+ throw new Error("provider.name is required.");
40
+ if (options.advanced?.interaction === undefined &&
41
+ (options.provider.login === undefined || options.provider.connect === undefined)) {
42
+ throw new Error("provider.login and provider.connect are required without an advanced interaction.");
43
+ }
44
+ if (options.provider.login !== undefined && options.provider.login.fields.length === 0) {
45
+ throw new Error("provider.login.fields must contain at least one field.");
46
+ }
47
+ const fieldNames = (options.provider.login?.fields ?? []).map((field) => typeof field === "string" ? field : field.name);
48
+ if (new Set(fieldNames).size !== fieldNames.length ||
49
+ fieldNames.some((name) => name.length === 0 || name === "signal" || name === "csrf" || name === "transaction")) {
50
+ throw new Error("provider.login.fields must have unique, non-reserved names.");
51
+ }
52
+ if (options.advanced?.interaction !== undefined &&
53
+ (options.advanced.interaction.paths.length === 0 ||
54
+ new Set(options.advanced.interaction.paths).size !==
55
+ options.advanced.interaction.paths.length ||
56
+ options.advanced.interaction.paths.some((path) => !path.startsWith("/") ||
57
+ [
58
+ "/healthz",
59
+ "/oauth/connect",
60
+ "/authorize",
61
+ "/register",
62
+ "/token",
63
+ "/revoke",
64
+ "/.well-known/oauth-authorization-server",
65
+ "/.well-known/jwks.json",
66
+ normalizePublicUrl(options.publicUrl).pathname
67
+ ].includes(path)))) {
68
+ throw new Error("advanced.interaction.paths must contain unique, non-reserved absolute paths.");
69
+ }
70
+ normalizePublicUrl(options.publicUrl);
71
+ return {
72
+ ...options,
73
+ kind: "hosted",
74
+ async prepare({ production = process.env.NODE_ENV === "production" } = {}) {
75
+ const errors = configurationErrors(this, production);
76
+ if (errors.length > 0) {
77
+ throw new Error(`Hosted OAuth configuration requires: ${errors.join(", ")}.`);
78
+ }
79
+ const publicUrl = normalizePublicUrl(this.publicUrl);
80
+ return {
81
+ publicUrl,
82
+ issuer: new URL(publicUrl.origin),
83
+ scopes: this.advanced?.scopes ?? ["mcp", "offline_access"]
84
+ };
85
+ }
86
+ };
87
+ }
88
+ export class HostedOAuthLoginError extends Error {
89
+ constructor(message) {
90
+ super(message);
91
+ this.name = "HostedOAuthLoginError";
92
+ }
93
+ }
94
+ function escapeHtml(value) {
95
+ return value
96
+ .replaceAll("&", "&amp;")
97
+ .replaceAll("<", "&lt;")
98
+ .replaceAll(">", "&gt;")
99
+ .replaceAll('"', "&quot;")
100
+ .replaceAll("'", "&#39;");
101
+ }
102
+ function loginField(field) {
103
+ if (typeof field !== "string")
104
+ return field;
105
+ return {
106
+ name: field,
107
+ label: field === "apiKey" ? "API key" : `${field[0]?.toUpperCase() ?? ""}${field.slice(1)}`,
108
+ type: field === "password" || field === "apiKey" ? "password" : field === "email" ? "email" : "text"
109
+ };
110
+ }
111
+ function renderLogin(providerName, fields, transaction, csrfToken, error) {
112
+ const controls = fields
113
+ .map((field) => {
114
+ const name = escapeHtml(field.name);
115
+ return `<label>${escapeHtml(field.label ?? field.name)}<input name="${name}" type="${field.type ?? "text"}" required autocomplete="${field.type === "password" ? "current-password" : "off"}"></label>`;
116
+ })
117
+ .join("");
118
+ return `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Connect ${escapeHtml(providerName)}</title><style>body{font:16px system-ui;max-width:28rem;margin:10vh auto;padding:1rem;color:#171717}form{display:grid;gap:1rem}label{display:grid;gap:.35rem}input,button{font:inherit;padding:.7rem}button{cursor:pointer}${error === undefined ? "" : ".error{color:#b42318}"}</style></head><body><h1>Connect ${escapeHtml(providerName)}</h1>${error === undefined ? "" : `<p class="error">${escapeHtml(error)}</p>`}<form method="post" action="/oauth/connect"><input type="hidden" name="transaction" value="${escapeHtml(transaction.id)}"><input type="hidden" name="csrf" value="${escapeHtml(csrfToken)}">${controls}<button type="submit">Connect</button></form></body></html>`;
119
+ }
120
+ function writeWebResponse(response, webResponse) {
121
+ webResponse.headers.forEach((value, name) => response.setHeader(name, value));
122
+ response.statusCode = webResponse.status;
123
+ return webResponse.arrayBuffer().then((body) => {
124
+ response.end(Buffer.from(body));
125
+ });
126
+ }
127
+ async function readBody(request, maxBytes = 65_536) {
128
+ const chunks = [];
129
+ let size = 0;
130
+ for await (const chunk of request) {
131
+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
132
+ size += buffer.length;
133
+ if (size > maxBytes)
134
+ throw new Error("Request body is too large.");
135
+ chunks.push(buffer);
136
+ }
137
+ return Buffer.concat(chunks);
138
+ }
139
+ function toWebRequest(request, issuer, body) {
140
+ const headers = new Headers();
141
+ for (const [name, value] of Object.entries(request.headers)) {
142
+ if (value !== undefined)
143
+ headers.set(name, Array.isArray(value) ? value.join(", ") : value);
144
+ }
145
+ return new Request(new URL(request.url ?? "/", issuer), {
146
+ method: request.method,
147
+ headers,
148
+ ...(body !== undefined && body.length > 0 ? { body: body.toString("utf8") } : {})
149
+ });
150
+ }
151
+ function credentialsFor(storage, subject) {
152
+ return {
153
+ async read() {
154
+ const credential = await storage.credentials.get(subject);
155
+ if (credential === undefined)
156
+ throw new Error("Provider credential is missing; reconnect required.");
157
+ return credential;
158
+ },
159
+ update: (update) => storage.credentials.update(subject, update),
160
+ delete: () => storage.credentials.delete(subject)
161
+ };
162
+ }
163
+ /** @internal */
164
+ export async function prepareHostedOAuthRuntime(config) {
165
+ const prepared = await config.prepare();
166
+ const fields = (config.provider.login?.fields ?? []).map(loginField);
167
+ const customInteraction = config.advanced?.interaction;
168
+ const displayName = config.advanced?.branding?.title ?? config.provider.name;
169
+ const interaction = {
170
+ async start({ request, transaction }) {
171
+ await config.storage.interactions.set(transaction);
172
+ if (customInteraction !== undefined) {
173
+ return customInteraction.start({ request, transaction });
174
+ }
175
+ const security = createAuthorizationInteractionSecurity();
176
+ return new Response(renderLogin(displayName, fields, transaction, security.csrfToken), {
177
+ status: 200,
178
+ headers: {
179
+ "content-type": "text/html; charset=utf-8",
180
+ "content-security-policy": "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'",
181
+ "cache-control": "no-store",
182
+ "referrer-policy": "no-referrer",
183
+ "x-content-type-options": "nosniff",
184
+ "set-cookie": security.setCookie
185
+ }
186
+ });
187
+ }
188
+ };
189
+ await config.storage.cleanup?.();
190
+ const authorizationServer = createOAuthAuthorizationServer({
191
+ issuer: prepared.issuer.href,
192
+ resources: [prepared.publicUrl.href],
193
+ scopesSupported: prepared.scopes,
194
+ signingKey: await config.storage.signingKey(),
195
+ additionalPublicJwks: config.advanced?.additionalPublicJwks,
196
+ store: config.storage.authorizationServer,
197
+ interaction,
198
+ accessTokenTtlSeconds: config.advanced?.accessTokenTtlSeconds,
199
+ authorizationCodeTtlSeconds: config.advanced?.authorizationCodeTtlSeconds,
200
+ authorizationTransactionTtlSeconds: config.advanced?.authorizationTransactionTtlSeconds,
201
+ refreshTokenTtlSeconds: config.advanced?.refreshTokenTtlSeconds
202
+ });
203
+ const requestHandler = async (request, response) => {
204
+ const url = new URL(request.url ?? "/", prepared.issuer);
205
+ if (request.method === "GET" && url.pathname === "/healthz") {
206
+ response.writeHead(200, { "content-type": "application/json", "cache-control": "no-store" });
207
+ response.end('{"ok":true}');
208
+ return true;
209
+ }
210
+ if (customInteraction?.paths.includes(url.pathname) === true) {
211
+ const body = request.method === "GET" || request.method === "HEAD" ? undefined : await readBody(request);
212
+ const handled = await customInteraction.handle({
213
+ request: toWebRequest(request, prepared.issuer, body),
214
+ async complete({ transactionId, accountId, credential }) {
215
+ const subject = await config.storage.resolveSubject(config.provider.name, accountId);
216
+ const completed = await authorizationServer.completeAuthorization({
217
+ transactionId,
218
+ subject
219
+ });
220
+ await config.storage.credentials.set(subject, credential);
221
+ await config.storage.interactions.delete(transactionId);
222
+ return new Response(null, {
223
+ status: 303,
224
+ headers: {
225
+ location: completed.redirectUrl.href,
226
+ "cache-control": "no-store",
227
+ "content-security-policy": "default-src 'none'",
228
+ "referrer-policy": "no-referrer"
229
+ }
230
+ });
231
+ }
232
+ });
233
+ await writeWebResponse(response, handled);
234
+ return true;
235
+ }
236
+ if (request.method === "POST" && url.pathname === "/oauth/connect") {
237
+ const body = new URLSearchParams((await readBody(request)).toString("utf8"));
238
+ const transactionId = body.get("transaction") ?? "";
239
+ const transaction = await config.storage.interactions.get(transactionId);
240
+ const csrf = body.get("csrf") ?? "";
241
+ if (transaction === undefined ||
242
+ !verifyAuthorizationInteractionCsrf({
243
+ cookieHeader: request.headers.cookie ?? null,
244
+ submittedToken: csrf
245
+ }) ||
246
+ transaction.expiresAt <= Date.now()) {
247
+ response.writeHead(400, {
248
+ "content-type": "text/plain; charset=utf-8",
249
+ "cache-control": "no-store"
250
+ });
251
+ response.end("This connection has expired or was already used. Restart the connection.");
252
+ return true;
253
+ }
254
+ const controller = new AbortController();
255
+ request.once("aborted", () => controller.abort());
256
+ const values = Object.assign(Object.create(null), {
257
+ signal: controller.signal
258
+ });
259
+ for (const field of fields)
260
+ values[field.name] = body.get(field.name) ?? "";
261
+ try {
262
+ const connect = config.provider.connect;
263
+ if (connect === undefined)
264
+ throw new Error("Provider form connection is not configured.");
265
+ const connected = await connect(values);
266
+ if (connected.accountId.trim().length === 0)
267
+ throw new Error("Provider returned an empty accountId.");
268
+ const subject = await config.storage.resolveSubject(config.provider.name, connected.accountId);
269
+ const completed = await authorizationServer.completeAuthorization({
270
+ transactionId,
271
+ subject
272
+ });
273
+ await config.storage.credentials.set(subject, connected.credential);
274
+ await config.storage.interactions.delete(transactionId);
275
+ response.writeHead(303, {
276
+ location: completed.redirectUrl.href,
277
+ "cache-control": "no-store",
278
+ "content-security-policy": "default-src 'none'",
279
+ "referrer-policy": "no-referrer"
280
+ });
281
+ response.end();
282
+ }
283
+ catch (error) {
284
+ const safeError = error instanceof HostedOAuthLoginError
285
+ ? error.message
286
+ : "Sign-in failed. Please try again.";
287
+ response.writeHead(400, {
288
+ "content-type": "text/html; charset=utf-8",
289
+ "content-security-policy": "default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; base-uri 'none'; frame-ancestors 'none'",
290
+ "cache-control": "no-store",
291
+ "referrer-policy": "no-referrer"
292
+ });
293
+ response.end(renderLogin(displayName, fields, transaction, csrf, safeError));
294
+ }
295
+ return true;
296
+ }
297
+ const oauthPaths = new Set([
298
+ "/.well-known/oauth-authorization-server",
299
+ "/.well-known/jwks.json",
300
+ "/authorize",
301
+ "/register",
302
+ "/token",
303
+ "/revoke"
304
+ ]);
305
+ if (!oauthPaths.has(url.pathname))
306
+ return false;
307
+ const body = request.method === "POST" ? await readBody(request) : undefined;
308
+ await writeWebResponse(response, await authorizationServer.handle(toWebRequest(request, prepared.issuer, body)));
309
+ return true;
310
+ };
311
+ return {
312
+ mcpPath: prepared.publicUrl.pathname,
313
+ oauth: {
314
+ resource: prepared.publicUrl.href,
315
+ authorizationServers: [authorizationServer.issuer],
316
+ requiredScopes: ["mcp"],
317
+ scopesSupported: [...prepared.scopes],
318
+ verifier: {
319
+ async verify(input) {
320
+ const verified = await authorizationServer.verifyAccessToken(input.token, prepared.publicUrl.href);
321
+ return {
322
+ token: input.token,
323
+ issuer: authorizationServer.issuer,
324
+ audience: [verified.resource],
325
+ scopes: [...verified.scopes],
326
+ expiresAt: verified.expiresAt,
327
+ claims: { sub: verified.subject, client_id: verified.clientId, jti: verified.tokenId },
328
+ subject: verified.subject,
329
+ clientId: verified.clientId
330
+ };
331
+ }
332
+ }
333
+ },
334
+ requestHandler,
335
+ async requestServices(subject) {
336
+ await credentialsFor(config.storage, subject).read();
337
+ return config.provider.services({ credentials: credentialsFor(config.storage, subject) });
338
+ }
339
+ };
340
+ }
341
+ export function createInMemoryHostedOAuthStorage(options) {
342
+ if (options.development !== true) {
343
+ throw new Error("In-memory hosted OAuth storage requires explicit development mode.");
344
+ }
345
+ const credentials = new Map();
346
+ const interactions = new Map();
347
+ const updates = new Map();
348
+ const subjectSalt = randomBytes(32);
349
+ let signingKeyPromise;
350
+ return {
351
+ authorizationServer: createInMemoryAuthorizationServerStore(),
352
+ interactions: {
353
+ async set(transaction) {
354
+ interactions.set(transaction.id, structuredClone(transaction));
355
+ },
356
+ async get(transactionId) {
357
+ const transaction = interactions.get(transactionId);
358
+ return transaction === undefined ? undefined : structuredClone(transaction);
359
+ },
360
+ async delete(transactionId) {
361
+ interactions.delete(transactionId);
362
+ }
363
+ },
364
+ capabilities: {
365
+ durable: false,
366
+ encryptedCredentials: false,
367
+ stableKeys: false,
368
+ shared: false
369
+ },
370
+ credentials: {
371
+ async get(subject) {
372
+ return credentials.get(subject);
373
+ },
374
+ async set(subject, credential) {
375
+ credentials.set(subject, credential);
376
+ },
377
+ async delete(subject) {
378
+ credentials.delete(subject);
379
+ },
380
+ async update(subject, update) {
381
+ const previous = updates.get(subject) ?? Promise.resolve();
382
+ const next = previous.then(async () => {
383
+ const current = credentials.get(subject);
384
+ if (current === undefined)
385
+ throw new Error("Provider credential is missing; reconnect required.");
386
+ const replacement = await update(current);
387
+ credentials.set(subject, replacement);
388
+ return replacement;
389
+ });
390
+ updates.set(subject, next);
391
+ try {
392
+ return await next;
393
+ }
394
+ finally {
395
+ if (updates.get(subject) === next)
396
+ updates.delete(subject);
397
+ }
398
+ }
399
+ },
400
+ async signingKey() {
401
+ signingKeyPromise ??= (async () => {
402
+ const { privateKey, publicKey } = generateKeyPairSync("ec", {
403
+ namedCurve: "prime256v1"
404
+ });
405
+ return {
406
+ algorithm: "ES256",
407
+ keyId: randomBytes(16).toString("base64url"),
408
+ privateKey,
409
+ publicJwk: await exportJWK(publicKey)
410
+ };
411
+ })();
412
+ return await signingKeyPromise;
413
+ },
414
+ async resolveSubject(providerName, accountId) {
415
+ return createHmac("sha256", subjectSalt)
416
+ .update(providerName)
417
+ .update("\0")
418
+ .update(accountId)
419
+ .digest("base64url");
420
+ }
421
+ };
422
+ }
@@ -1,18 +1,20 @@
1
1
  import "./node-require-shim.js";
2
- import { type HttpListenOptions, type HttpServer, type HttpServerHandle, type HttpToolContext, type HttpTransportOptions } from "../../tiny-http-mcp-server/dist/server.js";
2
+ import { type HttpListenOptions, type HttpServer, type HttpServerHandle, type HttpToolContext, type HttpTransportOptions, type TinyHttpMcpServerOAuthOptions } from "../../tiny-http-mcp-server/dist/server.js";
3
3
  import type { Group } from "./index.js";
4
4
  import type { OAuthAuthorizationServer } from "../../mcp-oauth-server/dist/index.js";
5
5
  import { type RunMCPOptions } from "./mcp.js";
6
+ import { type HostedOAuthConfiguration } from "./http-hosted-oauth.js";
6
7
  export type ToolcraftHTTPContext = HttpToolContext;
7
8
  export type ToolcraftHTTPServer = HttpServer;
8
9
  export type ToolcraftHTTPServerHandle = HttpServerHandle;
9
- type HTTPTransportControls = Omit<HttpTransportOptions, "name" | "version" | "validateToolArguments">;
10
+ type HTTPTransportControls = Omit<HttpTransportOptions, "name" | "version" | "validateToolArguments" | "oauth" | "requestHandler">;
10
11
  type HTTPListenControls = Omit<HttpListenOptions, "port" | "hostname" | "path">;
11
12
  export interface RunHTTPMCPOptions<TServices extends object = Record<string, unknown>> extends RunMCPOptions<TServices>, HTTPTransportControls, HTTPListenControls {
12
13
  hostname?: string;
13
14
  port?: number;
14
15
  path?: string;
15
16
  requestServices?(context: ToolcraftHTTPContext): Partial<TServices> | Promise<Partial<TServices>>;
17
+ oauth?: TinyHttpMcpServerOAuthOptions | HostedOAuthConfiguration<unknown, TServices>;
16
18
  }
17
19
  export interface HTTPMCPAuthorizationOptions {
18
20
  authorizationServer: Pick<OAuthAuthorizationServer, "issuer" | "verifyAccessToken">;