Skip to main content
Glama

list_network_security_groups

Retrieve Network Security Groups (NSGs) and their security rules from a specified Oracle Cloud Infrastructure compartment, optionally filtered by VCN.

Instructions

List all Network Security Groups (NSGs) in a compartment.

Args:
    compartment_id: OCID of the compartment to list NSGs from
    vcn_id: Optional OCID of the VCN to filter NSGs

Returns:
    List of NSGs with their security rules

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
compartment_idYes
vcn_idNo

Implementation Reference

  • Core handler function that executes the OCI API call to list network security groups in a compartment, optionally filtered by VCN, using pagination and formatting the response.
    def list_network_security_groups(network_client: oci.core.VirtualNetworkClient, compartment_id: str,
                                      vcn_id: Optional[str] = None) -> List[Dict[str, Any]]:
        """
        List all network security groups in a compartment, optionally filtered by VCN.
        
        Args:
            network_client: OCI VirtualNetwork client
            compartment_id: OCID of the compartment
            vcn_id: Optional OCID of the VCN to filter by
            
        Returns:
            List of network security groups with their details
        """
        try:
            nsgs_response = oci.pagination.list_call_get_all_results(
                network_client.list_network_security_groups,
                compartment_id,
                vcn_id=vcn_id
            )
            
            nsgs = []
            for nsg in nsgs_response.data:
                nsgs.append({
                    "id": nsg.id,
                    "display_name": nsg.display_name,
                    "compartment_id": nsg.compartment_id,
                    "vcn_id": nsg.vcn_id,
                    "lifecycle_state": nsg.lifecycle_state,
                    "time_created": str(nsg.time_created),
                })
            
            logger.info(f"Found {len(nsgs)} network security groups in compartment {compartment_id}")
            return nsgs
            
        except Exception as e:
            logger.exception(f"Error listing network security groups: {e}")
            raise
  • MCP tool registration using @mcp.tool decorator. This async wrapper function handles context, logging, error handling via mcp_tool_wrapper, and delegates to the core handler.
    @mcp.tool(name="list_network_security_groups")
    @mcp_tool_wrapper(
        start_msg="Listing network security groups in compartment {compartment_id}...",
        error_prefix="Error listing network security groups"
    )
    async def mcp_list_network_security_groups(ctx: Context, compartment_id: str, vcn_id: Optional[str] = None) -> List[Dict[str, Any]]:
        """
        List all Network Security Groups (NSGs) in a compartment.
    
        Args:
            compartment_id: OCID of the compartment to list NSGs from
            vcn_id: Optional OCID of the VCN to filter NSGs
    
        Returns:
            List of NSGs with their security rules
        """
        return list_network_security_groups(oci_clients["network"], compartment_id, vcn_id)

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/jopsis/mcp-server-oci'

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