@beeper/desktop-mcp 4.2.3 → 4.6.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 (384) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +18 -199
  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/auth.mjs +32 -0
  10. package/auth.mjs.map +1 -0
  11. package/code-tool-types.d.mts +7 -7
  12. package/code-tool-types.d.mts.map +1 -1
  13. package/code-tool-types.d.ts +7 -7
  14. package/code-tool-types.d.ts.map +1 -1
  15. package/code-tool-worker.d.mts.map +1 -1
  16. package/code-tool-worker.d.ts.map +1 -1
  17. package/code-tool-worker.js +103 -19
  18. package/code-tool-worker.js.map +1 -1
  19. package/code-tool-worker.mjs +103 -19
  20. package/code-tool-worker.mjs.map +1 -1
  21. package/code-tool.d.mts +12 -3
  22. package/code-tool.d.mts.map +1 -1
  23. package/code-tool.d.ts +12 -3
  24. package/code-tool.d.ts.map +1 -1
  25. package/code-tool.js +276 -95
  26. package/code-tool.js.map +1 -1
  27. package/code-tool.mjs +273 -95
  28. package/code-tool.mjs.map +1 -1
  29. package/http.d.mts +6 -9
  30. package/http.d.mts.map +1 -1
  31. package/http.d.ts +6 -9
  32. package/http.d.ts.map +1 -1
  33. package/http.js +81 -47
  34. package/http.js.map +1 -1
  35. package/http.mjs +80 -43
  36. package/http.mjs.map +1 -1
  37. package/index.js +19 -50
  38. package/index.js.map +1 -1
  39. package/index.mjs +19 -50
  40. package/index.mjs.map +1 -1
  41. package/instructions.d.mts +2 -0
  42. package/instructions.d.mts.map +1 -0
  43. package/instructions.d.ts +2 -0
  44. package/instructions.d.ts.map +1 -0
  45. package/instructions.js +48 -0
  46. package/instructions.js.map +1 -0
  47. package/instructions.mjs +45 -0
  48. package/instructions.mjs.map +1 -0
  49. package/logger.d.mts +7 -0
  50. package/logger.d.mts.map +1 -0
  51. package/logger.d.ts +7 -0
  52. package/logger.d.ts.map +1 -0
  53. package/logger.js +29 -0
  54. package/logger.js.map +1 -0
  55. package/logger.mjs +22 -0
  56. package/logger.mjs.map +1 -0
  57. package/methods.d.mts +10 -0
  58. package/methods.d.mts.map +1 -0
  59. package/methods.d.ts +10 -0
  60. package/methods.d.ts.map +1 -0
  61. package/methods.js +203 -0
  62. package/methods.js.map +1 -0
  63. package/methods.mjs +199 -0
  64. package/methods.mjs.map +1 -0
  65. package/options.d.mts +9 -10
  66. package/options.d.mts.map +1 -1
  67. package/options.d.ts +9 -10
  68. package/options.d.ts.map +1 -1
  69. package/options.js +52 -363
  70. package/options.js.map +1 -1
  71. package/options.mjs +52 -363
  72. package/options.mjs.map +1 -1
  73. package/package.json +68 -82
  74. package/server.d.mts +10 -16
  75. package/server.d.mts.map +1 -1
  76. package/server.d.ts +10 -16
  77. package/server.d.ts.map +1 -1
  78. package/server.js +82 -105
  79. package/server.js.map +1 -1
  80. package/server.mjs +81 -100
  81. package/server.mjs.map +1 -1
  82. package/src/auth.ts +42 -0
  83. package/src/code-tool-types.ts +8 -5
  84. package/src/code-tool-worker.ts +118 -34
  85. package/src/code-tool.ts +359 -118
  86. package/src/http.ts +99 -50
  87. package/src/index.ts +22 -63
  88. package/src/instructions.ts +67 -0
  89. package/src/logger.ts +28 -0
  90. package/src/methods.ts +224 -0
  91. package/src/options.ts +69 -390
  92. package/src/server.ts +95 -121
  93. package/src/stdio.ts +6 -5
  94. package/src/{tools/types.ts → types.ts} +25 -5
  95. package/src/util.ts +25 -0
  96. package/stdio.d.mts +1 -1
  97. package/stdio.d.mts.map +1 -1
  98. package/stdio.d.ts +1 -1
  99. package/stdio.d.ts.map +1 -1
  100. package/stdio.js +5 -4
  101. package/stdio.js.map +1 -1
  102. package/stdio.mjs +5 -4
  103. package/stdio.mjs.map +1 -1
  104. package/{tools/types.d.mts → types.d.mts} +10 -2
  105. package/types.d.mts.map +1 -0
  106. package/{tools/types.d.ts → types.d.ts} +10 -2
  107. package/types.d.ts.map +1 -0
  108. package/{tools/types.js → types.js} +12 -0
  109. package/types.js.map +1 -0
  110. package/{tools/types.mjs → types.mjs} +11 -0
  111. package/types.mjs.map +1 -0
  112. package/util.d.mts +4 -0
  113. package/util.d.mts.map +1 -0
  114. package/util.d.ts +4 -0
  115. package/util.d.ts.map +1 -0
  116. package/util.js +30 -0
  117. package/util.js.map +1 -0
  118. package/util.mjs +24 -0
  119. package/util.mjs.map +1 -0
  120. package/compat.d.mts +0 -58
  121. package/compat.d.mts.map +0 -1
  122. package/compat.d.ts +0 -58
  123. package/compat.d.ts.map +0 -1
  124. package/compat.js +0 -387
  125. package/compat.js.map +0 -1
  126. package/compat.mjs +0 -378
  127. package/compat.mjs.map +0 -1
  128. package/docs-search-tool.d.mts +0 -44
  129. package/docs-search-tool.d.mts.map +0 -1
  130. package/docs-search-tool.d.ts +0 -44
  131. package/docs-search-tool.d.ts.map +0 -1
  132. package/docs-search-tool.js +0 -43
  133. package/docs-search-tool.js.map +0 -1
  134. package/docs-search-tool.mjs +0 -39
  135. package/docs-search-tool.mjs.map +0 -1
  136. package/dynamic-tools.d.mts +0 -12
  137. package/dynamic-tools.d.mts.map +0 -1
  138. package/dynamic-tools.d.ts +0 -12
  139. package/dynamic-tools.d.ts.map +0 -1
  140. package/dynamic-tools.js +0 -135
  141. package/dynamic-tools.js.map +0 -1
  142. package/dynamic-tools.mjs +0 -132
  143. package/dynamic-tools.mjs.map +0 -1
  144. package/filtering.d.mts +0 -2
  145. package/filtering.d.mts.map +0 -1
  146. package/filtering.d.ts +0 -2
  147. package/filtering.d.ts.map +0 -1
  148. package/filtering.js +0 -20
  149. package/filtering.js.map +0 -1
  150. package/filtering.mjs +0 -13
  151. package/filtering.mjs.map +0 -1
  152. package/handlers/get-accounts.d.mts +0 -3
  153. package/handlers/get-accounts.d.mts.map +0 -1
  154. package/handlers/get-accounts.d.ts +0 -3
  155. package/handlers/get-accounts.d.ts.map +0 -1
  156. package/handlers/get-accounts.js +0 -32
  157. package/handlers/get-accounts.js.map +0 -1
  158. package/handlers/get-accounts.mjs +0 -28
  159. package/handlers/get-accounts.mjs.map +0 -1
  160. package/handlers/get-chat.d.mts +0 -3
  161. package/handlers/get-chat.d.mts.map +0 -1
  162. package/handlers/get-chat.d.ts +0 -3
  163. package/handlers/get-chat.d.ts.map +0 -1
  164. package/handlers/get-chat.js +0 -20
  165. package/handlers/get-chat.js.map +0 -1
  166. package/handlers/get-chat.mjs +0 -16
  167. package/handlers/get-chat.mjs.map +0 -1
  168. package/handlers/index.d.mts +0 -3
  169. package/handlers/index.d.mts.map +0 -1
  170. package/handlers/index.d.ts +0 -3
  171. package/handlers/index.d.ts.map +0 -1
  172. package/handlers/index.js +0 -30
  173. package/handlers/index.js.map +0 -1
  174. package/handlers/index.mjs +0 -27
  175. package/handlers/index.mjs.map +0 -1
  176. package/handlers/list-chats.d.mts +0 -3
  177. package/handlers/list-chats.d.mts.map +0 -1
  178. package/handlers/list-chats.d.ts +0 -3
  179. package/handlers/list-chats.d.ts.map +0 -1
  180. package/handlers/list-chats.js +0 -46
  181. package/handlers/list-chats.js.map +0 -1
  182. package/handlers/list-chats.mjs +0 -42
  183. package/handlers/list-chats.mjs.map +0 -1
  184. package/handlers/list-messages.d.mts +0 -3
  185. package/handlers/list-messages.d.mts.map +0 -1
  186. package/handlers/list-messages.d.ts +0 -3
  187. package/handlers/list-messages.d.ts.map +0 -1
  188. package/handlers/list-messages.js +0 -34
  189. package/handlers/list-messages.js.map +0 -1
  190. package/handlers/list-messages.mjs +0 -30
  191. package/handlers/list-messages.mjs.map +0 -1
  192. package/handlers/open-app.d.mts +0 -3
  193. package/handlers/open-app.d.mts.map +0 -1
  194. package/handlers/open-app.d.ts +0 -3
  195. package/handlers/open-app.d.ts.map +0 -1
  196. package/handlers/open-app.js +0 -26
  197. package/handlers/open-app.js.map +0 -1
  198. package/handlers/open-app.mjs +0 -22
  199. package/handlers/open-app.mjs.map +0 -1
  200. package/handlers/search-chats.d.mts +0 -3
  201. package/handlers/search-chats.d.mts.map +0 -1
  202. package/handlers/search-chats.d.ts +0 -3
  203. package/handlers/search-chats.d.ts.map +0 -1
  204. package/handlers/search-chats.js +0 -38
  205. package/handlers/search-chats.js.map +0 -1
  206. package/handlers/search-chats.mjs +0 -34
  207. package/handlers/search-chats.mjs.map +0 -1
  208. package/handlers/search-messages.d.mts +0 -3
  209. package/handlers/search-messages.d.mts.map +0 -1
  210. package/handlers/search-messages.d.ts +0 -3
  211. package/handlers/search-messages.d.ts.map +0 -1
  212. package/handlers/search-messages.js +0 -11
  213. package/handlers/search-messages.js.map +0 -1
  214. package/handlers/search-messages.mjs +0 -7
  215. package/handlers/search-messages.mjs.map +0 -1
  216. package/handlers/search.d.mts +0 -3
  217. package/handlers/search.d.mts.map +0 -1
  218. package/handlers/search.d.ts +0 -3
  219. package/handlers/search.d.ts.map +0 -1
  220. package/handlers/search.js +0 -29
  221. package/handlers/search.js.map +0 -1
  222. package/handlers/search.mjs +0 -25
  223. package/handlers/search.mjs.map +0 -1
  224. package/handlers/send-message.d.mts +0 -3
  225. package/handlers/send-message.d.mts.map +0 -1
  226. package/handlers/send-message.d.ts +0 -3
  227. package/handlers/send-message.d.ts.map +0 -1
  228. package/handlers/send-message.js +0 -20
  229. package/handlers/send-message.js.map +0 -1
  230. package/handlers/send-message.mjs +0 -16
  231. package/handlers/send-message.mjs.map +0 -1
  232. package/handlers/utils.d.mts +0 -29
  233. package/handlers/utils.d.mts.map +0 -1
  234. package/handlers/utils.d.ts +0 -29
  235. package/handlers/utils.d.ts.map +0 -1
  236. package/handlers/utils.js +0 -296
  237. package/handlers/utils.js.map +0 -1
  238. package/handlers/utils.mjs +0 -282
  239. package/handlers/utils.mjs.map +0 -1
  240. package/headers.d.mts +0 -4
  241. package/headers.d.mts.map +0 -1
  242. package/headers.d.ts +0 -4
  243. package/headers.d.ts.map +0 -1
  244. package/headers.js +0 -22
  245. package/headers.js.map +0 -1
  246. package/headers.mjs +0 -18
  247. package/headers.mjs.map +0 -1
  248. package/src/compat.ts +0 -483
  249. package/src/docs-search-tool.ts +0 -48
  250. package/src/dynamic-tools.ts +0 -159
  251. package/src/filtering.ts +0 -14
  252. package/src/handlers/get-accounts.ts +0 -28
  253. package/src/handlers/get-chat.ts +0 -18
  254. package/src/handlers/index.ts +0 -29
  255. package/src/handlers/list-chats.ts +0 -47
  256. package/src/handlers/list-messages.ts +0 -33
  257. package/src/handlers/open-app.ts +0 -20
  258. package/src/handlers/search-chats.ts +0 -39
  259. package/src/handlers/search-messages.ts +0 -8
  260. package/src/handlers/search.ts +0 -24
  261. package/src/handlers/send-message.ts +0 -17
  262. package/src/handlers/utils.ts +0 -381
  263. package/src/headers.ts +0 -23
  264. package/src/tools/accounts/get-accounts.ts +0 -34
  265. package/src/tools/chats/archive-chat.ts +0 -43
  266. package/src/tools/chats/get-chat.ts +0 -45
  267. package/src/tools/chats/reminders/clear-chat-reminder.ts +0 -41
  268. package/src/tools/chats/reminders/set-chat-reminder.ts +0 -54
  269. package/src/tools/chats/search-chats.ts +0 -104
  270. package/src/tools/index.ts +0 -92
  271. package/src/tools/messages/list-messages.ts +0 -51
  272. package/src/tools/messages/search-messages.ts +0 -123
  273. package/src/tools/messages/send-message.ts +0 -47
  274. package/src/tools/top-level/focus-app.ts +0 -52
  275. package/src/tools/top-level/search.ts +0 -41
  276. package/src/tools.ts +0 -1
  277. package/tools/accounts/get-accounts.d.mts +0 -45
  278. package/tools/accounts/get-accounts.d.mts.map +0 -1
  279. package/tools/accounts/get-accounts.d.ts +0 -45
  280. package/tools/accounts/get-accounts.d.ts.map +0 -1
  281. package/tools/accounts/get-accounts.js +0 -31
  282. package/tools/accounts/get-accounts.js.map +0 -1
  283. package/tools/accounts/get-accounts.mjs +0 -27
  284. package/tools/accounts/get-accounts.mjs.map +0 -1
  285. package/tools/chats/archive-chat.d.mts +0 -45
  286. package/tools/chats/archive-chat.d.mts.map +0 -1
  287. package/tools/chats/archive-chat.d.ts +0 -45
  288. package/tools/chats/archive-chat.d.ts.map +0 -1
  289. package/tools/chats/archive-chat.js +0 -40
  290. package/tools/chats/archive-chat.js.map +0 -1
  291. package/tools/chats/archive-chat.mjs +0 -36
  292. package/tools/chats/archive-chat.mjs.map +0 -1
  293. package/tools/chats/get-chat.d.mts +0 -45
  294. package/tools/chats/get-chat.d.mts.map +0 -1
  295. package/tools/chats/get-chat.d.ts +0 -45
  296. package/tools/chats/get-chat.d.ts.map +0 -1
  297. package/tools/chats/get-chat.js +0 -41
  298. package/tools/chats/get-chat.js.map +0 -1
  299. package/tools/chats/get-chat.mjs +0 -37
  300. package/tools/chats/get-chat.mjs.map +0 -1
  301. package/tools/chats/reminders/clear-chat-reminder.d.mts +0 -45
  302. package/tools/chats/reminders/clear-chat-reminder.d.mts.map +0 -1
  303. package/tools/chats/reminders/clear-chat-reminder.d.ts +0 -45
  304. package/tools/chats/reminders/clear-chat-reminder.d.ts.map +0 -1
  305. package/tools/chats/reminders/clear-chat-reminder.js +0 -38
  306. package/tools/chats/reminders/clear-chat-reminder.js.map +0 -1
  307. package/tools/chats/reminders/clear-chat-reminder.mjs +0 -34
  308. package/tools/chats/reminders/clear-chat-reminder.mjs.map +0 -1
  309. package/tools/chats/reminders/set-chat-reminder.d.mts +0 -45
  310. package/tools/chats/reminders/set-chat-reminder.d.mts.map +0 -1
  311. package/tools/chats/reminders/set-chat-reminder.d.ts +0 -45
  312. package/tools/chats/reminders/set-chat-reminder.d.ts.map +0 -1
  313. package/tools/chats/reminders/set-chat-reminder.js +0 -51
  314. package/tools/chats/reminders/set-chat-reminder.js.map +0 -1
  315. package/tools/chats/reminders/set-chat-reminder.mjs +0 -47
  316. package/tools/chats/reminders/set-chat-reminder.mjs.map +0 -1
  317. package/tools/chats/search-chats.d.mts +0 -45
  318. package/tools/chats/search-chats.d.mts.map +0 -1
  319. package/tools/chats/search-chats.d.ts +0 -45
  320. package/tools/chats/search-chats.d.ts.map +0 -1
  321. package/tools/chats/search-chats.js +0 -92
  322. package/tools/chats/search-chats.js.map +0 -1
  323. package/tools/chats/search-chats.mjs +0 -88
  324. package/tools/chats/search-chats.mjs.map +0 -1
  325. package/tools/index.d.mts +0 -10
  326. package/tools/index.d.mts.map +0 -1
  327. package/tools/index.d.ts +0 -10
  328. package/tools/index.d.ts.map +0 -1
  329. package/tools/index.js +0 -76
  330. package/tools/index.js.map +0 -1
  331. package/tools/index.mjs +0 -69
  332. package/tools/index.mjs.map +0 -1
  333. package/tools/messages/list-messages.d.mts +0 -45
  334. package/tools/messages/list-messages.d.mts.map +0 -1
  335. package/tools/messages/list-messages.d.ts +0 -45
  336. package/tools/messages/list-messages.d.ts.map +0 -1
  337. package/tools/messages/list-messages.js +0 -47
  338. package/tools/messages/list-messages.js.map +0 -1
  339. package/tools/messages/list-messages.mjs +0 -43
  340. package/tools/messages/list-messages.mjs.map +0 -1
  341. package/tools/messages/search-messages.d.mts +0 -45
  342. package/tools/messages/search-messages.d.mts.map +0 -1
  343. package/tools/messages/search-messages.d.ts +0 -45
  344. package/tools/messages/search-messages.d.ts.map +0 -1
  345. package/tools/messages/search-messages.js +0 -110
  346. package/tools/messages/search-messages.js.map +0 -1
  347. package/tools/messages/search-messages.mjs +0 -106
  348. package/tools/messages/search-messages.mjs.map +0 -1
  349. package/tools/messages/send-message.d.mts +0 -45
  350. package/tools/messages/send-message.d.mts.map +0 -1
  351. package/tools/messages/send-message.d.ts +0 -45
  352. package/tools/messages/send-message.d.ts.map +0 -1
  353. package/tools/messages/send-message.js +0 -43
  354. package/tools/messages/send-message.js.map +0 -1
  355. package/tools/messages/send-message.mjs +0 -39
  356. package/tools/messages/send-message.mjs.map +0 -1
  357. package/tools/top-level/focus-app.d.mts +0 -45
  358. package/tools/top-level/focus-app.d.mts.map +0 -1
  359. package/tools/top-level/focus-app.d.ts +0 -45
  360. package/tools/top-level/focus-app.d.ts.map +0 -1
  361. package/tools/top-level/focus-app.js +0 -47
  362. package/tools/top-level/focus-app.js.map +0 -1
  363. package/tools/top-level/focus-app.mjs +0 -43
  364. package/tools/top-level/focus-app.mjs.map +0 -1
  365. package/tools/top-level/search.d.mts +0 -45
  366. package/tools/top-level/search.d.mts.map +0 -1
  367. package/tools/top-level/search.d.ts +0 -45
  368. package/tools/top-level/search.d.ts.map +0 -1
  369. package/tools/top-level/search.js +0 -37
  370. package/tools/top-level/search.js.map +0 -1
  371. package/tools/top-level/search.mjs +0 -33
  372. package/tools/top-level/search.mjs.map +0 -1
  373. package/tools/types.d.mts.map +0 -1
  374. package/tools/types.d.ts.map +0 -1
  375. package/tools/types.js.map +0 -1
  376. package/tools/types.mjs.map +0 -1
  377. package/tools.d.mts +0 -2
  378. package/tools.d.mts.map +0 -1
  379. package/tools.d.ts +0 -2
  380. package/tools.d.ts.map +0 -1
  381. package/tools.js +0 -18
  382. package/tools.js.map +0 -1
  383. package/tools.mjs +0 -2
  384. package/tools.mjs.map +0 -1
