File size: 17,308 Bytes
c509b44
ba6735b
e4abb85
 
 
ba6735b
 
 
e4abb85
ba6735b
c509b44
ba6735b
c509b44
ba6735b
 
 
e4abb85
ba6735b
c509b44
 
 
 
ebb22ed
c509b44
 
 
 
 
 
 
 
0073bf0
c509b44
 
 
 
 
 
 
 
 
 
 
ba6735b
 
 
e4abb85
9a99098
ba6735b
0073bf0
 
4c04529
0073bf0
4c04529
 
0073bf0
ba6735b
e4abb85
ba6735b
e4abb85
9a99098
 
 
ba6735b
e4abb85
9a99098
e4abb85
9a99098
90160c5
e4abb85
 
 
eb29e58
e4abb85
eb29e58
0073bf0
eb29e58
b6650bb
 
f5cdb7d
eb29e58
b6650bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb29e58
69aea0d
 
 
 
 
 
 
f5cdb7d
69aea0d
 
 
f5cdb7d
 
 
 
 
 
 
69aea0d
 
 
 
 
 
 
f5cdb7d
69aea0d
0073bf0
eb29e58
c509b44
eb29e58
c509b44
 
 
0073bf0
c509b44
 
 
 
 
 
 
 
4c04529
c509b44
4c04529
 
 
c509b44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb29e58
0073bf0
9a99098
e4abb85
9a99098
c509b44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4749f94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4abb85
ba6735b
e4abb85
ba6735b
c509b44
ba6735b
9a99098
ba6735b
e4abb85
ba6735b
e4abb85
ba6735b
e4abb85
ba6735b
 
 
e4abb85
ba6735b
e4abb85
ba6735b
 
 
e4abb85
ba6735b
e4abb85
ba6735b
9a99098
 
c509b44
 
 
b6650bb
 
c509b44
 
 
4c04529
 
 
 
 
 
 
 
 
 
 
ebb22ed
 
 
 
 
 
4c04529
 
 
 
 
c509b44
b6650bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4abb85
9a99098
 
e4abb85
b6650bb
e4abb85
c509b44
ba6735b
 
 
9a99098
 
e4abb85
9a99098
c509b44
 
 
9a99098
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# IntegraChat β€” Enterprise MCP Autonomous Agent Platform

**Track:** MCP in Action  
**Category:** Enterprise  
**Tag:** `mcp-in-action-track-enterprise`

---

## Overview

**IntegraChat** is an enterprise-grade, multi-tenant AI platform that demonstrates the full capabilities of the **Model Context Protocol (MCP)** in a production-style environment. Built with enterprise governance and observability in mind, IntegraChat combines autonomous tool-using agents, RAG retrieval, live web search, and admin compliance under strict tenant isolation.

This platform showcases how MCP can power intelligent, governed, multi-tenant AI systems with real-time analytics, regex-based red-flag detection, and comprehensive tool orchestration.

---

## Features

### Core Capabilities

- πŸ€– **Autonomous Multi-Step MCP Agents** – Intelligent tool-aware agent that plans and executes multi-step workflows across RAG, Web, Admin, and LLM tools with memory of previous tool outputs
- πŸ“š **Enhanced Knowledge Base Management** – Upload raw text, URLs, or documents (PDF/DOCX/TXT/MD) with rich metadata (source URL, timestamp, document type) and optimized chunking (400-600 tokens)
- πŸ” **Optimized RAG Search** – Semantic search with configurable similarity threshold (default 0.3) for better recall, with fallback to return top results even if below threshold
- πŸ—‘οΈ **Document Management** – Delete individual documents or bulk delete all documents for a tenant with confirmation dialogs
- πŸ›‘οΈ **Enterprise Admin Governance** – Regex-based red-flag pattern matching with severity levels (low/medium/high/critical) and automatic admin alerts
- πŸ“Š **Comprehensive Analytics & Observability** – Full tenant-level analytics logging with SQLite backend:
  - Tool usage breakdown (RAG, Web, Admin, LLM) with latency and token tracking
  - RAG recall/precision indicators (average hits, scores, top scores)
  - Per-tenant query volume and active users
  - Red-flag violations with timestamps and confidence scores
  - LLM token logs and latency metrics
- 🌐 **Live Web Search** – DuckDuckGo-based MCP server with English-biased results
- 🏒 **Multi-Tenant Isolation** – Complete tenant isolation with centralized tenant ID management; backend enforces strict isolation for chat, ingestion, and admin ops
- πŸ”„ **Intelligent Multi-Tool Orchestration** – MCP agent orchestrator autonomously selects optimal tool chains (RAG + Web + LLM, etc.) based on query intent and context
- ⚑ **Robust Error Handling** – Structured error responses, retry mechanisms, and graceful fallbacks (e.g., if RAG fails β†’ fallback to LLM-only)

