@casys/mcp-server 0.26.1 → 0.27.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 (299) hide show
  1. package/CHANGELOG.md +1779 -0
  2. package/LICENSE +21 -0
  3. package/README.md +15 -4
  4. package/esm/_dnt.polyfills.d.ts +12 -0
  5. package/esm/_dnt.polyfills.d.ts.map +1 -0
  6. package/esm/_dnt.polyfills.js +15 -0
  7. package/esm/_dnt.shims.d.ts +2 -0
  8. package/esm/_dnt.shims.d.ts.map +1 -0
  9. package/esm/_dnt.shims.js +57 -0
  10. package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.d.ts +33 -0
  11. package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.d.ts.map +1 -0
  12. package/esm/deps/jsr.io/@std/yaml/1.2.0/_chars.js +48 -0
  13. package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.d.ts +106 -0
  14. package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.d.ts.map +1 -0
  15. package/esm/deps/jsr.io/@std/yaml/1.2.0/_dumper_state.js +636 -0
  16. package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.d.ts +67 -0
  17. package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.d.ts.map +1 -0
  18. package/esm/deps/jsr.io/@std/yaml/1.2.0/_loader_state.js +1393 -0
  19. package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.d.ts +44 -0
  20. package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.d.ts.map +1 -0
  21. package/esm/deps/jsr.io/@std/yaml/1.2.0/_schema.js +117 -0
  22. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.d.ts +3 -0
  23. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.d.ts.map +1 -0
  24. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/binary.js +103 -0
  25. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.d.ts +3 -0
  26. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.d.ts.map +1 -0
  27. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/bool.js +32 -0
  28. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.d.ts +3 -0
  29. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.d.ts.map +1 -0
  30. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/float.js +96 -0
  31. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.d.ts +3 -0
  32. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.d.ts.map +1 -0
  33. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/int.js +159 -0
  34. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.d.ts +3 -0
  35. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.d.ts.map +1 -0
  36. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/map.js +14 -0
  37. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.d.ts +3 -0
  38. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.d.ts.map +1 -0
  39. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/merge.js +10 -0
  40. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.d.ts +3 -0
  41. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.d.ts.map +1 -0
  42. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/nil.js +22 -0
  43. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.d.ts +3 -0
  44. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.d.ts.map +1 -0
  45. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/omap.js +29 -0
  46. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.d.ts +3 -0
  47. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.d.ts.map +1 -0
  48. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/pairs.js +19 -0
  49. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.d.ts +3 -0
  50. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.d.ts.map +1 -0
  51. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/regexp.js +30 -0
  52. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.d.ts +3 -0
  53. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.d.ts.map +1 -0
  54. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/seq.js +10 -0
  55. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.d.ts +3 -0
  56. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.d.ts.map +1 -0
  57. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/set.js +14 -0
  58. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.d.ts +3 -0
  59. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.d.ts.map +1 -0
  60. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/str.js +9 -0
  61. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.d.ts +3 -0
  62. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.d.ts.map +1 -0
  63. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/timestamp.js +81 -0
  64. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.d.ts +3 -0
  65. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.d.ts.map +1 -0
  66. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type/undefined.js +20 -0
  67. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.d.ts +32 -0
  68. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.d.ts.map +1 -0
  69. package/esm/deps/jsr.io/@std/yaml/1.2.0/_type.js +6 -0
  70. package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.d.ts +4 -0
  71. package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.d.ts.map +1 -0
  72. package/esm/deps/jsr.io/@std/yaml/1.2.0/_utils.js +13 -0
  73. package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.d.ts +51 -0
  74. package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.d.ts.map +1 -0
  75. package/esm/deps/jsr.io/@std/yaml/1.2.0/mod.js +53 -0
  76. package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.d.ts +79 -0
  77. package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.d.ts.map +1 -0
  78. package/esm/deps/jsr.io/@std/yaml/1.2.0/parse.js +91 -0
  79. package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.d.ts +100 -0
  80. package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.d.ts.map +1 -0
  81. package/esm/deps/jsr.io/@std/yaml/1.2.0/stringify.js +33 -0
  82. package/esm/deps/jsr.io/@std/yaml/1.2.0/types.d.ts +111 -0
  83. package/esm/deps/jsr.io/@std/yaml/1.2.0/types.d.ts.map +1 -0
  84. package/esm/deps/jsr.io/@std/yaml/1.2.0/types.js +104 -0
  85. package/esm/mod.d.ts +102 -0
  86. package/esm/mod.d.ts.map +1 -0
  87. package/esm/mod.js +103 -0
  88. package/esm/package.json +3 -0
  89. package/esm/src/auth/as-metadata-proxy.d.ts +112 -0
  90. package/esm/src/auth/as-metadata-proxy.d.ts.map +1 -0
  91. package/esm/src/auth/as-metadata-proxy.js +171 -0
  92. package/esm/src/auth/config.d.ts +117 -0
  93. package/esm/src/auth/config.d.ts.map +1 -0
  94. package/esm/src/auth/config.js +219 -0
  95. package/esm/src/auth/jwt-provider.d.ts +157 -0
  96. package/esm/src/auth/jwt-provider.d.ts.map +1 -0
  97. package/esm/src/auth/jwt-provider.js +187 -0
  98. package/esm/src/auth/middleware.d.ts +61 -0
  99. package/esm/src/auth/middleware.d.ts.map +1 -0
  100. package/esm/src/auth/middleware.js +209 -0
  101. package/esm/src/auth/mod.d.ts +23 -0
  102. package/esm/src/auth/mod.d.ts.map +1 -0
  103. package/esm/src/auth/mod.js +27 -0
  104. package/esm/src/auth/multi-tenant-middleware.d.ts +131 -0
  105. package/esm/src/auth/multi-tenant-middleware.d.ts.map +1 -0
  106. package/esm/src/auth/multi-tenant-middleware.js +128 -0
  107. package/esm/src/auth/presets.d.ts +231 -0
  108. package/esm/src/auth/presets.d.ts.map +1 -0
  109. package/esm/src/auth/presets.js +223 -0
  110. package/{src/auth/provider.ts → esm/src/auth/provider.d.ts} +13 -15
  111. package/esm/src/auth/provider.d.ts.map +1 -0
  112. package/esm/src/auth/provider.js +34 -0
  113. package/esm/src/auth/scope-middleware.d.ts +30 -0
  114. package/esm/src/auth/scope-middleware.d.ts.map +1 -0
  115. package/esm/src/auth/scope-middleware.js +48 -0
  116. package/esm/src/auth/static-token-provider.d.ts +155 -0
  117. package/esm/src/auth/static-token-provider.d.ts.map +1 -0
  118. package/esm/src/auth/static-token-provider.js +256 -0
  119. package/esm/src/auth/types.d.ts +151 -0
  120. package/esm/src/auth/types.d.ts.map +1 -0
  121. package/esm/src/auth/types.js +64 -0
  122. package/esm/src/client-auth/callback-server.d.ts +34 -0
  123. package/esm/src/client-auth/callback-server.d.ts.map +1 -0
  124. package/esm/src/client-auth/callback-server.js +141 -0
  125. package/esm/src/client-auth/client-id-metadata.d.ts +29 -0
  126. package/esm/src/client-auth/client-id-metadata.d.ts.map +1 -0
  127. package/esm/src/client-auth/client-id-metadata.js +149 -0
  128. package/esm/src/client-auth/connect.d.ts +32 -0
  129. package/esm/src/client-auth/connect.d.ts.map +1 -0
  130. package/esm/src/client-auth/connect.js +47 -0
  131. package/esm/src/client-auth/mod.d.ts +19 -0
  132. package/esm/src/client-auth/mod.d.ts.map +1 -0
  133. package/{src/client-auth/mod.ts → esm/src/client-auth/mod.js} +1 -26
  134. package/esm/src/client-auth/provider.d.ts +78 -0
  135. package/esm/src/client-auth/provider.d.ts.map +1 -0
  136. package/esm/src/client-auth/provider.js +246 -0
  137. package/esm/src/client-auth/token-store/file-store.d.ts +20 -0
  138. package/esm/src/client-auth/token-store/file-store.d.ts.map +1 -0
  139. package/esm/src/client-auth/token-store/file-store.js +62 -0
  140. package/esm/src/client-auth/token-store/memory-store.d.ts +14 -0
  141. package/esm/src/client-auth/token-store/memory-store.d.ts.map +1 -0
  142. package/esm/src/client-auth/token-store/memory-store.js +25 -0
  143. package/esm/src/client-auth/types.d.ts +89 -0
  144. package/esm/src/client-auth/types.d.ts.map +1 -0
  145. package/esm/src/client-auth/types.js +10 -0
  146. package/esm/src/concurrency/rate-limiter.d.ts +87 -0
  147. package/esm/src/concurrency/rate-limiter.d.ts.map +1 -0
  148. package/esm/src/concurrency/rate-limiter.js +165 -0
  149. package/esm/src/concurrency/request-queue.d.ts +71 -0
  150. package/esm/src/concurrency/request-queue.d.ts.map +1 -0
  151. package/esm/src/concurrency/request-queue.js +128 -0
  152. package/esm/src/http/body.d.ts +20 -0
  153. package/esm/src/http/body.d.ts.map +1 -0
  154. package/esm/src/http/body.js +63 -0
  155. package/esm/src/http/request-guards.d.ts +54 -0
  156. package/esm/src/http/request-guards.d.ts.map +1 -0
  157. package/esm/src/http/request-guards.js +114 -0
  158. package/esm/src/http/wire.d.ts +26 -0
  159. package/esm/src/http/wire.d.ts.map +1 -0
  160. package/{src/http/wire.ts → esm/src/http/wire.js} +13 -19
  161. package/esm/src/inspector/launcher.d.ts +48 -0
  162. package/esm/src/inspector/launcher.d.ts.map +1 -0
  163. package/esm/src/inspector/launcher.js +100 -0
  164. package/esm/src/mcp-app.d.ts +728 -0
  165. package/esm/src/mcp-app.d.ts.map +1 -0
  166. package/esm/src/mcp-app.js +3530 -0
  167. package/{src/middleware/backpressure.ts → esm/src/middleware/backpressure.d.ts} +2 -14
  168. package/esm/src/middleware/backpressure.d.ts.map +1 -0
  169. package/esm/src/middleware/backpressure.js +31 -0
  170. package/esm/src/middleware/mod.d.ts +11 -0
  171. package/esm/src/middleware/mod.d.ts.map +1 -0
  172. package/{src/middleware/mod.ts → esm/src/middleware/mod.js} +0 -10
  173. package/{src/middleware/rate-limit.ts → esm/src/middleware/rate-limit.d.ts} +2 -25
  174. package/esm/src/middleware/rate-limit.d.ts.map +1 -0
  175. package/esm/src/middleware/rate-limit.js +33 -0
  176. package/esm/src/middleware/runner.d.ts +32 -0
  177. package/esm/src/middleware/runner.d.ts.map +1 -0
  178. package/esm/src/middleware/runner.js +49 -0
  179. package/esm/src/middleware/types.d.ts +76 -0
  180. package/esm/src/middleware/types.d.ts.map +1 -0
  181. package/esm/src/middleware/types.js +9 -0
  182. package/esm/src/middleware/validation.d.ts +24 -0
  183. package/esm/src/middleware/validation.d.ts.map +1 -0
  184. package/esm/src/middleware/validation.js +30 -0
  185. package/esm/src/mrtr/capability-check.d.ts +75 -0
  186. package/esm/src/mrtr/capability-check.d.ts.map +1 -0
  187. package/esm/src/mrtr/capability-check.js +233 -0
  188. package/esm/src/mrtr/mod.d.ts +18 -0
  189. package/esm/src/mrtr/mod.d.ts.map +1 -0
  190. package/esm/src/mrtr/mod.js +12 -0
  191. package/esm/src/mrtr/replay-store.d.ts +50 -0
  192. package/esm/src/mrtr/replay-store.d.ts.map +1 -0
  193. package/esm/src/mrtr/replay-store.js +61 -0
  194. package/esm/src/mrtr/request-state.d.ts +181 -0
  195. package/esm/src/mrtr/request-state.d.ts.map +1 -0
  196. package/esm/src/mrtr/request-state.js +313 -0
  197. package/esm/src/mrtr/retry-guard.d.ts +61 -0
  198. package/esm/src/mrtr/retry-guard.d.ts.map +1 -0
  199. package/esm/src/mrtr/retry-guard.js +53 -0
  200. package/esm/src/mrtr/types.d.ts +99 -0
  201. package/esm/src/mrtr/types.d.ts.map +1 -0
  202. package/esm/src/mrtr/types.js +14 -0
  203. package/esm/src/observability/metrics.d.ts +104 -0
  204. package/esm/src/observability/metrics.d.ts.map +1 -0
  205. package/esm/src/observability/metrics.js +207 -0
  206. package/{src/observability/mod.ts → esm/src/observability/mod.d.ts} +2 -11
  207. package/esm/src/observability/mod.d.ts.map +1 -0
  208. package/esm/src/observability/mod.js +11 -0
  209. package/esm/src/observability/otel.d.ts +73 -0
  210. package/esm/src/observability/otel.d.ts.map +1 -0
  211. package/esm/src/observability/otel.js +260 -0
  212. package/esm/src/runtime/runtime.d.ts +10 -0
  213. package/esm/src/runtime/runtime.d.ts.map +1 -0
  214. package/esm/src/runtime/runtime.deno.d.ts +50 -0
  215. package/esm/src/runtime/runtime.deno.d.ts.map +1 -0
  216. package/esm/src/runtime/runtime.deno.js +120 -0
  217. package/{src/runtime/runtime.ts → esm/src/runtime/runtime.js} +5 -14
  218. package/esm/src/runtime/runtime.node.d.ts +51 -0
  219. package/esm/src/runtime/runtime.node.d.ts.map +1 -0
  220. package/esm/src/runtime/runtime.node.js +268 -0
  221. package/{src/runtime/types.ts → esm/src/runtime/types.d.ts} +28 -51
  222. package/esm/src/runtime/types.d.ts.map +1 -0
  223. package/esm/src/runtime/types.js +12 -0
  224. package/esm/src/security/channel-hmac.d.ts +43 -0
  225. package/esm/src/security/channel-hmac.d.ts.map +1 -0
  226. package/{src/security/channel-hmac.ts → esm/src/security/channel-hmac.js} +12 -17
  227. package/esm/src/security/csp.d.ts +50 -0
  228. package/esm/src/security/csp.d.ts.map +1 -0
  229. package/esm/src/security/csp.js +63 -0
  230. package/esm/src/security/message-signer.d.ts +88 -0
  231. package/esm/src/security/message-signer.d.ts.map +1 -0
  232. package/esm/src/security/message-signer.js +166 -0
  233. package/esm/src/subscriptions/subscription-registry.d.ts +256 -0
  234. package/esm/src/subscriptions/subscription-registry.d.ts.map +1 -0
  235. package/esm/src/subscriptions/subscription-registry.js +406 -0
  236. package/{src/tasks/mod.ts → esm/src/tasks/mod.d.ts} +3 -23
  237. package/esm/src/tasks/mod.d.ts.map +1 -0
  238. package/esm/src/tasks/mod.js +11 -0
  239. package/esm/src/tasks/task-store.d.ts +342 -0
  240. package/esm/src/tasks/task-store.d.ts.map +1 -0
  241. package/esm/src/tasks/task-store.js +649 -0
  242. package/esm/src/transport/request-headers.d.ts +145 -0
  243. package/esm/src/transport/request-headers.d.ts.map +1 -0
  244. package/esm/src/transport/request-headers.js +525 -0
  245. package/esm/src/types.d.ts +776 -0
  246. package/esm/src/types.d.ts.map +1 -0
  247. package/esm/src/types.js +92 -0
  248. package/esm/src/ui/viewer-utils.d.ts +45 -0
  249. package/esm/src/ui/viewer-utils.d.ts.map +1 -0
  250. package/esm/src/ui/viewer-utils.js +97 -0
  251. package/esm/src/validation/schema-validator.d.ts +115 -0
  252. package/esm/src/validation/schema-validator.d.ts.map +1 -0
  253. package/esm/src/validation/schema-validator.js +249 -0
  254. package/package.json +35 -24
  255. package/mod.ts +0 -302
  256. package/src/auth/as-metadata-proxy.ts +0 -288
  257. package/src/auth/config.ts +0 -368
  258. package/src/auth/jwt-provider.ts +0 -327
  259. package/src/auth/middleware.ts +0 -257
  260. package/src/auth/mod.ts +0 -89
  261. package/src/auth/multi-tenant-middleware.ts +0 -236
  262. package/src/auth/presets.ts +0 -351
  263. package/src/auth/scope-middleware.ts +0 -59
  264. package/src/auth/static-token-provider.ts +0 -197
  265. package/src/auth/types.ts +0 -207
  266. package/src/client-auth/callback-server.ts +0 -166
  267. package/src/client-auth/client-id-metadata.ts +0 -260
  268. package/src/client-auth/connect.ts +0 -72
  269. package/src/client-auth/provider.ts +0 -300
  270. package/src/client-auth/token-store/file-store.ts +0 -73
  271. package/src/client-auth/token-store/memory-store.ts +0 -32
  272. package/src/client-auth/types.ts +0 -95
  273. package/src/concurrency/rate-limiter.ts +0 -190
  274. package/src/concurrency/request-queue.ts +0 -140
  275. package/src/http/body.ts +0 -72
  276. package/src/http/request-guards.ts +0 -137
  277. package/src/inspector/launcher.ts +0 -131
  278. package/src/mcp-app.ts +0 -4595
  279. package/src/middleware/runner.ts +0 -63
  280. package/src/middleware/types.ts +0 -104
  281. package/src/middleware/validation.ts +0 -28
  282. package/src/mrtr/capability-check.ts +0 -303
  283. package/src/mrtr/mod.ts +0 -54
  284. package/src/mrtr/replay-store.ts +0 -103
  285. package/src/mrtr/request-state.ts +0 -435
  286. package/src/mrtr/retry-guard.ts +0 -110
  287. package/src/mrtr/types.ts +0 -107
  288. package/src/observability/metrics.ts +0 -336
  289. package/src/observability/otel.ts +0 -330
  290. package/src/runtime/runtime.deno.ts +0 -149
  291. package/src/runtime/runtime.node.ts +0 -324
  292. package/src/security/csp.ts +0 -89
  293. package/src/security/message-signer.ts +0 -223
  294. package/src/subscriptions/subscription-registry.ts +0 -558
  295. package/src/tasks/task-store.ts +0 -1014
  296. package/src/transport/request-headers.ts +0 -661
  297. package/src/types.ts +0 -908
  298. package/src/ui/viewer-utils.ts +0 -117
  299. package/src/validation/schema-validator.ts +0 -307
