netbox-mcp 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- netbox_mcp/__init__.py +5 -0
- netbox_mcp/filters.py +112 -0
- netbox_mcp/models.py +105 -0
- netbox_mcp/netbox_client.py +495 -0
- netbox_mcp/object_types.py +249 -0
- netbox_mcp/retry.py +164 -0
- netbox_mcp/sentry_init.py +92 -0
- netbox_mcp/server.py +269 -0
- netbox_mcp/tools/__init__.py +29 -0
- netbox_mcp/tools/branching.py +127 -0
- netbox_mcp/tools/common.py +249 -0
- netbox_mcp/tools/errors.py +71 -0
- netbox_mcp/tools/extras.py +332 -0
- netbox_mcp/tools/objects.py +416 -0
- netbox_mcp/tools/writes.py +376 -0
- netbox_mcp/utils.py +24 -0
- netbox_mcp-0.1.0.dist-info/METADATA +567 -0
- netbox_mcp-0.1.0.dist-info/RECORD +21 -0
- netbox_mcp-0.1.0.dist-info/WHEEL +4 -0
- netbox_mcp-0.1.0.dist-info/entry_points.txt +2 -0
- netbox_mcp-0.1.0.dist-info/licenses/LICENSE +21 -0
netbox_mcp/__init__.py
ADDED
netbox_mcp/filters.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Validation and encoding of NetBox list filters.
|
|
3
|
+
|
|
4
|
+
NetBox answers an unsupported filter by ignoring it, not by failing: a typo or a
|
|
5
|
+
Django-style lookup it does not know returns the *unfiltered* list, which a model
|
|
6
|
+
then reports as the answer. Rejecting the known-bad shapes up front turns that
|
|
7
|
+
silent over-match into an error the caller can fix.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
# Query parameters as (name, value) pairs; the value union matches what httpx2
|
|
13
|
+
# accepts, so the list can be passed straight through.
|
|
14
|
+
QueryPairs = list[tuple[str, str | int | float | None]]
|
|
15
|
+
|
|
16
|
+
# Lookup suffixes NetBox filtersets generate. Support is still field-specific.
|
|
17
|
+
VALID_LOOKUP_SUFFIXES = frozenset(
|
|
18
|
+
{
|
|
19
|
+
"n",
|
|
20
|
+
"ic",
|
|
21
|
+
"nic",
|
|
22
|
+
"isw",
|
|
23
|
+
"nisw",
|
|
24
|
+
"iew",
|
|
25
|
+
"niew",
|
|
26
|
+
"ie",
|
|
27
|
+
"nie",
|
|
28
|
+
"empty",
|
|
29
|
+
"regex",
|
|
30
|
+
"iregex",
|
|
31
|
+
"lt",
|
|
32
|
+
"lte",
|
|
33
|
+
"gt",
|
|
34
|
+
"gte",
|
|
35
|
+
}
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
# Query parameters the tools set themselves; filters must not override them.
|
|
39
|
+
RESERVED_PARAMS = frozenset(
|
|
40
|
+
{"limit", "offset", "fields", "omit", "brief", "ordering", "format"}
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def validate_filters(filters: dict[str, Any]) -> None:
|
|
45
|
+
"""Reject filter names NetBox would silently ignore.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
filters: Filter parameters, e.g. ``{"site_id": 1, "name__ic": "core"}``.
|
|
49
|
+
|
|
50
|
+
Raises:
|
|
51
|
+
ValueError: On a reserved parameter, the unsupported ``__in`` suffix, or
|
|
52
|
+
a multi-hop lookup such as ``device__site_id``.
|
|
53
|
+
"""
|
|
54
|
+
for name in filters:
|
|
55
|
+
if name in RESERVED_PARAMS:
|
|
56
|
+
raise ValueError(
|
|
57
|
+
f"Invalid filter {name!r}: pass it as the tool's own "
|
|
58
|
+
f"{name!r} argument, not inside filters."
|
|
59
|
+
)
|
|
60
|
+
if "__" not in name:
|
|
61
|
+
continue
|
|
62
|
+
|
|
63
|
+
parts = name.split("__")
|
|
64
|
+
if len(parts) == 2 and parts[1] == "in":
|
|
65
|
+
raise ValueError(
|
|
66
|
+
f"Invalid filter {name!r}: the '__in' suffix is not supported and "
|
|
67
|
+
"NetBox would ignore it. Pass a list to the field instead: "
|
|
68
|
+
f"{{'{parts[0]}': [1, 2, 3]}}"
|
|
69
|
+
)
|
|
70
|
+
if len(parts) == 2 and parts[1] in VALID_LOOKUP_SUFFIXES:
|
|
71
|
+
continue
|
|
72
|
+
raise ValueError(
|
|
73
|
+
f"Invalid filter {name!r}: multi-hop relationship lookups and unknown "
|
|
74
|
+
"suffixes are not supported. Use a direct filter such as 'site_id', or "
|
|
75
|
+
"query the related object first and filter by its ID. Valid suffixes: "
|
|
76
|
+
+ ", ".join(sorted(VALID_LOOKUP_SUFFIXES))
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _encode(value: Any) -> str:
|
|
81
|
+
"""Render a filter value the way NetBox's query-string parser expects."""
|
|
82
|
+
if isinstance(value, bool):
|
|
83
|
+
return "true" if value else "false"
|
|
84
|
+
if value is None:
|
|
85
|
+
return "null"
|
|
86
|
+
return str(value)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def encode_params(params: dict[str, Any], *, drop_none: bool = True) -> QueryPairs:
|
|
90
|
+
"""Encode query parameters, repeating the key for list values.
|
|
91
|
+
|
|
92
|
+
``{"site_id": [1, 2]}`` becomes ``site_id=1&site_id=2``, which is how NetBox
|
|
93
|
+
ORs multiple values of one filter.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
params: Parameter mapping.
|
|
97
|
+
drop_none: Drop ``None`` values (for optional tool arguments). When
|
|
98
|
+
False, ``None`` is sent as ``null`` - NetBox's "field is empty"
|
|
99
|
+
filter value, e.g. ``{"tenant_id": None}``.
|
|
100
|
+
|
|
101
|
+
Returns:
|
|
102
|
+
QueryPairs: Pairs ready to pass as httpx2 ``params``.
|
|
103
|
+
"""
|
|
104
|
+
encoded: QueryPairs = []
|
|
105
|
+
for key, value in params.items():
|
|
106
|
+
if value is None and drop_none:
|
|
107
|
+
continue
|
|
108
|
+
if isinstance(value, list | tuple | set):
|
|
109
|
+
encoded.extend((key, _encode(item)) for item in value)
|
|
110
|
+
else:
|
|
111
|
+
encoded.append((key, _encode(value)))
|
|
112
|
+
return encoded
|
netbox_mcp/models.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from typing import Literal
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
from pydantic import Field
|
|
5
|
+
from pydantic import model_validator
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class NetBoxConfig(BaseModel):
|
|
9
|
+
"""Configuration for NetBox API connection"""
|
|
10
|
+
|
|
11
|
+
netbox_url: str = Field(
|
|
12
|
+
...,
|
|
13
|
+
description="NetBox base URL, e.g. https://netbox.example.com",
|
|
14
|
+
)
|
|
15
|
+
token: str | None = Field(
|
|
16
|
+
None,
|
|
17
|
+
description="NetBox API token. v2 tokens (nbt_<key>.<token>) are sent as "
|
|
18
|
+
"'Bearer', legacy v1 tokens as 'Token'",
|
|
19
|
+
)
|
|
20
|
+
# Connection settings
|
|
21
|
+
verify_ssl: bool = Field(True, description="Verify SSL certificates (true/false)")
|
|
22
|
+
timeout: float = Field(
|
|
23
|
+
30,
|
|
24
|
+
gt=0,
|
|
25
|
+
description="Total time allowed for one request, retries included (seconds)",
|
|
26
|
+
)
|
|
27
|
+
branching_enabled: bool = Field(
|
|
28
|
+
False,
|
|
29
|
+
description="Enable netbox-branching support: branch tools and the 'branch' "
|
|
30
|
+
"argument (requires the netbox-branching plugin)",
|
|
31
|
+
)
|
|
32
|
+
default_branch: str | None = Field(
|
|
33
|
+
None,
|
|
34
|
+
description="Default netbox-branching branch (name or schema ID) for every call",
|
|
35
|
+
)
|
|
36
|
+
plugin_discovery: bool = Field(
|
|
37
|
+
False,
|
|
38
|
+
description="Discover plugin object types from /api/core/object-types/",
|
|
39
|
+
)
|
|
40
|
+
# Server behavior
|
|
41
|
+
read_only_mode: bool = Field(False, description="Read-only mode (true/false)")
|
|
42
|
+
dry_run_mode: bool = Field(
|
|
43
|
+
False,
|
|
44
|
+
description="Dry-run mode: write tools only simulate changes (true/false)",
|
|
45
|
+
)
|
|
46
|
+
disabled_tags: set[str] = Field(
|
|
47
|
+
default_factory=set, description="Set of tags to disable tools for"
|
|
48
|
+
)
|
|
49
|
+
response_max_size: int | None = Field(
|
|
50
|
+
None,
|
|
51
|
+
ge=1,
|
|
52
|
+
description="Maximum size of a tool response in bytes (unset = unlimited)",
|
|
53
|
+
)
|
|
54
|
+
# Rate limiting
|
|
55
|
+
rate_limit_enabled: bool = Field(
|
|
56
|
+
False, description="Enable rate limiting (true/false)"
|
|
57
|
+
)
|
|
58
|
+
rate_limit_max_requests: int = Field(60, description="Maximum requests per window")
|
|
59
|
+
rate_limit_window_minutes: int = Field(
|
|
60
|
+
1, description="Rate limit window in minutes"
|
|
61
|
+
)
|
|
62
|
+
# Tool search transform
|
|
63
|
+
tool_search_enabled: bool = Field(
|
|
64
|
+
False, description="Enable FastMCP tool-search transform (true/false)"
|
|
65
|
+
)
|
|
66
|
+
tool_search_strategy: Literal["bm25", "regex"] = Field(
|
|
67
|
+
"bm25",
|
|
68
|
+
description="Tool search strategy: 'bm25' (natural language) or 'regex'",
|
|
69
|
+
)
|
|
70
|
+
tool_search_max_results: int = Field(
|
|
71
|
+
5,
|
|
72
|
+
ge=1,
|
|
73
|
+
description="Maximum number of tools returned by search_tools",
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
@model_validator(mode="after")
|
|
77
|
+
def _branch_needs_branching(self) -> "NetBoxConfig":
|
|
78
|
+
# A default branch with branching off would be silently ignored, sending
|
|
79
|
+
# every change to main while the operator believes it is staged.
|
|
80
|
+
if self.default_branch and not self.branching_enabled:
|
|
81
|
+
raise ValueError(
|
|
82
|
+
"NETBOX_BRANCH is set but branching is disabled - set "
|
|
83
|
+
"NETBOX_BRANCHING_ENABLED=true (requires the netbox-branching plugin)"
|
|
84
|
+
)
|
|
85
|
+
return self
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class TransportConfig(BaseModel):
|
|
89
|
+
"""Configuration for MCP transport layer"""
|
|
90
|
+
|
|
91
|
+
transport_type: str = Field(
|
|
92
|
+
"stdio",
|
|
93
|
+
description="Transport type: 'stdio', 'sse' (Server-Sent Events), or 'http' (HTTP Streamable)",
|
|
94
|
+
)
|
|
95
|
+
# HTTP transport settings (for both SSE and HTTP Streamable)
|
|
96
|
+
http_host: str = Field(
|
|
97
|
+
"127.0.0.1",
|
|
98
|
+
description="Host to bind for HTTP transports (SSE/HTTP Streamable)",
|
|
99
|
+
)
|
|
100
|
+
http_port: int = Field(
|
|
101
|
+
8000, description="Port to bind for HTTP transports (SSE/HTTP Streamable)"
|
|
102
|
+
)
|
|
103
|
+
http_bearer_token: str | None = Field(
|
|
104
|
+
None, description="Bearer token for HTTP authentication"
|
|
105
|
+
)
|