Metadata-Version: 2.4
Name: redis-context-gateway
Version: 1.1.0
Summary: Redis Context Gateway REST and MCP SDK
License: MIT
License-File: LICENSE
Author: Redis AI Services Team
Requires-Python: >=3.10
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Requires-Dist: httpcore (>=1.0.9)
Requires-Dist: httpx (>=0.28.1,<1)
Requires-Dist: httpx2 (>=2.10.0,<3)
Requires-Dist: mcp (>=2.3.0,<3)
Requires-Dist: pydantic (>=2.11.2)
Project-URL: Repository, https://github.com/redis/iris-sdks.git
Description-Content-Type: text/markdown

# redis-context-gateway

Python SDK for Redis Context Gateway. One package holds both clients:

- `ContextGatewayService` is the control-plane client for Gateway and Upstream administration. Speakeasy generates it from the Gateway control-plane API, the same way as the other Iris SDKs, into `redis_context_gateway.controlplane`, which also holds its `models`, `errors` and HTTP client types.
- `ContextGateway` is the data-plane client in `redis_context_gateway.mcp`. It connects agents to a Gateway's MCP endpoint with the official `mcp` library.

Both are exported from `redis_context_gateway`. Each loads on first use, so a control-plane script never imports the MCP library and an agent never imports the REST client. The data plane doesn't import the control plane at all.

On PyPI as `redis-context-gateway`, generated from `redislabsdev/iris@35cdb1ca6` (iris `main`, the unified Iris API). Python 3.10 or later.

## Control plane

```python
from redis_context_gateway import ContextGatewayService

with ContextGatewayService(server_url=control_plane_url, admin_token=admin_token) as service:
    gateway = service.gateways.create(name="Docs", tool_exposure_mode="SEARCH")
    upstream = service.upstreams.create(
        gateway_id=gateway.gateway_id,
        kind="MCP",
        name="Docs MCP",
        namespace="docs",
        mcp={
            "endpoint": upstream_mcp_url,
            "authentication": {"methods": [{"bearer": {"value": upstream_token}}]},
        },
    )
```

Models and errors come from the control plane: `from redis_context_gateway.controlplane import errors, models`. Every product call sends the admin token. The operational health probes (`service.health.live()`, `service.health.ready()`) and public service probes (`service.health.service_live()` and `service.health.service_ready()`) send no credential. Async variants end in `_async`. See USAGE.md and docs/ for every operation and model.

An oversized REST request returns the typed `PayloadTooLargeErrorResponseContent` error for HTTP 413, with the server's problem details. Catch it from `redis_context_gateway.controlplane.errors`.

An Upstream has a `kind` and exactly one settings object named after it: `mcp` (`endpoint`, `transport`, `authentication`) for an MCP Upstream or `context_surface` (`database_id`, `data_model`) for a Context Surface Upstream. The control plane refuses a request whose `kind` and settings object disagree. Reads return the same object. A Context Surface Upstream reports `context_surface.database_binding`, and a Gateway reports `database_binding`. Each holds a `database_id` and a `name`, and `name` is `None` when the registry lookup fails, so print it with a fallback:

```python
binding = gateway.database_binding
label = binding.name or f"database {binding.database_id} (name unavailable)" if binding else "no database"
```

Omit `database_id` on Gateway creation when no database is selected; do not send JSON `null`. On-prem deployments can supply a default database. A Context Surface still needs a database, either explicitly selected or supplied by the deployment.

The client sends `database_id` exactly as given and leaves the verdict to the control plane, so an empty string or an unknown ID comes back as a 400 that names the field. `gateways.update(database_id=None)` sends JSON `null`, which clears the database, and the control plane decides whether that is allowed. On `gateways.create` the schema types `database_id` as a plain string, so `None` means omitted and nothing is sent. The TypeScript SDK refuses `null` on create instead. Both SDKs run the same table of cases from `tests/context-gateway/parity/control-plane-cases.json`.

### Context Surface data model

A Context Surface Upstream reads a Redis database through the data model you give it. `redis_context_gateway.datamodel` types that model with `TypedDict` classes that follow the iris data model, so a type checker catches a misspelled key or an unknown index type. They are plain dicts at run time. The control plane validates the model and names each refused member by its path under `contextSurface.dataModel`.

