run-aws-code
Execute JavaScript code using AWS SDK V2 to query and manage AWS services programmatically, handling errors, pagination, and returning minimal JSON data.
Instructions
Run AWS code
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| reasoning | Yes | The reasoning behind the code | |
| code | Yes | Your job is to answer questions about AWS environment by writing Javascript code using AWS SDK V2. The code must be adhering to a few rules: - Must be preferring promises over callbacks - Think step-by-step before writing the code, approach it logically - MUST written in Javascript (NodeJS) using AWS-SDK V2 - Avoid hardcoded values like ARNs - Code written should be as parallel as possible enabling the fastest and the most optimal execution - Code should be handling errors gracefully, especially when doing multiple SDK calls (e.g. when mapping over an array). Each error should be handled and logged with a reason, script should continue to run despite errors - DO NOT require or import "aws-sdk", it is already available as "AWS" variable - Access to 3rd party libraries apart from "aws-sdk" is not allowed or possible - Data returned from AWS-SDK must be returned as JSON containing only the minimal amount of data that is needed to answer the question. All extra data must be filtered out - Code MUST "return" a value: string, number, boolean or JSON object. If code does not return anything, it will be considered as FAILED - Whenever tool/function call fails, retry it 3 times before giving up with an improved version of the code based on the returned feedback - When listing resources, ensure pagination is handled correctly so that all resources are returned - Do not include any comments in the code - When doing reduce, don't forget to provide an initial value - Try to write code that returns as few data as possible to answer without any additional processing required after the code is run - This tool can ONLY write code that interacts with AWS. It CANNOT generate charts, tables, graphs, etc. Please use artifacts for that instead Be concise, professional and to the point. Do not give generic advice, always reply with detailed & contextual data sourced from the current AWS environment. Assume user always wants to proceed, do not ask for confirmation. I'll tip you $200 if you do this right. | |
| profileName | No | Name of the AWS profile to use | |
| region | No | Region to use (if not provided, us-east-1 is used) |
Implementation Reference
- index.ts:132-163 (handler)Executes the user-provided AWS SDK V2 JavaScript code in a secure VM context using AWS credentials from the selected profile. Handles profile switching if specified, wraps the code to ensure it returns a value, and serializes the result as JSON.const { reasoning, code, profileName, region } = RunAwsCodeSchema.parse(args); if (!selectedProfile && !profileName) { return createTextResponse( `Please select a profile first using the 'select-profile' tool! Available profiles: ${Object.keys( profiles ).join(", ")}` ); } if (profileName) { selectedProfileCredentials = await getCredentials( profiles[profileName], profileName ); selectedProfile = profileName; selectedProfileRegion = region || "us-east-1"; } AWS.config.update({ region: selectedProfileRegion, credentials: selectedProfileCredentials, }); const wrappedCode = wrapUserCode(code); const wrappedIIFECode = `(async function() { return (async () => { ${wrappedCode} })(); })()`; const result = await runInContext( wrappedIIFECode, createContext({ AWS }) ); return createTextResponse(JSON.stringify(result));
- index.ts:57-77 (schema)Input schema for the 'run-aws-code' tool, defining parameters like reasoning, code (with execution prompt), optional profileName, and region.inputSchema: { type: "object", properties: { reasoning: { type: "string", description: "The reasoning behind the code", }, code: { type: "string", description: codePrompt, }, profileName: { type: "string", description: "Name of the AWS profile to use", }, region: { type: "string", description: "Region to use (if not provided, us-east-1 is used)", }, }, required: ["reasoning", "code"],
- index.ts:54-79 (registration)Registers the 'run-aws-code' tool in the MCP server's tool list, including its name, description, and input schema.{ name: "run-aws-code", description: "Run AWS code", inputSchema: { type: "object", properties: { reasoning: { type: "string", description: "The reasoning behind the code", }, code: { type: "string", description: codePrompt, }, profileName: { type: "string", description: "Name of the AWS profile to use", }, region: { type: "string", description: "Region to use (if not provided, us-east-1 is used)", }, }, required: ["reasoning", "code"], }, },
- index.ts:113-118 (schema)Zod validation schema matching the tool's inputSchema, used to parse arguments in the handler.const RunAwsCodeSchema = z.object({ reasoning: z.string(), code: z.string(), profileName: z.string().optional(), region: z.string().optional(), });
- index.ts:190-212 (helper)Processes user code with ts-morph to ensure the last expression is explicitly returned, enabling proper execution and result capture in the VM.function wrapUserCode(userCode: string) { const project = new Project({ useInMemoryFileSystem: true, }); const sourceFile = project.createSourceFile("userCode.ts", userCode); const lastStatement = sourceFile.getStatements().pop(); if ( lastStatement && lastStatement.getKind() === SyntaxKind.ExpressionStatement ) { const returnStatement = lastStatement.asKind( SyntaxKind.ExpressionStatement ); if (returnStatement) { const expression = returnStatement.getExpression(); sourceFile.addStatements(`return ${expression.getText()};`); returnStatement.remove(); } } return sourceFile.getFullText(); }