@beeper/desktop-mcp 4.2.3 → 4.7.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/docs-search-tool.d.mts +22 -9
  30. package/docs-search-tool.d.mts.map +1 -1
  31. package/docs-search-tool.d.ts +22 -9
  32. package/docs-search-tool.d.ts.map +1 -1
  33. package/docs-search-tool.js +37 -6
  34. package/docs-search-tool.js.map +1 -1
  35. package/docs-search-tool.mjs +37 -6
  36. package/docs-search-tool.mjs.map +1 -1
  37. package/http.d.mts +6 -9
  38. package/http.d.mts.map +1 -1
  39. package/http.d.ts +6 -9
  40. package/http.d.ts.map +1 -1
  41. package/http.js +81 -47
  42. package/http.js.map +1 -1
  43. package/http.mjs +80 -43
  44. package/http.mjs.map +1 -1
  45. package/index.js +19 -50
  46. package/index.js.map +1 -1
  47. package/index.mjs +19 -50
  48. package/index.mjs.map +1 -1
  49. package/instructions.d.mts +2 -0
  50. package/instructions.d.mts.map +1 -0
  51. package/instructions.d.ts +2 -0
  52. package/instructions.d.ts.map +1 -0
  53. package/instructions.js +48 -0
  54. package/instructions.js.map +1 -0
  55. package/instructions.mjs +45 -0
  56. package/instructions.mjs.map +1 -0
  57. package/logger.d.mts +7 -0
  58. package/logger.d.mts.map +1 -0
  59. package/logger.d.ts +7 -0
  60. package/logger.d.ts.map +1 -0
  61. package/logger.js +29 -0
  62. package/logger.js.map +1 -0
  63. package/logger.mjs +22 -0
  64. package/logger.mjs.map +1 -0
  65. package/methods.d.mts +10 -0
  66. package/methods.d.mts.map +1 -0
  67. package/methods.d.ts +10 -0
  68. package/methods.d.ts.map +1 -0
  69. package/methods.js +203 -0
  70. package/methods.js.map +1 -0
  71. package/methods.mjs +199 -0
  72. package/methods.mjs.map +1 -0
  73. package/options.d.mts +9 -9
  74. package/options.d.mts.map +1 -1
  75. package/options.d.ts +9 -9
  76. package/options.d.ts.map +1 -1
  77. package/options.js +54 -359
  78. package/options.js.map +1 -1
  79. package/options.mjs +54 -359
  80. package/options.mjs.map +1 -1
  81. package/package.json +68 -72
  82. package/server.d.mts +10 -16
  83. package/server.d.mts.map +1 -1
  84. package/server.d.ts +10 -16
  85. package/server.d.ts.map +1 -1
  86. package/server.js +82 -101
  87. package/server.js.map +1 -1
  88. package/server.mjs +81 -96
  89. package/server.mjs.map +1 -1
  90. package/src/auth.ts +42 -0
  91. package/src/code-tool-types.ts +8 -5
  92. package/src/code-tool-worker.ts +118 -34
  93. package/src/code-tool.ts +359 -118
  94. package/src/docs-search-tool.ts +59 -7
  95. package/src/http.ts +99 -50
  96. package/src/index.ts +22 -63
  97. package/src/instructions.ts +67 -0
  98. package/src/logger.ts +28 -0
  99. package/src/methods.ts +224 -0
  100. package/src/options.ts +72 -384
  101. package/src/server.ts +94 -117
  102. package/src/stdio.ts +6 -5
  103. package/src/{tools/types.ts → types.ts} +25 -5
  104. package/src/util.ts +25 -0
  105. package/stdio.d.mts +1 -1
  106. package/stdio.d.mts.map +1 -1
  107. package/stdio.d.ts +1 -1
  108. package/stdio.d.ts.map +1 -1
  109. package/stdio.js +5 -4
  110. package/stdio.js.map +1 -1
  111. package/stdio.mjs +5 -4
  112. package/stdio.mjs.map +1 -1
  113. package/{tools/types.d.mts → types.d.mts} +10 -2
  114. package/types.d.mts.map +1 -0
  115. package/{tools/types.d.ts → types.d.ts} +10 -2
  116. package/types.d.ts.map +1 -0
  117. package/{tools/types.js → types.js} +12 -0
  118. package/types.js.map +1 -0
  119. package/{tools/types.mjs → types.mjs} +11 -0
  120. package/types.mjs.map +1 -0
  121. package/util.d.mts +4 -0
  122. package/util.d.mts.map +1 -0
  123. package/util.d.ts +4 -0
  124. package/util.d.ts.map +1 -0
  125. package/util.js +30 -0
  126. package/util.js.map +1 -0
  127. package/util.mjs +24 -0
  128. package/util.mjs.map +1 -0
  129. package/compat.d.mts +0 -58
  130. package/compat.d.mts.map +0 -1
  131. package/compat.d.ts +0 -58
  132. package/compat.d.ts.map +0 -1
  133. package/compat.js +0 -387
  134. package/compat.js.map +0 -1
  135. package/compat.mjs +0 -378
  136. package/compat.mjs.map +0 -1
  137. package/dynamic-tools.d.mts +0 -12
  138. package/dynamic-tools.d.mts.map +0 -1
  139. package/dynamic-tools.d.ts +0 -12
  140. package/dynamic-tools.d.ts.map +0 -1
  141. package/dynamic-tools.js +0 -135
  142. package/dynamic-tools.js.map +0 -1
  143. package/dynamic-tools.mjs +0 -132
  144. package/dynamic-tools.mjs.map +0 -1
  145. package/filtering.d.mts +0 -2
  146. package/filtering.d.mts.map +0 -1
  147. package/filtering.d.ts +0 -2
  148. package/filtering.d.ts.map +0 -1
  149. package/filtering.js +0 -20
  150. package/filtering.js.map +0 -1
  151. package/filtering.mjs +0 -13
  152. package/filtering.mjs.map +0 -1
  153. package/handlers/get-accounts.d.mts +0 -3
  154. package/handlers/get-accounts.d.mts.map +0 -1
  155. package/handlers/get-accounts.d.ts +0 -3
  156. package/handlers/get-accounts.d.ts.map +0 -1
  157. package/handlers/get-accounts.js +0 -32
  158. package/handlers/get-accounts.js.map +0 -1
  159. package/handlers/get-accounts.mjs +0 -28
  160. package/handlers/get-accounts.mjs.map +0 -1
  161. package/handlers/get-chat.d.mts +0 -3
  162. package/handlers/get-chat.d.mts.map +0 -1
  163. package/handlers/get-chat.d.ts +0 -3
  164. package/handlers/get-chat.d.ts.map +0 -1
  165. package/handlers/get-chat.js +0 -20
  166. package/handlers/get-chat.js.map +0 -1
  167. package/handlers/get-chat.mjs +0 -16
  168. package/handlers/get-chat.mjs.map +0 -1
  169. package/handlers/index.d.mts +0 -3
  170. package/handlers/index.d.mts.map +0 -1
  171. package/handlers/index.d.ts +0 -3
  172. package/handlers/index.d.ts.map +0 -1
  173. package/handlers/index.js +0 -30
  174. package/handlers/index.js.map +0 -1
  175. package/handlers/index.mjs +0 -27
  176. package/handlers/index.mjs.map +0 -1
  177. package/handlers/list-chats.d.mts +0 -3
  178. package/handlers/list-chats.d.mts.map +0 -1
  179. package/handlers/list-chats.d.ts +0 -3
  180. package/handlers/list-chats.d.ts.map +0 -1
  181. package/handlers/list-chats.js +0 -46
  182. package/handlers/list-chats.js.map +0 -1
  183. package/handlers/list-chats.mjs +0 -42
  184. package/handlers/list-chats.mjs.map +0 -1
  185. package/handlers/list-messages.d.mts +0 -3
  186. package/handlers/list-messages.d.mts.map +0 -1
  187. package/handlers/list-messages.d.ts +0 -3
  188. package/handlers/list-messages.d.ts.map +0 -1
  189. package/handlers/list-messages.js +0 -34
  190. package/handlers/list-messages.js.map +0 -1
  191. package/handlers/list-messages.mjs +0 -30
  192. package/handlers/list-messages.mjs.map +0 -1
  193. package/handlers/open-app.d.mts +0 -3
  194. package/handlers/open-app.d.mts.map +0 -1
  195. package/handlers/open-app.d.ts +0 -3
  196. package/handlers/open-app.d.ts.map +0 -1
  197. package/handlers/open-app.js +0 -26
  198. package/handlers/open-app.js.map +0 -1
  199. package/handlers/open-app.mjs +0 -22
  200. package/handlers/open-app.mjs.map +0 -1
  201. package/handlers/search-chats.d.mts +0 -3
  202. package/handlers/search-chats.d.mts.map +0 -1
  203. package/handlers/search-chats.d.ts +0 -3
  204. package/handlers/search-chats.d.ts.map +0 -1
  205. package/handlers/search-chats.js +0 -38
  206. package/handlers/search-chats.js.map +0 -1
  207. package/handlers/search-chats.mjs +0 -34
  208. package/handlers/search-chats.mjs.map +0 -1
  209. package/handlers/search-messages.d.mts +0 -3
  210. package/handlers/search-messages.d.mts.map +0 -1
  211. package/handlers/search-messages.d.ts +0 -3
  212. package/handlers/search-messages.d.ts.map +0 -1
  213. package/handlers/search-messages.js +0 -11
  214. package/handlers/search-messages.js.map +0 -1
  215. package/handlers/search-messages.mjs +0 -7
  216. package/handlers/search-messages.mjs.map +0 -1
  217. package/handlers/search.d.mts +0 -3
  218. package/handlers/search.d.mts.map +0 -1
  219. package/handlers/search.d.ts +0 -3
  220. package/handlers/search.d.ts.map +0 -1
  221. package/handlers/search.js +0 -29
  222. package/handlers/search.js.map +0 -1
  223. package/handlers/search.mjs +0 -25
  224. package/handlers/search.mjs.map +0 -1
  225. package/handlers/send-message.d.mts +0 -3
  226. package/handlers/send-message.d.mts.map +0 -1
  227. package/handlers/send-message.d.ts +0 -3
  228. package/handlers/send-message.d.ts.map +0 -1
  229. package/handlers/send-message.js +0 -20
  230. package/handlers/send-message.js.map +0 -1
  231. package/handlers/send-message.mjs +0 -16
  232. package/handlers/send-message.mjs.map +0 -1
  233. package/handlers/utils.d.mts +0 -29
  234. package/handlers/utils.d.mts.map +0 -1
  235. package/handlers/utils.d.ts +0 -29
  236. package/handlers/utils.d.ts.map +0 -1
  237. package/handlers/utils.js +0 -296
  238. package/handlers/utils.js.map +0 -1
  239. package/handlers/utils.mjs +0 -282
  240. package/handlers/utils.mjs.map +0 -1
  241. package/headers.d.mts +0 -4
  242. package/headers.d.mts.map +0 -1
  243. package/headers.d.ts +0 -4
  244. package/headers.d.ts.map +0 -1
  245. package/headers.js +0 -22
  246. package/headers.js.map +0 -1
  247. package/headers.mjs +0 -18
  248. package/headers.mjs.map +0 -1
  249. package/src/compat.ts +0 -483
  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
