@codeqr/ts-mcp 0.9.0 → 0.24.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 (488) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +16 -218
  3. package/auth.d.mts +6 -0
  4. package/auth.d.mts.map +1 -0
  5. package/auth.d.ts +6 -0
  6. package/auth.d.ts.map +1 -0
  7. package/auth.js +37 -0
  8. package/auth.js.map +1 -0
  9. package/{headers.mjs → auth.mjs} +16 -2
  10. package/auth.mjs.map +1 -0
  11. package/code-tool-paths.cjs +8 -0
  12. package/code-tool-paths.cjs.map +1 -0
  13. package/code-tool-paths.d.cts +2 -0
  14. package/code-tool-paths.d.cts.map +1 -0
  15. package/code-tool-types.d.mts +7 -9
  16. package/code-tool-types.d.mts.map +1 -1
  17. package/code-tool-types.d.ts +7 -9
  18. package/code-tool-types.d.ts.map +1 -1
  19. package/code-tool-worker.d.mts +5 -0
  20. package/code-tool-worker.d.mts.map +1 -0
  21. package/code-tool-worker.d.ts +5 -0
  22. package/code-tool-worker.d.ts.map +1 -0
  23. package/code-tool-worker.js +291 -0
  24. package/code-tool-worker.js.map +1 -0
  25. package/code-tool-worker.mjs +253 -0
  26. package/code-tool-worker.mjs.map +1 -0
  27. package/code-tool.d.mts +12 -45
  28. package/code-tool.d.mts.map +1 -1
  29. package/code-tool.d.ts +12 -45
  30. package/code-tool.d.ts.map +1 -1
  31. package/code-tool.js +264 -27
  32. package/code-tool.js.map +1 -1
  33. package/code-tool.mjs +231 -27
  34. package/code-tool.mjs.map +1 -1
  35. package/docs-search-tool.d.mts +12 -3
  36. package/docs-search-tool.d.mts.map +1 -1
  37. package/docs-search-tool.d.ts +12 -3
  38. package/docs-search-tool.d.ts.map +1 -1
  39. package/docs-search-tool.js +23 -10
  40. package/docs-search-tool.js.map +1 -1
  41. package/docs-search-tool.mjs +22 -10
  42. package/docs-search-tool.mjs.map +1 -1
  43. package/http.d.mts +6 -3
  44. package/http.d.mts.map +1 -1
  45. package/http.d.ts +6 -3
  46. package/http.d.ts.map +1 -1
  47. package/http.js +130 -48
  48. package/http.js.map +1 -1
  49. package/http.mjs +130 -48
  50. package/http.mjs.map +1 -1
  51. package/index.js +19 -50
  52. package/index.js.map +1 -1
  53. package/index.mjs +19 -50
  54. package/index.mjs.map +1 -1
  55. package/instructions.d.mts +5 -0
  56. package/instructions.d.mts.map +1 -0
  57. package/instructions.d.ts +5 -0
  58. package/instructions.d.ts.map +1 -0
  59. package/instructions.js +45 -0
  60. package/instructions.js.map +1 -0
  61. package/instructions.mjs +39 -0
  62. package/instructions.mjs.map +1 -0
  63. package/local-docs-search.d.mts +28 -0
  64. package/local-docs-search.d.mts.map +1 -0
  65. package/local-docs-search.d.ts +28 -0
  66. package/local-docs-search.d.ts.map +1 -0
  67. package/local-docs-search.js +1259 -0
  68. package/local-docs-search.js.map +1 -0
  69. package/local-docs-search.mjs +1219 -0
  70. package/local-docs-search.mjs.map +1 -0
  71. package/logger.d.mts +7 -0
  72. package/logger.d.mts.map +1 -0
  73. package/logger.d.ts +7 -0
  74. package/logger.d.ts.map +1 -0
  75. package/logger.js +29 -0
  76. package/logger.js.map +1 -0
  77. package/logger.mjs +22 -0
  78. package/logger.mjs.map +1 -0
  79. package/methods.d.mts +10 -0
  80. package/methods.d.mts.map +1 -0
  81. package/methods.d.ts +10 -0
  82. package/methods.d.ts.map +1 -0
  83. package/methods.js +215 -0
  84. package/methods.js.map +1 -0
  85. package/methods.mjs +211 -0
  86. package/methods.mjs.map +1 -0
  87. package/options.d.mts +12 -9
  88. package/options.d.mts.map +1 -1
  89. package/options.d.ts +12 -9
  90. package/options.d.ts.map +1 -1
  91. package/options.js +67 -353
  92. package/options.js.map +1 -1
  93. package/options.mjs +67 -353
  94. package/options.mjs.map +1 -1
  95. package/package.json +94 -60
  96. package/server.d.mts +19 -16
  97. package/server.d.mts.map +1 -1
  98. package/server.d.ts +19 -16
  99. package/server.d.ts.map +1 -1
  100. package/server.js +95 -101
  101. package/server.js.map +1 -1
  102. package/server.mjs +94 -96
  103. package/server.mjs.map +1 -1
  104. package/src/{headers.ts → auth.ts} +18 -1
  105. package/src/code-tool-paths.cts +5 -0
  106. package/src/code-tool-types.ts +8 -9
  107. package/src/code-tool-worker.ts +304 -0
  108. package/src/code-tool.ts +292 -36
  109. package/src/docs-search-tool.ts +34 -14
  110. package/src/http.ts +154 -54
  111. package/src/index.ts +22 -63
  112. package/src/instructions.ts +57 -0
  113. package/src/local-docs-search.ts +1368 -0
  114. package/src/logger.ts +28 -0
  115. package/src/methods.ts +236 -0
  116. package/src/options.ts +90 -378
  117. package/src/server.ts +118 -117
  118. package/src/stdio.ts +9 -5
  119. package/src/{tools/types.ts → types.ts} +16 -5
  120. package/src/util.ts +25 -0
  121. package/stdio.d.mts +1 -1
  122. package/stdio.d.mts.map +1 -1
  123. package/stdio.d.ts +1 -1
  124. package/stdio.d.ts.map +1 -1
  125. package/stdio.js +8 -4
  126. package/stdio.js.map +1 -1
  127. package/stdio.mjs +8 -4
  128. package/stdio.mjs.map +1 -1
  129. package/{tools/types.d.mts → types.d.mts} +15 -2
  130. package/types.d.mts.map +1 -0
  131. package/{tools/types.d.ts → types.d.ts} +15 -2
  132. package/types.d.ts.map +1 -0
  133. package/types.js.map +1 -0
  134. package/types.mjs.map +1 -0
  135. package/util.d.mts +4 -0
  136. package/util.d.mts.map +1 -0
  137. package/util.d.ts +4 -0
  138. package/util.d.ts.map +1 -0
  139. package/util.js +30 -0
  140. package/util.js.map +1 -0
  141. package/util.mjs +24 -0
  142. package/util.mjs.map +1 -0
  143. package/compat.d.mts +0 -58
  144. package/compat.d.mts.map +0 -1
  145. package/compat.d.ts +0 -58
  146. package/compat.d.ts.map +0 -1
  147. package/compat.js +0 -387
  148. package/compat.js.map +0 -1
  149. package/compat.mjs +0 -378
  150. package/compat.mjs.map +0 -1
  151. package/dynamic-tools.d.mts +0 -12
  152. package/dynamic-tools.d.mts.map +0 -1
  153. package/dynamic-tools.d.ts +0 -12
  154. package/dynamic-tools.d.ts.map +0 -1
  155. package/dynamic-tools.js +0 -135
  156. package/dynamic-tools.js.map +0 -1
  157. package/dynamic-tools.mjs +0 -132
  158. package/dynamic-tools.mjs.map +0 -1
  159. package/filtering.d.mts +0 -3
  160. package/filtering.d.mts.map +0 -1
  161. package/filtering.d.ts +0 -3
  162. package/filtering.d.ts.map +0 -1
  163. package/filtering.js +0 -24
  164. package/filtering.js.map +0 -1
  165. package/filtering.mjs +0 -16
  166. package/filtering.mjs.map +0 -1
  167. package/headers.d.mts +0 -4
  168. package/headers.d.mts.map +0 -1
  169. package/headers.d.ts +0 -4
  170. package/headers.d.ts.map +0 -1
  171. package/headers.js +0 -22
  172. package/headers.js.map +0 -1
  173. package/headers.mjs.map +0 -1
  174. package/src/compat.ts +0 -483
  175. package/src/dynamic-tools.ts +0 -153
  176. package/src/filtering.ts +0 -18
  177. package/src/tools/analytics/retrieve-analytics.ts +0 -441
  178. package/src/tools/domains/create-domains.ts +0 -75
  179. package/src/tools/domains/delete-domains.ts +0 -55
  180. package/src/tools/domains/list-domains.ts +0 -68
  181. package/src/tools/domains/update-domains.ts +0 -79
  182. package/src/tools/events/list-events.ts +0 -421
  183. package/src/tools/index.ts +0 -133
  184. package/src/tools/links/bulk/create-links-bulk.ts +0 -966
  185. package/src/tools/links/bulk/delete-links-bulk.ts +0 -59
  186. package/src/tools/links/count-links.ts +0 -957
  187. package/src/tools/links/create-links.ts +0 -957
  188. package/src/tools/links/delete-links.ts +0 -56
  189. package/src/tools/links/list-links.ts +0 -980
  190. package/src/tools/links/retrieve-info-links.ts +0 -75
  191. package/src/tools/links/update-links.ts +0 -964
  192. package/src/tools/links/upsert-links.ts +0 -958
  193. package/src/tools/projects/list-projects.ts +0 -51
  194. package/src/tools/projects/retrieve-projects.ts +0 -56
  195. package/src/tools/qrcodes/bulk/create-qrcodes-bulk.ts +0 -1077
  196. package/src/tools/qrcodes/bulk/delete-qrcodes-bulk.ts +0 -59
  197. package/src/tools/qrcodes/count-qrcodes.ts +0 -220
  198. package/src/tools/qrcodes/create-qrcodes.ts +0 -1067
  199. package/src/tools/qrcodes/delete-qrcodes.ts +0 -56
  200. package/src/tools/qrcodes/list-qrcodes.ts +0 -221
  201. package/src/tools/qrcodes/retrieve-qrcodes.ts +0 -67
  202. package/src/tools/qrcodes/update-qrcodes.ts +0 -1074
  203. package/src/tools/qrcodes/upsert-qrcodes.ts +0 -1068
  204. package/src/tools/tags/create-tags.ts +0 -59
  205. package/src/tools/tags/delete-tags.ts +0 -55
  206. package/src/tools/tags/list-tags.ts +0 -87
  207. package/src/tools/tags/update-tags.ts +0 -63
  208. package/src/tools/track/track-lead-track.ts +0 -93
  209. package/src/tools/track/track-sale-track.ts +0 -108
  210. package/src/tools.ts +0 -1
  211. package/tools/analytics/retrieve-analytics.d.mts +0 -51
  212. package/tools/analytics/retrieve-analytics.d.mts.map +0 -1
  213. package/tools/analytics/retrieve-analytics.d.ts +0 -51
  214. package/tools/analytics/retrieve-analytics.d.ts.map +0 -1
  215. package/tools/analytics/retrieve-analytics.js +0 -438
  216. package/tools/analytics/retrieve-analytics.js.map +0 -1
  217. package/tools/analytics/retrieve-analytics.mjs +0 -431
  218. package/tools/analytics/retrieve-analytics.mjs.map +0 -1
  219. package/tools/domains/create-domains.d.mts +0 -51
  220. package/tools/domains/create-domains.d.mts.map +0 -1
  221. package/tools/domains/create-domains.d.ts +0 -51
  222. package/tools/domains/create-domains.d.ts.map +0 -1
  223. package/tools/domains/create-domains.js +0 -74
  224. package/tools/domains/create-domains.js.map +0 -1
  225. package/tools/domains/create-domains.mjs +0 -67
  226. package/tools/domains/create-domains.mjs.map +0 -1
  227. package/tools/domains/delete-domains.d.mts +0 -51
  228. package/tools/domains/delete-domains.d.mts.map +0 -1
  229. package/tools/domains/delete-domains.d.ts +0 -51
  230. package/tools/domains/delete-domains.d.ts.map +0 -1
  231. package/tools/domains/delete-domains.js +0 -55
  232. package/tools/domains/delete-domains.js.map +0 -1
  233. package/tools/domains/delete-domains.mjs +0 -48
  234. package/tools/domains/delete-domains.mjs.map +0 -1
  235. package/tools/domains/list-domains.d.mts +0 -51
  236. package/tools/domains/list-domains.d.mts.map +0 -1
  237. package/tools/domains/list-domains.d.ts +0 -51
  238. package/tools/domains/list-domains.d.ts.map +0 -1
  239. package/tools/domains/list-domains.js +0 -67
  240. package/tools/domains/list-domains.js.map +0 -1
  241. package/tools/domains/list-domains.mjs +0 -60
  242. package/tools/domains/list-domains.mjs.map +0 -1
  243. package/tools/domains/update-domains.d.mts +0 -51
  244. package/tools/domains/update-domains.d.mts.map +0 -1
  245. package/tools/domains/update-domains.d.ts +0 -51
  246. package/tools/domains/update-domains.d.ts.map +0 -1
  247. package/tools/domains/update-domains.js +0 -78
  248. package/tools/domains/update-domains.js.map +0 -1
  249. package/tools/domains/update-domains.mjs +0 -71
  250. package/tools/domains/update-domains.mjs.map +0 -1
  251. package/tools/events/list-events.d.mts +0 -51
  252. package/tools/events/list-events.d.mts.map +0 -1
  253. package/tools/events/list-events.d.ts +0 -51
  254. package/tools/events/list-events.d.ts.map +0 -1
  255. package/tools/events/list-events.js +0 -418
  256. package/tools/events/list-events.js.map +0 -1
  257. package/tools/events/list-events.mjs +0 -411
  258. package/tools/events/list-events.mjs.map +0 -1
  259. package/tools/index.d.mts +0 -10
  260. package/tools/index.d.mts.map +0 -1
  261. package/tools/index.d.ts +0 -10
  262. package/tools/index.d.ts.map +0 -1
  263. package/tools/index.js +0 -117
  264. package/tools/index.js.map +0 -1
  265. package/tools/index.mjs +0 -110
  266. package/tools/index.mjs.map +0 -1
  267. package/tools/links/bulk/create-links-bulk.d.mts +0 -51
  268. package/tools/links/bulk/create-links-bulk.d.mts.map +0 -1
  269. package/tools/links/bulk/create-links-bulk.d.ts +0 -51
  270. package/tools/links/bulk/create-links-bulk.d.ts.map +0 -1
  271. package/tools/links/bulk/create-links-bulk.js +0 -956
  272. package/tools/links/bulk/create-links-bulk.js.map +0 -1
  273. package/tools/links/bulk/create-links-bulk.mjs +0 -949
  274. package/tools/links/bulk/create-links-bulk.mjs.map +0 -1
  275. package/tools/links/bulk/delete-links-bulk.d.mts +0 -51
  276. package/tools/links/bulk/delete-links-bulk.d.mts.map +0 -1
  277. package/tools/links/bulk/delete-links-bulk.d.ts +0 -51
  278. package/tools/links/bulk/delete-links-bulk.d.ts.map +0 -1
  279. package/tools/links/bulk/delete-links-bulk.js +0 -58
  280. package/tools/links/bulk/delete-links-bulk.js.map +0 -1
  281. package/tools/links/bulk/delete-links-bulk.mjs +0 -51
  282. package/tools/links/bulk/delete-links-bulk.mjs.map +0 -1
  283. package/tools/links/count-links.d.mts +0 -51
  284. package/tools/links/count-links.d.mts.map +0 -1
  285. package/tools/links/count-links.d.ts +0 -51
  286. package/tools/links/count-links.d.ts.map +0 -1
  287. package/tools/links/count-links.js +0 -951
  288. package/tools/links/count-links.js.map +0 -1
  289. package/tools/links/count-links.mjs +0 -944
  290. package/tools/links/count-links.mjs.map +0 -1
  291. package/tools/links/create-links.d.mts +0 -51
  292. package/tools/links/create-links.d.mts.map +0 -1
  293. package/tools/links/create-links.d.ts +0 -51
  294. package/tools/links/create-links.d.ts.map +0 -1
  295. package/tools/links/create-links.js +0 -947
  296. package/tools/links/create-links.js.map +0 -1
  297. package/tools/links/create-links.mjs +0 -940
  298. package/tools/links/create-links.mjs.map +0 -1
  299. package/tools/links/delete-links.d.mts +0 -51
  300. package/tools/links/delete-links.d.mts.map +0 -1
  301. package/tools/links/delete-links.d.ts +0 -51
  302. package/tools/links/delete-links.d.ts.map +0 -1
  303. package/tools/links/delete-links.js +0 -55
  304. package/tools/links/delete-links.js.map +0 -1
  305. package/tools/links/delete-links.mjs +0 -48
  306. package/tools/links/delete-links.mjs.map +0 -1
  307. package/tools/links/list-links.d.mts +0 -51
  308. package/tools/links/list-links.d.mts.map +0 -1
  309. package/tools/links/list-links.d.ts +0 -51
  310. package/tools/links/list-links.d.ts.map +0 -1
  311. package/tools/links/list-links.js +0 -973
  312. package/tools/links/list-links.js.map +0 -1
  313. package/tools/links/list-links.mjs +0 -966
  314. package/tools/links/list-links.mjs.map +0 -1
  315. package/tools/links/retrieve-info-links.d.mts +0 -51
  316. package/tools/links/retrieve-info-links.d.mts.map +0 -1
  317. package/tools/links/retrieve-info-links.d.ts +0 -51
  318. package/tools/links/retrieve-info-links.d.ts.map +0 -1
  319. package/tools/links/retrieve-info-links.js +0 -71
  320. package/tools/links/retrieve-info-links.js.map +0 -1
  321. package/tools/links/retrieve-info-links.mjs +0 -64
  322. package/tools/links/retrieve-info-links.mjs.map +0 -1
  323. package/tools/links/update-links.d.mts +0 -51
  324. package/tools/links/update-links.d.mts.map +0 -1
  325. package/tools/links/update-links.d.ts +0 -51
  326. package/tools/links/update-links.d.ts.map +0 -1
  327. package/tools/links/update-links.js +0 -953
  328. package/tools/links/update-links.js.map +0 -1
  329. package/tools/links/update-links.mjs +0 -946
  330. package/tools/links/update-links.mjs.map +0 -1
  331. package/tools/links/upsert-links.d.mts +0 -51
  332. package/tools/links/upsert-links.d.mts.map +0 -1
  333. package/tools/links/upsert-links.d.ts +0 -51
  334. package/tools/links/upsert-links.d.ts.map +0 -1
  335. package/tools/links/upsert-links.js +0 -947
  336. package/tools/links/upsert-links.js.map +0 -1
  337. package/tools/links/upsert-links.mjs +0 -940
  338. package/tools/links/upsert-links.mjs.map +0 -1
  339. package/tools/projects/list-projects.d.mts +0 -51
  340. package/tools/projects/list-projects.d.mts.map +0 -1
  341. package/tools/projects/list-projects.d.ts +0 -51
  342. package/tools/projects/list-projects.d.ts.map +0 -1
  343. package/tools/projects/list-projects.js +0 -51
  344. package/tools/projects/list-projects.js.map +0 -1
  345. package/tools/projects/list-projects.mjs +0 -44
  346. package/tools/projects/list-projects.mjs.map +0 -1
  347. package/tools/projects/retrieve-projects.d.mts +0 -51
  348. package/tools/projects/retrieve-projects.d.mts.map +0 -1
  349. package/tools/projects/retrieve-projects.d.ts +0 -51
  350. package/tools/projects/retrieve-projects.d.ts.map +0 -1
  351. package/tools/projects/retrieve-projects.js +0 -55
  352. package/tools/projects/retrieve-projects.js.map +0 -1
  353. package/tools/projects/retrieve-projects.mjs +0 -48
  354. package/tools/projects/retrieve-projects.mjs.map +0 -1
  355. package/tools/qrcodes/bulk/create-qrcodes-bulk.d.mts +0 -51
  356. package/tools/qrcodes/bulk/create-qrcodes-bulk.d.mts.map +0 -1
  357. package/tools/qrcodes/bulk/create-qrcodes-bulk.d.ts +0 -51
  358. package/tools/qrcodes/bulk/create-qrcodes-bulk.d.ts.map +0 -1
  359. package/tools/qrcodes/bulk/create-qrcodes-bulk.js +0 -1068
  360. package/tools/qrcodes/bulk/create-qrcodes-bulk.js.map +0 -1
  361. package/tools/qrcodes/bulk/create-qrcodes-bulk.mjs +0 -1061
  362. package/tools/qrcodes/bulk/create-qrcodes-bulk.mjs.map +0 -1
  363. package/tools/qrcodes/bulk/delete-qrcodes-bulk.d.mts +0 -51
  364. package/tools/qrcodes/bulk/delete-qrcodes-bulk.d.mts.map +0 -1
  365. package/tools/qrcodes/bulk/delete-qrcodes-bulk.d.ts +0 -51
  366. package/tools/qrcodes/bulk/delete-qrcodes-bulk.d.ts.map +0 -1
  367. package/tools/qrcodes/bulk/delete-qrcodes-bulk.js +0 -58
  368. package/tools/qrcodes/bulk/delete-qrcodes-bulk.js.map +0 -1
  369. package/tools/qrcodes/bulk/delete-qrcodes-bulk.mjs +0 -51
  370. package/tools/qrcodes/bulk/delete-qrcodes-bulk.mjs.map +0 -1
  371. package/tools/qrcodes/count-qrcodes.d.mts +0 -51
  372. package/tools/qrcodes/count-qrcodes.d.mts.map +0 -1
  373. package/tools/qrcodes/count-qrcodes.d.ts +0 -51
  374. package/tools/qrcodes/count-qrcodes.d.ts.map +0 -1
  375. package/tools/qrcodes/count-qrcodes.js +0 -215
  376. package/tools/qrcodes/count-qrcodes.js.map +0 -1
  377. package/tools/qrcodes/count-qrcodes.mjs +0 -208
  378. package/tools/qrcodes/count-qrcodes.mjs.map +0 -1
  379. package/tools/qrcodes/create-qrcodes.d.mts +0 -51
  380. package/tools/qrcodes/create-qrcodes.d.mts.map +0 -1
  381. package/tools/qrcodes/create-qrcodes.d.ts +0 -51
  382. package/tools/qrcodes/create-qrcodes.d.ts.map +0 -1
  383. package/tools/qrcodes/create-qrcodes.js +0 -1059
  384. package/tools/qrcodes/create-qrcodes.js.map +0 -1
  385. package/tools/qrcodes/create-qrcodes.mjs +0 -1052
  386. package/tools/qrcodes/create-qrcodes.mjs.map +0 -1
  387. package/tools/qrcodes/delete-qrcodes.d.mts +0 -51
  388. package/tools/qrcodes/delete-qrcodes.d.mts.map +0 -1
  389. package/tools/qrcodes/delete-qrcodes.d.ts +0 -51
  390. package/tools/qrcodes/delete-qrcodes.d.ts.map +0 -1
  391. package/tools/qrcodes/delete-qrcodes.js +0 -55
  392. package/tools/qrcodes/delete-qrcodes.js.map +0 -1
  393. package/tools/qrcodes/delete-qrcodes.mjs +0 -48
  394. package/tools/qrcodes/delete-qrcodes.mjs.map +0 -1
  395. package/tools/qrcodes/list-qrcodes.d.mts +0 -51
  396. package/tools/qrcodes/list-qrcodes.d.mts.map +0 -1
  397. package/tools/qrcodes/list-qrcodes.d.ts +0 -51
  398. package/tools/qrcodes/list-qrcodes.d.ts.map +0 -1
  399. package/tools/qrcodes/list-qrcodes.js +0 -217
  400. package/tools/qrcodes/list-qrcodes.js.map +0 -1
  401. package/tools/qrcodes/list-qrcodes.mjs +0 -210
  402. package/tools/qrcodes/list-qrcodes.mjs.map +0 -1
  403. package/tools/qrcodes/retrieve-qrcodes.d.mts +0 -51
  404. package/tools/qrcodes/retrieve-qrcodes.d.mts.map +0 -1
  405. package/tools/qrcodes/retrieve-qrcodes.d.ts +0 -51
  406. package/tools/qrcodes/retrieve-qrcodes.d.ts.map +0 -1
  407. package/tools/qrcodes/retrieve-qrcodes.js +0 -65
  408. package/tools/qrcodes/retrieve-qrcodes.js.map +0 -1
  409. package/tools/qrcodes/retrieve-qrcodes.mjs +0 -58
  410. package/tools/qrcodes/retrieve-qrcodes.mjs.map +0 -1
  411. package/tools/qrcodes/update-qrcodes.d.mts +0 -51
  412. package/tools/qrcodes/update-qrcodes.d.mts.map +0 -1
  413. package/tools/qrcodes/update-qrcodes.d.ts +0 -51
  414. package/tools/qrcodes/update-qrcodes.d.ts.map +0 -1
  415. package/tools/qrcodes/update-qrcodes.js +0 -1065
  416. package/tools/qrcodes/update-qrcodes.js.map +0 -1
  417. package/tools/qrcodes/update-qrcodes.mjs +0 -1058
  418. package/tools/qrcodes/update-qrcodes.mjs.map +0 -1
  419. package/tools/qrcodes/upsert-qrcodes.d.mts +0 -51
  420. package/tools/qrcodes/upsert-qrcodes.d.mts.map +0 -1
  421. package/tools/qrcodes/upsert-qrcodes.d.ts +0 -51
  422. package/tools/qrcodes/upsert-qrcodes.d.ts.map +0 -1
  423. package/tools/qrcodes/upsert-qrcodes.js +0 -1059
  424. package/tools/qrcodes/upsert-qrcodes.js.map +0 -1
  425. package/tools/qrcodes/upsert-qrcodes.mjs +0 -1052
  426. package/tools/qrcodes/upsert-qrcodes.mjs.map +0 -1
  427. package/tools/tags/create-tags.d.mts +0 -51
  428. package/tools/tags/create-tags.d.mts.map +0 -1
  429. package/tools/tags/create-tags.d.ts +0 -51
  430. package/tools/tags/create-tags.d.ts.map +0 -1
  431. package/tools/tags/create-tags.js +0 -58
  432. package/tools/tags/create-tags.js.map +0 -1
  433. package/tools/tags/create-tags.mjs +0 -51
  434. package/tools/tags/create-tags.mjs.map +0 -1
  435. package/tools/tags/delete-tags.d.mts +0 -51
  436. package/tools/tags/delete-tags.d.mts.map +0 -1
  437. package/tools/tags/delete-tags.d.ts +0 -51
  438. package/tools/tags/delete-tags.d.ts.map +0 -1
  439. package/tools/tags/delete-tags.js +0 -55
  440. package/tools/tags/delete-tags.js.map +0 -1
  441. package/tools/tags/delete-tags.mjs +0 -48
  442. package/tools/tags/delete-tags.mjs.map +0 -1
  443. package/tools/tags/list-tags.d.mts +0 -51
  444. package/tools/tags/list-tags.d.mts.map +0 -1
  445. package/tools/tags/list-tags.d.ts +0 -51
  446. package/tools/tags/list-tags.d.ts.map +0 -1
  447. package/tools/tags/list-tags.js +0 -87
  448. package/tools/tags/list-tags.js.map +0 -1
  449. package/tools/tags/list-tags.mjs +0 -80
  450. package/tools/tags/list-tags.mjs.map +0 -1
  451. package/tools/tags/update-tags.d.mts +0 -51
  452. package/tools/tags/update-tags.d.mts.map +0 -1
  453. package/tools/tags/update-tags.d.ts +0 -51
  454. package/tools/tags/update-tags.d.ts.map +0 -1
  455. package/tools/tags/update-tags.js +0 -63
  456. package/tools/tags/update-tags.js.map +0 -1
  457. package/tools/tags/update-tags.mjs +0 -56
  458. package/tools/tags/update-tags.mjs.map +0 -1
  459. package/tools/track/track-lead-track.d.mts +0 -51
  460. package/tools/track/track-lead-track.d.mts.map +0 -1
  461. package/tools/track/track-lead-track.d.ts +0 -51
  462. package/tools/track/track-lead-track.d.ts.map +0 -1
  463. package/tools/track/track-lead-track.js +0 -87
  464. package/tools/track/track-lead-track.js.map +0 -1
  465. package/tools/track/track-lead-track.mjs +0 -80
  466. package/tools/track/track-lead-track.mjs.map +0 -1
  467. package/tools/track/track-sale-track.d.mts +0 -51
  468. package/tools/track/track-sale-track.d.mts.map +0 -1
  469. package/tools/track/track-sale-track.d.ts +0 -51
  470. package/tools/track/track-sale-track.d.ts.map +0 -1
  471. package/tools/track/track-sale-track.js +0 -99
  472. package/tools/track/track-sale-track.js.map +0 -1
  473. package/tools/track/track-sale-track.mjs +0 -92
  474. package/tools/track/track-sale-track.mjs.map +0 -1
  475. package/tools/types.d.mts.map +0 -1
  476. package/tools/types.d.ts.map +0 -1
  477. package/tools/types.js.map +0 -1
  478. package/tools/types.mjs.map +0 -1
  479. package/tools.d.mts +0 -2
  480. package/tools.d.mts.map +0 -1
  481. package/tools.d.ts +0 -2
  482. package/tools.d.ts.map +0 -1
  483. package/tools.js +0 -18
  484. package/tools.js.map +0 -1
  485. package/tools.mjs +0 -2
  486. package/tools.mjs.map +0 -1
  487. /package/{tools/types.js → types.js} +0 -0
  488. /package/{tools/types.mjs → types.mjs} +0 -0
