Skip to main content

Python client library for the Journal Gateway protocol

Project description

journal-gateway-client

Python client library for the Journal Gateway protocol. Runs a WebSocket server that gateways connect to, authenticates them, auto-pulls their tools and skills, and lets you call tools.

Install

pip install journal-gateway-client

Usage

import asyncio
from journal_gateway_client import GatewayServer, TokenValidationResult


async def validate_token(token: str) -> TokenValidationResult | None:
    # Return a TokenValidationResult on success, None on failure
    if token == "gw_valid":
        return TokenValidationResult(organization_id="org_123")
    return None


async def main() -> None:
    server = GatewayServer(validate_token=validate_token, port=8080)

    server.on_gateway_connected = lambda gw: print("connected:", gw.id, gw.integrations)
    server.on_gateway_updated = lambda gw: print("tools/skills changed:", gw.id)
    server.on_gateway_disconnected = lambda gw: print("disconnected:", gw.id)

    await server.start()

    # Call a tool on a connected gateway
    result = await server.call_tool("postgresql", "query", {"sql": "SELECT 1"})
    print(result.content)

    await server.stop()


asyncio.run(main())

Key APIs

  • start() / stop() — lifecycle
  • call_tool(integration_id, tool_name, arguments, timeout=60.0) — execute a tool call on any gateway that provides the integration
  • call_tool_for_org(organization_id, integration_id, tool_name, arguments, timeout=90.0) — same, scoped to an organization with automatic load balancing and retry on a different gateway
  • get_tools_for_org(organization_id) — list deduplicated tools across all gateways for an org
  • get_versions(gateway_id) / get_tools(gateway_id) / get_skills(gateway_id) — explicit pulls from a specific gateway
  • connected_gateways — all currently connected gateways

Callbacks

Set these attributes on the server instance:

  • on_gateway_connected(gateway) — fired after a gateway authenticates and its initial tools/skills are pulled
  • on_gateway_updated(gateway) — fired when a gateway's tools or skills change at runtime
  • on_gateway_disconnected(gateway) — fired when a gateway disconnects

Telemetry

The library has no telemetry dependency of its own. Two constructor arguments let you wire it into your logging/tracing stack:

  • get_trace_context — called on every call_tool. Return the active W3C trace context as {"traceparent": ..., "tracestate": ...} and it is propagated on the tool_call message; the gateway parents its gateway.tool_call span onto it, so the remote tool execution appears in your distributed trace. Return None when there is no active span.
  • on_socket_error(error, gateway) — called when a gateway socket drops abnormally (e.g. a connection reset). gateway is None if the socket errored before completing the handshake. If the gateway had connected, on_gateway_disconnected fires as usual. When not provided, unexpected connection errors fall back to the journal_gateway_client logger — bind this callback if you want to route them into your own error tracking.

Example wiring with OpenTelemetry and a logger:

from opentelemetry import propagate

def trace_context():
    carrier: dict[str, str] = {}
    propagate.inject(carrier)
    if "traceparent" not in carrier:
        return None
    return {"traceparent": carrier["traceparent"], "tracestate": carrier.get("tracestate")}

server = GatewayServer(
    validate_token=validate_token,
    port=8080,
    get_trace_context=trace_context,
    on_socket_error=lambda err, gw: logger.error(
        "gateway socket error", exc_info=err, extra={"gateway_id": gw.id if gw else None}
    ),
)

Full documentation

See the root README for protocol details, gateway configuration, and architecture.

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

journal_gateway_client-0.7.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

journal_gateway_client-0.7.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file journal_gateway_client-0.7.0.tar.gz.

File metadata

  • Download URL: journal_gateway_client-0.7.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for journal_gateway_client-0.7.0.tar.gz
Algorithm Hash digest
SHA256 cf3cb656ad8c6506bd216be4a0c07cc595a05112f084890db8a822498dd89d28
MD5 92be19dba76d9d0cfdb1f33ebcc15ce8
BLAKE2b-256 6beb8807f1b836b4e4e30bd5781f4003a89b07007bd0721cbf1c8062a5592bbf

See more details on using hashes here.

File details

Details for the file journal_gateway_client-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for journal_gateway_client-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 812eb06b15b5e670d562060d7fae91939629dcd81a1a362729fe0bbbbeccceaa
MD5 3f428725b04dcbf7d3f758eb3443a35a
BLAKE2b-256 a66159fa0a7f549e3512401b4832d253a9bcb13a1e7a976c76202142db3d6998

See more details on using hashes here.

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