### Enterprise Features

- πŸ” **Regex-Based Red-Flag Detection** – Support for complex regex patterns with keyword fallback and semantic scoring
- πŸ“ˆ **Real-Time Analytics Dashboard** – Per-tenant analytics with configurable time windows (7, 30, 90 days)
- πŸ› οΈ **Admin API Endpoints** – `/admin/violations`, `/admin/tools/logs`, `/admin/tenants` for comprehensive governance
- 🧠 **Agent Debug & Planning** – `/agent/debug` and `/agent/plan` endpoints for observability and tool selection inspection
- πŸ’Ύ **Persistent Analytics Storage** – SQLite-based analytics store with indexes for fast queries

---

## How to Run the Space
### Prerequisites

1. **Backend services running**:
   - FastAPI API (`uvicorn backend.api.main:app --port 8000`)
   - Unified MCP server (port 8900) as described in `backend/README.md`
   - Optional: Ollama / Groq credentials for the LLM client
   
   **Quick Start**: Run `start.bat` (Windows) to launch all services automatically.
2. **Python 3.10+** with the dependencies in `requirements.txt`

### Installation

1. **Install dependencies**:
   ```bash
   pip install -r requirements.txt
   ```

2. **Start the Gradio app**:
   ```bash
   python app.py
   ```

3. **Access the interface**:
   - Local: `http://localhost:7860`
   - The app will automatically connect to the backend at `http://localhost:8000`

### Usage

The Gradio UI exposes four tabs once you launch `app.py`:

1. **Chat** – enter your Tenant ID, ask questions, and see multi-tool MCP responses with autonomous tool orchestration.

2. **Document Ingestion** – toggle between Raw Text, URL, or File Upload to populate the tenant RAG index. View and manage your ingested documents with delete functionality.

3. **Knowledge Base Library** – comprehensive document management interface with:
   - **Statistics Dashboard**: Visual cards showing total documents, document types (Text, PDF, FAQ, Link), and average length
   - **Interactive Charts**: Plotly pie chart displaying document type distribution
   - **Semantic Search**: Search your knowledge base with relevance scoring
   - **Type Filtering**: Filter documents by type (all, text, pdf, faq, link)
   - **Document Management**: View all documents in a table with preview, delete individual documents, or delete all at once
   - **Auto-refresh**: Document lists automatically update after ingestion or deletion

4. **Admin Analytics** – comprehensive analytics dashboard with visualizations:
   - **Statistics Cards**: Total queries, active users, red flags, and RAG searches
   - **Interactive Bar Charts**: 
     - Tool Usage Count (RAG, Web, Admin tools)
     - Average Tool Latency (performance metrics)
     - RAG Quality Metrics (hits, scores, recall indicators)
   - **Tool Usage Table**: Detailed breakdown of tool performance with counts, latency, success/error rates, and token usage
   - **Formatted Summary**: Key metrics displayed in an easy-to-read format
   - Click "πŸ”„ Fetch Analytics Snapshot" to load the latest data

5. **Admin Rules & Compliance** – upload/delete governance rules that are stored via the backend `/admin/rules` API.

**Tip:** Every action requires a tenant ID. The tenant ID is now managed centrally and persists across page refreshes. The Knowledge Base Library and Admin Analytics tabs feature beautiful, modern UI with dark theme styling and interactive Plotly visualizations.

### Frontend (Next.js) Operator Console

The companion Next.js frontend (`frontend/`) now exposes dedicated pages for each workflow:

| URL | Description |
| --- | --- |
| `/` | Landing page with hero + quick access panels |
| `/ingestion` | Data ingestion walkthrough (text/URL/files) with document management |
| `/chat` | Chat console wrapper around the MCP agent |
| `/analytics` | Analytics overview and explainer |
| `/admin-rules` | Admin rule ingestion explainer |
| `/knowledge-base` | View all ingested documents with search, filter, and delete functionality |

**Key Features:**
- **Centralized Tenant ID Management** – Tenant ID is managed globally via React Context and persists in localStorage
- **Document Management** – View, search, filter, and delete documents from the knowledge base
- **Improved Error Handling** – Clear error messages with retry options for failed operations
- **Real-time Updates** – Document lists automatically refresh after ingestion or deletion

Run the console locally with:
```bash
cd frontend
npm install
npm run dev
```
Then open `http://localhost:3000`. The navbar links on the landing page route to each section, and you can link directly to those URLs for demo purposes. The tenant ID selector is available in the navbar on all pages.

---

## API Endpoints

### Agent Endpoints

