@imagekit/api-mcp 7.2.2 → 7.4.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/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/{headers.mjs → auth.js} +21 -2
- package/auth.js.map +1 -0
- package/{headers.js → auth.mjs} +16 -6
- package/auth.mjs.map +1 -0
- package/code-tool-paths.cjs +8 -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.map +1 -1
- package/code-tool-types.d.ts.map +1 -1
- 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 +314 -0
- package/code-tool-worker.js.map +1 -0
- package/code-tool-worker.mjs +276 -0
- package/code-tool-worker.mjs.map +1 -0
- package/code-tool.d.mts +11 -2
- package/code-tool.d.mts.map +1 -1
- package/code-tool.d.ts +11 -2
- package/code-tool.d.ts.map +1 -1
- package/code-tool.js +285 -42
- package/code-tool.js.map +1 -1
- package/code-tool.mjs +253 -43
- package/code-tool.mjs.map +1 -1
- package/docs-search-tool.d.mts +11 -3
- package/docs-search-tool.d.mts.map +1 -1
- package/docs-search-tool.d.ts +11 -3
- package/docs-search-tool.d.ts.map +1 -1
- package/docs-search-tool.js +58 -5
- package/docs-search-tool.js.map +1 -1
- package/docs-search-tool.mjs +57 -5
- package/docs-search-tool.mjs.map +1 -1
- package/http.d.mts +6 -3
- package/http.d.mts.map +1 -1
- package/http.d.ts +6 -3
- package/http.d.ts.map +1 -1
- package/http.js +130 -28
- package/http.js.map +1 -1
- package/http.mjs +130 -28
- package/http.mjs.map +1 -1
- package/index.js +17 -12
- package/index.js.map +1 -1
- package/index.mjs +17 -12
- package/index.mjs.map +1 -1
- package/instructions.d.mts +5 -0
- package/instructions.d.mts.map +1 -0
- package/instructions.d.ts +5 -0
- package/instructions.d.ts.map +1 -0
- package/instructions.js +61 -0
- package/instructions.js.map +1 -0
- package/instructions.mjs +55 -0
- package/instructions.mjs.map +1 -0
- package/local-docs-search.d.mts +28 -0
- package/local-docs-search.d.mts.map +1 -0
- package/local-docs-search.d.ts +28 -0
- package/local-docs-search.d.ts.map +1 -0
- package/local-docs-search.js +2807 -0
- package/local-docs-search.js.map +1 -0
- package/local-docs-search.mjs +2767 -0
- package/local-docs-search.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 +349 -0
- package/methods.js.map +1 -0
- package/methods.mjs +345 -0
- package/methods.mjs.map +1 -0
- package/options.d.mts +12 -0
- package/options.d.mts.map +1 -1
- package/options.d.ts +12 -0
- package/options.d.ts.map +1 -1
- package/options.js +81 -12
- package/options.js.map +1 -1
- package/options.mjs +81 -12
- package/options.mjs.map +1 -1
- package/package.json +89 -15
- package/server.d.mts +18 -11
- package/server.d.mts.map +1 -1
- package/server.d.ts +18 -11
- package/server.d.ts.map +1 -1
- package/server.js +89 -49
- package/server.js.map +1 -1
- package/server.mjs +88 -45
- package/server.mjs.map +1 -1
- package/src/{headers.ts → auth.ts} +18 -1
- package/src/code-tool-paths.cts +5 -0
- package/src/code-tool-types.ts +1 -0
- package/src/code-tool-worker.ts +327 -0
- package/src/code-tool.ts +339 -55
- package/src/docs-search-tool.ts +86 -7
- package/src/http.ts +154 -33
- package/src/index.ts +19 -13
- package/src/instructions.ts +83 -0
- package/src/local-docs-search.ts +3380 -0
- package/src/logger.ts +28 -0
- package/src/methods.ts +370 -0
- package/src/options.ts +105 -12
- package/src/server.ts +111 -57
- package/src/stdio.ts +9 -4
- package/src/types.ts +15 -4
- package/src/util.ts +25 -0
- package/stdio.d.mts +2 -1
- package/stdio.d.mts.map +1 -1
- package/stdio.d.ts +2 -1
- package/stdio.d.ts.map +1 -1
- package/stdio.js +8 -4
- package/stdio.js.map +1 -1
- package/stdio.mjs +8 -4
- package/stdio.mjs.map +1 -1
- package/types.d.mts +14 -1
- package/types.d.mts.map +1 -1
- package/types.d.ts +14 -1
- package/types.d.ts.map +1 -1
- package/types.js.map +1 -1
- package/types.mjs.map +1 -1
- 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/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.map +0 -1
- package/headers.mjs.map +0 -1
package/src/http.ts
CHANGED
|
@@ -2,41 +2,93 @@
|
|
|
2
2
|
|
|
3
3
|
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp';
|
|
4
4
|
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
|
5
|
-
|
|
5
|
+
import { ClientOptions } from '@imagekit/nodejs';
|
|
6
6
|
import express from 'express';
|
|
7
|
+
import pino from 'pino';
|
|
8
|
+
import pinoHttp from 'pino-http';
|
|
9
|
+
import { getStainlessApiKey, parseClientAuthHeaders } from './auth';
|
|
10
|
+
import { getLogger } from './logger';
|
|
7
11
|
import { McpOptions } from './options';
|
|
8
|
-
import {
|
|
9
|
-
import { parseAuthHeaders } from './headers';
|
|
12
|
+
import { initMcpServer, newMcpServer } from './server';
|
|
10
13
|
|
|
11
|
-
const newServer = ({
|
|
14
|
+
const newServer = async ({
|
|
12
15
|
clientOptions,
|
|
16
|
+
mcpOptions,
|
|
13
17
|
req,
|
|
14
18
|
res,
|
|
15
19
|
}: {
|
|
16
20
|
clientOptions: ClientOptions;
|
|
21
|
+
mcpOptions: McpOptions;
|
|
17
22
|
req: express.Request;
|
|
18
23
|
res: express.Response;
|
|
19
|
-
}): McpServer | null => {
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
24
|
+
}): Promise<McpServer | null> => {
|
|
25
|
+
const stainlessApiKey = getStainlessApiKey(req, mcpOptions);
|
|
26
|
+
const customInstructionsPath = mcpOptions.customInstructionsPath;
|
|
27
|
+
const server = await newMcpServer({ stainlessApiKey, customInstructionsPath });
|
|
28
|
+
|
|
29
|
+
const authOptions = parseClientAuthHeaders(req, false);
|
|
30
|
+
|
|
31
|
+
let upstreamClientEnvs: Record<string, string> | undefined;
|
|
32
|
+
const clientEnvsHeader = req.headers['x-stainless-mcp-client-envs'];
|
|
33
|
+
if (typeof clientEnvsHeader === 'string') {
|
|
34
|
+
try {
|
|
35
|
+
const parsed = JSON.parse(clientEnvsHeader);
|
|
36
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
37
|
+
upstreamClientEnvs = parsed;
|
|
38
|
+
}
|
|
39
|
+
} catch {
|
|
40
|
+
// Ignore malformed header
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Parse x-stainless-mcp-client-permissions header to override permission options
|
|
45
|
+
//
|
|
46
|
+
// Note: Permissions are best-effort and intended to prevent clients from doing unexpected things;
|
|
47
|
+
// they're not a hard security boundary, so we allow arbitrary, client-driven overrides.
|
|
48
|
+
//
|
|
49
|
+
// See the Stainless MCP documentation for more details.
|
|
50
|
+
let effectiveMcpOptions = mcpOptions;
|
|
51
|
+
const clientPermissionsHeader = req.headers['x-stainless-mcp-client-permissions'];
|
|
52
|
+
if (typeof clientPermissionsHeader === 'string') {
|
|
53
|
+
try {
|
|
54
|
+
const parsed = JSON.parse(clientPermissionsHeader);
|
|
55
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
56
|
+
effectiveMcpOptions = {
|
|
57
|
+
...mcpOptions,
|
|
58
|
+
...(typeof parsed.allow_http_gets === 'boolean' && { codeAllowHttpGets: parsed.allow_http_gets }),
|
|
59
|
+
...(Array.isArray(parsed.allowed_methods) && { codeAllowedMethods: parsed.allowed_methods }),
|
|
60
|
+
...(Array.isArray(parsed.blocked_methods) && { codeBlockedMethods: parsed.blocked_methods }),
|
|
61
|
+
};
|
|
62
|
+
getLogger().info(
|
|
63
|
+
{ clientPermissions: parsed },
|
|
64
|
+
'Overriding code execution permissions from x-stainless-mcp-client-permissions header',
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
} catch (error) {
|
|
68
|
+
getLogger().warn({ error }, 'Failed to parse x-stainless-mcp-client-permissions header');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const mcpClientInfo =
|
|
73
|
+
typeof req.body?.params?.clientInfo?.name === 'string' ?
|
|
74
|
+
{ name: req.body.params.clientInfo.name, version: String(req.body.params.clientInfo.version ?? '') }
|
|
75
|
+
: undefined;
|
|
76
|
+
|
|
77
|
+
await initMcpServer({
|
|
78
|
+
server: server,
|
|
79
|
+
mcpOptions: effectiveMcpOptions,
|
|
80
|
+
clientOptions: {
|
|
81
|
+
...clientOptions,
|
|
82
|
+
...authOptions,
|
|
83
|
+
},
|
|
84
|
+
stainlessApiKey: stainlessApiKey,
|
|
85
|
+
upstreamClientEnvs,
|
|
86
|
+
mcpSessionId: (req as any).mcpSessionId,
|
|
87
|
+
mcpClientInfo,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (mcpClientInfo) {
|
|
91
|
+
getLogger().info({ mcpSessionId: (req as any).mcpSessionId, mcpClientInfo }, 'MCP client connected');
|
|
40
92
|
}
|
|
41
93
|
|
|
42
94
|
return server;
|
|
@@ -45,7 +97,7 @@ const newServer = ({
|
|
|
45
97
|
const post =
|
|
46
98
|
(options: { clientOptions: ClientOptions; mcpOptions: McpOptions }) =>
|
|
47
99
|
async (req: express.Request, res: express.Response) => {
|
|
48
|
-
const server = newServer({ ...options, req, res });
|
|
100
|
+
const server = await newServer({ ...options, req, res });
|
|
49
101
|
// If we return null, we already set the authorization error.
|
|
50
102
|
if (server === null) return;
|
|
51
103
|
const transport = new StreamableHTTPServerTransport();
|
|
@@ -73,17 +125,78 @@ const del = async (req: express.Request, res: express.Response) => {
|
|
|
73
125
|
});
|
|
74
126
|
};
|
|
75
127
|
|
|
128
|
+
const redactHeaders = (headers: Record<string, any>) => {
|
|
129
|
+
const hiddenHeaders = /auth|cookie|key|token|x-stainless-mcp-client-envs/i;
|
|
130
|
+
const filtered = { ...headers };
|
|
131
|
+
Object.keys(filtered).forEach((key) => {
|
|
132
|
+
if (hiddenHeaders.test(key)) {
|
|
133
|
+
filtered[key] = '[REDACTED]';
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
return filtered;
|
|
137
|
+
};
|
|
138
|
+
|
|
76
139
|
export const streamableHTTPApp = ({
|
|
77
140
|
clientOptions = {},
|
|
78
|
-
mcpOptions
|
|
141
|
+
mcpOptions,
|
|
79
142
|
}: {
|
|
80
143
|
clientOptions?: ClientOptions;
|
|
81
|
-
mcpOptions
|
|
144
|
+
mcpOptions: McpOptions;
|
|
82
145
|
}): express.Express => {
|
|
83
146
|
const app = express();
|
|
84
147
|
app.set('query parser', 'extended');
|
|
85
148
|
app.use(express.json());
|
|
149
|
+
app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
150
|
+
const existing = req.headers['mcp-session-id'];
|
|
151
|
+
const sessionId = (Array.isArray(existing) ? existing[0] : existing) || crypto.randomUUID();
|
|
152
|
+
(req as any).mcpSessionId = sessionId;
|
|
153
|
+
const origWriteHead = res.writeHead.bind(res);
|
|
154
|
+
res.writeHead = function (statusCode: number, ...rest: any[]) {
|
|
155
|
+
res.setHeader('mcp-session-id', sessionId);
|
|
156
|
+
return origWriteHead(statusCode, ...rest);
|
|
157
|
+
} as typeof res.writeHead;
|
|
158
|
+
next();
|
|
159
|
+
});
|
|
160
|
+
app.use(
|
|
161
|
+
pinoHttp({
|
|
162
|
+
logger: getLogger(),
|
|
163
|
+
customProps: (req) => ({
|
|
164
|
+
mcpSessionId: (req as any).mcpSessionId,
|
|
165
|
+
}),
|
|
166
|
+
customLogLevel: (req, res) => {
|
|
167
|
+
if (res.statusCode >= 500) {
|
|
168
|
+
return 'error';
|
|
169
|
+
} else if (res.statusCode >= 400) {
|
|
170
|
+
return 'warn';
|
|
171
|
+
}
|
|
172
|
+
return 'info';
|
|
173
|
+
},
|
|
174
|
+
customSuccessMessage: function (req, res) {
|
|
175
|
+
return `Request ${req.method} to ${req.url} completed with status ${res.statusCode}`;
|
|
176
|
+
},
|
|
177
|
+
customErrorMessage: function (req, res, err) {
|
|
178
|
+
return `Request ${req.method} to ${req.url} errored with status ${res.statusCode}`;
|
|
179
|
+
},
|
|
180
|
+
serializers: {
|
|
181
|
+
req: pino.stdSerializers.wrapRequestSerializer((req) => {
|
|
182
|
+
return {
|
|
183
|
+
...req,
|
|
184
|
+
headers: redactHeaders(req.raw.headers),
|
|
185
|
+
};
|
|
186
|
+
}),
|
|
187
|
+
res: pino.stdSerializers.wrapResponseSerializer((res) => {
|
|
188
|
+
return {
|
|
189
|
+
...res,
|
|
190
|
+
headers: redactHeaders(res.headers),
|
|
191
|
+
};
|
|
192
|
+
}),
|
|
193
|
+
},
|
|
194
|
+
}),
|
|
195
|
+
);
|
|
86
196
|
|
|
197
|
+
app.get('/health', async (req: express.Request, res: express.Response) => {
|
|
198
|
+
res.status(200).send('OK');
|
|
199
|
+
});
|
|
87
200
|
app.get('/', get);
|
|
88
201
|
app.post('/', post({ clientOptions, mcpOptions }));
|
|
89
202
|
app.delete('/', del);
|
|
@@ -91,16 +204,24 @@ export const streamableHTTPApp = ({
|
|
|
91
204
|
return app;
|
|
92
205
|
};
|
|
93
206
|
|
|
94
|
-
export const launchStreamableHTTPServer = async (
|
|
95
|
-
|
|
207
|
+
export const launchStreamableHTTPServer = async ({
|
|
208
|
+
mcpOptions,
|
|
209
|
+
port,
|
|
210
|
+
}: {
|
|
211
|
+
mcpOptions: McpOptions;
|
|
212
|
+
port: number | string | undefined;
|
|
213
|
+
}) => {
|
|
214
|
+
const app = streamableHTTPApp({ mcpOptions });
|
|
96
215
|
const server = app.listen(port);
|
|
97
216
|
const address = server.address();
|
|
98
217
|
|
|
218
|
+
const logger = getLogger();
|
|
219
|
+
|
|
99
220
|
if (typeof address === 'string') {
|
|
100
|
-
|
|
221
|
+
logger.info(`MCP Server running on streamable HTTP at ${address}`);
|
|
101
222
|
} else if (address !== null) {
|
|
102
|
-
|
|
223
|
+
logger.info(`MCP Server running on streamable HTTP on port ${address.port}`);
|
|
103
224
|
} else {
|
|
104
|
-
|
|
225
|
+
logger.info(`MCP Server running on streamable HTTP on port ${port}`);
|
|
105
226
|
}
|
|
106
227
|
};
|
package/src/index.ts
CHANGED
|
@@ -5,30 +5,39 @@ import { McpOptions, parseCLIOptions } from './options';
|
|
|
5
5
|
import { launchStdioServer } from './stdio';
|
|
6
6
|
import { launchStreamableHTTPServer } from './http';
|
|
7
7
|
import type { McpTool } from './types';
|
|
8
|
+
import { configureLogger, getLogger } from './logger';
|
|
8
9
|
|
|
9
10
|
async function main() {
|
|
10
11
|
const options = parseOptionsOrError();
|
|
12
|
+
configureLogger({
|
|
13
|
+
level: options.debug ? 'debug' : 'info',
|
|
14
|
+
pretty: options.logFormat === 'pretty',
|
|
15
|
+
});
|
|
11
16
|
|
|
12
17
|
const selectedTools = await selectToolsOrError(options);
|
|
13
18
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
selectedTools.
|
|
19
|
+
getLogger().info(
|
|
20
|
+
{ tools: selectedTools.map((e) => e.tool.name) },
|
|
21
|
+
`MCP Server starting with ${selectedTools.length} tools`,
|
|
17
22
|
);
|
|
18
23
|
|
|
19
24
|
switch (options.transport) {
|
|
20
25
|
case 'stdio':
|
|
21
|
-
await launchStdioServer();
|
|
26
|
+
await launchStdioServer(options);
|
|
22
27
|
break;
|
|
23
28
|
case 'http':
|
|
24
|
-
await launchStreamableHTTPServer(
|
|
29
|
+
await launchStreamableHTTPServer({
|
|
30
|
+
mcpOptions: options,
|
|
31
|
+
port: options.socket ?? options.port,
|
|
32
|
+
});
|
|
25
33
|
break;
|
|
26
34
|
}
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
if (require.main === module) {
|
|
30
38
|
main().catch((error) => {
|
|
31
|
-
|
|
39
|
+
// Logger might not be initialized yet
|
|
40
|
+
console.error('Fatal error in main()', error);
|
|
32
41
|
process.exit(1);
|
|
33
42
|
});
|
|
34
43
|
}
|
|
@@ -37,7 +46,8 @@ function parseOptionsOrError() {
|
|
|
37
46
|
try {
|
|
38
47
|
return parseCLIOptions();
|
|
39
48
|
} catch (error) {
|
|
40
|
-
|
|
49
|
+
// Logger is initialized after options, so use console.error here
|
|
50
|
+
console.error('Error parsing options', error);
|
|
41
51
|
process.exit(1);
|
|
42
52
|
}
|
|
43
53
|
}
|
|
@@ -46,16 +56,12 @@ async function selectToolsOrError(options: McpOptions): Promise<McpTool[]> {
|
|
|
46
56
|
try {
|
|
47
57
|
const includedTools = selectTools(options);
|
|
48
58
|
if (includedTools.length === 0) {
|
|
49
|
-
|
|
59
|
+
getLogger().error('No tools match the provided filters');
|
|
50
60
|
process.exit(1);
|
|
51
61
|
}
|
|
52
62
|
return includedTools;
|
|
53
63
|
} catch (error) {
|
|
54
|
-
|
|
55
|
-
console.error('Error filtering tools:', error.message);
|
|
56
|
-
} else {
|
|
57
|
-
console.error('Error filtering tools:', error);
|
|
58
|
-
}
|
|
64
|
+
getLogger().error({ error }, 'Error filtering tools');
|
|
59
65
|
process.exit(1);
|
|
60
66
|
}
|
|
61
67
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
+
|
|
3
|
+
import fs from 'fs/promises';
|
|
4
|
+
import { readEnv } from './util';
|
|
5
|
+
import { getLogger } from './logger';
|
|
6
|
+
|
|
7
|
+
const INSTRUCTIONS_CACHE_TTL_MS = 15 * 60 * 1000; // 15 minutes
|
|
8
|
+
|
|
9
|
+
interface InstructionsCacheEntry {
|
|
10
|
+
fetchedInstructions: string;
|
|
11
|
+
fetchedAt: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const instructionsCache = new Map<string, InstructionsCacheEntry>();
|
|
15
|
+
|
|
16
|
+
export async function getInstructions({
|
|
17
|
+
stainlessApiKey,
|
|
18
|
+
customInstructionsPath,
|
|
19
|
+
}: {
|
|
20
|
+
stainlessApiKey?: string | undefined;
|
|
21
|
+
customInstructionsPath?: string | undefined;
|
|
22
|
+
}): Promise<string> {
|
|
23
|
+
const now = Date.now();
|
|
24
|
+
const cacheKey = customInstructionsPath ?? stainlessApiKey ?? '';
|
|
25
|
+
const cached = instructionsCache.get(cacheKey);
|
|
26
|
+
|
|
27
|
+
if (cached && now - cached.fetchedAt <= INSTRUCTIONS_CACHE_TTL_MS) {
|
|
28
|
+
return cached.fetchedInstructions;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Evict stale entries so the cache doesn't grow unboundedly.
|
|
32
|
+
for (const [key, entry] of instructionsCache) {
|
|
33
|
+
if (now - entry.fetchedAt > INSTRUCTIONS_CACHE_TTL_MS) {
|
|
34
|
+
instructionsCache.delete(key);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let fetchedInstructions: string;
|
|
39
|
+
|
|
40
|
+
if (customInstructionsPath) {
|
|
41
|
+
fetchedInstructions = await fetchLatestInstructionsFromFile(customInstructionsPath);
|
|
42
|
+
} else {
|
|
43
|
+
fetchedInstructions = await fetchLatestInstructionsFromApi(stainlessApiKey);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
instructionsCache.set(cacheKey, { fetchedInstructions, fetchedAt: now });
|
|
47
|
+
return fetchedInstructions;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function fetchLatestInstructionsFromFile(path: string): Promise<string> {
|
|
51
|
+
try {
|
|
52
|
+
return await fs.readFile(path, 'utf-8');
|
|
53
|
+
} catch (error) {
|
|
54
|
+
getLogger().error({ error, path }, 'Error fetching instructions from file');
|
|
55
|
+
throw error;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function fetchLatestInstructionsFromApi(stainlessApiKey: string | undefined): Promise<string> {
|
|
60
|
+
// Setting the stainless API key is optional, but may be required
|
|
61
|
+
// to authenticate requests to the Stainless API.
|
|
62
|
+
const response = await fetch(
|
|
63
|
+
readEnv('CODE_MODE_INSTRUCTIONS_URL') ?? 'https://api.stainless.com/api/ai/instructions/imagekit',
|
|
64
|
+
{
|
|
65
|
+
method: 'GET',
|
|
66
|
+
headers: { ...(stainlessApiKey && { Authorization: stainlessApiKey }) },
|
|
67
|
+
},
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
let instructions: string | undefined;
|
|
71
|
+
if (!response.ok) {
|
|
72
|
+
getLogger().warn(
|
|
73
|
+
'Warning: failed to retrieve MCP server instructions. Proceeding with default instructions...',
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
instructions =
|
|
77
|
+
'\n This is the imagekit 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 ';
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
instructions ??= ((await response.json()) as { instructions: string }).instructions;
|
|
81
|
+
|
|
82
|
+
return instructions;
|
|
83
|
+
}
|