Intelligence Layer for AI Agents
PyStreamMCP is a Rust-powered, Python-extensible platform that optimizes queries and discovers context for AI agents. It delivers 60-75% token reduction while maintaining quality and speed.
Rather than agents asking for everything and parsing the response, PyStreamMCP asks: What does this agent actually need?
Agent Query
↓
Query Planning (token budget)
↓
Context Discovery (what's relevant?)
↓
Optimization (60-75% reduction)
↓
Optimal Context Window
↓
Agent Response
The platform sits between agent frameworks and data systems, dramatically reducing token usage without sacrificing quality.
- Understand agent intent (Retrieve, Discover, Aggregate, Synthesize, Analyze)
- Enforce token budgets per query
- Set latency and confidence constraints
- Token-efficient mode (500 token cap, 0.85 confidence floor)
- Identify relevant data sources automatically
- Rank by relevance + freshness scores
- Estimate token cost per source
- Discover across warehouses, caches, APIs
- Caching — Reuse computed results
- Summarization — Summarize instead of full text
- Sampling — Sample data instead of full scan
- Pruning — Remove low-relevance items
- Compression — Compress representations
- Async — Parallelize requests
- Early Termination — Stop when confident
- Integrate StatGuardian for data quality
- Validate context before including in responses
- Configurable freshness requirements
- Block bad data automatically
pip install pystreammcpfrom pystreammcp import Query, Discovery, Optimization
# Agent asks a question
query = Query("Which customers are at churn risk?", agent_id="agent_123")
query = query.token_efficient() # Max 500 tokens, 0.85 confidence
# Discover relevant context
discovery = Discovery.new(query.id)
# ... sources discovered and ranked ...
# Optimize for cost
strategy = Optimization.for_token_efficiency(query.id)
# Caching + Pruning + Summarization + Early Termination
# Result: 70% token reduction
# Cost: $0.10 instead of $0.33What agents need to know:
- Retrieve — Get specific information
- Discover — Explore available data
- Aggregate — Summarize multiple sources
- Synthesize — Combine information
- Analyze — Statistical analysis
Relevant information discovered automatically:
- Entity data (customers, accounts, products)
- Relationships (who knows whom, what links where)
- Metrics (MRR, churn rate, engagement)
- Historical context (trends, patterns)
- Similar items (recommendations, benchmarks)
- Contextual information (categories, tags)
Find what matters:
- Scan available data sources
- Rank by relevance + freshness
- Estimate token costs
- Identify caching opportunities
- Discover relationships and dependencies
Reduce without losing quality:
- 60-75% token reduction target
- Multiple techniques combined
- Quality validation throughout
- Cost metrics tracked
- Query planning with constraints
- Context discovery and ranking
- Cost optimization strategies
- SQLite persistence
- StatGuardian validation
- 18+ unit tests
- MCP (Model Context Protocol) support
- 6 LLM framework integrations (Langchain, Llamaindex, Semantic Kernel, CrewAI, PydanticAI, Haystack)
- 6 workflow orchestration tools (Temporal, Airflow, n8n, Power Automate, UiPath, Automation Anywhere)
- REST API server + CLI interface
- Docker deployment support
- 27+ integration tests
- ML & Observability — Learned relevance models (accuracy > 80%), OpenTelemetry metrics
- LangSmith Integration — Distributed tracing, span management, cost analytics, dashboard
- Query Decomposition — Complex query analysis, parallelization detection, 3.5x speedup potential
- QA Framework — Quality validation rules, SLA enforcement, audit logging, compliance reporting
- Auto Prompt Tagging — Intent/complexity/domain detection, quality scoring, routing strategies
- Advanced Optimization — Streaming context windows (<50ms latency), multi-agent context sharing (+20% savings)
- Query parsing and planning
- Context discovery
- Optimization engine
- Token estimation
- SQLite persistence
- SDK and bindings
- Agent integrations
- Custom optimizers
- Langchain/Llamaindex adapters
- SQLite (v0.2 - lightweight, no setup required)
- PostgreSQL (v1.0+)
PyStreamMCP aims for 60-75% token reduction:
Before:
- Query: "Which customers are at churn risk?" (10 tokens)
- Full customer data: 2,000 rows × 50 tokens = 100,000 tokens
- Full interaction history: 500 interactions × 20 tokens = 10,000 tokens
- Similar customers: 100 rows × 50 tokens = 5,000 tokens
- Total: 115,010 tokens
After (with PyStreamMCP):
- Query: "Which customers are at churn risk?" (10 tokens)
- Top 10 at-risk customers + key traits: 500 tokens
- Recent interactions (last 30 days): 1,000 tokens
- Cohort comparison: 500 tokens
- Total: 2,010 tokens
- Reduction: 98.3% (exceeds 60-75% target) ✅
PyStreamMCP is part of the data intelligence platform:
- StatGuardian — Ensures data is trustworthy (validation, contracts, drift)
- ClusterAudienceKit — Identifies who matters (segmentation, clustering)
- PyReverseETL — Activates intelligence in operational systems
- PyCustomerJourney — Drives engagement through journeys
- PyStreamMCP — Optimizes intelligence for AI agents (this project)
PyStreamMCP focuses exclusively on:
✓ Query planning and optimization ✓ Context discovery ✓ Cost optimization (token reduction) ✓ Agent intelligence
PyStreamMCP does NOT:
✗ Validate data (StatGuardian) ✗ Create audiences (ClusterAudienceKit) ✗ Activate data (PyReverseETL) ✗ Manage journeys (PyCustomerJourney)
# Build Rust core
cargo build -p pystreammcp-core
# Build Python bindings
maturin develop
# Run tests
cargo test
pytest tests/See CONTRIBUTING.md for guidelines.
MIT License. See LICENSE for details.
- GitHub Issues: PyStreamMCP/issues
- Discussions: PyStreamMCP/discussions
PyStreamMCP: Intelligent Context for Smarter Agents