@revoxai/sdk-mcp 0.1.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 +201 -0
- package/README.md +101 -0
- 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-paths.cjs +6 -0
- package/code-tool-paths.cjs.map +1 -0
- package/code-tool-paths.d.cts +2 -0
- package/code-tool-paths.d.cts.map +1 -0
- package/code-tool-types.d.mts +14 -0
- package/code-tool-types.d.mts.map +1 -0
- package/code-tool-types.d.ts +14 -0
- package/code-tool-types.d.ts.map +1 -0
- package/code-tool-types.js +4 -0
- package/code-tool-types.js.map +1 -0
- package/code-tool-types.mjs +3 -0
- package/code-tool-types.mjs.map +1 -0
- package/code-tool-worker.d.mts +5 -0
- package/code-tool-worker.d.mts.map +1 -0
- package/code-tool-worker.d.ts +5 -0
- package/code-tool-worker.d.ts.map +1 -0
- package/code-tool-worker.js +237 -0
- package/code-tool-worker.js.map +1 -0
- package/code-tool-worker.mjs +232 -0
- package/code-tool-worker.mjs.map +1 -0
- package/code-tool.d.mts +21 -0
- package/code-tool.d.mts.map +1 -0
- package/code-tool.d.ts +21 -0
- package/code-tool.d.ts.map +1 -0
- package/code-tool.js +331 -0
- package/code-tool.js.map +1 -0
- package/code-tool.mjs +292 -0
- package/code-tool.mjs.map +1 -0
- package/docs-search-tool.d.mts +57 -0
- package/docs-search-tool.d.mts.map +1 -0
- package/docs-search-tool.d.ts +57 -0
- package/docs-search-tool.d.ts.map +1 -0
- package/docs-search-tool.js +74 -0
- package/docs-search-tool.js.map +1 -0
- package/docs-search-tool.mjs +70 -0
- package/docs-search-tool.mjs.map +1 -0
- package/http.d.mts +12 -0
- package/http.d.mts.map +1 -0
- package/http.d.ts +12 -0
- package/http.d.ts.map +1 -0
- package/http.js +128 -0
- package/http.js.map +1 -0
- package/http.mjs +120 -0
- package/http.mjs.map +1 -0
- package/index.d.mts +3 -0
- package/index.d.mts.map +1 -0
- package/index.d.ts +3 -0
- package/index.d.ts.map +1 -0
- package/index.js +60 -0
- package/index.js.map +1 -0
- package/index.mjs +58 -0
- package/index.mjs.map +1 -0
- 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 +46 -0
- package/instructions.js.map +1 -0
- package/instructions.mjs +43 -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 +143 -0
- package/methods.js.map +1 -0
- package/methods.mjs +139 -0
- package/methods.mjs.map +1 -0
- package/options.d.mts +20 -0
- package/options.d.mts.map +1 -0
- package/options.d.ts +20 -0
- package/options.d.ts.map +1 -0
- package/options.js +122 -0
- package/options.js.map +1 -0
- package/options.mjs +115 -0
- package/options.mjs.map +1 -0
- package/package.json +224 -0
- package/server.d.mts +29 -0
- package/server.d.mts.map +1 -0
- package/server.d.ts +29 -0
- package/server.d.ts.map +1 -0
- package/server.js +158 -0
- package/server.js.map +1 -0
- package/server.mjs +148 -0
- package/server.mjs.map +1 -0
- package/src/auth.ts +42 -0
- package/src/code-tool-paths.cts +3 -0
- package/src/code-tool-types.ts +17 -0
- package/src/code-tool-worker.ts +283 -0
- package/src/code-tool.ts +383 -0
- package/src/docs-search-tool.ts +100 -0
- package/src/http.ts +159 -0
- package/src/index.ts +67 -0
- package/src/instructions.ts +65 -0
- package/src/logger.ts +28 -0
- package/src/methods.ts +164 -0
- package/src/options.ts +161 -0
- package/src/server.ts +188 -0
- package/src/stdio.ts +14 -0
- package/src/tsconfig.json +11 -0
- package/src/types.ts +123 -0
- package/src/util.ts +25 -0
- package/stdio.d.mts +3 -0
- package/stdio.d.mts.map +1 -0
- package/stdio.d.ts +3 -0
- package/stdio.d.ts.map +1 -0
- package/stdio.js +15 -0
- package/stdio.js.map +1 -0
- package/stdio.mjs +11 -0
- package/stdio.mjs.map +1 -0
- package/types.d.mts +59 -0
- package/types.d.mts.map +1 -0
- package/types.d.ts +59 -0
- package/types.d.ts.map +1 -0
- package/types.js +58 -0
- package/types.js.map +1 -0
- package/types.mjs +53 -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/server.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.newMcpServer = void 0;
|
|
8
|
+
exports.initMcpServer = initMcpServer;
|
|
9
|
+
exports.selectTools = selectTools;
|
|
10
|
+
exports.executeHandler = executeHandler;
|
|
11
|
+
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
12
|
+
const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
|
|
13
|
+
const sdk_1 = __importDefault(require("@revoxai/sdk"));
|
|
14
|
+
const code_tool_1 = require("./code-tool.js");
|
|
15
|
+
const docs_search_tool_1 = __importDefault(require("./docs-search-tool.js"));
|
|
16
|
+
const instructions_1 = require("./instructions.js");
|
|
17
|
+
const methods_1 = require("./methods.js");
|
|
18
|
+
const newMcpServer = async (stainlessApiKey) => new mcp_js_1.McpServer({
|
|
19
|
+
name: 'revoxai_sdk_api',
|
|
20
|
+
version: '0.1.0',
|
|
21
|
+
}, {
|
|
22
|
+
instructions: await (0, instructions_1.getInstructions)(stainlessApiKey),
|
|
23
|
+
capabilities: { tools: {}, logging: {} },
|
|
24
|
+
});
|
|
25
|
+
exports.newMcpServer = newMcpServer;
|
|
26
|
+
/**
|
|
27
|
+
* Initializes the provided MCP Server with the given tools and handlers.
|
|
28
|
+
* If not provided, the default client, tools and handlers will be used.
|
|
29
|
+
*/
|
|
30
|
+
async function initMcpServer(params) {
|
|
31
|
+
const server = params.server instanceof mcp_js_1.McpServer ? params.server.server : params.server;
|
|
32
|
+
const logAtLevel = (level) => (message, ...rest) => {
|
|
33
|
+
void server.sendLoggingMessage({
|
|
34
|
+
level,
|
|
35
|
+
data: { message, rest },
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
const logger = {
|
|
39
|
+
debug: logAtLevel('debug'),
|
|
40
|
+
info: logAtLevel('info'),
|
|
41
|
+
warn: logAtLevel('warning'),
|
|
42
|
+
error: logAtLevel('error'),
|
|
43
|
+
};
|
|
44
|
+
let _client;
|
|
45
|
+
let _clientError;
|
|
46
|
+
let _logLevel;
|
|
47
|
+
const getClient = () => {
|
|
48
|
+
if (_clientError)
|
|
49
|
+
throw _clientError;
|
|
50
|
+
if (!_client) {
|
|
51
|
+
try {
|
|
52
|
+
_client = new sdk_1.default({
|
|
53
|
+
logger,
|
|
54
|
+
...params.clientOptions,
|
|
55
|
+
defaultHeaders: {
|
|
56
|
+
...params.clientOptions?.defaultHeaders,
|
|
57
|
+
'X-Stainless-MCP': 'true',
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
if (_logLevel) {
|
|
61
|
+
_client = _client.withOptions({ logLevel: _logLevel });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
_clientError = e instanceof Error ? e : new Error(String(e));
|
|
66
|
+
throw _clientError;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return _client;
|
|
70
|
+
};
|
|
71
|
+
const providedTools = selectTools(params.mcpOptions);
|
|
72
|
+
const toolMap = Object.fromEntries(providedTools.map((mcpTool) => [mcpTool.tool.name, mcpTool]));
|
|
73
|
+
server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => {
|
|
74
|
+
return {
|
|
75
|
+
tools: providedTools.map((mcpTool) => mcpTool.tool),
|
|
76
|
+
};
|
|
77
|
+
});
|
|
78
|
+
server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
|
|
79
|
+
const { name, arguments: args } = request.params;
|
|
80
|
+
const mcpTool = toolMap[name];
|
|
81
|
+
if (!mcpTool) {
|
|
82
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
83
|
+
}
|
|
84
|
+
let client;
|
|
85
|
+
try {
|
|
86
|
+
client = getClient();
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
return {
|
|
90
|
+
content: [
|
|
91
|
+
{
|
|
92
|
+
type: 'text',
|
|
93
|
+
text: `Failed to initialize client: ${error instanceof Error ? error.message : String(error)}`,
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
isError: true,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return executeHandler({
|
|
100
|
+
handler: mcpTool.handler,
|
|
101
|
+
reqContext: {
|
|
102
|
+
client,
|
|
103
|
+
stainlessApiKey: params.stainlessApiKey ?? params.mcpOptions?.stainlessApiKey,
|
|
104
|
+
},
|
|
105
|
+
args,
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
server.setRequestHandler(types_js_1.SetLevelRequestSchema, async (request) => {
|
|
109
|
+
const { level } = request.params;
|
|
110
|
+
let logLevel;
|
|
111
|
+
switch (level) {
|
|
112
|
+
case 'debug':
|
|
113
|
+
logLevel = 'debug';
|
|
114
|
+
break;
|
|
115
|
+
case 'info':
|
|
116
|
+
logLevel = 'info';
|
|
117
|
+
break;
|
|
118
|
+
case 'notice':
|
|
119
|
+
case 'warning':
|
|
120
|
+
logLevel = 'warn';
|
|
121
|
+
break;
|
|
122
|
+
case 'error':
|
|
123
|
+
logLevel = 'error';
|
|
124
|
+
break;
|
|
125
|
+
default:
|
|
126
|
+
logLevel = 'off';
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
_logLevel = logLevel;
|
|
130
|
+
if (_client) {
|
|
131
|
+
_client = _client.withOptions({ logLevel });
|
|
132
|
+
}
|
|
133
|
+
return {};
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Selects the tools to include in the MCP Server based on the provided options.
|
|
138
|
+
*/
|
|
139
|
+
function selectTools(options) {
|
|
140
|
+
const includedTools = [];
|
|
141
|
+
if (options?.includeCodeTool ?? true) {
|
|
142
|
+
includedTools.push((0, code_tool_1.codeTool)({
|
|
143
|
+
blockedMethods: (0, methods_1.blockedMethodsForCodeTool)(options),
|
|
144
|
+
codeExecutionMode: options?.codeExecutionMode ?? 'stainless-sandbox',
|
|
145
|
+
}));
|
|
146
|
+
}
|
|
147
|
+
if (options?.includeDocsTools ?? true) {
|
|
148
|
+
includedTools.push(docs_search_tool_1.default);
|
|
149
|
+
}
|
|
150
|
+
return includedTools;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Runs the provided handler with the given client and arguments.
|
|
154
|
+
*/
|
|
155
|
+
async function executeHandler({ handler, reqContext, args, }) {
|
|
156
|
+
return await handler({ reqContext, args: args || {} });
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=server.js.map
|
package/server.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["src/server.ts"],"names":[],"mappings":";AAAA,sFAAsF;;;;;;AAkCtF,sCAsHC;AAKD,kCAeC;AAKD,wCAUC;AAxLD,oEAAoE;AACpE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ACjB,MAAM;QACV,CAAC;QACD,SAAS,GAAG,QAAQ,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,SAAgB,WAAW,CAAC,OAAoB;IAC9C,MAAM,aAAa,GAAG,EAAE,CAAC;IAEzB,IAAI,OAAO,EAAE,eAAe,IAAI,IAAI,EAAE,CAAC;QACrC,aAAa,CAAC,IAAI,CAChB,IAAA,oBAAQ,EAAC;YACP,cAAc,EAAE,IAAA,mCAAyB,EAAC,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,0BAAc,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,aAAa,CAAC;AACvB,CAAC;AAED;;GAEG;AACI,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/server.mjs
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
+
import { CallToolRequestSchema, ListToolsRequestSchema, SetLevelRequestSchema, } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
+
import Revox from '@revoxai/sdk';
|
|
5
|
+
import { codeTool } from "./code-tool.mjs";
|
|
6
|
+
import docsSearchTool from "./docs-search-tool.mjs";
|
|
7
|
+
import { getInstructions } from "./instructions.mjs";
|
|
8
|
+
import { blockedMethodsForCodeTool } from "./methods.mjs";
|
|
9
|
+
export const newMcpServer = async (stainlessApiKey) => new McpServer({
|
|
10
|
+
name: 'revoxai_sdk_api',
|
|
11
|
+
version: '0.1.0',
|
|
12
|
+
}, {
|
|
13
|
+
instructions: await getInstructions(stainlessApiKey),
|
|
14
|
+
capabilities: { tools: {}, logging: {} },
|
|
15
|
+
});
|
|
16
|
+
/**
|
|
17
|
+
* Initializes the provided MCP Server with the given tools and handlers.
|
|
18
|
+
* If not provided, the default client, tools and handlers will be used.
|
|
19
|
+
*/
|
|
20
|
+
export async function initMcpServer(params) {
|
|
21
|
+
const server = params.server instanceof McpServer ? params.server.server : params.server;
|
|
22
|
+
const logAtLevel = (level) => (message, ...rest) => {
|
|
23
|
+
void server.sendLoggingMessage({
|
|
24
|
+
level,
|
|
25
|
+
data: { message, rest },
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
const logger = {
|
|
29
|
+
debug: logAtLevel('debug'),
|
|
30
|
+
info: logAtLevel('info'),
|
|
31
|
+
warn: logAtLevel('warning'),
|
|
32
|
+
error: logAtLevel('error'),
|
|
33
|
+
};
|
|
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 Revox({
|
|
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
|
+
}
|
|
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 () => {
|
|
64
|
+
return {
|
|
65
|
+
tools: providedTools.map((mcpTool) => mcpTool.tool),
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
69
|
+
const { name, arguments: args } = request.params;
|
|
70
|
+
const mcpTool = toolMap[name];
|
|
71
|
+
if (!mcpTool) {
|
|
72
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
73
|
+
}
|
|
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
|
+
});
|
|
97
|
+
});
|
|
98
|
+
server.setRequestHandler(SetLevelRequestSchema, async (request) => {
|
|
99
|
+
const { level } = request.params;
|
|
100
|
+
let logLevel;
|
|
101
|
+
switch (level) {
|
|
102
|
+
case 'debug':
|
|
103
|
+
logLevel = 'debug';
|
|
104
|
+
break;
|
|
105
|
+
case 'info':
|
|
106
|
+
logLevel = 'info';
|
|
107
|
+
break;
|
|
108
|
+
case 'notice':
|
|
109
|
+
case 'warning':
|
|
110
|
+
logLevel = 'warn';
|
|
111
|
+
break;
|
|
112
|
+
case 'error':
|
|
113
|
+
logLevel = 'error';
|
|
114
|
+
break;
|
|
115
|
+
default:
|
|
116
|
+
logLevel = 'off';
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
_logLevel = logLevel;
|
|
120
|
+
if (_client) {
|
|
121
|
+
_client = _client.withOptions({ logLevel });
|
|
122
|
+
}
|
|
123
|
+
return {};
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Selects the tools to include in the MCP Server based on the provided options.
|
|
128
|
+
*/
|
|
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
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
if (options?.includeDocsTools ?? true) {
|
|
138
|
+
includedTools.push(docsSearchTool);
|
|
139
|
+
}
|
|
140
|
+
return includedTools;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Runs the provided handler with the given client and arguments.
|
|
144
|
+
*/
|
|
145
|
+
export async function executeHandler({ handler, reqContext, args, }) {
|
|
146
|
+
return await handler({ reqContext, args: args || {} });
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=server.mjs.map
|
package/server.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
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,oCAAo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AAG,KAAK,CAAC;gBACjB,MAAM;QACV,CAAC;QACD,SAAS,GAAG,QAAQ,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 '@revoxai/sdk';
|
|
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 { apiKey: 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 apiKey =
|
|
24
|
+
Array.isArray(req.headers['x-revox-api-key']) ?
|
|
25
|
+
req.headers['x-revox-api-key'][0]
|
|
26
|
+
: req.headers['x-revox-api-key'];
|
|
27
|
+
return { apiKey };
|
|
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
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { ClientOptions } from '@revoxai/sdk';
|
|
4
|
+
|
|
5
|
+
export type WorkerInput = {
|
|
6
|
+
project_name: string;
|
|
7
|
+
code: string;
|
|
8
|
+
client_opts: ClientOptions;
|
|
9
|
+
intent?: string | undefined;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type WorkerOutput = {
|
|
13
|
+
is_error: boolean;
|
|
14
|
+
result: unknown | null;
|
|
15
|
+
log_lines: string[];
|
|
16
|
+
err_lines: string[];
|
|
17
|
+
};
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import util from 'node:util';
|
|
5
|
+
import Fuse from 'fuse.js';
|
|
6
|
+
import ts from 'typescript';
|
|
7
|
+
import { WorkerOutput } from './code-tool-types';
|
|
8
|
+
import { Revox, ClientOptions } from '@revoxai/sdk';
|
|
9
|
+
|
|
10
|
+
function getRunFunctionSource(code: string): {
|
|
11
|
+
type: 'declaration' | 'expression';
|
|
12
|
+
client: string | undefined;
|
|
13
|
+
code: string;
|
|
14
|
+
} | null {
|
|
15
|
+
const sourceFile = ts.createSourceFile('code.ts', code, ts.ScriptTarget.Latest, true);
|
|
16
|
+
const printer = ts.createPrinter();
|
|
17
|
+
|
|
18
|
+
for (const statement of sourceFile.statements) {
|
|
19
|
+
// Check for top-level function declarations
|
|
20
|
+
if (ts.isFunctionDeclaration(statement)) {
|
|
21
|
+
if (statement.name?.text === 'run') {
|
|
22
|
+
return {
|
|
23
|
+
type: 'declaration',
|
|
24
|
+
client: statement.parameters[0]?.name.getText(),
|
|
25
|
+
code: printer.printNode(ts.EmitHint.Unspecified, statement.body!, sourceFile),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Check for variable declarations: const run = () => {} or const run = function() {}
|
|
31
|
+
if (ts.isVariableStatement(statement)) {
|
|
32
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
33
|
+
if (
|
|
34
|
+
ts.isIdentifier(declaration.name) &&
|
|
35
|
+
declaration.name.text === 'run' &&
|
|
36
|
+
// Check if it's initialized with a function
|
|
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
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function getTSDiagnostics(code: string): string[] {
|
|
54
|
+
const functionSource = getRunFunctionSource(code)!;
|
|
55
|
+
const codeWithImport = [
|
|
56
|
+
'import { Revox } from "@revoxai/sdk";',
|
|
57
|
+
functionSource.type === 'declaration' ?
|
|
58
|
+
`async function run(${functionSource.client}: Revox)`
|
|
59
|
+
: `const run: (${functionSource.client}: Revox) => 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
|
+
|
|
105
|
+
const fuse = new Fuse(
|
|
106
|
+
[
|
|
107
|
+
'client.assistants.create',
|
|
108
|
+
'client.assistants.delete',
|
|
109
|
+
'client.assistants.list',
|
|
110
|
+
'client.assistants.retrieve',
|
|
111
|
+
'client.assistants.update',
|
|
112
|
+
'client.call.create',
|
|
113
|
+
'client.call.list',
|
|
114
|
+
'client.call.retrieve',
|
|
115
|
+
'client.voices.list',
|
|
116
|
+
'client.voices.preview',
|
|
117
|
+
'client.voices.retrieve',
|
|
118
|
+
'client.users.me.retrieve',
|
|
119
|
+
'client.users.me.update',
|
|
120
|
+
],
|
|
121
|
+
{ threshold: 1, shouldSort: true },
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
function getMethodSuggestions(fullyQualifiedMethodName: string): string[] {
|
|
125
|
+
return fuse
|
|
126
|
+
.search(fullyQualifiedMethodName)
|
|
127
|
+
.map(({ item }) => item)
|
|
128
|
+
.slice(0, 5);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const proxyToObj = new WeakMap<any, any>();
|
|
132
|
+
const objToProxy = new WeakMap<any, any>();
|
|
133
|
+
|
|
134
|
+
type ClientProxyConfig = {
|
|
135
|
+
path: string[];
|
|
136
|
+
isBelievedBad?: boolean;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T {
|
|
140
|
+
let proxy: T = objToProxy.get(obj);
|
|
141
|
+
|
|
142
|
+
if (!proxy) {
|
|
143
|
+
proxy = new Proxy(obj, {
|
|
144
|
+
get(target, prop, receiver) {
|
|
145
|
+
const propPath = [...path, String(prop)];
|
|
146
|
+
const value = Reflect.get(target, prop, receiver);
|
|
147
|
+
|
|
148
|
+
if (isBelievedBad || (!(prop in target) && value === undefined)) {
|
|
149
|
+
// If we're accessing a path that doesn't exist, it will probably eventually error.
|
|
150
|
+
// Let's proxy it and mark it bad so that we can control the error message.
|
|
151
|
+
// We proxy an empty class so that an invocation or construction attempt is possible.
|
|
152
|
+
return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
|
|
156
|
+
return makeSdkProxy(value, { path: propPath, isBelievedBad });
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return value;
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
apply(target, thisArg, args) {
|
|
163
|
+
if (isBelievedBad || typeof target !== 'function') {
|
|
164
|
+
const fullyQualifiedMethodName = path.join('.');
|
|
165
|
+
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
|
|
166
|
+
throw new Error(
|
|
167
|
+
`${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`,
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
construct(target, args, newTarget) {
|
|
175
|
+
if (isBelievedBad || typeof target !== 'function') {
|
|
176
|
+
const fullyQualifiedMethodName = path.join('.');
|
|
177
|
+
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
|
|
178
|
+
throw new Error(
|
|
179
|
+
`${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`,
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return Reflect.construct(target, args, newTarget);
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
objToProxy.set(obj, proxy);
|
|
188
|
+
proxyToObj.set(proxy, obj);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return proxy;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function parseError(code: string, error: unknown): string | undefined {
|
|
195
|
+
if (!(error instanceof Error)) return;
|
|
196
|
+
const message = error.name ? `${error.name}: ${error.message}` : error.message;
|
|
197
|
+
try {
|
|
198
|
+
// Deno uses V8; the first "<anonymous>:LINE:COLUMN" is the top of stack.
|
|
199
|
+
const lineNumber = error.stack?.match(/<anonymous>:([0-9]+):[0-9]+/)?.[1];
|
|
200
|
+
// -1 for the zero-based indexing
|
|
201
|
+
const line =
|
|
202
|
+
lineNumber &&
|
|
203
|
+
code
|
|
204
|
+
.split('\n')
|
|
205
|
+
.at(parseInt(lineNumber, 10) - 1)
|
|
206
|
+
?.trim();
|
|
207
|
+
return line ? `${message}\n at line ${lineNumber}\n ${line}` : message;
|
|
208
|
+
} catch {
|
|
209
|
+
return message;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const fetch = async (req: Request): Promise<Response> => {
|
|
214
|
+
const { opts, code } = (await req.json()) as { opts: ClientOptions; code: string };
|
|
215
|
+
|
|
216
|
+
const runFunctionSource = code ? getRunFunctionSource(code) : null;
|
|
217
|
+
if (!runFunctionSource) {
|
|
218
|
+
const message =
|
|
219
|
+
code ?
|
|
220
|
+
'The code is missing a top-level `run` function.'
|
|
221
|
+
: 'The code argument is missing. Provide one containing a top-level `run` function.';
|
|
222
|
+
return Response.json(
|
|
223
|
+
{
|
|
224
|
+
is_error: true,
|
|
225
|
+
result: `${message} Write code within this template:\n\n\`\`\`\nasync function run(client) {\n // Fill this out\n}\n\`\`\``,
|
|
226
|
+
log_lines: [],
|
|
227
|
+
err_lines: [],
|
|
228
|
+
} satisfies WorkerOutput,
|
|
229
|
+
{ status: 400, statusText: 'Code execution error' },
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const diagnostics = getTSDiagnostics(code);
|
|
234
|
+
if (diagnostics.length > 0) {
|
|
235
|
+
return Response.json(
|
|
236
|
+
{
|
|
237
|
+
is_error: true,
|
|
238
|
+
result: `The code contains TypeScript diagnostics:\n${diagnostics.join('\n')}`,
|
|
239
|
+
log_lines: [],
|
|
240
|
+
err_lines: [],
|
|
241
|
+
} satisfies WorkerOutput,
|
|
242
|
+
{ status: 400, statusText: 'Code execution error' },
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const client = new Revox({
|
|
247
|
+
...opts,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const log_lines: string[] = [];
|
|
251
|
+
const err_lines: string[] = [];
|
|
252
|
+
const console = {
|
|
253
|
+
log: (...args: unknown[]) => {
|
|
254
|
+
log_lines.push(util.format(...args));
|
|
255
|
+
},
|
|
256
|
+
error: (...args: unknown[]) => {
|
|
257
|
+
err_lines.push(util.format(...args));
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
try {
|
|
261
|
+
let run_ = async (client: any) => {};
|
|
262
|
+
eval(`${code}\nrun_ = run;`);
|
|
263
|
+
const result = await run_(makeSdkProxy(client, { path: ['client'] }));
|
|
264
|
+
return Response.json({
|
|
265
|
+
is_error: false,
|
|
266
|
+
result,
|
|
267
|
+
log_lines,
|
|
268
|
+
err_lines,
|
|
269
|
+
} satisfies WorkerOutput);
|
|
270
|
+
} catch (e) {
|
|
271
|
+
return Response.json(
|
|
272
|
+
{
|
|
273
|
+
is_error: true,
|
|
274
|
+
result: parseError(code, e),
|
|
275
|
+
log_lines,
|
|
276
|
+
err_lines,
|
|
277
|
+
} satisfies WorkerOutput,
|
|
278
|
+
{ status: 400, statusText: 'Code execution error' },
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
export default { fetch };
|