@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
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { readEnv } from './util';
|
|
4
|
+
import { getLogger } from './logger';
|
|
5
|
+
|
|
6
|
+
const INSTRUCTIONS_CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes
|
|
7
|
+
|
|
8
|
+
interface InstructionsCacheEntry {
|
|
9
|
+
fetchedInstructions: string;
|
|
10
|
+
fetchedAt: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const instructionsCache = new Map<string, InstructionsCacheEntry>();
|
|
14
|
+
|
|
15
|
+
// Periodically evict stale entries so the cache doesn't grow unboundedly.
|
|
16
|
+
const _cacheCleanupInterval = setInterval(() => {
|
|
17
|
+
const now = Date.now();
|
|
18
|
+
for (const [key, entry] of instructionsCache) {
|
|
19
|
+
if (now - entry.fetchedAt > INSTRUCTIONS_CACHE_TTL_MS) {
|
|
20
|
+
instructionsCache.delete(key);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}, INSTRUCTIONS_CACHE_TTL_MS);
|
|
24
|
+
|
|
25
|
+
// Don't keep the process alive just for cleanup.
|
|
26
|
+
_cacheCleanupInterval.unref();
|
|
27
|
+
|
|
28
|
+
export async function getInstructions(stainlessApiKey: string | undefined): Promise<string> {
|
|
29
|
+
const cacheKey = stainlessApiKey ?? '';
|
|
30
|
+
const cached = instructionsCache.get(cacheKey);
|
|
31
|
+
|
|
32
|
+
if (cached && Date.now() - cached.fetchedAt <= INSTRUCTIONS_CACHE_TTL_MS) {
|
|
33
|
+
return cached.fetchedInstructions;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const fetchedInstructions = await fetchLatestInstructions(stainlessApiKey);
|
|
37
|
+
instructionsCache.set(cacheKey, { fetchedInstructions, fetchedAt: Date.now() });
|
|
38
|
+
return fetchedInstructions;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function fetchLatestInstructions(stainlessApiKey: string | undefined): Promise<string> {
|
|
42
|
+
// Setting the stainless API key is optional, but may be required
|
|
43
|
+
// to authenticate requests to the Stainless API.
|
|
44
|
+
const response = await fetch(
|
|
45
|
+
readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/revox',
|
|
46
|
+
{
|
|
47
|
+
method: 'GET',
|
|
48
|
+
headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) },
|
|
49
|
+
},
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
let instructions: string | undefined;
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
getLogger().warn(
|
|
55
|
+
'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
instructions =
|
|
59
|
+
'\n This is the revox MCP server.\n\n Available tools:\n - search_docs: Search SDK documentation to find the right methods and parameters.\n - execute: Run TypeScript code against a pre-authenticated SDK client. Define an async run(client) function.\n\n Workflow:\n - If unsure about the API, call search_docs first.\n - Write complete solutions in a single execute call when possible. For large datasets, use API filters to narrow results or paginate within a single execute block.\n - If execute returns an error, read the error and fix your code rather than retrying the same approach.\n - Variables do not persist between execute calls. Return or log all data you need.\n - Individual HTTP requests to the API have a 30-second timeout. If a request times out, try a smaller query or add filters.\n - Code execution has a total timeout of approximately 5 minutes. If your code times out, simplify it or break it into smaller steps.\n ';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
instructions ??= ((await response.json()) as { instructions: string }).instructions;
|
|
63
|
+
|
|
64
|
+
return instructions;
|
|
65
|
+
}
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { pino, type Level, type Logger } from 'pino';
|
|
4
|
+
import pretty from 'pino-pretty';
|
|
5
|
+
|
|
6
|
+
let _logger: Logger | undefined;
|
|
7
|
+
|
|
8
|
+
export function configureLogger({ level, pretty: usePretty }: { level: Level; pretty: boolean }): void {
|
|
9
|
+
_logger = pino(
|
|
10
|
+
{
|
|
11
|
+
level,
|
|
12
|
+
timestamp: pino.stdTimeFunctions.isoTime,
|
|
13
|
+
formatters: {
|
|
14
|
+
level(label) {
|
|
15
|
+
return { level: label };
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
usePretty ? pretty({ colorize: true, levelFirst: true, destination: 2 }) : process.stderr,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function getLogger(): Logger {
|
|
24
|
+
if (!_logger) {
|
|
25
|
+
throw new Error('Logger has not been configured. Call configureLogger() before using the logger.');
|
|
26
|
+
}
|
|
27
|
+
return _logger;
|
|
28
|
+
}
|
package/src/methods.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { McpOptions } from './options';
|
|
4
|
+
|
|
5
|
+
export type SdkMethod = {
|
|
6
|
+
clientCallName: string;
|
|
7
|
+
fullyQualifiedName: string;
|
|
8
|
+
httpMethod?: 'get' | 'post' | 'put' | 'patch' | 'delete' | 'query';
|
|
9
|
+
httpPath?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const sdkMethods: SdkMethod[] = [
|
|
13
|
+
{
|
|
14
|
+
clientCallName: 'client.assistants.create',
|
|
15
|
+
fullyQualifiedName: 'assistants.create',
|
|
16
|
+
httpMethod: 'post',
|
|
17
|
+
httpPath: '/assistants',
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
clientCallName: 'client.assistants.retrieve',
|
|
21
|
+
fullyQualifiedName: 'assistants.retrieve',
|
|
22
|
+
httpMethod: 'get',
|
|
23
|
+
httpPath: '/assistants/{id}',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
clientCallName: 'client.assistants.update',
|
|
27
|
+
fullyQualifiedName: 'assistants.update',
|
|
28
|
+
httpMethod: 'patch',
|
|
29
|
+
httpPath: '/assistants/{id}',
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
clientCallName: 'client.assistants.list',
|
|
33
|
+
fullyQualifiedName: 'assistants.list',
|
|
34
|
+
httpMethod: 'get',
|
|
35
|
+
httpPath: '/assistants',
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
clientCallName: 'client.assistants.delete',
|
|
39
|
+
fullyQualifiedName: 'assistants.delete',
|
|
40
|
+
httpMethod: 'delete',
|
|
41
|
+
httpPath: '/assistants/{id}',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
clientCallName: 'client.call.create',
|
|
45
|
+
fullyQualifiedName: 'call.create',
|
|
46
|
+
httpMethod: 'post',
|
|
47
|
+
httpPath: '/call',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
clientCallName: 'client.call.retrieve',
|
|
51
|
+
fullyQualifiedName: 'call.retrieve',
|
|
52
|
+
httpMethod: 'get',
|
|
53
|
+
httpPath: '/call/{id}',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
clientCallName: 'client.call.list',
|
|
57
|
+
fullyQualifiedName: 'call.list',
|
|
58
|
+
httpMethod: 'get',
|
|
59
|
+
httpPath: '/call',
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
clientCallName: 'client.voices.retrieve',
|
|
63
|
+
fullyQualifiedName: 'voices.retrieve',
|
|
64
|
+
httpMethod: 'get',
|
|
65
|
+
httpPath: '/voices/{id}',
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
clientCallName: 'client.voices.list',
|
|
69
|
+
fullyQualifiedName: 'voices.list',
|
|
70
|
+
httpMethod: 'get',
|
|
71
|
+
httpPath: '/voices',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
clientCallName: 'client.voices.preview',
|
|
75
|
+
fullyQualifiedName: 'voices.preview',
|
|
76
|
+
httpMethod: 'post',
|
|
77
|
+
httpPath: '/voices/preview',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
clientCallName: 'client.users.me.retrieve',
|
|
81
|
+
fullyQualifiedName: 'users.me.retrieve',
|
|
82
|
+
httpMethod: 'get',
|
|
83
|
+
httpPath: '/users/me',
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
clientCallName: 'client.users.me.update',
|
|
87
|
+
fullyQualifiedName: 'users.me.update',
|
|
88
|
+
httpMethod: 'patch',
|
|
89
|
+
httpPath: '/users/me',
|
|
90
|
+
},
|
|
91
|
+
];
|
|
92
|
+
|
|
93
|
+
function allowedMethodsForCodeTool(options: McpOptions | undefined): SdkMethod[] | undefined {
|
|
94
|
+
if (!options) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
let allowedMethods: SdkMethod[];
|
|
99
|
+
|
|
100
|
+
if (options.codeAllowHttpGets || options.codeAllowedMethods) {
|
|
101
|
+
// Start with nothing allowed and then add into it from options
|
|
102
|
+
let allowedMethodsSet = new Set<SdkMethod>();
|
|
103
|
+
|
|
104
|
+
if (options.codeAllowHttpGets) {
|
|
105
|
+
// Add all methods that map to an HTTP GET
|
|
106
|
+
sdkMethods
|
|
107
|
+
.filter((method) => method.httpMethod === 'get')
|
|
108
|
+
.forEach((method) => allowedMethodsSet.add(method));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (options.codeAllowedMethods) {
|
|
112
|
+
// Add all methods that match any of the allowed regexps
|
|
113
|
+
const allowedRegexps = options.codeAllowedMethods.map((pattern) => {
|
|
114
|
+
try {
|
|
115
|
+
return new RegExp(pattern);
|
|
116
|
+
} catch (e) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`Invalid regex pattern for allowed method: "${pattern}": ${e instanceof Error ? e.message : e}`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
sdkMethods
|
|
124
|
+
.filter((method) => allowedRegexps.some((regexp) => regexp.test(method.fullyQualifiedName)))
|
|
125
|
+
.forEach((method) => allowedMethodsSet.add(method));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
allowedMethods = Array.from(allowedMethodsSet);
|
|
129
|
+
} else {
|
|
130
|
+
// Start with everything allowed
|
|
131
|
+
allowedMethods = [...sdkMethods];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (options.codeBlockedMethods) {
|
|
135
|
+
// Filter down based on blocked regexps
|
|
136
|
+
const blockedRegexps = options.codeBlockedMethods.map((pattern) => {
|
|
137
|
+
try {
|
|
138
|
+
return new RegExp(pattern);
|
|
139
|
+
} catch (e) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`Invalid regex pattern for blocked method: "${pattern}": ${e instanceof Error ? e.message : e}`,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
allowedMethods = allowedMethods.filter(
|
|
147
|
+
(method) => !blockedRegexps.some((regexp) => regexp.test(method.fullyQualifiedName)),
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return allowedMethods;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function blockedMethodsForCodeTool(options: McpOptions | undefined): SdkMethod[] | undefined {
|
|
155
|
+
const allowedMethods = allowedMethodsForCodeTool(options);
|
|
156
|
+
if (!allowedMethods) {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const allowedSet = new Set(allowedMethods.map((method) => method.fullyQualifiedName));
|
|
161
|
+
|
|
162
|
+
// Return any methods that are not explicitly allowed
|
|
163
|
+
return sdkMethods.filter((method) => !allowedSet.has(method.fullyQualifiedName));
|
|
164
|
+
}
|
package/src/options.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import qs from 'qs';
|
|
4
|
+
import yargs from 'yargs';
|
|
5
|
+
import { hideBin } from 'yargs/helpers';
|
|
6
|
+
import z from 'zod';
|
|
7
|
+
import { readEnv } from './util';
|
|
8
|
+
|
|
9
|
+
export type CLIOptions = McpOptions & {
|
|
10
|
+
debug: boolean;
|
|
11
|
+
logFormat: 'json' | 'pretty';
|
|
12
|
+
transport: 'stdio' | 'http';
|
|
13
|
+
port: number | undefined;
|
|
14
|
+
socket: string | undefined;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type McpOptions = {
|
|
18
|
+
includeCodeTool?: boolean | undefined;
|
|
19
|
+
includeDocsTools?: boolean | undefined;
|
|
20
|
+
stainlessApiKey?: string | undefined;
|
|
21
|
+
codeAllowHttpGets?: boolean | undefined;
|
|
22
|
+
codeAllowedMethods?: string[] | undefined;
|
|
23
|
+
codeBlockedMethods?: string[] | undefined;
|
|
24
|
+
codeExecutionMode: McpCodeExecutionMode;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type McpCodeExecutionMode = 'stainless-sandbox' | 'local';
|
|
28
|
+
|
|
29
|
+
export function parseCLIOptions(): CLIOptions {
|
|
30
|
+
const opts = yargs(hideBin(process.argv))
|
|
31
|
+
.option('code-allow-http-gets', {
|
|
32
|
+
type: 'boolean',
|
|
33
|
+
description:
|
|
34
|
+
'Allow all code tool methods that map to HTTP GET operations. If all code-allow-* flags are unset, then everything is allowed.',
|
|
35
|
+
})
|
|
36
|
+
.option('code-allowed-methods', {
|
|
37
|
+
type: 'string',
|
|
38
|
+
array: true,
|
|
39
|
+
description:
|
|
40
|
+
'Methods to explicitly allow for code tool. Evaluated as regular expressions against method fully qualified names. If all code-allow-* flags are unset, then everything is allowed.',
|
|
41
|
+
})
|
|
42
|
+
.option('code-blocked-methods', {
|
|
43
|
+
type: 'string',
|
|
44
|
+
array: true,
|
|
45
|
+
description:
|
|
46
|
+
'Methods to explicitly block for code tool. Evaluated as regular expressions against method fully qualified names. If all code-allow-* flags are unset, then everything is allowed.',
|
|
47
|
+
})
|
|
48
|
+
.option('code-execution-mode', {
|
|
49
|
+
type: 'string',
|
|
50
|
+
choices: ['stainless-sandbox', 'local'],
|
|
51
|
+
default: 'stainless-sandbox',
|
|
52
|
+
description:
|
|
53
|
+
"Where to run code execution in code tool; 'stainless-sandbox' will execute code in Stainless-hosted sandboxes whereas 'local' will execute code locally on the MCP server machine.",
|
|
54
|
+
})
|
|
55
|
+
.option('debug', { type: 'boolean', description: 'Enable debug logging' })
|
|
56
|
+
.option('log-format', {
|
|
57
|
+
type: 'string',
|
|
58
|
+
choices: ['json', 'pretty'],
|
|
59
|
+
description: 'Format for log output; defaults to json unless tty is detected',
|
|
60
|
+
})
|
|
61
|
+
.option('no-tools', {
|
|
62
|
+
type: 'string',
|
|
63
|
+
array: true,
|
|
64
|
+
choices: ['code', 'docs'],
|
|
65
|
+
description: 'Tools to explicitly disable',
|
|
66
|
+
})
|
|
67
|
+
.option('port', {
|
|
68
|
+
type: 'number',
|
|
69
|
+
default: 3000,
|
|
70
|
+
description: 'Port to serve on if using http transport',
|
|
71
|
+
})
|
|
72
|
+
.option('socket', { type: 'string', description: 'Unix socket to serve on if using http transport' })
|
|
73
|
+
.option('stainless-api-key', {
|
|
74
|
+
type: 'string',
|
|
75
|
+
default: readEnv('STAINLESS_API_KEY'),
|
|
76
|
+
description:
|
|
77
|
+
'API key for Stainless. Used to authenticate requests to Stainless-hosted tools endpoints.',
|
|
78
|
+
})
|
|
79
|
+
.option('tools', {
|
|
80
|
+
type: 'string',
|
|
81
|
+
array: true,
|
|
82
|
+
choices: ['code', 'docs'],
|
|
83
|
+
description: 'Tools to explicitly enable',
|
|
84
|
+
})
|
|
85
|
+
.option('transport', {
|
|
86
|
+
type: 'string',
|
|
87
|
+
choices: ['stdio', 'http'],
|
|
88
|
+
default: 'stdio',
|
|
89
|
+
description: 'What transport to use; stdio for local servers or http for remote servers',
|
|
90
|
+
})
|
|
91
|
+
.env('MCP_SERVER')
|
|
92
|
+
.version(true)
|
|
93
|
+
.help();
|
|
94
|
+
|
|
95
|
+
const argv = opts.parseSync();
|
|
96
|
+
|
|
97
|
+
const shouldIncludeToolType = (toolType: 'code' | 'docs') =>
|
|
98
|
+
argv.noTools?.includes(toolType) ? false
|
|
99
|
+
: argv.tools?.includes(toolType) ? true
|
|
100
|
+
: undefined;
|
|
101
|
+
|
|
102
|
+
const includeCodeTool = shouldIncludeToolType('code');
|
|
103
|
+
const includeDocsTools = shouldIncludeToolType('docs');
|
|
104
|
+
|
|
105
|
+
const transport = argv.transport as 'stdio' | 'http';
|
|
106
|
+
const logFormat =
|
|
107
|
+
argv.logFormat ? (argv.logFormat as 'json' | 'pretty')
|
|
108
|
+
: process.stderr.isTTY ? 'pretty'
|
|
109
|
+
: 'json';
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
...(includeCodeTool !== undefined && { includeCodeTool }),
|
|
113
|
+
...(includeDocsTools !== undefined && { includeDocsTools }),
|
|
114
|
+
debug: !!argv.debug,
|
|
115
|
+
stainlessApiKey: argv.stainlessApiKey,
|
|
116
|
+
codeAllowHttpGets: argv.codeAllowHttpGets,
|
|
117
|
+
codeAllowedMethods: argv.codeAllowedMethods,
|
|
118
|
+
codeBlockedMethods: argv.codeBlockedMethods,
|
|
119
|
+
codeExecutionMode: argv.codeExecutionMode as McpCodeExecutionMode,
|
|
120
|
+
transport,
|
|
121
|
+
logFormat,
|
|
122
|
+
port: argv.port,
|
|
123
|
+
socket: argv.socket,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const coerceArray = <T extends z.ZodTypeAny>(zodType: T) =>
|
|
128
|
+
z.preprocess(
|
|
129
|
+
(val) =>
|
|
130
|
+
Array.isArray(val) ? val
|
|
131
|
+
: val ? [val]
|
|
132
|
+
: val,
|
|
133
|
+
z.array(zodType).optional(),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
const QueryOptions = z.object({
|
|
137
|
+
tools: coerceArray(z.enum(['code', 'docs'])).describe('Specify which MCP tools to use'),
|
|
138
|
+
no_tools: coerceArray(z.enum(['code', 'docs'])).describe('Specify which MCP tools to not use.'),
|
|
139
|
+
tool: coerceArray(z.string()).describe('Include tools matching the specified names'),
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
export function parseQueryOptions(defaultOptions: McpOptions, query: unknown): McpOptions {
|
|
143
|
+
const queryObject = typeof query === 'string' ? qs.parse(query) : query;
|
|
144
|
+
const queryOptions = QueryOptions.parse(queryObject);
|
|
145
|
+
|
|
146
|
+
let codeTool: boolean | undefined =
|
|
147
|
+
queryOptions.no_tools && queryOptions.no_tools?.includes('code') ? false
|
|
148
|
+
: queryOptions.tools?.includes('code') ? true
|
|
149
|
+
: defaultOptions.includeCodeTool;
|
|
150
|
+
|
|
151
|
+
let docsTools: boolean | undefined =
|
|
152
|
+
queryOptions.no_tools && queryOptions.no_tools?.includes('docs') ? false
|
|
153
|
+
: queryOptions.tools?.includes('docs') ? true
|
|
154
|
+
: defaultOptions.includeDocsTools;
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
...(codeTool !== undefined && { includeCodeTool: codeTool }),
|
|
158
|
+
...(docsTools !== undefined && { includeDocsTools: docsTools }),
|
|
159
|
+
codeExecutionMode: defaultOptions.codeExecutionMode,
|
|
160
|
+
};
|
|
161
|
+
}
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
4
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
5
|
+
import {
|
|
6
|
+
CallToolRequestSchema,
|
|
7
|
+
ListToolsRequestSchema,
|
|
8
|
+
SetLevelRequestSchema,
|
|
9
|
+
} from '@modelcontextprotocol/sdk/types.js';
|
|
10
|
+
import { ClientOptions } from '@revoxai/sdk';
|
|
11
|
+
import Revox from '@revoxai/sdk';
|
|
12
|
+
import { codeTool } from './code-tool';
|
|
13
|
+
import docsSearchTool from './docs-search-tool';
|
|
14
|
+
import { getInstructions } from './instructions';
|
|
15
|
+
import { McpOptions } from './options';
|
|
16
|
+
import { blockedMethodsForCodeTool } from './methods';
|
|
17
|
+
import { HandlerFunction, McpRequestContext, ToolCallResult, McpTool } from './types';
|
|
18
|
+
|
|
19
|
+
export const newMcpServer = async (stainlessApiKey: string | undefined) =>
|
|
20
|
+
new McpServer(
|
|
21
|
+
{
|
|
22
|
+
name: 'revoxai_sdk_api',
|
|
23
|
+
version: '0.1.0',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
instructions: await getInstructions(stainlessApiKey),
|
|
27
|
+
capabilities: { tools: {}, logging: {} },
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Initializes the provided MCP Server with the given tools and handlers.
|
|
33
|
+
* If not provided, the default client, tools and handlers will be used.
|
|
34
|
+
*/
|
|
35
|
+
export async function initMcpServer(params: {
|
|
36
|
+
server: Server | McpServer;
|
|
37
|
+
clientOptions?: ClientOptions;
|
|
38
|
+
mcpOptions?: McpOptions;
|
|
39
|
+
stainlessApiKey?: string | undefined;
|
|
40
|
+
}) {
|
|
41
|
+
const server = params.server instanceof McpServer ? params.server.server : params.server;
|
|
42
|
+
|
|
43
|
+
const logAtLevel =
|
|
44
|
+
(level: 'debug' | 'info' | 'warning' | 'error') =>
|
|
45
|
+
(message: string, ...rest: unknown[]) => {
|
|
46
|
+
void server.sendLoggingMessage({
|
|
47
|
+
level,
|
|
48
|
+
data: { message, rest },
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
const logger = {
|
|
52
|
+
debug: logAtLevel('debug'),
|
|
53
|
+
info: logAtLevel('info'),
|
|
54
|
+
warn: logAtLevel('warning'),
|
|
55
|
+
error: logAtLevel('error'),
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
let _client: Revox | undefined;
|
|
59
|
+
let _clientError: Error | undefined;
|
|
60
|
+
let _logLevel: 'debug' | 'info' | 'warn' | 'error' | 'off' | undefined;
|
|
61
|
+
|
|
62
|
+
const getClient = (): Revox => {
|
|
63
|
+
if (_clientError) throw _clientError;
|
|
64
|
+
if (!_client) {
|
|
65
|
+
try {
|
|
66
|
+
_client = new Revox({
|
|
67
|
+
logger,
|
|
68
|
+
...params.clientOptions,
|
|
69
|
+
defaultHeaders: {
|
|
70
|
+
...params.clientOptions?.defaultHeaders,
|
|
71
|
+
'X-Stainless-MCP': 'true',
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
if (_logLevel) {
|
|
75
|
+
_client = _client.withOptions({ logLevel: _logLevel });
|
|
76
|
+
}
|
|
77
|
+
} catch (e) {
|
|
78
|
+
_clientError = e instanceof Error ? e : new Error(String(e));
|
|
79
|
+
throw _clientError;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return _client;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const providedTools = selectTools(params.mcpOptions);
|
|
86
|
+
const toolMap = Object.fromEntries(providedTools.map((mcpTool) => [mcpTool.tool.name, mcpTool]));
|
|
87
|
+
|
|
88
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
89
|
+
return {
|
|
90
|
+
tools: providedTools.map((mcpTool) => mcpTool.tool),
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
95
|
+
const { name, arguments: args } = request.params;
|
|
96
|
+
const mcpTool = toolMap[name];
|
|
97
|
+
if (!mcpTool) {
|
|
98
|
+
throw new Error(`Unknown tool: ${name}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let client: Revox;
|
|
102
|
+
try {
|
|
103
|
+
client = getClient();
|
|
104
|
+
} catch (error) {
|
|
105
|
+
return {
|
|
106
|
+
content: [
|
|
107
|
+
{
|
|
108
|
+
type: 'text' as const,
|
|
109
|
+
text: `Failed to initialize client: ${error instanceof Error ? error.message : String(error)}`,
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
isError: true,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return executeHandler({
|
|
117
|
+
handler: mcpTool.handler,
|
|
118
|
+
reqContext: {
|
|
119
|
+
client,
|
|
120
|
+
stainlessApiKey: params.stainlessApiKey ?? params.mcpOptions?.stainlessApiKey,
|
|
121
|
+
},
|
|
122
|
+
args,
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
server.setRequestHandler(SetLevelRequestSchema, async (request) => {
|
|
127
|
+
const { level } = request.params;
|
|
128
|
+
let logLevel: 'debug' | 'info' | 'warn' | 'error' | 'off';
|
|
129
|
+
switch (level) {
|
|
130
|
+
case 'debug':
|
|
131
|
+
logLevel = 'debug';
|
|
132
|
+
break;
|
|
133
|
+
case 'info':
|
|
134
|
+
logLevel = 'info';
|
|
135
|
+
break;
|
|
136
|
+
case 'notice':
|
|
137
|
+
case 'warning':
|
|
138
|
+
logLevel = 'warn';
|
|
139
|
+
break;
|
|
140
|
+
case 'error':
|
|
141
|
+
logLevel = 'error';
|
|
142
|
+
break;
|
|
143
|
+
default:
|
|
144
|
+
logLevel = 'off';
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
_logLevel = logLevel;
|
|
148
|
+
if (_client) {
|
|
149
|
+
_client = _client.withOptions({ logLevel });
|
|
150
|
+
}
|
|
151
|
+
return {};
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Selects the tools to include in the MCP Server based on the provided options.
|
|
157
|
+
*/
|
|
158
|
+
export function selectTools(options?: McpOptions): McpTool[] {
|
|
159
|
+
const includedTools = [];
|
|
160
|
+
|
|
161
|
+
if (options?.includeCodeTool ?? true) {
|
|
162
|
+
includedTools.push(
|
|
163
|
+
codeTool({
|
|
164
|
+
blockedMethods: blockedMethodsForCodeTool(options),
|
|
165
|
+
codeExecutionMode: options?.codeExecutionMode ?? 'stainless-sandbox',
|
|
166
|
+
}),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
if (options?.includeDocsTools ?? true) {
|
|
170
|
+
includedTools.push(docsSearchTool);
|
|
171
|
+
}
|
|
172
|
+
return includedTools;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Runs the provided handler with the given client and arguments.
|
|
177
|
+
*/
|
|
178
|
+
export async function executeHandler({
|
|
179
|
+
handler,
|
|
180
|
+
reqContext,
|
|
181
|
+
args,
|
|
182
|
+
}: {
|
|
183
|
+
handler: HandlerFunction;
|
|
184
|
+
reqContext: McpRequestContext;
|
|
185
|
+
args: Record<string, unknown> | undefined;
|
|
186
|
+
}): Promise<ToolCallResult> {
|
|
187
|
+
return await handler({ reqContext, args: args || {} });
|
|
188
|
+
}
|
package/src/stdio.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
2
|
+
import { McpOptions } from './options';
|
|
3
|
+
import { initMcpServer, newMcpServer } from './server';
|
|
4
|
+
import { getLogger } from './logger';
|
|
5
|
+
|
|
6
|
+
export const launchStdioServer = async (mcpOptions: McpOptions) => {
|
|
7
|
+
const server = await newMcpServer(mcpOptions.stainlessApiKey);
|
|
8
|
+
|
|
9
|
+
await initMcpServer({ server, mcpOptions, stainlessApiKey: mcpOptions.stainlessApiKey });
|
|
10
|
+
|
|
11
|
+
const transport = new StdioServerTransport();
|
|
12
|
+
await server.connect(transport);
|
|
13
|
+
getLogger().info('MCP Server running on stdio');
|
|
14
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
// this config is included in the published src directory to prevent TS errors
|
|
3
|
+
// from appearing when users go to source, and VSCode opens the source .ts file
|
|
4
|
+
// via declaration maps
|
|
5
|
+
"include": ["index.ts"],
|
|
6
|
+
"compilerOptions": {
|
|
7
|
+
"target": "es2015",
|
|
8
|
+
"lib": ["DOM"],
|
|
9
|
+
"moduleResolution": "node"
|
|
10
|
+
}
|
|
11
|
+
}
|