Skip to content

Commit a1cbb00

Browse files
feat(mcp): add code execution tool
1 parent 736876e commit a1cbb00

File tree

9 files changed

+230
-12
lines changed

9 files changed

+230
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"publint": "^0.2.12",
4545
"ts-jest": "^29.1.0",
4646
"ts-node": "^10.5.0",
47-
"tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.8/tsc-multi.tgz",
47+
"tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.9/tsc-multi.tgz",
4848
"tsconfig-paths": "^4.0.0",
4949
"typescript": "5.8.3",
5050
"typescript-eslint": "8.31.1"

packages/mcp-server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"isaacus": "file:../../dist/",
3131
"@cloudflare/cabidela": "^0.2.4",
3232
"@modelcontextprotocol/sdk": "^1.11.5",
33+
"@valtown/deno-http-worker": "^0.0.21",
3334
"cors": "^2.8.5",
3435
"express": "^5.1.0",
3536
"jq-web": "https://github.com/stainless-api/jq-web/releases/download/v0.8.6/jq-web.tar.gz",
@@ -57,7 +58,7 @@
5758
"ts-jest": "^29.1.0",
5859
"ts-morph": "^19.0.0",
5960
"ts-node": "^10.5.0",
60-
"tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.8/tsc-multi.tgz",
61+
"tsc-multi": "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.9/tsc-multi.tgz",
6162
"tsconfig-paths": "^4.0.0",
6263
"typescript": "5.8.3"
6364
},
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
export const workerPath = require.resolve('./code-tool-worker.mjs');
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import { type ClientOptions } from 'isaacus/client';
4+
5+
export type WorkerInput = {
6+
opts: ClientOptions;
7+
code: string;
8+
};
9+
export type WorkerSuccess = {
10+
result: unknown | null;
11+
logLines: string[];
12+
errLines: string[];
13+
};
14+
export type WorkerError = { message: string | undefined };
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import util from 'node:util';
4+
import { WorkerInput, WorkerSuccess, WorkerError } from './code-tool-types';
5+
import { Isaacus } from 'isaacus';
6+
7+
const fetch = async (req: Request): Promise<Response> => {
8+
const { opts, code } = (await req.json()) as WorkerInput;
9+
const client = new Isaacus({
10+
...opts,
11+
});
12+
13+
const logLines: string[] = [];
14+
const errLines: string[] = [];
15+
const console = {
16+
log: (...args: unknown[]) => {
17+
logLines.push(util.format(...args));
18+
},
19+
error: (...args: unknown[]) => {
20+
errLines.push(util.format(...args));
21+
},
22+
};
23+
try {
24+
let run_ = async (client: any) => {};
25+
eval(`
26+
${code}
27+
run_ = run;
28+
`);
29+
const result = await run_(client);
30+
return Response.json({
31+
result,
32+
logLines,
33+
errLines,
34+
} satisfies WorkerSuccess);
35+
} catch (e) {
36+
const message = e instanceof Error ? e.message : undefined;
37+
return Response.json(
38+
{
39+
message,
40+
} satisfies WorkerError,
41+
{ status: 400, statusText: 'Code execution error' },
42+
);
43+
}
44+
};
45+
46+
export default { fetch };
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import { type ClientOptions } from 'isaacus/client';
4+
5+
import { dirname } from 'node:path';
6+
import { pathToFileURL } from 'node:url';
7+
import Isaacus from 'isaacus';
8+
import { Endpoint, ContentBlock, Metadata } from './tools/types';
9+
10+
import { Tool } from '@modelcontextprotocol/sdk/types.js';
11+
12+
import { newDenoHTTPWorker } from '@valtown/deno-http-worker';
13+
import { WorkerInput, WorkerError, WorkerSuccess } from './code-tool-types';
14+
import { workerPath } from './code-tool-paths.cjs';
15+
16+
/**
17+
* A tool that runs code against a copy of the SDK.
18+
*
19+
* Instead of exposing every endpoint as it's own tool, which uses up too many tokens for LLMs to use at once,
20+
* we expose a single tool that can be used to search for endpoints by name, resource, operation, or tag, and then
21+
* a generic endpoint that can be used to invoke any endpoint with the provided arguments.
22+
*
23+
* @param endpoints - The endpoints to include in the list.
24+
*/
25+
export function codeTool(): Endpoint {
26+
const metadata: Metadata = { resource: 'all', operation: 'write', tags: [] };
27+
const tool: Tool = {
28+
name: 'execute',
29+
description:
30+
'Runs Typescript code to interact with the API.\nYou are a skilled programmer writing code to interface with the service.\nDefine an async function named "run" that takes a single parameter of an initialized client, and it will be run.\nDo not initialize a client, but instead use the client that you are given as a parameter.\nYou will be returned anything that your function returns, plus the results of any console.log statements.\nIf any code triggers an error, the tool will return an error response, so you do not need to add error handling unless you want to output something more helpful than the raw error.\nIt is not necessary to add comments to code, unless by adding those comments you believe that you can generate better code.\nThis code will run in a container, and you will not be able to use fetch or otherwise interact with the network calls other than through the client you are given.\nAny variables you define won\'t live between successive uses of this call, so make sure to return or log any data you might need later.',
31+
inputSchema: { type: 'object', properties: { code: { type: 'string' } } },
32+
};
33+
34+
const handler = async (client: Isaacus, args: unknown) => {
35+
const baseURLHostname = new URL(client.baseURL).hostname;
36+
const { code } = args as { code: string };
37+
38+
const worker = await newDenoHTTPWorker(pathToFileURL(workerPath), {
39+
runFlags: [
40+
`--node-modules-dir=manual`,
41+
`--allow-read=code-tool-worker.mjs,${workerPath.replace(/([\/\\]node_modules)[\/\\].+$/, '$1')}/`,
42+
`--allow-net=${baseURLHostname}`,
43+
// Allow environment variables because instantiating the client will try to read from them,
44+
// even though they are not set.
45+
'--allow-env',
46+
],
47+
printOutput: true,
48+
spawnOptions: {
49+
cwd: dirname(workerPath),
50+
},
51+
});
52+
53+
try {
54+
const resp = await new Promise<Response>((resolve, reject) => {
55+
worker.addEventListener('exit', (exitCode) => {
56+
reject(new Error(`Worker exited with code ${exitCode}`));
57+
});
58+
59+
const opts: ClientOptions = {
60+
baseURL: client.baseURL,
61+
apiKey: client.apiKey,
62+
defaultHeaders: {
63+
'X-Stainless-MCP': 'true',
64+
},
65+
};
66+
67+
const req = worker.request(
68+
'http://localhost',
69+
{
70+
headers: {
71+
'content-type': 'application/json',
72+
},
73+
method: 'POST',
74+
},
75+
(resp) => {
76+
const body: Uint8Array[] = [];
77+
resp.on('error', (err) => {
78+
reject(err);
79+
});
80+
resp.on('data', (chunk) => {
81+
body.push(chunk);
82+
});
83+
resp.on('end', () => {
84+
resolve(
85+
new Response(Buffer.concat(body).toString(), {
86+
status: resp.statusCode ?? 200,
87+
headers: resp.headers as any,
88+
}),
89+
);
90+
});
91+
},
92+
);
93+
94+
const body = JSON.stringify({
95+
opts,
96+
code,
97+
} satisfies WorkerInput);
98+
99+
req.write(body, (err) => {
100+
if (err !== null && err !== undefined) {
101+
reject(err);
102+
}
103+
});
104+
105+
req.end();
106+
});
107+
108+
if (resp.status === 200) {
109+
const { result, logLines, errLines } = (await resp.json()) as WorkerSuccess;
110+
const returnOutput: ContentBlock | null =
111+
result === null ? null
112+
: result === undefined ? null
113+
: {
114+
type: 'text',
115+
text: typeof result === 'string' ? (result as string) : JSON.stringify(result),
116+
};
117+
const logOutput: ContentBlock | null =
118+
logLines.length === 0 ?
119+
null
120+
: {
121+
type: 'text',
122+
text: logLines.join('\n'),
123+
};
124+
const errOutput: ContentBlock | null =
125+
errLines.length === 0 ?
126+
null
127+
: {
128+
type: 'text',
129+
text: 'Error output:\n' + errLines.join('\n'),
130+
};
131+
return {
132+
content: [returnOutput, logOutput, errOutput].filter((block) => block !== null),
133+
};
134+
} else {
135+
const { message } = (await resp.json()) as WorkerError;
136+
throw new Error(message);
137+
}
138+
} catch (e) {
139+
throw e;
140+
} finally {
141+
worker.terminate();
142+
}
143+
};
144+
145+
return { metadata, tool, handler };
146+
}

