ZMP Manual Chatbot Backend
A production-ready FastAPI backend for the ZMP Manual Chatbot with OAuth2 authentication, multi-agent workflow, and MCP Server integration for intelligent document retrieval and response generation.
Features
- 🔐 OAuth2 Authentication: Secure authentication with Keycloak integration
- 🤖 Multi-Agent Workflow: Intelligent planning, research, and response generation
- 📚 MCP Server Integration: Async integration with MCP Server for knowledge retrieval
- 🔄 Smart Answer Reuse: Global semantic search with reusable answer functionality
- 💬 Thread Continuity: Advanced session management with thread-based conversation tracking
- 📖 OpenAPI Documentation: Complete API documentation with Swagger UI
- 🔄 Streaming Responses: Real-time streaming of multi-agent workflow progress
- 🏗️ Production-Ready: Logging, error handling, health checks, session management
- 🐳 Containerization-Ready: Docker/Kubernetes deployment support
- 🧪 Comprehensive Testing: Test suite for API and core logic
Getting Started
Prerequisites
- Python 3.12+
- MCP Server running and accessible (see
MCP_SERVER_URLin config) - Keycloak server configured for OAuth2 authentication
Installation
Clone this repository:
# Clone the repo
cd /path/to/your/workspace
git clone <your-repo-url>
cd zmp-manual-chatbot-backend
Set up a virtual environment and install dependencies:
python3 -m venv .venv
source .venv/bin/activate
poetry install
Configuration
Environment Variables
Create a .env file or set the following environment variables:
# MCP Server Configuration
export MCP_SERVER_URL='http://localhost:5371/mcp'
# Keycloak OAuth2 Configuration
export KEYCLOAK_SERVER_URL='https://keycloak.ags.cloudzcp.net/auth'
export KEYCLOAK_REALM='ags'
export KEYCLOAK_CLIENT_ID='zmp-client'
export KEYCLOAK_CLIENT_SECRET='your-client-secret'
export KEYCLOAK_REDIRECT_URI='http://localhost:5370/api/manual-chatbot/v1/auth/callback'
# Server Configuration
export HOST='0.0.0.0'
export PORT='5370'
export LOG_LEVEL='info'
Keycloak Setup
- Configure your Keycloak client with:
- Valid Redirect URIs:
http://localhost:5370/api/manual-chatbot/v1/auth/callback - Web Origins:
http://localhost:5370 - Client Protocol:
openid-connect - Access Type:
confidential
- Valid Redirect URIs:
Running Locally
Start the FastAPI app with Uvicorn:
poetry run uvicorn src.zmp_manual_chatbot_backend.main:app --reload --host 0.0.0.0 --port 5370
- API Documentation: http://localhost:5370/api/manual-chatbot/v1/api-docs
- Health Check: http://localhost:5370/health
- OAuth2 Callback: http://localhost:5370/api/manual-chatbot/v1/auth/callback
API Usage
Authentication System
The ZMP Manual Chatbot Backend now uses the zmp_authentication_provider package for comprehensive OAuth2 authentication with Keycloak integration.
🔐 Authentication Overview
The authentication system provides:
- OAuth2 with Keycloak: Enterprise-grade authentication
- Session Management: Secure session handling and persistence
- Token Management: Access token and refresh token lifecycle
- User Profile Access: Authenticated user information retrieval
📋 Available Authentication Endpoints
| Endpoint | Method | Description | Usage |
|---|---|---|---|
/auth/home |
GET | Authentication home page | Entry point for authentication flow |
/auth/authenticate |
GET | Initiate OAuth2 authentication | Redirects to Keycloak login |
/auth/oauth2/callback |
GET | OAuth2 callback handler | Handles Keycloak response |
/auth/access_token |
GET | Retrieve current access token | Get token for API calls |
/auth/refresh_token |
PATCH | Refresh expired access token | Renew authentication |
/auth/logout |
GET | Logout and clear session | End authentication session |
/auth/profile |
GET | Get authenticated user profile | Retrieve user information |
🚀 Client Authentication Flow
Step 1: Initiate Authentication
# Navigate user to authentication endpoint
GET /api/manual-chatbot/v1/auth/authenticate
This will redirect to Keycloak for user login.
Step 2: Handle OAuth2 Callback
After successful login, Keycloak redirects to:
/api/manual-chatbot/v1/auth/oauth2/callback?code=AUTH_CODE&state=STATE
Step 3: Retrieve Access Token
# Get access token for API calls
curl -X GET \
'http://localhost:5370/api/manual-chatbot/v1/auth/access_token' \
-H 'Cookie: session_id=YOUR_SESSION_COOKIE'
Response:
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "Bearer",
"expires_in": 3600
}
Step 4: Use Token for API Calls
# Make authenticated API calls
curl -X POST \
'http://localhost:5370/api/manual-chatbot/v1/chat/query' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"question": "Your question here",
"chat_history": []
}'
🔄 Token Management
Refresh Expired Tokens
# Refresh access token when expired
curl -X PATCH \
'http://localhost:5370/api/manual-chatbot/v1/auth/refresh_token' \
-H 'Cookie: session_id=YOUR_SESSION_COOKIE'
Check User Profile
# Get authenticated user information
curl -X GET \
'http://localhost:5370/api/manual-chatbot/v1/auth/profile' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'
Response:
{
"sub": "user-id",
"preferred_username": "username",
"email": "user@example.com",
"name": "User Name",
"roles": ["user", "admin"]
}
🖥️ Web Client Integration
JavaScript/Frontend Integration
// 1. Initiate authentication
window.location.href = '/api/manual-chatbot/v1/auth/authenticate';
// 2. After callback, get access token
async function getAccessToken() {
const response = await fetch('/api/manual-chatbot/v1/auth/access_token', {
credentials: 'include' // Include session cookies
});
if (response.ok) {
const data = await response.json();
return data.access_token;
}
throw new Error('Authentication required');
}
// 3. Make authenticated API calls
async function chatQuery(question, accessToken) {
const response = await fetch('/api/manual-chatbot/v1/chat/query', {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
question: question,
chat_history: []
})
});
return response.json();
}
// 4. Handle token refresh
async function refreshToken() {
const response = await fetch('/api/manual-chatbot/v1/auth/refresh_token', {
method: 'PATCH',
credentials: 'include'
});
if (response.ok) {
return await response.json();
}
// Redirect to authentication if refresh fails
window.location.href = '/api/manual-chatbot/v1/auth/authenticate';
}
// 5. Logout
async function logout() {
await fetch('/api/manual-chatbot/v1/auth/logout', {
credentials: 'include'
});
// Redirect to home or login page
}
📱 Mobile/Native App Integration
// React Native / Mobile app example
class AuthService {
constructor() {
this.baseURL = 'http://your-api-domain.com/api/manual-chatbot/v1';
this.accessToken = null;
}
// Open web view for authentication
async authenticate() {
const authURL = `${this.baseURL}/auth/authenticate`;
// Open WebView or browser for OAuth flow
// Handle redirect to get session
}
// Get access token after authentication
async getAccessToken(sessionCookie) {
const response = await fetch(`${this.baseURL}/auth/access_token`, {
headers: {
'Cookie': sessionCookie
}
});
const data = await response.json();
this.accessToken = data.access_token;
return this.accessToken;
}
// Make authenticated requests
async makeAuthenticatedRequest(endpoint, options = {}) {
const response = await fetch(`${this.baseURL}${endpoint}`, {
...options,
headers: {
...options.headers,
'Authorization': `Bearer ${this.accessToken}`
}
});
if (response.status === 401) {
// Token expired, try refresh
await this.refreshToken();
// Retry request
return this.makeAuthenticatedRequest(endpoint, options);
}
return response;
}
}
🔒 Security Best Practices for Clients
-
Secure Token Storage
- Store tokens securely (Keychain/Keystore for mobile)
- Never log tokens in production
- Use secure HTTP-only cookies when possible
-
Token Lifecycle Management
- Implement automatic token refresh
- Handle token expiration gracefully
- Clear tokens on logout
-
Error Handling
- Handle 401 responses appropriately
- Implement retry logic for network failures
- Provide clear authentication error messages
-
Session Management
- Include credentials/cookies for session-based requests
- Handle session timeouts
- Implement proper logout functionality
🧪 Testing Authentication
You can test the authentication flow using:
- Swagger UI: Visit http://localhost:5370/api/manual-chatbot/v1/api-docs
- Browser: Navigate to authentication endpoints directly
- cURL: Use the examples provided above
- Automated Tests: Run the comprehensive test suite (see Testing section)
🛠️ Authentication Methods
You can authenticate using any of these methods:
- Web Browser Flow: Complete OAuth2 flow in browser
- API Token: Use a Bearer token in the Authorization header
- Session Cookies: Maintain session after initial authentication
- Swagger UI: Built-in authentication for API testing
Chat Query Endpoint
Endpoint: POST /api/manual-chatbot/v1/chat/query
Request Schema:
{
"question": "Your question here",
"chat_history": [],
"image_url": null,
"thread_id": null
}
Enhanced Response Features:
- Reusable Answers: The system now leverages global semantic search to identify and reuse previously generated high-quality answers for similar queries
- Thread Continuity: Automatic thread ID management ensures seamless conversation flow across sessions
- Improved Context Handling: Enhanced MCP result extraction with robust parsing for various record formats
Example cURL:
curl -X 'POST' \
'http://localhost:5370/api/manual-chatbot/v1/chat/query' \
-H 'accept: application/json' \
-H 'Authorization: Bearer YOUR_OAUTH2_TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"question": "Kubernetes에 대해 알려줘",
"chat_history": [],
"image_url": null
}'
Response: Streaming JSON with multi-agent workflow progress:
{"plan": [], "final_answer": null, "doc_urls": null, "citation_map": null}
{"plan": ["Step 1", "Step 2"], "final_answer": null, "doc_urls": null, "citation_map": null}
{"plan": [], "final_answer": "Complete answer with citations", "doc_urls": ["url1"], "citation_map": {...}}
Complete Authentication API Reference
The authentication system provides a full suite of endpoints for client integration:
| Endpoint | Method | Description | Authentication Required |
|---|---|---|---|
/auth/home |
GET | Authentication home page | No |
/auth/authenticate |
GET | Initiate OAuth2 flow | No |
/auth/oauth2/callback |
GET | Handle OAuth2 callback | No |
/auth/access_token |
GET | Get current access token | Session |
/auth/refresh_token |
PATCH | Refresh access token | Session |
/auth/logout |
GET | Logout and clear session | Session |
/auth/profile |
GET | Get user profile | Bearer Token |
Base URL: http://localhost:5370/api/manual-chatbot/v1
For detailed client integration examples, see the Authentication System section above.
Project Structure
zmp-manual-chatbot-backend/
├── src/
│ └── zmp_manual_chatbot_backend/
│ ├── __init__.py
│ ├── main.py # FastAPI app entrypoint with OAuth2 config
│ ├── config.py # Global settings (MCP, Keycloak, etc.)
│ ├── mcp_client.py # Async client for MCP Server
│ ├── service.py # Business logic/multi-agent workflow
│ ├── schemas.py # Pydantic models for API
│ ├── router.py # Chat API endpoints
│ ├── auth_router.py # OAuth2 authentication endpoints
│ ├── auth_service.py # Authentication service layer
│ ├── oauth2_keycloak.py # Keycloak OAuth2 integration
│ ├── auth_models.py # Authentication Pydantic models
│ ├── session.py # Session management
│ └── agents.py # Multi-agent workflow definitions
├── tests/
│ ├── __init__.py
│ ├── test_chat.py # Tests for chat API
│ └── outputs/
│ └── __init__.py
├── k8s/ # Kubernetes deployment files
├── pyproject.toml
├── README.md
├── logging.conf
└── .env # Environment configuration
Key Components
main.py: FastAPI app with OAuth2 security schemes and custom OpenAPI schemarouter.py: Chat query endpoint with authentication requirementsauth_router.py: OAuth2 callback and authentication endpointsservice.py: Multi-agent workflow orchestrationagents.py: Individual agent implementations (planner, researcher, etc.)schemas.py: Pydantic models for API requests/responsesoauth2_keycloak.py: Keycloak integration and JWT validation
Multi-Agent Workflow
The chatbot uses a sophisticated multi-agent workflow:
- Query Processing Agents: Query rewriter, anonymization, and optimization
- Planning Agents: Intelligent task breakdown and execution strategy
- Context Retrieval Agents: MCP server integration and chat history search with global semantic search
- Answer Generation Agents: Smart answer synthesis with reusable answer prioritization
- Response Enhancement: Citation generation and answer quality scoring
Docker Usage
# Build the Docker image
docker build -t zmp-manual-chatbot-backend .
# Run the container
docker run -e MCP_SERVER_URL=http://localhost:5371/mcp \
-e KEYCLOAK_SERVER_URL=https://keycloak.ags.cloudzcp.net/auth \
-e KEYCLOAK_CLIENT_SECRET=your-secret \
-p 5370:5370 zmp-manual-chatbot-backend
Kubernetes Deployment
See the k8s/ directory for Kubernetes deployment configurations:
backend-deployment.yaml- Main application deploymentingress.yaml- Ingress configurationnamespace.yaml- Namespace setupmonitoring.yaml- Monitoring and logging
Documentation
📚 Complete Documentation Suite
For comprehensive information about the ZMP Manual Chatbot Backend:
For Developers
- Configuration Guide - Environment setup and configuration options
- API Reference - Complete API documentation with examples
- Architecture Overview - System design and components
For Administrators
- Deployment Guide - Production deployment instructions
- Security Documentation - Security implementation and best practices
🔧 Advanced Features
Multi-Agent Workflow System
The chatbot uses a sophisticated multi-agent architecture:
- Query Processing Agents: Query rewriter, anonymization, language detection
- Planning Agents: Intelligent task breakdown, context-aware routing
- Information Retrieval Agents: MCP server integration, chat history search
- Response Generation Agents: LLM-powered synthesis, citation generation
LLM Provider Support
- Ollama Integration: Local model deployment with automatic model management
- OpenAI Integration: Cloud-based LLM services with intelligent fallback
- Automatic Provider Detection: Seamless switching between available providers
Security Features
- OAuth2 with Keycloak: Enterprise-grade authentication and authorization
- JWT Validation: Secure token-based API access
- RBAC Support: Role-based access control for granular permissions
- Security Hardening: Comprehensive security headers and validation
🚀 Deployment Options
Development Environment
# Quick start for development
poetry install
poetry run uvicorn src.zmp_manual_chatbot_backend.main:app --reload
Docker Deployment
# Optimized container deployment
docker build -t zmp-manual-chatbot-backend .
docker run -p 5370:5370 zmp-manual-chatbot-backend
Kubernetes Production
Complete production-ready Kubernetes manifests available in k8s/ directory with:
- High availability deployment
- Auto-scaling configuration
- Security policies and network isolation
- Monitoring and observability integration
Testing
Test Suite
# Run all tests
pytest
# Run with coverage
pytest --cov=src/zmp_manual_chatbot_backend
# Run specific test categories
pytest tests/test_chat.py -v
Test Categories
- Unit Tests: Individual component testing
- Integration Tests: API endpoint and workflow testing
- Security Tests: Authentication and authorization validation
- Performance Tests: Load testing and performance benchmarks
Development
Development Workflow
- Setup Environment: Follow installation and configuration guides
- Code Development: Use provided development tools and standards
- Testing: Ensure comprehensive test coverage
- Documentation: Update relevant documentation for changes
- Quality Checks: Run linting, type checking, and security scans
Development Tools
- Poetry: Dependency management and packaging
- Ruff: Fast Python linter and code formatter
- mypy: Static type checking
- pytest: Testing framework with fixtures and mocking
- Black: Code formatting (integrated with Ruff)
Adding New Features
Multi-Agent Extensions
- Define new agent in
agents.pywith proper typing - Integrate with workflow in
service.py - Add comprehensive tests and documentation
- Update API documentation if needed
Authentication Extensions
- OAuth2 configuration:
oauth2_keycloak.py - Authentication endpoints:
auth_router.py - Security schemes:
main.py(custom OpenAPI) - RBAC extensions: Role-based access patterns
Monitoring & Observability
Application Metrics
- Request count, duration, and error rates
- Multi-agent workflow performance metrics
- External service health and response times
- Resource utilization and performance trends
Health Monitoring
- Health Endpoint:
/healthfor basic health checks - Dependency Checks: MCP server, Keycloak, LLM provider status
- Performance Monitoring: Response time tracking and alerting
- Security Monitoring: Authentication events and suspicious activity
Troubleshooting
Common Issues
Authentication Problems
- Issue: OAuth2 authentication failures
- Solution: Verify Keycloak client configuration, check redirect URIs, review token validation logs
- Debug: Enable debug logging and check JWT token structure
LLM Provider Issues
- Issue: "No valid LLM provider available"
- Solution: Ensure Ollama service is running OR OpenAI API key is configured
- Debug: Check provider connectivity and model availability
Performance Issues
- Issue: Slow response times or high resource usage
- Solution: Monitor resource limits, optimize model selection, review query complexity
- Debug: Enable performance metrics and analyze bottlenecks
Integration Problems
- Issue: MCP server connection failures
- Solution: Test MCP server connectivity, verify network configuration
- Debug: Check logs for connection errors and timeout issues
Getting Help
- Documentation: Comprehensive guides available in
docs/directory - Logs: Check application logs with
tail -f logs/app.log - Health Checks: Use
/healthendpoint for service diagnostics - Configuration Validation: Built-in validation catches common configuration errors
For detailed troubleshooting guides and advanced configuration options, see the complete documentation.
Contributing
Development Standards
- Follow PEP 8 Python style guidelines
- Include comprehensive docstrings for all functions and classes
- Maintain high test coverage (>80%)
- Use type hints throughout the codebase
- Update documentation for all changes
Pull Request Process
- Fork the repository and create a feature branch
- Implement changes with appropriate tests
- Run quality checks:
ruff check,mypy,pytest - Update relevant documentation
- Submit pull request with clear description and testing evidence
Code Review Criteria
- Functionality and correctness
- Security considerations and best practices
- Performance implications
- Test coverage and quality
- Documentation completeness and accuracy
Authors
SK inc. C&C. Modern Tech. Team
License
This project is licensed under the MIT License - see the LICENSE file for details.
For more detailed information about any aspect of the system, please refer to the comprehensive documentation suite.
Metadata
Release files for zmp-manual-chatbot-backend 0.1.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| zmp_manual_chatbot_backend-0.1.9.tar.gz | 100.8 kB | Without provenance | Details for zmp_manual_chatbot_backend-0.1.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| zmp_manual_chatbot_backend-0.1.9-py3-none-any.whl | Python 3 | none | any | Details for zmp_manual_chatbot_backend-0.1.9-py3-none-any.whl |
Total release size: 210.8 kB
Release files / zmp_manual_chatbot_backend-0.1.9.tar.gz
| Download URL | zmp_manual_chatbot_backend-0.1.9.tar.gz |
|---|---|
| Size | 100.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a8537b12334b9ca9a278404bfd117a9b552e66173679b4a9a1d38772b0b2706f
|
|
BLAKE2b-256 checksum How to use checksums |
96cb35e1509148e779d8d81137b907cdb9ac8677ab4528730f459b0ca5d3c735
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.3 Darwin/24.5.0
|
Release files / zmp_manual_chatbot_backend-0.1.9-py3-none-any.whl
| Download URL | zmp_manual_chatbot_backend-0.1.9-py3-none-any.whl |
|---|---|
| Size | 110.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2ba19f05df8b2d61f830290251a5c78d7d1a0415f7f6c458fdd7385858bc425a
|
|
BLAKE2b-256 checksum How to use checksums |
cedf95d46239bb9a7adcb1540797981a2643e946cf9d560b7b836825d929e002
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.3 Darwin/24.5.0
|
Security for zmp-manual-chatbot-backend 0.1.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.