@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.
- package/LICENSE +1 -1
- package/README.md +18 -199
- package/auth.d.mts +6 -0
- package/auth.d.mts.map +1 -0
- package/auth.d.ts +6 -0
- package/auth.d.ts.map +1 -0
- package/auth.js +37 -0
- package/auth.js.map +1 -0
- package/auth.mjs +32 -0
- package/auth.mjs.map +1 -0
- package/code-tool-types.d.mts +7 -7
- package/code-tool-types.d.mts.map +1 -1
- package/code-tool-types.d.ts +7 -7
- package/code-tool-types.d.ts.map +1 -1
- package/code-tool-worker.d.mts.map +1 -1
- package/code-tool-worker.d.ts.map +1 -1
- package/code-tool-worker.js +103 -19
- package/code-tool-worker.js.map +1 -1
- package/code-tool-worker.mjs +103 -19
- package/code-tool-worker.mjs.map +1 -1
- package/code-tool.d.mts +12 -3
- package/code-tool.d.mts.map +1 -1
- package/code-tool.d.ts +12 -3
- package/code-tool.d.ts.map +1 -1
- package/code-tool.js +276 -95
- package/code-tool.js.map +1 -1
- package/code-tool.mjs +273 -95
- package/code-tool.mjs.map +1 -1
- package/http.d.mts +6 -9
- package/http.d.mts.map +1 -1
- package/http.d.ts +6 -9
- package/http.d.ts.map +1 -1
- package/http.js +81 -47
- package/http.js.map +1 -1
- package/http.mjs +80 -43
- package/http.mjs.map +1 -1
- package/index.js +19 -50
- package/index.js.map +1 -1
- package/index.mjs +19 -50
- package/index.mjs.map +1 -1
- package/instructions.d.mts +2 -0
- package/instructions.d.mts.map +1 -0
- package/instructions.d.ts +2 -0
- package/instructions.d.ts.map +1 -0
- package/instructions.js +48 -0
- package/instructions.js.map +1 -0
- package/instructions.mjs +45 -0
- package/instructions.mjs.map +1 -0
- package/logger.d.mts +7 -0
- package/logger.d.mts.map +1 -0
- package/logger.d.ts +7 -0
- package/logger.d.ts.map +1 -0
- package/logger.js +29 -0
- package/logger.js.map +1 -0
- package/logger.mjs +22 -0
- package/logger.mjs.map +1 -0
- package/methods.d.mts +10 -0
- package/methods.d.mts.map +1 -0
- package/methods.d.ts +10 -0
- package/methods.d.ts.map +1 -0
- package/methods.js +203 -0
- package/methods.js.map +1 -0
- package/methods.mjs +199 -0
- package/methods.mjs.map +1 -0
- package/options.d.mts +9 -10
- package/options.d.mts.map +1 -1
- package/options.d.ts +9 -10
- package/options.d.ts.map +1 -1
- package/options.js +52 -363
- package/options.js.map +1 -1
- package/options.mjs +52 -363
- package/options.mjs.map +1 -1
- package/package.json +68 -82
- package/server.d.mts +10 -16
- package/server.d.mts.map +1 -1
- package/server.d.ts +10 -16
- package/server.d.ts.map +1 -1
- package/server.js +82 -105
- package/server.js.map +1 -1
- package/server.mjs +81 -100
- package/server.mjs.map +1 -1
- package/src/auth.ts +42 -0
- package/src/code-tool-types.ts +8 -5
- package/src/code-tool-worker.ts +118 -34
- package/src/code-tool.ts +359 -118
- package/src/http.ts +99 -50
- package/src/index.ts +22 -63
- package/src/instructions.ts +67 -0
- package/src/logger.ts +28 -0
- package/src/methods.ts +224 -0
- package/src/options.ts +69 -390
- package/src/server.ts +95 -121
- package/src/stdio.ts +6 -5
- package/src/{tools/types.ts → types.ts} +25 -5
- package/src/util.ts +25 -0
- package/stdio.d.mts +1 -1
- package/stdio.d.mts.map +1 -1
- package/stdio.d.ts +1 -1
- package/stdio.d.ts.map +1 -1
- package/stdio.js +5 -4
- package/stdio.js.map +1 -1
- package/stdio.mjs +5 -4
- package/stdio.mjs.map +1 -1
- package/{tools/types.d.mts → types.d.mts} +10 -2
- package/types.d.mts.map +1 -0
- package/{tools/types.d.ts → types.d.ts} +10 -2
- package/types.d.ts.map +1 -0
- package/{tools/types.js → types.js} +12 -0
- package/types.js.map +1 -0
- package/{tools/types.mjs → types.mjs} +11 -0
- package/types.mjs.map +1 -0
- package/util.d.mts +4 -0
- package/util.d.mts.map +1 -0
- package/util.d.ts +4 -0
- package/util.d.ts.map +1 -0
- package/util.js +30 -0
- package/util.js.map +1 -0
- package/util.mjs +24 -0
- package/util.mjs.map +1 -0
- package/compat.d.mts +0 -58
- package/compat.d.mts.map +0 -1
- package/compat.d.ts +0 -58
- package/compat.d.ts.map +0 -1
- package/compat.js +0 -387
- package/compat.js.map +0 -1
- package/compat.mjs +0 -378
- package/compat.mjs.map +0 -1
- package/docs-search-tool.d.mts +0 -44
- package/docs-search-tool.d.mts.map +0 -1
- package/docs-search-tool.d.ts +0 -44
- package/docs-search-tool.d.ts.map +0 -1
- package/docs-search-tool.js +0 -43
- package/docs-search-tool.js.map +0 -1
- package/docs-search-tool.mjs +0 -39
- package/docs-search-tool.mjs.map +0 -1
- package/dynamic-tools.d.mts +0 -12
- package/dynamic-tools.d.mts.map +0 -1
- package/dynamic-tools.d.ts +0 -12
- package/dynamic-tools.d.ts.map +0 -1
- package/dynamic-tools.js +0 -135
- package/dynamic-tools.js.map +0 -1
- package/dynamic-tools.mjs +0 -132
- package/dynamic-tools.mjs.map +0 -1
- package/filtering.d.mts +0 -2
- package/filtering.d.mts.map +0 -1
- package/filtering.d.ts +0 -2
- package/filtering.d.ts.map +0 -1
- package/filtering.js +0 -20
- package/filtering.js.map +0 -1
- package/filtering.mjs +0 -13
- package/filtering.mjs.map +0 -1
- package/handlers/get-accounts.d.mts +0 -3
- package/handlers/get-accounts.d.mts.map +0 -1
- package/handlers/get-accounts.d.ts +0 -3
- package/handlers/get-accounts.d.ts.map +0 -1
- package/handlers/get-accounts.js +0 -32
- package/handlers/get-accounts.js.map +0 -1
- package/handlers/get-accounts.mjs +0 -28
- package/handlers/get-accounts.mjs.map +0 -1
- package/handlers/get-chat.d.mts +0 -3
- package/handlers/get-chat.d.mts.map +0 -1
- package/handlers/get-chat.d.ts +0 -3
- package/handlers/get-chat.d.ts.map +0 -1
- package/handlers/get-chat.js +0 -20
- package/handlers/get-chat.js.map +0 -1
- package/handlers/get-chat.mjs +0 -16
- package/handlers/get-chat.mjs.map +0 -1
- package/handlers/index.d.mts +0 -3
- package/handlers/index.d.mts.map +0 -1
- package/handlers/index.d.ts +0 -3
- package/handlers/index.d.ts.map +0 -1
- package/handlers/index.js +0 -30
- package/handlers/index.js.map +0 -1
- package/handlers/index.mjs +0 -27
- package/handlers/index.mjs.map +0 -1
- package/handlers/list-chats.d.mts +0 -3
- package/handlers/list-chats.d.mts.map +0 -1
- package/handlers/list-chats.d.ts +0 -3
- package/handlers/list-chats.d.ts.map +0 -1
- package/handlers/list-chats.js +0 -46
- package/handlers/list-chats.js.map +0 -1
- package/handlers/list-chats.mjs +0 -42
- package/handlers/list-chats.mjs.map +0 -1
- package/handlers/list-messages.d.mts +0 -3
- package/handlers/list-messages.d.mts.map +0 -1
- package/handlers/list-messages.d.ts +0 -3
- package/handlers/list-messages.d.ts.map +0 -1
- package/handlers/list-messages.js +0 -34
- package/handlers/list-messages.js.map +0 -1
- package/handlers/list-messages.mjs +0 -30
- package/handlers/list-messages.mjs.map +0 -1
- package/handlers/open-app.d.mts +0 -3
- package/handlers/open-app.d.mts.map +0 -1
- package/handlers/open-app.d.ts +0 -3
- package/handlers/open-app.d.ts.map +0 -1
- package/handlers/open-app.js +0 -26
- package/handlers/open-app.js.map +0 -1
- package/handlers/open-app.mjs +0 -22
- package/handlers/open-app.mjs.map +0 -1
- package/handlers/search-chats.d.mts +0 -3
- package/handlers/search-chats.d.mts.map +0 -1
- package/handlers/search-chats.d.ts +0 -3
- package/handlers/search-chats.d.ts.map +0 -1
- package/handlers/search-chats.js +0 -38
- package/handlers/search-chats.js.map +0 -1
- package/handlers/search-chats.mjs +0 -34
- package/handlers/search-chats.mjs.map +0 -1
- package/handlers/search-messages.d.mts +0 -3
- package/handlers/search-messages.d.mts.map +0 -1
- package/handlers/search-messages.d.ts +0 -3
- package/handlers/search-messages.d.ts.map +0 -1
- package/handlers/search-messages.js +0 -11
- package/handlers/search-messages.js.map +0 -1
- package/handlers/search-messages.mjs +0 -7
- package/handlers/search-messages.mjs.map +0 -1
- package/handlers/search.d.mts +0 -3
- package/handlers/search.d.mts.map +0 -1
- package/handlers/search.d.ts +0 -3
- package/handlers/search.d.ts.map +0 -1
- package/handlers/search.js +0 -29
- package/handlers/search.js.map +0 -1
- package/handlers/search.mjs +0 -25
- package/handlers/search.mjs.map +0 -1
- package/handlers/send-message.d.mts +0 -3
- package/handlers/send-message.d.mts.map +0 -1
- package/handlers/send-message.d.ts +0 -3
- package/handlers/send-message.d.ts.map +0 -1
- package/handlers/send-message.js +0 -20
- package/handlers/send-message.js.map +0 -1
- package/handlers/send-message.mjs +0 -16
- package/handlers/send-message.mjs.map +0 -1
- package/handlers/utils.d.mts +0 -29
- package/handlers/utils.d.mts.map +0 -1
- package/handlers/utils.d.ts +0 -29
- package/handlers/utils.d.ts.map +0 -1
- package/handlers/utils.js +0 -296
- package/handlers/utils.js.map +0 -1
- package/handlers/utils.mjs +0 -282
- package/handlers/utils.mjs.map +0 -1
- package/headers.d.mts +0 -4
- package/headers.d.mts.map +0 -1
- package/headers.d.ts +0 -4
- package/headers.d.ts.map +0 -1
- package/headers.js +0 -22
- package/headers.js.map +0 -1
- package/headers.mjs +0 -18
- package/headers.mjs.map +0 -1
- package/src/compat.ts +0 -483
- package/src/docs-search-tool.ts +0 -48
- package/src/dynamic-tools.ts +0 -159
- package/src/filtering.ts +0 -14
- package/src/handlers/get-accounts.ts +0 -28
- package/src/handlers/get-chat.ts +0 -18
- package/src/handlers/index.ts +0 -29
- package/src/handlers/list-chats.ts +0 -47
- package/src/handlers/list-messages.ts +0 -33
- package/src/handlers/open-app.ts +0 -20
- package/src/handlers/search-chats.ts +0 -39
- package/src/handlers/search-messages.ts +0 -8
- package/src/handlers/search.ts +0 -24
- package/src/handlers/send-message.ts +0 -17
- package/src/handlers/utils.ts +0 -381
- package/src/headers.ts +0 -23
- package/src/tools/accounts/get-accounts.ts +0 -34
- package/src/tools/chats/archive-chat.ts +0 -43
- package/src/tools/chats/get-chat.ts +0 -45
- package/src/tools/chats/reminders/clear-chat-reminder.ts +0 -41
- package/src/tools/chats/reminders/set-chat-reminder.ts +0 -54
- package/src/tools/chats/search-chats.ts +0 -104
- package/src/tools/index.ts +0 -92
- package/src/tools/messages/list-messages.ts +0 -51
- package/src/tools/messages/search-messages.ts +0 -123
- package/src/tools/messages/send-message.ts +0 -47
- package/src/tools/top-level/focus-app.ts +0 -52
- package/src/tools/top-level/search.ts +0 -41
- package/src/tools.ts +0 -1
- package/tools/accounts/get-accounts.d.mts +0 -45
- package/tools/accounts/get-accounts.d.mts.map +0 -1
- package/tools/accounts/get-accounts.d.ts +0 -45
- package/tools/accounts/get-accounts.d.ts.map +0 -1
- package/tools/accounts/get-accounts.js +0 -31
- package/tools/accounts/get-accounts.js.map +0 -1
- package/tools/accounts/get-accounts.mjs +0 -27
- package/tools/accounts/get-accounts.mjs.map +0 -1
- package/tools/chats/archive-chat.d.mts +0 -45
- package/tools/chats/archive-chat.d.mts.map +0 -1
- package/tools/chats/archive-chat.d.ts +0 -45
- package/tools/chats/archive-chat.d.ts.map +0 -1
- package/tools/chats/archive-chat.js +0 -40
- package/tools/chats/archive-chat.js.map +0 -1
- package/tools/chats/archive-chat.mjs +0 -36
- package/tools/chats/archive-chat.mjs.map +0 -1
- package/tools/chats/get-chat.d.mts +0 -45
- package/tools/chats/get-chat.d.mts.map +0 -1
- package/tools/chats/get-chat.d.ts +0 -45
- package/tools/chats/get-chat.d.ts.map +0 -1
- package/tools/chats/get-chat.js +0 -41
- package/tools/chats/get-chat.js.map +0 -1
- package/tools/chats/get-chat.mjs +0 -37
- package/tools/chats/get-chat.mjs.map +0 -1
- package/tools/chats/reminders/clear-chat-reminder.d.mts +0 -45
- package/tools/chats/reminders/clear-chat-reminder.d.mts.map +0 -1
- package/tools/chats/reminders/clear-chat-reminder.d.ts +0 -45
- package/tools/chats/reminders/clear-chat-reminder.d.ts.map +0 -1
- package/tools/chats/reminders/clear-chat-reminder.js +0 -38
- package/tools/chats/reminders/clear-chat-reminder.js.map +0 -1
- package/tools/chats/reminders/clear-chat-reminder.mjs +0 -34
- package/tools/chats/reminders/clear-chat-reminder.mjs.map +0 -1
- package/tools/chats/reminders/set-chat-reminder.d.mts +0 -45
- package/tools/chats/reminders/set-chat-reminder.d.mts.map +0 -1
- package/tools/chats/reminders/set-chat-reminder.d.ts +0 -45
- package/tools/chats/reminders/set-chat-reminder.d.ts.map +0 -1
- package/tools/chats/reminders/set-chat-reminder.js +0 -51
- package/tools/chats/reminders/set-chat-reminder.js.map +0 -1
- package/tools/chats/reminders/set-chat-reminder.mjs +0 -47
- package/tools/chats/reminders/set-chat-reminder.mjs.map +0 -1
- package/tools/chats/search-chats.d.mts +0 -45
- package/tools/chats/search-chats.d.mts.map +0 -1
- package/tools/chats/search-chats.d.ts +0 -45
- package/tools/chats/search-chats.d.ts.map +0 -1
- package/tools/chats/search-chats.js +0 -92
- package/tools/chats/search-chats.js.map +0 -1
- package/tools/chats/search-chats.mjs +0 -88
- package/tools/chats/search-chats.mjs.map +0 -1
- package/tools/index.d.mts +0 -10
- package/tools/index.d.mts.map +0 -1
- package/tools/index.d.ts +0 -10
- package/tools/index.d.ts.map +0 -1
- package/tools/index.js +0 -76
- package/tools/index.js.map +0 -1
- package/tools/index.mjs +0 -69
- package/tools/index.mjs.map +0 -1
- package/tools/messages/list-messages.d.mts +0 -45
- package/tools/messages/list-messages.d.mts.map +0 -1
- package/tools/messages/list-messages.d.ts +0 -45
- package/tools/messages/list-messages.d.ts.map +0 -1
- package/tools/messages/list-messages.js +0 -47
- package/tools/messages/list-messages.js.map +0 -1
- package/tools/messages/list-messages.mjs +0 -43
- package/tools/messages/list-messages.mjs.map +0 -1
- package/tools/messages/search-messages.d.mts +0 -45
- package/tools/messages/search-messages.d.mts.map +0 -1
- package/tools/messages/search-messages.d.ts +0 -45
- package/tools/messages/search-messages.d.ts.map +0 -1
- package/tools/messages/search-messages.js +0 -110
- package/tools/messages/search-messages.js.map +0 -1
- package/tools/messages/search-messages.mjs +0 -106
- package/tools/messages/search-messages.mjs.map +0 -1
- package/tools/messages/send-message.d.mts +0 -45
- package/tools/messages/send-message.d.mts.map +0 -1
- package/tools/messages/send-message.d.ts +0 -45
- package/tools/messages/send-message.d.ts.map +0 -1
- package/tools/messages/send-message.js +0 -43
- package/tools/messages/send-message.js.map +0 -1
- package/tools/messages/send-message.mjs +0 -39
- package/tools/messages/send-message.mjs.map +0 -1
- package/tools/top-level/focus-app.d.mts +0 -45
- package/tools/top-level/focus-app.d.mts.map +0 -1
- package/tools/top-level/focus-app.d.ts +0 -45
- package/tools/top-level/focus-app.d.ts.map +0 -1
- package/tools/top-level/focus-app.js +0 -47
- package/tools/top-level/focus-app.js.map +0 -1
- package/tools/top-level/focus-app.mjs +0 -43
- package/tools/top-level/focus-app.mjs.map +0 -1
- package/tools/top-level/search.d.mts +0 -45
- package/tools/top-level/search.d.mts.map +0 -1
- package/tools/top-level/search.d.ts +0 -45
- package/tools/top-level/search.d.ts.map +0 -1
- package/tools/top-level/search.js +0 -37
- package/tools/top-level/search.js.map +0 -1
- package/tools/top-level/search.mjs +0 -33
- package/tools/top-level/search.mjs.map +0 -1
- package/tools/types.d.mts.map +0 -1
- package/tools/types.d.ts.map +0 -1
- package/tools/types.js.map +0 -1
- package/tools/types.mjs.map +0 -1
- package/tools.d.mts +0 -2
- package/tools.d.mts.map +0 -1
- package/tools.d.ts +0 -2
- package/tools.d.ts.map +0 -1
- package/tools.js +0 -18
- package/tools.js.map +0 -1
- package/tools.mjs +0 -2
- 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
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
27
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
95
|
-
opts,
|
|
96
|
-
code,
|
|
97
|
-
} satisfies WorkerInput);
|
|
258
|
+
const allowRead = allowReadPaths.join(',');
|
|
98
259
|
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
317
|
+
const body = JSON.stringify({
|
|
318
|
+
opts,
|
|
319
|
+
code,
|
|
106
320
|
});
|
|
107
321
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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
|
-
|
|
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
|
+
};
|