```python
from redis_context_gateway.datamodel import DataModel

data_model: DataModel = {
    "title": "Healthcare scheduling",
    "description": "Clinics and the providers who work at them",
    "entities": [
        {
            "name": "Location",
            "description": "A medical facility where providers see patients",
            "redis_key_template": "location:{id}",
            "fields": [
                {"name": "id", "type": "str", "description": "Location ID", "is_key_component": True, "redis_indices": []},
                {"name": "name", "type": "str", "description": "Facility name", "redis_indices": [{"type": "text", "weight": 1.0}]},
                {"name": "city", "type": "str", "description": "City", "redis_indices": [{"type": "tag"}]},
            ],
            "relationships": [],
        },
        {
            "name": "Provider",
            "description": "A clinician who sees patients at one location",
            "redis_key_template": "provider:{id}",
            "fields": [
                {"name": "id", "type": "str", "description": "Provider ID", "is_key_component": True, "redis_indices": []},
                {"name": "specialty", "type": "str", "description": "Specialty", "redis_indices": [{"type": "tag"}]},
                {"name": "location_id", "type": "str", "description": "Home location", "redis_indices": [{"type": "tag"}]},
            ],
            "relationships": [
                {"name": "works_at", "target": "Location", "description": "The provider's home location", "source_field": "location_id"}
            ],
        },
    ],
    "entity_count": 2,
}

surface = service.upstreams.create(
    gateway_id=gateway.gateway_id,
    kind="CONTEXT_SURFACE",
    name="Scheduling",
    context_surface={"database_id": database_id, "data_model": data_model},
)
```

`tests/context-gateway/healthcare-datamodel.json` is a complete six-entity model that passes the control plane's validation. `acl_field_mappings` is not in the type because the control plane refuses it as not supported yet. The compact JSON of a model is at most 1 MiB.

## Agents (MCP)

```python
from redis_context_gateway import ContextGateway

client = ContextGateway(gateway.mcp_endpoint, agent_key)
async with client.connect() as session:
    found = await session.call_tool("search_tools", {"query": "find an issue"})
    result = await session.call_tool(
        "run_tool", {"tool": {"upstreamId": upstream.upstream_id, "originalName": "get_issue"}, "toolArgs": {"id": 42}}
    )
```

`client.connect()` opens a negotiated official `mcp.ClientSession`, so external instrumentation can observe the session. Each call opens a new session. The constructor checks the URL, so an unsafe one fails before any request. Search tools, `run_tool` and the handle tools are ordinary tool calls. A tool failure comes back as a result with `is_error` set to `True`, not an exception. Result fields are snake_case attributes in `mcp` 2, such as `structured_content`.

When the Gateway refuses a request with HTTP 401, 403 or 404, the client raises `redis_context_gateway.mcp.GatewayHTTPError`. Its `status` attribute is the HTTP status: 401 means the credential wasn't accepted, 403 that it has no grant for this Gateway, and 404 that no Gateway answers at the URL. `mcp` 2 would otherwise report these as a JSON-RPC error without the status. The error carries no response body and never the credential. It comes out of the `async with client.connect()` block, from connecting or from the call that got it, so catch it around the block. Any other error status, and a JSON-RPC error such as an unknown direct `tools/call` name, is an `mcp.MCPError`.

A host that can't be reached raises `redis_context_gateway.exceptions.GatewayConnectionError`, and a timeout raises `GatewayTimeoutError`. Both subclass `GatewayTransportError`, carry the URL in `url` and name it in the message. The control-plane client raises the same two classes for the same failures, so one `except GatewayTransportError` covers both planes. These errors come out as themselves. No `ExceptionGroup` leaves `connect()`, and an error from your own code inside the `async with` block is not wrapped in one. An empty `agent_key` raises `ValueError` from the constructor.

```python
from redis_context_gateway.mcp import GatewayHTTPError

try:
    async with client.connect() as session:
        result = await session.call_tool("search_tools", {"query": "find an issue"})
except GatewayHTTPError as error:
    if error.status == 401:
        ...  # get a new key or token
```

