@mcp-z/oauth 1.0.0

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 (131) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +71 -0
  3. package/dist/cjs/account-utils.d.cts +107 -0
  4. package/dist/cjs/account-utils.d.ts +107 -0
  5. package/dist/cjs/account-utils.js +481 -0
  6. package/dist/cjs/account-utils.js.map +1 -0
  7. package/dist/cjs/index.d.cts +19 -0
  8. package/dist/cjs/index.d.ts +19 -0
  9. package/dist/cjs/index.js +149 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/dist/cjs/jwt-auth.d.cts +53 -0
  12. package/dist/cjs/jwt-auth.d.ts +53 -0
  13. package/dist/cjs/jwt-auth.js +417 -0
  14. package/dist/cjs/jwt-auth.js.map +1 -0
  15. package/dist/cjs/key-utils.d.cts +131 -0
  16. package/dist/cjs/key-utils.d.ts +131 -0
  17. package/dist/cjs/key-utils.js +421 -0
  18. package/dist/cjs/key-utils.js.map +1 -0
  19. package/dist/cjs/lib/account-server/index.d.cts +45 -0
  20. package/dist/cjs/lib/account-server/index.d.ts +45 -0
  21. package/dist/cjs/lib/account-server/index.js +67 -0
  22. package/dist/cjs/lib/account-server/index.js.map +1 -0
  23. package/dist/cjs/lib/account-server/loopback.d.cts +22 -0
  24. package/dist/cjs/lib/account-server/loopback.d.ts +22 -0
  25. package/dist/cjs/lib/account-server/loopback.js +778 -0
  26. package/dist/cjs/lib/account-server/loopback.js.map +1 -0
  27. package/dist/cjs/lib/account-server/me.d.cts +23 -0
  28. package/dist/cjs/lib/account-server/me.d.ts +23 -0
  29. package/dist/cjs/lib/account-server/me.js +412 -0
  30. package/dist/cjs/lib/account-server/me.js.map +1 -0
  31. package/dist/cjs/lib/account-server/shared-utils.d.cts +6 -0
  32. package/dist/cjs/lib/account-server/shared-utils.d.ts +6 -0
  33. package/dist/cjs/lib/account-server/shared-utils.js +235 -0
  34. package/dist/cjs/lib/account-server/shared-utils.js.map +1 -0
  35. package/dist/cjs/lib/account-server/stateless.d.cts +20 -0
  36. package/dist/cjs/lib/account-server/stateless.d.ts +20 -0
  37. package/dist/cjs/lib/account-server/stateless.js +32 -0
  38. package/dist/cjs/lib/account-server/stateless.js.map +1 -0
  39. package/dist/cjs/lib/account-server/types.d.cts +32 -0
  40. package/dist/cjs/lib/account-server/types.d.ts +32 -0
  41. package/dist/cjs/lib/account-server/types.js +7 -0
  42. package/dist/cjs/lib/account-server/types.js.map +1 -0
  43. package/dist/cjs/lib/dcr-types.d.cts +126 -0
  44. package/dist/cjs/lib/dcr-types.d.ts +126 -0
  45. package/dist/cjs/lib/dcr-types.js +12 -0
  46. package/dist/cjs/lib/dcr-types.js.map +1 -0
  47. package/dist/cjs/lib/rfc-metadata-types.d.cts +46 -0
  48. package/dist/cjs/lib/rfc-metadata-types.d.ts +46 -0
  49. package/dist/cjs/lib/rfc-metadata-types.js +8 -0
  50. package/dist/cjs/lib/rfc-metadata-types.js.map +1 -0
  51. package/dist/cjs/package.json +1 -0
  52. package/dist/cjs/pkce.d.cts +36 -0
  53. package/dist/cjs/pkce.d.ts +36 -0
  54. package/dist/cjs/pkce.js +25 -0
  55. package/dist/cjs/pkce.js.map +1 -0
  56. package/dist/cjs/sanitizer.d.cts +37 -0
  57. package/dist/cjs/sanitizer.d.ts +37 -0
  58. package/dist/cjs/sanitizer.js +407 -0
  59. package/dist/cjs/sanitizer.js.map +1 -0
  60. package/dist/cjs/schemas/index.d.cts +36 -0
  61. package/dist/cjs/schemas/index.d.ts +36 -0
  62. package/dist/cjs/schemas/index.js +28 -0
  63. package/dist/cjs/schemas/index.js.map +1 -0
  64. package/dist/cjs/session-auth.d.cts +79 -0
  65. package/dist/cjs/session-auth.d.ts +79 -0
  66. package/dist/cjs/session-auth.js +354 -0
  67. package/dist/cjs/session-auth.js.map +1 -0
  68. package/dist/cjs/templates.d.cts +18 -0
  69. package/dist/cjs/templates.d.ts +18 -0
  70. package/dist/cjs/templates.js +38 -0
  71. package/dist/cjs/templates.js.map +1 -0
  72. package/dist/cjs/types.d.cts +343 -0
  73. package/dist/cjs/types.d.ts +343 -0
  74. package/dist/cjs/types.js +210 -0
  75. package/dist/cjs/types.js.map +1 -0
  76. package/dist/esm/account-utils.d.ts +107 -0
  77. package/dist/esm/account-utils.js +179 -0
  78. package/dist/esm/account-utils.js.map +1 -0
  79. package/dist/esm/index.d.ts +19 -0
  80. package/dist/esm/index.js +23 -0
  81. package/dist/esm/index.js.map +1 -0
  82. package/dist/esm/jwt-auth.d.ts +53 -0
  83. package/dist/esm/jwt-auth.js +164 -0
  84. package/dist/esm/jwt-auth.js.map +1 -0
  85. package/dist/esm/key-utils.d.ts +131 -0
  86. package/dist/esm/key-utils.js +143 -0
  87. package/dist/esm/key-utils.js.map +1 -0
  88. package/dist/esm/lib/account-server/index.d.ts +45 -0
  89. package/dist/esm/lib/account-server/index.js +41 -0
  90. package/dist/esm/lib/account-server/index.js.map +1 -0
  91. package/dist/esm/lib/account-server/loopback.d.ts +22 -0
  92. package/dist/esm/lib/account-server/loopback.js +372 -0
  93. package/dist/esm/lib/account-server/loopback.js.map +1 -0
  94. package/dist/esm/lib/account-server/me.d.ts +23 -0
  95. package/dist/esm/lib/account-server/me.js +170 -0
  96. package/dist/esm/lib/account-server/me.js.map +1 -0
  97. package/dist/esm/lib/account-server/shared-utils.d.ts +6 -0
  98. package/dist/esm/lib/account-server/shared-utils.js +24 -0
  99. package/dist/esm/lib/account-server/shared-utils.js.map +1 -0
  100. package/dist/esm/lib/account-server/stateless.d.ts +20 -0
  101. package/dist/esm/lib/account-server/stateless.js +25 -0
  102. package/dist/esm/lib/account-server/stateless.js.map +1 -0
  103. package/dist/esm/lib/account-server/types.d.ts +32 -0
  104. package/dist/esm/lib/account-server/types.js +6 -0
  105. package/dist/esm/lib/account-server/types.js.map +1 -0
  106. package/dist/esm/lib/dcr-types.d.ts +126 -0
  107. package/dist/esm/lib/dcr-types.js +13 -0
  108. package/dist/esm/lib/dcr-types.js.map +1 -0
  109. package/dist/esm/lib/rfc-metadata-types.d.ts +46 -0
  110. package/dist/esm/lib/rfc-metadata-types.js +7 -0
  111. package/dist/esm/lib/rfc-metadata-types.js.map +1 -0
  112. package/dist/esm/package.json +1 -0
  113. package/dist/esm/pkce.d.ts +36 -0
  114. package/dist/esm/pkce.js +33 -0
  115. package/dist/esm/pkce.js.map +1 -0
  116. package/dist/esm/sanitizer.d.ts +37 -0
  117. package/dist/esm/sanitizer.js +256 -0
  118. package/dist/esm/sanitizer.js.map +1 -0
  119. package/dist/esm/schemas/index.d.ts +36 -0
  120. package/dist/esm/schemas/index.js +19 -0
  121. package/dist/esm/schemas/index.js.map +1 -0
  122. package/dist/esm/session-auth.d.ts +79 -0
  123. package/dist/esm/session-auth.js +141 -0
  124. package/dist/esm/session-auth.js.map +1 -0
  125. package/dist/esm/templates.d.ts +18 -0
  126. package/dist/esm/templates.js +132 -0
  127. package/dist/esm/templates.js.map +1 -0
  128. package/dist/esm/types.d.ts +343 -0
  129. package/dist/esm/types.js +34 -0
  130. package/dist/esm/types.js.map +1 -0
  131. package/package.json +82 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Kevin Malakoff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # @mcp-z/oauth
