A gRPC service for providing real-time, streamable context to LLMs.
Project description
ModuleContextStreaming (MCS) ๐
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
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 (
.envfile) and the server'sTool Registrymakes 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
-
Clone the repository:
git clone [https://github.com/armstrongsam25/ModuleContextStreaming.git](https://github.com/armstrongsam25/ModuleContextStreaming.git) cd ModuleContextStreaming
-
Create and activate a virtual environment:
python -m venv .venv # On Windows: .\.venv\Scripts\activate # On macOS/Linux: source .venv/bin/activate
-
Install dependencies: This installs the project in editable mode (
-e) along with all development dependencies.pip install -e .[dev]
-
Generate gRPC Code: Run these commands to compile the
.protofile and apply the necessary import patch. You only need to re-run this whenprotos/mcs.protochanges.# 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
-
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
- Configure your environment: Copy
.env.exampleto.envand fill in your Keycloak details. - Start the Server: In your first terminal, run:
python examples/simple_server.py - 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
MCPToolAdapterto 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modulecontextstreaming-0.0.6.tar.gz.
File metadata
- Download URL: modulecontextstreaming-0.0.6.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98f4d8696615312bd9e3c8203ac83340c569d0e22228fdddc853a3a0b08547f2
|
|
| MD5 |
7933352b2311ffc6e2ae17149d37ab7d
|
|
| BLAKE2b-256 |
7dacd76d451bbcdfe72f1a40f1fd41edfe1a31d5ac36702a4072bc6efc44c513
|
Provenance
The following attestation bundles were made for modulecontextstreaming-0.0.6.tar.gz:
Publisher:
publish-to-pypi.yml on armstrongsam25/ModuleContextStreaming
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
modulecontextstreaming-0.0.6.tar.gz -
Subject digest:
98f4d8696615312bd9e3c8203ac83340c569d0e22228fdddc853a3a0b08547f2 - Sigstore transparency entry: 582817610
- Sigstore integration time:
-
Permalink:
armstrongsam25/ModuleContextStreaming@e2d1030524502229fdfabd1fc4bcfed56a65b80a -
Branch / Tag:
refs/heads/main - Owner: https://github.com/armstrongsam25
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@e2d1030524502229fdfabd1fc4bcfed56a65b80a -
Trigger Event:
push
-
Statement type:
File details
Details for the file modulecontextstreaming-0.0.6-py3-none-any.whl.
File metadata
- Download URL: modulecontextstreaming-0.0.6-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6f370783cda91820bfb5abcbee1dca7fbb16a768b8ce725795395abbdf3582f
|
|
| MD5 |
636996dd29f8b2c3b6c4333429971f8b
|
|
| BLAKE2b-256 |
15fd10a05197e4ba9b5d9ca4400fc4320fe6001d699d7060701703aaa7744a55
|
Provenance
The following attestation bundles were made for modulecontextstreaming-0.0.6-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on armstrongsam25/ModuleContextStreaming
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
modulecontextstreaming-0.0.6-py3-none-any.whl -
Subject digest:
d6f370783cda91820bfb5abcbee1dca7fbb16a768b8ce725795395abbdf3582f - Sigstore transparency entry: 582817618
- Sigstore integration time:
-
Permalink:
armstrongsam25/ModuleContextStreaming@e2d1030524502229fdfabd1fc4bcfed56a65b80a -
Branch / Tag:
refs/heads/main - Owner: https://github.com/armstrongsam25
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@e2d1030524502229fdfabd1fc4bcfed56a65b80a -
Trigger Event:
push
-
Statement type: