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