Skip to main content

A gRPC service for providing real-time, streamable context to LLMs.

Project description

ModuleContextStreaming (MCS) ๐Ÿš€

PyPI Pytest License Python Version

A high-performance gRPC service designed to provide real-time, streamable context to Large Language Models (LLMs). This project serves as a robust backend for AI applications that require secure, low-latency communication with server-side tools and resources.

It features a secure, authenticated API and a powerful adapter for integrating with Model Context Protocol (MCP) servers, allowing you to expose tools from any MCP-compatible backend through a single, unified gRPC interface.


Why Use MCS?

  • Solve LLM Latency: Traditional REST APIs can be slow for conversational AI. MCS uses gRPC streaming to send data back to the LLM as it's generated, creating a more responsive user experience.
  • Unify Your Tools: Stop building one-off backends. MCS can act as a secure gateway to all your tools, whether they are native Python functions or external Model Context Protocol (MCP) servers.
  • Secure by Default: Don't worry about boilerplate security code. MCS provides out-of-the-box JWT authentication via Keycloak and enforces TLS encryption.
  • Type-Safe & Reliable: By using a Protobuf schema, you eliminate entire classes of bugs. The API contract is clear, versionable, and reliable.

High-Level Architecture

High-Level Architecture


Features

  • High-Performance Streaming: Built on gRPC and HTTP/2 for efficient, multiplexed, and low-latency data streaming.
  • Strict API Contract: Uses Protocol Buffers (.proto) as the single source of truth for the API, ensuring type-safe communication.
  • Secure by Default:
    • JWT Authentication: Integrates with Keycloak for robust, token-based authentication using a gRPC interceptor.
    • TLS Encryption: Supports secure gRPC channels out-of-the-box for encrypted client-server communication.
  • MCP Backend Integration: Seamlessly connect to and expose tools from MCP servers running over stdio.
  • Intelligent Client: Includes an MCP-aware parser that can automatically distinguish between text, images, structured JSON, resources, and errors from tool responses.
  • Configurable & Extensible: The server and client are configured via environment variables (.env file) and the server's Tool Registry makes it simple to add new native Python tools.

Project Structure

ModuleContextStreaming/         # The project root directory
โ”œโ”€โ”€ ModuleContextStreaming/     # The main, installable Python package
โ”‚   โ”œโ”€โ”€ init.py
โ”‚   โ”œโ”€โ”€ server.py             # Reusable gRPC Server class with MCP adapter
โ”‚   โ”œโ”€โ”€ client.py             # Reusable gRPC Client class with MCP parser
โ”‚   โ”œโ”€โ”€ auth.py               # Keycloak JWT authentication interceptor
โ”‚   โ”œโ”€โ”€ exceptions.py         # Custom exception types
โ”‚   โ”œโ”€โ”€ mcs_pb2.py            # Generated Protobuf messages
โ”‚   โ””โ”€โ”€ mcs_pb2_grpc.py       # Generated gRPC client/server stubs
โ”œโ”€โ”€ protos/
โ”‚   โ””โ”€โ”€ mcs.proto             # The API contract source of truth
โ”œโ”€โ”€ examples/
โ”‚   โ”œโ”€โ”€ simple_server.py      # Example runnable server with native tools
โ”‚   โ””โ”€โ”€ simple_client.py      # Example runnable client
โ”œโ”€โ”€ certs/
โ”‚   โ”œโ”€โ”€ private.key           # Placeholder for your TLS private key
โ”‚   โ””โ”€โ”€ certificate.pem       # Placeholder for your TLS certificate
โ”œโ”€โ”€ build_scripts.py          # Script to generate and patch gRPC code
โ”œโ”€โ”€ DESIGN.md                 # Project architecture and design decisions
โ”œโ”€โ”€ pyproject.toml            # Project metadata and dependencies
โ””โ”€โ”€ README.md

Setup and Installation

Prerequisites

  • Python 3.10+
  • Git
  • (Optional) OpenSSL - for self-signed certificates

Installation Steps

  1. Clone the repository:

    git clone [https://github.com/armstrongsam25/ModuleContextStreaming.git](https://github.com/armstrongsam25/ModuleContextStreaming.git)
    cd ModuleContextStreaming
    
  2. Create and activate a virtual environment:

    python -m venv .venv
    
    # On Windows: 
    .\.venv\Scripts\activate
    
    # On macOS/Linux: 
    source .venv/bin/activate
    
  3. Install dependencies: This installs the project in editable mode (-e) along with all development dependencies.

    pip install -e .[dev]
    
  4. Generate gRPC Code: Run these commands to compile the .proto file and apply the necessary import patch. You only need to re-run this when protos/mcs.proto changes.

    # 1. Generate gRPC code from the .proto file
    python -m grpc_tools.protoc -I ./protos --python_out=./ModuleContextStreaming --grpc_python_out=./ModuleContextStreaming mcs.proto
    
    # 2. Patch the generated code for correct relative imports
    python build_scripts/build.py
    
  5. Generate Self-Signed Certificates (for local testing):

    openssl req -x509 -newkey rsa:4096 -keyout certs/private.key -out certs/certificate.pem -sha256 -days 365 -nodes -subj "/CN=localhost"
    

Usage

  1. Configure your environment: Copy .env.example to .env and fill in your Keycloak details.
  2. Start the Server: In your first terminal, run:
    python examples/simple_server.py
    
  3. Run the Client: In a second terminal, run:
    python examples/simple_client.py
    

Contributing

Contributions are welcome! Please feel free to open an issue to report a bug or request a feature, or submit a pull request.


Roadmap

  • Advanced Authorization: Implement role-based access control (RBAC) based on JWT claims.
  • TCP-based MCP Backends: Extend the MCPToolAdapter to support TCP sockets.
  • Enhanced Observability: Integrate structured logging and OpenTelemetry.
  • Comprehensive Testing: Add a full suite of unit and integration tests.
  • PyPI Packaging: Finalize packaging and publish to PyPI.

License

Distributed under the MIT License.

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

modulecontextstreaming-0.0.5.tar.gz (18.4 kB view details)

Uploaded Source

Built Distribution

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

modulecontextstreaming-0.0.5-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file modulecontextstreaming-0.0.5.tar.gz.

File metadata

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

File hashes

Hashes for modulecontextstreaming-0.0.5.tar.gz
Algorithm Hash digest
SHA256 ce01ea1519cb4ab6732437e31e4f653b84d90863fdc51e11c886c7a4a5b85b41
MD5 948915160cf6629a807e22cf8cf136fd
BLAKE2b-256 0491354f78f0e080ed6da0b94295331295241c785c6490d0af084a098657e755

See more details on using hashes here.

Provenance

The following attestation bundles were made for modulecontextstreaming-0.0.5.tar.gz:

Publisher: publish-to-pypi.yml on armstrongsam25/ModuleContextStreaming

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

File details

Details for the file modulecontextstreaming-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for modulecontextstreaming-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 07a6e7b0d93462aff457304f22b898de8ca72b72310c239695e2b0ba1d1cee87
MD5 3eb25bc9463086878527ad954ad70610
BLAKE2b-256 e948de5035e516c0d35ebce526b645dc5c8b2757f2e12730f34610f56741af09

See more details on using hashes here.

Provenance

The following attestation bundles were made for modulecontextstreaming-0.0.5-py3-none-any.whl:

Publisher: publish-to-pypi.yml on armstrongsam25/ModuleContextStreaming

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