Skip to main content
Glama
dkruyt

Hetzner Cloud MCP Server

by dkruyt

set_firewall_rules

Configure or replace firewall rules on Hetzner Cloud to control network traffic for your servers.

Instructions

Set rules for a firewall.

Sets the rules of a firewall. All existing rules will be overwritten.
Pass an empty rules array to remove all rules.

Example:
- Set rules: {"firewall_id": 12345, "rules": [{"direction": "in", "protocol": "tcp", "port": "80", "source_ips": ["0.0.0.0/0"]}]}

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
paramsYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The handler function for the 'set_firewall_rules' MCP tool. It retrieves the specified firewall, converts the provided rule parameters into hcloud FirewallRule objects, applies the rules using the Hetzner API client.firewalls.set_rules(), and returns the resulting actions.
    def set_firewall_rules(params: SetFirewallRulesParams) -> Dict[str, Any]:
        """
        Set rules for a firewall.
        
        Sets the rules of a firewall. All existing rules will be overwritten.
        Pass an empty rules array to remove all rules.
        
        Example:
        - Set rules: {"firewall_id": 12345, "rules": [{"direction": "in", "protocol": "tcp", "port": "80", "source_ips": ["0.0.0.0/0"]}]}
        """
        try:
            firewall = client.firewalls.get_by_id(params.firewall_id)
            if not firewall:
                return {"error": f"Firewall with ID {params.firewall_id} not found"}
            
            # Convert rule parameters to FirewallRule objects
            rules = []
            for rule_param in params.rules:
                rule = FirewallRule(
                    direction=rule_param.direction,
                    protocol=rule_param.protocol,
                    source_ips=rule_param.source_ips,
                    port=rule_param.port,
                    destination_ips=rule_param.destination_ips,
                    description=rule_param.description
                )
                rules.append(rule)
            
            # Set the rules
            actions = client.firewalls.set_rules(firewall, rules)
            
            # Format the response
            return {
                "success": True,
                "actions": [
                    {
                        "id": action.id,
                        "status": action.status,
                        "command": action.command,
                        "progress": action.progress,
                        "error": action.error,
                        "started": action.started.isoformat() if action.started else None,
                        "finished": action.finished.isoformat() if action.finished else None,
                    }
                    for action in actions
                ] if actions else None,
            }
        except Exception as e:
            return {"error": f"Failed to set firewall rules: {str(e)}"}
  • Pydantic BaseModel defining the input schema for the set_firewall_rules tool, including firewall_id and rules list.
    class SetFirewallRulesParams(BaseModel):
        firewall_id: int = Field(..., description="The ID of the firewall")
        rules: List[FirewallRuleParam] = Field(..., description="List of firewall rules")
  • Pydantic BaseModel defining the structure for individual firewall rules used in set_firewall_rules.
    class FirewallRuleParam(BaseModel):
        direction: str = Field(..., description="Direction of the rule (in or out)")
        protocol: str = Field(..., description="Protocol (tcp, udp, icmp, esp, or gre)")
        source_ips: List[str] = Field(..., description="List of source IPs in CIDR notation")
        port: Optional[str] = Field(None, description="Port or port range (e.g., '80' or '80-85'), only for TCP/UDP")
        destination_ips: Optional[List[str]] = Field(None, description="List of destination IPs in CIDR notation")
        description: Optional[str] = Field(None, description="Description of the rule")
  • The @mcp.tool() decorator registers the set_firewall_rules function as an MCP tool.
    def set_firewall_rules(params: SetFirewallRulesParams) -> Dict[str, Any]:
Behavior4/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes key traits: it's a mutation operation ('Set rules'), specifies that it overwrites existing rules, and explains how to remove all rules. This covers critical aspects like destructive behavior and usage patterns, though it could add more on error handling or permissions.

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 front-loaded with the core action and key behavioral notes, followed by a helpful example. Each sentence adds value, such as clarifying the overwrite behavior and removal method, making it efficient. It could be slightly more structured by separating guidelines from examples, but overall it's well-organized and avoids redundancy.

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

Completeness4/5

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

Given the complexity of a mutation tool with no annotations and an output schema present, the description does a solid job. It covers the purpose, behavioral traits, and parameter usage through the example. The output schema likely handles return values, so the description focuses on input and behavior, making it reasonably complete for agent use, though it could benefit from more explicit sibling differentiation.

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 schema description coverage is 0%, so the description must compensate. It includes an example that illustrates the parameters ('firewall_id' and 'rules' array with nested properties like 'direction'), adding practical meaning beyond the bare schema. However, it doesn't fully explain all parameter details (e.g., optional fields like 'destination_ips'), leaving some gaps in understanding.

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 action ('Set rules for a firewall') and specifies the resource ('firewall'), making the purpose evident. However, it doesn't explicitly differentiate from sibling tools like 'update_firewall' or 'create_firewall', which could handle similar operations, leaving some ambiguity about when to choose this specific tool.

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

Usage Guidelines3/5

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

The description implies usage by noting that 'All existing rules will be overwritten' and 'Pass an empty rules array to remove all rules', which provides some context for when to use it (e.g., for full replacements or clearing). However, it lacks explicit guidance on alternatives like 'update_firewall' or prerequisites, leaving the agent to infer the best choice among siblings.

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

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/dkruyt/mcp-hetzner'

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