packages/mcp-server/src/options.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export type McpOptions = {
1616
client: ClientType | undefined;
1717
includeDynamicTools: boolean | undefined;
1818
includeAllTools: boolean | undefined;
19+
includeCodeTools: boolean | undefined;
1920
filters: Filter[];
2021
capabilities?: Partial<ClientCapabilities>;
2122
};
@@ -54,13 +55,13 @@ export function parseCLIOptions(): CLIOptions {
5455
.option('tools', {
5556
type: 'string',
5657
array: true,
57-
choices: ['dynamic', 'all'],
58+
choices: ['dynamic', 'all', 'code'],
5859
description: 'Use dynamic tools or all tools',
5960
})
6061
.option('no-tools', {
6162
type: 'string',
6263
array: true,
63-
choices: ['dynamic', 'all'],
64+
choices: ['dynamic', 'all', 'code'],
6465
description: 'Do not use any dynamic or all tools',
6566
})
6667
.option('tool', {
@@ -251,11 +252,13 @@ export function parseCLIOptions(): CLIOptions {
251252
}
252253
}
253254

255+
const shouldIncludeToolType = (toolType: 'dynamic' | 'all' | 'code') =>
256+
explicitTools ? argv.tools?.includes(toolType) && !argv.noTools?.includes(toolType) : undefined;
257+
254258
const explicitTools = Boolean(argv.tools || argv.noTools);
255-
const includeDynamicTools =
256-
explicitTools ? argv.tools?.includes('dynamic') && !argv.noTools?.includes('dynamic') : undefined;
257-
const includeAllTools =
258-
explicitTools ? argv.tools?.includes('all') && !argv.noTools?.includes('all') : undefined;
259+
const includeDynamicTools = shouldIncludeToolType('dynamic');
260+
const includeAllTools = shouldIncludeToolType('all');
261+
const includeCodeTools = shouldIncludeToolType('code');
259262

260263
const transport = argv.transport as 'stdio' | 'http';
261264

@@ -264,6 +267,7 @@ export function parseCLIOptions(): CLIOptions {
264267
client: client && knownClients[client] ? client : undefined,
265268
includeDynamicTools,
266269
includeAllTools,
270+
includeCodeTools,
267271
filters,
268272
capabilities: clientCapabilities,
269273
list: argv.list || false,
@@ -385,6 +389,8 @@ export function parseQueryOptions(defaultOptions: McpOptions, query: unknown): M
385389
includeAllTools:
386390
defaultOptions.includeAllTools ??
387391
(queryOptions.tools?.includes('all') && !queryOptions.no_tools?.includes('all')),
392+
// Never include code tools on remote server.
393+
includeCodeTools: undefined,
388394
filters,
389395
capabilities: clientCapabilities,
390396
};

packages/mcp-server/src/server.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
parseEmbeddedJSON,
1515
} from './compat';
1616
import { dynamicTools } from './dynamic-tools';
17+
import { codeTool } from './code-tool';
1718
import { McpOptions } from './options';
1819

1920
export { McpOptions } from './options';
@@ -116,6 +117,8 @@ export function selectTools(endpoints: Endpoint[], options: McpOptions): Endpoin
116117
includedTools = endpoints;
117118
} else if (options.includeDynamicTools) {
118119
includedTools = dynamicTools(endpoints);
120+
} else if (options.includeCodeTools) {
121+
includedTools = [codeTool()];
119122
} else {
120123
includedTools = endpoints;
121124
}

yarn.lock

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,10 +3283,9 @@ ts-node@^10.5.0:
32833283
v8-compile-cache-lib "^3.0.0"
32843284
yn "3.1.1"
32853285

3286-
"tsc-multi@https://github.com/stainless-api/tsc-multi/releases/download/v1.1.8/tsc-multi.tgz":
3287-
version "1.1.8"
3288-
uid f544b359b8f05e607771ffacc280e58201476b04
3289-
resolved "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.8/tsc-multi.tgz#f544b359b8f05e607771ffacc280e58201476b04"
3286+
"tsc-multi@https://github.com/stainless-api/tsc-multi/releases/download/v1.1.9/tsc-multi.tgz":
3287+
version "1.1.9"
3288+
resolved "https://github.com/stainless-api/tsc-multi/releases/download/v1.1.9/tsc-multi.tgz#777f6f5d9e26bf0e94e5170990dd3a841d6707cd"
32903289
dependencies:
32913290
debug "^4.3.7"
32923291
fast-glob "^3.3.2"

0 commit comments

Comments
 (0)