@diffra-ai/mcp 0.0.0-stage → 0.1.1

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.
Files changed (125) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE +22 -0
  3. package/README.md +190 -2
  4. package/THIRD_PARTY_NOTICES/modelcontextprotocol-typescript-sdk.txt +21 -0
  5. package/dist/auth-provider.d.ts +19 -0
  6. package/dist/auth-provider.d.ts.map +1 -0
  7. package/dist/auth-provider.js +2 -0
  8. package/dist/auth-provider.js.map +1 -0
  9. package/dist/client.d.ts +89 -0
  10. package/dist/client.d.ts.map +1 -0
  11. package/dist/client.js +461 -0
  12. package/dist/client.js.map +1 -0
  13. package/dist/config.d.ts +117 -0
  14. package/dist/config.d.ts.map +1 -0
  15. package/dist/config.js +153 -0
  16. package/dist/config.js.map +1 -0
  17. package/dist/index.d.ts +16 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +14 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/input-schema.d.ts +7 -0
  22. package/dist/input-schema.d.ts.map +1 -0
  23. package/dist/input-schema.js +13 -0
  24. package/dist/input-schema.js.map +1 -0
  25. package/dist/model-content.d.ts +19 -0
  26. package/dist/model-content.d.ts.map +1 -0
  27. package/dist/model-content.js +67 -0
  28. package/dist/model-content.js.map +1 -0
  29. package/dist/naming.d.ts +23 -0
  30. package/dist/naming.d.ts.map +1 -0
  31. package/dist/naming.js +37 -0
  32. package/dist/naming.js.map +1 -0
  33. package/dist/oauth/auth-provider.d.ts +52 -0
  34. package/dist/oauth/auth-provider.d.ts.map +1 -0
  35. package/dist/oauth/auth-provider.js +97 -0
  36. package/dist/oauth/auth-provider.js.map +1 -0
  37. package/dist/oauth/callback.d.ts +48 -0
  38. package/dist/oauth/callback.d.ts.map +1 -0
  39. package/dist/oauth/callback.js +119 -0
  40. package/dist/oauth/callback.js.map +1 -0
  41. package/dist/oauth/client-settings.d.ts +107 -0
  42. package/dist/oauth/client-settings.d.ts.map +1 -0
  43. package/dist/oauth/client-settings.js +114 -0
  44. package/dist/oauth/client-settings.js.map +1 -0
  45. package/dist/oauth/discovery.d.ts +30 -0
  46. package/dist/oauth/discovery.d.ts.map +1 -0
  47. package/dist/oauth/discovery.js +156 -0
  48. package/dist/oauth/discovery.js.map +1 -0
  49. package/dist/oauth/errors.d.ts +32 -0
  50. package/dist/oauth/errors.d.ts.map +1 -0
  51. package/dist/oauth/errors.js +58 -0
  52. package/dist/oauth/errors.js.map +1 -0
  53. package/dist/oauth/flow.d.ts +112 -0
  54. package/dist/oauth/flow.d.ts.map +1 -0
  55. package/dist/oauth/flow.js +360 -0
  56. package/dist/oauth/flow.js.map +1 -0
  57. package/dist/oauth/index.d.ts +11 -0
  58. package/dist/oauth/index.d.ts.map +1 -0
  59. package/dist/oauth/index.js +9 -0
  60. package/dist/oauth/index.js.map +1 -0
  61. package/dist/oauth/loopback.d.ts +51 -0
  62. package/dist/oauth/loopback.d.ts.map +1 -0
  63. package/dist/oauth/loopback.js +98 -0
  64. package/dist/oauth/loopback.js.map +1 -0
  65. package/dist/oauth/provider.d.ts +62 -0
  66. package/dist/oauth/provider.d.ts.map +1 -0
  67. package/dist/oauth/provider.js +118 -0
  68. package/dist/oauth/provider.js.map +1 -0
  69. package/dist/oauth/sign-in.d.ts +73 -0
  70. package/dist/oauth/sign-in.d.ts.map +1 -0
  71. package/dist/oauth/sign-in.js +182 -0
  72. package/dist/oauth/sign-in.js.map +1 -0
  73. package/dist/oauth/types.d.ts +83 -0
  74. package/dist/oauth/types.d.ts.map +1 -0
  75. package/dist/oauth/types.js +113 -0
  76. package/dist/oauth/types.js.map +1 -0
  77. package/dist/output-limit.d.ts +24 -0
  78. package/dist/output-limit.d.ts.map +1 -0
  79. package/dist/output-limit.js +58 -0
  80. package/dist/output-limit.js.map +1 -0
  81. package/dist/protocol/content.d.ts +83 -0
  82. package/dist/protocol/content.d.ts.map +1 -0
  83. package/dist/protocol/content.js +41 -0
  84. package/dist/protocol/content.js.map +1 -0
  85. package/dist/protocol/jsonrpc.d.ts +59 -0
  86. package/dist/protocol/jsonrpc.d.ts.map +1 -0
  87. package/dist/protocol/jsonrpc.js +67 -0
  88. package/dist/protocol/jsonrpc.js.map +1 -0
  89. package/dist/protocol/types.d.ts +123 -0
  90. package/dist/protocol/types.d.ts.map +1 -0
  91. package/dist/protocol/types.js +7 -0
  92. package/dist/protocol/types.js.map +1 -0
  93. package/dist/result-schema.d.ts +10 -0
  94. package/dist/result-schema.d.ts.map +1 -0
  95. package/dist/result-schema.js +78 -0
  96. package/dist/result-schema.js.map +1 -0
  97. package/dist/testing/index.d.ts +2 -0
  98. package/dist/testing/index.d.ts.map +1 -0
  99. package/dist/testing/index.js +2 -0
  100. package/dist/testing/index.js.map +1 -0
  101. package/dist/transports/in-memory.d.ts +19 -0
  102. package/dist/transports/in-memory.d.ts.map +1 -0
  103. package/dist/transports/in-memory.js +50 -0
  104. package/dist/transports/in-memory.js.map +1 -0
  105. package/dist/transports/lines.d.ts +34 -0
  106. package/dist/transports/lines.d.ts.map +1 -0
  107. package/dist/transports/lines.js +85 -0
  108. package/dist/transports/lines.js.map +1 -0
  109. package/dist/transports/pipe.d.ts +49 -0
  110. package/dist/transports/pipe.d.ts.map +1 -0
  111. package/dist/transports/pipe.js +96 -0
  112. package/dist/transports/pipe.js.map +1 -0
  113. package/dist/transports/stdio.d.ts +29 -0
  114. package/dist/transports/stdio.d.ts.map +1 -0
  115. package/dist/transports/stdio.js +170 -0
  116. package/dist/transports/stdio.js.map +1 -0
  117. package/dist/transports/streamable-http.d.ts +103 -0
  118. package/dist/transports/streamable-http.d.ts.map +1 -0
  119. package/dist/transports/streamable-http.js +531 -0
  120. package/dist/transports/streamable-http.js.map +1 -0
  121. package/dist/transports/transport.d.ts +36 -0
  122. package/dist/transports/transport.d.ts.map +1 -0
  123. package/dist/transports/transport.js +38 -0
  124. package/dist/transports/transport.js.map +1 -0
  125. package/package.json +59 -5
