@@ -4,6 +4,7 @@ import { McpTool, Metadata, ToolCallResult, asErrorResult, asTextContentResult }
44import { Tool } from '@modelcontextprotocol/sdk/types.js' ;
55import { readEnv , requireValue } from './server' ;
66import { WorkerInput , WorkerOutput } from './code-tool-types' ;
7+ import { SdkMethod } from './methods' ;
78import { ImageKit } from '@imagekit/nodejs' ;
89
910const prompt = `Runs JavaScript code to interact with the Image Kit API.
@@ -35,7 +36,7 @@ Variables will not persist between calls, so make sure to return or log any data
3536 *
3637 * @param endpoints - The endpoints to include in the list.
3738 */
38- export function codeTool ( ) : McpTool {
39+ export function codeTool ( params : { blockedMethods : SdkMethod [ ] | undefined } ) : McpTool {
3940 const metadata : Metadata = { resource : 'all' , operation : 'write' , tags : [ ] } ;
4041 const tool : Tool = {
4142 name : 'execute' ,
@@ -59,6 +60,24 @@ export function codeTool(): McpTool {
5960 const code = args . code as string ;
6061 const intent = args . intent as string | undefined ;
6162
63+ // Do very basic blocking of code that includes forbidden method names.
64+ //
65+ // WARNING: This is not secure against obfuscation and other evasion methods. If
66+ // stronger security blocks are required, then these should be enforced in the downstream
67+ // API (e.g., by having users call the MCP server with API keys with limited permissions).
68+ if ( params . blockedMethods ) {
69+ const blockedMatches = params . blockedMethods . filter ( ( method ) =>
70+ code . includes ( method . fullyQualifiedName ) ,
71+ ) ;
72+ if ( blockedMatches . length > 0 ) {
73+ return asErrorResult (
74+ `The following methods have been blocked by the MCP server and cannot be used in code execution: ${ blockedMatches
75+ . map ( ( m ) => m . fullyQualifiedName )
76+ . join ( ', ' ) } `,
77+ ) ;
78+ }
79+ }
80+
6281 // this is not required, but passing a Stainless API key for the matching project_name
6382 // will allow you to run code-mode queries against non-published versions of your SDK.
6483 const stainlessAPIKey = readEnv ( 'STAINLESS_API_KEY' ) ;
0 commit comments