Overview
gravitywell is a typed Python client for Pi-hole's v6 REST API, in sync and async versions.
It handles the session for you, so you can get straight to work: pause blocking for five minutes, add an allow rule, or pull the last hour of blocked queries from one device. Answers come back as Pydantic models your editor can autocomplete.
It's built for AI agents too. Its methods double as tools with descriptions written for a model, and the insights tools answer questions like "why is this domain blocked?" in a single call. gravitywell-mcp serves them to Claude and other MCP clients, read-only unless you allow more.
Install
pip install gravitywell
For the MCP server, install the mcp extra: pip install "gravitywell[mcp]".
Using an AI coding assistant? Point it at llms.txt, a compact guide to gravitywell for models.
Quickstart
Create an application password in the Pi-hole's web interface, under Settings › Web interface / API, and put it in the PIHOLE_PASSWORD environment variable:
import os
from gravitywell import PiHole, PiHoleAuth
auth = PiHoleAuth(os.environ["PIHOLE_PASSWORD"]) # an application password
with PiHole("http://pi.hole", auth=auth) as api:
summary = api.stats.summary() # GET /api/stats/summary -> SummaryResponse
print(f"{summary.queries.percent_blocked:.1f}% of queries blocked in the last 24 hours")
status = api.dns.blocking() # GET /api/dns/blocking -> BlockingResponse
print(f"Blocking is {status.blocking}") # enabled
api.dns.set_blocking(False, timer=300) # pause blocking for five minutes
why = api.explain_domain("ads.example.com") # rules, lists and recent queries in one call
for rule in why.rules:
print(f"{rule.type} rule ({rule.kind}): {rule.domain}")
- To keep the URL and password out of your scripts, save them once with
gravitywell setup and load them with gravitywell.config.load_config(), as Saved Pi-holes explains.
- The client logs in on its first request, reuses that session, and logs out when the
with block ends: a Pi-hole allows only a few sessions at a time.
- Each method returns a Pydantic model of the Pi-hole's answer, which your editor and type checker know field by field.
- A
4xx or 5xx answer raises httpx2.HTTPStatusError, with the Pi-hole's reason in its response.
- For async code, use
AsyncPiHole and await each method, as Async clients explains.
Use it from Claude
Save your Pi-hole once. gravitywell setup asks for its URL and an application password, logs in to check them, and saves them where only you can read them:
uvx gravitywell setup
Then add the server to Claude Code:
claude mcp add --scope user pihole -- \
uvx --from "gravitywell[mcp]" gravitywell-mcp --pihole primary
Ask "is my Pi-hole healthy?", "why is this site blocked?" or "which device keeps asking for this domain?". It serves read-only tools until you add --allow-writes or --allow-destructive. MCP server covers Claude Desktop and other MCP clients, and choosing which tools to serve.
Features
- The whole API. Blocking, domain rules, lists, groups and clients, search, statistics, activity history, the query log, settings, DHCP, the network, diagnosis, logs, actions, sessions, and Teleporter backups.
- Typed throughout. Pydantic v2 models for every answer,
Literal types for fixed sets of values, and times as datetime objects in UTC. The package type-checks in pyright's strict mode.
- Sessions handled. One login per client, shared by concurrent requests, renewed once when it expires, sent in a header rather than the URL, and logged out on close.
- Sync and async.
PiHole and AsyncPiHole have the same methods, built on httpx2.
- AI tools. Every endpoint is also a tool for a model, with descriptions written for one, JSON Schemas, safety hints, and errors it can act on. The insights tools answer common questions in one call: why a domain is blocked, who a device is, and what's wrong with the Pi-hole.
- An MCP server.
gravitywell-mcp serves the tools to Claude and other MCP clients, read-only by default, and only the areas you choose.
- Saved Pi-holes.
gravitywell setup saves a primary Pi-hole, and optionally a secondary, which load_config() loads and gravitywell-mcp --pihole serves.
- Guard rails. It won't delete the Default group, change settings that could lock you out, hand a model the session or the password hashes, or let a misspelled setting slip past silently, as Pi-hole would.
Guide
- Connecting to a Pi-hole: the URL, application passwords, sessions, HTTPS certificates, and
app_sudo.
- Async clients:
AsyncPiHole, concurrent requests and streaming.
- Blocking and allowing: pausing blocking, domain rules, lists, groups and clients, and why a domain is blocked.
- Settings: local DNS records, upstream servers, reverse servers, static leases, and other settings.
- Statistics and the query log: totals, top lists, time ranges, and the query log.
- Health and maintenance: health checks, logs, actions, the network, sessions, and backups.
- Errors and Testing.
- AI tools and the MCP server.
- API reference: every method, the endpoint it calls, and how its tool is marked.
Compatibility
gravitywell supports Pi-hole v6, whose REST API replaced v5's interface, on Python 3.12 and newer. It was modeled on, and tested against, Pi-hole core v6.6, web v6.6.1 and FTL v6.9, which gravitywell.PIHOLE_VERSIONS records. Pi-hole compatibility covers newer releases, and the few endpoints gravitywell leaves out.
Project status
gravitywell is pre-1.0, so its API may change between minor versions. Changes are listed in the changelog. It's maintained in spare time: issues and pull requests are welcome, and CONTRIBUTING.md explains how to set up and check a change.
Acknowledgments
gravitywell is built on ClientX, httpx2 and Pydantic, and its MCP server on FastMCP.
gravitywell is an independent, third-party project. It isn't made, endorsed or supported by the Pi-hole project, and uses the name "Pi-hole" only to say what it works with. Pi-hole® is a registered trademark of Pi-hole LLC.
License
gravitywell is released under the MIT License.