@@ -0,0 +1,52 @@
1
+ /**
2
+ * The auth provider for connections to an OAuth-protected MCP server: it sends the stored access token
3
+ * and refreshes it, but never starts a browser flow. When the user has to sign in, it fails with
4
+ * `McpOAuthAuthorizationRequiredError` and the application runs `startMcpSignIn`.
5
+ */
6
+ import type { AuthProvider, McpFetch } from "../auth-provider.ts";
7
+ import { type McpOAuthClientOptions, type McpOAuthSettings } from "./client-settings.ts";
8
+ import { type OAuthFlowResult } from "./flow.ts";
9
+ import type { McpOAuthState } from "./provider.ts";
10
+ import type { OAuthChallenge } from "./types.ts";
11
+ /** An `AuthProvider` that can say when its refreshes have finished. */
12
+ export interface McpAuthProvider extends AuthProvider {
13
+ /** Resolves when no refresh is running, so shutdown does not drop rotated tokens before they are saved. */
14
+ settled(): Promise<void>;
15
+ }
16
+ /**
17
+ * Auth provider for MCP connections: sends the stored access token and refreshes it when it is about
18
+ * to expire or after a 401. Throws `McpOAuthAuthorizationRequiredError` when the user has to sign in,
19
+ * including when the server asks for more scope (`insufficient_scope`). `onChallenge` receives the
20
+ * server's `WWW-Authenticate` challenge so sign-in can use its resource metadata URL and scope.
21
+ * `settings` is only called when a refresh is needed, so a secret that fails to resolve fails the
22
+ * refresh instead of the whole connection setup.
23
+ *
24
+ * Many servers rotate refresh tokens, so two refreshes with the same refresh token lose the grant.
25
+ * Requests in this process share one refresh, and other processes are kept out by the store's
26
+ * refresh lock, held from reading the tokens to saving new ones. Tokens that changed meanwhile
27
+ * (another process refreshed them, or the user signed in) are used without refreshing.
28
+ */
29
+ export declare function createMcpAuthProvider(options: Omit<McpOAuthClientOptions, "settings"> & {
30
+ settings: () => McpOAuthSettings;
31
+ onChallenge: (challenge: OAuthChallenge) => void;
32
+ /**
33
+ * A redirect URI the application signs in with, such as its https callback route. Refreshing tokens
34
+ * from a sign-in with a Client ID Metadata Document needs a redirect URI the documents list; a
35
+ * refresh never redirects, so any listed one works. Default: the loopback redirect URI of `settings`.
36
+ */
37
+ redirectUrl?: string;
38
+ }): McpAuthProvider;
39
+ /**
40
+ * Refresh the tokens of `state` with the stored client (or the document it signed in with), or report
41
+ * `REDIRECT` when that is not possible. `options.redirectUrl` is a redirect URI the application signs in
42
+ * with, tried before the loopback ones of the settings and the stored client. Never registers a client or
43
+ * prepares an authorization request, so a pending sign-in stays usable. Call while holding the store's
44
+ * refresh lock. Not cancellable: a refresh the server answered may have rotated the refresh token, so its
45
+ * answer must be saved.
46
+ */
47
+ export declare function refreshStoredTokens(client: McpOAuthClientOptions, state: McpOAuthState, options?: {
48
+ fetch?: McpFetch;
49
+ challenge?: OAuthChallenge;
50
+ redirectUrl?: string;
51
+ }): Promise<OAuthFlowResult>;
52
+ //# sourceMappingURL=auth-provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-provider.d.ts","sourceRoot":"","sources":["../../src/oauth/auth-provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAIN,KAAK,qBAAqB,EAC1B,KAAK,gBAAgB,EAGrB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EAAgB,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAC/D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,uEAAuE;AACvE,MAAM,WAAW,eAAgB,SAAQ,YAAY;IACpD,2GAA2G;IAC3G,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACzB;AAKD;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CACpC,OAAO,EAAE,IAAI,CAAC,qBAAqB,EAAE,UAAU,CAAC,GAAG;IAClD,QAAQ,EAAE,MAAM,gBAAgB,CAAC;IACjC,WAAW,EAAE,CAAC,SAAS,EAAE,cAAc,KAAK,IAAI,CAAC;IACjD;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,GACC,eAAe,CA2CjB;AAED;;;;;;;GAOG;AACH,wBAAsB,mBAAmB,CACxC,MAAM,EAAE,qBAAqB,EAC7B,KAAK,EAAE,aAAa,EACpB,OAAO,GAAE;IAAE,KAAK,CAAC,EAAE,QAAQ,CAAC;IAAC,SAAS,CAAC,EAAE,cAAc,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAO,GAClF,OAAO,CAAC,eAAe,CAAC,CAiB1B"}
@@ -0,0 +1,97 @@
1
+ /**
2
+ * The auth provider for connections to an OAuth-protected MCP server: it sends the stored access token
3
+ * and refreshes it, but never starts a browser flow. When the user has to sign in, it fails with
4
+ * `McpOAuthAuthorizationRequiredError` and the application runs `startMcpSignIn`.
5
+ */
6
+ import { createOAuthClientProvider, DEFAULT_REDIRECT_URL, loopbackSettings, registeredRedirectUrls, timedFetch, } from "./client-settings.js";
7
+ import { parseWwwAuthenticate } from "./discovery.js";
8
+ import { McpOAuthAuthorizationRequiredError } from "./errors.js";
9
+ import { authorizeMcp } from "./flow.js";
10
+ /** Access tokens this close to expiry are refreshed before they are sent. */
11
+ const REFRESH_SKEW_MS = 30_000;
12
+ /**
13
+ * Auth provider for MCP connections: sends the stored access token and refreshes it when it is about
14
+ * to expire or after a 401. Throws `McpOAuthAuthorizationRequiredError` when the user has to sign in,
15
+ * including when the server asks for more scope (`insufficient_scope`). `onChallenge` receives the
16
+ * server's `WWW-Authenticate` challenge so sign-in can use its resource metadata URL and scope.
17
+ * `settings` is only called when a refresh is needed, so a secret that fails to resolve fails the
18
+ * refresh instead of the whole connection setup.
19
+ *
20
+ * Many servers rotate refresh tokens, so two refreshes with the same refresh token lose the grant.
21
+ * Requests in this process share one refresh, and other processes are kept out by the store's
22
+ * refresh lock, held from reading the tokens to saving new ones. Tokens that changed meanwhile
23
+ * (another process refreshed them, or the user signed in) are used without refreshing.
24
+ */
25
+ export function createMcpAuthProvider(options) {
26
+ const { store, redirectUrl } = options;
27
+ let refreshing;
28
+ /** Replace `staleToken`, the access token that expired or was rejected. */
29
+ const refresh = (staleToken, fetch = globalThis.fetch, challenge) => {
30
+ refreshing ??= store
31
+ .withRefreshLock(async () => {
32
+ const state = await store.load();
33
+ if (state?.tokens?.access_token !== staleToken)
34
+ return;
35
+ if (!state?.tokens?.refresh_token)
36
+ throw new McpOAuthAuthorizationRequiredError();
37
+ const client = { ...options, settings: options.settings() };
38
+ const result = await refreshStoredTokens(client, state, { fetch, challenge, redirectUrl });
39
+ if (result === "REDIRECT")
40
+ throw new McpOAuthAuthorizationRequiredError();
41
+ })
42
+ .finally(() => {
43
+ refreshing = undefined;
44
+ });
45
+ return refreshing;
46
+ };
47
+ return {
48
+ token: async () => {
49
+ await refreshing?.catch(() => undefined);
50
+ const state = await store.load();
51
+ const token = state?.tokens?.access_token;
52
+ const expired = state?.tokensExpireAt !== undefined && state.tokensExpireAt - REFRESH_SKEW_MS <= Date.now();
53
+ if (!expired || !state?.tokens?.refresh_token)
54
+ return token;
55
+ // Failures fall through: the request goes out with the old token and a 401 decides what happens.
56
+ await refresh(token).catch(() => undefined);
57
+ return (await store.load())?.tokens?.access_token;
58
+ },
59
+ onUnauthorized: async (context) => {
60
+ const challenge = parseWwwAuthenticate(context.response.headers.get("www-authenticate"));
61
+ options.onChallenge(challenge);
62
+ // A refresh keeps the granted scope, so more scope needs a new sign-in.
63
+ if (challenge.error === "insufficient_scope")
64
+ throw new McpOAuthAuthorizationRequiredError();
65
+ await refresh(context.token, context.fetch, challenge);
66
+ },
67
+ settled: async () => {
68
+ await refreshing?.catch(() => undefined);
69
+ },
70
+ };
71
+ }
72
+ /**
73
+ * Refresh the tokens of `state` with the stored client (or the document it signed in with), or report
74
+ * `REDIRECT` when that is not possible. `options.redirectUrl` is a redirect URI the application signs in
75
+ * with, tried before the loopback ones of the settings and the stored client. Never registers a client or
76
+ * prepares an authorization request, so a pending sign-in stays usable. Call while holding the store's
77
+ * refresh lock. Not cancellable: a refresh the server answered may have rotated the refresh token, so its
78
+ * answer must be saved.
79
+ */
80
+ export async function refreshStoredTokens(client, state, options = {}) {
81
+ // Refreshing never redirects the user; the redirect URI only decides whether tokens without a stored
82
+ // client can be refreshed with a Client ID Metadata Document, so any URI the documents list works.
83
+ const candidates = [
84
+ options.redirectUrl,
85
+ loopbackSettings(client.settings).fixedRedirectUrl,
86
+ registeredRedirectUrls(state.clientInformation)[0],
87
+ ].filter((url) => url !== undefined);
88
+ const redirectUrl = candidates.find((url) => client.clientMetadataDocuments?.listsRedirectUrl(url)) ?? DEFAULT_REDIRECT_URL;
89
+ return authorizeMcp(createOAuthClientProvider(client, redirectUrl), {
90
+ serverUrl: client.serverUrl,
91
+ resourceMetadataUrl: options.challenge?.resourceMetadataUrl,
92
+ authorizationServerMetadataUrl: client.settings.authServerMetadataUrl,
93
+ fetch: timedFetch(options.fetch),
94
+ refreshOnly: true,
95
+ });
96
+ }
97
+ //# sourceMappingURL=auth-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-provider.js","sourceRoot":"","sources":["../../src/oauth/auth-provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EACN,yBAAyB,EACzB,oBAAoB,EACpB,gBAAgB,EAGhB,sBAAsB,EACtB,UAAU,GACV,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,kCAAkC,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,YAAY,EAAwB,MAAM,WAAW,CAAC;AAU/D,6EAA6E;AAC7E,MAAM,eAAe,GAAG,MAAM,CAAC;AAE/B;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,qBAAqB,CACpC,OASC;IAED,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC;IACvC,IAAI,UAAqC,CAAC;IAE1C,2EAA2E;IAC3E,MAAM,OAAO,GAAG,CAAC,UAA8B,E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,MAAM,CAAC,uBAAuB,EAAE,gBAAgB,CAAC,GAAG,CAAC,CAAC,IAAI,oBAAoB,CAAC;IACzG,OAAO,YAAY,CAAC,yBAAyB,CAAC,MAAM,EAAE,WAAW,CAAC,EAAE;QACnE,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,mBAAmB,EAAE,OAAO,CAAC,SAAS,EAAE,mBAAmB;QAC3D,8BAA8B,EAAE,MAAM,CAAC,QAAQ,CAAC,qBAAqB;QACrE,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC;QAChC,WAAW,EAAE,IAAI;KACjB,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * The auth provider for connections to an OAuth-protected MCP server: it sends the stored access token\n * and refreshes it, but never starts a browser flow. When the user has to sign in, it fails with\n * `McpOAuthAuthorizationRequiredError` and the application runs `startMcpSignIn`.\n */\n\nimport type { AuthProvider, McpFetch } from \"../auth-provider.ts\";\nimport {\n\tcreateOAuthClientProvider,\n\tDEFAULT_REDIRECT_URL,\n\tloopbackSettings,\n\ttype McpOAuthClientOptions,\n\ttype McpOAuthSettings,\n\tregisteredRedirectUrls,\n\ttimedFetch,\n} from \"./client-settings.ts\";\nimport { parseWwwAuthenticate } from \"./discovery.ts\";\nimport { McpOAuthAuthorizationRequiredError } from \"./errors.ts\";\nimport { authorizeMcp, type OAuthFlowResult } from \"./flow.ts\";\nimport type { McpOAuthState } from \"./provider.ts\";\nimport type { OAuthChallenge } from \"./types.ts\";\n\n/** An `AuthProvider` that can say when its refreshes have finished. */\nexport interface McpAuthProvider extends AuthProvider {\n\t/** Resolves when no refresh is running, so shutdown does not drop rotated tokens before they are saved. */\n\tsettled(): Promise<void>;\n}\n\n/** Access tokens this close to expiry are refreshed before they are sent. */\nconst REFRESH_SKEW_MS = 30_000;\n\n/**\n * Auth provider for MCP connections: sends the stored access token and refreshes it when it is about\n * to expire or after a 401. Throws `McpOAuthAuthorizationRequiredError` when the user has to sign in,\n * including when the server asks for more scope (`insufficient_scope`). `onChallenge` receives the\n * server's `WWW-Authenticate` challenge so sign-in can use its resource metadata URL and scope.\n * `settings` is only called when a refresh is needed, so a secret that fails to resolve fails the\n * refresh instead of the whole connection setup.\n *\n * Many servers rotate refresh tokens, so two refreshes with the same refresh token lose the grant.\n * Requests in this process share one refresh, and other processes are kept out by the store's\n * refresh lock, held from reading the tokens to saving new ones. Tokens that changed meanwhile\n * (another process refreshed them, or the user signed in) are used without refreshing.\n */\nexport function createMcpAuthProvider(\n\toptions: Omit<McpOAuthClientOptions, \"settings\"> & {\n\t\tsettings: () => McpOAuthSettings;\n\t\tonChallenge: (challenge: OAuthChallenge) => void;\n\t\t/**\n\t\t * A redirect URI the application signs in with, such as its https callback route. Refreshing tokens\n\t\t * from a sign-in with a Client ID Metadata Document needs a redirect URI the documents list; a\n\t\t * refresh never redirects, so any listed one works. Default: the loopback redirect URI of `settings`.\n\t\t */\n\t\tredirectUrl?: string;\n\t},\n): McpAuthProvider {\n\tconst { store, redirectUrl } = options;\n\tlet refreshing: Promise<void> | undefined;\n\n\t/** Replace `staleToken`, the access token that expired or was rejected. */\n\tconst refresh = (staleToken: string | undefined, fetch: McpFetch = globalThis.fetch, challenge?: OAuthChallenge) => {\n\t\trefreshing ??= store\n\t\t\t.withRefreshLock(async () => {\n\t\t\t\tconst state = await store.load();\n\t\t\t\tif (state?.tokens?.access_token !== staleToken) return;\n\t\t\t\tif (!state?.tokens?.refresh_token) throw new McpOAuthAuthorizationRequiredError();\n\t\t\t\tconst client = { ...options, settings: options.settings() };\n\t\t\t\tconst result = await refreshStoredTokens(client, state, { fetch, challenge, redirectUrl });\n\t\t\t\tif (result === \"REDIRECT\") throw new McpOAuthAuthorizationRequiredError();\n\t\t\t})\n\t\t\t.finally(() => {\n\t\t\t\trefreshing = undefined;\n\t\t\t});\n\t\treturn refreshing;\n\t};\n\n\treturn {\n\t\ttoken: async () => {\n\t\t\tawait refreshing?.catch(() => undefined);\n\t\t\tconst state = await store.load();\n\t\t\tconst token = state?.tokens?.access_token;\n\t\t\tconst expired = state?.tokensExpireAt !== undefined && state.tokensExpireAt - REFRESH_SKEW_MS <= Date.now();\n\t\t\tif (!expired || !state?.tokens?.refresh_token) return token;\n\t\t\t// Failures fall through: the request goes out with the old token and a 401 decides what happens.\n\t\t\tawait refresh(token).catch(() => undefined);\n\t\t\treturn (await store.load())?.tokens?.access_token;\n\t\t},\n\t\tonUnauthorized: async (context) => {\n\t\t\tconst challenge = parseWwwAuthenticate(context.response.headers.get(\"www-authenticate\"));\n\t\t\toptions.onChallenge(challenge);\n\t\t\t// A refresh keeps the granted scope, so more scope needs a new sign-in.\n\t\t\tif (challenge.error === \"insufficient_scope\") throw new McpOAuthAuthorizationRequiredError();\n\t\t\tawait refresh(context.token, context.fetch, challenge);\n\t\t},\n\t\tsettled: async () => {\n\t\t\tawait refreshing?.catch(() => undefined);\n\t\t},\n\t};\n}\n\n/**\n * Refresh the tokens of `state` with the stored client (or the document it signed in with), or report\n * `REDIRECT` when that is not possible. `options.redirectUrl` is a redirect URI the application signs in\n * with, tried before the loopback ones of the settings and the stored client. Never registers a client or\n * prepares an authorization request, so a pending sign-in stays usable. Call while holding the store's\n * refresh lock. Not cancellable: a refresh the server answered may have rotated the refresh token, so its\n * answer must be saved.\n */\nexport async function refreshStoredTokens(\n\tclient: McpOAuthClientOptions,\n\tstate: McpOAuthState,\n\toptions: { fetch?: McpFetch; challenge?: OAuthChallenge; redirectUrl?: string } = {},\n): Promise<OAuthFlowResult> {\n\t// Refreshing never redirects the user; the redirect URI only decides whether tokens without a stored\n\t// client can be refreshed with a Client ID Metadata Document, so any URI the documents list works.\n\tconst candidates = [\n\t\toptions.redirectUrl,\n\t\tloopbackSettings(client.settings).fixedRedirectUrl,\n\t\tregisteredRedirectUrls(state.clientInformation)[0],\n\t].filter((url) => url !== undefined);\n\tconst redirectUrl =\n\t\tcandidates.find((url) => client.clientMetadataDocuments?.listsRedirectUrl(url)) ?? DEFAULT_REDIRECT_URL;\n\treturn authorizeMcp(createOAuthClientProvider(client, redirectUrl), {\n\t\tserverUrl: client.serverUrl,\n\t\tresourceMetadataUrl: options.challenge?.resourceMetadataUrl,\n\t\tauthorizationServerMetadataUrl: client.settings.authServerMetadataUrl,\n\t\tfetch: timedFetch(options.fetch),\n\t\trefreshOnly: true,\n\t});\n}\n"]}
@@ -0,0 +1,48 @@
1
+ export interface OAuthCallback {
2
+ code: string;
3
+ state: string;
4
+ iss?: string;
5
+ }
6
+ /** Outcome shown on the browser page after the redirect. */
7
+ export type OAuthCallbackPage = {
8
+ ok: true;
9
+ } | {
10
+ ok: false;
11
+ message: string;
12
+ details?: string;
13
+ };
14
+ export interface OAuthCallbackServerOptions {
15
+ /** Address to listen on. Default: `127.0.0.1`. */
16
+ host?: string;
17
+ /**
18
+ * Host name in `redirectUrl`, for example `localhost` for a client registered with it while
19
+ * listening on `127.0.0.1`. Default: `host`.
20
+ */
21
+ redirectHost?: string;
22
+ port?: number;
23
+ path?: string;
24
+ /** More paths that receive the callback, for example a server-specific path of a redirect URI. */
25
+ extraPaths?: string[];
26
+ timeoutMs?: number;
27
+ /** Render the browser page as HTML. Default: a plain-text message. */
28
+ renderPage?: (page: OAuthCallbackPage) => string;
29
+ }
30
+ export declare class OAuthCallbackServer {
31
+ readonly redirectUrl: string;
32
+ private server;
33
+ private paths;
34
+ private timeoutMs;
35
+ private renderPage;
36
+ private pending;
37
+ private constructor();
38
+ static listen(options?: OAuthCallbackServerOptions): Promise<OAuthCallbackServer>;
39
+ /**
40
+ * Wait for the authorization response with `state`. With `path`, a response on another path fails, so
41
+ * a server-specific redirect URI can tell authorization servers apart (RFC 9700 section 4.4.2.2).
42
+ */
43
+ waitForCallback(state: string, path?: string): Promise<OAuthCallback>;
44
+ close(): Promise<void>;
45
+ private reply;
46
+ private handle;
47
+ }
48
+ //# sourceMappingURL=callback.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"callback.d.ts","sourceRoot":"","sources":["../../src/oauth/callback.ts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AAO,CAAC,MAAM;CAwCd"}
@@ -0,0 +1,119 @@
1
+ import { createServer } from "node:http";
2
+ function plainText(page) {
3
+ if (page.ok)
4
+ return "Authorization complete. You may close this window.";
5
+ return page.details ? `${page.message}\n\n${page.details}` : page.message;
6
+ }
7
+ export class OAuthCallbackServer {
8
+ redirectUrl;
9
+ server;
10
+ paths;
11
+ timeoutMs;
12
+ renderPage;
13
+ pending = new Map();
14
+ constructor(server, redirectUrl, paths, timeoutMs, renderPage) {
15
+ this.server = server;
16
+ this.redirectUrl = redirectUrl;
17
+ this.paths = paths;
18
+ this.timeoutMs = timeoutMs;
19
+ this.renderPage = renderPage;
20
+ }
21
+ static async listen(options = {}) {
22
+ const host = options.host ?? "127.0.0.1";
23
+ const redirectHost = options.redirectHost ?? host;
24
+ const path = options.path ?? "/callback";
25
+ let instance;
26
+ const server = createServer((request, response) => instance?.handle(request.url ?? "/", response));
27
+ await new Promise((resolve, reject) => {
28
+ server.once("error", reject);
29
+ server.listen(options.port ?? 0, host, () => {
30
+ server.off("error", reject);
31
+ resolve();
32
+ });
33
+ });
34
+ const address = server.address();
35
+ if (!address || typeof address === "string")
36
+ throw new Error("OAuth callback server did not bind to TCP");
37
+ instance = new OAuthCallbackServer(server, `http://${redirectHost.includes(":") ? `[${redirectHost}]` : redirectHost}:${address.port}${path}`, [path, ...(options.extraPaths ?? [])], options.timeoutMs ?? 5 * 60_000, options.renderPage);
38
+ return instance;
39
+ }
40
+ /**
41
+ * Wait for the authorization response with `state`. With `path`, a response on another path fails, so
42
+ * a server-specific redirect URI can tell authorization servers apart (RFC 9700 section 4.4.2.2).
43
+ */
44
+ waitForCallback(state, path) {
45
+ if (this.pending.has(state))
46
+ throw new Error("OAuth state is already pending");
47
+ return new Promise((resolve, reject) => {
48
+ const timer = setTimeout(() => {
49
+ this.pending.delete(state);
50
+ reject(new Error("OAuth callback timed out"));
51
+ }, this.timeoutMs);
52
+ this.pending.set(state, { resolve, reject, timer, path });
53
+ });
54
+ }
55
+ async close() {
56
+ for (const pending of this.pending.values()) {
57
+ clearTimeout(pending.timer);
58
+ pending.reject(new Error("OAuth callback server closed"));
59
+ }
60
+ this.pending.clear();
61
+ const closed = new Promise((resolve, reject) => {
62
+ this.server.close((error) => (error ? reject(error) : resolve()));
63
+ });
64
+ // `close()` waits for every connection, and a browser may keep an unused one open (a preconnect or a
65
+ // spare keep-alive socket) for minutes. Responses have been sent by now, so drop them.
66
+ this.server.closeAllConnections();
67
+ await closed;
68
+ }
69
+ reply(response, status, page) {
70
+ if (this.renderPage) {
71
+ response.writeHead(status, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" });
72
+ response.end(this.renderPage(page));
73
+ }
74
+ else {
75
+ response.writeHead(status, { "content-type": "text/plain; charset=utf-8" }).end(plainText(page));
76
+ }
77
+ }
78
+ handle(rawUrl, response) {
79
+ const url = new URL(rawUrl, this.redirectUrl);
80
+ if (!this.paths.includes(url.pathname)) {
81
+ this.reply(response, 404, { ok: false, message: "Not found" });
82
+ return;
83
+ }
84
+ const state = url.searchParams.get("state");
85
+ const pending = state ? this.pending.get(state) : undefined;
86
+ if (!state || !pending) {
87
+ this.reply(response, 400, { ok: false, message: "Invalid or expired OAuth state" });
88
+ return;
89
+ }
90
+ clearTimeout(pending.timer);
91
+ this.pending.delete(state);
92
+ if (pending.path !== undefined && url.pathname !== pending.path) {
93
+ pending.reject(new Error("The authorization response arrived on another redirect URI"));
94
+ this.reply(response, 400, { ok: false, message: "Unexpected redirect URI" });
95
+ return;
96
+ }
97
+ const error = url.searchParams.get("error");
98
+ if (error) {
99
+ const description = url.searchParams.get("error_description") ?? error;
100
+ pending.reject(new Error(description));
101
+ this.reply(response, 200, {
102
+ ok: false,
103
+ message: "Authorization failed. You may close this window.",
104
+ details: description,
105
+ });
106
+ return;
107
+ }
108
+ const code = url.searchParams.get("code");
109
+ if (!code) {
110
+ pending.reject(new Error("OAuth callback did not include an authorization code"));
111
+ this.reply(response, 400, { ok: false, message: "Missing authorization code" });
112
+ return;
113
+ }
114
+ const iss = url.searchParams.get("iss");
115
+ pending.resolve({ code, state, ...(iss ? { iss } : {}) });
116
+ this.reply(response, 200, { ok: true });
117
+ }
118
+ }
119
+ //# sourceMappingURL=callback.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"callback.js","sourceRoot":"","sources":["../../src/oauth/callback.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAoC,MAAM,WAAW,CAAC;AA4B3E,SAAS,SAAS,CAAC,IAAuB;IACzC,IAAI,IAAI,CAAC,EAAE;QAAE,OAAO,oDAAoD,CAAC;IACzE,OAAO,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC;AAC3E,CAAC;AAED,MAAM,OAAO,mBAAmB;IACtB,WAAW,CAAS;IACrB,MAAM,CAAS;IACf,KAAK,CAAW;IAChB,SAAS,CAAS;IAClB,UAAU,CAAoD;IAC9D,OAAO,GAAG,IAAI,GAAG,EAQtB,CAAC;IAEJ,YACC,MAAc,EACd,WAAmB,EACnB,KAAe,EACf,SAAiB,EACjB,UAA6D;QAE7D,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC9B,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,GAA+B,EAAE;QAC3D,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,WAAW,CAAC;QACzC,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,IAAI,CAAC;QAClD,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,WAAW,CAAC;QACzC,I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AO,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;QAC1D,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;IACzC,CAAC;CACD","sourcesContent":["import { createServer, type Server, type ServerResponse } from \"node:http\";\n\nexport interface OAuthCallback {\n\tcode: string;\n\tstate: string;\n\tiss?: string;\n}\n\n/** Outcome shown on the browser page after the redirect. */\nexport type OAuthCallbackPage = { ok: true } | { ok: false; message: string; details?: string };\n\nexport interface OAuthCallbackServerOptions {\n\t/** Address to listen on. Default: `127.0.0.1`. */\n\thost?: string;\n\t/**\n\t * Host name in `redirectUrl`, for example `localhost` for a client registered with it while\n\t * listening on `127.0.0.1`. Default: `host`.\n\t */\n\tredirectHost?: string;\n\tport?: number;\n\tpath?: string;\n\t/** More paths that receive the callback, for example a server-specific path of a redirect URI. */\n\textraPaths?: string[];\n\ttimeoutMs?: number;\n\t/** Render the browser page as HTML. Default: a plain-text message. */\n\trenderPage?: (page: OAuthCallbackPage) => string;\n}\n\nfunction plainText(page: OAuthCallbackPage): string {\n\tif (page.ok) return \"Authorization complete. You may close this window.\";\n\treturn page.details ? `${page.message}\\n\\n${page.details}` : page.message;\n}\n\nexport class OAuthCallbackServer {\n\treadonly redirectUrl: string;\n\tprivate server: Server;\n\tprivate paths: string[];\n\tprivate timeoutMs: number;\n\tprivate renderPage: ((page: OAuthCallbackPage) => string) | undefined;\n\tprivate pending = new Map<\n\t\tstring,\n\t\t{\n\t\t\tresolve: (callback: OAuthCallback) => void;\n\t\t\treject: (error: Error) => void;\n\t\t\ttimer: ReturnType<typeof setTimeout>;\n\t\t\tpath: string | undefined;\n\t\t}\n\t>();\n\n\tprivate constructor(\n\t\tserver: Server,\n\t\tredirectUrl: string,\n\t\tpaths: string[],\n\t\ttimeoutMs: number,\n\t\trenderPage: ((page: OAuthCallbackPage) => string) | undefined,\n\t) {\n\t\tthis.server = server;\n\t\tthis.redirectUrl = redirectUrl;\n\t\tthis.paths = paths;\n\t\tthis.timeoutMs = timeoutMs;\n\t\tthis.renderPage = renderPage;\n\t}\n\n\tstatic async listen(options: OAuthCallbackServerOptions = {}): Promise<OAuthCallbackServer> {\n\t\tconst host = options.host ?? \"127.0.0.1\";\n\t\tconst redirectHost = options.redirectHost ?? host;\n\t\tconst path = options.path ?? \"/callback\";\n\t\tlet instance: OAuthCallbackServer | undefined;\n\t\tconst server = createServer((request, response) => instance?.handle(request.url ?? \"/\", response));\n\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\tserver.once(\"error\", reject);\n\t\t\tserver.listen(options.port ?? 0, host, () => {\n\t\t\t\tserver.off(\"error\", reject);\n\t\t\t\tresolve();\n\t\t\t});\n\t\t});\n\t\tconst address = server.address();\n\t\tif (!address || typeof address === \"string\") throw new Error(\"OAuth callback server did not bind to TCP\");\n\t\tinstance = new OAuthCallbackServer(\n\t\t\tserver,\n\t\t\t`http://${redirectHost.includes(\":\") ? `[${redirectHost}]` : redirectHost}:${address.port}${path}`,\n\t\t\t[path, ...(options.extraPaths ?? [])],\n\t\t\toptions.timeoutMs ?? 5 * 60_000,\n\t\t\toptions.renderPage,\n\t\t);\n\t\treturn instance;\n\t}\n\n\t/**\n\t * Wait for the authorization response with `state`. With `path`, a response on another path fails, so\n\t * a server-specific redirect URI can tell authorization servers apart (RFC 9700 section 4.4.2.2).\n\t */\n\twaitForCallback(state: string, path?: string): Promise<OAuthCallback> {\n\t\tif (this.pending.has(state)) throw new Error(\"OAuth state is already pending\");\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst timer = setTimeout(() => {\n\t\t\t\tthis.pending.delete(state);\n\t\t\t\treject(new Error(\"OAuth callback timed out\"));\n\t\t\t}, this.timeoutMs);\n\t\t\tthis.pending.set(state, { resolve, reject, timer, path });\n\t\t});\n\t}\n\n\tasync close(): Promise<void> {\n\t\tfor (const pending of this.pending.values()) {\n\t\t\tclearTimeout(pending.timer);\n\t\t\tpending.reject(new Error(\"OAuth callback server closed\"));\n\t\t}\n\t\tthis.pending.clear();\n\t\tconst closed = new Promise<void>((resolve, reject) => {\n\t\t\tthis.server.close((error) => (error ? reject(error) : resolve()));\n\t\t});\n\t\t// `close()` waits for every connection, and a browser may keep an unused one open (a preconnect or a\n\t\t// spare keep-alive socket) for minutes. Responses have been sent by now, so drop them.\n\t\tthis.server.closeAllConnections();\n\t\tawait closed;\n\t}\n\n\tprivate reply(response: ServerResponse, status: number, page: OAuthCallbackPage): void {\n\t\tif (this.renderPage) {\n\t\t\tresponse.writeHead(status, { \"content-type\": \"text/html; charset=utf-8\", \"cache-control\": \"no-store\" });\n\t\t\tresponse.end(this.renderPage(page));\n\t\t} else {\n\t\t\tresponse.writeHead(status, { \"content-type\": \"text/plain; charset=utf-8\" }).end(plainText(page));\n\t\t}\n\t}\n\n\tprivate handle(rawUrl: string, response: ServerResponse): void {\n\t\tconst url = new URL(rawUrl, this.redirectUrl);\n\t\tif (!this.paths.includes(url.pathname)) {\n\t\t\tthis.reply(response, 404, { ok: false, message: \"Not found\" });\n\t\t\treturn;\n\t\t}\n\t\tconst state = url.searchParams.get(\"state\");\n\t\tconst pending = state ? this.pending.get(state) : undefined;\n\t\tif (!state || !pending) {\n\t\t\tthis.reply(response, 400, { ok: false, message: \"Invalid or expired OAuth state\" });\n\t\t\treturn;\n\t\t}\n\t\tclearTimeout(pending.timer);\n\t\tthis.pending.delete(state);\n\t\tif (pending.path !== undefined && url.pathname !== pending.path) {\n\t\t\tpending.reject(new Error(\"The authorization response arrived on another redirect URI\"));\n\t\t\tthis.reply(response, 400, { ok: false, message: \"Unexpected redirect URI\" });\n\t\t\treturn;\n\t\t}\n\t\tconst error = url.searchParams.get(\"error\");\n\t\tif (error) {\n\t\t\tconst description = url.searchParams.get(\"error_description\") ?? error;\n\t\t\tpending.reject(new Error(description));\n\t\t\tthis.reply(response, 200, {\n\t\t\t\tok: false,\n\t\t\t\tmessage: \"Authorization failed. You may close this window.\",\n\t\t\t\tdetails: description,\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\t\tconst code = url.searchParams.get(\"code\");\n\t\tif (!code) {\n\t\t\tpending.reject(new Error(\"OAuth callback did not include an authorization code\"));\n\t\t\tthis.reply(response, 400, { ok: false, message: \"Missing authorization code\" });\n\t\t\treturn;\n\t\t}\n\t\tconst iss = url.searchParams.get(\"iss\");\n\t\tpending.resolve({ code, state, ...(iss ? { iss } : {}) });\n\t\tthis.reply(response, 200, { ok: true });\n\t}\n}\n"]}
@@ -0,0 +1,107 @@
1
+ /**
2
+ * The OAuth client that sign-in and refreshes use for one MCP server: its settings, how it identifies
3
+ * itself (a pre-registered client, a Client ID Metadata Document, or dynamic client registration), and
4
+ * the `McpOAuthProvider` built from them.
5
+ */
6
+ import type { McpFetch } from "../auth-provider.ts";
7
+ import { McpOAuthProvider, type McpOAuthStateStore } from "./provider.ts";
8
+ import type { OAuthClientInformationMixed } from "./types.ts";
9
+ /** Client settings of one MCP server, usually from the application's configuration. */
10
+ export interface McpOAuthSettings {
11
+ /** Pre-registered client id. Without it, sign-in uses a Client ID Metadata Document or registers a client. */
12
+ clientId?: string;
13
+ /** Already resolved. */
14
+ clientSecret?: string;
15
+ /**
16
+ * Port of the loopback callback server, for clients registered with a fixed redirect URI. Without
17
+ * `callbackUrl`, the redirect URI is `http://127.0.0.1:<port>/callback`. Only `listenForLoopbackSignIn`
18
+ * and the redirect URI of refreshes use it; `startMcpSignIn` and `completeMcpSignIn` take theirs from
19
+ * the caller.
20
+ */
21
+ callbackPort?: number;
22
+ /**
23
+ * Loopback redirect URI registered for `clientId`, for example `http://localhost:8080/oauth/callback`.
24
+ * Without a port, the callback server listens on `callbackPort` or a free port, which is added to the
25
+ * URI (RFC 8252). Default: `http://127.0.0.1/callback`. Like `callbackPort`, only `listenForLoopbackSignIn`
26
+ * and refreshes use it.
27
+ */
28
+ callbackUrl?: string;
29
+ /** Scopes to request, separated by spaces. Default: the scopes the server advertises. */
30
+ scope?: string;
31
+ /** `client_name` for dynamic client registration. Default: the application's `clientName`. */
32
+ clientName?: string;
33
+ /**
34
+ * How to identify without `clientId`. `auto` (default): a Client ID Metadata Document when the
35
+ * authorization server supports them for public clients and the documents list the redirect URI,
36
+ * otherwise dynamic client registration; with `clientName`, always dynamic client registration.
37
+ * `cimd`: always the document, and sign-in fails without support. `dcr`: always dynamic client
38
+ * registration.
39
+ */
40
+ clientRegistration?: "auto" | "dcr" | "cimd";
41
+ /**
42
+ * Authorization server metadata document to use instead of discovery through the server, for servers
43
+ * that advertise a wrong authorization server or none. It is trusted as configured.
44
+ */
45
+ authServerMetadataUrl?: URL;
46
+ }
47
+ /**
48
+ * The Client ID Metadata Documents an application publishes: `<baseUrl>/client.json`, and for each MCP
49
+ * server `<baseUrl>/<callback ID>/client.json`, whose redirect URIs carry the path `<path>/<callback ID>`.
50
+ */
51
+ export interface McpClientMetadataDocuments {
52
+ /** For example `https://pi.dev/oauth`. */
53
+ baseUrl: string;
54
+ /** Whether the documents list `redirectUrl`. Loopback redirect URIs match on any port (RFC 8252). */
55
+ listsRedirectUrl(redirectUrl: string): boolean;
56
+ }
57
+ /** State store of one MCP server, with a lock that keeps other processes from refreshing at the same time. */
58
+ export interface McpOAuthServerStore extends McpOAuthStateStore {
59
+ /**
60
+ * Run `fn` while no other process refreshes the server's tokens. Not reentrant: `startMcpSignIn` and
61
+ * `createMcpAuthProvider` take it themselves, so never call them from `fn`. Throw
62
+ * `McpRefreshLockBusyError` when another process holds the lock for the whole wait: `startMcpSignIn`
63
+ * then goes to the browser step. Any other error fails the sign-in or refresh.
64
+ */
65
+ withRefreshLock<T>(fn: () => Promise<T>): Promise<T>;
66
+ }
67
+ /** The OAuth client of one MCP server: where its state lives, its settings, and the application's identity. */
68
+ export interface McpOAuthClientOptions {
69
+ serverUrl: string;
70
+ store: McpOAuthServerStore;
71
+ settings: McpOAuthSettings;
72
+ /** `client_name` for dynamic client registration unless `settings.clientName` names another: the application. */
73
+ clientName: string;
74
+ /** Omitted: dynamic client registration only. */
75
+ clientMetadataDocuments?: McpClientMetadataDocuments;
76
+ }
77
+ /** An `McpOAuthProvider` for the client, redirecting to `redirectUrl` or the document's variant of it. */
78
+ export declare function createOAuthClientProvider(options: McpOAuthClientOptions, redirectUrl: string, onRedirect?: (url: URL) => void): McpOAuthProvider;
79
+ /**
80
+ * Whether the client may identify with a Client ID Metadata Document instead of registering. `auto`
81
+ * registers when the user asked for a client name or the documents do not list the redirect URI.
82
+ */
83
+ export declare function offersClientMetadataDocument(settings: McpOAuthSettings, documents: McpClientMetadataDocuments | undefined, redirectUrl: string): boolean;
84
+ /** The path of the redirect URI of a server-specific Client ID Metadata Document: `<path>/<callback ID>`. */
85
+ export declare function serverSpecificPath(path: string, serverUrl: string): string;
86
+ /** Redirect URIs a stored client was registered with. */
87
+ export declare function registeredRedirectUrls(client: OAuthClientInformationMixed | undefined): string[];
88
+ /** `fetch` with `OAUTH_REQUEST_TIMEOUT_MS` per request, on top of the request's own signal. */
89
+ export declare function timedFetch(fetch?: McpFetch): McpFetch;
90
+ /** Redirect URI when the settings name none and no client is stored. */
91
+ export declare const DEFAULT_REDIRECT_URL = "http://127.0.0.1/callback";
92
+ /** Where the loopback callback server listens and the redirect URI it serves, from `callbackUrl` and `callbackPort`. */
93
+ export interface LoopbackSettings {
94
+ /** The redirect URI, without the port unless one is configured. */
95
+ url: string;
96
+ /** Address to listen on. */
97
+ host: string;
98
+ /** Host name in the redirect URI. */
99
+ redirectHost: string;
100
+ port: number | undefined;
101
+ path: string;
102
+ /** The exact redirect URI, when the port is fixed. */
103
+ fixedRedirectUrl: string | undefined;
104
+ }
105
+ /** The loopback callback settings of `settings`. */
106
+ export declare function loopbackSettings(settings: McpOAuthSettings): LoopbackSettings;
107
+ //# sourceMappingURL=client-settings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3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reshes use for one MCP server: its settings, how it identifies
3
+ * itself (a pre-registered client, a Client ID Metadata Document, or dynamic client registration), and
4
+ * the `McpOAuthProvider` built from them.
5
+ */
6
+ import { createHash } from "node:crypto";
7
+ import { McpOAuthProvider } from "./provider.js";
8
+ /** An `McpOAuthProvider` for the client, redirecting to `redirectUrl` or the document's variant of it. */
9
+ export function createOAuthClientProvider(options, redirectUrl, onRedirect = () => { }) {
10
+ const { serverUrl, settings, clientMetadataDocuments: documents } = options;
11
+ return new McpOAuthProvider({
12
+ serverUrl,
13
+ redirectUrl,
14
+ clientMetadata: { client_name: settings.clientName ?? options.clientName },
15
+ clientMetadataDocument: offersClientMetadataDocument(settings, documents, redirectUrl)
16
+ ? (metadata) => {
17
+ if (!documents?.listsRedirectUrl(redirectUrl)) {
18
+ throw new Error(`oauth.clientRegistration "cimd" needs Client ID Metadata Documents that list the redirect URI ${redirectUrl}`);
19
+ }
20
+ const document = clientMetadataDocument(serverUrl, redirectUrl, metadata, documents.baseUrl);
21
+ if (!document && settings.clientRegistration === "cimd") {
22
+ throw new Error('The authorization server does not support Client ID Metadata Documents for public clients; remove oauth.clientRegistration "cimd"');
23
+ }
24
+ return document;
25
+ }
26
+ : undefined,
27
+ clientId: settings.clientId,
28
+ clientSecret: settings.clientSecret,
29
+ store: options.store,
30
+ onRedirect,
31
+ });
32
+ }
33
+ /**
34
+ * Whether the client may identify with a Client ID Metadata Document instead of registering. `auto`
35
+ * registers when the user asked for a client name or the documents do not list the redirect URI.
36
+ */
37
+ export function offersClientMetadataDocument(settings, documents, redirectUrl) {
38
+ const registration = settings.clientRegistration ?? "auto";
39
+ if (registration !== "auto")
40
+ return registration === "cimd";
41
+ return !settings.clientId && !settings.clientName && documents?.listsRedirectUrl(redirectUrl) === true;
42
+ }
43
+ /**
44
+ * The Client ID Metadata Document to use, chosen like Codex chooses its own, or `undefined` when the
45
+ * authorization server does not support documents for public clients. Without the `iss` parameter in
46
+ * authorization responses (RFC 9207), the redirect URI and the document are specific to the MCP server,
47
+ * so a response cannot be mixed up with one from another authorization server (RFC 9700 section 4.4.2.2).
48
+ */
49
+ function clientMetadataDocument(serverUrl, redirectUrl, metadata, baseUrl) {
50
+ if (!metadata?.client_id_metadata_document_supported ||
51
+ !metadata.token_endpoint_auth_methods_supported?.includes("none")) {
52
+ return undefined;
53
+ }
54
+ if (metadata.authorization_response_iss_parameter_supported)
55
+ return { url: `${baseUrl}/client.json`, redirectUrl };
56
+ const redirect = new URL(redirectUrl);
57
+ redirect.pathname = serverSpecificPath(redirect.pathname, serverUrl);
58
+ return { url: `${baseUrl}/${callbackId(serverUrl)}/client.json`, redirectUrl: redirect.href };
59
+ }
60
+ /** The path of the redirect URI of a server-specific Client ID Metadata Document: `<path>/<callback ID>`. */
61
+ export function serverSpecificPath(path, serverUrl) {
62
+ return `${path.replace(/\/$/, "")}/${callbackId(serverUrl)}`;
63
+ }
64
+ /** 12 characters identifying an MCP server URL in callback paths, computed like Codex does. */
65
+ function callbackId(serverUrl) {
66
+ const url = new URL(serverUrl);
67
+ url.hash = "";
68
+ return createHash("sha256").update(url.href).digest().subarray(0, 9).toString("base64url");
69
+ }
70
+ /** Redirect URIs a stored client was registered with. */
71
+ export function registeredRedirectUrls(client) {
72
+ return client && "redirect_uris" in client ? client.redirect_uris : [];
73
+ }
74
+ /**
75
+ * Bounds each request to the authorization server, so an unresponsive one cannot hold the refresh lock,
76
+ * delay shutdown, or keep a sign-in waiting for long.
77
+ */
78
+ const OAUTH_REQUEST_TIMEOUT_MS = 15_000;
79
+ /** `fetch` with `OAUTH_REQUEST_TIMEOUT_MS` per request, on top of the request's own signal. */
80
+ export function timedFetch(fetch = globalThis.fetch) {
81
+ return (input, init) => {
82
+ const timeout = AbortSignal.timeout(OAUTH_REQUEST_TIMEOUT_MS);
83
+ return fetch(input, { ...init, signal: init?.signal ? AbortSignal.any([init.signal, timeout]) : timeout });
84
+ };
85
+ }
86
+ const CALLBACK_HOST = "127.0.0.1";
87
+ const CALLBACK_PATH = "/callback";
88
+ /** Redirect URI when the settings name none and no client is stored. */
89
+ export const DEFAULT_REDIRECT_URL = `http://${CALLBACK_HOST}${CALLBACK_PATH}`;
90
+ /** The loopback callback settings of `settings`. */
91
+ export function loopbackSettings(settings) {
92
+ const configured = settings.callbackUrl ?? DEFAULT_REDIRECT_URL;
93
+ const url = new URL(configured);
94
+ const address = url.hostname.replace(/^\[|\]$/g, "");
95
+ const port = url.port ? Number(url.port) : settings.callbackPort;
96
+ let fixedRedirectUrl;
97
+ // A configured URI with a port is sent exactly as written, since servers compare it as a string.
98
+ if (url.port)
99
+ fixedRedirectUrl = settings.callbackUrl;
100
+ else if (port !== undefined) {
101
+ url.port = String(port);
102
+ fixedRedirectUrl = url.href;
103
+ }
104
+ return {
105
+ url: configured,
106
+ // `localhost` is served on 127.0.0.1; browsers fall back to it when ::1 refuses.
107
+ host: address === "localhost" ? CALLBACK_HOST : address,
108
+ redirectHost: address,
109
+ port,
110
+ path: url.pathname,
111
+ fixedRedirectUrl,
112
+ };
113
+ }
114
+ //# sourceMappingURL=client-settings.js.map