Skip to main content

IP allowlist guard for MCP servers. Ships with OpenAI/ChatGPT egress IP ranges and Azure public cloud ranges for ChatGPT developer mode. Zero dependencies.

Project description

listo-mcp-ip-guard

IP allowlist guard for Python MCP servers. Ships with OpenAI/ChatGPT egress IP ranges and Microsoft Azure public cloud ranges for ChatGPT developer mode. Zero production dependencies.

Install

pip install listo-mcp-ip-guard

Quick Start

from mcp_ip_guard import create_ip_guard

# Creates a guard with all OpenAI/ChatGPT IPs pre-loaded
guard = create_ip_guard()

# Check a raw IP
if guard.is_allowed("52.173.123.5"):
    print("Allowed")

Options

guard = create_ip_guard(
    # Include OpenAI/ChatGPT egress IPs (default: True)
    include_openai_ranges=True,

    # Include Azure public cloud IPs for ChatGPT developer mode (default: False)
    include_azure_ranges=False,

    # Add your own IPs/CIDR ranges
    additional_ranges=[
        "10.0.0.0/8",        # CIDR range
        "192.168.1.100",     # Single IP (treated as /32)
    ],

    # Allow localhost in non-production (default: True)
    allow_localhost_in_dev=True,

    # Number of trusted reverse proxies (default: 1)
    # Uses X-Forwarded-For[-depth] to extract the client IP
    trusted_proxy_depth=1,

    # Log blocked IPs (default: False)
    debug=False,

    # Hook for telemetry / custom logging
    on_blocked=lambda ip, path: print(f"Blocked {ip} on {path}"),
)

API

create_ip_guard(**options) -> IpGuard

Creates a new guard instance.

guard.is_allowed(ip: str) -> bool

Check if a raw IP address string is in the allowlist.

guard.get_client_ip(scope) -> str

Extract the client IP from an ASGI scope dict or Starlette/FastAPI Request. Uses X-Forwarded-For[-trusted_proxy_depth] to select the IP added by the outermost trusted proxy.

guard.get_client_ip_from_headers(remote_addr, x_forwarded_for, trusted_proxy_depth=1) -> str

Extract client IP from raw header values. Static method.

guard.check_request(client_ip, path) -> GuardResult

Check if a client IP is allowed. Fires the on_blocked callback if denied. Returns GuardResult(allowed=bool, client_ip=str).

guard.range_count -> int

Total number of parsed CIDR ranges in the allowlist.

OPENAI_IP_RANGES

The raw tuple of OpenAI/ChatGPT egress IP ranges in CIDR notation.

AZURE_IP_RANGES

Microsoft Azure public cloud IPv4 ranges (10,360 CIDRs). Lazy-loaded on first access.

ASGI Middleware (Starlette / FastAPI)

from starlette.applications import Starlette
from mcp_ip_guard.middleware import IpGuardMiddleware

app = Starlette(...)
app.add_middleware(
    IpGuardMiddleware,
    protected_paths=["/mcp", "/mcp/messages"],
)

With Azure ranges for developer mode:

app.add_middleware(
    IpGuardMiddleware,
    protected_paths=["/mcp", "/mcp/messages"],
    include_azure_ranges=True,
)

The middleware guards both HTTP and WebSocket connections on the specified protected_paths. All other paths pass through unblocked. Paths are normalized (trailing slashes stripped) before matching.

With multi-proxy setup (e.g. CDN -> Load Balancer -> App):

app.add_middleware(
    IpGuardMiddleware,
    protected_paths=["/mcp", "/mcp/messages"],
    trusted_proxy_depth=2,  # skip the inner proxy's XFF entry
)

Usage with Python MCP Server

from mcp.server.fastmcp import FastMCP
from mcp_ip_guard import create_ip_guard

guard = create_ip_guard(
    debug=True,
    on_blocked=lambda ip, path: print(f"Blocked {ip} on {path}"),
)

mcp = FastMCP("my-server")

# In your custom HTTP handler or middleware:
client_ip = guard.get_client_ip(request)
result = guard.check_request(client_ip, request.url.path)
if not result.allowed:
    return JSONResponse({"error": "Access denied"}, status_code=403)

ChatGPT Developer Mode

When connecting an MCP server directly to ChatGPT in developer mode, requests may come from Azure infrastructure IPs rather than the dedicated OpenAI egress IPs. Enable include_azure_ranges to allow these:

guard = create_ip_guard(include_azure_ranges=True)

This adds ~10,360 Azure IPv4 CIDR ranges to the allowlist. Only enable this when you need developer-mode compatibility — in production with ChatGPT's public integration, the default OpenAI ranges are sufficient.

Reverse Proxy Configuration

The guard extracts the client IP from the X-Forwarded-For header using trusted_proxy_depth to select the correct entry. The default (1) works for single-proxy deployments (e.g. Railway, Cloudflare, or a single load balancer).

Deployment Depth XFF example Extracted IP
Single proxy (Cloudflare -> App) 1 client, cloudflare cloudflare entry = client IP added by Cloudflare
Two proxies (CDN -> LB -> App) 2 client, cdn, lb cdn entry = client IP added by CDN
Direct (no proxy) 1 (empty) Falls back to remote_addr

Set trusted_proxy_depth to match the number of trusted reverse proxies in front of your application. An incorrect value means the guard checks the wrong IP.

Environment

  • ENVIRONMENT (or NODE_ENV as fallback) — When set to "production", localhost is blocked (unless allow_localhost_in_dev=False).

IP Ranges Sources

  • OpenAI — Published egress IPs (2026-02-21). Includes /28, /26, and /32 entries covering all ChatGPT outbound traffic to MCP servers.
  • Azure — Microsoft Azure Service Tags – Public Cloud (2026-03-02). The AzureCloud service tag with 10,360 IPv4 CIDR ranges covering all Azure datacenter egress.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

listo_mcp_ip_guard-0.2.0.tar.gz (45.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

listo_mcp_ip_guard-0.2.0-py3-none-any.whl (44.0 kB view details)

Uploaded Python 3

File details

Details for the file listo_mcp_ip_guard-0.2.0.tar.gz.

File metadata

  • Download URL: listo_mcp_ip_guard-0.2.0.tar.gz
  • Upload date:
  • Size: 45.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for listo_mcp_ip_guard-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5cc232f865d7bea4b8675bf1609b0e2e4235c6e1f65d753f0042e455f0895d53
MD5 1d65945f5138f776aa961e3285c6e204
BLAKE2b-256 59bf8cbdab54548fa7c564fa721f15b27349549544a35b6e925e35829b318bdd

See more details on using hashes here.

Provenance

The following attestation bundles were made for listo_mcp_ip_guard-0.2.0.tar.gz:

Publisher: publish.yml on Listo-Labs-Ltd/mcp-ip-guard-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file listo_mcp_ip_guard-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for listo_mcp_ip_guard-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a7889f665f534f0490acfb848291238f5a1393182e3b5d75419c3a8340f9b717
MD5 7861f323356c0a038d5a5e65a9c12d32
BLAKE2b-256 036dd9e1ac2fbb7259f3cbd9e5d5eab8be18e469cd2a8dc7afc1215f6605e2e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for listo_mcp_ip_guard-0.2.0-py3-none-any.whl:

Publisher: publish.yml on Listo-Labs-Ltd/mcp-ip-guard-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page