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.7.tar.gz (21.6 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.7-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modulecontextstreaming-0.0.7.tar.gz
  • Upload date:
  • Size: 21.6 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.7.tar.gz
Algorithm Hash digest
SHA256 e5a2bd4b377c4b517e0a55d942acc3215dbd21d81ad86960c2170cee9fb98a45
MD5 c22270b9f977b910d0eec95b741ea413
BLAKE2b-256 ce625151c0a1df567fb05f717c2943f7da4a176083741575419d6c97c12f5566

See more details on using hashes here.

Provenance

The following attestation bundles were made for modulecontextstreaming-0.0.7.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.7-py3-none-any.whl.

File metadata

File hashes

Hashes for modulecontextstreaming-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 71db2575b9287a8f38f9a8c7b7c6d74439a1bdf8b4ced7fa13b3ea0c216f2273
MD5 faf73a7886347352cbd57e948219069a
BLAKE2b-256 a9d70528a660e78d8bb15e1cd043cb0ce663b854764ea2ce33046ab2dad09639

See more details on using hashes here.

Provenance

The following attestation bundles were made for modulecontextstreaming-0.0.7-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