palaryn 0.1.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 (607) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +716 -0
  3. package/dist/sdk/typescript/src/client.d.ts +71 -0
  4. package/dist/sdk/typescript/src/client.d.ts.map +1 -0
  5. package/dist/sdk/typescript/src/client.js +176 -0
  6. package/dist/sdk/typescript/src/client.js.map +1 -0
  7. package/dist/sdk/typescript/src/errors.d.ts +50 -0
  8. package/dist/sdk/typescript/src/errors.d.ts.map +1 -0
  9. package/dist/sdk/typescript/src/errors.js +103 -0
  10. package/dist/sdk/typescript/src/errors.js.map +1 -0
  11. package/dist/sdk/typescript/src/index.d.ts +4 -0
  12. package/dist/sdk/typescript/src/index.d.ts.map +1 -0
  13. package/dist/sdk/typescript/src/index.js +15 -0
  14. package/dist/sdk/typescript/src/index.js.map +1 -0
  15. package/dist/sdk/typescript/src/types.d.ts +101 -0
  16. package/dist/sdk/typescript/src/types.d.ts.map +1 -0
  17. package/dist/sdk/typescript/src/types.js +6 -0
  18. package/dist/sdk/typescript/src/types.js.map +1 -0
  19. package/dist/src/admin/index.d.ts +2 -0
  20. package/dist/src/admin/index.d.ts.map +1 -0
  21. package/dist/src/admin/index.js +6 -0
  22. package/dist/src/admin/index.js.map +1 -0
  23. package/dist/src/admin/routes.d.ts +5 -0
  24. package/dist/src/admin/routes.d.ts.map +1 -0
  25. package/dist/src/admin/routes.js +471 -0
  26. package/dist/src/admin/routes.js.map +1 -0
  27. package/dist/src/admin/templates.d.ts +51 -0
  28. package/dist/src/admin/templates.d.ts.map +1 -0
  29. package/dist/src/admin/templates.js +500 -0
  30. package/dist/src/admin/templates.js.map +1 -0
  31. package/dist/src/anomaly/detector.d.ts +141 -0
  32. package/dist/src/anomaly/detector.d.ts.map +1 -0
  33. package/dist/src/anomaly/detector.js +554 -0
  34. package/dist/src/anomaly/detector.js.map +1 -0
  35. package/dist/src/anomaly/index.d.ts +2 -0
  36. package/dist/src/anomaly/index.d.ts.map +1 -0
  37. package/dist/src/anomaly/index.js +7 -0
  38. package/dist/src/anomaly/index.js.map +1 -0
  39. package/dist/src/approval/manager.d.ts +147 -0
  40. package/dist/src/approval/manager.d.ts.map +1 -0
  41. package/dist/src/approval/manager.js +511 -0
  42. package/dist/src/approval/manager.js.map +1 -0
  43. package/dist/src/approval/webhook.d.ts +36 -0
  44. package/dist/src/approval/webhook.d.ts.map +1 -0
  45. package/dist/src/approval/webhook.js +135 -0
  46. package/dist/src/approval/webhook.js.map +1 -0
  47. package/dist/src/audit/logger.d.ts +70 -0
  48. package/dist/src/audit/logger.d.ts.map +1 -0
  49. package/dist/src/audit/logger.js +440 -0
  50. package/dist/src/audit/logger.js.map +1 -0
  51. package/dist/src/auth/index.d.ts +6 -0
  52. package/dist/src/auth/index.d.ts.map +1 -0
  53. package/dist/src/auth/index.js +22 -0
  54. package/dist/src/auth/index.js.map +1 -0
  55. package/dist/src/auth/password.d.ts +3 -0
  56. package/dist/src/auth/password.d.ts.map +1 -0
  57. package/dist/src/auth/password.js +25 -0
  58. package/dist/src/auth/password.js.map +1 -0
  59. package/dist/src/auth/pkce.d.ts +13 -0
  60. package/dist/src/auth/pkce.d.ts.map +1 -0
  61. package/dist/src/auth/pkce.js +58 -0
  62. package/dist/src/auth/pkce.js.map +1 -0
  63. package/dist/src/auth/providers.d.ts +28 -0
  64. package/dist/src/auth/providers.d.ts.map +1 -0
  65. package/dist/src/auth/providers.js +198 -0
  66. package/dist/src/auth/providers.js.map +1 -0
  67. package/dist/src/auth/routes.d.ts +14 -0
  68. package/dist/src/auth/routes.d.ts.map +1 -0
  69. package/dist/src/auth/routes.js +431 -0
  70. package/dist/src/auth/routes.js.map +1 -0
  71. package/dist/src/auth/session.d.ts +24 -0
  72. package/dist/src/auth/session.d.ts.map +1 -0
  73. package/dist/src/auth/session.js +105 -0
  74. package/dist/src/auth/session.js.map +1 -0
  75. package/dist/src/billing/index.d.ts +7 -0
  76. package/dist/src/billing/index.d.ts.map +1 -0
  77. package/dist/src/billing/index.js +14 -0
  78. package/dist/src/billing/index.js.map +1 -0
  79. package/dist/src/billing/plan-enforcer.d.ts +44 -0
  80. package/dist/src/billing/plan-enforcer.d.ts.map +1 -0
  81. package/dist/src/billing/plan-enforcer.js +110 -0
  82. package/dist/src/billing/plan-enforcer.js.map +1 -0
  83. package/dist/src/billing/routes.d.ts +15 -0
  84. package/dist/src/billing/routes.d.ts.map +1 -0
  85. package/dist/src/billing/routes.js +193 -0
  86. package/dist/src/billing/routes.js.map +1 -0
  87. package/dist/src/billing/stripe-client.d.ts +14 -0
  88. package/dist/src/billing/stripe-client.d.ts.map +1 -0
  89. package/dist/src/billing/stripe-client.js +51 -0
  90. package/dist/src/billing/stripe-client.js.map +1 -0
  91. package/dist/src/billing/webhook-handler.d.ts +19 -0
  92. package/dist/src/billing/webhook-handler.d.ts.map +1 -0
  93. package/dist/src/billing/webhook-handler.js +169 -0
  94. package/dist/src/billing/webhook-handler.js.map +1 -0
  95. package/dist/src/billing/webhook-routes.d.ts +5 -0
  96. package/dist/src/billing/webhook-routes.d.ts.map +1 -0
  97. package/dist/src/billing/webhook-routes.js +30 -0
  98. package/dist/src/billing/webhook-routes.js.map +1 -0
  99. package/dist/src/budget/manager.d.ts +95 -0
  100. package/dist/src/budget/manager.d.ts.map +1 -0
  101. package/dist/src/budget/manager.js +547 -0
  102. package/dist/src/budget/manager.js.map +1 -0
  103. package/dist/src/budget/usage-extractor.d.ts +38 -0
  104. package/dist/src/budget/usage-extractor.d.ts.map +1 -0
  105. package/dist/src/budget/usage-extractor.js +165 -0
  106. package/dist/src/budget/usage-extractor.js.map +1 -0
  107. package/dist/src/cli.d.ts +3 -0
  108. package/dist/src/cli.d.ts.map +1 -0
  109. package/dist/src/cli.js +115 -0
  110. package/dist/src/cli.js.map +1 -0
  111. package/dist/src/config/defaults.d.ts +3 -0
  112. package/dist/src/config/defaults.d.ts.map +1 -0
  113. package/dist/src/config/defaults.js +243 -0
  114. package/dist/src/config/defaults.js.map +1 -0
  115. package/dist/src/config/validate.d.ts +15 -0
  116. package/dist/src/config/validate.d.ts.map +1 -0
  117. package/dist/src/config/validate.js +105 -0
  118. package/dist/src/config/validate.js.map +1 -0
  119. package/dist/src/dlp/composite-scanner.d.ts +47 -0
  120. package/dist/src/dlp/composite-scanner.d.ts.map +1 -0
  121. package/dist/src/dlp/composite-scanner.js +186 -0
  122. package/dist/src/dlp/composite-scanner.js.map +1 -0
  123. package/dist/src/dlp/index.d.ts +10 -0
  124. package/dist/src/dlp/index.d.ts.map +1 -0
  125. package/dist/src/dlp/index.js +26 -0
  126. package/dist/src/dlp/index.js.map +1 -0
  127. package/dist/src/dlp/interfaces.d.ts +33 -0
  128. package/dist/src/dlp/interfaces.d.ts.map +1 -0
  129. package/dist/src/dlp/interfaces.js +3 -0
  130. package/dist/src/dlp/interfaces.js.map +1 -0
  131. package/dist/src/dlp/patterns.d.ts +9 -0
  132. package/dist/src/dlp/patterns.d.ts.map +1 -0
  133. package/dist/src/dlp/patterns.js +25 -0
  134. package/dist/src/dlp/patterns.js.map +1 -0
  135. package/dist/src/dlp/prompt-injection-backend.d.ts +68 -0
  136. package/dist/src/dlp/prompt-injection-backend.d.ts.map +1 -0
  137. package/dist/src/dlp/prompt-injection-backend.js +148 -0
  138. package/dist/src/dlp/prompt-injection-backend.js.map +1 -0
  139. package/dist/src/dlp/prompt-injection-patterns.d.ts +32 -0
  140. package/dist/src/dlp/prompt-injection-patterns.d.ts.map +1 -0
  141. package/dist/src/dlp/prompt-injection-patterns.js +290 -0
  142. package/dist/src/dlp/prompt-injection-patterns.js.map +1 -0
  143. package/dist/src/dlp/regex-backend.d.ts +32 -0
  144. package/dist/src/dlp/regex-backend.d.ts.map +1 -0
  145. package/dist/src/dlp/regex-backend.js +153 -0
  146. package/dist/src/dlp/regex-backend.js.map +1 -0
  147. package/dist/src/dlp/scanner.d.ts +122 -0
  148. package/dist/src/dlp/scanner.d.ts.map +1 -0
  149. package/dist/src/dlp/scanner.js +444 -0
  150. package/dist/src/dlp/scanner.js.map +1 -0
  151. package/dist/src/dlp/text-normalizer.d.ts +41 -0
  152. package/dist/src/dlp/text-normalizer.d.ts.map +1 -0
  153. package/dist/src/dlp/text-normalizer.js +203 -0
  154. package/dist/src/dlp/text-normalizer.js.map +1 -0
  155. package/dist/src/dlp/trufflehog-backend.d.ts +64 -0
  156. package/dist/src/dlp/trufflehog-backend.d.ts.map +1 -0
  157. package/dist/src/dlp/trufflehog-backend.js +151 -0
  158. package/dist/src/dlp/trufflehog-backend.js.map +1 -0
  159. package/dist/src/executor/http-executor.d.ts +25 -0
  160. package/dist/src/executor/http-executor.d.ts.map +1 -0
  161. package/dist/src/executor/http-executor.js +333 -0
  162. package/dist/src/executor/http-executor.js.map +1 -0
  163. package/dist/src/executor/index.d.ts +6 -0
  164. package/dist/src/executor/index.d.ts.map +1 -0
  165. package/dist/src/executor/index.js +12 -0
  166. package/dist/src/executor/index.js.map +1 -0
  167. package/dist/src/executor/interfaces.d.ts +11 -0
  168. package/dist/src/executor/interfaces.d.ts.map +1 -0
  169. package/dist/src/executor/interfaces.js +3 -0
  170. package/dist/src/executor/interfaces.js.map +1 -0
  171. package/dist/src/executor/noop-executor.d.ts +13 -0
  172. package/dist/src/executor/noop-executor.d.ts.map +1 -0
  173. package/dist/src/executor/noop-executor.js +21 -0
  174. package/dist/src/executor/noop-executor.js.map +1 -0
  175. package/dist/src/executor/registry.d.ts +30 -0
  176. package/dist/src/executor/registry.d.ts.map +1 -0
  177. package/dist/src/executor/registry.js +62 -0
  178. package/dist/src/executor/registry.js.map +1 -0
  179. package/dist/src/executor/slack-executor.d.ts +24 -0
  180. package/dist/src/executor/slack-executor.d.ts.map +1 -0
  181. package/dist/src/executor/slack-executor.js +147 -0
  182. package/dist/src/executor/slack-executor.js.map +1 -0
  183. package/dist/src/index.d.ts +25 -0
  184. package/dist/src/index.d.ts.map +1 -0
  185. package/dist/src/index.js +74 -0
  186. package/dist/src/index.js.map +1 -0
  187. package/dist/src/mcp/auth-verifier.d.ts +23 -0
  188. package/dist/src/mcp/auth-verifier.d.ts.map +1 -0
  189. package/dist/src/mcp/auth-verifier.js +162 -0
  190. package/dist/src/mcp/auth-verifier.js.map +1 -0
  191. package/dist/src/mcp/bridge.d.ts +132 -0
  192. package/dist/src/mcp/bridge.d.ts.map +1 -0
  193. package/dist/src/mcp/bridge.js +734 -0
  194. package/dist/src/mcp/bridge.js.map +1 -0
  195. package/dist/src/mcp/http-transport.d.ts +32 -0
  196. package/dist/src/mcp/http-transport.d.ts.map +1 -0
  197. package/dist/src/mcp/http-transport.js +538 -0
  198. package/dist/src/mcp/http-transport.js.map +1 -0
  199. package/dist/src/mcp/index.d.ts +10 -0
  200. package/dist/src/mcp/index.d.ts.map +1 -0
  201. package/dist/src/mcp/index.js +17 -0
  202. package/dist/src/mcp/index.js.map +1 -0
  203. package/dist/src/mcp/oauth-pages.d.ts +23 -0
  204. package/dist/src/mcp/oauth-pages.d.ts.map +1 -0
  205. package/dist/src/mcp/oauth-pages.js +121 -0
  206. package/dist/src/mcp/oauth-pages.js.map +1 -0
  207. package/dist/src/mcp/oauth-postgres-stores.d.ts +55 -0
  208. package/dist/src/mcp/oauth-postgres-stores.d.ts.map +1 -0
  209. package/dist/src/mcp/oauth-postgres-stores.js +226 -0
  210. package/dist/src/mcp/oauth-postgres-stores.js.map +1 -0
  211. package/dist/src/mcp/oauth-provider.d.ts +95 -0
  212. package/dist/src/mcp/oauth-provider.d.ts.map +1 -0
  213. package/dist/src/mcp/oauth-provider.js +360 -0
  214. package/dist/src/mcp/oauth-provider.js.map +1 -0
  215. package/dist/src/mcp/oauth-stores.d.ts +62 -0
  216. package/dist/src/mcp/oauth-stores.d.ts.map +1 -0
  217. package/dist/src/mcp/oauth-stores.js +154 -0
  218. package/dist/src/mcp/oauth-stores.js.map +1 -0
  219. package/dist/src/mcp/server.d.ts +18 -0
  220. package/dist/src/mcp/server.d.ts.map +1 -0
  221. package/dist/src/mcp/server.js +51 -0
  222. package/dist/src/mcp/server.js.map +1 -0
  223. package/dist/src/metrics/collector.d.ts +106 -0
  224. package/dist/src/metrics/collector.d.ts.map +1 -0
  225. package/dist/src/metrics/collector.js +311 -0
  226. package/dist/src/metrics/collector.js.map +1 -0
  227. package/dist/src/metrics/index.d.ts +2 -0
  228. package/dist/src/metrics/index.d.ts.map +1 -0
  229. package/dist/src/metrics/index.js +6 -0
  230. package/dist/src/metrics/index.js.map +1 -0
  231. package/dist/src/middleware/auth.d.ts +77 -0
  232. package/dist/src/middleware/auth.d.ts.map +1 -0
  233. package/dist/src/middleware/auth.js +720 -0
  234. package/dist/src/middleware/auth.js.map +1 -0
  235. package/dist/src/middleware/session.d.ts +18 -0
  236. package/dist/src/middleware/session.d.ts.map +1 -0
  237. package/dist/src/middleware/session.js +67 -0
  238. package/dist/src/middleware/session.js.map +1 -0
  239. package/dist/src/middleware/validate.d.ts +3 -0
  240. package/dist/src/middleware/validate.d.ts.map +1 -0
  241. package/dist/src/middleware/validate.js +85 -0
  242. package/dist/src/middleware/validate.js.map +1 -0
  243. package/dist/src/policy/engine.d.ts +107 -0
  244. package/dist/src/policy/engine.d.ts.map +1 -0
  245. package/dist/src/policy/engine.js +646 -0
  246. package/dist/src/policy/engine.js.map +1 -0
  247. package/dist/src/policy/index.d.ts +3 -0
  248. package/dist/src/policy/index.d.ts.map +1 -0
  249. package/dist/src/policy/index.js +8 -0
  250. package/dist/src/policy/index.js.map +1 -0
  251. package/dist/src/policy/opa-engine.d.ts +176 -0
  252. package/dist/src/policy/opa-engine.d.ts.map +1 -0
  253. package/dist/src/policy/opa-engine.js +790 -0
  254. package/dist/src/policy/opa-engine.js.map +1 -0
  255. package/dist/src/proxy/forward-proxy.d.ts +30 -0
  256. package/dist/src/proxy/forward-proxy.d.ts.map +1 -0
  257. package/dist/src/proxy/forward-proxy.js +580 -0
  258. package/dist/src/proxy/forward-proxy.js.map +1 -0
  259. package/dist/src/proxy/index.d.ts +2 -0
  260. package/dist/src/proxy/index.d.ts.map +1 -0
  261. package/dist/src/proxy/index.js +8 -0
  262. package/dist/src/proxy/index.js.map +1 -0
  263. package/dist/src/ratelimit/limiter.d.ts +45 -0
  264. package/dist/src/ratelimit/limiter.d.ts.map +1 -0
  265. package/dist/src/ratelimit/limiter.js +158 -0
  266. package/dist/src/ratelimit/limiter.js.map +1 -0
  267. package/dist/src/replay/engine.d.ts +40 -0
  268. package/dist/src/replay/engine.d.ts.map +1 -0
  269. package/dist/src/replay/engine.js +106 -0
  270. package/dist/src/replay/engine.js.map +1 -0
  271. package/dist/src/replay/index.d.ts +2 -0
  272. package/dist/src/replay/index.d.ts.map +1 -0
  273. package/dist/src/replay/index.js +6 -0
  274. package/dist/src/replay/index.js.map +1 -0
  275. package/dist/src/saas/index.d.ts +2 -0
  276. package/dist/src/saas/index.d.ts.map +1 -0
  277. package/dist/src/saas/index.js +18 -0
  278. package/dist/src/saas/index.js.map +1 -0
  279. package/dist/src/saas/routes.d.ts +18 -0
  280. package/dist/src/saas/routes.d.ts.map +1 -0
  281. package/dist/src/saas/routes.js +1566 -0
  282. package/dist/src/saas/routes.js.map +1 -0
  283. package/dist/src/server/app.d.ts +44 -0
  284. package/dist/src/server/app.d.ts.map +1 -0
  285. package/dist/src/server/app.js +854 -0
  286. package/dist/src/server/app.js.map +1 -0
  287. package/dist/src/server/errors.d.ts +32 -0
  288. package/dist/src/server/errors.d.ts.map +1 -0
  289. package/dist/src/server/errors.js +39 -0
  290. package/dist/src/server/errors.js.map +1 -0
  291. package/dist/src/server/gateway.d.ts +165 -0
  292. package/dist/src/server/gateway.d.ts.map +1 -0
  293. package/dist/src/server/gateway.js +964 -0
  294. package/dist/src/server/gateway.js.map +1 -0
  295. package/dist/src/server/index.d.ts +2 -0
  296. package/dist/src/server/index.d.ts.map +1 -0
  297. package/dist/src/server/index.js +295 -0
  298. package/dist/src/server/index.js.map +1 -0
  299. package/dist/src/server/logger.d.ts +33 -0
  300. package/dist/src/server/logger.d.ts.map +1 -0
  301. package/dist/src/server/logger.js +230 -0
  302. package/dist/src/server/logger.js.map +1 -0
  303. package/dist/src/server/stream-proxy.d.ts +32 -0
  304. package/dist/src/server/stream-proxy.d.ts.map +1 -0
  305. package/dist/src/server/stream-proxy.js +184 -0
  306. package/dist/src/server/stream-proxy.js.map +1 -0
  307. package/dist/src/storage/file-persistence.d.ts +48 -0
  308. package/dist/src/storage/file-persistence.d.ts.map +1 -0
  309. package/dist/src/storage/file-persistence.js +280 -0
  310. package/dist/src/storage/file-persistence.js.map +1 -0
  311. package/dist/src/storage/index.d.ts +5 -0
  312. package/dist/src/storage/index.d.ts.map +1 -0
  313. package/dist/src/storage/index.js +21 -0
  314. package/dist/src/storage/index.js.map +1 -0
  315. package/dist/src/storage/interfaces.d.ts +237 -0
  316. package/dist/src/storage/interfaces.d.ts.map +1 -0
  317. package/dist/src/storage/interfaces.js +3 -0
  318. package/dist/src/storage/interfaces.js.map +1 -0
  319. package/dist/src/storage/memory.d.ts +162 -0
  320. package/dist/src/storage/memory.d.ts.map +1 -0
  321. package/dist/src/storage/memory.js +603 -0
  322. package/dist/src/storage/memory.js.map +1 -0
  323. package/dist/src/storage/postgres.d.ts +267 -0
  324. package/dist/src/storage/postgres.d.ts.map +1 -0
  325. package/dist/src/storage/postgres.js +1555 -0
  326. package/dist/src/storage/postgres.js.map +1 -0
  327. package/dist/src/storage/redis.d.ts +202 -0
  328. package/dist/src/storage/redis.d.ts.map +1 -0
  329. package/dist/src/storage/redis.js +629 -0
  330. package/dist/src/storage/redis.js.map +1 -0
  331. package/dist/src/tracing/index.d.ts +2 -0
  332. package/dist/src/tracing/index.d.ts.map +1 -0
  333. package/dist/src/tracing/index.js +6 -0
  334. package/dist/src/tracing/index.js.map +1 -0
  335. package/dist/src/tracing/provider.d.ts +43 -0
  336. package/dist/src/tracing/provider.d.ts.map +1 -0
  337. package/dist/src/tracing/provider.js +74 -0
  338. package/dist/src/tracing/provider.js.map +1 -0
  339. package/dist/src/trust/calculator.d.ts +54 -0
  340. package/dist/src/trust/calculator.d.ts.map +1 -0
  341. package/dist/src/trust/calculator.js +102 -0
  342. package/dist/src/trust/calculator.js.map +1 -0
  343. package/dist/src/trust/index.d.ts +2 -0
  344. package/dist/src/trust/index.d.ts.map +1 -0
  345. package/dist/src/trust/index.js +7 -0
  346. package/dist/src/trust/index.js.map +1 -0
  347. package/dist/src/types/budget.d.ts +30 -0
  348. package/dist/src/types/budget.d.ts.map +1 -0
  349. package/dist/src/types/budget.js +3 -0
  350. package/dist/src/types/budget.js.map +1 -0
  351. package/dist/src/types/config.d.ts +176 -0
  352. package/dist/src/types/config.d.ts.map +1 -0
  353. package/dist/src/types/config.js +3 -0
  354. package/dist/src/types/config.js.map +1 -0
  355. package/dist/src/types/events.d.ts +24 -0
  356. package/dist/src/types/events.d.ts.map +1 -0
  357. package/dist/src/types/events.js +3 -0
  358. package/dist/src/types/events.js.map +1 -0
  359. package/dist/src/types/index.d.ts +8 -0
  360. package/dist/src/types/index.d.ts.map +1 -0
  361. package/dist/src/types/index.js +24 -0
  362. package/dist/src/types/index.js.map +1 -0
  363. package/dist/src/types/policy.d.ts +60 -0
  364. package/dist/src/types/policy.d.ts.map +1 -0
  365. package/dist/src/types/policy.js +3 -0
  366. package/dist/src/types/policy.js.map +1 -0
  367. package/dist/src/types/stripe-config.d.ts +12 -0
  368. package/dist/src/types/stripe-config.d.ts.map +1 -0
  369. package/dist/src/types/stripe-config.js +3 -0
  370. package/dist/src/types/stripe-config.js.map +1 -0
  371. package/dist/src/types/subscription.d.ts +24 -0
  372. package/dist/src/types/subscription.d.ts.map +1 -0
  373. package/dist/src/types/subscription.js +38 -0
  374. package/dist/src/types/subscription.js.map +1 -0
  375. package/dist/src/types/tool-call.d.ts +42 -0
  376. package/dist/src/types/tool-call.d.ts.map +1 -0
  377. package/dist/src/types/tool-call.js +3 -0
  378. package/dist/src/types/tool-call.js.map +1 -0
  379. package/dist/src/types/tool-result.d.ts +58 -0
  380. package/dist/src/types/tool-result.d.ts.map +1 -0
  381. package/dist/src/types/tool-result.js +3 -0
  382. package/dist/src/types/tool-result.js.map +1 -0
  383. package/dist/src/types/user.d.ts +101 -0
  384. package/dist/src/types/user.d.ts.map +1 -0
  385. package/dist/src/types/user.js +6 -0
  386. package/dist/src/types/user.js.map +1 -0
  387. package/dist/tests/integration/api.test.d.ts +2 -0
  388. package/dist/tests/integration/api.test.d.ts.map +1 -0
  389. package/dist/tests/integration/api.test.js +1199 -0
  390. package/dist/tests/integration/api.test.js.map +1 -0
  391. package/dist/tests/integration/proxy.test.d.ts +2 -0
  392. package/dist/tests/integration/proxy.test.d.ts.map +1 -0
  393. package/dist/tests/integration/proxy.test.js +251 -0
  394. package/dist/tests/integration/proxy.test.js.map +1 -0
  395. package/dist/tests/integration/storage.test.d.ts +16 -0
  396. package/dist/tests/integration/storage.test.d.ts.map +1 -0
  397. package/dist/tests/integration/storage.test.js +826 -0
  398. package/dist/tests/integration/storage.test.js.map +1 -0
  399. package/dist/tests/unit/admin.test.d.ts +2 -0
  400. package/dist/tests/unit/admin.test.d.ts.map +1 -0
  401. package/dist/tests/unit/admin.test.js +698 -0
  402. package/dist/tests/unit/admin.test.js.map +1 -0
  403. package/dist/tests/unit/anomaly-detector.test.d.ts +2 -0
  404. package/dist/tests/unit/anomaly-detector.test.d.ts.map +1 -0
  405. package/dist/tests/unit/anomaly-detector.test.js +903 -0
  406. package/dist/tests/unit/anomaly-detector.test.js.map +1 -0
  407. package/dist/tests/unit/approval-manager.test.d.ts +2 -0
  408. package/dist/tests/unit/approval-manager.test.d.ts.map +1 -0
  409. package/dist/tests/unit/approval-manager.test.js +528 -0
  410. package/dist/tests/unit/approval-manager.test.js.map +1 -0
  411. package/dist/tests/unit/approval-webhook.test.d.ts +2 -0
  412. package/dist/tests/unit/approval-webhook.test.d.ts.map +1 -0
  413. package/dist/tests/unit/approval-webhook.test.js +355 -0
  414. package/dist/tests/unit/approval-webhook.test.js.map +1 -0
  415. package/dist/tests/unit/audit-logger.test.d.ts +2 -0
  416. package/dist/tests/unit/audit-logger.test.d.ts.map +1 -0
  417. package/dist/tests/unit/audit-logger.test.js +635 -0
  418. package/dist/tests/unit/audit-logger.test.js.map +1 -0
  419. package/dist/tests/unit/auth-routes.test.d.ts +2 -0
  420. package/dist/tests/unit/auth-routes.test.d.ts.map +1 -0
  421. package/dist/tests/unit/auth-routes.test.js +281 -0
  422. package/dist/tests/unit/auth-routes.test.js.map +1 -0
  423. package/dist/tests/unit/auth.test.d.ts +2 -0
  424. package/dist/tests/unit/auth.test.d.ts.map +1 -0
  425. package/dist/tests/unit/auth.test.js +1382 -0
  426. package/dist/tests/unit/auth.test.js.map +1 -0
  427. package/dist/tests/unit/billing.test.d.ts +2 -0
  428. package/dist/tests/unit/billing.test.d.ts.map +1 -0
  429. package/dist/tests/unit/billing.test.js +579 -0
  430. package/dist/tests/unit/billing.test.js.map +1 -0
  431. package/dist/tests/unit/budget-manager.test.d.ts +2 -0
  432. package/dist/tests/unit/budget-manager.test.d.ts.map +1 -0
  433. package/dist/tests/unit/budget-manager.test.js +778 -0
  434. package/dist/tests/unit/budget-manager.test.js.map +1 -0
  435. package/dist/tests/unit/budget-race.test.d.ts +2 -0
  436. package/dist/tests/unit/budget-race.test.d.ts.map +1 -0
  437. package/dist/tests/unit/budget-race.test.js +58 -0
  438. package/dist/tests/unit/budget-race.test.js.map +1 -0
  439. package/dist/tests/unit/cli.test.d.ts +2 -0
  440. package/dist/tests/unit/cli.test.d.ts.map +1 -0
  441. package/dist/tests/unit/cli.test.js +93 -0
  442. package/dist/tests/unit/cli.test.js.map +1 -0
  443. package/dist/tests/unit/concurrency.test.d.ts +2 -0
  444. package/dist/tests/unit/concurrency.test.d.ts.map +1 -0
  445. package/dist/tests/unit/concurrency.test.js +1270 -0
  446. package/dist/tests/unit/concurrency.test.js.map +1 -0
  447. package/dist/tests/unit/config-validate.test.d.ts +2 -0
  448. package/dist/tests/unit/config-validate.test.d.ts.map +1 -0
  449. package/dist/tests/unit/config-validate.test.js +230 -0
  450. package/dist/tests/unit/config-validate.test.js.map +1 -0
  451. package/dist/tests/unit/defaults.test.d.ts +2 -0
  452. package/dist/tests/unit/defaults.test.d.ts.map +1 -0
  453. package/dist/tests/unit/defaults.test.js +364 -0
  454. package/dist/tests/unit/defaults.test.js.map +1 -0
  455. package/dist/tests/unit/dlp-backends.test.d.ts +2 -0
  456. package/dist/tests/unit/dlp-backends.test.d.ts.map +1 -0
  457. package/dist/tests/unit/dlp-backends.test.js +563 -0
  458. package/dist/tests/unit/dlp-backends.test.js.map +1 -0
  459. package/dist/tests/unit/dlp-scanner.test.d.ts +2 -0
  460. package/dist/tests/unit/dlp-scanner.test.d.ts.map +1 -0
  461. package/dist/tests/unit/dlp-scanner.test.js +739 -0
  462. package/dist/tests/unit/dlp-scanner.test.js.map +1 -0
  463. package/dist/tests/unit/error-responses.test.d.ts +2 -0
  464. package/dist/tests/unit/error-responses.test.d.ts.map +1 -0
  465. package/dist/tests/unit/error-responses.test.js +101 -0
  466. package/dist/tests/unit/error-responses.test.js.map +1 -0
  467. package/dist/tests/unit/executor-registry.test.d.ts +2 -0
  468. package/dist/tests/unit/executor-registry.test.d.ts.map +1 -0
  469. package/dist/tests/unit/executor-registry.test.js +390 -0
  470. package/dist/tests/unit/executor-registry.test.js.map +1 -0
  471. package/dist/tests/unit/forward-proxy.test.d.ts +2 -0
  472. package/dist/tests/unit/forward-proxy.test.d.ts.map +1 -0
  473. package/dist/tests/unit/forward-proxy.test.js +621 -0
  474. package/dist/tests/unit/forward-proxy.test.js.map +1 -0
  475. package/dist/tests/unit/gateway-features.test.d.ts +2 -0
  476. package/dist/tests/unit/gateway-features.test.d.ts.map +1 -0
  477. package/dist/tests/unit/gateway-features.test.js +753 -0
  478. package/dist/tests/unit/gateway-features.test.js.map +1 -0
  479. package/dist/tests/unit/http-executor.test.d.ts +2 -0
  480. package/dist/tests/unit/http-executor.test.d.ts.map +1 -0
  481. package/dist/tests/unit/http-executor.test.js +310 -0
  482. package/dist/tests/unit/http-executor.test.js.map +1 -0
  483. package/dist/tests/unit/mcp-bridge.test.d.ts +2 -0
  484. package/dist/tests/unit/mcp-bridge.test.d.ts.map +1 -0
  485. package/dist/tests/unit/mcp-bridge.test.js +1136 -0
  486. package/dist/tests/unit/mcp-bridge.test.js.map +1 -0
  487. package/dist/tests/unit/mcp-http-transport.test.d.ts +2 -0
  488. package/dist/tests/unit/mcp-http-transport.test.d.ts.map +1 -0
  489. package/dist/tests/unit/mcp-http-transport.test.js +899 -0
  490. package/dist/tests/unit/mcp-http-transport.test.js.map +1 -0
  491. package/dist/tests/unit/mcp-oauth.test.d.ts +2 -0
  492. package/dist/tests/unit/mcp-oauth.test.d.ts.map +1 -0
  493. package/dist/tests/unit/mcp-oauth.test.js +759 -0
  494. package/dist/tests/unit/mcp-oauth.test.js.map +1 -0
  495. package/dist/tests/unit/mcp-server.test.d.ts +15 -0
  496. package/dist/tests/unit/mcp-server.test.d.ts.map +1 -0
  497. package/dist/tests/unit/mcp-server.test.js +158 -0
  498. package/dist/tests/unit/mcp-server.test.js.map +1 -0
  499. package/dist/tests/unit/metrics.test.d.ts +2 -0
  500. package/dist/tests/unit/metrics.test.d.ts.map +1 -0
  501. package/dist/tests/unit/metrics.test.js +208 -0
  502. package/dist/tests/unit/metrics.test.js.map +1 -0
  503. package/dist/tests/unit/oauth.test.d.ts +2 -0
  504. package/dist/tests/unit/oauth.test.d.ts.map +1 -0
  505. package/dist/tests/unit/oauth.test.js +281 -0
  506. package/dist/tests/unit/oauth.test.js.map +1 -0
  507. package/dist/tests/unit/opa-circuit-breaker.test.d.ts +2 -0
  508. package/dist/tests/unit/opa-circuit-breaker.test.d.ts.map +1 -0
  509. package/dist/tests/unit/opa-circuit-breaker.test.js +297 -0
  510. package/dist/tests/unit/opa-circuit-breaker.test.js.map +1 -0
  511. package/dist/tests/unit/opa-engine.test.d.ts +2 -0
  512. package/dist/tests/unit/opa-engine.test.d.ts.map +1 -0
  513. package/dist/tests/unit/opa-engine.test.js +1813 -0
  514. package/dist/tests/unit/opa-engine.test.js.map +1 -0
  515. package/dist/tests/unit/pipeline-timing.test.d.ts +2 -0
  516. package/dist/tests/unit/pipeline-timing.test.d.ts.map +1 -0
  517. package/dist/tests/unit/pipeline-timing.test.js +528 -0
  518. package/dist/tests/unit/pipeline-timing.test.js.map +1 -0
  519. package/dist/tests/unit/policy-engine.test.d.ts +2 -0
  520. package/dist/tests/unit/policy-engine.test.d.ts.map +1 -0
  521. package/dist/tests/unit/policy-engine.test.js +1345 -0
  522. package/dist/tests/unit/policy-engine.test.js.map +1 -0
  523. package/dist/tests/unit/policy-store.test.d.ts +2 -0
  524. package/dist/tests/unit/policy-store.test.d.ts.map +1 -0
  525. package/dist/tests/unit/policy-store.test.js +60 -0
  526. package/dist/tests/unit/policy-store.test.js.map +1 -0
  527. package/dist/tests/unit/postgres-storage.test.d.ts +2 -0
  528. package/dist/tests/unit/postgres-storage.test.d.ts.map +1 -0
  529. package/dist/tests/unit/postgres-storage.test.js +614 -0
  530. package/dist/tests/unit/postgres-storage.test.js.map +1 -0
  531. package/dist/tests/unit/prompt-injection-backend.test.d.ts +2 -0
  532. package/dist/tests/unit/prompt-injection-backend.test.d.ts.map +1 -0
  533. package/dist/tests/unit/prompt-injection-backend.test.js +621 -0
  534. package/dist/tests/unit/prompt-injection-backend.test.js.map +1 -0
  535. package/dist/tests/unit/proxy-hardening.test.d.ts +2 -0
  536. package/dist/tests/unit/proxy-hardening.test.d.ts.map +1 -0
  537. package/dist/tests/unit/proxy-hardening.test.js +166 -0
  538. package/dist/tests/unit/proxy-hardening.test.js.map +1 -0
  539. package/dist/tests/unit/rate-limiter.test.d.ts +2 -0
  540. package/dist/tests/unit/rate-limiter.test.d.ts.map +1 -0
  541. package/dist/tests/unit/rate-limiter.test.js +443 -0
  542. package/dist/tests/unit/rate-limiter.test.js.map +1 -0
  543. package/dist/tests/unit/redis-storage.test.d.ts +2 -0
  544. package/dist/tests/unit/redis-storage.test.d.ts.map +1 -0
  545. package/dist/tests/unit/redis-storage.test.js +766 -0
  546. package/dist/tests/unit/redis-storage.test.js.map +1 -0
  547. package/dist/tests/unit/replay-engine.test.d.ts +2 -0
  548. package/dist/tests/unit/replay-engine.test.d.ts.map +1 -0
  549. package/dist/tests/unit/replay-engine.test.js +371 -0
  550. package/dist/tests/unit/replay-engine.test.js.map +1 -0
  551. package/dist/tests/unit/saas-routes.test.d.ts +2 -0
  552. package/dist/tests/unit/saas-routes.test.d.ts.map +1 -0
  553. package/dist/tests/unit/saas-routes.test.js +1399 -0
  554. package/dist/tests/unit/saas-routes.test.js.map +1 -0
  555. package/dist/tests/unit/session.test.d.ts +2 -0
  556. package/dist/tests/unit/session.test.d.ts.map +1 -0
  557. package/dist/tests/unit/session.test.js +532 -0
  558. package/dist/tests/unit/session.test.js.map +1 -0
  559. package/dist/tests/unit/slack-executor.test.d.ts +2 -0
  560. package/dist/tests/unit/slack-executor.test.d.ts.map +1 -0
  561. package/dist/tests/unit/slack-executor.test.js +209 -0
  562. package/dist/tests/unit/slack-executor.test.js.map +1 -0
  563. package/dist/tests/unit/storage-hardening.test.d.ts +2 -0
  564. package/dist/tests/unit/storage-hardening.test.d.ts.map +1 -0
  565. package/dist/tests/unit/storage-hardening.test.js +165 -0
  566. package/dist/tests/unit/storage-hardening.test.js.map +1 -0
  567. package/dist/tests/unit/storage.test.d.ts +2 -0
  568. package/dist/tests/unit/storage.test.d.ts.map +1 -0
  569. package/dist/tests/unit/storage.test.js +698 -0
  570. package/dist/tests/unit/storage.test.js.map +1 -0
  571. package/dist/tests/unit/text-normalizer.test.d.ts +2 -0
  572. package/dist/tests/unit/text-normalizer.test.d.ts.map +1 -0
  573. package/dist/tests/unit/text-normalizer.test.js +229 -0
  574. package/dist/tests/unit/text-normalizer.test.js.map +1 -0
  575. package/dist/tests/unit/tracing.test.d.ts +2 -0
  576. package/dist/tests/unit/tracing.test.d.ts.map +1 -0
  577. package/dist/tests/unit/tracing.test.js +611 -0
  578. package/dist/tests/unit/tracing.test.js.map +1 -0
  579. package/dist/tests/unit/trust-calculator.test.d.ts +2 -0
  580. package/dist/tests/unit/trust-calculator.test.d.ts.map +1 -0
  581. package/dist/tests/unit/trust-calculator.test.js +497 -0
  582. package/dist/tests/unit/trust-calculator.test.js.map +1 -0
  583. package/dist/tests/unit/ts-sdk.test.d.ts +2 -0
  584. package/dist/tests/unit/ts-sdk.test.d.ts.map +1 -0
  585. package/dist/tests/unit/ts-sdk.test.js +421 -0
  586. package/dist/tests/unit/ts-sdk.test.js.map +1 -0
  587. package/dist/tests/unit/usage-extractor-llm.test.d.ts +2 -0
  588. package/dist/tests/unit/usage-extractor-llm.test.d.ts.map +1 -0
  589. package/dist/tests/unit/usage-extractor-llm.test.js +139 -0
  590. package/dist/tests/unit/usage-extractor-llm.test.js.map +1 -0
  591. package/dist/tests/unit/usage-extractor.test.d.ts +2 -0
  592. package/dist/tests/unit/usage-extractor.test.d.ts.map +1 -0
  593. package/dist/tests/unit/usage-extractor.test.js +271 -0
  594. package/dist/tests/unit/usage-extractor.test.js.map +1 -0
  595. package/dist/tests/unit/user-stores.test.d.ts +2 -0
  596. package/dist/tests/unit/user-stores.test.d.ts.map +1 -0
  597. package/dist/tests/unit/user-stores.test.js +687 -0
  598. package/dist/tests/unit/user-stores.test.js.map +1 -0
  599. package/dist/tests/unit/validate.test.d.ts +2 -0
  600. package/dist/tests/unit/validate.test.d.ts.map +1 -0
  601. package/dist/tests/unit/validate.test.js +545 -0
  602. package/dist/tests/unit/validate.test.js.map +1 -0
  603. package/package.json +86 -0
  604. package/policy-packs/README.md +42 -0
  605. package/policy-packs/default.yaml +46 -0
  606. package/policy-packs/dev_fast.yaml +54 -0
  607. package/policy-packs/prod_strict.yaml +83 -0
