|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +LLM Meta-Server — a Rails 8.0 API + web UI that provides a unified interface for multiple LLM providers (OpenAI, Anthropic, Google Gemini, Ollama) through a single API. Users register encrypted API keys and call any supported model via one endpoint. Also integrates MCP (Model Context Protocol) servers for tool calling. |
| 8 | + |
| 9 | +## Common Commands |
| 10 | + |
| 11 | +```bash |
| 12 | +bin/dev # Start dev server (web + Tailwind CSS watch via Foreman) |
| 13 | +bin/spec # Run all RSpec tests |
| 14 | +bin/spec spec/path/file_spec.rb # Run a single test file |
| 15 | +bin/rubocop # Lint (rubocop-rails-omakase style) |
| 16 | +bin/rubocop -A # Auto-fix lint issues |
| 17 | +bin/brakeman --no-pager # Security scan |
| 18 | +bin/rails db:test:prepare # Prepare test database |
| 19 | +bin/rails db:setup # Create DB + migrate + seed |
| 20 | +``` |
| 21 | + |
| 22 | +CI runs: RuboCop, Brakeman, importmap audit, RSpec (in that order). |
| 23 | + |
| 24 | +## Architecture |
| 25 | + |
| 26 | +### Dual Interface |
| 27 | + |
| 28 | +The app serves both a **web UI** (Devise/OAuth, standard Rails controllers with Hotwire/Turbo) and a **JSON API** (namespace `Api::`, inherits from `ApiController`). API authentication uses Google ID tokens via Bearer header; web UI uses Devise sessions with Google OAuth2. |
| 29 | + |
| 30 | +### Key Flow: Chat Completion |
| 31 | + |
| 32 | +`POST /api/llm_api_keys/:uuid/models/:name/chats` is the core endpoint. |
| 33 | + |
| 34 | +1. `Api::ChatsController` authenticates via Google ID token, resolves the user's `LlmApiKey` by UUID |
| 35 | +2. `LlmModelMap.fetch!` maps the friendly model name to the provider-specific model ID |
| 36 | +3. `LlmRbFacade.call!` creates an `LLM::Session` (from the `llm.rb` gem) and executes the chat |
| 37 | +4. If MCP `tool_ids` are passed, `McpToolAdapter` converts MCP tool schemas to `llm.rb` function format; tool calls are executed and results fed back to the LLM |
| 38 | + |
| 39 | +### Services (`app/services/`) |
| 40 | + |
| 41 | +- **LlmRbFacade** — Facade over the `llm.rb` gem. Handles provider client creation, chat execution, and tool call loops. Entry point: `LlmRbFacade.call!` |
| 42 | +- **ApiKeyEncrypter / ApiKeyDecrypter** — AWS KMS encryption for stored API keys |
| 43 | +- **GoogleIdTokenVerifier** — Validates Google ID tokens; supports multiple client IDs via `ALLOWED_GOOGLE_CLIENT_IDS` env var |
| 44 | +- **McpClient** — JSON-RPC 2.0 client for MCP servers (supports SSE responses) |
| 45 | +- **McpToolAdapter** — Converts MCP tool schemas to `llm.rb` function schemas |
| 46 | +- **McpToolFetcher** — Fetches available tools from registered MCP servers |
| 47 | + |
| 48 | +### Models |
| 49 | + |
| 50 | +- **User** has_many `LlmApiKey`, has_many `McpServer` |
| 51 | +- **LlmApiKey** — per-user encrypted API credentials with UUID for API access. Uses `EncryptableApiKey` concern for encryption lifecycle |
| 52 | +- **Llm** has_many `LlmModel` — platform definitions (openai, anthropic, google, ollama) |
| 53 | +- **LlmModelMap** — constants-based mapping from friendly names to provider model IDs. Ollama models are special-cased (no API key required) |
| 54 | +- **McpServer** has_many `McpTool` — user-registered MCP servers |
| 55 | + |
| 56 | +### Controllers |
| 57 | + |
| 58 | +- **ApiController** — base for all API controllers. Handles Google ID token auth, rescues JWT/auth errors |
| 59 | +- Web controllers inherit from `ApplicationController` (Devise session auth) |
| 60 | +- API controllers live under `app/controllers/api/` |
| 61 | + |
| 62 | +## Environment Variables |
| 63 | + |
| 64 | +Required: `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_REGION`, `KMS_KEY_ID`, `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET`, `ALLOWED_GOOGLE_CLIENT_IDS`. See `.env` for full list. |
| 65 | + |
| 66 | +## Tech Stack |
| 67 | + |
| 68 | +Ruby 3.4.9, Rails 8.0.3, SQLite3, Puma, Hotwire (Turbo + Stimulus), Tailwind CSS, Devise + OmniAuth, `llm.rb` gem, AWS KMS, RSpec, RuboCop (omakase). |
0 commit comments