@@ -0,0 +1,304 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import path from 'node:path';
4
+ import util from 'node:util';
5
+ import Fuse from 'fuse.js';
6
+ import ts from 'typescript';
7
+ import { WorkerOutput } from './code-tool-types';
8
+ import { Codeqr, ClientOptions } from '@codeqr/ts';
9
+
10
+ async function tseval(code: string) {
11
+ return import('data:application/typescript;charset=utf-8;base64,' + Buffer.from(code).toString('base64'));
12
+ }
13
+
14
+ function getRunFunctionSource(code: string): {
15
+ type: 'declaration' | 'expression';
16
+ client: string | undefined;
17
+ code: string;
18
+ } | null {
19
+ const sourceFile = ts.createSourceFile('code.ts', code, ts.ScriptTarget.Latest, true);
20
+ const printer = ts.createPrinter();
21
+
22
+ for (const statement of sourceFile.statements) {
23
+ // Check for top-level function declarations
24
+ if (ts.isFunctionDeclaration(statement)) {
25
+ if (statement.name?.text === 'run') {
26
+ return {
27
+ type: 'declaration',
28
+ client: statement.parameters[0]?.name.getText(),
29
+ code: printer.printNode(ts.EmitHint.Unspecified, statement.body!, sourceFile),
30
+ };
31
+ }
32
+ }
33
+
34
+ // Check for variable declarations: const run = () => {} or const run = function() {}
35
+ if (ts.isVariableStatement(statement)) {
36
+ for (const declaration of statement.declarationList.declarations) {
37
+ if (
38
+ ts.isIdentifier(declaration.name) &&
39
+ declaration.name.text === 'run' &&
40
+ // Check if it's initialized with a function
41
+ declaration.initializer &&
42
+ (ts.isFunctionExpression(declaration.initializer) || ts.isArrowFunction(declaration.initializer))
43
+ ) {
44
+ return {
45
+ type: 'expression',
46
+ client: declaration.initializer.parameters[0]?.name.getText(),
47
+ code: printer.printNode(ts.EmitHint.Unspecified, declaration.initializer, sourceFile),
48
+ };
49
+ }
50
+ }
51
+ }
52
+ }
53
+
54
+ return null;
55
+ }
56
+
57
+ function getTSDiagnostics(code: string): string[] {
58
+ const functionSource = getRunFunctionSource(code)!;
59
+ const codeWithImport = [
60
+ 'import { Codeqr } from "@codeqr/ts";',
61
+ functionSource.type === 'declaration' ?
62
+ `async function run(${functionSource.client}: Codeqr)`
63
+ : `const run: (${functionSource.client}: Codeqr) => Promise<unknown> =`,
64
+ functionSource.code,
65
+ ].join('\n');
66
+ const sourcePath = path.resolve('code.ts');
67
+ const ast = ts.createSourceFile(sourcePath, codeWithImport, ts.ScriptTarget.Latest, true);
68
+ const options = ts.getDefaultCompilerOptions();
69
+ options.target = ts.ScriptTarget.Latest;
70
+ options.module = ts.ModuleKind.NodeNext;
71
+ options.moduleResolution = ts.ModuleResolutionKind.NodeNext;
72
+ const host = ts.createCompilerHost(options, true);
73
+ const newHost: typeof host = {
74
+ ...host,
75
+ getSourceFile: (...args) => {
76
+ if (path.resolve(args[0]) === sourcePath) {
77
+ return ast;
78
+ }
79
+ return host.getSourceFile(...args);
80
+ },
81
+ readFile: (...args) => {
82
+ if (path.resolve(args[0]) === sourcePath) {
83
+ return codeWithImport;
84
+ }
85
+ return host.readFile(...args);
86
+ },
87
+ fileExists: (...args) => {
88
+ if (path.resolve(args[0]) === sourcePath) {
89
+ return true;
90
+ }
91
+ return host.fileExists(...args);
92
+ },
93
+ };
94
+ const program = ts.createProgram({
95
+ options,
96
+ rootNames: [sourcePath],
97
+ host: newHost,
98
+ });
99
+ const diagnostics = ts.getPreEmitDiagnostics(program, ast);
100
+ return diagnostics.map((d) => {
101
+ const message = ts.flattenDiagnosticMessageText(d.messageText, '\n');
102
+ if (!d.file || !d.start) return `- ${message}`;
103
+ const { line: lineNumber } = ts.getLineAndCharacterOfPosition(d.file, d.start);
104
+ const line = codeWithImport.split('\n').at(lineNumber)?.trim();
105
+ return line ? `- ${message}\n ${line}` : `- ${message}`;
106
+ });
107
+ }
108
+
109
+ const fuse = new Fuse(
110
+ [
111
+ 'client.links.create',
112
+ 'client.links.delete',
113
+ 'client.links.list',
114
+ 'client.links.retrieveInfo',
115
+ 'client.links.update',
116
+ 'client.links.bulk.create',
117
+ 'client.qrcodes.create',
118
+ 'client.qrcodes.delete',
119
+ 'client.qrcodes.list',
120
+ 'client.qrcodes.retrieve',
121
+ 'client.qrcodes.update',
122
+ 'client.qrcodes.bulk.create',
123
+ 'client.analytics.retrieve',
124
+ 'client.events.list',
125
+ 'client.tags.create',
126
+ 'client.tags.delete',
127
+ 'client.tags.list',
128
+ 'client.tags.update',
129
+ 'client.domains.create',
130
+ 'client.domains.delete',
131
+ 'client.domains.list',
132
+ 'client.domains.update',
133
+ 'client.track.trackLead',
134
+ 'client.track.trackSale',
135
+ 'client.projects.list',
136
+ ],
137
+ { threshold: 1, shouldSort: true },
138
+ );
139
+
140
+ function getMethodSuggestions(fullyQualifiedMethodName: string): string[] {
141
+ return fuse
142
+ .search(fullyQualifiedMethodName)
143
+ .map(({ item }) => item)
144
+ .slice(0, 5);
145
+ }
146
+
147
+ const proxyToObj = new WeakMap<any, any>();
148
+ const objToProxy = new WeakMap<any, any>();
149
+
150
+ type ClientProxyConfig = {
151
+ path: string[];
152
+ isBelievedBad?: boolean;
153
+ };
154
+
155
+ function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T {
156
+ let proxy: T = objToProxy.get(obj);
157
+
158
+ if (!proxy) {
159
+ proxy = new Proxy(obj, {
160
+ get(target, prop, receiver) {
161
+ const propPath = [...path, String(prop)];
162
+ const value = Reflect.get(target, prop, receiver);
163
+
164
+ if (isBelievedBad || (!(prop in target) && value === undefined)) {
165
+ // If we're accessing a path that doesn't exist, it will probably eventually error.
166
+ // Let's proxy it and mark it bad so that we can control the error message.
167
+ // We proxy an empty class so that an invocation or construction attempt is possible.
168
+ return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true });
169
+ }
170
+
171
+ if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
172
+ return makeSdkProxy(value, { path: propPath, isBelievedBad });
173
+ }
174
+
175
+ return value;
176
+ },
177
+
178
+ apply(target, thisArg, args) {
179
+ if (isBelievedBad || typeof target !== 'function') {
180
+ const fullyQualifiedMethodName = path.join('.');
181
+ const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
182
+ throw new Error(
183
+ `${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`,
184
+ );
185
+ }
186
+
187
+ return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
188
+ },
189
+
190
+ construct(target, args, newTarget) {
191
+ if (isBelievedBad || typeof target !== 'function') {
192
+ const fullyQualifiedMethodName = path.join('.');
193
+ const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
194
+ throw new Error(
195
+ `${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`,
196
+ );
197
+ }
198
+
199
+ return Reflect.construct(target, args, newTarget);
200
+ },
201
+ });
202
+
203
+ objToProxy.set(obj, proxy);
204
+ proxyToObj.set(proxy, obj);
205
+ }
206
+
207
+ return proxy;
208
+ }
209
+
210
+ function parseError(code: string, error: unknown): string | undefined {
211
+ if (!(error instanceof Error)) return;
212
+ const cause = error.cause instanceof Error ? `: ${error.cause.message}` : '';
213
+ const message = error.name ? `${error.name}: ${error.message}${cause}` : `${error.message}${cause}`;
214
+ try {
215
+ // Deno uses V8; the first "<anonymous>:LINE:COLUMN" is the top of stack.
216
+ const lineNumber = error.stack?.match(/<anonymous>:([0-9]+):[0-9]+/)?.[1];
217
+ // -1 for the zero-based indexing
218
+ const line =
219
+ lineNumber &&
220
+ code
221
+ .split('\n')
222
+ .at(parseInt(lineNumber, 10) - 1)
223
+ ?.trim();
224
+ return line ? `${message}\n at line ${lineNumber}\n ${line}` : message;
225
+ } catch {
226
+ return message;
227
+ }
228
+ }
229
+
230
+ const fetch = async (req: Request): Promise<Response> => {
231
+ const { opts, code } = (await req.json()) as { opts: ClientOptions; code: string };
232
+
233
+ const runFunctionSource = code ? getRunFunctionSource(code) : null;
234
+ if (!runFunctionSource) {
235
+ const message =
236
+ code ?
237
+ 'The code is missing a top-level `run` function.'
238
+ : 'The code argument is missing. Provide one containing a top-level `run` function.';
239
+ return Response.json(
240
+ {
241
+ is_error: true,
242
+ result: `${message} Write code within this template:\n\n\`\`\`\nasync function run(client) {\n // Fill this out\n}\n\`\`\``,
243
+ log_lines: [],
244
+ err_lines: [],
245
+ } satisfies WorkerOutput,
246
+ { status: 400, statusText: 'Code execution error' },
247
+ );
248
+ }
249
+
250
+ const diagnostics = getTSDiagnostics(code);
251
+ if (diagnostics.length > 0) {
252
+ return Response.json(
253
+ {
254
+ is_error: true,
255
+ result: `The code contains TypeScript diagnostics:\n${diagnostics.join('\n')}`,
256
+ log_lines: [],
257
+ err_lines: [],
258
+ } satisfies WorkerOutput,
259
+ { status: 400, statusText: 'Code execution error' },
260
+ );
261
+ }
262
+
263
+ const client = new Codeqr({
264
+ ...opts,
265
+ });
266
+
267
+ const log_lines: string[] = [];
268
+ const err_lines: string[] = [];
269
+ const originalConsole = globalThis.console;
270
+ globalThis.console = {
271
+ ...originalConsole,
272
+ log: (...args: unknown[]) => {
273
+ log_lines.push(util.format(...args));
274
+ },
275
+ error: (...args: unknown[]) => {
276
+ err_lines.push(util.format(...args));
277
+ },
278
+ };
279
+ try {
280
+ let run_ = async (client: any) => {};
281
+ run_ = (await tseval(`${code}\nexport default run;`)).default;
282
+ const result = await run_(makeSdkProxy(client, { path: ['client'] }));
283
+ return Response.json({
284
+ is_error: false,
285
+ result,
286
+ log_lines,
287
+ err_lines,
288
+ } satisfies WorkerOutput);
289
+ } catch (e) {
290
+ return Response.json(
291
+ {
292
+ is_error: true,
293
+ result: parseError(code, e),
294
+ log_lines,
295
+ err_lines,
296
+ } satisfies WorkerOutput,
297
+ { status: 400, statusText: 'Code execution error' },
298
+ );
299
+ } finally {
300
+ globalThis.console = originalConsole;
301
+ }
302
+ };
303
+
304
+ export default { fetch };
package/src/code-tool.ts CHANGED
@@ -1,9 +1,35 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { Metadata, ToolCallResult, asTextContentResult } from './tools/types';
3
+ import { ContentBlock, McpRequestContext, McpTool, Metadata, ToolCallResult, asErrorResult } from './types';
4
4
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
5
- import { readEnv } from './server';
6
- import { WorkerSuccess } from './code-tool-types';
5
+ import { WorkerOutput } from './code-tool-types';
6
+ import { getLogger } from './logger';
7
+ import { SdkMethod } from './methods';
8
+ import { McpCodeExecutionMode } from './options';
9
+ import { ClientOptions } from '@codeqr/ts';
10
+
11
+ const prompt = `Runs JavaScript code to interact with the Codeqr API.
12
+
13
+ You are a skilled TypeScript programmer writing code to interface with the service.
14
+ Define an async function named "run" that takes a single parameter of an initialized SDK client and it will be run.
15
+ For example:
16
+
17
+ \`\`\`
18
+ async function run(client) {
19
+ const link = await client.links.create({ url: 'REPLACE_ME' });
20
+
21
+ console.log(link.id);
22
+ }
23
+ \`\`\`
24
+
25
+ You will be returned anything that your function returns, plus the results of any console.log statements.
26
+ Do not add try-catch blocks for single API calls. The tool will handle errors for you.
27
+ Do not add comments unless necessary for generating better code.
28
+ Code will run in a container, and cannot interact with the network outside of the given SDK client.
29
+ Variables will not persist between calls, so make sure to return or log any data you might need later.
30
+ Remember that you are writing TypeScript code, so you need to be careful with your types.
31
+ Always type dynamic key-value stores explicitly as Record<string, YourValueType> instead of {}.`;
32
+
7
33
  /**
8
34
  * A tool that runs code against a copy of the SDK.
9
35
  *
@@ -11,49 +37,279 @@ import { WorkerSuccess } from './code-tool-types';
11
37
  * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
12
38
  * a generic endpoint that can be used to invoke any endpoint with the provided arguments.
13
39
  *
14
- * @param endpoints - The endpoints to include in the list.
40
+ * @param blockedMethods - The methods to block for code execution. Blocking is done by simple string
41
+ * matching, so it is not secure against obfuscation. For stronger security, block in the downstream API
42
+ * with limited API keys.
43
+ * @param codeExecutionMode - Whether to execute code in a local Deno environment or in a remote
44
+ * sandbox environment hosted by Stainless.
15
45
  */