The credential is either of these, and both go in the `agent_key` argument:

- An Identity Service agent key with a `context-gateway` / `gateway` / `use` grant for the Gateway.
- An access token from the deployment's identity provider, when the deployment has IdP sign-in on. The Gateway still answers 403 when the signed-in caller has no access grant for it.

The client sends the value unchanged as `Authorization: Bearer`, the only place the Gateway accepts an access token. It doesn't sign users in or refresh tokens. To find the identity provider, read the Gateway's protected resource metadata (RFC 9728) without a credential at `/.well-known/oauth-protected-resource/iris/data/v1/context-gateway/gateways/{gatewayId}/mcp` on the data-plane host, or take the `resource_metadata` URL from the `WWW-Authenticate` challenge of a 401. Its `authorization_servers` names the issuer and `scopes_supported` the scopes to request. When a token expires, build a new client with a fresh one.

The Gateway negotiates MCP protocol `2026-07-28`, `2025-11-25`, `2025-06-18` and `2025-03-26`. The SDK requires `mcp>=2.3,<3`. A session asks for `2026-07-28` with `server/discover`, so that is the version it uses with the Gateway, and it sends that version on every later request. A server that answers `server/discover` with an error gets the `initialize` handshake instead, which offers `2025-11-25` and also accepts `2025-06-18` or `2025-03-26`.

MCP reads default to a 300-second inactivity timeout, with 30-second connect, write and connection-pool budgets. Pass `timeout=10` to use ten seconds for every HTTP timeout, or `timeout=httpx2.Timeout(30, read=600)` for a separate read budget. `mcp` 2 uses the `httpx2` package, so the timeout and the test `transport` are `httpx2` types. These are HTTP inactivity limits, not a total tool-call deadline. REST timeout defaults are unchanged.

In full mode, use the tool names returned by discovery unchanged: a namespace such as `docs` and tool `search` appear as `docs__search`. The service enforces a 32-character namespace limit; generated SDK models accept a plain string and leave this validation to the service. Search-mode calls continue to identify tools by `upstreamId` and `originalName`.

An unknown or inaccessible target passed to `run_tool` or `run_tool_as_handle` returns a tool error result with `is_error=True`, `unknown_tool`, and `not_dispatched`. The MCP SDK preserves this result instead of raising a transport exception.

## Handles

A Gateway can hold a large tool result and answer with a small handle stub instead. The agent then reads the result in pieces through seven tools:

| Tool | What it does |
| --- | --- |
| `run_tool_as_handle` | Runs one tool and holds its result whatever its size |
| `page_results` | Reads a held result in order, by item for a collection or by byte for text |
| `query_results` | Selects from a held JSON result with an RFC 9535 JSONPath |
| `count_results` | Counts items or JSONPath selections, optionally grouped by one `groupBy` field |
| `aggregate_results` | Reduces the numbers a JSONPath selects: count, sum, min, max and avg |
| `describe_result` | Describes a held result again: shape, size, fields, completeness, source and expiry |
| `release_results` | Deletes a held result before it expires |

The Gateway lists them in both exposure modes when the data plane runs with `compression.enabled: true`. Handles are held in the Gateway's database, so the Gateway also needs a `databaseId`. Without one, automatic capture returns the ordinary result whole; explicit `run_tool_as_handle` capture returns a tool error, and handle reads return an unknown-handle error.

`count_results` accepts `jsonpath` and `groupBy` together: for example, `{"handle": "h_...", "jsonpath": "$.issues[*]", "groupBy": "/user/login"}` groups the selected issues. A selected array contributes its elements; a selected null remains a node. Field forms include `user.login`, `/user/login`, and `$.user.login`. Omit optional arguments when unused. Counts cover the captured items; check `completeness` and `warning` before treating them as whole-match totals.

`run_tool_as_handle` takes the arguments of the call it wraps. In full mode that is `{"name": <tool name from tools/list>, "toolArgs": {...}}`. In search mode it is `{"tool": {"upstreamId": ..., "originalName": ...}, "toolArgs": {...}}`, as `run_tool` takes. The other tools take a `handle`, an ID that matches `^h_[0-9a-f]{32}$`. Explicit capture failures remain ordinary MCP results with `is_error=True` and their failure metadata; the client does not turn them into exceptions.