| Purpose | Method & Path | Description |
| --- | --- | --- |
| Chat with agent | `POST /agent/message` | Main chat endpoint with `tenant_id`, `message`, optional history |
| Agent debug | `POST /agent/debug` | Returns detailed debugging info: reasoning trace, tool selection, intent classification |
| Agent plan | `POST /agent/plan` | Returns tool selection plan without execution (intent, tool scores, planned steps) |

### RAG Endpoints

| Purpose | Method & Path | Description |
| --- | --- | --- |
| Ingest document | `POST /rag/ingest-document` | Accepts `source_type`, `content`, metadata (filename, URL, doc_id). Supports raw text, URLs, PDFs, DOCX, TXT, and Markdown files |
| Ingest file | `POST /rag/ingest-file` | Multipart upload with `x-tenant-id` header (PDF/DOCX/TXT/MD) |
| List documents | `GET /rag/list?tenant_id={id}&limit={n}&offset={n}` | Returns all documents for a tenant with pagination. Requires `x-tenant-id` header or `tenant_id` query parameter |
| Delete document | `DELETE /rag/delete/{document_id}?tenant_id={id}` | Deletes a specific document by ID. Requires `x-tenant-id` header or `tenant_id` query parameter |
| Delete all documents | `DELETE /rag/delete-all?tenant_id={id}` | Deletes all documents for a tenant. Requires `x-tenant-id` header or `tenant_id` query parameter |

### Admin & Governance Endpoints

| Purpose | Method & Path | Description |
| --- | --- | --- |
| List rules | `GET /admin/rules?detailed=true` | Get all rules (use `detailed=true` for regex/severity metadata) |
| Add rule | `POST /admin/rules` | Add rule with optional `pattern` (regex), `severity` (low/medium/high/critical), `description` |
| Delete rule | `DELETE /admin/rules/{rule}` | Delete a specific rule |
| List violations | `GET /admin/violations?days=30&limit=50` | Get red-flag violations with timestamps and confidence scores |
| Tool logs | `GET /admin/tools/logs?tool_name=rag&days=7` | Get detailed tool usage logs with latency and token counts |
| Manage tenants | `GET/POST/DELETE /admin/tenants` | Tenant management endpoints (placeholder implementation) |

### Analytics Endpoints

| Purpose | Method & Path | Description |
| --- | --- | --- |
| Overview | `GET /analytics/overview?days=30` | Comprehensive analytics: total queries, tool usage, red-flag count, RAG quality |
| Tool usage | `GET /analytics/tool-usage?days=30` | Detailed tool usage stats: counts, latency, tokens, success/error rates |
| Red flags | `GET /analytics/redflags?limit=50&days=30` | Recent red-flag violations for tenant |
| Activity | `GET /analytics/activity?days=30` | Tenant activity summary: queries, active users, last query timestamp |
| RAG quality | `GET /analytics/rag-quality?days=30` | RAG quality metrics: avg hits, scores, latency (recall/precision indicators) |

All calls are proxied through the FastAPI backend running at `http://localhost:8000`. Ensure those services are online before launching the Space.

---

## Architecture Highlights

### Enterprise-Grade Features

1. **Autonomous Multi-Step Planning**: The agent uses LLM-powered planning to determine optimal tool sequences, with memory of previous tool outputs in multi-step workflows.

2. **Regex-Based Governance**: Admin rules support regex patterns with fallback to keyword matching and semantic similarity scoring for flexible policy enforcement.

3. **Comprehensive Analytics**: All tool usage, RAG searches, LLM calls, and red-flag violations are logged to SQLite with indexed queries for fast analytics retrieval.

4. **Enhanced RAG Pipeline**: Documents are chunked with optimal size (400-600 tokens) and enriched with metadata (source URL, timestamp, document type) for better retrieval.

5. **Structured Error Handling**: All errors are logged with context, and the system gracefully falls back (e.g., if RAG fails β†’ use LLM-only, if web fails β†’ skip web).

### Data Storage

- **SQLite Databases** (for demo/development):
  - `data/admin_rules.db` - Admin rules with regex patterns and severity
  - `data/analytics.db` - Analytics events, tool usage, violations, RAG metrics

- **Production Ready**: Can easily swap SQLite for PostgreSQL/Supabase for production deployments.

---

## Testing & Diagnostics

IntegraChat ships with several helper scripts to validate the full stack end-to-end:

- `python verify_tenant_isolation.py`  
  Runs a comprehensive suite that covers analytics logging, admin rule storage, API reachability, andβ€”most importantlyβ€”multi-tenant RAG isolation.  
  - βœ… **Prerequisites:** FastAPI backend plus all MCP servers (RAG/Web/Admin) running locally.  
  - βœ… **What it checks:**  
    1. Direct database writes via the analytics and rules stores  
    2. CRUD over the `/admin/*` and `/analytics/*` endpoints  
    3. RAG ingestion and isolation by issuing queries as multiple tenants and ensuring secrets never leak across IDs  
  - βœ… **Pass criteria:** At least 80β€―% of the sub-tests succeed (the RAG isolation test must pass for overall success).