package/src/code-tool.ts CHANGED
@@ -1,13 +1,53 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { dirname } from 'node:path';
4
- import { pathToFileURL } from 'node:url';
5
- import BeeperDesktop, { ClientOptions } from '@beeper/desktop-api';
6
- import { ContentBlock, Endpoint, Metadata, ToolCallResult } from './tools/types';
7
-
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+ import url from 'node:url';
6
+ import { newDenoHTTPWorker } from '@valtown/deno-http-worker';
7
+ import { workerPath } from './code-tool-paths.cjs';
8
+ import {
9
+ ContentBlock,
10
+ McpRequestContext,
11
+ McpTool,
12
+ Metadata,
13
+ ToolCallResult,
14
+ asErrorResult,
15
+ asTextContentResult,
16
+ } from './types';
8
17
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
18
+ import { readEnv, requireValue } from './util';
19
+ import { WorkerInput, WorkerOutput } from './code-tool-types';
20
+ import { getLogger } from './logger';
21
+ import { SdkMethod } from './methods';
22
+ import { McpCodeExecutionMode } from './options';
23
+ import { ClientOptions } from '@beeper/desktop-api';
24
+
25
+ const prompt = `Runs JavaScript code to interact with the Beeper Desktop API.
26
+
27
+ You are a skilled TypeScript programmer writing code to interface with the service.
28
+ Define an async function named "run" that takes a single parameter of an initialized SDK client and it will be run.
29
+ For example:
30
+
31
+ \`\`\`
32
+ async function run(client) {
33
+ const page = await client.chats.search({
34
+ includeMuted: true,
35
+ limit: 3,
36
+ type: 'single',
37
+ });
38
+ const chat = page.items[0]
9
39
 
10
- import { WorkerInput, WorkerError, WorkerSuccess } from './code-tool-types';
40
+ console.log(chat.id);
41
+ }
42
+ \`\`\`
43
+
44
+ You will be returned anything that your function returns, plus the results of any console.log statements.
45
+ Do not add try-catch blocks for single API calls. The tool will handle errors for you.
46
+ Do not add comments unless necessary for generating better code.
47
+ Code will run in a container, and cannot interact with the network outside of the given SDK client.
48
+ Variables will not persist between calls, so make sure to return or log any data you might need later.
49
+ Remember that you are writing TypeScript code, so you need to be careful with your types.
50
+ Always type dynamic key-value stores explicitly as Record<string, YourValueType> instead of {}.`;
11
51
 