```python
from redis_context_gateway.mcp import HANDLE_META_KEY, PAGE_RESULTS, RELEASE_RESULTS, RUN_TOOL_AS_HANDLE

async with client.connect() as session:
    stub = await session.call_tool(
        RUN_TOOL_AS_HANDLE,
        {"tool": {"upstreamId": upstream.upstream_id, "originalName": "list_issues"}, "toolArgs": {}},
    )
    held = (stub.meta or {}).get(HANDLE_META_KEY)
    if held is not None:
        try:
            page = await session.call_tool(PAGE_RESULTS, {"handle": held["handle"], "offset": 0, "limit": 10})
        finally:
            # Release even if paging fails, or the result stays in the Gateway database until its TTL.
            await session.call_tool(RELEASE_RESULTS, {"handle": held["handle"]})
```

A stub carries the envelope as `structuredContent` and JSON text, and the same envelope without its preview and next step under `_meta["com.redis.context-gateway/handle"]`. An ordinary tool call can fall back to its whole result when automatic capture fails, so check for that key before reading a stub. An explicit `run_tool_as_handle` call instead returns a tool error when storage cannot create the handle, including an out-of-memory refusal. An unknown, expired, released or another caller's handle is a tool error result, not an exception. Handles are bound to the caller and the tenant.

`redis_context_gateway.mcp` exports the tool names (`RUN_TOOL_AS_HANDLE`, `PAGE_RESULTS`, `QUERY_RESULTS`, `COUNT_RESULTS`, `AGGREGATE_RESULTS`, `DESCRIBE_RESULT`, `RELEASE_RESULTS`, and all seven in `HANDLE_TOOLS`), `HANDLE_META_KEY` and `HANDLE_ID_PATTERN`. Check an ID with `re.fullmatch(HANDLE_ID_PATTERN, value)`, because a Python `$` also matches before a trailing newline.

## Examples

`examples/` builds two agents, on OpenAI function calling, over a Redis fleet inventory kept in Redis, each on its own Gateway: a Redis helpdesk chat agent (full mode, a Context Surface plus the public Redis docs MCP server, tool cache) and a scheduled capacity planner (search mode, handles). An admin script creates both Gateways. Its README has the run order.

## Moving to 1.0.0b1

1.0.0b1 follows the unified Iris API (`/iris/v1/context-gateway/...` and `/iris/data/v1/context-gateway/...`).

| Before | 1.0.0b1 |
| --- | --- |
| `display_name=` | `name=` (1-128 characters) |
| `gateway.id`, `upstream.id` | `gateway.gateway_id`, `upstream.upstream_id`; IDs are `gw_` / `up_` plus 32 lowercase hex characters |
| `tool_exposure_mode="search"`, `kind="mcp"`, `kind="context-surface"` | `"SEARCH"` / `"FULL"`, `"MCP"`, `"CONTEXT_SURFACE"`; every enum value is UPPER_SNAKE_CASE |
| `next_token` / `max_results` | `cursor` / `limit` (default 50, at most 200); lists return `items` and `next_cursor` |
| RFC 9728 metadata at `/.well-known/oauth-protected-resource/v1/gateways/{gatewayId}/mcp` | `/.well-known/oauth-protected-resource/iris/data/v1/context-gateway/gateways/{gatewayId}/mcp` |

## Migrating from 0.1.0a4

0.1.0a5 names the data-plane client after the product, as `Playbook` and `Lens` do. There is no alias for the old name.

| 0.1.0a4 | 0.1.0a5 |
| --- | --- |
| `from redis_context_gateway import ContextGatewayClient` | `from redis_context_gateway import ContextGateway` |
| `ContextGatewayClient(url, agent_key).connect()` | `ContextGateway(url, agent_key).connect()` |

`ContextGatewayService` is unchanged.

## Migrating from 0.1.0a3

0.1.0a4 moves the MCP client to `mcp` 2.

