How oasis-pandey/slack-ai-agent works
Purpose and Runtime
Canvas Slack Agent is a single-user Slack bot that answers Canvas LMS questions using live Canvas data. It runs as a long-lived Python worker in Slack Socket Mode, not as an HTTP web service. Primary entry point: canvas_bot/main.py; local agent CLI is exposed by canvas_bot/agent.py.
Deployment is containerized in Dockerfile and configured for Railway worker deployment through railway.json. Credentials use SQLite locally (data/creds.db) or optional Postgres.
Slack Event and UI Boundary
canvas_bot/main.py owns Slack Bolt wiring: app_mention, App Home, interactive controls, status-message updates, and announcement modal submission. It receives events over Slack’s outbound Socket Mode WebSocket, preventing public inbound endpoints.
Slack-specific pure transformations are isolated from the runtime integration:
• canvas_bot/slack/helpers.py
• canvas_bot/slack/blocks.py
• canvas_bot/main.py
The handler deduplicates Slack retries by client message ID before an agent run, then posts/edits a threaded progress response.
Thread Context as Memory
The system is intentionally agent-stateless. For each mention, the Slack layer reads the current thread, limits it to the latest 20 messages, and converts messages to speaker-labeled conversational history. It does not retain loose channel history or agent memory in the database.
Key boundary paths:
• canvas_bot/main.py
• canvas_bot/slack/helpers.py
This makes Slack the source of conversational context and allows restarts without losing thread history.
Agent Orchestration
canvas_bot/agent.py implements a bounded ReAct loop against Groq’s llama-3.3-70b-versatile. It accepts question plus reconstructed thread history, asks the model to choose tools, executes calls, feeds results back to the model, and returns a final natural-language response and structured announcement intent where applicable.
Controls include a six-step maximum, per-model and per-tool timeouts, and an overall agent deadline. The agent is Slack-agnostic; Slack formatting and delivery remain in the outer layer.
Core paths:
• canvas_bot/agent.py
• canvas_bot/main.py
Canvas Tool Gateway
canvas_bot/canvas/bridge.py is the protocol boundary between Groq function calling and canvas-mcp-server. It launches the MCP server as a stdio subprocess, translates MCP tool schemas/results into Groq-compatible tool calls, and restricts exposure to an allowlist of Canvas operations. This limits model tool selection to supported reads and narrowly scoped writes.
Core paths:
• canvas_bot/canvas/bridge.py
• canvas_bot/agent.py
• canvas_bot/canvas/__init__.py
External dependencies are Canvas MCP and Canvas’s REST API, authenticated per user with a Canvas Personal Access Token and base URL.
Direct REST and Local Write Tools
Some operations bypass MCP. canvas_bot/canvas/rest.py calls Canvas REST directly for structured announcement and planner-related data. canvas_bot/canvas/local_tools.py provides the local planner-note creation tool absent from Canvas MCP.
Course-visible writes—announcements, discussions, posts, and replies—require explicit user confirmation through Slack UX. Private planner notes can be created directly. Unsupported destructive or academic actions are excluded.
Relevant paths:
• canvas_bot/canvas/rest.py
• canvas_bot/canvas/local_tools.py
• canvas_bot/main.py
Credential Storage and Trust Boundaries
canvas_bot/store.py persists each user’s Canvas URL and PAT encrypted at rest with Fernet/AES. The storage backend uses local SQLite by default and switches to Postgres through DATABASE_URL. App Home connects or disconnects credentials; disconnect deletes stored user data.
Sensitive service boundaries:
• Slack tokens and Socket Mode: Slack API
• GROQ_API_KEY: Groq inference API
• encrypted Canvas PAT/base URL: canvas_bot/store.py
• Canvas data access: MCP subprocess and direct REST client
Primary paths:
• canvas_bot/store.py
• canvas_bot/main.py
• data/creds.db
Main entry points: main.py Slack worker · agent.py Bounded ReAct agent · rest.py Direct REST client · helpers.py Thread context
Components 15
Slack boundary
Socket Mode event handling and thread-based UX.
- Slack API / Socket Mode · Delivers events over an outbound WebSocket.
- Slack worker · Owns events, interactive UX, progress updates, and confirmation flow.
canvas_bot/main.py - Thread context · Builds speaker-labeled history from the latest 20 thread messages.
canvas_bot/slack/helpers.py - Slack blocks
canvas_bot/slack/blocks.py
Agent runtime
Stateless bounded orchestration.
- Local agent CLI
canvas_bot/agent.py - Bounded ReAct agent · Runs up to six model-tool steps under tool, model, and overall deadlines.
canvas_bot/agent.py - Groq inference API · Hosts llama-3.3-70b-versatile function calling.
Canvas access
MCP gateway plus direct Canvas tools.
- Canvas MCP bridge · Maps MCP schemas and results to an allowlisted Groq tool surface.
canvas_bot/canvas/bridge.py - canvas-mcp-server
- Direct REST client · Handles structured announcement and planner-related data.
canvas_bot/canvas/rest.py - Local planner tools · Creates private planner notes without MCP.
canvas_bot/canvas/local_tools.py - Canvas REST API · Accessed with each user's Canvas URL and PAT.
Credentials and deployment
Encrypted user credentials and worker packaging.
- Encrypted credential store · Stores Canvas URL and PAT encrypted with Fernet/AES.
canvas_bot/store.py - Credentials database · Uses local SQLite by default or Postgres via DATABASE_URL.
data/creds.db - Container worker deployment · Containerized worker configured for Railway.
Dockerfile
Connections 18
- Slack API / Socket Mode → Slack worker: Socket Mode events
- Slack worker → Thread context: read and limit thread
- Thread context → Slack worker: conversation history
- Slack worker → Slack blocks: render controls
- Slack worker → Slack API / Socket Mode: post/edit threaded status
- Slack worker → Encrypted credential store: connect / disconnect user
- Encrypted credential store → Credentials database: persist encrypted values
- Slack worker → Bounded ReAct agent: question and thread history
- Local agent CLI → Bounded ReAct agent: invoke locally
- Bounded ReAct agent → Groq inference API: model and tool calls
- Bounded ReAct agent → Canvas MCP bridge: Canvas tool calls
- Canvas MCP bridge → canvas-mcp-server: launch and communicate over stdio
- canvas-mcp-server → Canvas REST API: allowlisted Canvas operations
- Bounded ReAct agent → Direct REST client: direct structured operations
- Direct REST client → Canvas REST API: REST requests
- Bounded ReAct agent → Local planner tools: private planner note
- Slack worker → Direct REST client: confirmed course-visible writes · Announcements and other visible writes require Slack confirmation.
- Container worker deployment → Slack worker: runs worker