@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/http.ts CHANGED
@@ -2,22 +2,24 @@
2
2
 
3
3
  import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
4
4
  import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
5
-
5
+ import { ClientOptions } from '@beeper/desktop-api';
6
6
  import cors from 'cors';
7
7
  import express from 'express';
8
- import { fromError } from 'zod-validation-error/v3';
9
- import { McpOptions, parseQueryOptions } from './options';
10
- import { ClientOptions, initMcpServer, newMcpServer } from './server';
11
- import { parseAuthHeaders } from './headers';
8
+ import pino from 'pino';
9
+ import pinoHttp from 'pino-http';
10
+ import { getStainlessApiKey, parseClientAuthHeaders } from './auth';
11
+ import { getLogger } from './logger';
12
+ import { McpOptions } from './options';
13
+ import { initMcpServer, newMcpServer } from './server';
12
14
 
13
15
  const oauthResourceIdentifier = (req: express.Request): string => {
14
16
  const protocol = req.headers['x-forwarded-proto'] ?? req.protocol;
15
17
  return `${protocol}://${req.get('host')}/`;
16
18
  };
17
19
 
18
- const newServer = ({
20
+ const newServer = async ({
19
21
  clientOptions,
20
- mcpOptions: defaultMcpOptions,
22
+ mcpOptions,
21
23
  req,
22
24
  res,
23
25
  }: {
@@ -25,34 +27,18 @@ const newServer = ({
25
27
  mcpOptions: McpOptions;
26
28
  req: express.Request;
27
29
  res: express.Response;
28
- }): McpServer | null => {
29
- const server = newMcpServer();
30
-
31
- let mcpOptions: McpOptions;
30
+ }): Promise<McpServer | null> => {
31
+ const stainlessApiKey = getStainlessApiKey(req, mcpOptions);
32
+ const server = await newMcpServer(stainlessApiKey);
33
+
34
+ // parseClientAuthHeaders throws if the Authorization header uses an unsupported
35
+ // scheme, or (when the second arg is true) if the header is missing entirely.
36
+ // On error, we return 401 with WWW-Authenticate pointing to the OAuth metadata
37
+ // endpoint so clients know how to authenticate (RFC 9728).
38
+ let authOptions: Partial<ClientOptions>;
32
39
  try {
33
- mcpOptions = parseQueryOptions(defaultMcpOptions, req.query);
40
+ authOptions = parseClientAuthHeaders(req, false);
34
41
  } catch (error) {
35
- res.status(400).json({
36
- jsonrpc: '2.0',
37
- error: {
38
- code: -32000,
39
- message: `Invalid request: ${fromError(error)}`,
40
- },
41
- });
42
- return null;
43
- }
44
-
45
- try {
46
- const authOptions = parseAuthHeaders(req);
47
- initMcpServer({
48
- server: server,
49
- clientOptions: {
50
- ...clientOptions,
51
- ...authOptions,
52
- },
53
- mcpOptions,
54
- });
55
- } catch {
56
42
  const resourceIdentifier = oauthResourceIdentifier(req);
57
43
  res.set(
58
44
  'WWW-Authenticate',
@@ -62,30 +48,37 @@ const newServer = ({
62
48
  jsonrpc: '2.0',
63
49
  error: {
64
50
  code: -32000,
65
- message: 'Unauthorized',
51
+ message: `Unauthorized: ${error instanceof Error ? error.message : error}`,
66
52
  },
67
53
  });
68
54
  return null;
69
55
  }
70
56
 
57
+ await initMcpServer({
58
+ server: server,
59
+ mcpOptions: mcpOptions,
60
+ clientOptions: {
61
+ ...clientOptions,
62
+ ...authOptions,
63
+ },
64
+ stainlessApiKey: stainlessApiKey,
65
+ });
66
+
71
67
  return server;
72
68
  };
73
69
 
74
- export const post =
70
+ const post =
75
71
  (options: { clientOptions: ClientOptions; mcpOptions: McpOptions }) =>
76
72
  async (req: express.Request, res: express.Response) => {
77
- const server = newServer({ ...options, req, res });
73
+ const server = await newServer({ ...options, req, res });
78
74
  // If we return null, we already set the authorization error.
79
75
  if (server === null) return;
80
- const transport = new StreamableHTTPServerTransport({
81
- // Stateless server
82
- sessionIdGenerator: undefined,
83
- });
84
- await server.connect(transport);
76
+ const transport = new StreamableHTTPServerTransport();
77
+ await server.connect(transport as any);
85
78
  await transport.handleRequest(req, res, req.body);
86
79
  };
87
80
 
88
- export const get = async (req: express.Request, res: express.Response) => {
81
+ const get = async (req: express.Request, res: express.Response) => {
89
82
  res.status(405).json({
90
83
  jsonrpc: '2.0',
91
84
  error: {
@@ -95,7 +88,7 @@ export const get = async (req: express.Request, res: express.Response) => {
95
88
  });
96
89
  };
97
90
 
98
- export const del = async (req: express.Request, res: express.Response) => {
91
+ const del = async (req: express.Request, res: express.Response) => {
99
92
  res.status(405).json({
100
93
  jsonrpc: '2.0',
101
94
  error: {
@@ -115,18 +108,66 @@ const oauthMetadata = (req: express.Request, res: express.Response) => {
115
108
  });
116
109
  };
117
110
 
111
+ const redactHeaders = (headers: Record<string, any>) => {
112
+ const hiddenHeaders = /auth|cookie|key|token/i;
113
+ const filtered = { ...headers };
114
+ Object.keys(filtered).forEach((key) => {
115
+ if (hiddenHeaders.test(key)) {
116
+ filtered[key] = '[REDACTED]';
117
+ }
118
+ });
119
+ return filtered;
120
+ };
121
+
118
122
  export const streamableHTTPApp = ({
119
123
  clientOptions = {},
120
- mcpOptions = {},
124
+ mcpOptions,
121
125
  }: {
122
126
  clientOptions?: ClientOptions;
123
- mcpOptions?: McpOptions;
127
+ mcpOptions: McpOptions;
124
128
  }): express.Express => {
125
129
  const app = express();
126
130
  app.set('query parser', 'extended');
127
131
  app.use(express.json());
132
+ app.use(
133
+ pinoHttp({
134
+ logger: getLogger(),
135
+ customLogLevel: (req, res) => {
136
+ if (res.statusCode >= 500) {
137
+ return 'error';
138
+ } else if (res.statusCode >= 400) {
139
+ return 'warn';
140
+ }
141
+ return 'info';
142
+ },
143
+ customSuccessMessage: function (req, res) {
144
+ return `Request ${req.method} to ${req.url} completed with status ${res.statusCode}`;
145
+ },
146
+ customErrorMessage: function (req, res, err) {
147
+ return `Request ${req.method} to ${req.url} errored with status ${res.statusCode}`;
148
+ },
149
+ serializers: {
150
+ req: pino.stdSerializers.wrapRequestSerializer((req) => {
151
+ return {
152
+ ...req,
153
+ headers: redactHeaders(req.raw.headers),
154
+ };
155
+ }),
156
+ res: pino.stdSerializers.wrapResponseSerializer((res) => {
157
+ return {
158
+ ...res,
159
+ headers: redactHeaders(res.headers),
160
+ };
161
+ }),
162
+ },
163
+ }),
164
+ );
128
165
 
129
166
  app.get('/.well-known/oauth-protected-resource', cors(), oauthMetadata);
167
+
168
+ app.get('/health', async (req: express.Request, res: express.Response) => {
169
+ res.status(200).send('OK');
170
+ });
130
171
  app.get('/', get);
131
172
  app.post('/', post({ clientOptions, mcpOptions }));
132
173
  app.delete('/', del);
@@ -134,16 +175,24 @@ export const streamableHTTPApp = ({
134
175
  return app;
135
176
  };
136
177
 
137
- export const launchStreamableHTTPServer = async (options: McpOptions, port: number | string | undefined) => {
138
- const app = streamableHTTPApp({ mcpOptions: options });
178
+ export const launchStreamableHTTPServer = async ({
179
+ mcpOptions,
180
+ port,
181
+ }: {
182
+ mcpOptions: McpOptions;
183
+ port: number | string | undefined;
184
+ }) => {
185
+ const app = streamableHTTPApp({ mcpOptions });
139
186
  const server = app.listen(port);
140
187
  const address = server.address();
141
188
 
189
+ const logger = getLogger();
190
+
142
191
  if (typeof address === 'string') {
143
- console.error(`MCP Server running on streamable HTTP at ${address}`);
192
+ logger.info(`MCP Server running on streamable HTTP at ${address}`);
144
193
  } else if (address !== null) {
145
- console.error(`MCP Server running on streamable HTTP on port ${address.port}`);
194
+ logger.info(`MCP Server running on streamable HTTP on port ${address.port}`);
146
195
  } else {
147
- console.error(`MCP Server running on streamable HTTP on port ${port}`);
196
+ logger.info(`MCP Server running on streamable HTTP on port ${port}`);
148
197
  }
149
198
  };
package/src/index.ts CHANGED
@@ -1,24 +1,24 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { selectTools } from './server';
4
- import { Endpoint, endpoints } from './tools';
5
4
  import { McpOptions, parseCLIOptions } from './options';
6
5
  import { launchStdioServer } from './stdio';
7
6
  import { launchStreamableHTTPServer } from './http';
7
+ import type { McpTool } from './types';
8
+ import { configureLogger, getLogger } from './logger';
8
9
 
9
10
  async function main() {
10
11
  const options = parseOptionsOrError();
12
+ configureLogger({
13
+ level: options.debug ? 'debug' : 'info',
14
+ pretty: options.logFormat === 'pretty',
15
+ });
11
16
 
12
- if (options.list) {
13
- listAllTools();
14
- return;
15
- }
16
-
17
- const selectedTools = await selectToolsOrError(endpoints, options);
17
+ const selectedTools = await selectToolsOrError(options);
18
18
 
19
- console.error(
20
- `MCP Server starting with ${selectedTools.length} tools:`,
21
- selectedTools.map((e) => e.tool.name),
19
+ getLogger().info(
20
+ { tools: selectedTools.map((e) => e.tool.name) },
21
+ `MCP Server starting with ${selectedTools.length} tools`,
22
22
  );
23
23
 
24
24
  switch (options.transport) {
@@ -26,14 +26,18 @@ async function main() {
26
26
  await launchStdioServer(options);
27
27
  break;
28
28
  case 'http':
29
- await launchStreamableHTTPServer(options, options.port ?? options.socket);
29
+ await launchStreamableHTTPServer({
30
+ mcpOptions: options,
31
+ port: options.socket ?? options.port,
32
+ });
30
33
  break;
31
34
  }
32
35
  }
33
36
 
34
37
  if (require.main === module) {
35
38
  main().catch((error) => {
36
- console.error('Fatal error in main():', error);
39
+ // Logger might not be initialized yet
40
+ console.error('Fatal error in main()', error);
37
41
  process.exit(1);
38
42
  });
39
43
  }
@@ -42,67 +46,22 @@ function parseOptionsOrError() {
42
46
  try {
43
47
  return parseCLIOptions();
44
48
  } catch (error) {
45
- console.error('Error parsing options:', error);
49
+ // Logger is initialized after options, so use console.error here
50
+ console.error('Error parsing options', error);
46
51
  process.exit(1);
47
52
  }
48
53
  }
49
54
 
50
- async function selectToolsOrError(endpoints: Endpoint[], options: McpOptions): Promise<Endpoint[]> {
55
+ async function selectToolsOrError(options: McpOptions): Promise<McpTool[]> {
51
56
  try {
52
- const includedTools = await selectTools(endpoints, options);
57
+ const includedTools = selectTools(options);
53
58
  if (includedTools.length === 0) {
54
- console.error('No tools match the provided filters.');
59
+ getLogger().error('No tools match the provided filters');
55
60
  process.exit(1);
56
61
  }
57
62
  return includedTools;
58
63
  } catch (error) {
59
- if (error instanceof Error) {
60
- console.error('Error filtering tools:', error.message);
61
- } else {
62
- console.error('Error filtering tools:', error);
63
- }
64
+ getLogger().error({ error }, 'Error filtering tools');
64
65
  process.exit(1);
65
66
  }
66
67
  }
67
-
68
- function listAllTools() {
69
- if (endpoints.length === 0) {
70
- console.log('No tools available.');
71
- return;
72
- }
73
- console.log('Available tools:\n');
74
-
75
- // Group endpoints by resource
76
- const resourceGroups = new Map<string, typeof endpoints>();
77
-
78
- for (const endpoint of endpoints) {
79
- const resource = endpoint.metadata.resource;
80
- if (!resourceGroups.has(resource)) {
81
- resourceGroups.set(resource, []);
82
- }
83
- resourceGroups.get(resource)!.push(endpoint);
84
- }
85
-
86
- // Sort resources alphabetically
87
- const sortedResources = Array.from(resourceGroups.keys()).sort();
88
-
89
- // Display hierarchically by resource
90
- for (const resource of sortedResources) {
91
- console.log(`Resource: ${resource}`);
92
-
93
- const resourceEndpoints = resourceGroups.get(resource)!;
94
- // Sort endpoints by tool name
95
- resourceEndpoints.sort((a, b) => a.tool.name.localeCompare(b.tool.name));
96
-
97
- for (const endpoint of resourceEndpoints) {
98
- const {
99
- tool,
100
- metadata: { operation, tags },
101
- } = endpoint;
102
-
103
- console.log(` - ${tool.name} (${operation}) ${tags.length > 0 ? `tags: ${tags.join(', ')}` : ''}`);
104
- console.log(` Description: ${tool.description}`);
105
- }
106
- console.log('');
107
- }
108
- }
@@ -0,0 +1,67 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { readEnv } from './util';
4
+ import { getLogger } from './logger';
5
+
6
+ const INSTRUCTIONS_CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes
7
+
8
+ interface InstructionsCacheEntry {
9
+ fetchedInstructions: string;
10
+ fetchedAt: number;
11
+ }
12
+
13
+ const instructionsCache = new Map<string, InstructionsCacheEntry>();
14
+
15
+ // Periodically evict stale entries so the cache doesn't grow unboundedly.
16
+ const _cacheCleanupInterval = setInterval(() => {
17
+ const now = Date.now();
18
+ for (const [key, entry] of instructionsCache) {
19
+ if (now - entry.fetchedAt > INSTRUCTIONS_CACHE_TTL_MS) {
20
+ instructionsCache.delete(key);
21
+ }
22
+ }
23
+ }, INSTRUCTIONS_CACHE_TTL_MS);
24
+
25
+ // Don't keep the process alive just for cleanup.
26
+ _cacheCleanupInterval.unref();
27
+
28
+ export async function getInstructions(stainlessApiKey: string | undefined): Promise<string> {
29
+ const cacheKey = stainlessApiKey ?? '';
30
+ const cached = instructionsCache.get(cacheKey);
31
+
32
+ if (cached && Date.now() - cached.fetchedAt <= INSTRUCTIONS_CACHE_TTL_MS) {
33
+ return cached.fetchedInstructions;
34
+ }
35
+
36
+ const fetchedInstructions = await fetchLatestInstructions(stainlessApiKey);
37
+ instructionsCache.set(cacheKey, { fetchedInstructions, fetchedAt: Date.now() });
38
+ return fetchedInstructions;
39
+ }
40
+
41
+ async function fetchLatestInstructions(stainlessApiKey: string | undefined): Promise<string> {
42
+ // Setting the stainless API key is optional, but may be required
43
+ // to authenticate requests to the Stainless API.
44
+ const response = await fetch(
45
+ readEnv('CODE_MODE_INSTRUCTIONS_URL') ??
46
+ 'https://api.stainless.com/api/ai/instructions/beeper-desktop-api',
47
+ {
48
+ method: 'GET',
49
+ headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) },
50
+ },
51
+ );
52
+
53
+ let instructions: string | undefined;
54
+ if (!response.ok) {
55
+ getLogger().warn(
56
+ 'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
57
+ );
58
+
59
+ instructions =
60
+ '\n This is the beeper-desktop-api MCP server.\n\n Available tools:\n - search_docs: Search SDK documentation to find the right methods and parameters.\n - execute: Run TypeScript code against a pre-authenticated SDK client. Define an async run(client) function.\n\n Workflow:\n - If unsure about the API, call search_docs first.\n - Write complete solutions in a single execute call when possible. For large datasets, use API filters to narrow results or paginate within a single execute block.\n - If execute returns an error, read the error and fix your code rather than retrying the same approach.\n - Variables do not persist between execute calls. Return or log all data you need.\n - Individual HTTP requests to the API have a 30-second timeout. If a request times out, try a smaller query or add filters.\n - Code execution has a total timeout of approximately 5 minutes. If your code times out, simplify it or break it into smaller steps.\n ';
61
+ }
62
+
63
+ instructions ??= ((await response.json()) as { instructions: string }).instructions;
64
+
65
+ instructions += `\nAccess to all chats and messages across networks using Beeper Desktop. Can be used to find, get, send, and manage messages and chats.`;
66
+ return instructions;
67
+ }
package/src/logger.ts ADDED
@@ -0,0 +1,28 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { pino, type Level, type Logger } from 'pino';
4
+ import pretty from 'pino-pretty';
5
+
6
+ let _logger: Logger | undefined;
7
+
8
+ export function configureLogger({ level, pretty: usePretty }: { level: Level; pretty: boolean }): void {
9
+ _logger = pino(
10
+ {
11
+ level,
12
+ timestamp: pino.stdTimeFunctions.isoTime,
13
+ formatters: {
14
+ level(label) {
15
+ return { level: label };
16
+ },
17
+ },
18
+ },
19
+ usePretty ? pretty({ colorize: true, levelFirst: true, destination: 2 }) : process.stderr,
20
+ );
21
+ }
22
+
23
+ export function getLogger(): Logger {
24
+ if (!_logger) {
25
+ throw new Error('Logger has not been configured. Call configureLogger() before using the logger.');
26
+ }
27
+ return _logger;
28
+ }
package/src/methods.ts ADDED
@@ -0,0 +1,224 @@
1
+ // File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2
+
3
+ import { McpOptions } from './options';
4
+
5
+ export type SdkMethod = {
6
+ clientCallName: string;
7
+ fullyQualifiedName: string;
8
+ httpMethod?: 'get' | 'post' | 'put' | 'patch' | 'delete' | 'query';
9
+ httpPath?: string;
10
+ };
11
+
12
+ export const sdkMethods: SdkMethod[] = [
13
+ {
14
+ clientCallName: 'client.focus',
15
+ fullyQualifiedName: 'focus',
16
+ httpMethod: 'post',
17
+ httpPath: '/v1/focus',
18
+ },
19
+ {
20
+ clientCallName: 'client.search',
21
+ fullyQualifiedName: 'search',
22
+ httpMethod: 'get',
23
+ httpPath: '/v1/search',
24
+ },
25
+ {
26
+ clientCallName: 'client.accounts.list',
27
+ fullyQualifiedName: 'accounts.list',
28
+ httpMethod: 'get',
29
+ httpPath: '/v1/accounts',
30
+ },
31
+ {
32
+ clientCallName: 'client.accounts.contacts.list',
33
+ fullyQualifiedName: 'accounts.contacts.list',
34
+ httpMethod: 'get',
35
+ httpPath: '/v1/accounts/{accountID}/contacts/list',
36
+ },
37
+ {
38
+ clientCallName: 'client.accounts.contacts.search',
39
+ fullyQualifiedName: 'accounts.contacts.search',
40
+ httpMethod: 'get',
41
+ httpPath: '/v1/accounts/{accountID}/contacts',
42
+ },
43
+ {
44
+ clientCallName: 'client.chats.create',
45
+ fullyQualifiedName: 'chats.create',
46
+ httpMethod: 'post',
47
+ httpPath: '/v1/chats',
48
+ },
49
+ {
50
+ clientCallName: 'client.chats.retrieve',
51
+ fullyQualifiedName: 'chats.retrieve',
52
+ httpMethod: 'get',
53
+ httpPath: '/v1/chats/{chatID}',
54
+ },
55
+ {
56
+ clientCallName: 'client.chats.list',
57
+ fullyQualifiedName: 'chats.list',
58
+ httpMethod: 'get',
59
+ httpPath: '/v1/chats',
60
+ },
61
+ {
62
+ clientCallName: 'client.chats.archive',
63
+ fullyQualifiedName: 'chats.archive',
64
+ httpMethod: 'post',
65
+ httpPath: '/v1/chats/{chatID}/archive',
66
+ },
67
+ {
68
+ clientCallName: 'client.chats.search',
69
+ fullyQualifiedName: 'chats.search',
70
+ httpMethod: 'get',
71
+ httpPath: '/v1/chats/search',
72
+ },
73
+ {
74
+ clientCallName: 'client.chats.reminders.create',
75
+ fullyQualifiedName: 'chats.reminders.create',
76
+ httpMethod: 'post',
77
+ httpPath: '/v1/chats/{chatID}/reminders',
78
+ },
79
+ {
80
+ clientCallName: 'client.chats.reminders.delete',
81
+ fullyQualifiedName: 'chats.reminders.delete',
82
+ httpMethod: 'delete',
83
+ httpPath: '/v1/chats/{chatID}/reminders',
84
+ },
85
+ {
86
+ clientCallName: 'client.chats.messages.reactions.delete',
87
+ fullyQualifiedName: 'chats.messages.reactions.delete',
88
+ httpMethod: 'delete',
89
+ httpPath: '/v1/chats/{chatID}/messages/{messageID}/reactions',
90
+ },
91
+ {
92
+ clientCallName: 'client.chats.messages.reactions.add',
93
+ fullyQualifiedName: 'chats.messages.reactions.add',
94
+ httpMethod: 'post',
95
+ httpPath: '/v1/chats/{chatID}/messages/{messageID}/reactions',
96
+ },
97
+ {
98
+ clientCallName: 'client.messages.update',
99
+ fullyQualifiedName: 'messages.update',
100
+ httpMethod: 'put',
101
+ httpPath: '/v1/chats/{chatID}/messages/{messageID}',
102
+ },
103
+ {
104
+ clientCallName: 'client.messages.list',
105
+ fullyQualifiedName: 'messages.list',
106
+ httpMethod: 'get',
107
+ httpPath: '/v1/chats/{chatID}/messages',
108
+ },
109
+ {
110
+ clientCallName: 'client.messages.search',
111
+ fullyQualifiedName: 'messages.search',
112
+ httpMethod: 'get',
113
+ httpPath: '/v1/messages/search',
114
+ },
115
+ {
116
+ clientCallName: 'client.messages.send',
117
+ fullyQualifiedName: 'messages.send',
118
+ httpMethod: 'post',
119
+ httpPath: '/v1/chats/{chatID}/messages',
120
+ },
121
+ {
122
+ clientCallName: 'client.assets.download',
123
+ fullyQualifiedName: 'assets.download',
124
+ httpMethod: 'post',
125
+ httpPath: '/v1/assets/download',
126
+ },
127
+ {
128
+ clientCallName: 'client.assets.serve',
129
+ fullyQualifiedName: 'assets.serve',
130
+ httpMethod: 'get',
131
+ httpPath: '/v1/assets/serve',
132
+ },
133
+ {
134
+ clientCallName: 'client.assets.upload',
135
+ fullyQualifiedName: 'assets.upload',
136
+ httpMethod: 'post',
137
+ httpPath: '/v1/assets/upload',
138
+ },
139
+ {
140
+ clientCallName: 'client.assets.uploadBase64',
141
+ fullyQualifiedName: 'assets.uploadBase64',
142
+ httpMethod: 'post',
143
+ httpPath: '/v1/assets/upload/base64',
144
+ },
145
+ {
146
+ clientCallName: 'client.info.retrieve',
147
+ fullyQualifiedName: 'info.retrieve',
148
+ httpMethod: 'get',
149
+ httpPath: '/v1/info',
150
+ },
151
+ ];
152
+
153
+ function allowedMethodsForCodeTool(options: McpOptions | undefined): SdkMethod[] | undefined {
154
+ if (!options) {
155
+ return undefined;
156
+ }
157
+
158
+ let allowedMethods: SdkMethod[];
159
+
160
+ if (options.codeAllowHttpGets || options.codeAllowedMethods) {
161
+ // Start with nothing allowed and then add into it from options
162
+ let allowedMethodsSet = new Set<SdkMethod>();
163
+
164
+ if (options.codeAllowHttpGets) {
165
+ // Add all methods that map to an HTTP GET
166
+ sdkMethods
167
+ .filter((method) => method.httpMethod === 'get')
168
+ .forEach((method) => allowedMethodsSet.add(method));
169
+ }
170
+
171
+ if (options.codeAllowedMethods) {
172
+ // Add all methods that match any of the allowed regexps
173
+ const allowedRegexps = options.codeAllowedMethods.map((pattern) => {
174
+ try {
175
+ return new RegExp(pattern);
176
+ } catch (e) {
177
+ throw new Error(
178
+ `Invalid regex pattern for allowed method: "${pattern}": ${e instanceof Error ? e.message : e}`,
179
+ );
180
+ }
181
+ });
182
+
183
+ sdkMethods
184
+ .filter((method) => allowedRegexps.some((regexp) => regexp.test(method.fullyQualifiedName)))
185
+ .forEach((method) => allowedMethodsSet.add(method));
186
+ }
187
+
188
+ allowedMethods = Array.from(allowedMethodsSet);
189
+ } else {
190
+ // Start with everything allowed
191
+ allowedMethods = [...sdkMethods];
192
+ }
193
+
194
+ if (options.codeBlockedMethods) {
195
+ // Filter down based on blocked regexps
196
+ const blockedRegexps = options.codeBlockedMethods.map((pattern) => {
197
+ try {
198
+ return new RegExp(pattern);
199
+ } catch (e) {
200
+ throw new Error(
201
+ `Invalid regex pattern for blocked method: "${pattern}": ${e instanceof Error ? e.message : e}`,
202
+ );
203
+ }
204
+ });
205
+
206
+ allowedMethods = allowedMethods.filter(
207
+ (method) => !blockedRegexps.some((regexp) => regexp.test(method.fullyQualifiedName)),
208
+ );
209
+ }
210
+
211
+ return allowedMethods;
212
+ }
213
+
214
+ export function blockedMethodsForCodeTool(options: McpOptions | undefined): SdkMethod[] | undefined {
215
+ const allowedMethods = allowedMethodsForCodeTool(options);
216
+ if (!allowedMethods) {
217
+ return undefined;
218
+ }
219
+
220
+ const allowedSet = new Set(allowedMethods.map((method) => method.fullyQualifiedName));
221
+
222
+ // Return any methods that are not explicitly allowed
223
+ return sdkMethods.filter((method) => !allowedSet.has(method.fullyQualifiedName));
224
+ }