Skip to main content

Compliance-grade audit logging for MCP client operations

Project description

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

from lokryn_mcp_log import HTTPSink

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

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_LOGIN session/initialize
list_tools() EVENT_TOOL_INVOCATION tools/list
call_tool(name, args) EVENT_TOOL_INVOCATION tools/{name}
list_resources() EVENT_RESOURCE_ACCESS resources/list
read_resource(uri) EVENT_CONTEXT_ACCESS {uri}
list_prompts() EVENT_PROMPT_EXECUTION 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)
  • Duration (milliseconds)
  • Correlation ID (for tracing)
  • Input arguments
  • 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 HTTPSink

sink = HTTPSink(
    endpoint="https://fieldnotes.lokryn.com/ingest",
    headers={"Authorization": "Bearer <your-api-key>"},
)

License

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

Links

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

lokryn_mcp_log-0.2.0.tar.gz (67.4 kB view details)

Uploaded Source

Built Distribution

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

lokryn_mcp_log-0.2.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file lokryn_mcp_log-0.2.0.tar.gz.

File metadata

  • Download URL: lokryn_mcp_log-0.2.0.tar.gz
  • Upload date:
  • Size: 67.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lokryn_mcp_log-0.2.0.tar.gz
Algorithm Hash digest
SHA256 00fc0a5c812e9894b70d55dfe1de8bf35fbdb29f8ae27e03bfee180e4ca66338
MD5 c4f24d583d566636edf34631b87f4351
BLAKE2b-256 a31efb803954dc327a7316266534f915b5ac2ec6aa6d9cfacf1de4ef7c24defe

See more details on using hashes here.

Provenance

The following attestation bundles were made for lokryn_mcp_log-0.2.0.tar.gz:

Publisher: publish.yml on lokryn-llc/mcp-log

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

File details

Details for the file lokryn_mcp_log-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: lokryn_mcp_log-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lokryn_mcp_log-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 046dc3cd7dd508feee7f1a7574f95853bbcede44dd5c28cf655be0289368b7b6
MD5 3955b1e8b2bec0b187907d6294b47349
BLAKE2b-256 ed73007281eebe2a572480d84dc1bb6ff16e4c53d4d2c52ef9615e3db508d616

See more details on using hashes here.

Provenance

The following attestation bundles were made for lokryn_mcp_log-0.2.0-py3-none-any.whl:

Publisher: publish.yml on lokryn-llc/mcp-log

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