+ };
@@ -1,8 +1,8 @@
1
1
  // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
2
 
3
- import { Metadata, asTextContentResult } from './tools/types';
4
-
5
3
  import { Tool } from '@modelcontextprotocol/sdk/types.js';
4
+ import { Metadata, McpRequestContext, asTextContentResult } from './types';
5
+ import { getLogger } from './logger';
6
6
 
7
7
  export const metadata: Metadata = {
8
8
  resource: 'all',
@@ -14,7 +14,7 @@ export const metadata: Metadata = {
14
14
  export const tool: Tool = {
15
15
  name: 'search_docs',
16
16
  description:
17
- 'Search for documentation for how to use the client to interact with the API.\nThe tool will return an array of Markdown-formatted documentation pages.',
17
+ 'Search SDK documentation to find methods, parameters, and usage examples for interacting with the API. Use this before writing code when you need to discover the right approach.',
18
18
  inputSchema: {
19
19
  type: 'object',
20
20
  properties: {
@@ -25,7 +25,12 @@ export const tool: Tool = {
25
25
  language: {
26
26
  type: 'string',
27
27
  description: 'The language for the SDK to search for.',
28
- enum: ['http', 'python', 'go', 'typescript', 'terraform', 'ruby', 'java', 'kotlin'],
28
+ enum: ['http', 'python', 'go', 'typescript', 'javascript', 'terraform', 'ruby', 'java', 'kotlin'],
29
+ },
30
+ detail: {
31
+ type: 'string',
32
+ description: 'The amount of detail to return.',
33
+ enum: ['default', 'verbose'],
29
34
  },
30
35
  },
31
36
  required: ['query', 'language'],
@@ -38,11 +43,58 @@ export const tool: Tool = {
38
43
  const docsSearchURL =
39
44
  process.env['DOCS_SEARCH_URL'] || 'https://api.stainless.com/api/projects/beeper-desktop-api/docs/search';
40
45
 
41
- export const handler = async (_: unknown, args: Record<string, unknown> | undefined) => {
46
+ export const handler = async ({
47
+ reqContext,
48
+ args,
49
+ }: {
50
+ reqContext: McpRequestContext;
51
+ args: Record<string, unknown> | undefined;
52
+ }) => {
42
53
  const body = args as any;
43
54
  const query = new URLSearchParams(body).toString();
44
- const result = await fetch(`${docsSearchURL}?${query}`);
45
- return asTextContentResult(await result.json());
55
+
56
+ const startTime = Date.now();
57
+ const result = await fetch(`${docsSearchURL}?${query}`, {
58
+ headers: {
59
+ ...(reqContext.stainlessApiKey && { Authorization: reqContext.stainlessApiKey }),
60
+ },
61
+ });
62
+
63
+ const logger = getLogger();
64
+
65
+ if (!result.ok) {
66
+ const errorText = await result.text();
67
+ logger.warn(
68
+ {
69
+ durationMs: Date.now() - startTime,
70
+ query: body.query,
71
+ status: result.status,
72
+ statusText: result.statusText,
73
+ errorText,
74
+ },
75
+ 'Got error response from docs search tool',
76
+ );
77
+
78
+ if (result.status === 404 && !reqContext.stainlessApiKey) {
79
+ throw new Error(
80
+ 'Could not find docs 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.',
81
+ );
82
+ }
83
+
84
+ throw new Error(
85
+ `${result.status}: ${result.statusText} when using doc search tool. Details: ${errorText}`,
86
+ );
87
+ }
88
+
89
+ const resultBody = await result.json();
90
+ logger.info(
91
+ {
92
+ durationMs: Date.now() - startTime,
93
+ query: body.query,
94
+ },
95
+ 'Got docs search result',
96
+ );
97
+ return asTextContentResult(resultBody);
46
98
  };
47
99
 
48
100
  export default { metadata, tool, handler };