@kya-os/mcp-i-runtime 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 (366) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/dist/auth/handshake.d.ts +136 -0
  3. package/dist/auth/handshake.d.ts.map +1 -0
  4. package/dist/auth/handshake.js +276 -0
  5. package/dist/auth/handshake.js.map +1 -0
  6. package/dist/auth/index.d.ts +6 -0
  7. package/dist/auth/index.d.ts.map +1 -0
  8. package/dist/auth/index.js +12 -0
  9. package/dist/auth/index.js.map +1 -0
  10. package/dist/auth/types.d.ts +52 -0
  11. package/dist/auth/types.d.ts.map +1 -0
  12. package/dist/auth/types.js +10 -0
  13. package/dist/auth/types.js.map +1 -0
  14. package/dist/cache/oauth-config-cache.d.ts +69 -0
  15. package/dist/cache/oauth-config-cache.d.ts.map +1 -0
  16. package/dist/cache/oauth-config-cache.js +76 -0
  17. package/dist/cache/oauth-config-cache.js.map +1 -0
  18. package/dist/cache/tool-protection-cache.d.ts +102 -0
  19. package/dist/cache/tool-protection-cache.d.ts.map +1 -0
  20. package/dist/cache/tool-protection-cache.js +131 -0
  21. package/dist/cache/tool-protection-cache.js.map +1 -0
  22. package/dist/compliance/index.d.ts +8 -0
  23. package/dist/compliance/index.d.ts.map +1 -0
  24. package/dist/compliance/index.js +24 -0
  25. package/dist/compliance/index.js.map +1 -0
  26. package/dist/compliance/schema-registry.d.ts +50 -0
  27. package/dist/compliance/schema-registry.d.ts.map +1 -0
  28. package/dist/compliance/schema-registry.js +438 -0
  29. package/dist/compliance/schema-registry.js.map +1 -0
  30. package/dist/compliance/schema-verifier.d.ts +110 -0
  31. package/dist/compliance/schema-verifier.d.ts.map +1 -0
  32. package/dist/compliance/schema-verifier.js +518 -0
  33. package/dist/compliance/schema-verifier.js.map +1 -0
  34. package/dist/config/remote-config.d.ts +188 -0
  35. package/dist/config/remote-config.d.ts.map +1 -0
  36. package/dist/config/remote-config.js +268 -0
  37. package/dist/config/remote-config.js.map +1 -0
  38. package/dist/config.d.ts +182 -0
  39. package/dist/config.d.ts.map +1 -0
  40. package/dist/config.js +148 -0
  41. package/dist/config.js.map +1 -0
  42. package/dist/identity/idp-token-resolver.d.ts +69 -0
  43. package/dist/identity/idp-token-resolver.d.ts.map +1 -0
  44. package/dist/identity/idp-token-resolver.js +136 -0
  45. package/dist/identity/idp-token-resolver.js.map +1 -0
  46. package/dist/identity/idp-token-storage.interface.d.ts +80 -0
  47. package/dist/identity/idp-token-storage.interface.d.ts.map +1 -0
  48. package/dist/identity/idp-token-storage.interface.js +14 -0
  49. package/dist/identity/idp-token-storage.interface.js.map +1 -0
  50. package/dist/identity/user-did-manager.d.ts +228 -0
  51. package/dist/identity/user-did-manager.d.ts.map +1 -0
  52. package/dist/identity/user-did-manager.js +356 -0
  53. package/dist/identity/user-did-manager.js.map +1 -0
  54. package/dist/index.d.ts +69 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +193 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/logging/index.d.ts +5 -0
  59. package/dist/logging/index.d.ts.map +1 -0
  60. package/dist/logging/index.js +10 -0
  61. package/dist/logging/index.js.map +1 -0
  62. package/dist/logging/logger.d.ts +33 -0
  63. package/dist/logging/logger.d.ts.map +1 -0
  64. package/dist/logging/logger.js +104 -0
  65. package/dist/logging/logger.js.map +1 -0
  66. package/dist/proof/generator.d.ts +109 -0
  67. package/dist/proof/generator.d.ts.map +1 -0
  68. package/dist/proof/generator.js +236 -0
  69. package/dist/proof/generator.js.map +1 -0
  70. package/dist/proof/index.d.ts +5 -0
  71. package/dist/proof/index.d.ts.map +1 -0
  72. package/dist/proof/index.js +11 -0
  73. package/dist/proof/index.js.map +1 -0
  74. package/dist/providers/base.d.ts +95 -0
  75. package/dist/providers/base.d.ts.map +1 -0
  76. package/dist/providers/base.js +47 -0
  77. package/dist/providers/base.js.map +1 -0
  78. package/dist/providers/memory.d.ts +42 -0
  79. package/dist/providers/memory.d.ts.map +1 -0
  80. package/dist/providers/memory.js +121 -0
  81. package/dist/providers/memory.js.map +1 -0
  82. package/dist/runtime/audit-logger.d.ts +37 -0
  83. package/dist/runtime/audit-logger.d.ts.map +1 -0
  84. package/dist/runtime/audit-logger.js +9 -0
  85. package/dist/runtime/audit-logger.js.map +1 -0
  86. package/dist/runtime/base.d.ts +392 -0
  87. package/dist/runtime/base.d.ts.map +1 -0
  88. package/dist/runtime/base.js +1527 -0
  89. package/dist/runtime/base.js.map +1 -0
  90. package/dist/runtime/dif-core.d.ts +33 -0
  91. package/dist/runtime/dif-core.d.ts.map +1 -0
  92. package/dist/runtime/dif-core.js +14 -0
  93. package/dist/runtime/dif-core.js.map +1 -0
  94. package/dist/runtime/ext-apps-constants.d.ts +20 -0
  95. package/dist/runtime/ext-apps-constants.d.ts.map +1 -0
  96. package/dist/runtime/ext-apps-constants.js +23 -0
  97. package/dist/runtime/ext-apps-constants.js.map +1 -0
  98. package/dist/services/access-control.service.d.ts +138 -0
  99. package/dist/services/access-control.service.d.ts.map +1 -0
  100. package/dist/services/access-control.service.js +721 -0
  101. package/dist/services/access-control.service.js.map +1 -0
  102. package/dist/services/authorization/authorization-registry.d.ts +29 -0
  103. package/dist/services/authorization/authorization-registry.d.ts.map +1 -0
  104. package/dist/services/authorization/authorization-registry.js +58 -0
  105. package/dist/services/authorization/authorization-registry.js.map +1 -0
  106. package/dist/services/authorization/types.d.ts +53 -0
  107. package/dist/services/authorization/types.d.ts.map +1 -0
  108. package/dist/services/authorization/types.js +10 -0
  109. package/dist/services/authorization/types.js.map +1 -0
  110. package/dist/services/batch-delegation.service.d.ts +53 -0
  111. package/dist/services/batch-delegation.service.d.ts.map +1 -0
  112. package/dist/services/batch-delegation.service.js +95 -0
  113. package/dist/services/batch-delegation.service.js.map +1 -0
  114. package/dist/services/crypto.service.d.ts +69 -0
  115. package/dist/services/crypto.service.d.ts.map +1 -0
  116. package/dist/services/crypto.service.js +225 -0
  117. package/dist/services/crypto.service.js.map +1 -0
  118. package/dist/services/delegation-error-formatter.d.ts +124 -0
  119. package/dist/services/delegation-error-formatter.d.ts.map +1 -0
  120. package/dist/services/delegation-error-formatter.js +214 -0
  121. package/dist/services/delegation-error-formatter.js.map +1 -0
  122. package/dist/services/errors.d.ts +49 -0
  123. package/dist/services/errors.d.ts.map +1 -0
  124. package/dist/services/errors.js +66 -0
  125. package/dist/services/errors.js.map +1 -0
  126. package/dist/services/index.d.ts +9 -0
  127. package/dist/services/index.d.ts.map +1 -0
  128. package/dist/services/index.js +15 -0
  129. package/dist/services/index.js.map +1 -0
  130. package/dist/services/oauth-config.service.d.ts +53 -0
  131. package/dist/services/oauth-config.service.d.ts.map +1 -0
  132. package/dist/services/oauth-config.service.js +141 -0
  133. package/dist/services/oauth-config.service.js.map +1 -0
  134. package/dist/services/oauth-provider-registry.d.ts +88 -0
  135. package/dist/services/oauth-provider-registry.d.ts.map +1 -0
  136. package/dist/services/oauth-provider-registry.js +128 -0
  137. package/dist/services/oauth-provider-registry.js.map +1 -0
  138. package/dist/services/oauth-service.d.ts +122 -0
  139. package/dist/services/oauth-service.d.ts.map +1 -0
  140. package/dist/services/oauth-service.js +442 -0
  141. package/dist/services/oauth-service.js.map +1 -0
  142. package/dist/services/oauth-token-retrieval.service.d.ts +49 -0
  143. package/dist/services/oauth-token-retrieval.service.d.ts.map +1 -0
  144. package/dist/services/oauth-token-retrieval.service.js +150 -0
  145. package/dist/services/oauth-token-retrieval.service.js.map +1 -0
  146. package/dist/services/policy.service.d.ts +130 -0
  147. package/dist/services/policy.service.d.ts.map +1 -0
  148. package/dist/services/policy.service.js +399 -0
  149. package/dist/services/policy.service.js.map +1 -0
  150. package/dist/services/proof-verifier.d.ts +103 -0
  151. package/dist/services/proof-verifier.d.ts.map +1 -0
  152. package/dist/services/proof-verifier.js +190 -0
  153. package/dist/services/proof-verifier.js.map +1 -0
  154. package/dist/services/provider-resolver.d.ts +89 -0
  155. package/dist/services/provider-resolver.d.ts.map +1 -0
  156. package/dist/services/provider-resolver.js +196 -0
  157. package/dist/services/provider-resolver.js.map +1 -0
  158. package/dist/services/provider-validator.d.ts +55 -0
  159. package/dist/services/provider-validator.d.ts.map +1 -0
  160. package/dist/services/provider-validator.js +135 -0
  161. package/dist/services/provider-validator.js.map +1 -0
  162. package/dist/services/session-registration.service.d.ts +80 -0
  163. package/dist/services/session-registration.service.d.ts.map +1 -0
  164. package/dist/services/session-registration.service.js +174 -0
  165. package/dist/services/session-registration.service.js.map +1 -0
  166. package/dist/services/storage.service.d.ts +116 -0
  167. package/dist/services/storage.service.d.ts.map +1 -0
  168. package/dist/services/storage.service.js +411 -0
  169. package/dist/services/storage.service.js.map +1 -0
  170. package/dist/services/tool-context-builder.d.ts +74 -0
  171. package/dist/services/tool-context-builder.d.ts.map +1 -0
  172. package/dist/services/tool-context-builder.js +215 -0
  173. package/dist/services/tool-context-builder.js.map +1 -0
  174. package/dist/services/tool-protection.service.d.ts +209 -0
  175. package/dist/services/tool-protection.service.d.ts.map +1 -0
  176. package/dist/services/tool-protection.service.js +752 -0
  177. package/dist/services/tool-protection.service.js.map +1 -0
  178. package/dist/session/index.d.ts +5 -0
  179. package/dist/session/index.d.ts.map +1 -0
  180. package/dist/session/index.js +11 -0
  181. package/dist/session/index.js.map +1 -0
  182. package/dist/session/manager.d.ts +118 -0
  183. package/dist/session/manager.d.ts.map +1 -0
  184. package/dist/session/manager.js +285 -0
  185. package/dist/session/manager.js.map +1 -0
  186. package/dist/types/oauth-required-error.d.ts +46 -0
  187. package/dist/types/oauth-required-error.d.ts.map +1 -0
  188. package/dist/types/oauth-required-error.js +42 -0
  189. package/dist/types/oauth-required-error.js.map +1 -0
  190. package/dist/types/tool-protection.d.ts +144 -0
  191. package/dist/types/tool-protection.d.ts.map +1 -0
  192. package/dist/types/tool-protection.js +48 -0
  193. package/dist/types/tool-protection.js.map +1 -0
  194. package/dist/utils/base58.d.ts +31 -0
  195. package/dist/utils/base58.d.ts.map +1 -0
  196. package/dist/utils/base58.js +103 -0
  197. package/dist/utils/base58.js.map +1 -0
  198. package/dist/utils/base64.d.ts +32 -0
  199. package/dist/utils/base64.d.ts.map +1 -0
  200. package/dist/utils/base64.js +172 -0
  201. package/dist/utils/base64.js.map +1 -0
  202. package/dist/utils/cors.d.ts +52 -0
  203. package/dist/utils/cors.d.ts.map +1 -0
  204. package/dist/utils/cors.js +79 -0
  205. package/dist/utils/cors.js.map +1 -0
  206. package/dist/utils/did-helpers.d.ts +168 -0
  207. package/dist/utils/did-helpers.d.ts.map +1 -0
  208. package/dist/utils/did-helpers.js +222 -0
  209. package/dist/utils/did-helpers.js.map +1 -0
  210. package/dist/utils/index.d.ts +8 -0
  211. package/dist/utils/index.d.ts.map +1 -0
  212. package/dist/utils/index.js +24 -0
  213. package/dist/utils/index.js.map +1 -0
  214. package/dist/utils/storage-keys.d.ts +120 -0
  215. package/dist/utils/storage-keys.d.ts.map +1 -0
  216. package/dist/utils/storage-keys.js +217 -0
  217. package/dist/utils/storage-keys.js.map +1 -0
  218. package/dist/wire-format.d.ts +62 -0
  219. package/dist/wire-format.d.ts.map +1 -0
  220. package/dist/wire-format.js +89 -0
  221. package/dist/wire-format.js.map +1 -0
  222. package/package.json +83 -0
  223. package/src/__tests__/cache/tool-protection-cache.test.ts +640 -0
  224. package/src/__tests__/config/provider-runtime-config.test.ts +309 -0
  225. package/src/__tests__/config.test.ts +307 -0
  226. package/src/__tests__/identity/user-did-manager.test.ts +232 -0
  227. package/src/__tests__/index.test.ts +56 -0
  228. package/src/__tests__/integration/full-flow.test.ts +779 -0
  229. package/src/__tests__/integration.test.ts +281 -0
  230. package/src/__tests__/providers/base.test.ts +173 -0
  231. package/src/__tests__/providers/memory.test.ts +319 -0
  232. package/src/__tests__/regression/phase2-regression.test.ts +406 -0
  233. package/src/__tests__/runtime/audit-logger.test.ts +154 -0
  234. package/src/__tests__/runtime/base-extensions.test.ts +595 -0
  235. package/src/__tests__/runtime/base.test.ts +1328 -0
  236. package/src/__tests__/runtime/delegation-flow.test.ts +159 -0
  237. package/src/__tests__/runtime/process-tool-call-security.test.ts +1024 -0
  238. package/src/__tests__/runtime/proof-client-did.test.ts +376 -0
  239. package/src/__tests__/runtime/provider-selection.test.ts +292 -0
  240. package/src/__tests__/runtime/route-interception.test.ts +670 -0
  241. package/src/__tests__/runtime/tool-protection-enforcement.test.ts +989 -0
  242. package/src/__tests__/services/access-control-retry.test.ts +595 -0
  243. package/src/__tests__/services/agentshield-integration.test.ts +709 -0
  244. package/src/__tests__/services/cache-busting.test.ts +125 -0
  245. package/src/__tests__/services/oauth-service-errors.test.ts +704 -0
  246. package/src/__tests__/services/oauth-service-pkce.test.ts +556 -0
  247. package/src/__tests__/services/oauth-service-secret-resolution.test.ts +349 -0
  248. package/src/__tests__/services/provider-resolver-edge-cases.test.ts +591 -0
  249. package/src/__tests__/services/tool-protection-failsafe.test.ts +683 -0
  250. package/src/__tests__/services/tool-protection-merged-config.test.ts +664 -0
  251. package/src/__tests__/services/tool-protection-oauth-provider.test.ts +300 -0
  252. package/src/__tests__/services/tool-protection.service.test.ts +1201 -0
  253. package/src/__tests__/utils/mock-providers.ts +340 -0
  254. package/src/__tests__/wire-format.test.ts +123 -0
  255. package/src/auth/__tests__/resume-token.test.ts +42 -0
  256. package/src/auth/handshake.ts +476 -0
  257. package/src/auth/index.ts +15 -0
  258. package/src/auth/types.ts +68 -0
  259. package/src/cache/__tests__/oauth-config-cache.test.ts +215 -0
  260. package/src/cache/oauth-config-cache.d.ts +69 -0
  261. package/src/cache/oauth-config-cache.d.ts.map +1 -0
  262. package/src/cache/oauth-config-cache.js.map +1 -0
  263. package/src/cache/oauth-config-cache.ts +123 -0
  264. package/src/cache/tool-protection-cache.ts +184 -0
  265. package/src/compliance/EXAMPLE.md +412 -0
  266. package/src/compliance/__tests__/schema-verifier.test.ts +791 -0
  267. package/src/compliance/index.ts +8 -0
  268. package/src/compliance/schema-registry.ts +460 -0
  269. package/src/compliance/schema-verifier.ts +708 -0
  270. package/src/config/__tests__/merged-config.spec.ts +445 -0
  271. package/src/config/__tests__/remote-config-edge-cases.test.ts +424 -0
  272. package/src/config/__tests__/remote-config-helpers.test.ts +280 -0
  273. package/src/config/__tests__/remote-config.spec.ts +265 -0
  274. package/src/config/remote-config.ts +368 -0
  275. package/src/config.ts +312 -0
  276. package/src/identity/__tests__/idp-token-resolver.test.ts +340 -0
  277. package/src/identity/__tests__/user-did-manager.test.ts +487 -0
  278. package/src/identity/idp-token-resolver.ts +181 -0
  279. package/src/identity/idp-token-storage.interface.ts +102 -0
  280. package/src/identity/user-did-manager.ts +592 -0
  281. package/src/index.ts +331 -0
  282. package/src/logging/__tests__/logger.test.ts +366 -0
  283. package/src/logging/index.ts +10 -0
  284. package/src/logging/logger.ts +112 -0
  285. package/src/proof/__tests__/crypto-parity.test.ts +202 -0
  286. package/src/proof/__tests__/proof-generator.test.ts +484 -0
  287. package/src/proof/generator.ts +347 -0
  288. package/src/proof/index.ts +12 -0
  289. package/src/providers/base.d.ts +91 -0
  290. package/src/providers/base.d.ts.map +1 -0
  291. package/src/providers/base.js.map +1 -0
  292. package/src/providers/base.ts +100 -0
  293. package/src/providers/memory.ts +142 -0
  294. package/src/runtime/__tests__/dif-esm-bridge.test.ts +33 -0
  295. package/src/runtime/__tests__/ext-apps-consent.test.ts +334 -0
  296. package/src/runtime/audit-logger.ts +39 -0
  297. package/src/runtime/base.ts +2100 -0
  298. package/src/runtime/dif-core.ts +39 -0
  299. package/src/runtime/ext-apps-constants.ts +20 -0
  300. package/src/services/__tests__/access-control.integration.test.ts +441 -0
  301. package/src/services/__tests__/access-control.proof-response-validation.test.ts +578 -0
  302. package/src/services/__tests__/access-control.service.test.ts +1132 -0
  303. package/src/services/__tests__/batch-delegation.service.test.ts +351 -0
  304. package/src/services/__tests__/crypto.service.test.ts +531 -0
  305. package/src/services/__tests__/delegation-error-formatter.test.ts +643 -0
  306. package/src/services/__tests__/oauth-config.service.test.ts +359 -0
  307. package/src/services/__tests__/oauth-provider-registry.test.ts +142 -0
  308. package/src/services/__tests__/oauth-token-retrieval.service.test.ts +365 -0
  309. package/src/services/__tests__/proof-verifier.integration.test.ts +494 -0
  310. package/src/services/__tests__/proof-verifier.test.ts +489 -0
  311. package/src/services/__tests__/provider-resolution.integration.test.ts +202 -0
  312. package/src/services/__tests__/provider-resolver.test.ts +213 -0
  313. package/src/services/__tests__/provider-validator.test.ts +370 -0
  314. package/src/services/__tests__/session-registration.service.test.ts +312 -0
  315. package/src/services/__tests__/storage.service.test.ts +358 -0
  316. package/src/services/__tests__/tool-context-builder.test.ts +438 -0
  317. package/src/services/access-control.service.ts +1048 -0
  318. package/src/services/authorization/authorization-registry.ts +67 -0
  319. package/src/services/authorization/types.ts +71 -0
  320. package/src/services/batch-delegation.service.ts +136 -0
  321. package/src/services/crypto.service.ts +296 -0
  322. package/src/services/delegation-error-formatter.ts +299 -0
  323. package/src/services/errors.ts +76 -0
  324. package/src/services/index.ts +34 -0
  325. package/src/services/oauth-config.service.d.ts +53 -0
  326. package/src/services/oauth-config.service.d.ts.map +1 -0
  327. package/src/services/oauth-config.service.js.map +1 -0
  328. package/src/services/oauth-config.service.ts +192 -0
  329. package/src/services/oauth-provider-registry.d.ts +57 -0
  330. package/src/services/oauth-provider-registry.d.ts.map +1 -0
  331. package/src/services/oauth-provider-registry.js.map +1 -0
  332. package/src/services/oauth-provider-registry.ts +141 -0
  333. package/src/services/oauth-service.ts +652 -0
  334. package/src/services/oauth-token-retrieval.service.ts +245 -0
  335. package/src/services/policy.service.ts +527 -0
  336. package/src/services/proof-verifier.ts +295 -0
  337. package/src/services/provider-resolver.d.ts +48 -0
  338. package/src/services/provider-resolver.d.ts.map +1 -0
  339. package/src/services/provider-resolver.js.map +1 -0
  340. package/src/services/provider-resolver.ts +236 -0
  341. package/src/services/provider-validator.ts +170 -0
  342. package/src/services/session-registration.service.ts +259 -0
  343. package/src/services/storage.service.ts +566 -0
  344. package/src/services/tool-context-builder.ts +285 -0
  345. package/src/services/tool-protection.service.ts +971 -0
  346. package/src/session/__tests__/session-manager.test.ts +327 -0
  347. package/src/session/index.ts +10 -0
  348. package/src/session/manager.ts +380 -0
  349. package/src/types/__tests__/oauth-required-error.test.ts +79 -0
  350. package/src/types/__tests__/tool-protection.test.ts +121 -0
  351. package/src/types/oauth-required-error.ts +71 -0
  352. package/src/types/tool-protection.ts +179 -0
  353. package/src/utils/__tests__/base58.test.ts +281 -0
  354. package/src/utils/__tests__/base64.test.ts +239 -0
  355. package/src/utils/__tests__/cors.test.ts +184 -0
  356. package/src/utils/__tests__/did-helpers.test.ts +156 -0
  357. package/src/utils/__tests__/storage-keys.test.ts +372 -0
  358. package/src/utils/base58.ts +109 -0
  359. package/src/utils/base64.ts +185 -0
  360. package/src/utils/cors.ts +93 -0
  361. package/src/utils/did-helpers.ts +229 -0
  362. package/src/utils/index.ts +8 -0
  363. package/src/utils/storage-keys.ts +285 -0
  364. package/src/wire-format.ts +114 -0
  365. package/tsconfig.json +21 -0
  366. package/vitest.config.mts +57 -0