12
52
  /**
13
53
  * A tool that runs code against a copy of the SDK.
@@ -16,132 +56,333 @@ import { WorkerInput, WorkerError, WorkerSuccess } from './code-tool-types';
16
56
  * we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
17
57
  * a generic endpoint that can be used to invoke any endpoint with the provided arguments.
18
58
  *
19
- * @param endpoints - The endpoints to include in the list.
59
+ * @param blockedMethods - The methods to block for code execution. Blocking is done by simple string
60
+ * matching, so it is not secure against obfuscation. For stronger security, block in the downstream API
61
+ * with limited API keys.
62
+ * @param codeExecutionMode - Whether to execute code in a local Deno environment or in a remote
63
+ * sandbox environment hosted by Stainless.
20
64
  */
21
- export async function codeTool(): Promise<Endpoint> {
65
+ export function codeTool({
66
+ blockedMethods,
67
+ codeExecutionMode,
68
+ }: {
69
+ blockedMethods: SdkMethod[] | undefined;
70
+ codeExecutionMode: McpCodeExecutionMode;
71
+ }): McpTool {
22
72
  const metadata: Metadata = { resource: 'all', operation: 'write', tags: [] };
23
73
  const tool: Tool = {
24
74
  name: 'execute',
25
- description:
26
- '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 client named "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.',
27
- inputSchema: { type: 'object', properties: { code: { type: 'string' } } },
75
+ description: prompt,
76
+ inputSchema: {
77
+ type: 'object',
78
+ properties: {
79
+ code: {
80
+ type: 'string',
81
+ description: 'Code to execute.',
82
+ },
83
+ intent: {
84
+ type: 'string',
85
+ description: 'Task you are trying to perform. Used for improving the service.',
86
+ },
87
+ },
88
+ required: ['code'],
89
+ },
28
90
  };
29
91
 
30
- // Import dynamically to avoid failing at import time in cases where the environment is not well-supported.
31
- const { newDenoHTTPWorker } = await import('@valtown/deno-http-worker');
32
- const { workerPath } = await import('./code-tool-paths.cjs');
33
-
34
- const handler = async (client: BeeperDesktop, args: unknown): Promise<ToolCallResult> => {
35
- const baseURLHostname = new URL(client.baseURL).hostname;
36
- const { code } = args as { code: string };
37
-
38
- const worker = await newDenoHTTPWorker(pathToFileURL(workerPath), {
39
- runFlags: [
40
- `--node-modules-dir=manual`,
41
- `--allow-read=code-tool-worker.mjs,${workerPath.replace(/([\/\\]node_modules)[\/\\].+$/, '$1')}/`,
42
- `--allow-net=${baseURLHostname}`,
43
- // Allow environment variables because instantiating the client will try to read from them,
44
- // even though they are not set.
45
- '--allow-env',
46
- ],
47
- printOutput: true,
48
- spawnOptions: {
49
- cwd: dirname(workerPath),
92
+ const logger = getLogger();
93
+
94
+ const handler = async ({
95
+ reqContext,
96
+ args,
97
+ }: {
98
+ reqContext: McpRequestContext;
99
+ args: any;
100
+ }): Promise<ToolCallResult> => {
101
+ const code = args.code as string;
102
+ // Do very basic blocking of code that includes forbidden method names.
103
+ //
104
+ // WARNING: This is not secure against obfuscation and other evasion methods. If
105
+ // stronger security blocks are required, then these should be enforced in the downstream
106
+ // API (e.g., by having users call the MCP server with API keys with limited permissions).
107
+ if (blockedMethods) {
108
+ const blockedMatches = blockedMethods.filter((method) => code.includes(method.fullyQualifiedName));
109
+ if (blockedMatches.length > 0) {
110
+ return asErrorResult(
111
+ `The following methods have been blocked by the MCP server and cannot be used in code execution: ${blockedMatches
112
+ .map((m) => m.fullyQualifiedName)
113
+ .join(', ')}`,
114
+ );
115
+ }
116
+ }
117
+
118
+ let result: ToolCallResult;
119
+ const startTime = Date.now();
120
+
121
+ if (codeExecutionMode === 'local') {
122
+ logger.debug('Executing code in local Deno environment');
123
+ result = await localDenoHandler({ reqContext, args });
124
+ } else {
125
+ logger.debug('Executing code in remote Stainless environment');
126
+ result = await remoteStainlessHandler({ reqContext, args });
127
+ }
128
+
129
+ logger.info(
130
+ {
131
+ codeExecutionMode,
132
+ durationMs: Date.now() - startTime,
133
+ isError: result.isError,
134
+ contentRows: result.content?.length ?? 0,
50
135
  },
51
- });
136
+ 'Got code tool execution result',
137
+ );
138
+ return result;
139
+ };
140
+
141
+ return { metadata, tool, handler };
142
+ }
143
+
144
+ const remoteStainlessHandler = async ({
145
+ reqContext,
146
+ args,
147
+ }: {
148
+ reqContext: McpRequestContext;
149
+ args: any;
150
+ }): Promise<ToolCallResult> => {
151
+ const code = args.code as string;
152
+ const intent = args.intent as string | undefined;
153
+ const client = reqContext.client;
154
+
155
+ const codeModeEndpoint = readEnv('CODE_MODE_ENDPOINT_URL') ?? 'https://api.stainless.com/api/ai/code-tool';
156
+
157
+ // Setting a Stainless API key authenticates requests to the code tool endpoint.
158
+ const res = await fetch(codeModeEndpoint, {
159
+ method: 'POST',
160
+ headers: {
161
+ ...(reqContext.stainlessApiKey && { Authorization: reqContext.stainlessApiKey }),
162
+ 'Content-Type': 'application/json',
163
+ 'x-stainless-mcp-client-envs': JSON.stringify({
164
+ BEEPER_ACCESS_TOKEN: requireValue(
165
+ readEnv('BEEPER_ACCESS_TOKEN') ?? client.accessToken,
166
+ 'set BEEPER_ACCESS_TOKEN environment variable or provide accessToken client option',
167
+ ),
168
+ BEEPER_DESKTOP_BASE_URL: readEnv('BEEPER_DESKTOP_BASE_URL') ?? client.baseURL ?? undefined,
169
+ }),
170
+ },
171
+ body: JSON.stringify({
172
+ project_name: 'beeper-desktop-api',
173
+ code,
174
+ intent,
175
+ client_opts: {},
176
+ } satisfies WorkerInput),
177
+ });
178
+
179
+ if (!res.ok) {
180
+ if (res.status === 404 && !reqContext.stainlessApiKey) {
181
+ throw new Error(
182
+ 'Could not access code tool for this project. You may need to provide a Stainless API key via the STAINLESS_API_KEY environment variable, the --stainless-api-key flag, or the x-stainless-api-key HTTP header.',
183
+ );
184
+ }
185
+ throw new Error(
186
+ `${res.status}: ${
187
+ res.statusText
188
+ } error when trying to contact Code Tool server. Details: ${await res.text()}`,
189
+ );
190
+ }
191
+
192
+ const { is_error, result, log_lines, err_lines } = (await res.json()) as WorkerOutput;
193
+ const hasLogs = log_lines.length > 0 || err_lines.length > 0;
194
+ const output = {
195
+ result,
196
+ ...(log_lines.length > 0 && { log_lines }),
197
+ ...(err_lines.length > 0 && { err_lines }),
198
+ };
199
+ if (is_error) {
200
+ return asErrorResult(typeof result === 'string' && !hasLogs ? result : JSON.stringify(output, null, 2));
201
+ }
202
+ return asTextContentResult(output);
203
+ };
204
+
205
+ const localDenoHandler = async ({
206
+ reqContext,
207
+ args,
208
+ }: {
209
+ reqContext: McpRequestContext;
210
+ args: unknown;
211
+ }): Promise<ToolCallResult> => {
212
+ const client = reqContext.client;
213
+ const baseURLHostname = new URL(client.baseURL).hostname;
214
+ const { code } = args as { code: string };
215
+
216
+ let denoPath: string;
217
+
218
+ const packageRoot = path.resolve(path.dirname(workerPath), '..');
219
+ const packageNodeModulesPath = path.resolve(packageRoot, 'node_modules');
52
220
 
221
+ // Check if deno is in PATH
222
+ const { execSync } = await import('node:child_process');
223
+ try {
224
+ execSync('command -v deno', { stdio: 'ignore' });
225
+ denoPath = 'deno';
226
+ } catch {
53
227
  try {
54
- const resp = await new Promise<Response>((resolve, reject) => {
55
- worker.addEventListener('exit', (exitCode) => {
56
- reject(new Error(`Worker exited with code ${exitCode}`));
57
- });
58
-
59
- const opts: ClientOptions = {
60
- baseURL: client.baseURL,
61
- accessToken: client.accessToken,
62
- defaultHeaders: {
63
- 'X-Stainless-MCP': 'true',
64
- },
65
- };
228
+ // Use deno binary in node_modules if it's found
229
+ const denoNodeModulesPath = path.resolve(packageNodeModulesPath, 'deno', 'bin.cjs');
230
+ await fs.promises.access(denoNodeModulesPath, fs.constants.X_OK);
231
+ denoPath = denoNodeModulesPath;
232
+ } catch {
233
+ return asErrorResult(
234
+ 'Deno is required for code execution but was not found. ' +
235
+ 'Install it from https://deno.land or run: npm install deno',
236
+ );
237
+ }
238
+ }
66
239
 
67
- const req = worker.request(
68
- 'http://localhost',
69
- {
70
- headers: {
71
- 'content-type': 'application/json',
72
- },
73
- method: 'POST',
74
- },
75
- (resp) => {
76
- const body: Uint8Array[] = [];
77
- resp.on('error', (err) => {
78
- reject(err);
79
- });
80
- resp.on('data', (chunk) => {
81
- body.push(chunk);
82
- });
83
- resp.on('end', () => {
84
- resolve(
85
- new Response(Buffer.concat(body).toString(), {
86
- status: resp.statusCode ?? 200,
87
- headers: resp.headers as any,
88
- }),
89
- );
90
- });
91
- },
92
- );
240
+ const allowReadPaths = [
241
+ 'code-tool-worker.mjs',
242
+ `${workerPath.replace(/([\/\\]node_modules)[\/\\].+$/, '$1')}/`,
243
+ packageRoot,
244
+ ];
245
+
246
+ // Follow symlinks in node_modules to allow read access to workspace-linked packages
247
+ try {
248
+ const sdkPkgName = '@beeper/desktop-api';
249
+ const sdkDir = path.resolve(packageNodeModulesPath, sdkPkgName);
250
+ const realSdkDir = fs.realpathSync(sdkDir);
251
+ if (realSdkDir !== sdkDir) {
252
+ allowReadPaths.push(realSdkDir);
253
+ }
254
+ } catch {
255
+ // Ignore if symlink resolution fails
256
+ }
93
257
 
94
- const body = JSON.stringify({
95
- opts,
96
- code,
97
- } satisfies WorkerInput);
258
+ const allowRead = allowReadPaths.join(',');
98
259
 
99
- req.write(body, (err) => {
100
- if (err != null) {
260
+ const worker = await newDenoHTTPWorker(url.pathToFileURL(workerPath), {
261
+ denoExecutable: denoPath,
262
+ runFlags: [
263
+ `--node-modules-dir=manual`,
264
+ `--allow-read=${allowRead}`,
265
+ `--allow-net=${baseURLHostname}`,
266
+ // Allow environment variables because instantiating the client will try to read from them,
267
+ // even though they are not set.
268
+ '--allow-env',
269
+ ],
270
+ printOutput: true,
271
+ spawnOptions: {
272
+ cwd: path.dirname(workerPath),
273
+ },
274
+ });
275
+
276
+ try {
277
+ const resp = await new Promise<Response>((resolve, reject) => {
278
+ worker.addEventListener('exit', (exitCode) => {
279
+ reject(new Error(`Worker exited with code ${exitCode}`));
280
+ });
281
+
282
+ const opts: ClientOptions = {
283
+ baseURL: client.baseURL,
284
+ accessToken: client.accessToken,
285
+ defaultHeaders: {
286
+ 'X-Stainless-MCP': 'true',
287
+ },
288
+ };
289
+
290
+ const req = worker.request(
291
+ 'http://localhost',
292
+ {
293
+ headers: {
294
+ 'content-type': 'application/json',
295
+ },
296
+ method: 'POST',
297
+ },
298
+ (resp) => {
299
+ const body: Uint8Array[] = [];
300
+ resp.on('error', (err) => {
101
301
  reject(err);
102
- }
103
- });
302
+ });
303
+ resp.on('data', (chunk) => {
304
+ body.push(chunk);
305
+ });
306
+ resp.on('end', () => {
307
+ resolve(
308
+ new Response(Buffer.concat(body).toString(), {
309
+ status: resp.statusCode ?? 200,
310
+ headers: resp.headers as any,
311
+ }),
312
+ );
313
+ });
314
+ },
315
+ );
104
316
 
105
- req.end();
317
+ const body = JSON.stringify({
318
+ opts,
319
+ code,
106
320
  });
107
321
 
108
- if (resp.status === 200) {
109
- const { result, logLines, errLines } = (await resp.json()) as WorkerSuccess;
110
- const returnOutput: ContentBlock | null =
111
- result == null ? null : (
112
- {
113
- type: 'text',
114
- text: typeof result === 'string' ? result : JSON.stringify(result),
115
- }
116
- );
117
- const logOutput: ContentBlock | null =
118
- logLines.length === 0 ?
119
- null
120
- : {
121
- type: 'text',
122
- text: logLines.join('\n'),
123
- };
124
- const errOutput: ContentBlock | null =
125
- errLines.length === 0 ?
126
- null
127
- : {
128
- type: 'text',
129
- text: 'Error output:\n' + errLines.join('\n'),
130
- };
131
- return {
132
- content: [returnOutput, logOutput, errOutput].filter((block) => block !== null),
133
- };
134
- } else {
135
- const { message } = (await resp.json()) as WorkerError;
136
- return {
137
- content: message == null ? [] : [{ type: 'text', text: message }],
138
- isError: true,
139
- };
140
- }
141
- } finally {
142
- worker.terminate();
143
- }
144
- };
322
+ req.write(body, (err) => {
323
+ if (err != null) {
324
+ reject(err);
325
+ }
326
+ });
145
327
 
146
- return { metadata, tool, handler };
147
- }
328
+ req.end();
329
+ });
330
+
331
+ if (resp.status === 200) {
332
+ const { result, log_lines, err_lines } = (await resp.json()) as WorkerOutput;
333
+ const returnOutput: ContentBlock | null =
334
+ result == null ? null : (
335
+ {
336
+ type: 'text',
337
+ text: typeof result === 'string' ? result : JSON.stringify(result),
338
+ }
339
+ );
340
+ const logOutput: ContentBlock | null =
341
+ log_lines.length === 0 ?
342
+ null
343
+ : {
344
+ type: 'text',
345
+ text: log_lines.join('\n'),
346
+ };
347
+ const errOutput: ContentBlock | null =
348
+ err_lines.length === 0 ?
349
+ null
350
+ : {
351
+ type: 'text',
352
+ text: 'Error output:\n' + err_lines.join('\n'),
353
+ };
354
+ return {
355
+ content: [returnOutput, logOutput, errOutput].filter((block) => block !== null),
356
+ };
357
+ } else {
358
+ const { result, log_lines, err_lines } = (await resp.json()) as WorkerOutput;
359
+ const messageOutput: ContentBlock | null =
360
+ result == null ? null : (
361
+ {
362
+ type: 'text',
363
+ text: typeof result === 'string' ? result : JSON.stringify(result),
364
+ }
365
+ );
366
+ const logOutput: ContentBlock | null =
367
+ log_lines.length === 0 ?
368
+ null
369
+ : {
370
+ type: 'text',
371
+ text: log_lines.join('\n'),
372
+ };
373
+ const errOutput: ContentBlock | null =
374
+ err_lines.length === 0 ?
375
+ null
376
+ : {
377
+ type: 'text',
378
+ text: 'Error output:\n' + err_lines.join('\n'),
379
+ };
380
+ return {
381
+ content: [messageOutput, logOutput, errOutput].filter((block) => block !== null),
382
+ isError: true,
383
+ };
384
+ }
385
+ } finally {
386
+ worker.terminate();
387
+ }
388
+ };