Skip to main content

Python client library for the Journal Gateway protocol

Project description

journal-gateway-client

PyPI

Python service-side library for the Journal Gateway protocol. Use this package in the service that accepts gateway WebSocket connections, validates gateway tokens, receives tool and skill catalogs, and calls tools on connected gateways.

If you want to run the customer-side gateway process, install the npm package journal-gateway instead.

Install

Requires Python 3.11 or newer.

pip install journal-gateway-client

Quick Start

import asyncio
import os

from journal_gateway_client import GatewayServer, TokenValidationResult


async def validate_token(token: str) -> TokenValidationResult | None:
    if token == os.environ["JOURNAL_GATEWAY_TOKEN"]:
        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 gateway: print(
        "gateway connected", gateway.id, gateway.organization_id
    )
    server.on_gateway_updated = lambda gateway: print(
        "gateway catalog updated", gateway.id
    )
    server.on_gateway_disconnected = lambda gateway: print(
        "gateway disconnected", gateway.id
    )

    await server.start()

    # After a gateway for org_123 connects and publishes the postgresql integration:
    result = await server.call_tool_for_org(
        "org_123",
        "postgresql",
        "execute_sql",
        {"sql": "SELECT 1"},
    )
    print(result.content)

    await server.stop()


asyncio.run(main())

The library never prints to stdout by itself. Route callbacks into your own logger, metrics, and tracing stack. If on_socket_error is not provided, unexpected socket errors go to the journal_gateway_client logger, which is silent by default unless your application configures logging.

Key APIs

  • start() / stop(): start or stop the WebSocket server.
  • call_tool(integration_id, tool_name, arguments, timeout=60.0): call a tool on any connected gateway that exposes the integration.
  • call_tool_for_org(organization_id, integration_id, tool_name, arguments, timeout=90.0): call a tool for one organization, with candidate gateway selection and retry on connection-level failure.
  • get_tools_for_org(organization_id): list deduplicated tools for an organization.
  • get_versions(gateway_id), get_tools(gateway_id), get_skills(gateway_id): explicitly pull catalog data from a specific gateway.
  • connected_gateways: inspect currently connected gateways.

Callbacks

Set these attributes on the server instance:

  • on_gateway_connected(gateway): fired after authentication and initial catalog pull.
  • on_gateway_updated(gateway): fired when MCP tools or skills change.
  • on_gateway_disconnected(gateway): fired after a connected gateway disconnects.

Constructor callbacks:

  • get_trace_context: returns W3C trace context for each tool call.
  • on_socket_error(error, gateway | None): receives socket-level failures such as connection resets.

Trace Propagation

from opentelemetry import propagate


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


def on_socket_error(error: Exception, gateway) -> None:
    logger.error(
        "gateway socket error",
        exc_info=error,
        extra={"gateway_id": gateway.id if gateway else None},
    )


server = GatewayServer(
    validate_token=validate_token,
    port=8080,
    get_trace_context=get_trace_context,
    on_socket_error=on_socket_error,
)

get_trace_context is called for each tool call. The returned W3C trace context is sent to the gateway and used as the parent for remote tool execution spans.

Version Compatibility

Journal Gateway packages release in lockstep. Use matching versions of:

  • npm journal-gateway
  • npm journal-gateway-client for TypeScript services
  • npm journal-gateway-protocol
  • PyPI journal-gateway-client for Python services

More Documentation

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.8.0.tar.gz (10.1 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.8.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for journal_gateway_client-0.8.0.tar.gz
Algorithm Hash digest
SHA256 f1a0ef07b0c0cc6936af06a147010b2c89cff67a329de624691ba86c826ed262
MD5 4cfeee4a40e6f806ecc7b08a647829ea
BLAKE2b-256 4f1e9256e05fa24856532bfadf32b2e86430f81fa16fec123279837009419a8e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for journal_gateway_client-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2723b6662e951586e6cef63c9884836886c02be1b92c8bdf35720084b48b199d
MD5 b480342f02368184cb4f5acea946f1f7
BLAKE2b-256 0bf7a912f88bee348453506d5951e986a81eee727403208385138a42e9219e5e

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