@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.
- 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/docs-search-tool.d.mts +22 -9
- package/docs-search-tool.d.mts.map +1 -1
- package/docs-search-tool.d.ts +22 -9
- package/docs-search-tool.d.ts.map +1 -1
- package/docs-search-tool.js +37 -6
- package/docs-search-tool.js.map +1 -1
- package/docs-search-tool.mjs +37 -6
- package/docs-search-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 -9
- package/options.d.mts.map +1 -1
- package/options.d.ts +9 -9
- package/options.d.ts.map +1 -1
- package/options.js +54 -359
- package/options.js.map +1 -1
- package/options.mjs +54 -359
- package/options.mjs.map +1 -1
- package/package.json +68 -72
- 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 -101
- package/server.js.map +1 -1
- package/server.mjs +81 -96
- 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/docs-search-tool.ts +59 -7
- 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 +72 -384
- package/src/server.ts +94 -117
- 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/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/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/server.mjs
CHANGED
|
@@ -1,46 +1,24 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
-
import { endpoints, query } from "./tools.mjs";
|
|
4
3
|
import { CallToolRequestSchema, ListToolsRequestSchema, SetLevelRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
|
|
5
4
|
import BeeperDesktop from '@beeper/desktop-api';
|
|
6
|
-
import { applyCompatibilityTransformations, defaultClientCapabilities, knownClients, parseEmbeddedJSON, } from "./compat.mjs";
|
|
7
|
-
import { dynamicTools } from "./dynamic-tools.mjs";
|
|
8
5
|
import { codeTool } from "./code-tool.mjs";
|
|
9
6
|
import docsSearchTool from "./docs-search-tool.mjs";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export const newMcpServer = () => new McpServer({
|
|
7
|
+
import { getInstructions } from "./instructions.mjs";
|
|
8
|
+
import { blockedMethodsForCodeTool } from "./methods.mjs";
|
|
9
|
+
export const newMcpServer = async (stainlessApiKey) => new McpServer({
|
|
13
10
|
name: 'beeper_desktop_api_api',
|
|
14
|
-
version: '4.
|
|
11
|
+
version: '4.7.0',
|
|
15
12
|
}, {
|
|
13
|
+
instructions: await getInstructions(stainlessApiKey),
|
|
16
14
|
capabilities: { tools: {}, logging: {} },
|
|
17
|
-
instructions: 'Access to all chats and messages across networks using Beeper Desktop. Can be used to find, get, send, and manage messages and chats.',
|
|
18
15
|
});
|
|
19
|
-
// Create server instance
|
|
20
|
-
export const server = newMcpServer();
|
|
21
16
|
/**
|
|
22
17
|
* Initializes the provided MCP Server with the given tools and handlers.
|
|
23
18
|
* If not provided, the default client, tools and handlers will be used.
|
|
24
19
|
*/
|
|
25
|
-
export function initMcpServer(params) {
|
|
20
|
+
export async function initMcpServer(params) {
|
|
26
21
|
const server = params.server instanceof McpServer ? params.server.server : params.server;
|
|
27
|
-
const mcpOptions = params.mcpOptions ?? {};
|
|
28
|
-
let providedEndpoints = null;
|
|
29
|
-
let endpointMap = null;
|
|
30
|
-
const initTools = async (implementation) => {
|
|
31
|
-
if (implementation && (!mcpOptions.client || mcpOptions.client === 'infer')) {
|
|
32
|
-
mcpOptions.client =
|
|
33
|
-
implementation.name.toLowerCase().includes('claude') ? 'claude'
|
|
34
|
-
: implementation.name.toLowerCase().includes('cursor') ? 'cursor'
|
|
35
|
-
: undefined;
|
|
36
|
-
mcpOptions.capabilities = {
|
|
37
|
-
...(mcpOptions.client && knownClients[mcpOptions.client]),
|
|
38
|
-
...mcpOptions.capabilities,
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
providedEndpoints ?? (providedEndpoints = await selectTools(endpoints, mcpOptions));
|
|
42
|
-
endpointMap ?? (endpointMap = Object.fromEntries(providedEndpoints.map((endpoint) => [endpoint.tool.name, endpoint])));
|
|
43
|
-
};
|
|
44
22
|
const logAtLevel = (level) => (message, ...rest) => {
|
|
45
23
|
void server.sendLoggingMessage({
|
|
46
24
|
level,
|
|
@@ -53,111 +31,118 @@ export function initMcpServer(params) {
|
|
|
53
31
|
warn: logAtLevel('warning'),
|
|
54
32
|
error: logAtLevel('error'),
|
|
55
33
|
};
|
|
56
|
-
let
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
34
|
+
let _client;
|
|
35
|
+
let _clientError;
|
|
36
|
+
let _logLevel;
|
|
37
|
+
const getClient = () => {
|
|
38
|
+
if (_clientError)
|
|
39
|
+
throw _clientError;
|
|
40
|
+
if (!_client) {
|
|
41
|
+
try {
|
|
42
|
+
_client = new BeeperDesktop({
|
|
43
|
+
logger,
|
|
44
|
+
...params.clientOptions,
|
|
45
|
+
defaultHeaders: {
|
|
46
|
+
...params.clientOptions?.defaultHeaders,
|
|
47
|
+
'X-Stainless-MCP': 'true',
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
if (_logLevel) {
|
|
51
|
+
_client = _client.withOptions({ logLevel: _logLevel });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
_clientError = e instanceof Error ? e : new Error(String(e));
|
|
56
|
+
throw _clientError;
|
|
57
|
+
}
|
|
67
58
|
}
|
|
59
|
+
return _client;
|
|
60
|
+
};
|
|
61
|
+
const providedTools = selectTools(params.mcpOptions);
|
|
62
|
+
const toolMap = Object.fromEntries(providedTools.map((mcpTool) => [mcpTool.tool.name, mcpTool]));
|
|
63
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
68
64
|
return {
|
|
69
|
-
tools:
|
|
65
|
+
tools: providedTools.map((mcpTool) => mcpTool.tool),
|
|
70
66
|
};
|
|
71
67
|
});
|
|
72
68
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
73
|
-
if (endpointMap === null) {
|
|
74
|
-
await initTools(server.getClientVersion());
|
|
75
|
-
}
|
|
76
69
|
const { name, arguments: args } = request.params;
|
|
77
|
-
const
|
|
78
|
-
if (!
|
|
70
|
+
const mcpTool = toolMap[name];
|
|
71
|
+
if (!mcpTool) {
|
|
79
72
|
throw new Error(`Unknown tool: ${name}`);
|
|
80
73
|
}
|
|
81
|
-
|
|
74
|
+
let client;
|
|
75
|
+
try {
|
|
76
|
+
client = getClient();
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
return {
|
|
80
|
+
content: [
|
|
81
|
+
{
|
|
82
|
+
type: 'text',
|
|
83
|
+
text: `Failed to initialize client: ${error instanceof Error ? error.message : String(error)}`,
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
isError: true,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return executeHandler({
|
|
90
|
+
handler: mcpTool.handler,
|
|
91
|
+
reqContext: {
|
|
92
|
+
client,
|
|
93
|
+
stainlessApiKey: params.stainlessApiKey ?? params.mcpOptions?.stainlessApiKey,
|
|
94
|
+
},
|
|
95
|
+
args,
|
|
96
|
+
});
|
|
82
97
|
});
|
|
83
98
|
server.setRequestHandler(SetLevelRequestSchema, async (request) => {
|
|
84
99
|
const { level } = request.params;
|
|
100
|
+
let logLevel;
|
|
85
101
|
switch (level) {
|
|
86
102
|
case 'debug':
|
|
87
|
-
|
|
103
|
+
logLevel = 'debug';
|
|
88
104
|
break;
|
|
89
105
|
case 'info':
|
|
90
|
-
|
|
106
|
+
logLevel = 'info';
|
|
91
107
|
break;
|
|
92
108
|
case 'notice':
|
|
93
109
|
case 'warning':
|
|
94
|
-
|
|
110
|
+
logLevel = 'warn';
|
|
95
111
|
break;
|
|
96
112
|
case 'error':
|
|
97
|
-
|
|
113
|
+
logLevel = 'error';
|
|
98
114
|
break;
|
|
99
115
|
default:
|
|
100
|
-
|
|
116
|
+
logLevel = 'off';
|
|
101
117
|
break;
|
|
102
118
|
}
|
|
119
|
+
_logLevel = logLevel;
|
|
120
|
+
if (_client) {
|
|
121
|
+
_client = _client.withOptions({ logLevel });
|
|
122
|
+
}
|
|
103
123
|
return {};
|
|
104
124
|
});
|
|
105
125
|
}
|
|
106
126
|
/**
|
|
107
127
|
* Selects the tools to include in the MCP Server based on the provided options.
|
|
108
128
|
*/
|
|
109
|
-
export
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
if (options?.includeAllTools) {
|
|
119
|
-
includedTools = endpoints.slice();
|
|
120
|
-
}
|
|
121
|
-
else if (options?.includeDynamicTools) {
|
|
122
|
-
includedTools = dynamicTools(endpoints);
|
|
123
|
-
}
|
|
124
|
-
else if (options?.includeCodeTools) {
|
|
125
|
-
includedTools = [await codeTool()];
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
includedTools = endpoints.slice();
|
|
129
|
-
}
|
|
129
|
+
export function selectTools(options) {
|
|
130
|
+
const includedTools = [];
|
|
131
|
+
if (options?.includeCodeTool ?? true) {
|
|
132
|
+
includedTools.push(codeTool({
|
|
133
|
+
blockedMethods: blockedMethodsForCodeTool(options),
|
|
134
|
+
codeExecutionMode: options?.codeExecutionMode ?? 'stainless-sandbox',
|
|
135
|
+
}));
|
|
130
136
|
}
|
|
131
137
|
if (options?.includeDocsTools ?? true) {
|
|
132
138
|
includedTools.push(docsSearchTool);
|
|
133
139
|
}
|
|
134
|
-
|
|
135
|
-
return applyCompatibilityTransformations(includedTools, capabilities);
|
|
140
|
+
return includedTools;
|
|
136
141
|
}
|
|
137
142
|
/**
|
|
138
143
|
* Runs the provided handler with the given client and arguments.
|
|
139
144
|
*/
|
|
140
|
-
export async function executeHandler(
|
|
141
|
-
|
|
142
|
-
if (!options.validJson && args) {
|
|
143
|
-
args = parseEmbeddedJSON(args, tool.inputSchema);
|
|
144
|
-
}
|
|
145
|
-
return await handler(client, args || {});
|
|
145
|
+
export async function executeHandler({ handler, reqContext, args, }) {
|
|
146
|
+
return await handler({ reqContext, args: args || {} });
|
|
146
147
|
}
|
|
147
|
-
export const readEnv = (env) => {
|
|
148
|
-
if (typeof globalThis.process !== 'undefined') {
|
|
149
|
-
return globalThis.process.env?.[env]?.trim();
|
|
150
|
-
}
|
|
151
|
-
else if (typeof globalThis.Deno !== 'undefined') {
|
|
152
|
-
return globalThis.Deno.env?.get?.(env)?.trim();
|
|
153
|
-
}
|
|
154
|
-
return;
|
|
155
|
-
};
|
|
156
|
-
export const readEnvOrError = (env) => {
|
|
157
|
-
let envValue = readEnv(env);
|
|
158
|
-
if (envValue === undefined) {
|
|
159
|
-
throw new Error(`Environment variable ${env} is not set`);
|
|
160
|
-
}
|
|
161
|
-
return envValue;
|
|
162
|
-
};
|
|
163
148
|
//# sourceMappingURL=server.mjs.map
|
package/server.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.mjs","sourceRoot":"","sources":["src/server.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAG/E,EAAE,SAAS,EAAE,MAAM,yCAAyC;OAC5D,
|
|
1
|
+
{"version":3,"file":"server.mjs","sourceRoot":"","sources":["src/server.ts"],"names":[],"mappings":"AAAA,sFAAsF;OAG/E,EAAE,SAAS,EAAE,MAAM,yCAAyC;OAC5D,EACL,qBAAqB,EACrB,sBAAsB,EACtB,qBAAqB,GACtB,MAAM,oCAAoC;OAEpC,aAAa,MAAM,qBAAqB;OACxC,EAAE,QAAQ,EAAE;OACZ,cAAc;OACd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CAAC;QACrB,IAAI,OAAO,EAAE,CAAC;YACZ,OAAO,GAAG,OAAO,CAAC,WAAW,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC9C,CAAC;QACD,OAAO,EAAE,CAAC;IACZ,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,OAAoB;IAC9C,MAAM,aAAa,GAAG,EAAE,CAAC;IAEzB,IAAI,OAAO,EAAE,eAAe,IAAI,IAAI,EAAE,CAAC;QACrC,aAAa,CAAC,IAAI,CAChB,QAAQ,CAAC;YACP,cAAc,EAAE,yBAAyB,CAAC,OAAO,CAAC;YAClD,iBAAiB,EAAE,OAAO,EAAE,iBAAiB,IAAI,mBAAmB;SACrE,CAAC,CACH,CAAC;IACJ,CAAC;IACD,IAAI,OAAO,EAAE,gBAAgB,IAAI,IAAI,EAAE,CAAC;QACtC,aAAa,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,aAAa,CAAC;AACvB,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,EACnC,OAAO,EACP,UAAU,EACV,IAAI,GAKL;IACC,OAAO,MAAM,OAAO,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,IAAI,IAAI,EAAE,EAAE,CAAC,CAAC;AACzD,CAAC"}
|
package/src/auth.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { IncomingMessage } from 'node:http';
|
|
4
|
+
import { ClientOptions } from '@beeper/desktop-api';
|
|
5
|
+
import { McpOptions } from './options';
|
|
6
|
+
|
|
7
|
+
export const parseClientAuthHeaders = (req: IncomingMessage, required?: boolean): Partial<ClientOptions> => {
|
|
8
|
+
if (req.headers.authorization) {
|
|
9
|
+
const scheme = req.headers.authorization.split(' ')[0]!;
|
|
10
|
+
const value = req.headers.authorization.slice(scheme.length + 1);
|
|
11
|
+
switch (scheme) {
|
|
12
|
+
case 'Bearer':
|
|
13
|
+
return { accessToken: req.headers.authorization.slice('Bearer '.length) };
|
|
14
|
+
default:
|
|
15
|
+
throw new Error(
|
|
16
|
+
'Unsupported authorization scheme. Expected the "Authorization" header to be a supported scheme (Bearer).',
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
} else if (required) {
|
|
20
|
+
throw new Error('Missing required Authorization header; see WWW-Authenticate header for details.');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const accessToken =
|
|
24
|
+
Array.isArray(req.headers['x-beeper-access-token']) ?
|
|
25
|
+
req.headers['x-beeper-access-token'][0]
|
|
26
|
+
: req.headers['x-beeper-access-token'];
|
|
27
|
+
return { accessToken };
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const getStainlessApiKey = (req: IncomingMessage, mcpOptions: McpOptions): string | undefined => {
|
|
31
|
+
// Try to get the key from the x-stainless-api-key header
|
|
32
|
+
const headerKey =
|
|
33
|
+
Array.isArray(req.headers['x-stainless-api-key']) ?
|
|
34
|
+
req.headers['x-stainless-api-key'][0]
|
|
35
|
+
: req.headers['x-stainless-api-key'];
|
|
36
|
+
if (headerKey && typeof headerKey === 'string') {
|
|
37
|
+
return headerKey;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Fall back to value set in the mcpOptions (e.g. from environment variable), if provided
|
|
41
|
+
return mcpOptions.stainlessApiKey;
|
|
42
|
+
};
|
package/src/code-tool-types.ts
CHANGED
|
@@ -3,12 +3,15 @@
|
|
|
3
3
|
import { ClientOptions } from '@beeper/desktop-api';
|
|
4
4
|
|
|
5
5
|
export type WorkerInput = {
|
|
6
|
-
|
|
6
|
+
project_name: string;
|
|
7
7
|
code: string;
|
|
8
|
+
client_opts: ClientOptions;
|
|
9
|
+
intent?: string | undefined;
|
|
8
10
|
};
|
|
9
|
-
|
|
11
|
+
|
|
12
|
+
export type WorkerOutput = {
|
|
13
|
+
is_error: boolean;
|
|
10
14
|
result: unknown | null;
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
log_lines: string[];
|
|
16
|
+
err_lines: string[];
|
|
13
17
|
};
|
|
14
|
-
export type WorkerError = { message: string | undefined };
|
package/src/code-tool-worker.ts
CHANGED
|
@@ -1,37 +1,47 @@
|
|
|
1
1
|
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
2
|
|
|
3
|
+
import path from 'node:path';
|
|
3
4
|
import util from 'node:util';
|
|
4
|
-
|
|
5
5
|
import Fuse from 'fuse.js';
|
|
6
6
|
import ts from 'typescript';
|
|
7
|
+
import { WorkerOutput } from './code-tool-types';
|
|
8
|
+
import { BeeperDesktop, ClientOptions } from '@beeper/desktop-api';
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
): ts.FunctionDeclaration | ts.FunctionExpression | ts.ArrowFunction | null {
|
|
10
|
+
function getRunFunctionSource(code: string): {
|
|
11
|
+
type: 'declaration' | 'expression';
|
|
12
|
+
client: string | undefined;
|
|
13
|
+
code: string;
|
|
14
|
+
} | null {
|
|
14
15
|
const sourceFile = ts.createSourceFile('code.ts', code, ts.ScriptTarget.Latest, true);
|
|
16
|
+
const printer = ts.createPrinter();
|
|
15
17
|
|
|
16
18
|
for (const statement of sourceFile.statements) {
|
|
17
19
|
// Check for top-level function declarations
|
|
18
20
|
if (ts.isFunctionDeclaration(statement)) {
|
|
19
21
|
if (statement.name?.text === 'run') {
|
|
20
|
-
return
|
|
22
|
+
return {
|
|
23
|
+
type: 'declaration',
|
|
24
|
+
client: statement.parameters[0]?.name.getText(),
|
|
25
|
+
code: printer.printNode(ts.EmitHint.Unspecified, statement.body!, sourceFile),
|
|
26
|
+
};
|
|
21
27
|
}
|
|
22
28
|
}
|
|
23
29
|
|
|
24
30
|
// Check for variable declarations: const run = () => {} or const run = function() {}
|
|
25
31
|
if (ts.isVariableStatement(statement)) {
|
|
26
32
|
for (const declaration of statement.declarationList.declarations) {
|
|
27
|
-
if (
|
|
33
|
+
if (
|
|
34
|
+
ts.isIdentifier(declaration.name) &&
|
|
35
|
+
declaration.name.text === 'run' &&
|
|
28
36
|
// Check if it's initialized with a function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
declaration.initializer &&
|
|
38
|
+
(ts.isFunctionExpression(declaration.initializer) || ts.isArrowFunction(declaration.initializer))
|
|
39
|
+
) {
|
|
40
|
+
return {
|
|
41
|
+
type: 'expression',
|
|
42
|
+
client: declaration.initializer.parameters[0]?.name.getText(),
|
|
43
|
+
code: printer.printNode(ts.EmitHint.Unspecified, declaration.initializer, sourceFile),
|
|
44
|
+
};
|
|
35
45
|
}
|
|
36
46
|
}
|
|
37
47
|
}
|
|
@@ -40,11 +50,64 @@ function getRunFunctionNode(
|
|
|
40
50
|
return null;
|
|
41
51
|
}
|
|
42
52
|
|
|
53
|
+
function getTSDiagnostics(code: string): string[] {
|
|
54
|
+
const functionSource = getRunFunctionSource(code)!;
|
|
55
|
+
const codeWithImport = [
|
|
56
|
+
'import { BeeperDesktop } from "@beeper/desktop-api";',
|
|
57
|
+
functionSource.type === 'declaration' ?
|
|
58
|
+
`async function run(${functionSource.client}: BeeperDesktop)`
|
|
59
|
+
: `const run: (${functionSource.client}: BeeperDesktop) => Promise<unknown> =`,
|
|
60
|
+
functionSource.code,
|
|
61
|
+
].join('\n');
|
|
62
|
+
const sourcePath = path.resolve('code.ts');
|
|
63
|
+
const ast = ts.createSourceFile(sourcePath, codeWithImport, ts.ScriptTarget.Latest, true);
|
|
64
|
+
const options = ts.getDefaultCompilerOptions();
|
|
65
|
+
options.target = ts.ScriptTarget.Latest;
|
|
66
|
+
options.module = ts.ModuleKind.NodeNext;
|
|
67
|
+
options.moduleResolution = ts.ModuleResolutionKind.NodeNext;
|
|
68
|
+
const host = ts.createCompilerHost(options, true);
|
|
69
|
+
const newHost: typeof host = {
|
|
70
|
+
...host,
|
|
71
|
+
getSourceFile: (...args) => {
|
|
72
|
+
if (path.resolve(args[0]) === sourcePath) {
|
|
73
|
+
return ast;
|
|
74
|
+
}
|
|
75
|
+
return host.getSourceFile(...args);
|
|
76
|
+
},
|
|
77
|
+
readFile: (...args) => {
|
|
78
|
+
if (path.resolve(args[0]) === sourcePath) {
|
|
79
|
+
return codeWithImport;
|
|
80
|
+
}
|
|
81
|
+
return host.readFile(...args);
|
|
82
|
+
},
|
|
83
|
+
fileExists: (...args) => {
|
|
84
|
+
if (path.resolve(args[0]) === sourcePath) {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
return host.fileExists(...args);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
const program = ts.createProgram({
|
|
91
|
+
options,
|
|
92
|
+
rootNames: [sourcePath],
|
|
93
|
+
host: newHost,
|
|
94
|
+
});
|
|
95
|
+
const diagnostics = ts.getPreEmitDiagnostics(program, ast);
|
|
96
|
+
return diagnostics.map((d) => {
|
|
97
|
+
const message = ts.flattenDiagnosticMessageText(d.messageText, '\n');
|
|
98
|
+
if (!d.file || !d.start) return `- ${message}`;
|
|
99
|
+
const { line: lineNumber } = ts.getLineAndCharacterOfPosition(d.file, d.start);
|
|
100
|
+
const line = codeWithImport.split('\n').at(lineNumber)?.trim();
|
|
101
|
+
return line ? `- ${message}\n ${line}` : `- ${message}`;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
43
105
|
const fuse = new Fuse(
|
|
44
106
|
[
|
|
45
107
|
'client.focus',
|
|
46
108
|
'client.search',
|
|
47
109
|
'client.accounts.list',
|
|
110
|
+
'client.accounts.contacts.list',
|
|
48
111
|
'client.accounts.contacts.search',
|
|
49
112
|
'client.chats.archive',
|
|
50
113
|
'client.chats.create',
|
|
@@ -53,10 +116,17 @@ const fuse = new Fuse(
|
|
|
53
116
|
'client.chats.search',
|
|
54
117
|
'client.chats.reminders.create',
|
|
55
118
|
'client.chats.reminders.delete',
|
|
119
|
+
'client.chats.messages.reactions.add',
|
|
120
|
+
'client.chats.messages.reactions.delete',
|
|
56
121
|
'client.messages.list',
|
|
57
122
|
'client.messages.search',
|
|
58
123
|
'client.messages.send',
|
|
124
|
+
'client.messages.update',
|
|
59
125
|
'client.assets.download',
|
|
126
|
+
'client.assets.serve',
|
|
127
|
+
'client.assets.upload',
|
|
128
|
+
'client.assets.uploadBase64',
|
|
129
|
+
'client.info.retrieve',
|
|
60
130
|
],
|
|
61
131
|
{ threshold: 1, shouldSort: true },
|
|
62
132
|
);
|
|
@@ -151,24 +221,34 @@ function parseError(code: string, error: unknown): string | undefined {
|
|
|
151
221
|
}
|
|
152
222
|
|
|
153
223
|
const fetch = async (req: Request): Promise<Response> => {
|
|
154
|
-
const { opts, code } = (await req.json()) as
|
|
155
|
-
|
|
224
|
+
const { opts, code } = (await req.json()) as { opts: ClientOptions; code: string };
|
|
225
|
+
|
|
226
|
+
const runFunctionSource = code ? getRunFunctionSource(code) : null;
|
|
227
|
+
if (!runFunctionSource) {
|
|
228
|
+
const message =
|
|
229
|
+
code ?
|
|
230
|
+
'The code is missing a top-level `run` function.'
|
|
231
|
+
: 'The code argument is missing. Provide one containing a top-level `run` function.';
|
|
156
232
|
return Response.json(
|
|
157
233
|
{
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
234
|
+
is_error: true,
|
|
235
|
+
result: `${message} Write code within this template:\n\n\`\`\`\nasync function run(client) {\n // Fill this out\n}\n\`\`\``,
|
|
236
|
+
log_lines: [],
|
|
237
|
+
err_lines: [],
|
|
238
|
+
} satisfies WorkerOutput,
|
|
161
239
|
{ status: 400, statusText: 'Code execution error' },
|
|
162
240
|
);
|
|
163
241
|
}
|
|
164
242
|
|
|
165
|
-
const
|
|
166
|
-
if (
|
|
243
|
+
const diagnostics = getTSDiagnostics(code);
|
|
244
|
+
if (diagnostics.length > 0) {
|
|
167
245
|
return Response.json(
|
|
168
246
|
{
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
247
|
+
is_error: true,
|
|
248
|
+
result: `The code contains TypeScript diagnostics:\n${diagnostics.join('\n')}`,
|
|
249
|
+
log_lines: [],
|
|
250
|
+
err_lines: [],
|
|
251
|
+
} satisfies WorkerOutput,
|
|
172
252
|
{ status: 400, statusText: 'Code execution error' },
|
|
173
253
|
);
|
|
174
254
|
}
|
|
@@ -177,14 +257,14 @@ const fetch = async (req: Request): Promise<Response> => {
|
|
|
177
257
|
...opts,
|
|
178
258
|
});
|
|
179
259
|
|
|
180
|
-
const
|
|
181
|
-
const
|
|
260
|
+
const log_lines: string[] = [];
|
|
261
|
+
const err_lines: string[] = [];
|
|
182
262
|
const console = {
|
|
183
263
|
log: (...args: unknown[]) => {
|
|
184
|
-
|
|
264
|
+
log_lines.push(util.format(...args));
|
|
185
265
|
},
|
|
186
266
|
error: (...args: unknown[]) => {
|
|
187
|
-
|
|
267
|
+
err_lines.push(util.format(...args));
|
|
188
268
|
},
|
|
189
269
|
};
|
|
190
270
|
try {
|
|
@@ -192,15 +272,19 @@ const fetch = async (req: Request): Promise<Response> => {
|
|
|
192
272
|
eval(`${code}\nrun_ = run;`);
|
|
193
273
|
const result = await run_(makeSdkProxy(client, { path: ['client'] }));
|
|
194
274
|
return Response.json({
|
|
275
|
+
is_error: false,
|
|
195
276
|
result,
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
} satisfies
|
|
277
|
+
log_lines,
|
|
278
|
+
err_lines,
|
|
279
|
+
} satisfies WorkerOutput);
|
|
199
280
|
} catch (e) {
|
|
200
281
|
return Response.json(
|
|
201
282
|
{
|
|
202
|
-
|
|
203
|
-
|
|
283
|
+
is_error: true,
|
|
284
|
+
result: parseError(code, e),
|
|
285
|
+
log_lines,
|
|
286
|
+
err_lines,
|
|
287
|
+
} satisfies WorkerOutput,
|
|
204
288
|
{ status: 400, statusText: 'Code execution error' },
|
|
205
289
|
);
|
|
206
290
|
}
|