Skip to main content

Overview

Digcrate is a Next.js application with a Convex real-time backend. The browser communicates with serverless API routes on Vercel, which in turn talk to Convex (database), Anthropic (AI), and MCP tool servers (music data).

Tech stack


Project structure


The agentic loop

Every user message goes through a multi-turn tool-use loop implemented in src/lib/agentic-loop.ts.
  1. The browser sends a POST to /api/chat with the message, session history, model choice, and API key.
  2. The route handler builds the tool list from crate-cli MCP servers and calls agenticLoop().
  3. The loop calls the Anthropic (or OpenRouter) API with the message and available tools.
  4. If the model returns tool_use blocks, the loop executes each tool call against the appropriate MCP server.
  5. Tool results are fed back to the model as tool_result blocks. Steps 3–5 repeat up to 25 turns.
  6. Each turn emits Server-Sent Events to the browser: thinking, tool_start, tool_end, answer_token, and finally done.
  7. The browser’s chat panel renders tokens as they arrive and parses OpenUI components from the final answer.
The loop supports both Anthropic (direct SDK) and OpenRouter (OpenAI-compatible API) with the same event format. The useOpenRouter flag on AgenticLoopOptions selects the path.

Key data flows

Authentication

User records are synced from Clerk to Convex via a webhook at /api/webhooks/clerk. The Convex users table stores the Clerk user ID, email, encrypted API keys, and Stripe customer ID.

Agent query

Real-time state (Convex)

The browser subscribes to Convex queries that push updates in real time:

Connected services (Auth0 Token Vault)


Database schema

The Convex database has 24 tables. Key tables: