get_prefixes
Retrieve network prefixes from Nautobot with filtering by prefix, status, site, role, tenant, VRF, and pagination options.
Instructions
Retrieve network prefixes from Nautobot with filtering options
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prefix | No | Specific network prefix to search for | |
| status | No | Status to filter by | |
| site | No | Site to filter by | |
| role | No | Role to filter by | |
| tenant | No | Tenant to filter by | |
| vrf | No | VRF to filter by | |
| limit | No | Maximum number of results to return (default: 100, max: 1000) | |
| offset | No | Number of results to skip for pagination (default: 0) |
Implementation Reference
- The handler logic within @server.call_tool() that processes the 'get_prefixes' tool call, extracts parameters, calls the NautobotClient helper, formats results as JSON, and returns them as TextContent.
elif name == "get_prefixes": # Extract and validate arguments prefix = args.get("prefix") status = args.get("status") site = args.get("site") role = args.get("role") tenant = args.get("tenant") vrf = args.get("vrf") limit = min(args.get("limit", 100), 1000) # Cap at 1000 offset = args.get("offset", 0) logger.info(f"Retrieving prefixes with filters: {args}") # Get prefixes from Nautobot prefixes = await client.get_prefixes( prefix=prefix, status=status, site=site, role=role, tenant=tenant, vrf=vrf, limit=limit, offset=offset ) # Format results result = { "count": len(prefixes), "filters_applied": {k: v for k, v in args.items() if v is not None}, "results": [prefix_obj.model_dump() for prefix_obj in prefixes] } return [ types.TextContent( type="text", text=f"Retrieved {len(prefixes)} network prefixes from Nautobot:\n\n" f"```json\n{result}\n```" ) ] - src/mcp_nautobot_server/server.py:434-480 (registration)Registration of the 'get_prefixes' tool in the @server.list_tools() handler, including name, description, and input schema.
types.Tool( name="get_prefixes", description="Retrieve network prefixes from Nautobot with filtering options", inputSchema={ "type": "object", "properties": { "prefix": { "type": "string", "description": "Specific network prefix to search for" }, "status": { "type": "string", "description": "Status to filter by" }, "site": { "type": "string", "description": "Site to filter by" }, "role": { "type": "string", "description": "Role to filter by" }, "tenant": { "type": "string", "description": "Tenant to filter by" }, "vrf": { "type": "string", "description": "VRF to filter by" }, "limit": { "type": "integer", "description": "Maximum number of results to return (default: 100, max: 1000)", "default": 100, "minimum": 1, "maximum": 1000 }, "offset": { "type": "integer", "description": "Number of results to skip for pagination (default: 0)", "default": 0, "minimum": 0 } }, "additionalProperties": False }, ), - Helper method in NautobotClient that performs the actual API request to retrieve prefixes from Nautobot /ipam/prefixes/ endpoint, applies filters, parses responses into Prefix models.
async def get_prefixes( self, prefix: Optional[str] = None, status: Optional[str] = None, site: Optional[str] = None, role: Optional[str] = None, tenant: Optional[str] = None, vrf: Optional[str] = None, limit: int = 100, offset: int = 0 ) -> List[Prefix]: """ Retrieve network prefixes from Nautobot. Args: prefix: Filter by network prefix status: Filter by status slug site: Filter by site slug role: Filter by role slug tenant: Filter by tenant slug vrf: Filter by VRF name limit: Maximum number of results to return offset: Number of results to skip Returns: List of Prefix objects Raises: NautobotError: For API or connection errors """ params: Dict[str, Any] = { "limit": limit, "offset": offset, } # Add optional filters if prefix: params["prefix"] = prefix if status: params["status"] = status if site: params["site"] = site if role: params["role"] = role if tenant: params["tenant"] = tenant if vrf: params["vrf"] = vrf try: response = await self._make_request("GET", "/ipam/prefixes/", params) # Parse results prefixes = [] for item in response.get("results", []): try: prefixes.append(Prefix(**item)) except Exception as e: logger.warning(f"Failed to parse prefix data: {e}") continue logger.info(f"Retrieved {len(prefixes)} prefixes") return prefixes except Exception as e: logger.error(f"Failed to retrieve prefixes: {e}") raise - Pydantic model defining the structure for prefix data returned from Nautobot API.
class Prefix(BaseModel): """Pydantic model for Nautobot prefix data.""" id: str url: HttpUrl prefix: str status: Dict[str, Any] site: Optional[Dict[str, Any]] = None vrf: Optional[Dict[str, Any]] = None tenant: Optional[Dict[str, Any]] = None vlan: Optional[Dict[str, Any]] = None role: Optional[Dict[str, Any]] = None is_pool: bool = False description: Optional[str] = None comments: Optional[str] = None tags: List[Dict[str, Any]] = Field(default_factory=list) custom_fields: Dict[str, Any] = Field(default_factory=dict) created: str last_updated: str