2
+
3
+ Docs: https://mcp-z.github.io/oauth
4
+ Multi-account OAuth orchestration and token storage for MCP servers.
5
+
6
+ ## Common uses
7
+
8
+ - Add consistent account tools to MCP servers
9
+ - Store OAuth tokens with a shared config and storage backend
10
+ - Reuse the same account lifecycle across Google and Microsoft providers
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install @mcp-z/oauth
16
+ ```
17
+
18
+ Optional storage backends:
19
+
20
+ ```bash
21
+ npm install keyv-duckdb
22
+ npm install keyv-file
23
+ ```
24
+
25
+ ## Initialize token storage
26
+
27
+ ```bash
28
+ npx @mcp-z/oauth init
29
+ ```
30
+
31
+ This creates a `.tokens/` directory and a default config file for token storage.
32
+
33
+ ## Account tools and modes
34
+
35
+ Use `AccountServer` to add account tools to your MCP server.
36
+
37
+ ### Loopback mode (multi-account)
38
+
39
+ When using loopback OAuth, these tools are added:
40
+
41
+ - `account-me`
42
+ - `account-switch`
43
+ - `account-remove`
44
+ - `account-list`
45
+
46
+ ### Stateless mode (DCR/bearer)
47
+
48
+ When using stateless auth (DCR/bearer tokens), only this tool is available:
49
+
50
+ - `account-me`
51
+
52
+ ## Example
53
+
54
+ ```ts
55
+ import { AccountServer } from '@mcp-z/oauth';
56
+
57
+ const { tools, prompts } = AccountServer.createLoopback({
58
+ service: 'gmail',
59
+ store: tokenStore,
60
+ logger,
61
+ auth: authProvider
62
+ });
63
+ ```
64
+
65
+ ## Logging helper
66
+
67
+ Use `sanitizeForLoggingFormatter` to avoid leaking secrets in logs.
68
+
69
+ ## Requirements
70
+
71
+ - Node.js >= 22
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Account management utilities for OAuth token storage
3
+ *
4
+ * Provides account lifecycle operations (add, remove, activate) and account data
5
+ * access (tokens, metadata). Uses named parameters consistent with key-utils.ts.
6
+ */
7
+ import type { Keyv } from 'keyv';
8
+ import { type AccountKeyParams, type ServiceKeyParams } from './key-utils.js';
9
+ import type { AccountInfo } from './types.js';
10
+ /**
11
+ * Add account to linked accounts list and set as active if first account.
12
+ *
13
+ * @param store - Keyv storage instance
14
+ * @param params - Account identification (service, accountId)
15
+ *
16
+ * @example
17
+ * await addAccount(tokenStore, {
18
+ * service: 'gmail',
19
+ * accountId: 'alice@gmail.com'
20
+ * });
21
+ */
22
+ export declare function addAccount(store: Keyv, params: AccountKeyParams): Promise<void>;
23
+ /**
24
+ * Remove account: delete token, metadata, update linked list, and active account.
25
+ *
26
+ * @param store - Keyv storage instance
27
+ * @param params - Account identification (service, accountId)
28
+ *
29
+ * @example
30
+ * await removeAccount(tokenStore, {
31
+ * service: 'gmail',
32
+ * accountId: 'alice@gmail.com'
33
+ * });
34
+ */
35
+ export declare function removeAccount(store: Keyv, params: AccountKeyParams): Promise<void>;
36
+ /**
37
+ * Get active account ID for a service.
38
+ *
39
+ * Key: {service}:active
40
+ *
41
+ * @param store - Keyv storage instance
42
+ * @param params - Service identification (service)
43
+ * @returns Active account ID or undefined if none set
44
+ */
45
+ export declare function getActiveAccount(store: Keyv, params: ServiceKeyParams): Promise<string | undefined>;
46
+ /**
47
+ * Set active account ID for a service.
48
+ * Pass null as accountId to deactivate (clear active account).
49
+ *
50
+ * Key: {service}:active
51
+ *
52
+ * @param store - Keyv storage instance
53
+ * @param params - Account identification (service, accountId). Pass accountId: null to deactivate.
54
+ */
55
+ export declare function setActiveAccount(store: Keyv, params: AccountKeyParams | (ServiceKeyParams & {
56
+ accountId: null;
57
+ })): Promise<void>;
58
+ /**
59
+ * Get list of linked account IDs for a service.
60
+ *
61
+ * Key: {service}:linked
62
+ *
63
+ * @param store - Keyv storage instance
64
+ * @param params - Service identification (service)
65
+ * @returns Array of account IDs (empty array if none)
66
+ */
67
+ export declare function getLinkedAccounts(store: Keyv, params: ServiceKeyParams): Promise<string[]>;
68
+ /**
69
+ * Get account metadata (alias, lastUsed, etc).
70
+ *
71
+ * Key: {accountId}:{service}:metadata
72
+ *
73
+ * @param store - Keyv storage instance
74
+ * @param params - Account identification (accountId, service)
75
+ * @returns Account info or undefined if not found
76
+ */
77
+ export declare function getAccountInfo(store: Keyv, params: AccountKeyParams): Promise<AccountInfo | undefined>;
78
+ /**
79
+ * Set account metadata (alias, lastUsed, etc).
80
+ *
81
+ * Key: {accountId}:{service}:metadata
82
+ *
83
+ * @param store - Keyv storage instance
84
+ * @param params - Account identification (accountId, service)
85
+ * @param info - Account metadata to store
86
+ */
87
+ export declare function setAccountInfo(store: Keyv, params: AccountKeyParams, info: AccountInfo): Promise<void>;
88
+ /**
89
+ * Get OAuth token for an account.
90
+ *
91
+ * Key: {accountId}:{service}:token
92
+ *
93
+ * @param store - Keyv storage instance
94
+ * @param params - Account identification (accountId, service)
95
+ * @returns Token or undefined if not found
96
+ */
97
+ export declare function getToken<T>(store: Keyv, params: AccountKeyParams): Promise<T | undefined>;
98
+ /**
99
+ * Set OAuth token for an account.
100
+ *
101
+ * Key: {accountId}:{service}:token
102
+ *
103
+ * @param store - Keyv storage instance
104
+ * @param params - Account identification (accountId, service)
105
+ * @param token - OAuth token data to store
106
+ */
107
+ export declare function setToken<T>(store: Keyv, params: AccountKeyParams, token: T): Promise<void>;
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Account management utilities for OAuth token storage
3
+ *
4
+ * Provides account lifecycle operations (add, remove, activate) and account data
5
+ * access (tokens, metadata). Uses named parameters consistent with key-utils.ts.
6
+ */
7
+ import type { Keyv } from 'keyv';
8
+ import { type AccountKeyParams, type ServiceKeyParams } from './key-utils.js';
9
+ import type { AccountInfo } from './types.js';
10
+ /**
11
+ * Add account to linked accounts list and set as active if first account.
12
+ *
13
+ * @param store - Keyv storage instance
14
+ * @param params - Account identification (service, accountId)
15
+ *
16
+ * @example
17
+ * await addAccount(tokenStore, {
18
+ * service: 'gmail',
19
+ * accountId: 'alice@gmail.com'
20
+ * });
21
+ */
22
+ export declare function addAccount(store: Keyv, params: AccountKeyParams): Promise<void>;
23
+ /**
24
+ * Remove account: delete token, metadata, update linked list, and active account.
25
+ *
26
+ * @param store - Keyv storage instance
27
+ * @param params - Account identification (service, accountId)
28
+ *
29
+ * @example
30
+ * await removeAccount(tokenStore, {
31
+ * service: 'gmail',
32
+ * accountId: 'alice@gmail.com'
33
+ * });
34
+ */
35
+ export declare function removeAccount(store: Keyv, params: AccountKeyParams): Promise<void>;
36
+ /**
37
+ * Get active account ID for a service.
38
+ *
39
+ * Key: {service}:active
40
+ *
41
+ * @param store - Keyv storage instance
42
+ * @param params - Service identification (service)
43
+ * @returns Active account ID or undefined if none set
44
+ */
45
+ export declare function getActiveAccount(store: Keyv, params: ServiceKeyParams): Promise<string | undefined>;
46
+ /**
47
+ * Set active account ID for a service.
48
+ * Pass null as accountId to deactivate (clear active account).
49
+ *
50
+ * Key: {service}:active
51
+ *
52
+ * @param store - Keyv storage instance
53
+ * @param params - Account identification (service, accountId). Pass accountId: null to deactivate.
54
+ */
55
+ export declare function setActiveAccount(store: Keyv, params: AccountKeyParams | (ServiceKeyParams & {
56
+ accountId: null;
57
+ })): Promise<void>;
58
+ /**
59
+ * Get list of linked account IDs for a service.
60
+ *
61
+ * Key: {service}:linked
62
+ *
63
+ * @param store - Keyv storage instance
64
+ * @param params - Service identification (service)
65
+ * @returns Array of account IDs (empty array if none)
66
+ */
67
+ export declare function getLinkedAccounts(store: Keyv, params: ServiceKeyParams): Promise<string[]>;
68
+ /**
69
+ * Get account metadata (alias, lastUsed, etc).
70
+ *
71
+ * Key: {accountId}:{service}:metadata
72
+ *
73
+ * @param store - Keyv storage instance
74
+ * @param params - Account identification (accountId, service)
75
+ * @returns Account info or undefined if not found
76
+ */
77
+ export declare function getAccountInfo(store: Keyv, params: AccountKeyParams): Promise<AccountInfo | undefined>;
78
+ /**
79
+ * Set account metadata (alias, lastUsed, etc).
80
+ *
81
+ * Key: {accountId}:{service}:metadata
82
+ *
83
+ * @param store - Keyv storage instance
84
+ * @param params - Account identification (accountId, service)
85
+ * @param info - Account metadata to store
86
+ */
87
+ export declare function setAccountInfo(store: Keyv, params: AccountKeyParams, info: AccountInfo): Promise<void>;
88
+ /**
89
+ * Get OAuth token for an account.
90
+ *
91
+ * Key: {accountId}:{service}:token
92
+ *
93
+ * @param store - Keyv storage instance
94
+ * @param params - Account identification (accountId, service)
95
+ * @returns Token or undefined if not found
96
+ */
97
+ export declare function getToken<T>(store: Keyv, params: AccountKeyParams): Promise<T | undefined>;
98
+ /**
99
+ * Set OAuth token for an account.
100
+ *
101
+ * Key: {accountId}:{service}:token
102
+ *
103
+ * @param store - Keyv storage instance
104
+ * @param params - Account identification (accountId, service)
105
+ * @param token - OAuth token data to store
106
+ */
107
+ export declare function setToken<T>(store: Keyv, params: AccountKeyParams, token: T): Promise<void>;