Skip to main content
Glama
baryhuang

AWS Resources MCP Server

aws_resources_query_or_modify

Execute Python code with boto3 to query or modify AWS resources directly from conversations using the AWS Resources MCP Server.

Instructions

Execute a boto3 code snippet to query or modify AWS resources

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
code_snippetYesPython code using boto3 to query or modify AWS resources. The code should have default execution setting variable named 'result'. Example code: 'result = boto3.client('s3').list_buckets()'

Implementation Reference

  • Core handler that executes the boto3 code snippet provided by the tool input, performing safety validations and returning JSON results.
    def execute_query(self, code_snippet: str) -> str:
        """
        Execute a boto3 code snippet and return the results
    
        Args:
            code_snippet (str): Python code using boto3 to query AWS resources
    
        Returns:
            str: JSON string containing the query results or error message
        """
        try:
            # Parse the code into an AST
            tree = ast.parse(code_snippet)
    
            # Analyze the code
            executor = CodeExecutor()
            executor.visit(tree)
    
            # Validate imports
            allowed_modules = {'boto3', 'operator', 'json', 'datetime', 'pytz', 'dateutil', 're', 'time'}
            unauthorized_imports = executor.imported_modules - allowed_modules
            if unauthorized_imports:
                return json.dumps({
                    "error": f"Unauthorized imports: {', '.join(unauthorized_imports)}. "
                            f"Only {', '.join(allowed_modules)} are allowed."
                })
    
            # Create execution namespace
            local_ns = {
                'boto3': boto3,
                'session': self.session,
                'result': None,
                'itemgetter': itemgetter,
                '__builtins__': {
                    name: getattr(__builtins__, name)
                    for name in [
                        'dict', 'list', 'tuple', 'set', 'str', 'int', 'float', 'bool',
                        'len', 'max', 'min', 'sorted', 'filter', 'map', 'sum', 'any', 'all',
                        '__import__', 'hasattr', 'getattr', 'isinstance', 'print'
                    ]
                }
            }
    
            # Compile and execute the code
            compiled_code = compile(tree, '<string>', 'exec')
            exec(compiled_code, local_ns)
    
            # Get the result
            result = local_ns.get('result')
    
            # Validate result was set
            if not executor.has_result:
                return json.dumps({
                    "error": "Code must set a 'result' variable with the query output"
                })
    
            # Convert result to JSON-serializable format
            if result is not None:
                if hasattr(result, 'to_dict'):
                    result = result.to_dict()
                return json.dumps(result, default=str)
            else:
                return json.dumps({"error": "Result cannot be None"})
    
        except SyntaxError as e:
            logger.error(f"Syntax error in code: {str(e)}")
            return json.dumps({"error": f"Syntax error: {str(e)}"})
        except Exception as e:
            logger.error(f"Error executing query: {str(e)}")
            return json.dumps({"error": str(e)})
  • Input schema definition for the tool, specifying the required 'code_snippet' parameter.
    inputSchema={
        "type": "object",
        "properties": {
            "code_snippet": {
                "type": "string",
                "description": "Python code using boto3 to query or modify AWS resources. The code should have default execution setting variable named 'result'. Example code: 'result = boto3.client('s3').list_buckets()'"
            }
        },
        "required": ["code_snippet"]
    },
  • Registration of the tool in the MCP server's list_tools method, including name, description, and schema.
    return [
        types.Tool(
            name="aws_resources_query_or_modify",
            description="Execute a boto3 code snippet to query or modify AWS resources",
            inputSchema={
                "type": "object",
                "properties": {
                    "code_snippet": {
                        "type": "string",
                        "description": "Python code using boto3 to query or modify AWS resources. The code should have default execution setting variable named 'result'. Example code: 'result = boto3.client('s3').list_buckets()'"
                    }
                },
                "required": ["code_snippet"]
            },
        )
    ]
  • MCP server call_tool handler that checks the tool name and invokes the execution logic.
    @server.call_tool()
    async def handle_call_tool(
        name: str, arguments: dict[str, Any] | None
    ) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]:
        """Handle tool execution requests"""
        try:
            if name == "aws_resources_query_or_modify":
                if not arguments or "code_snippet" not in arguments:
                    raise ValueError("Missing code_snippet argument")
    
                results = aws_querier.execute_query(arguments["code_snippet"])
                return [types.TextContent(type="text", text=str(results))]
            else:
                raise ValueError(f"Unknown tool: {name}")
    
        except Exception as e:
            return [types.TextContent(type="text", text=f"Error: {str(e)}")]
  • Helper class for AST analysis to validate code snippets: checks for 'result' assignment and tracks imports.
    class CodeExecutor(ast.NodeTransformer):
        """Custom AST NodeTransformer to validate and transform the code"""
    
        def __init__(self):
            self.has_result = False
            self.imported_modules = set()
    
        def visit_Assign(self, node):
            """Track if 'result' variable is assigned"""
            for target in node.targets:
                if isinstance(target, ast.Name) and target.id == 'result':
                    self.has_result = True
            return node
    
        def visit_Import(self, node):
            """Track imported modules"""
            for alias in node.names:
                self.imported_modules.add(alias.name)
            return node
    
        def visit_ImportFrom(self, node):
            """Track imported modules"""
            self.imported_modules.add(node.module)
            return node
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool can 'query or modify' AWS resources, implying both read and write operations, but fails to detail critical aspects like authentication requirements, error handling, rate limits, or safety considerations. This leaves significant gaps in understanding the tool's behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is concise and front-loaded in a single sentence: 'Execute a boto3 code snippet to query or modify AWS resources.' It efficiently conveys the core purpose without unnecessary details, though it could be slightly improved by structuring usage hints separately.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (executing arbitrary code for AWS operations) and the absence of annotations and output schema, the description is incomplete. It lacks information on return values, error cases, security implications, and operational constraints, which are crucial for safe and effective use by an AI agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 100% description coverage, with the 'code_snippet' parameter well-documented in the schema. The description adds no additional meaning beyond what the schema provides, as it only repeats the boto3 and AWS context. According to the rules, with high schema coverage, the baseline is 3 even without param info in the description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Execute a boto3 code snippet to query or modify AWS resources.' It specifies the action (execute), technology (boto3), and target (AWS resources). However, it doesn't distinguish from siblings since there are none, so it cannot achieve the full differentiation required for a score of 5.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It mentions querying or modifying AWS resources but offers no context about specific scenarios, prerequisites, or exclusions. This lack of usage direction limits its effectiveness for an AI agent.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/baryhuang/mcp-server-aws-resources-python'

If you have feedback or need assistance with the MCP directory API, please join our Discord server