16
- export async function codeTool() {
46
+ export function codeTool({
47
+ blockedMethods,
48
+ codeExecutionMode,
49
+ }: {
50
+ blockedMethods: SdkMethod[] | undefined;
51
+ codeExecutionMode: McpCodeExecutionMode;
52
+ }): McpTool {
17
53
  const metadata: Metadata = { resource: 'all', operation: 'write', tags: [] };
18
54
  const tool: Tool = {
19
55
  name: 'execute',
20
- description:
21
- 'Runs JavaScript code to interact with the API.\n\nYou are a skilled programmer writing code to interface with the service.\nDefine an async function named "run" that takes a single parameter of an initialized SDK client and it will be run.\nWrite code within this template:\n\n```\nasync function run(client) {\n // Fill this out\n}\n```\n\nYou will be returned anything that your function returns, plus the results of any console.log statements.\nIf any code triggers an error, the tool will return an error response, so you do not need to add error handling unless you want to output something more helpful than the raw error.\nIt is not necessary to add comments to code, unless by adding those comments you believe that you can generate better code.\nThis code will run in a container, and you will not be able to use fetch or otherwise interact with the network calls other than through the client you are given.\nAny variables you define won\'t live between successive uses of this call, so make sure to return or log any data you might need later.',
22
- inputSchema: { type: 'object', properties: { code: { type: 'string' } } },
56
+ description: prompt,
57
+ inputSchema: {
58
+ type: 'object',
59
+ properties: {
60
+ code: {
61
+ type: 'string',
62
+ description: 'Code to execute.',
63
+ },
64
+ intent: {
65
+ type: 'string',
66
+ description: 'Task you are trying to perform. Used for improving the service.',
67
+ },
68
+ },
69
+ required: ['code'],
70
+ },
23
71
  };
24
- const handler = async (_: unknown, args: any): Promise<ToolCallResult> => {
25
- const code = args.code as string;
26
72
 
27
- // this is not required, but passing a Stainless API key for the matching project_name
28
- // will allow you to run code-mode queries against non-published versions of your SDK.
29
- const stainlessAPIKey = readEnv('STAINLESS_API_KEY');
30
- const codeModeEndpoint =
31
- readEnv('CODE_MODE_ENDPOINT_URL') ?? 'https://api.stainless.com/api/ai/code-tool';
32
-
33
- const res = await fetch(codeModeEndpoint, {
34
- method: 'POST',
35
- headers: {
36
- ...(stainlessAPIKey && { Authorization: stainlessAPIKey }),
37
- 'Content-Type': 'application/json',
38
- client_envs: JSON.stringify({ CODEQR_API_KEY: readEnv('CODEQR_API_KEY') }),
39
- },
40
- body: JSON.stringify({
41
- project_name: 'codeqr',
42
- client_opts: {},
43
- code,
44
- }),
45
- });
73
+ const logger = getLogger();
46
74
 
47
- if (!res.ok) {
48
- throw new Error(
49
- `${res.status}: ${
50
- res.statusText
51
- } error when trying to contact Code Tool server. Details: ${await res.text()}`,
52
- );
75
+ const handler = async ({
76
+ reqContext,
77
+ args,
78
+ }: {
79
+ reqContext: McpRequestContext;
80
+ args: any;
81
+ }): Promise<ToolCallResult> => {
82
+ const code = args.code as string;
83
+ // Do very basic blocking of code that includes forbidden method names.
84
+ //
85
+ // WARNING: This is not secure against obfuscation and other evasion methods. If
86
+ // stronger security blocks are required, then these should be enforced in the downstream
87
+ // API (e.g., by having users call the MCP server with API keys with limited permissions).
88
+ if (blockedMethods) {
89
+ const blockedMatches = blockedMethods.filter((method) => code.includes(method.fullyQualifiedName));
90
+ if (blockedMatches.length > 0) {
91
+ return asErrorResult(
92
+ `The following methods have been blocked by the MCP server and cannot be used in code execution: ${blockedMatches
93
+ .map((m) => m.fullyQualifiedName)
94
+ .join(', ')}`,
95
+ );
96
+ }
53
97
  }
54
98
 
55
- return asTextContentResult((await res.json()) as WorkerSuccess);
99
+ let result: ToolCallResult;
100
+ const startTime = Date.now();
101
+
102
+ logger.debug('Executing code in local Deno environment');
103
+ result = await localDenoHandler({ reqContext, args });
104
+
105
+ logger.info(
106
+ {
107
+ codeExecutionMode,
108
+ durationMs: Date.now() - startTime,
109
+ isError: result.isError,
110
+ contentRows: result.content?.length ?? 0,
111
+ },
112
+ 'Got code tool execution result',
113
+ );
114
+ return result;
56
115
  };
57
116
 
58
117
  return { metadata, tool, handler };
59
118
  }
119
+
120
+ const localDenoHandler = async ({
121
+ reqContext,
122
+ args,
123
+ }: {
124
+ reqContext: McpRequestContext;
125
+ args: unknown;
126
+ }): Promise<ToolCallResult> => {
127
+ const fs = await import('node:fs');
128
+ const path = await import('node:path');
129
+ const url = await import('node:url');
130
+ const { newDenoHTTPWorker } = await import('@valtown/deno-http-worker');
131
+ const { getWorkerPath } = await import('./code-tool-paths.cjs');
132
+ const workerPath = getWorkerPath();
133
+
134
+ const client = reqContext.client;
135
+ const baseURLHostname = new URL(client.baseURL).hostname;
136
+ const { code } = args as { code: string };
137
+
138
+ let denoPath: string;
139
+
140
+ const packageRoot = path.resolve(path.dirname(workerPath), '..');
141
+ const packageNodeModulesPath = path.resolve(packageRoot, 'node_modules');
142
+
143
+ // Check if deno is in PATH
144
+ const { execSync } = await import('node:child_process');
145
+ try {
146
+ execSync('command -v deno', { stdio: 'ignore' });
147
+ denoPath = 'deno';
148
+ } catch {
149
+ try {
150
+ // Use deno binary in node_modules if it's found
151
+ const denoNodeModulesPath = path.resolve(packageNodeModulesPath, 'deno', 'bin.cjs');
152
+ await fs.promises.access(denoNodeModulesPath, fs.constants.X_OK);
153
+ denoPath = denoNodeModulesPath;
154
+ } catch {
155
+ return asErrorResult(
156
+ 'Deno is required for code execution but was not found. ' +
157
+ 'Install it from https://deno.land or run: npm install deno',
158
+ );
159
+ }
160
+ }
161
+
162
+ const allowReadPaths = [
163
+ 'code-tool-worker.mjs',
164
+ `${workerPath.replace(/([\/\\]node_modules)[\/\\].+$/, '$1')}/`,
165
+ packageRoot,
166
+ ];
167
+
168
+ // Follow symlinks in node_modules to allow read access to workspace-linked packages
169
+ try {
170
+ const sdkPkgName = '@codeqr/ts';
171
+ const sdkDir = path.resolve(packageNodeModulesPath, sdkPkgName);
172
+ const realSdkDir = fs.realpathSync(sdkDir);
173
+ if (realSdkDir !== sdkDir) {
174
+ allowReadPaths.push(realSdkDir);
175
+ }
176
+ } catch {
177
+ // Ignore if symlink resolution fails
178
+ }
179
+
180
+ const allowRead = allowReadPaths.join(',');
181
+
182
+ const worker = await newDenoHTTPWorker(url.pathToFileURL(workerPath), {
183
+ denoExecutable: denoPath,
184
+ runFlags: [
185
+ `--node-modules-dir=manual`,
186
+ `--allow-read=${allowRead}`,
187
+ `--allow-net=${baseURLHostname}`,
188
+ // Allow environment variables because instantiating the client will try to read from them,
189
+ // even though they are not set.
190
+ '--allow-env',
191
+ ],
192
+ printOutput: true,
193
+ spawnOptions: {
194
+ cwd: path.dirname(workerPath),
195
+ // Merge any upstream client envs into the Deno subprocess environment,
196
+ // with the upstream env vars taking precedence.
197
+ env: { ...process.env, ...reqContext.upstreamClientEnvs },
198
+ },
199
+ });
200
+
201
+ try {
202
+ const resp = await new Promise<Response>((resolve, reject) => {
203
+ worker.addEventListener('exit', (exitCode) => {
204
+ reject(new Error(`Worker exited with code ${exitCode}`));
205
+ });
206
+
207
+ // Strip null/undefined values so that the worker SDK client can fall back to
208
+ // reading from environment variables (including any upstreamClientEnvs).
209
+ const opts = {
210
+ ...(client.baseURL != null ? { baseURL: client.baseURL } : undefined),
211
+ ...(client.apiKey != null ? { apiKey: client.apiKey } : undefined),
212
+ defaultHeaders: {
213
+ 'X-Stainless-MCP': 'true',
214
+ },
215
+ } satisfies Partial<ClientOptions> as ClientOptions;
216
+
217
+ const req = worker.request(
218
+ 'http://localhost',
219
+ {
220
+ headers: {
221
+ 'content-type': 'application/json',
222
+ },
223
+ method: 'POST',
224
+ },
225
+ (resp) => {
226
+ const body: Uint8Array[] = [];
227
+ resp.on('error', (err) => {
228
+ reject(err);
229
+ });
230
+ resp.on('data', (chunk) => {
231
+ body.push(chunk);
232
+ });
233
+ resp.on('end', () => {
234
+ resolve(
235
+ new Response(Buffer.concat(body).toString(), {
236
+ status: resp.statusCode ?? 200,
237
+ headers: resp.headers as any,
238
+ }),
239
+ );
240
+ });
241
+ },
242
+ );
243
+
244
+ const body = JSON.stringify({
245
+ opts,
246
+ code,
247
+ });
248
+
249
+ req.write(body, (err) => {
250
+ if (err != null) {
251
+ reject(err);
252
+ }
253
+ });
254
+
255
+ req.end();
256
+ });
257
+
258
+ if (resp.status === 200) {
259
+ const { result, log_lines, err_lines } = (await resp.json()) as WorkerOutput;
260
+ const returnOutput: ContentBlock | null =
261
+ result == null ? null : (
262
+ {
263
+ type: 'text',
264
+ text: typeof result === 'string' ? result : JSON.stringify(result),
265
+ }
266
+ );
267
+ const logOutput: ContentBlock | null =
268
+ log_lines.length === 0 ?
269
+ null
270
+ : {
271
+ type: 'text',
272
+ text: log_lines.join('\n'),
273
+ };
274
+ const errOutput: ContentBlock | null =
275
+ err_lines.length === 0 ?
276
+ null
277
+ : {
278
+ type: 'text',
279
+ text: 'Error output:\n' + err_lines.join('\n'),
280
+ };
281
+ return {
282
+ content: [returnOutput, logOutput, errOutput].filter((block) => block !== null),
283
+ };
284
+ } else {
285
+ const { result, log_lines, err_lines } = (await resp.json()) as WorkerOutput;
286
+ const messageOutput: ContentBlock | null =
287
+ result == null ? null : (
288
+ {
289
+ type: 'text',
290
+ text: typeof result === 'string' ? result : JSON.stringify(result),
291
+ }
292
+ );
293
+ const logOutput: ContentBlock | null =
294
+ log_lines.length === 0 ?
295
+ null
296
+ : {
297
+ type: 'text',
298
+ text: log_lines.join('\n'),
299
+ };
300
+ const errOutput: ContentBlock | null =
301
+ err_lines.length === 0 ?
302
+ null
303
+ : {
304
+ type: 'text',
305
+ text: 'Error output:\n' + err_lines.join('\n'),
306
+ };
307
+ return {
308
+ content: [messageOutput, logOutput, errOutput].filter((block) => block !== null),
309
+ isError: true,
310
+ };
311
+ }
312
+ } finally {
313
+ worker.terminate();
314
+ }
315
+ };