| 0.1.0a3 | 0.1.0a4 |
| --- | --- |
| `mcp>=1.30,<2` | `mcp>=2.3,<3` |
| `timeout=httpx.Timeout(...)`, `transport=httpx.MockTransport(...)` | `timeout=httpx2.Timeout(...)`, `transport=httpx2.MockTransport(...)` |
| `result.isError`, `result.structuredContent` | `result.is_error`, `result.structured_content` |
| `except httpx.HTTPStatusError` for 401, 403 and 404 | `except GatewayHTTPError` around the `connect()` block, reading `error.status` |

## Migrating from 0.1.0a1

0.1.0a2 renames the clients so that, following the naming adopted in the ADR-0003 amendment, `<Product>Service` is the control-plane client and `<Product>Client` is the data-plane client.

| 0.1.0a1 | 0.1.0a2 |
| --- | --- |
| `ContextGatewayClient(server_url=..., admin_token=...)` (control plane) | `ContextGatewayService(server_url=..., admin_token=...)` |
| `from redis_context_gateway.mcp import connect` (removed after 0.1.0a2) | `from redis_context_gateway import ContextGatewayClient` |
| `async with connect(url, agent_key) as session:` | `async with ContextGatewayClient(url, agent_key).connect() as session:` |
| `connect(url, agent_key, timeout=..., transport=...)` | `ContextGatewayClient(url, agent_key, timeout=..., transport=...).connect()` |
| `from redis_context_gateway import models` (or `redis_context_gateway.models`) | `from redis_context_gateway.controlplane import models` |
| `from redis_context_gateway import errors` (or `redis_context_gateway.errors`) | `from redis_context_gateway.controlplane import errors` |
| Other generated names at the root, such as `HttpClient`, `AsyncHttpClient`, `RetryConfig`, `SDKConfiguration` and `USER_AGENT` | The same names from `redis_context_gateway.controlplane`. `VERSION` and `__version__` stay at the root |

The old `ContextGatewayClient` name now means the data-plane client, so code that builds it with `server_url=` fails with a `TypeError`. Rename those calls to `ContextGatewayService`. `redis_context_gateway.mcp.connect` was deprecated in 0.1.0a2 and is removed in the next alpha, so importing it fails with `ImportError`. The old root modules (`models`, `errors`, `utils`, `types`, `sdk` and the rest of the generated code) are gone, so those imports fail with `ImportError` until they import from `redis_context_gateway.controlplane`. Operations, models, errors and their arguments are otherwise unchanged.

## Safety

- Neither client follows redirects, so the admin token and agent keys stay on the Gateway.
- Plain HTTP is refused unless the host is loopback (localhost, 127.0.0.0/8 or ::1).
- Each request must match its own operation's method, path and query names, so an ID like `../../v1/gateways` fails before anything is sent. A custom HTTP client that can't turn redirects off is refused with a `TypeError`.
- The SDK never reads credentials from environment variables. Pass them in explicitly. The `redis-iris` CLI and goal objects own `IRIS_CONTEXT_GATEWAY_*` settings and the `[context_gateway]` config table.
- Upstream secrets are write-only: no response model carries them.
- Generated payload debugging is disabled, even when `debug_logger` is supplied, because it would log request secrets. Application logging and custom HTTP transports are outside this guarantee. Do not log whole generated exceptions: validation diagnostics can retain supplied credentials, and response errors retain server-provided bodies.
- No call is retried automatically.

## Development

```sh
uv venv && uv pip install -e . pytest pytest-asyncio ruff
.venv/bin/python -m pytest tests
```

Speakeasy generates the control plane straight into `src/redis_context_gateway/controlplane/` (`moduleName: redis_context_gateway.controlplane` in `.speakeasy/gen.yaml`). Hand-written files, protected by `.genignore`, are the package root (`src/redis_context_gateway/__init__.py` and `py.typed`), the data plane (`src/redis_context_gateway/mcp.py`), the data model types (`src/redis_context_gateway/datamodel.py`), the control-plane hooks (`src/redis_context_gateway/controlplane/_hooks/registration.py`), `tests/`, `examples/` and this README. Everything else is regenerated by `speakeasy run -t context-gateway-python-sdk` from the repository root, or by the `sdk_generation_context_gateway_python_sdk.yaml` workflow.

