# oasis-pandey/slack-ai-agent architecture

> Architecture diagram of the GitHub repository oasis-pandey/slack-ai-agent, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/oasis-pandey/slack-ai-agent
- Repository: https://github.com/oasis-pandey/slack-ai-agent

## Overview

#### 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: [`canvas_bot/main.py`](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/main.py) (Slack worker), [`canvas_bot/agent.py`](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/agent.py) (Bounded ReAct agent), [`canvas_bot/canvas/rest.py`](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/canvas/rest.py) (Direct REST client), [`canvas_bot/slack/helpers.py`](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/slack/helpers.py) (Thread context)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_slack["Slack boundary"]
  node_slack_api(("Slack API / Socket Mode<br/>external API"))
  node_main["Slack worker<br/>Bolt runtime<br/>[main.py]"]
  node_slack_helpers["Thread context<br/>Slack transforms<br/>[helpers.py]"]
  node_slack_blocks["Slack blocks<br/>UI formatter<br/>[blocks.py]"]
end

subgraph group_agent["Agent runtime"]
  node_agent_cli["Local agent CLI<br/>CLI entrypoint<br/>[agent.py]"]
  node_react_agent["Bounded ReAct agent<br/>agent orchestration<br/>[agent.py]"]
  node_groq(("Groq inference API<br/>external API"))
end

subgraph group_canvas["Canvas access"]
  node_canvas_bridge{{"Canvas MCP bridge<br/>tool gateway<br/>[bridge.py]"}}
  node_canvas_mcp["canvas-mcp-server<br/>stdio subprocess"]
  node_canvas_rest["Direct REST client<br/>Canvas client<br/>[rest.py]"]
  node_local_tools["Local planner tools<br/>local tools<br/>[local_tools.py]"]
  node_canvas_api(("Canvas REST API<br/>external API"))
end

subgraph group_storage["Credentials and deployment"]
  node_credential_store[("Encrypted credential store<br/>persistence<br/>[store.py]")]
  node_creds_db[("Credentials database<br/>SQLite / Postgres<br/>[creds.db]")]
  node_worker_deployment["Container worker deployment"]
end

node_slack_api -->|"Socket Mode events"| node_main
node_main -->|"read and limit thread"| node_slack_helpers
node_slack_helpers -->|"conversation history"| node_main
node_main -->|"render controls"| node_slack_blocks
node_main -->|"post/edit threaded status"| node_slack_api
node_main -->|"connect / disconnect user"| node_credential_store
node_credential_store -->|"persist encrypted values"| node_creds_db
node_main -->|"question and thread history"| node_react_agent
node_agent_cli -->|"invoke locally"| node_react_agent
node_react_agent -->|"model and tool calls"| node_groq
node_react_agent -->|"Canvas tool calls"| node_canvas_bridge
node_canvas_bridge -->|"launch and communicate over stdio"| node_canvas_mcp
node_canvas_mcp -->|"allowlisted Canvas operations"| node_canvas_api
node_react_agent -->|"direct structured operations"| node_canvas_rest
node_canvas_rest -->|"REST requests"| node_canvas_api
node_react_agent -->|"private planner note"| node_local_tools
node_main -.->|"confirmed course-visible writes"| node_canvas_rest
node_worker_deployment -->|"runs worker"| node_main

click node_main "https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/main.py"
click node_slack_helpers "https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/slack/helpers.py"
click node_slack_blocks "https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/slack/blocks.py"
click node_agent_cli "https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/agent.py"
click node_react_agent "https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/agent.py"
click node_canvas_bridge "https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/canvas/bridge.py"
click node_canvas_rest "https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/canvas/rest.py"
click node_local_tools "https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/canvas/local_tools.py"
click node_credential_store "https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/store.py"
click node_creds_db "https://github.com/oasis-pandey/slack-ai-agent/blob/main/data/creds.db"
click node_worker_deployment "https://github.com/oasis-pandey/slack-ai-agent/blob/main/Dockerfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_slack_api,node_main,node_slack_helpers,node_slack_blocks toneBlue
class node_agent_cli,node_react_agent,node_groq toneAmber
class node_canvas_bridge,node_canvas_mcp,node_canvas_rest,node_local_tools,node_canvas_api toneMint
class node_credential_store,node_creds_db,node_worker_deployment toneRose
```

## Components

### Slack boundary

Socket Mode event handling and thread-based UX.

- **Slack API / Socket Mode**: external API · Delivers events over an outbound WebSocket.
- [Slack worker](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/main.py): Bolt runtime · Owns events, interactive UX, progress updates, and confirmation flow. · `canvas_bot/main.py`
- [Thread context](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/slack/helpers.py): Slack transforms · Builds speaker-labeled history from the latest 20 thread messages. · `canvas_bot/slack/helpers.py`
- [Slack blocks](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/slack/blocks.py): UI formatter · `canvas_bot/slack/blocks.py`

### Agent runtime

Stateless bounded orchestration.

- [Local agent CLI](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/agent.py): CLI entrypoint · `canvas_bot/agent.py`
- [Bounded ReAct agent](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/agent.py): agent orchestration · Runs up to six model-tool steps under tool, model, and overall deadlines. · `canvas_bot/agent.py`
- **Groq inference API**: external API · Hosts llama-3.3-70b-versatile function calling.

### Canvas access

MCP gateway plus direct Canvas tools.

- [Canvas MCP bridge](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/canvas/bridge.py): tool gateway · Maps MCP schemas and results to an allowlisted Groq tool surface. · `canvas_bot/canvas/bridge.py`
- **canvas-mcp-server**: stdio subprocess
- [Direct REST client](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/canvas/rest.py): Canvas client · Handles structured announcement and planner-related data. · `canvas_bot/canvas/rest.py`
- [Local planner tools](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/canvas/local_tools.py): local tools · Creates private planner notes without MCP. · `canvas_bot/canvas/local_tools.py`
- **Canvas REST API**: external API · Accessed with each user's Canvas URL and PAT.

### Credentials and deployment

Encrypted user credentials and worker packaging.

- [Encrypted credential store](https://github.com/oasis-pandey/slack-ai-agent/blob/main/canvas_bot/store.py): persistence · Stores Canvas URL and PAT encrypted with Fernet/AES. · `canvas_bot/store.py`
- [Credentials database](https://github.com/oasis-pandey/slack-ai-agent/blob/main/data/creds.db): SQLite / Postgres · Uses local SQLite by default or Postgres via DATABASE_URL. · `data/creds.db`
- [Container worker deployment](https://github.com/oasis-pandey/slack-ai-agent/blob/main/Dockerfile): deployment · Containerized worker configured for Railway. · `Dockerfile`

## Connections

- 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