@@ -1,300 +0,0 @@
1
- /**
2
- * OAuth client provider implementation.
3
- *
4
- * Implements OAuthClientProvider from the MCP SDK for authenticating
5
- * against OAuth-protected MCP servers. Handles token storage,
6
- * PKCE flow, and browser-based authorization.
7
- *
8
- * @module lib/server/client-auth/provider
9
- */
10
-
11
- import type {
12
- OAuthClientInformationMixed,
13
- OAuthClientMetadata,
14
- OAuthTokens,
15
- } from "@modelcontextprotocol/sdk/shared/auth.js";
16
- import type {
17
- OAuthClientProvider,
18
- OAuthDiscoveryState,
19
- } from "@modelcontextprotocol/sdk/client/auth.js";
20
- import type { OAuthClientConfig } from "./types.js";
21
- import {
22
- CimdConfigError,
23
- isCimdConfig,
24
- resolveClientMode,
25
- validateCimdClientConfig,
26
- } from "./client-id-metadata.js";
27
-
28
- /**
29
- * Normalise an authorization server identifier before comparing it.
30
- *
31
- * `https://as.example.com` and `https://as.example.com/` name the same server,
32
- * and so do `HTTPS://AS.Example.com` and an explicit `:443`. Comparing raw
33
- * strings would read those as a server change and wipe valid credentials on
34
- * every call — a self-inflicted login loop. Falls back to the raw string if it
35
- * does not parse, which then simply never matches.
36
- */
37
- function canonicalIssuer(value: string): string {
38
- try {
39
- return new URL(value).href;
40
- } catch {
41
- return value;
42
- }
43
- }
44
-
45
- /**
46
- * An authorization server URL, safe to put in a log line.
47
- *
48
- * A URL may carry `userinfo` credentials, and a query string may carry a token
49
- * or an internal identifier. Origin and path are enough to tell an operator
50
- * which server is involved; the rest is only a way for secrets to reach a log
51
- * aggregator.
52
- */
53
- function redactUrl(value: string): string {
54
- try {
55
- const url = new URL(value);
56
- return `${url.origin}${url.pathname}`;
57
- } catch {
58
- return "<unparseable url>";
59
- }
60
- }
61
-
62
- export class OAuthClientProviderImpl implements OAuthClientProvider {
63
- private serverUrl: string;
64
- private config: OAuthClientConfig;
65
- private _codeVerifier = "";
66
- private _clientInfo: OAuthClientInformationMixed | undefined;
67
- private _redirectUrl: string | undefined;
68
- /**
69
- * Issuer the SDK resolved for the request currently being authorized
70
- * (SEP-2352). Set by {@link saveDiscoveryState}, which `auth()` calls after
71
- * discovery and before it asks for tokens.
72
- */
73
- private _currentIssuer: string | undefined;
74
-
75
- constructor(serverUrl: string, config: OAuthClientConfig) {
76
- const mode = resolveClientMode(config);
77
- if (mode === "client_id_metadata") {
78
- validateCimdClientConfig(config);
79
- }
80
- this.serverUrl = serverUrl;
81
- this.config = config;
82
- }
83
-
84
- get redirectUrl(): string | URL {
85
- if (isCimdConfig(this.config)) {
86
- return this.config.clientRegistration.redirectUri;
87
- }
88
- return this._redirectUrl ?? "http://localhost:0/callback";
89
- }
90
-
91
- /** Set the redirect URL (called after CallbackServer binds to a port). */
92
- setRedirectUrl(url: string): void {
93
- if (isCimdConfig(this.config)) {
94
- if (url !== this.config.clientRegistration.redirectUri) {
95
- throw new CimdConfigError(
96
- "cimd_redirect_mismatch",
97
- "CIMD redirectUrl cannot differ from configured redirectUri",
98
- {
99
- redirectUrl: url,
100
- configuredRedirectUri: this.config.clientRegistration.redirectUri,
101
- },
102
- "Use clientRegistration.redirectUri as the runtime redirect URL.",
103
- );
104
- }
105
- return;
106
- }
107
- this._redirectUrl = url;
108
- }
109
-
110
- get clientMetadata(): OAuthClientMetadata {
111
- return {
112
- client_name: this.config.clientName ?? "PML Client",
113
- redirect_uris: [String(this.redirectUrl)],
114
- grant_types: ["authorization_code", "refresh_token"],
115
- response_types: ["code"],
116
- token_endpoint_auth_method: "none", // public client, PKCE only
117
- application_type: "native", // loopback CLI/desktop — prevents AS rejection of http://127.0.0.1 redirects
118
- };
119
- }
120
-
121
- clientInformation(): Promise<OAuthClientInformationMixed | undefined> {
122
- if (isCimdConfig(this.config)) {
123
- return Promise.resolve({
124
- client_id: this.config.clientRegistration.clientIdMetadataUrl,
125
- });
126
- }
127
- return Promise.resolve(
128
- this._clientInfo ?? {
129
- client_id: this.config.clientId,
130
- },
131
- );
132
- }
133
-
134
- saveClientInformation(
135
- info: OAuthClientInformationMixed,
136
- ): Promise<void> {
137
- this._clientInfo = info;
138
- return Promise.resolve();
139
- }
140
-
141
- /**
142
- * Record the authorization server the SDK just resolved (SEP-2352).
143
- *
144
- * `auth()` calls this after RFC 9728 discovery and before {@link tokens},
145
- * which is what makes the check there possible.
146
- *
147
- * The binding is the **discovered URL**, not `metadata.issuer`. That
148
- * distinction is the whole protection: `issuer` is a field the authorization
149
- * server publishes about itself, so an attacker's server reached at
150
- * `https://attacker.example.com` can claim
151
- * `issuer: "https://as-one.example.com"` and, since SDK 1.29 validates the
152
- * metadata's shape without checking it against the URL it was fetched from,
153
- * would be handed the credentials minted by the real AS-one. The URL the
154
- * client actually resolved is the one thing in this exchange the server
155
- * cannot forge.
156
- *
157
- * RFC 8414 does require `issuer` to match the discovery URL. A mismatch is
158
- * therefore a broken or hostile authorization server, and it is logged —
159
- * but nothing here depends on the claim being honest.
160
- */
161
- saveDiscoveryState(state: OAuthDiscoveryState): Promise<void> {
162
- const resolved = canonicalIssuer(state.authorizationServerUrl);
163
- const claimed = state.authorizationServerMetadata?.issuer;
164
-
165
- if (claimed !== undefined && canonicalIssuer(claimed) !== resolved) {
166
- this.log(
167
- `authorization server at ${redactUrl(state.authorizationServerUrl)} ` +
168
- `claims issuer "${redactUrl(claimed)}" ` +
169
- "(RFC 8414 requires them to match); " +
170
- "binding credentials to the discovered URL",
171
- );
172
- }
173
-
174
- this._currentIssuer = resolved;
175
- return Promise.resolve();
176
- }
177
-
178
- /**
179
- * Stored tokens, unless they were issued by a different authorization server.
180
- *
181
- * SEP-2352 makes this a MUST, and the reason is concrete rather than
182
- * bureaucratic. `auth()` hands whatever this returns to
183
- * `refreshAuthorization(authorizationServerUrl, …)`, where the URL comes from
184
- * the MCP server's own protected-resource metadata. A server that changes —
185
- * or is made to advertise — a different authorization server would therefore
186
- * receive a refresh token minted by the previous one. The client must not let
187
- * the resource server pick who sees its credentials.
188
- *
189
- * The check only runs once an issuer is known, which is the case inside
190
- * `auth()`. A transport asking for the bearer to attach to an ordinary
191
- * request has no issuer in hand, and that path is not the exposure: those
192
- * tokens go to the MCP server they were minted for, not to an authorization
193
- * server.
194
- */
195
- async tokens(): Promise<OAuthTokens | undefined> {
196
- const stored = await this.config.tokenStore.get(this.serverUrl);
197
- if (!stored) return undefined;
198
- if (this._currentIssuer === undefined) return stored.tokens;
199
-
200
- if (stored.authServerUrl === undefined) {
201
- // Written before this binding existed: nothing proves which authorization
202
- // server minted them. Treated as unusable rather than trusted once —
203
- // the whole point is to stop handing credentials to an unverified party,
204
- // and "probably fine" is the assumption this check exists to remove. The
205
- // cost is one re-authorization per stored credential, at upgrade time.
206
- await this.config.tokenStore.delete(this.serverUrl);
207
- this.log(
208
- "discarding stored credentials with no recorded issuer (SEP-2352); re-authorization required",
209
- );
210
- return undefined;
211
- }
212
-
213
- if (canonicalIssuer(stored.authServerUrl) !== this._currentIssuer) {
214
- // A legitimate migration lands here too, and is handled the same way:
215
- // drop the old credentials and let the flow re-authorize against the new
216
- // server. What must not happen is the old refresh token travelling there.
217
- await this.config.tokenStore.delete(this.serverUrl);
218
- this.log(
219
- `authorization server changed for ${redactUrl(this.serverUrl)} ` +
220
- `(${redactUrl(stored.authServerUrl)} → ` +
221
- `${redactUrl(this._currentIssuer)}); ` +
222
- "stored credentials discarded, re-authorization required",
223
- );
224
- return undefined;
225
- }
226
-
227
- return stored.tokens;
228
- }
229
-
230
- async saveTokens(tokens: OAuthTokens): Promise<void> {
231
- // Sequential guard only: keep an existing refresh_token when a later save omits it.
232
- // This get/set is not atomic; concurrent saves for one serverUrl are unsupported
233
- // because refresh is serialized by the caller.
234
- const stored = await this.config.tokenStore.get(this.serverUrl);
235
-
236
- // …but only from the same authorization server. `auth({ authorizationCode })`
237
- // reaches here without going through `tokens()`, so this is the one path
238
- // where a credential from a previous issuer can survive: if the code
239
- // exchange returns no refresh token, carrying the old one over would
240
- // re-label another server's secret with the new issuer and hand it over on
241
- // the next refresh. A legacy record has no recorded issuer, so it cannot
242
- // clear this bar either.
243
- const inheritable = stored?.authServerUrl !== undefined &&
244
- this._currentIssuer !== undefined &&
245
- canonicalIssuer(stored.authServerUrl) === this._currentIssuer;
246
-
247
- const tokensToStore = tokens.refresh_token === undefined &&
248
- inheritable && stored?.tokens.refresh_token !== undefined
249
- ? { ...tokens, refresh_token: stored.tokens.refresh_token }
250
- : tokens;
251
-
252
- await this.config.tokenStore.set(this.serverUrl, {
253
- serverUrl: this.serverUrl,
254
- tokens: tokensToStore,
255
- obtainedAt: Date.now(),
256
- // Binds the credentials to their minter. Without it every save produces
257
- // exactly the legacy record `tokens()` has to throw away.
258
- ...(this._currentIssuer !== undefined
259
- ? { authServerUrl: this._currentIssuer }
260
- : {}),
261
- });
262
- }
263
-
264
- private log(message: string): void {
265
- this.config.logger?.(`[client-auth] ${message}`);
266
- }
267
-
268
- async redirectToAuthorization(authorizationUrl: URL): Promise<void> {
269
- await this.config.openBrowser(authorizationUrl.toString());
270
- }
271
-
272
- saveCodeVerifier(codeVerifier: string): Promise<void> {
273
- this._codeVerifier = codeVerifier;
274
- return Promise.resolve();
275
- }
276
-
277
- codeVerifier(): Promise<string> {
278
- return Promise.resolve(this._codeVerifier);
279
- }
280
-
281
- async invalidateCredentials(
282
- scope: "all" | "client" | "tokens" | "verifier" | "discovery",
283
- ): Promise<void> {
284
- if (scope === "all" || scope === "tokens") {
285
- await this.config.tokenStore.delete(this.serverUrl);
286
- }
287
- if (scope === "all" || scope === "verifier") {
288
- this._codeVerifier = "";
289
- }
290
- if (scope === "all" || scope === "client") {
291
- this._clientInfo = undefined;
292
- }
293
- if (scope === "all" || scope === "discovery") {
294
- // Keeping a stale issuer would make the next `tokens()` compare against
295
- // an authorization server the SDK has already given up on, and discard
296
- // credentials that are in fact still valid.
297
- this._currentIssuer = undefined;
298
- }
299
- }
300
- }
@@ -1,73 +0,0 @@
1
- /**
2
- * File-based token store.
3
- *
4
- * Stores OAuth credentials as JSON files with restrictive permissions (0o600).
5
- * One file per MCP server, keyed by SHA-256 hash of the server URL.
6
- *
7
- * @module lib/server/client-auth/token-store/file-store
8
- */
9
-
10
- import type { StoredCredentials, TokenStore } from "../types.js";
11
- import {
12
- mkdir,
13
- readDir,
14
- readTextFile,
15
- remove,
16
- writeTextFile,
17
- } from "../../runtime/runtime.js";
18
-
19
- async function sha256hex(input: string): Promise<string> {
20
- const data = new TextEncoder().encode(input);
21
- const hash = await crypto.subtle.digest("SHA-256", data);
22
- return Array.from(new Uint8Array(hash))
23
- .map((b) => b.toString(16).padStart(2, "0"))
24
- .join("");
25
- }
26
-
27
- export class FileTokenStore implements TokenStore {
28
- constructor(private baseDir: string) {}
29
-
30
- private async filePath(serverUrl: string): Promise<string> {
31
- const hash = await sha256hex(serverUrl);
32
- return `${this.baseDir}/${hash}.json`;
33
- }
34
-
35
- private async ensureDir(): Promise<void> {
36
- await mkdir(this.baseDir, { recursive: true, mode: 0o700 });
37
- }
38
-
39
- async get(serverUrl: string): Promise<StoredCredentials | null> {
40
- const path = await this.filePath(serverUrl);
41
- const content = await readTextFile(path);
42
- return content === null ? null : JSON.parse(content) as StoredCredentials;
43
- }
44
-
45
- async set(serverUrl: string, credentials: StoredCredentials): Promise<void> {
46
- await this.ensureDir();
47
- const path = await this.filePath(serverUrl);
48
- const content = JSON.stringify(credentials, null, 2);
49
- await writeTextFile(path, content, { mode: 0o600 });
50
- }
51
-
52
- async delete(serverUrl: string): Promise<void> {
53
- const path = await this.filePath(serverUrl);
54
- await remove(path);
55
- }
56
-
57
- async list(): Promise<string[]> {
58
- const urls: string[] = [];
59
- for (const name of await readDir(this.baseDir)) {
60
- if (name.endsWith(".json")) {
61
- try {
62
- const content = await readTextFile(`${this.baseDir}/${name}`);
63
- if (content === null) continue;
64
- const creds = JSON.parse(content) as StoredCredentials;
65
- urls.push(creds.serverUrl);
66
- } catch {
67
- // Corrupted file, skip
68
- }
69
- }
70
- }
71
- return urls;
72
- }
73
- }
@@ -1,32 +0,0 @@
1
- /**
2
- * In-memory token store for testing and ephemeral use.
3
- *
4
- * @module lib/server/client-auth/token-store/memory-store
5
- */
6
-
7
- import type { StoredCredentials, TokenStore } from "../types.js";
8
-
9
- export class MemoryTokenStore implements TokenStore {
10
- private store = new Map<string, StoredCredentials>();
11
-
12
- // Not `async`: the Map operations are synchronous. The interface returns
13
- // Promises so a real store can be I/O-backed; `Promise.resolve` satisfies it
14
- // without claiming an await point that does not exist.
15
- get(serverUrl: string): Promise<StoredCredentials | null> {
16
- return Promise.resolve(this.store.get(serverUrl) ?? null);
17
- }
18
-
19
- set(serverUrl: string, credentials: StoredCredentials): Promise<void> {
20
- this.store.set(serverUrl, credentials);
21
- return Promise.resolve();
22
- }
23
-
24
- delete(serverUrl: string): Promise<void> {
25
- this.store.delete(serverUrl);
26
- return Promise.resolve();
27
- }
28
-
29
- list(): Promise<string[]> {
30
- return Promise.resolve([...this.store.keys()]);
31
- }
32
- }
@@ -1,95 +0,0 @@
1
- /**
2
- * OAuth client auth types.
3
- *
4
- * Interfaces for token storage and OAuth client configuration.
5
- * Used by OAuthClientProviderImpl to authenticate against
6
- * OAuth-protected MCP servers.
7
- *
8
- * @module lib/server/client-auth/types
9
- */
10
-
11
- import type { OAuthTokens } from "@modelcontextprotocol/sdk/shared/auth.js";
12
-
13
- /** Stored OAuth credentials for one MCP server */
14
- export interface StoredCredentials {
15
- /** The MCP server URL these credentials are for */
16
- serverUrl: string;
17
- /** OAuth tokens (access_token, refresh_token, etc.) */
18
- tokens: OAuthTokens;
19
- /** When the tokens were obtained (Unix epoch ms) */
20
- obtainedAt: number;
21
- /**
22
- * Issuer that minted these credentials (SEP-2352).
23
- *
24
- * Absent on records written before the binding existed; `tokens()` treats
25
- * that as unusable, since nothing proves who minted them.
26
- */
27
- authServerUrl?: string;
28
- }
29
-
30
- /** Abstract token storage — consumers provide the implementation */
31
- export interface TokenStore {
32
- /** Retrieve stored credentials for a server, or null if none */
33
- get(serverUrl: string): Promise<StoredCredentials | null>;
34
- /** Store credentials for a server */
35
- set(serverUrl: string, credentials: StoredCredentials): Promise<void>;
36
- /** Delete credentials for a server */
37
- delete(serverUrl: string): Promise<void>;
38
- /** List all server URLs with stored credentials */
39
- list(): Promise<string[]>;
40
- }
41
-
42
- /** Fields shared by all OAuth client registration modes. */
43
- export interface OAuthClientConfigBase {
44
- /** Client name shown in consent screen */
45
- clientName?: string;
46
- /** Scopes to request (space-separated in OAuth, array here) */
47
- scopes?: string[];
48
- /** Token storage backend */
49
- tokenStore: TokenStore;
50
- /** Callback to open browser — injected by consumer (platform-specific) */
51
- openBrowser: (url: string) => Promise<void>;
52
- /** Callback server port (0 = OS auto-assign, default: 0) */
53
- callbackPort?: number;
54
- /** Timeout for user to complete OAuth flow (ms, default: 120_000) */
55
- authTimeout?: number;
56
- /**
57
- * Optional sink for credential-lifecycle events.
58
- *
59
- * Notably an authorization-server change (SEP-2352): the client silently
60
- * drops its stored credentials and re-authorizes, and an operator debugging
61
- * an unexpected login prompt has no other way to see why.
62
- */
63
- logger?: (message: string) => void;
64
- }
65
-
66
- /** Static OAuth client registration (default, backwards-compatible mode). */
67
- export interface StaticClientConfig extends OAuthClientConfigBase {
68
- /** OAuth Client ID (public — embedded in consumer binary) */
69
- clientId: string;
70
- /** Absence of clientRegistration selects static client-id behavior. */
71
- clientRegistration?: undefined;
72
- }
73
-
74
- /** CIMD client registration (explicit opt-in). */
75
- export interface CimdClientConfig extends OAuthClientConfigBase {
76
- /** In CIMD mode, client_id is derived from clientIdMetadataUrl. */
77
- clientId?: never;
78
- clientRegistration: {
79
- method: "client_id_metadata";
80
- /** HTTPS URL of the hosted Client ID Metadata Document; also the client_id. */
81
- clientIdMetadataUrl: string;
82
- /** Runtime redirect URI published in the metadata document. */
83
- redirectUri: string;
84
- /** Optional display and extension metadata for the hosted document. */
85
- metadata?: {
86
- client_uri?: string;
87
- logo_uri?: string;
88
- contacts?: string[];
89
- extra?: Record<string, unknown>;
90
- };
91
- };
92
- }
93
-
94
- /** Configuration for creating an OAuthClientProvider */
95
- export type OAuthClientConfig = StaticClientConfig | CimdClientConfig;
@@ -1,190 +0,0 @@
1
- /**
2
- * Rate Limiter
3
- *
4
- * Sliding window rate limiter for per-client request throttling.
5
- * Prevents server overload by limiting requests per time window.
6
- *
7
- * @module lib/server/rate-limiter
8
- */
9
-
10
- /**
11
- * Sliding window rate limiter
12
- *
13
- * Features:
14
- * - Per-key rate limiting (client ID, IP, etc.)
15
- * - Sliding window for smooth rate enforcement
16
- * - Automatic cleanup of old timestamps
17
- * - Backoff waiting when limit exceeded
18
- *
19
- * @example
20
- * ```typescript
21
- * const limiter = new RateLimiter({ maxRequests: 100, windowMs: 60000 });
22
- *
23
- * if (await limiter.checkLimit("client-123")) {
24
- * // Execute request
25
- * } else {
26
- * // Rate limited
27
- * }
28
- * ```
29
- */
30
- export class RateLimiter {
31
- private requestCounts = new Map<string, number[]>();
32
- private readonly windowMs: number;
33
- private readonly maxRequests: number;
34
- private operationsSinceLastPurge = 0;
35
- private static readonly PURGE_EVERY_N_OPS = 1000;
36
-
37
- constructor(options: { maxRequests: number; windowMs: number }) {
38
- this.maxRequests = options.maxRequests;
39
- this.windowMs = options.windowMs;
40
- }
41
-
42
- /**
43
- * Check if request is allowed for the given key
44
- *
45
- * @param key - Identifier (client ID, IP, etc.)
46
- * @returns true if allowed, false if rate limited
47
- */
48
- checkLimit(key: string): boolean {
49
- const now = Date.now();
50
- const requests = this.requestCounts.get(key) || [];
51
-
52
- // Remove old requests outside the sliding window
53
- const validRequests = requests.filter((time) => now - time < this.windowMs);
54
-
55
- if (validRequests.length === 0) {
56
- // No active requests for this key — remove from map to prevent unbounded growth
57
- this.requestCounts.delete(key);
58
- }
59
-
60
- if (validRequests.length >= this.maxRequests) {
61
- // Update with cleaned list
62
- this.requestCounts.set(key, validRequests);
63
- return false;
64
- }
65
-
66
- // Add current request timestamp
67
- validRequests.push(now);
68
- this.requestCounts.set(key, validRequests);
69
-
70
- // Periodic full purge to catch keys that haven't been checked recently
71
- this.operationsSinceLastPurge++;
72
- if (this.operationsSinceLastPurge >= RateLimiter.PURGE_EVERY_N_OPS) {
73
- this.purgeExpiredKeys();
74
- }
75
-
76
- return true;
77
- }
78
-
79
- /**
80
- * Remove all keys with no active requests in the current window.
81
- * Called automatically every PURGE_EVERY_N_OPS operations.
82
- */
83
- purgeExpiredKeys(): number {
84
- const now = Date.now();
85
- let purged = 0;
86
- for (const [key, requests] of this.requestCounts) {
87
- const active = requests.filter((time) => now - time < this.windowMs);
88
- if (active.length === 0) {
89
- this.requestCounts.delete(key);
90
- purged++;
91
- } else {
92
- this.requestCounts.set(key, active);
93
- }
94
- }
95
- this.operationsSinceLastPurge = 0;
96
- return purged;
97
- }
98
-
99
- /**
100
- * Wait until request slot is available (with exponential backoff)
101
- *
102
- * @param key - Identifier (client ID, IP, etc.)
103
- * @throws Error if max wait time exceeded (defaults to windowMs)
104
- */
105
- async waitForSlot(key: string): Promise<void> {
106
- let retries = 0;
107
- const baseDelay = 100;
108
- const maxWaitMs = this.windowMs;
109
- const startTime = Date.now();
110
-
111
- while (!this.checkLimit(key)) {
112
- if (Date.now() - startTime >= maxWaitMs) {
113
- throw new Error(
114
- `Rate limit wait timeout: waited ${maxWaitMs}ms for key "${key}"`,
115
- );
116
- }
117
- // Exponential backoff: 100ms, 200ms, 400ms, 800ms, cap at 1000ms
118
- const delay = Math.min(baseDelay * Math.pow(2, retries), 1000);
119
- await new Promise((resolve) => setTimeout(resolve, delay));
120
- retries++;
121
- }
122
- }
123
-
124
- /**
125
- * Get current request count for a key
126
- */
127
- getCurrentCount(key: string): number {
128
- const now = Date.now();
129
- const requests = this.requestCounts.get(key) || [];
130
- return requests.filter((time) => now - time < this.windowMs).length;
131
- }
132
-
133
- /**
134
- * Get remaining requests for a key
135
- */
136
- getRemainingRequests(key: string): number {
137
- return Math.max(0, this.maxRequests - this.getCurrentCount(key));
138
- }
139
-
140
- /**
141
- * Get time until next slot is available (in ms)
142
- * Returns 0 if a slot is available now
143
- */
144
- getTimeUntilSlot(key: string): number {
145
- const now = Date.now();
146
- const requests = this.requestCounts.get(key) || [];
147
- const validRequests = requests.filter((time) => now - time < this.windowMs);
148
-
149
- if (validRequests.length < this.maxRequests) {
150
- return 0;
151
- }
152
-
153
- // Find oldest request in window - when it expires, a slot opens
154
- const oldest = Math.min(...validRequests);
155
- return Math.max(0, oldest + this.windowMs - now);
156
- }
157
-
158
- /**
159
- * Clear rate limit history for a key
160
- */
161
- clear(key: string): void {
162
- this.requestCounts.delete(key);
163
- }
164
-
165
- /**
166
- * Clear all rate limit history
167
- */
168
- clearAll(): void {
169
- this.requestCounts.clear();
170
- }
171
-
172
- /**
173
- * Get metrics for monitoring
174
- */
175
- getMetrics(): { keys: number; totalRequests: number } {
176
- let totalRequests = 0;
177
- const now = Date.now();
178
-
179
- for (const requests of this.requestCounts.values()) {
180
- totalRequests += requests.filter((time) =>
181
- now - time < this.windowMs
182
- ).length;
183
- }
184
-
185
- return {
186
- keys: this.requestCounts.size,
187
- totalRequests,
188
- };
189
- }
190
- }