Skip to main content

Python client SDK for the Memory Governance Protocol

Project description

MGP Python SDK

PyPI Python 3.11+ License: MIT

Python client SDK for the Memory Governance Protocol.

Install

From the repository root:

python3 -m pip install ./sdk/python

Or for development with test dependencies:

cd sdk/python
python3 -m pip install -e ".[dev]"

If you're using the full repository toolchain, make install covers this automatically.

What It Includes

Component Description
MGPClient Synchronous HTTP client for all protocol operations
AsyncMGPClient Async client for async runtimes
PolicyContextBuilder Helper for constructing policy context objects
SearchQuery / AuditQuery Typed query builders
Auth helpers ApiKeyAuth, BearerAuth, TLSConfig
RetryConfig Retry configuration for transient failures
Pagination helpers Iterators for search and audit result streams
Task polling Helpers for async protocol operations
Error classes Mapped from MGP protocol error codes

Quick Example

from mgp_client import MGPClient, PolicyContextBuilder, SearchQuery

ctx = PolicyContextBuilder(
    actor_agent="nanobot/main",
    subject_id="user_123",
    tenant_id="tenant_1",
)

with MGPClient("http://127.0.0.1:8080") as client:
    # Write
    client.write_memory(
        ctx.build("write"),
        {
            "memory_id": "mem_1",
            "subject": {"kind": "user", "id": "user_123"},
            "scope": "user",
            "type": "preference",
            "content": {
                "statement": "User prefers dark mode.",
                "preference_key": "theme",
                "preference_value": "dark",
            },
            "source": {"kind": "human", "ref": "chat:1"},
            "created_at": "2026-03-17T12:00:00Z",
            "backend_ref": {"tenant_id": "tenant_1"},
            "extensions": {},
        },
    )

    # Search
    result = client.search_memory(
        ctx.build("search"),
        SearchQuery(
            query_text="What does the user prefer for theme?",
            intent_type="preference_lookup",
            keywords=["dark", "theme"],
            target_memory_types=["preference"],
        ),
    )
    print(result.data["results"])

Async Usage

from mgp_client import AsyncMGPClient, BearerAuth, PolicyContextBuilder

ctx = PolicyContextBuilder(
    actor_agent="runtime/agent",
    subject_id="user_123",
    tenant_id="tenant_1",
)

async with AsyncMGPClient(
    "http://127.0.0.1:8080",
    auth=BearerAuth("secret-token"),
) as client:
    capabilities = await client.get_capabilities()
    print(capabilities["manifest"]["backend_kind"])

    response = await client.export_memories(
        ctx.build("read"),
        {"execution_mode": "async", "limit": 100},
    )
    task = await client.wait_for_task(response.data["task"]["task_id"])
    print(task["status"])

Auth And Retry

from mgp_client import ApiKeyAuth, MGPClient, RetryConfig, TLSConfig

client = MGPClient(
    "https://gateway.example.com",
    auth=ApiKeyAuth("gateway-api-key"),
    tls=TLSConfig(verify=True),
    retry=RetryConfig(max_attempts=3, backoff_seconds=0.2),
)

Pagination

for item in client.iter_search_results(
    ctx.build("search"),
    SearchQuery(query_text="preferences", limit=50),
):
    print(item["memory"]["memory_id"])

Surface Coverage

  • Core memory operations (write, search, get, update, expire, revoke, delete, purge)
  • Lifecycle operations (initialize, capabilities)
  • Batch write
  • Export / import / sync
  • Audit query
  • Async task polling

Notes

  • This SDK is transport-focused and independent of the reference gateway implementation.
  • write_memory() accepts canonical memory objects; write_candidate() accepts MemoryCandidate payloads.
  • Search and get responses include consumable_text, return_mode, and redaction_info — runtimes should prefer those fields over assuming raw memory.content is always prompt-safe.
  • The SDK can be used against any gateway that implements the MGP HTTP binding.

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

mgp_client-0.1.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

mgp_client-0.1.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file mgp_client-0.1.0.tar.gz.

File metadata

  • Download URL: mgp_client-0.1.0.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mgp_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9e45779097041d532200c6ce84588d71e74a3521c7178a9977887ab53a0988bc
MD5 bd1c8dcb2f8f82fc94d7fa67d53314f4
BLAKE2b-256 79f9a39ce34789dbb71c3d1edfabd99d3ec39b177cc2d29081f8f48afc2daeaa

See more details on using hashes here.

Provenance

The following attestation bundles were made for mgp_client-0.1.0.tar.gz:

Publisher: publish-mgp-client.yml on HKUDS/MGP

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

File details

Details for the file mgp_client-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mgp_client-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mgp_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6a390e32b597505e807d6035a93a14293ab12bdf6513279568767231842ec1d0
MD5 063df6ae5a955b21221b41e63049f40a
BLAKE2b-256 221f88ed69e036041ef9caf16cf4eda6bee45b96a930652a72052cb81bcbb144

See more details on using hashes here.

Provenance

The following attestation bundles were made for mgp_client-0.1.0-py3-none-any.whl:

Publisher: publish-mgp-client.yml on HKUDS/MGP

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