@@ -0,0 +1,179 @@
1
+ /**
2
+ * Tool Protection Types
3
+ *
4
+ * Implementation-specific type definitions for tool protection services.
5
+ * Core spec types are imported from @kya-os/contracts/tool-protection.
6
+ *
7
+ * @package @kya-os/mcp-i-core
8
+ * @version 1.0.0
9
+ */
10
+
11
+ // Import spec types from contracts
12
+ import type {
13
+ ToolProtection,
14
+ ToolProtectionMap,
15
+ } from "@kya-os/contracts/tool-protection";
16
+
17
+ // Re-export for backward compatibility
18
+ export type { ToolProtection };
19
+
20
+ /**
21
+ * Complete tool protection configuration for a project
22
+ * @deprecated Use ToolProtectionMap from @kya-os/contracts/tool-protection
23
+ */
24
+ export interface ToolProtectionConfig {
25
+ /**
26
+ * Map of tool names to their protection settings
27
+ */
28
+ toolProtections: ToolProtectionMap;
29
+ }
30
+
31
+ /**
32
+ * Configuration for the ToolProtectionService
33
+ */
34
+ export interface ToolProtectionServiceConfig {
35
+ /**
36
+ * AgentShield API base URL
37
+ * @example "https://kya.vouched.id"
38
+ */
39
+ apiUrl: string;
40
+
41
+ /**
42
+ * AgentShield API key for authentication
43
+ * @example "sk_..."
44
+ */
45
+ apiKey: string;
46
+
47
+ /**
48
+ * Project ID (optional, used for backward compatibility)
49
+ * The service now fetches config by agent DID instead
50
+ * @deprecated Use agent DID from runtime identity
51
+ * @example "mcp-i-ieu0ro"
52
+ */
53
+ projectId?: string;
54
+
55
+ /**
56
+ * Cache TTL in milliseconds
57
+ * @default 300000 (5 minutes)
58
+ */
59
+ cacheTtl?: number;
60
+
61
+ /**
62
+ * Local/default configuration (LOWEST priority after defaults)
63
+ *
64
+ * This config serves as a base that gets OVERRIDDEN by remote config.
65
+ * Use this for development defaults or project-specific base settings.
66
+ *
67
+ * Precedence order (highest to lowest):
68
+ * 1. Remote config (AgentShield API) - WINS
69
+ * 2. Local config (this field)
70
+ * 3. Framework defaults
71
+ *
72
+ * @optional
73
+ */
74
+ localConfig?: ToolProtectionConfig;
75
+
76
+ /**
77
+ * Offline fallback configuration (ONLY used when API is unavailable)
78
+ *
79
+ * Unlike localConfig, this is NOT merged with remote config.
80
+ * It's only used as a complete replacement when the API cannot be reached.
81
+ *
82
+ * Fallback order when API fails:
83
+ * 1. Stale cache (if allowStaleCache=true and within maxStaleCacheAge)
84
+ * 2. This offlineFallbackConfig
85
+ * 3. failSafeBehavior (deny-all or allow-all)
86
+ *
87
+ * @optional
88
+ */
89
+ offlineFallbackConfig?: ToolProtectionConfig;
90
+
91
+ /**
92
+ * Fail-safe behavior when API is unavailable and no offline fallback is provided
93
+ * - 'deny-all': Block all tools by default (secure, recommended for production)
94
+ * - 'allow-all': Allow all tools (insecure, not recommended for production)
95
+ * @default 'deny-all'
96
+ */
97
+ failSafeBehavior?: "deny-all" | "allow-all";
98
+
99
+ /**
100
+ * Maximum age (in milliseconds) for stale cache to be used during outages
101
+ * Stale cache is used when API fails and no fallback is provided
102
+ * @default 86400000 (24 hours)
103
+ */
104
+ maxStaleCacheAge?: number;
105
+
106
+ /**
107
+ * Allow using stale cache during outages
108
+ * @default true
109
+ */
110
+ allowStaleCache?: boolean;
111
+
112
+ /**
113
+ * Enable debug logging
114
+ * @default false
115
+ */
116
+ debug?: boolean;
117
+ }
118
+
119
+ /**
120
+ * API response from /api/v1/bouncer/projects/{projectId}/tool-protections
121
+ */
122
+ export interface ToolProtectionApiResponse {
123
+ success: boolean;
124
+ data: {
125
+ toolProtections: Record<string, ToolProtection>;
126
+ };
127
+ metadata: {
128
+ timestamp: string;
129
+ cachedUntil: string;
130
+ };
131
+ }
132
+
133
+ /**
134
+ * Error thrown when a tool requires delegation but none is provided
135
+ *
136
+ * Note: Resume token generation is handled by MCPIRuntimeBase.generateResumeToken().
137
+ * The runtime should generate the token and pass it to this constructor.
138
+ */
139
+ export class DelegationRequiredError extends Error {
140
+ public resumeToken?: string;
141
+ public interceptedCall?: InterceptedToolCall;
142
+
143
+ constructor(
144
+ public toolName: string,
145
+ public requiredScopes: string[],
146
+ public consentUrl?: string,
147
+ interceptedCall?: InterceptedToolCall,
148
+ resumeToken?: string
149
+ ) {
150
+ super(`Delegation required for tool "${toolName}"`);
151
+ this.name = "DelegationRequiredError";
152
+ this.interceptedCall = interceptedCall;
153
+ this.resumeToken = resumeToken;
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Context for an intercepted tool call that needs authorization
159
+ */
160
+ export interface InterceptedToolCall {
161
+ toolName: string;
162
+ args: any;
163
+ sessionId: string;
164
+ timestamp: number;
165
+ expiresAt: number;
166
+ }
167
+
168
+ /**
169
+ * Error thrown when the tool protection service fails
170
+ */
171
+ export class ToolProtectionServiceError extends Error {
172
+ constructor(
173
+ message: string,
174
+ public cause?: Error
175
+ ) {
176
+ super(message);
177
+ this.name = "ToolProtectionServiceError";
178
+ }
179
+ }
@@ -0,0 +1,281 @@
1
+ /**
2
+ * Tests for Base58 Utilities
3
+ */
4
+
5
+ import { describe, it, expect } from "vitest";
6
+ import { base58Encode, base58Decode, isValidBase58 } from "../base58.js";
7
+
8
+ describe("Base58 Utilities", () => {
9
+ describe("base58Encode", () => {
10
+ it("should encode bytes to Base58", () => {
11
+ const bytes = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
12
+ const encoded = base58Encode(bytes);
13
+
14
+ expect(encoded).toBeDefined();
15
+ expect(typeof encoded).toBe("string");
16
+ expect(encoded.length).toBeGreaterThan(0);
17
+ });
18
+
19
+ it("should encode empty bytes to empty string", () => {
20
+ const bytes = new Uint8Array(0);
21
+ const encoded = base58Encode(bytes);
22
+
23
+ expect(encoded).toBe("");
24
+ });
25
+
26
+ it("should encode single byte", () => {
27
+ const bytes = new Uint8Array([42]);
28
+ const encoded = base58Encode(bytes);
29
+
30
+ expect(encoded).toBeDefined();
31
+ expect(encoded.length).toBeGreaterThan(0);
32
+ });
33
+
34
+ it("should encode bytes with leading zeros", () => {
35
+ const bytes = new Uint8Array([0, 0, 1, 2, 3]);
36
+ const encoded = base58Encode(bytes);
37
+
38
+ // Leading zeros should be encoded as '1'
39
+ expect(encoded.startsWith("1")).toBe(true);
40
+ });
41
+
42
+ it("should encode all zero bytes", () => {
43
+ const bytes = new Uint8Array([0, 0, 0]);
44
+ const encoded = base58Encode(bytes);
45
+
46
+ expect(encoded).toBeDefined();
47
+ // All zeros should result in multiple '1' characters
48
+ expect(encoded.length).toBeGreaterThan(0);
49
+ });
50
+
51
+ it("should produce deterministic output", () => {
52
+ const bytes = new Uint8Array([1, 2, 3, 4, 5]);
53
+ const encoded1 = base58Encode(bytes);
54
+ const encoded2 = base58Encode(bytes);
55
+
56
+ expect(encoded1).toBe(encoded2);
57
+ });
58
+ });
59
+
60
+ describe("base58Decode", () => {
61
+ it("should decode Base58 to bytes", () => {
62
+ const original = new Uint8Array([72, 101, 108, 108, 111]);
63
+ const encoded = base58Encode(original);
64
+ const decoded = base58Decode(encoded);
65
+
66
+ expect(decoded).toEqual(original);
67
+ });
68
+
69
+ it("should decode empty string to empty bytes", () => {
70
+ const decoded = base58Decode("");
71
+
72
+ expect(decoded).toEqual(new Uint8Array(0));
73
+ });
74
+
75
+ it("should decode string with leading '1' characters", () => {
76
+ // Leading '1' characters represent leading zeros
77
+ const encoded = "11ABC";
78
+ const decoded = base58Decode(encoded);
79
+
80
+ expect(decoded).toBeDefined();
81
+ expect(decoded[0]).toBe(0);
82
+ expect(decoded[1]).toBe(0);
83
+ });
84
+
85
+ it("should round-trip encode and decode", () => {
86
+ const testCases = [
87
+ new Uint8Array([1, 2, 3]),
88
+ new Uint8Array([255, 255, 255]),
89
+ new Uint8Array([0, 1, 2]),
90
+ new Uint8Array([128, 64, 32]),
91
+ ];
92
+
93
+ for (const bytes of testCases) {
94
+ const encoded = base58Encode(bytes);
95
+ const decoded = base58Decode(encoded);
96
+
97
+ expect(decoded).toEqual(bytes);
98
+ }
99
+ });
100
+
101
+ it("should throw error for invalid Base58 character", () => {
102
+ expect(() => {
103
+ base58Decode("invalid@base58");
104
+ }).toThrow("Invalid base58 character");
105
+ });
106
+
107
+ it("should throw error for character '0'", () => {
108
+ expect(() => {
109
+ base58Decode("ABC0DEF");
110
+ }).toThrow("Invalid base58 character");
111
+ });
112
+
113
+ it("should throw error for character 'O'", () => {
114
+ expect(() => {
115
+ base58Decode("ABCODEF");
116
+ }).toThrow("Invalid base58 character");
117
+ });
118
+
119
+ it("should throw error for character 'I'", () => {
120
+ expect(() => {
121
+ base58Decode("ABCDIEF");
122
+ }).toThrow("Invalid base58 character");
123
+ });
124
+
125
+ it("should throw error for character 'l'", () => {
126
+ expect(() => {
127
+ base58Decode("ABCDlEF");
128
+ }).toThrow("Invalid base58 character");
129
+ });
130
+ });
131
+
132
+ describe("isValidBase58", () => {
133
+ it("should return true for valid Base58 string", () => {
134
+ const valid = base58Encode(new Uint8Array([1, 2, 3]));
135
+ expect(isValidBase58(valid)).toBe(true);
136
+ });
137
+
138
+ it("should return true for empty string", () => {
139
+ expect(isValidBase58("")).toBe(true);
140
+ });
141
+
142
+ it("should return false for string with invalid character '0'", () => {
143
+ expect(isValidBase58("ABC0DEF")).toBe(false);
144
+ });
145
+
146
+ it("should return false for string with invalid character 'O'", () => {
147
+ expect(isValidBase58("ABCODEF")).toBe(false);
148
+ });
149
+
150
+ it("should return false for string with invalid character 'I'", () => {
151
+ expect(isValidBase58("ABCDIEF")).toBe(false);
152
+ });
153
+
154
+ it("should return false for string with invalid character 'l'", () => {
155
+ expect(isValidBase58("ABCDlEF")).toBe(false);
156
+ });
157
+
158
+ it("should return false for string with special characters", () => {
159
+ expect(isValidBase58("ABC@DEF")).toBe(false);
160
+ expect(isValidBase58("ABC-DEF")).toBe(false);
161
+ expect(isValidBase58("ABC DEF")).toBe(false);
162
+ });
163
+
164
+ it("should return true for all valid Base58 characters", () => {
165
+ const validChars =
166
+ "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
167
+ expect(isValidBase58(validChars)).toBe(true);
168
+ });
169
+
170
+ it("should return true for string with only '1' characters", () => {
171
+ expect(isValidBase58("1111")).toBe(true);
172
+ });
173
+ });
174
+
175
+ describe("Edge Cases", () => {
176
+ it("should handle large byte arrays", () => {
177
+ const largeArray = new Uint8Array(100);
178
+ for (let i = 0; i < 100; i++) {
179
+ largeArray[i] = i % 256;
180
+ }
181
+
182
+ const encoded = base58Encode(largeArray);
183
+ const decoded = base58Decode(encoded);
184
+
185
+ expect(decoded).toEqual(largeArray);
186
+ });
187
+
188
+ it("should handle bytes with maximum values", () => {
189
+ const maxBytes = new Uint8Array([255, 255, 255, 255]);
190
+ const encoded = base58Encode(maxBytes);
191
+ const decoded = base58Decode(encoded);
192
+
193
+ expect(decoded).toEqual(maxBytes);
194
+ });
195
+
196
+ it("should handle single byte encoding/decoding", () => {
197
+ for (let i = 0; i < 256; i++) {
198
+ const bytes = new Uint8Array([i]);
199
+ const encoded = base58Encode(bytes);
200
+ const decoded = base58Decode(encoded);
201
+
202
+ expect(decoded).toEqual(bytes);
203
+ }
204
+ });
205
+
206
+ it("should handle mixed leading zeros and non-zero bytes", () => {
207
+ const testCases = [
208
+ new Uint8Array([0, 1]),
209
+ new Uint8Array([0, 0, 1]),
210
+ new Uint8Array([0, 255]),
211
+ new Uint8Array([0, 0, 0, 1, 2, 3]),
212
+ ];
213
+
214
+ for (const bytes of testCases) {
215
+ const encoded = base58Encode(bytes);
216
+ const decoded = base58Decode(encoded);
217
+
218
+ expect(decoded).toEqual(bytes);
219
+ }
220
+ });
221
+
222
+ it("should produce shorter output for small values", () => {
223
+ const small = new Uint8Array([1]);
224
+ const large = new Uint8Array([255, 255, 255]);
225
+
226
+ const smallEncoded = base58Encode(small);
227
+ const largeEncoded = base58Encode(large);
228
+
229
+ // Small values should produce shorter Base58 strings
230
+ expect(smallEncoded.length).toBeLessThan(largeEncoded.length);
231
+ });
232
+ });
233
+
234
+ describe("Real-world DID:key Examples", () => {
235
+ it("should encode/decode Ed25519 public key bytes", () => {
236
+ // Example Ed25519 public key (32 bytes)
237
+ const ed25519Key = new Uint8Array([
238
+ 0xed,
239
+ 0x01, // Ed25519 multicodec prefix
240
+ 0x12,
241
+ 0x34,
242
+ 0x56,
243
+ 0x78,
244
+ 0x9a,
245
+ 0xbc,
246
+ 0xde,
247
+ 0xf0,
248
+ 0x12,
249
+ 0x34,
250
+ 0x56,
251
+ 0x78,
252
+ 0x9a,
253
+ 0xbc,
254
+ 0xde,
255
+ 0xf0,
256
+ 0x12,
257
+ 0x34,
258
+ 0x56,
259
+ 0x78,
260
+ 0x9a,
261
+ 0xbc,
262
+ 0xde,
263
+ 0xf0,
264
+ 0x12,
265
+ 0x34,
266
+ 0x56,
267
+ 0x78,
268
+ 0x9a,
269
+ 0xbc,
270
+ 0xde,
271
+ 0xf0,
272
+ ]);
273
+
274
+ const encoded = base58Encode(ed25519Key);
275
+ expect(isValidBase58(encoded)).toBe(true);
276
+
277
+ const decoded = base58Decode(encoded);
278
+ expect(decoded).toEqual(ed25519Key);
279
+ });
280
+ });
281
+ });
@@ -0,0 +1,239 @@
1
+ /**
2
+ * Tests for Base64URL Utilities
3
+ */
4
+
5
+ import { describe, it, expect } from "vitest";
6
+ import {
7
+ base64urlDecodeToString,
8
+ base64urlDecodeToBytes,
9
+ base64urlEncodeFromString,
10
+ base64urlEncodeFromBytes,
11
+ bytesToBase64,
12
+ base64ToBytes,
13
+ } from "../base64.js";
14
+
15
+ describe("Base64URL Utilities", () => {
16
+ describe("base64urlEncodeFromString", () => {
17
+ it("should encode string to base64url", () => {
18
+ const input = "Hello, World!";
19
+ const encoded = base64urlEncodeFromString(input);
20
+
21
+ expect(encoded).toBeDefined();
22
+ expect(encoded).not.toContain("+");
23
+ expect(encoded).not.toContain("/");
24
+ expect(encoded).not.toContain("=");
25
+ });
26
+
27
+ it("should encode empty string", () => {
28
+ const encoded = base64urlEncodeFromString("");
29
+ expect(encoded).toBe("");
30
+ });
31
+
32
+ it("should encode special characters", () => {
33
+ const input = "test@example.com";
34
+ const encoded = base64urlEncodeFromString(input);
35
+ const decoded = base64urlDecodeToString(encoded);
36
+
37
+ expect(decoded).toBe(input);
38
+ });
39
+
40
+ it("should encode Unicode characters", () => {
41
+ // In Node.js, Buffer.from handles Unicode correctly
42
+ // btoa doesn't handle Unicode, but our function falls back to Buffer
43
+ // Test with characters that work in both paths
44
+ const input = "Hello 世界 World";
45
+
46
+ // This should work in Node.js (Buffer path)
47
+ const encoded = base64urlEncodeFromString(input);
48
+ const decoded = base64urlDecodeToString(encoded);
49
+
50
+ expect(decoded).toBe(input);
51
+ });
52
+ });
53
+
54
+ describe("base64urlDecodeToString", () => {
55
+ it("should decode base64url string", () => {
56
+ const original = "Hello, World!";
57
+ const encoded = base64urlEncodeFromString(original);
58
+ const decoded = base64urlDecodeToString(encoded);
59
+
60
+ expect(decoded).toBe(original);
61
+ });
62
+
63
+ it("should decode string without padding", () => {
64
+ // Base64URL strings don't have padding
65
+ const encoded = "SGVsbG8gV29ybGQ"; // "Hello World" in base64url
66
+ const decoded = base64urlDecodeToString(encoded);
67
+
68
+ expect(decoded).toBe("Hello World");
69
+ });
70
+
71
+ it("should decode string with padding", () => {
72
+ // Even if padding is present, it should work
73
+ const encoded = "SGVsbG8="; // "Hello" with padding
74
+ const decoded = base64urlDecodeToString(encoded);
75
+
76
+ expect(decoded).toBe("Hello");
77
+ });
78
+
79
+ it("should throw error for invalid base64url string", () => {
80
+ expect(() => {
81
+ base64urlDecodeToString("invalid-base64!!!");
82
+ }).toThrow("Invalid base64url string");
83
+ });
84
+
85
+ it("should throw error for string with invalid characters", () => {
86
+ expect(() => {
87
+ base64urlDecodeToString("SGVsbG8@World");
88
+ }).toThrow("Invalid base64url string");
89
+ });
90
+
91
+ it("should handle empty string", () => {
92
+ const decoded = base64urlDecodeToString("");
93
+ expect(decoded).toBe("");
94
+ });
95
+ });
96
+
97
+ describe("base64urlEncodeFromBytes", () => {
98
+ it("should encode Uint8Array to base64url", () => {
99
+ const bytes = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
100
+ const encoded = base64urlEncodeFromBytes(bytes);
101
+
102
+ expect(encoded).toBeDefined();
103
+ expect(encoded).not.toContain("+");
104
+ expect(encoded).not.toContain("/");
105
+ expect(encoded).not.toContain("=");
106
+ });
107
+
108
+ it("should encode empty Uint8Array", () => {
109
+ const bytes = new Uint8Array(0);
110
+ const encoded = base64urlEncodeFromBytes(bytes);
111
+ expect(encoded).toBe("");
112
+ });
113
+
114
+ it("should round-trip encode and decode", () => {
115
+ const original = new Uint8Array([1, 2, 3, 4, 5, 255]);
116
+ const encoded = base64urlEncodeFromBytes(original);
117
+ const decoded = base64urlDecodeToBytes(encoded);
118
+
119
+ expect(decoded).toEqual(original);
120
+ });
121
+ });
122
+
123
+ describe("base64urlDecodeToBytes", () => {
124
+ it("should decode base64url to Uint8Array", () => {
125
+ const original = new Uint8Array([72, 101, 108, 108, 111]);
126
+ const encoded = base64urlEncodeFromBytes(original);
127
+ const decoded = base64urlDecodeToBytes(encoded);
128
+
129
+ expect(decoded).toEqual(original);
130
+ });
131
+
132
+ it("should decode without padding", () => {
133
+ const encoded = "SGVsbG8"; // "Hello" without padding
134
+ const decoded = base64urlDecodeToBytes(encoded);
135
+
136
+ expect(decoded).toEqual(new Uint8Array([72, 101, 108, 108, 111]));
137
+ });
138
+
139
+ it("should throw error for invalid base64url", () => {
140
+ expect(() => {
141
+ base64urlDecodeToBytes("invalid!!!");
142
+ }).toThrow("Invalid base64url string");
143
+ });
144
+
145
+ it("should handle empty string", () => {
146
+ const decoded = base64urlDecodeToBytes("");
147
+ expect(decoded).toEqual(new Uint8Array(0));
148
+ });
149
+ });
150
+
151
+ describe("bytesToBase64", () => {
152
+ it("should convert bytes to standard base64", () => {
153
+ const bytes = new Uint8Array([72, 101, 108, 108, 111]);
154
+ const base64 = bytesToBase64(bytes);
155
+
156
+ expect(base64).toBeDefined();
157
+ expect(base64).toBe("SGVsbG8="); // Standard base64 includes padding
158
+ });
159
+
160
+ it("should handle empty bytes", () => {
161
+ const bytes = new Uint8Array(0);
162
+ const base64 = bytesToBase64(bytes);
163
+ expect(base64).toBe("");
164
+ });
165
+
166
+ it("should include padding when needed", () => {
167
+ const bytes = new Uint8Array([72]); // Single byte
168
+ const base64 = bytesToBase64(bytes);
169
+ expect(base64).toContain("="); // Should have padding
170
+ });
171
+ });
172
+
173
+ describe("base64ToBytes", () => {
174
+ it("should convert standard base64 to bytes", () => {
175
+ const base64 = "SGVsbG8="; // "Hello" in standard base64
176
+ const bytes = base64ToBytes(base64);
177
+
178
+ expect(bytes).toEqual(new Uint8Array([72, 101, 108, 108, 111]));
179
+ });
180
+
181
+ it("should handle base64 without padding", () => {
182
+ const base64 = "SGVsbG8"; // Without padding
183
+ const bytes = base64ToBytes(base64);
184
+
185
+ expect(bytes).toBeDefined();
186
+ });
187
+
188
+ it("should handle empty string", () => {
189
+ const bytes = base64ToBytes("");
190
+ expect(bytes).toEqual(new Uint8Array(0));
191
+ });
192
+
193
+ it("should round-trip with bytesToBase64", () => {
194
+ const original = new Uint8Array([1, 2, 3, 255, 0]);
195
+ const base64 = bytesToBase64(original);
196
+ const decoded = base64ToBytes(base64);
197
+
198
+ expect(decoded).toEqual(original);
199
+ });
200
+ });
201
+
202
+ describe("Edge Cases", () => {
203
+ it("should handle large byte arrays", () => {
204
+ const largeArray = new Uint8Array(1000);
205
+ for (let i = 0; i < 1000; i++) {
206
+ largeArray[i] = i % 256;
207
+ }
208
+
209
+ const encoded = base64urlEncodeFromBytes(largeArray);
210
+ const decoded = base64urlDecodeToBytes(encoded);
211
+
212
+ expect(decoded).toEqual(largeArray);
213
+ });
214
+
215
+ it("should handle bytes with all zeros", () => {
216
+ const zeros = new Uint8Array([0, 0, 0, 0]);
217
+ const encoded = base64urlEncodeFromBytes(zeros);
218
+ const decoded = base64urlDecodeToBytes(encoded);
219
+
220
+ expect(decoded).toEqual(zeros);
221
+ });
222
+
223
+ it("should handle bytes with all 255s", () => {
224
+ const maxBytes = new Uint8Array([255, 255, 255, 255]);
225
+ const encoded = base64urlEncodeFromBytes(maxBytes);
226
+ const decoded = base64urlDecodeToBytes(encoded);
227
+
228
+ expect(decoded).toEqual(maxBytes);
229
+ });
230
+
231
+ it("should handle single byte", () => {
232
+ const single = new Uint8Array([42]);
233
+ const encoded = base64urlEncodeFromBytes(single);
234
+ const decoded = base64urlDecodeToBytes(encoded);
235
+
236
+ expect(decoded).toEqual(single);
237
+ });
238
+ });
239
+ });