- `python check_rag_database.py`  
  Provides a low-level inspection of the RAG datastore. It connects straight to the pgvector/Postgres instance, lists all tenant IDs, prints sample chunks, and runs `search_vectors()` directly to ensure the SQL `WHERE tenant_id = …` filter is behaving as expected. Use this script when diagnosing suspected cross-tenant leakage or when seeding demo data.

- `python test_manual.py`  
  The existing manual test runner remains useful for smoke-testing analytics logging, admin rule CRUD, and API response codes. Run it whenever you adjust schemas or update MCP endpoints.

> **Tip:** All scripts assume the Python virtual environment is active (`source venv/bin/activate` or `.\venv\Scripts\activate`) and that `.env` contains the MCP server URLs/LLM settings noted earlier.

---

## Demo Video

πŸŽ₯ **[Demo Video Placeholder]** - Coming soon!

Watch how IntegraChat uses MCP to power autonomous agents with multi-tool selection, RAG retrieval, and enterprise governance.

---

## Social Media

πŸ“± **[Social Media Post Placeholder]** - Coming soon!

Follow us for updates and demos of IntegraChat in action!

---

## Team Member(s)

- **Your Name Here** - Developer & MCP Enthusiast

---

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---

## Technical Stack

- **Backend**: FastAPI with async/await for high-performance MCP orchestration
- **Frontend**: Gradio interface with Plotly visualizations + Next.js operator console
- **UI Libraries**: Plotly for interactive charts, Gradio for web interface
- **LLM Integration**: Ollama (local) or Groq (cloud) via configurable backend
- **Vector Store**: pgvector (via Supabase) or SQLite embeddings
- **Analytics**: SQLite with indexed queries for fast analytics
- **MCP Server**: Unified MCP server (port 8900) exposing all tools via namespaces
- **Database**: PostgreSQL with pgvector extension for RAG embeddings, SQLite for analytics

## Key Technical Features

### Tenant Isolation & Normalization
- **Strict tenant isolation** enforced at database level with `WHERE tenant_id = ...` filters
- **Automatic tenant ID normalization** handles whitespace and formatting differences
- Documents can be listed and deleted consistently across different tenant_id formats
- All operations validate tenant ownership before execution

### RAG Search & Retrieval
- **Optimized similarity threshold** (default 0.3) for better recall of relevant documents
- **Intelligent fallback** returns top result even if below threshold to ensure knowledge base content is accessible
- **Pattern-based tool selection** automatically triggers RAG for admin questions, fact lookups, and internal knowledge queries
- **Response unwrapping** ensures seamless integration between MCP server and orchestrator

### MCP Server Architecture
- **Unified server** running on a single port (default 8900) for all namespaced tools
- **Dual protocol support**: Both MCP protocol (POST with JSON) and RESTful HTTP (GET/DELETE)
- **Response wrapping**: Standardized response format with automatic unwrapping in clients
- **Error handling**: Comprehensive error responses with detailed messages for debugging

## UI Features

### Knowledge Base Library
- **Visual Statistics**: Real-time document counts and type distribution
- **Interactive Charts**: Plotly pie charts for document type visualization
- **Advanced Search**: Semantic search across all ingested documents with relevance scoring
- **Smart Filtering**: Filter by document type (text, PDF, FAQ, link)
- **Bulk Operations**: Delete individual documents or all documents at once
- **Auto-refresh**: Lists automatically update after operations

### Admin Analytics Dashboard
- **Statistics Cards**: Key metrics displayed in visually appealing cards with icons
- **Tool Usage Visualization**: Bar charts showing tool invocation counts and performance
- **Latency Metrics**: Visual representation of tool response times
- **RAG Quality Analysis**: Charts displaying search quality metrics (hits, scores, recall)
- **Detailed Tables**: Comprehensive tool usage breakdown with success/error rates
- **Dark Theme**: Modern UI with dark background and white text for better readability
- **Real-time Updates**: Fetch latest analytics data with a single click

## Acknowledgments

- Built with [Model Context Protocol (MCP)](https://modelcontextprotocol.io/)
- Powered by [Gradio](https://gradio.app/) for the interface
- Visualizations created with [Plotly](https://plotly.com/python/)
- Backend built with [FastAPI](https://fastapi.tiangolo.com/)
- Analytics and governance features inspired by enterprise AI platform requirements

---

<div align="center">

**Made with ❀️ for the MCP Hackathon**

**IntegraChat: Enterprise-Grade MCP Autonomous Agent Platform**

[⬆ Back to Top](#integrachat--enterprise-mcp-autonomous-agent-platform)

</div>