@@ -0,0 +1,734 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MCPBridge = void 0;
4
+ exports.startMCPBridge = startMCPBridge;
5
+ const crypto_1 = require("crypto");
6
+ const gateway_1 = require("../server/gateway");
7
+ const defaults_1 = require("../config/defaults");
8
+ const DEFAULT_BRIDGE_CONFIG = {
9
+ workspace_id: 'ws_mcp_default',
10
+ actor: { type: 'agent', id: 'mcp-agent', display: 'MCP Agent' },
11
+ source: { platform: 'mcp' },
12
+ task_id: '',
13
+ };
14
+ // ---------------------------------------------------------------------------
15
+ // MCP protocol constants
16
+ // ---------------------------------------------------------------------------
17
+ const LATEST_PROTOCOL_VERSION = '2025-03-26';
18
+ const JSONRPC_VERSION = '2.0';
19
+ const SERVER_INFO = {
20
+ name: 'palaryn-mcp-bridge',
21
+ version: '1.0.0',
22
+ };
23
+ // JSON-RPC error codes
24
+ const PARSE_ERROR = -32700;
25
+ const INVALID_REQUEST = -32600;
26
+ const METHOD_NOT_FOUND = -32601;
27
+ const INVALID_PARAMS = -32602;
28
+ const INTERNAL_ERROR = -32603;
29
+ // ---------------------------------------------------------------------------
30
+ // Tool schemas (JSON Schema format for MCP tools/list)
31
+ // ---------------------------------------------------------------------------
32
+ const HTTP_REQUEST_SCHEMA = {
33
+ name: 'http_request',
34
+ description: 'Execute an HTTP request through the Palaryn gateway. ' +
35
+ 'Supports all HTTP methods. The request goes through policy evaluation, ' +
36
+ 'DLP scanning, budget checks, and rate limiting before execution.',
37
+ inputSchema: {
38
+ type: 'object',
39
+ properties: {
40
+ url: { type: 'string', description: 'The target URL for the HTTP request' },
41
+ method: {
42
+ type: 'string',
43
+ enum: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
44
+ description: 'HTTP method (defaults to GET)',
45
+ },
46
+ headers: {
47
+ type: 'object',
48
+ additionalProperties: { type: 'string' },
49
+ description: 'HTTP headers as key-value pairs',
50
+ },
51
+ body: {
52
+ type: 'string',
53
+ description: 'Request body (typically JSON string for POST/PUT/PATCH)',
54
+ },
55
+ timeout_ms: { type: 'number', description: 'Request timeout in milliseconds' },
56
+ max_cost_usd: {
57
+ type: 'number',
58
+ description: 'Maximum cost budget for this request in USD',
59
+ },
60
+ purpose: {
61
+ type: 'string',
62
+ description: 'Description of why this request is being made',
63
+ },
64
+ labels: {
65
+ type: 'array',
66
+ items: { type: 'string' },
67
+ description: 'Classification labels for the request',
68
+ },
69
+ },
70
+ required: ['url'],
71
+ },
72
+ annotations: {
73
+ title: 'HTTP Request',
74
+ readOnlyHint: false,
75
+ destructiveHint: true,
76
+ openWorldHint: true,
77
+ },
78
+ };
79
+ const HTTP_GET_SCHEMA = {
80
+ name: 'http_get',
81
+ description: 'Execute an HTTP GET request through the Palaryn gateway. ' +
82
+ 'Shorthand for http_request with method=GET. The request goes through ' +
83
+ 'policy evaluation, DLP scanning, budget checks, and rate limiting.',
84
+ inputSchema: {
85
+ type: 'object',
86
+ properties: {
87
+ url: { type: 'string', description: 'The target URL for the GET request' },
88
+ headers: {
89
+ type: 'object',
90
+ additionalProperties: { type: 'string' },
91
+ description: 'HTTP headers as key-value pairs',
92
+ },
93
+ timeout_ms: { type: 'number', description: 'Request timeout in milliseconds' },
94
+ max_cost_usd: {
95
+ type: 'number',
96
+ description: 'Maximum cost budget for this request in USD',
97
+ },
98
+ purpose: {
99
+ type: 'string',
100
+ description: 'Description of why this request is being made',
101
+ },
102
+ labels: {
103
+ type: 'array',
104
+ items: { type: 'string' },
105
+ description: 'Classification labels for the request',
106
+ },
107
+ },
108
+ required: ['url'],
109
+ },
110
+ annotations: {
111
+ title: 'HTTP GET',
112
+ readOnlyHint: true,
113
+ destructiveHint: false,
114
+ openWorldHint: true,
115
+ },
116
+ };
117
+ const HTTP_POST_SCHEMA = {
118
+ name: 'http_post',
119
+ description: 'Execute an HTTP POST request through the Palaryn gateway. ' +
120
+ 'Shorthand for http_request with method=POST. The request goes through ' +
121
+ 'policy evaluation, DLP scanning, budget checks, and rate limiting.',
122
+ inputSchema: {
123
+ type: 'object',
124
+ properties: {
125
+ url: { type: 'string', description: 'The target URL for the POST request' },
126
+ headers: {
127
+ type: 'object',
128
+ additionalProperties: { type: 'string' },
129
+ description: 'HTTP headers as key-value pairs',
130
+ },
131
+ body: { type: 'string', description: 'Request body (typically a JSON string)' },
132
+ timeout_ms: { type: 'number', description: 'Request timeout in milliseconds' },
133
+ max_cost_usd: {
134
+ type: 'number',
135
+ description: 'Maximum cost budget for this request in USD',
136
+ },
137
+ purpose: {
138
+ type: 'string',
139
+ description: 'Description of why this request is being made',
140
+ },
141
+ labels: {
142
+ type: 'array',
143
+ items: { type: 'string' },
144
+ description: 'Classification labels for the request',
145
+ },
146
+ },
147
+ required: ['url'],
148
+ },
149
+ annotations: {
150
+ title: 'HTTP POST',
151
+ readOnlyHint: false,
152
+ destructiveHint: false,
153
+ openWorldHint: true,
154
+ },
155
+ };
156
+ const ALL_TOOLS = [HTTP_REQUEST_SCHEMA, HTTP_GET_SCHEMA, HTTP_POST_SCHEMA];
157
+ // ---------------------------------------------------------------------------
158
+ // StdioTransport - line-delimited JSON-RPC over stdin/stdout
159
+ // ---------------------------------------------------------------------------
160
+ /**
161
+ * Reads line-delimited JSON-RPC messages from a readable stream
162
+ * and writes JSON-RPC responses to a writable stream.
163
+ * Follows the MCP stdio transport specification.
164
+ */
165
+ class StdioTransport {
166
+ constructor(input, output) {
167
+ this.buffer = '';
168
+ this.onMessage = null;
169
+ this.bufferTimeoutHandle = null;
170
+ this.input = input || process.stdin;
171
+ this.output = output || process.stdout;
172
+ }
173
+ /** Start listening for messages on the input stream. */
174
+ start(handler) {
175
+ this.onMessage = handler;
176
+ this.input.setEncoding('utf8');
177
+ this.input.on('data', (chunk) => this.onData(chunk));
178
+ }
179
+ /** Send a JSON-RPC response to the output stream. */
180
+ send(response) {
181
+ const json = JSON.stringify(response);
182
+ this.output.write(json + '\n');
183
+ }
184
+ /** Stop listening and clean up. */
185
+ close() {
186
+ this.input.removeAllListeners('data');
187
+ this.onMessage = null;
188
+ this.clearBufferTimeout();
189
+ }
190
+ clearBufferTimeout() {
191
+ if (this.bufferTimeoutHandle) {
192
+ clearTimeout(this.bufferTimeoutHandle);
193
+ this.bufferTimeoutHandle = null;
194
+ }
195
+ }
196
+ /** Reset the inactivity timer for partial buffer data. */
197
+ resetBufferTimeout() {
198
+ this.clearBufferTimeout();
199
+ if (this.buffer.length > 0) {
200
+ this.bufferTimeoutHandle = setTimeout(() => {
201
+ if (this.buffer.length > 0) {
202
+ console.error(`[mcp-bridge] Buffer inactivity timeout (${StdioTransport.BUFFER_INACTIVITY_TIMEOUT_MS}ms) with ${this.buffer.length} bytes pending, clearing`);
203
+ this.buffer = '';
204
+ const errorResponse = {
205
+ jsonrpc: JSONRPC_VERSION,
206
+ id: null,
207
+ error: { code: PARSE_ERROR, message: 'Buffer inactivity timeout: incomplete message cleared' },
208
+ };
209
+ this.send(errorResponse);
210
+ }
211
+ }, StdioTransport.BUFFER_INACTIVITY_TIMEOUT_MS);
212
+ }
213
+ }
214
+ onData(chunk) {
215
+ this.buffer += chunk;
216
+ // Prevent unbounded buffer growth
217
+ if (this.buffer.length > StdioTransport.MAX_BUFFER_SIZE) {
218
+ console.error('[mcp-bridge] Buffer exceeded maximum size (10MB), clearing');
219
+ this.buffer = '';
220
+ this.clearBufferTimeout();
221
+ const errorResponse = {
222
+ jsonrpc: JSONRPC_VERSION,
223
+ id: null,
224
+ error: { code: PARSE_ERROR, message: 'Message too large: exceeded 10MB buffer limit' },
225
+ };
226
+ this.send(errorResponse);
227
+ return;
228
+ }
229
+ // Process complete lines
230
+ const lines = this.buffer.split('\n');
231
+ // Keep the last (possibly incomplete) line in the buffer
232
+ this.buffer = lines.pop() || '';
233
+ for (const line of lines) {
234
+ const trimmed = line.trim();
235
+ if (!trimmed)
236
+ continue;
237
+ try {
238
+ const parsed = JSON.parse(trimmed);
239
+ if (this.onMessage) {
240
+ this.onMessage(parsed);
241
+ }
242
+ }
243
+ catch {
244
+ // Send parse error for invalid JSON
245
+ const errorResponse = {
246
+ jsonrpc: JSONRPC_VERSION,
247
+ id: null,
248
+ error: { code: PARSE_ERROR, message: 'Parse error: invalid JSON' },
249
+ };
250
+ this.send(errorResponse);
251
+ }
252
+ }
253
+ // Reset inactivity timer if there's still partial data in the buffer
254
+ this.resetBufferTimeout();
255
+ }
256
+ }
257
+ StdioTransport.MAX_BUFFER_SIZE = 10 * 1024 * 1024; // 10MB
258
+ StdioTransport.BUFFER_INACTIVITY_TIMEOUT_MS = 30000; // 30s
259
+ // ---------------------------------------------------------------------------
260
+ // MCPBridge - the main bridge class
261
+ // ---------------------------------------------------------------------------
262
+ /**
263
+ * MCPBridge wraps a Palaryn Gateway instance as an MCP server, exposing
264
+ * the gateway's tool execution capabilities through the Model Context Protocol.
265
+ *
266
+ * Communication uses JSON-RPC 2.0 over stdio (line-delimited).
267
+ *
268
+ * It exposes three MCP tools:
269
+ * - `http_request` - Execute any HTTP request through the gateway
270
+ * - `http_get` - Shorthand for GET requests
271
+ * - `http_post` - Shorthand for POST requests
272
+ *
273
+ * Each tool constructs a proper ToolCall, runs it through the full gateway
274
+ * pipeline (policy, DLP, budget, rate limiting, execution), and returns
275
+ * the ToolResult as the MCP response.
276
+ *
277
+ * Supported MCP methods:
278
+ * - `initialize` - Protocol handshake (returns server info and capabilities)
279
+ * - `notifications/initialized` - Client acknowledgment (no-op notification)
280
+ * - `tools/list` - List available tools with their JSON schemas
281
+ * - `tools/call` - Execute a tool through the gateway
282
+ * - `ping` - Health check
283
+ */
284
+ class MCPBridge {
285
+ constructor(gateway, bridgeConfig) {
286
+ this.transport = null;
287
+ this.initialized = false;
288
+ this.gateway = gateway;
289
+ this.bridgeConfig = {
290
+ ...DEFAULT_BRIDGE_CONFIG,
291
+ ...bridgeConfig,
292
+ };
293
+ }
294
+ /**
295
+ * Connect via stdio transport (reads from stdin, writes to stdout).
296
+ * This is the standard way to run an MCP server for CLI-based clients.
297
+ * Optionally accepts custom input/output streams for testing.
298
+ */
299
+ async connectStdio(input, output) {
300
+ this.transport = new StdioTransport(input, output);
301
+ this.transport.start((msg) => this.handleMessage(msg));
302
+ }
303
+ /**
304
+ * Close the transport and shut down the gateway.
305
+ */
306
+ async close() {
307
+ if (this.transport) {
308
+ this.transport.close();
309
+ this.transport = null;
310
+ }
311
+ this.gateway.shutdown();
312
+ }
313
+ /**
314
+ * Returns the underlying Gateway instance.
315
+ */
316
+ getGateway() {
317
+ return this.gateway;
318
+ }
319
+ /**
320
+ * Whether the MCP handshake has been completed.
321
+ */
322
+ isInitialized() {
323
+ return this.initialized;
324
+ }
325
+ // ---------------------------------------------------------------------------
326
+ // Message dispatch
327
+ // ---------------------------------------------------------------------------
328
+ /** Handle an incoming JSON-RPC message (request or notification). */
329
+ handleMessage(msg) {
330
+ // Notifications have no 'id' field
331
+ if (!('id' in msg) || msg.id === undefined) {
332
+ this.handleNotification(msg);
333
+ return;
334
+ }
335
+ const request = msg;
336
+ this.handleRequest(request).catch((err) => {
337
+ this.sendError(request.id, INTERNAL_ERROR, `Internal error: ${err instanceof Error ? err.message : String(err)}`);
338
+ });
339
+ }
340
+ /** Handle a JSON-RPC notification (no response expected). */
341
+ handleNotification(msg) {
342
+ // The only notification we handle is `notifications/initialized`
343
+ if (msg.method === 'notifications/initialized') {
344
+ // Client acknowledges initialization is complete; no action needed.
345
+ return;
346
+ }
347
+ // Unknown notifications are silently ignored per MCP spec.
348
+ }
349
+ /** Handle a JSON-RPC request and send a response. */
350
+ async handleRequest(request) {
351
+ switch (request.method) {
352
+ case 'initialize':
353
+ this.handleInitialize(request);
354
+ break;
355
+ case 'tools/list':
356
+ this.handleToolsList(request);
357
+ break;
358
+ case 'tools/call':
359
+ await this.handleToolsCall(request);
360
+ break;
361
+ case 'ping':
362
+ this.sendResult(request.id, {});
363
+ break;
364
+ default:
365
+ this.sendError(request.id, METHOD_NOT_FOUND, `Method not found: ${request.method}`);
366
+ break;
367
+ }
368
+ }
369
+ // ---------------------------------------------------------------------------
370
+ // MCP method handlers
371
+ // ---------------------------------------------------------------------------
372
+ /** Handle `initialize` - protocol handshake. */
373
+ handleInitialize(request) {
374
+ this.initialized = true;
375
+ this.sendResult(request.id, {
376
+ protocolVersion: LATEST_PROTOCOL_VERSION,
377
+ capabilities: {
378
+ tools: {
379
+ listChanged: false,
380
+ },
381
+ },
382
+ serverInfo: SERVER_INFO,
383
+ });
384
+ }
385
+ /** Handle `tools/list` - return all registered tool definitions. */
386
+ handleToolsList(request) {
387
+ this.sendResult(request.id, {
388
+ tools: ALL_TOOLS,
389
+ });
390
+ }
391
+ /** Handle `tools/call` - execute a tool through the gateway. */
392
+ async handleToolsCall(request) {
393
+ const params = request.params;
394
+ if (!params || typeof params.name !== 'string') {
395
+ this.sendError(request.id, INVALID_PARAMS, 'Missing required parameter: name');
396
+ return;
397
+ }
398
+ const toolName = params.name;
399
+ const toolArgs = (params.arguments || {});
400
+ let result;
401
+ try {
402
+ switch (toolName) {
403
+ case 'http_request':
404
+ result = await this.executeHttpRequest(toolArgs);
405
+ break;
406
+ case 'http_get':
407
+ result = await this.executeHttpGet(toolArgs);
408
+ break;
409
+ case 'http_post':
410
+ result = await this.executeHttpPost(toolArgs);
411
+ break;
412
+ default:
413
+ this.sendError(request.id, INVALID_PARAMS, `Unknown tool: ${toolName}`);
414
+ return;
415
+ }
416
+ }
417
+ catch (err) {
418
+ result = {
419
+ content: [
420
+ {
421
+ type: 'text',
422
+ text: JSON.stringify({
423
+ error: err instanceof Error ? err.message : String(err),
424
+ status: 'error',
425
+ }, null, 2),
426
+ },
427
+ ],
428
+ isError: true,
429
+ };
430
+ }
431
+ this.sendResult(request.id, result);
432
+ }
433
+ // ---------------------------------------------------------------------------
434
+ // Tool execution methods
435
+ // ---------------------------------------------------------------------------
436
+ /** Execute `http_request` tool - arbitrary HTTP method. */
437
+ async executeHttpRequest(args) {
438
+ if (!args.url || typeof args.url !== 'string') {
439
+ return this.toolError('Missing required argument: url');
440
+ }
441
+ const method = (typeof args.method === 'string' ? args.method : 'GET').toUpperCase();
442
+ const capability = this.methodToCapability(method);
443
+ const toolCall = this.buildToolCall({
444
+ toolName: 'http.request',
445
+ capability,
446
+ args: {
447
+ method,
448
+ url: args.url,
449
+ headers: args.headers,
450
+ body: typeof args.body === 'string' ? this.parseBody(args.body) : undefined,
451
+ },
452
+ constraints: {
453
+ timeout_ms: typeof args.timeout_ms === 'number' ? args.timeout_ms : undefined,
454
+ max_cost_usd: typeof args.max_cost_usd === 'number' ? args.max_cost_usd : undefined,
455
+ },
456
+ context: {
457
+ purpose: typeof args.purpose === 'string' ? args.purpose : undefined,
458
+ labels: Array.isArray(args.labels) ? args.labels : undefined,
459
+ },
460
+ });
461
+ return this.executeAndFormat(toolCall);
462
+ }
463
+ /** Execute `http_get` tool - GET request shorthand. */
464
+ async executeHttpGet(args) {
465
+ if (!args.url || typeof args.url !== 'string') {
466
+ return this.toolError('Missing required argument: url');
467
+ }
468
+ const toolCall = this.buildToolCall({
469
+ toolName: 'http.get',
470
+ capability: 'read',
471
+ args: {
472
+ method: 'GET',
473
+ url: args.url,
474
+ headers: args.headers,
475
+ },
476
+ constraints: {
477
+ timeout_ms: typeof args.timeout_ms === 'number' ? args.timeout_ms : undefined,
478
+ max_cost_usd: typeof args.max_cost_usd === 'number' ? args.max_cost_usd : undefined,
479
+ },
480
+ context: {
481
+ purpose: typeof args.purpose === 'string' ? args.purpose : undefined,
482
+ labels: Array.isArray(args.labels) ? args.labels : undefined,
483
+ },
484
+ });
485
+ return this.executeAndFormat(toolCall);
486
+ }
487
+ /** Execute `http_post` tool - POST request shorthand. */
488
+ async executeHttpPost(args) {
489
+ if (!args.url || typeof args.url !== 'string') {
490
+ return this.toolError('Missing required argument: url');
491
+ }
492
+ const toolCall = this.buildToolCall({
493
+ toolName: 'http.post',
494
+ capability: 'write',
495
+ args: {
496
+ method: 'POST',
497
+ url: args.url,
498
+ headers: args.headers,
499
+ body: typeof args.body === 'string' ? this.parseBody(args.body) : undefined,
500
+ },
501
+ constraints: {
502
+ timeout_ms: typeof args.timeout_ms === 'number' ? args.timeout_ms : undefined,
503
+ max_cost_usd: typeof args.max_cost_usd === 'number' ? args.max_cost_usd : undefined,
504
+ },
505
+ context: {
506
+ purpose: typeof args.purpose === 'string' ? args.purpose : undefined,
507
+ labels: Array.isArray(args.labels) ? args.labels : undefined,
508
+ },
509
+ });
510
+ return this.executeAndFormat(toolCall);
511
+ }
512
+ // ---------------------------------------------------------------------------
513
+ // Internal helpers
514
+ // ---------------------------------------------------------------------------
515
+ /**
516
+ * Build a ToolCall from MCP tool arguments, applying bridge defaults.
517
+ */
518
+ buildToolCall(params) {
519
+ const toolCall = {
520
+ tool_call_id: (0, crypto_1.randomUUID)(),
521
+ task_id: this.bridgeConfig.task_id || (0, crypto_1.randomUUID)(),
522
+ workspace_id: this.bridgeConfig.workspace_id,
523
+ actor: { ...this.bridgeConfig.actor },
524
+ source: { ...this.bridgeConfig.source },
525
+ tool: {
526
+ name: params.toolName,
527
+ version: '1.0.0',
528
+ capability: params.capability,
529
+ },
530
+ args: params.args,
531
+ timestamp: new Date().toISOString(),
532
+ };
533
+ // Add constraints if any are specified
534
+ if (params.constraints?.timeout_ms != null || params.constraints?.max_cost_usd != null) {
535
+ toolCall.constraints = {};
536
+ if (params.constraints.timeout_ms != null) {
537
+ toolCall.constraints.timeout_ms = params.constraints.timeout_ms;
538
+ }
539
+ if (params.constraints.max_cost_usd != null) {
540
+ toolCall.constraints.max_cost_usd = params.constraints.max_cost_usd;
541
+ }
542
+ }
543
+ // Add context if any is specified
544
+ if (params.context?.purpose || params.context?.labels) {
545
+ toolCall.context = {};
546
+ if (params.context.purpose) {
547
+ toolCall.context.purpose = params.context.purpose;
548
+ }
549
+ if (params.context.labels) {
550
+ toolCall.context.labels = params.context.labels;
551
+ }
552
+ }
553
+ return toolCall;
554
+ }
555
+ /**
556
+ * Execute a ToolCall through the gateway and format as MCP result.
557
+ */
558
+ async executeAndFormat(toolCall) {
559
+ try {
560
+ const result = await this.gateway.execute(toolCall);
561
+ return this.formatResult(result);
562
+ }
563
+ catch (err) {
564
+ const errorMessage = err instanceof Error ? err.message : String(err);
565
+ return {
566
+ content: [
567
+ {
568
+ type: 'text',
569
+ text: JSON.stringify({
570
+ error: errorMessage,
571
+ tool_call_id: toolCall.tool_call_id,
572
+ status: 'error',
573
+ }, null, 2),
574
+ },
575
+ ],
576
+ isError: true,
577
+ };
578
+ }
579
+ }
580
+ /**
581
+ * Convert a gateway ToolResult into an MCP CallToolResult.
582
+ *
583
+ * The result includes:
584
+ * - The tool output body (or error message) as the primary text content
585
+ * - Gateway metadata (status, policy decision, DLP report, budget, timing)
586
+ * as a second text content block for transparency
587
+ */
588
+ formatResult(result) {
589
+ const isError = result.status === 'error' || result.status === 'blocked';
590
+ // Primary content: the actual output or error
591
+ let primaryText;
592
+ if (result.error) {
593
+ primaryText = result.error;
594
+ }
595
+ else if (result.output?.body !== undefined) {
596
+ primaryText =
597
+ typeof result.output.body === 'string'
598
+ ? result.output.body
599
+ : JSON.stringify(result.output.body, null, 2);
600
+ }
601
+ else {
602
+ primaryText = `Request completed with status: ${result.status}`;
603
+ }
604
+ // Metadata block for gateway transparency
605
+ const metadata = {
606
+ tool_call_id: result.tool_call_id,
607
+ task_id: result.task_id,
608
+ status: result.status,
609
+ policy: result.policy,
610
+ dlp: {
611
+ detected: result.dlp.detected,
612
+ severity: result.dlp.severity,
613
+ redaction_count: result.dlp.redactions.length,
614
+ },
615
+ budget: result.budget,
616
+ timing: result.timing,
617
+ http_status: result.output?.http_status,
618
+ };
619
+ return {
620
+ content: [
621
+ {
622
+ type: 'text',
623
+ text: primaryText,
624
+ },
625
+ {
626
+ type: 'text',
627
+ text: `--- Gateway Metadata ---\n${JSON.stringify(metadata, null, 2)}`,
628
+ },
629
+ ],
630
+ isError,
631
+ };
632
+ }
633
+ /**
634
+ * Map an HTTP method to a ToolInfo capability level.
635
+ */
636
+ methodToCapability(method) {
637
+ switch (method.toUpperCase()) {
638
+ case 'GET':
639
+ case 'HEAD':
640
+ case 'OPTIONS':
641
+ return 'read';
642
+ case 'POST':
643
+ case 'PUT':
644
+ case 'PATCH':
645
+ return 'write';
646
+ case 'DELETE':
647
+ return 'delete';
648
+ default:
649
+ return 'write';
650
+ }
651
+ }
652
+ /**
653
+ * Attempt to parse a body string as JSON, falling back to the raw string.
654
+ */
655
+ parseBody(body) {
656
+ try {
657
+ return JSON.parse(body);
658
+ }
659
+ catch {
660
+ return body;
661
+ }
662
+ }
663
+ /**
664
+ * Create an MCP tool error result.
665
+ */
666
+ toolError(message) {
667
+ return {
668
+ content: [{ type: 'text', text: message }],
669
+ isError: true,
670
+ };
671
+ }
672
+ /**
673
+ * Send a successful JSON-RPC response.
674
+ */
675
+ sendResult(id, result) {
676
+ if (this.transport) {
677
+ this.transport.send({
678
+ jsonrpc: JSONRPC_VERSION,
679
+ id,
680
+ result,
681
+ });
682
+ }
683
+ }
684
+ /**
685
+ * Send a JSON-RPC error response.
686
+ */
687
+ sendError(id, code, message, data) {
688
+ if (this.transport) {
689
+ this.transport.send({
690
+ jsonrpc: JSONRPC_VERSION,
691
+ id,
692
+ error: { code, message, data },
693
+ });
694
+ }
695
+ }
696
+ }
697
+ exports.MCPBridge = MCPBridge;
698
+ // ---------------------------------------------------------------------------
699
+ // Convenience entry point
700
+ // ---------------------------------------------------------------------------
701
+ /**
702
+ * Create a Gateway instance with the given config (or defaults),
703
+ * wrap it in an MCPBridge, and connect via stdio transport.
704
+ *
705
+ * This is the main entry point for running Palaryn as an MCP server.
706
+ *
707
+ * @param gatewayConfig - Full gateway configuration (defaults to DEFAULT_CONFIG with auth disabled)
708
+ * @param bridgeConfig - MCP bridge defaults for workspace, actor, source
709
+ * @returns The connected MCPBridge instance
710
+ */
711
+ async function startMCPBridge(gatewayConfig, bridgeConfig) {
712
+ // Merge with defaults, disabling auth for MCP (auth is handled by the MCP client/transport)
713
+ const config = {
714
+ ...defaults_1.DEFAULT_CONFIG,
715
+ ...gatewayConfig,
716
+ auth: {
717
+ ...defaults_1.DEFAULT_CONFIG.auth,
718
+ enabled: false,
719
+ ...gatewayConfig?.auth,
720
+ },
721
+ // Disable console audit output when running as MCP server
722
+ // to avoid polluting stdout (which is the MCP transport channel)
723
+ audit: {
724
+ ...defaults_1.DEFAULT_CONFIG.audit,
725
+ console_output: false,
726
+ ...gatewayConfig?.audit,
727
+ },
728
+ };
729
+ const gateway = new gateway_1.Gateway(config);
730
+ const bridge = new MCPBridge(gateway, bridgeConfig);
731
+ await bridge.connectStdio();
732
+ return bridge;
733
+ }
734
+ //# sourceMappingURL=bridge.js.map