Skip to main content

mmar-pyhr

Pydantic-HTTP-RPC - A lightweight RPC framework based on HTTP + FastAPI with Pydantic validation.

Overview

mmar-pyhr provides a simple RPC-style interface for service-to-service communication over HTTP, similar to mmar-ptag but using HTTP instead of gRPC. It was created to address Kubernetes compatibility issues with gRPC while maintaining the same developer-friendly interface.

Features

  • Type-safe RPC using Pydantic for validation
  • HTTP/JSON transport (Kubernetes-friendly, no service mesh required)
  • Interface-based design using Python classes
  • Built-in tracing with trace_id support via HTTP headers
  • Health check endpoints for Kubernetes pod lifecycle management
  • Smart address handling (port numbers, localhost, full URLs)
  • Sync/async support for service methods

Installation

uv pip install mmar-pyhr

Quick Start

Server

from mmar_pyhr import PyhrServerConfig, deploy_pyhr_server


class UserService:
    def get_user(self, *, user_id: int, trace_id: str = "") -> dict:
        return {"id": user_id, "name": "John Doe"}

    def create_user(self, *, name: str, email: str, trace_id: str = "") -> dict:
        return {"id": 123, "name": name, "email": email}


if __name__ == "__main__":
    config = PyhrServerConfig(port=8000, host="0.0.0.0")
    deploy_pyhr_server(
        config_server=config,
        service=UserService()
    )

config_server is optional - PyhrServerConfig, a dict, a callable returning either, or omitted entirely (same as {}, i.e. all defaults):

deploy_pyhr_server(service=UserService())                    # all defaults (port 8000)
deploy_pyhr_server({"port": 8000}, service=UserService())    # dict
deploy_pyhr_server(lambda: load_config(), service=UserService())  # lazy config

Client

from mmar_pyhr import pyhr_client


class UserServiceI:
    def get_user(self, *, user_id: int, trace_id: str = "") -> dict:
        raise NotImplementedError

    def create_user(self, *, name: str, email: str, trace_id: str = "") -> dict:
        raise NotImplementedError


# Create client
client = pyhr_client(UserServiceI, "localhost:8000")

# Make requests
user = client.get_user(user_id=42)
new_user = client.create_user(name="Alice", email="alice@example.com")

# With trace_id
user = client.get_user(user_id=42, trace_id="req-123")

CLI

The pyhr-server script wraps and runs a service without writing a launcher file. Built on mmar_envops: arguments are dispatched by mmar_envops.cli_cli, targets are found by mmar_envops.resolve_cli_class (pyhr-server --help lists the options):

pyhr-server run my_project.service.UserService --host=0.0.0.0 --port=8000
pyhr-server my_project.service.UserService --port=8000   # `run` is implied

Target formats:

  • pkg.module.Service - class path (the resolver instantiates it)
  • pkg.module - module; its single class is picked automatically (with several, the one named *cli*, else an error asks to name the class)
  • path/to/service.py:Service - file path

Options: --host, --port, --max-workers, --log-level, --validate_api (defaults to False on the CLI - pass --validate_api=True to require interface inheritance, as PyhrServerConfig does).

Address Formats

The client accepts various address formats:

pyhr_client(Service, 9494)                      # → http://localhost:9494
pyhr_client(Service, ":9494")                    # → http://localhost:9494
pyhr_client(Service, "localhost:9494")           # → http://localhost:9494
pyhr_client(Service, "http://localhost:9494")    # → http://localhost:9494
pyhr_client(Service, "https://api.example.com")  # → https://api.example.com

API Routes

The server automatically creates the following routes:

RPC Endpoints

POST /api/{method_name}  → service.method_name(**kwargs)

The body is the JSON object of method kwargs. It is optional: a request without a body is treated as {}, so no-arg methods can be called with a bare curl -X POST localhost:8000/api/ping. Methods with required args still fail with 422 listing the missing fields.

Health Checks

GET  /health_check        → {"status": "ok"}
GET  /health/liveness     → {"status": "alive"}
GET  /health/readiness    → {"status": "ready"}

Tracing

Trace IDs are automatically propagated via the X-Trace-ID HTTP header:

# Client
client.get_user(user_id=42, trace_id="req-123")

# Server automatically receives trace_id in context
# and can use it for logging

Transport Abstraction

mmar-pyhr integrates with the mmar-mimpl transport registry:

from mmar_mimpl import get_transport

# Uses MMAR_TRANSPORT environment variable
transport = get_transport()  # "pyhr" or "mmar_pyhr"

# Create client
client = transport.make_client(UserServiceI, address="localhost:8000")

# Deploy server
transport.deploy_server(
    service=UserService(),
    config=None,
    config_server=PyhrServerConfig(port=8000)
)

Set the transport via environment variable:

export MMAR_TRANSPORT=pyhr
# or
export MMAR_TRANSPORT=mmar_pyhr

Configuration

Client Configuration

from mmar_pyhr import PyhrTransportConfig

config = PyhrTransportConfig(
    timeout=30.0,        # Request timeout in seconds
    retry_attempts=3,    # Number of retry attempts
)

Server Configuration

from mmar_pyhr import PyhrServerConfig

config = PyhrServerConfig(
    host="0.0.0.0",      # Bind host
    port=8000,           # Bind port
    max_workers=1,       # Number of worker processes
    log_level="info",    # Logging level
)

Comparison with mmar-ptag

Feature mmar-ptag (gRPC) mmar-pyhr (HTTP)
Transport gRPC/HTTP2 HTTP/JSON
Serialization JSON over Protobuf Pure JSON
Kubernetes Requires service mesh Native support
Debugging Requires grpcurl Standard curl/httpie
Health checks Custom implementation Built-in HTTP endpoints
Load balancing Connection-level (needs mesh) Request-level (native K8s)

Development

# Run tests
uv run pytest

# Build package
uv build

License

MIT

See Also

Metadata

Release files for mmar-pyhr 0.0.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mmar-pyhr 0.0.12
File Size Uploaded Provenance Actions
mmar_pyhr-0.0.12.tar.gz 11.2 kB Without provenance Details for mmar_pyhr-0.0.12.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mmar-pyhr 0.0.12
File Interpreter ABI Platform Actions
mmar_pyhr-0.0.12-py3-none-any.whl Python 3 none any Details for mmar_pyhr-0.0.12-py3-none-any.whl

Total release size: 25.7 kB

Release files / mmar_pyhr-0.0.12.tar.gz

Download URL mmar_pyhr-0.0.12.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5aabd3629924da0bdadc518c0abf6af1e472240538d5c837268857455b9f219c
BLAKE2b-256 checksum
How to use checksums
eddc28d386bb6eea63c9d01d131e2670fc4faea7c42d02e9ed340fc84c07ef2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.31

Release files / mmar_pyhr-0.0.12-py3-none-any.whl

Download URL mmar_pyhr-0.0.12-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00f4827242f846e652d6a079fbab684f1aa27f8679cdfbd47e05b08c63f9f0f6
BLAKE2b-256 checksum
How to use checksums
1fd1ac2d4b6c1568efaa2aa60ea5980afea9045f38e222f8042e6be85730612d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.31

Security for mmar-pyhr 0.0.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page