Skip to main content

lokryn-mcp-log

Compliance-grade audit logging for MCP (Model Context Protocol) client operations.

What It Does

Wraps the official MCP Python SDK's ClientSession and logs every operation—tool calls, resource access, prompt executions—in a format that satisfies SOC2, HIPAA, and PCI audit requirements.

Logs conform to the lokryn-compliance-log-schema, an open standard for audit logging (schema vendored in this package, will be extracted to separate PyPI package).

Installation

pip install lokryn-mcp-log

Or with uv:

uv add lokryn-mcp-log

Quick Start

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from lokryn_mcp_log import with_logging, StdoutSink

server_params = StdioServerParameters(command="python", args=["server.py"])

async def main():
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()

            # Wrap with logging
            logged = with_logging(
                session,
                sink=StdoutSink(),
                environment="development",
            )

            # Use exactly like normal
            await logged.call_tool("add", {"a": 1, "b": 2})

Sinks

StdoutSink

from lokryn_mcp_log import StdoutSink

sink = StdoutSink()           # JSON to stdout
sink = StdoutSink(pretty=True) # Pretty-printed

FileSink

from lokryn_mcp_log import FileSink

sink = FileSink("/var/log/mcp-audit.jsonl")

HTTPSink

import os
from lokryn_mcp_log import HTTPSink

# JSON format (default)
sink = HTTPSink(
    endpoint="https://your-log-collector.com/ingest",
    headers={"Authorization": "Bearer <token>"},
)

# Protobuf format
sink = HTTPSink(
    endpoint="https://your-log-collector.com/ingest/proto",
    format="protobuf",
)

# With HMAC signing
sink = HTTPSink(
    endpoint="https://your-log-collector.com/ingest",
    hmac_key="your-secret-key",
)

# Using environment variables
sink = HTTPSink(
    endpoint=os.environ["LOG_ENDPOINT"],
    hmac_key=os.environ.get("LOG_HMAC_KEY"),
    format=os.environ.get("LOG_FORMAT", "json"),
)

Custom Sink

Implement the Sink protocol:

from lokryn_mcp_log.schema import log_pb2

class MySink:
    async def emit(self, record: log_pb2.LogRequest) -> None:
        # Your logic here
        pass

Configuration

from lokryn_mcp_log import with_logging
from lokryn_mcp_log.schema import log_pb2

logged = with_logging(
    session,
    sink=my_sink,
    environment="production",           # Required
    actor_id="agent-001",                # Optional, auto-generated if omitted
    component="my-agent",                # Defaults to "mcp-client"
    policy_tags=["SOC2", "HIPAA"],       # Optional compliance tags
    default_sensitivity=log_pb2.SENSITIVITY_CONFIDENTIAL,
)

What Gets Logged

Operation Event Type Resource
initialize() EVENT_MCP_INITIALIZE session/initialize
list_tools() EVENT_TOOL_LIST tools/list
call_tool(name, args) EVENT_TOOL_INVOCATION tools/{name}
list_resources() EVENT_RESOURCE_LIST resources/list
read_resource(uri) EVENT_RESOURCE_READ {uri}
list_prompts() EVENT_PROMPT_LIST prompts/list
get_prompt(name) EVENT_PROMPT_EXECUTION prompts/{name}
(session close) EVENT_LOGOUT session/close

Each log includes:

  • Timestamp
  • Actor ID (session/agent identifier)
  • Session ID
  • Duration (milliseconds)
  • Trace ID (for distributed tracing)
  • MCP payload (tool name, arguments, resource URI)
  • Outcome (success/failure)
  • Error details (on failure)

Error Handling

This is a library, not a service. If the sink fails, the exception propagates to your code. Handle it as appropriate for your use case.

Field Notes Integration

Send logs directly to Field Notes for tamper-evident storage and querying:

from lokryn_mcp_log import FieldNotesSink

# Configure via environment variables:
# FIELDNOTES_HMAC_KEY=your-secret-key
# FIELDNOTES_FORMAT=json  (or "protobuf")

sink = FieldNotesSink()

# Or pass explicitly
sink = FieldNotesSink(hmac_key="your-secret-key", format="protobuf")

License

AGPL-3.0. Commercial license available—contact license@lokryn.com.

Metadata

Release files for lokryn-mcp-log 0.4.0

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

Source distribution (sdist)

Source distribution for lokryn-mcp-log 0.4.0
File Size Uploaded Provenance
lokryn_mcp_log-0.4.0.tar.gz 81.0 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for lokryn-mcp-log 0.4.0
File Interpreter ABI Platform
lokryn_mcp_log-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 106.6 kB

Release files / lokryn_mcp_log-0.4.0.tar.gz

Download URL lokryn_mcp_log-0.4.0.tar.gz
Size 81.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8f1c22a2227f403d5668c4dbf3bf42950f1c75cfbbd08ea9555f26ca421be696
BLAKE2b-256 checksum
How to use checksums
00cea6ea438e194da58dda8408a0440f4c5fa427c62d7b60386f4595e6c64177
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 25, 2025.

Transparency log

Release files / lokryn_mcp_log-0.4.0-py3-none-any.whl

Download URL lokryn_mcp_log-0.4.0-py3-none-any.whl
Size 25.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31c3f2471e0d2e3f3dc2498b4c6a2a37046bc1470f68fdf28d41c93ce0fc77fc
BLAKE2b-256 checksum
How to use checksums
a764e3092cd68a2025f808ef85368354a7d59b0ee84de344c183efee396fec40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 25, 2025.

Transparency log

Security for lokryn-mcp-log 0.4.0

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for lokryn_mcp_log-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 25, 2025.

Transparency log

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