This guide explains how to set up the Agent Divergence Analysis as a local MCP (Model-Controller-Presenter) server tool that can run on your database. This setup provides a robust way to find agents that diverge significantly from population norms through a flexible API rather than requiring direct script execution.
- Database Integration: Connect directly to your simulation database
- API Interface: Access divergence analysis via REST endpoints or command-line
- Consistent Results: Cache computed results for efficiency
- Flexible Deployment: Run on the same machine as your database or separately
- Programmatic Access: Integrate with dashboards, notebooks, or other tools
pip install flask numpy scipy pandas faiss-cpu tabulate requestsEnsure these files are in your project directory:
agent_divergence_detector.py- The core divergence detection functionalityagent_divergence_server.py- The Flask server providing the APIagent_divergence_client.py- The command-line client for interacting with the server
Connect to your database:
python agent_divergence_server.py --db-path "path/to/simulation.db" --faiss-index-path "path/to/faiss_index"Options:
--port- Port to run the server on (default: 5000)--host- Host address to bind to (default: 127.0.0.1)--results-dir- Directory to store results (default: "results")--debug- Run in debug mode for development
Once the server is running, you can use the client to find divergent agents:
python agent_divergence_client.py find-divergent --metric mahalanobis_distance --top 5Or find common divergent agents across multiple metrics:
python agent_divergence_client.py find-common-divergentCheck details of a specific agent:
python agent_divergence_client.py get-agent 56q2nhmuN2SqH9beAEmVqoThe server works with SQLite databases containing:
agentstable with agent metadataagent_statestable with agent state historyagent_actionstable with agent actions
Additionally, a FAISS index containing agent state vectors is required:
<faiss_index_path>.faiss: The actual index data<faiss_index_path>.json: Metadata including vector IDs
The server exposes these API endpoints:
| Endpoint | Method | Description |
|---|---|---|
/api/health |
GET | Check server status |
/api/metrics |
GET | List available distance metrics |
/api/divergent |
GET | Find divergent agents using a specific metric |
/api/common-divergent |
GET | Find agents that are divergent across multiple metrics |
/api/agent/<agent_id> |
GET | Get detailed information about a specific agent |
import requests
# Find divergent agents using Mahalanobis distance
response = requests.get("http://localhost:5000/api/divergent", params={
"metric": "mahalanobis_distance",
"top": 5,
"threshold": 2.0
})
data = response.json()
top_divergent = data["top_divergent_agents"]
# Process the results
for agent in top_divergent:
print(f"Agent {agent['short_id']}: {agent['divergence_score']}")# Find divergent agents and format as table
$response = Invoke-RestMethod -Uri "http://localhost:5000/api/divergent?metric=mahalanobis_distance&top=5"
$response.top_divergent_agents | Format-Table short_id, divergence_score, @{N='Type';E={$_.metadata.agent_type}}The REST API can be integrated into dashboards like Grafana, Kibana, or custom web interfaces by making HTTP requests to the endpoints and visualizing the returned JSON data.
For larger deployments:
- Database Connection Pooling: Modify the server to use connection pooling for better performance
- Docker Containerization: Package the server in a Docker container for easy deployment
- Load Balancing: Set up multiple server instances behind a load balancer for higher throughput
- Persistent Caching: Implement Redis or another caching system for results persistence
Common issues:
- Server Won't Start: Check that the database path is correct and that Flask is installed
- Slow Performance: Large FAISS indices can be memory-intensive; consider using a machine with more RAM
- Database Errors: Ensure the database schema matches the expected format
- "Agent Not Found": Confirm the agent IDs exist in both the database and FAISS index