|
1 | | -aibridge |
| 1 | +# aibridge |
| 2 | + |
| 3 | +aibridge is an HTTP gateway that sits between AI clients and upstream AI providers (Anthropic, OpenAI). It intercepts requests to record token usage, prompts, and tool invocations per user. Optionally supports centralized [MCP](https://modelcontextprotocol.io/) tool injection with allowlist/denylist filtering. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +``` |
| 8 | +┌─────────────────┐ ┌───────────────────────────────────────────┐ |
| 9 | +│ AI Client │ │ aibridge │ |
| 10 | +│ (Claude Code, │────▶│ ┌─────────────────┐ ┌─────────────┐ │ |
| 11 | +│ Cursor, etc.) │ │ │ RequestBridge │───▶│ Providers │ │ |
| 12 | +└─────────────────┘ │ │ (http.Handler) │ │ (Anthropic │ │ |
| 13 | + │ └─────────────────┘ │ OpenAI) │ │ |
| 14 | + │ └──────┬──────┘ │ |
| 15 | + │ │ │ |
| 16 | + │ ▼ │ ┌─────────────┐ |
| 17 | + │ ┌─────────────────┐ ┌─────────────┐ │ │ Upstream │ |
| 18 | + │ │ Recorder │◀───│ Interceptor │─── ───▶│ API │ |
| 19 | + │ │ (tokens, tools, │ │ (streaming/ │ │ │ (Anthropic │ |
| 20 | + │ │ prompts) │ │ blocking) │ │ │ OpenAI) │ |
| 21 | + │ └────────┬────────┘ └──────┬──────┘ │ └─────────────┘ |
| 22 | + │ │ │ │ |
| 23 | + │ ▼ ┌──────▼──────┐ │ |
| 24 | + │ ┌ ─ ─ ─ ─ ─ ─ ─ ┐ │ MCP Proxy │ │ |
| 25 | + │ │ Database │ │ (tools) │ │ |
| 26 | + │ └ ─ ─ ─ ─ ─ ─ ─ ┘ └─────────────┘ │ |
| 27 | + └───────────────────────────────────────────┘ |
| 28 | +``` |
| 29 | + |
| 30 | +### Components |
| 31 | + |
| 32 | +- **RequestBridge**: The main `http.Handler` that routes requests to providers |
| 33 | +- **Provider**: Defines bridged routes (intercepted) and passthrough routes (proxied) |
| 34 | +- **Interceptor**: Handles request/response processing and streaming |
| 35 | +- **Recorder**: Interface for capturing usage data (tokens, prompts, tools) |
| 36 | +- **MCP Proxy** (optional): Connects to MCP servers to list tool, inject them into requests, and invoke them in an inner agentic loop |
| 37 | + |
| 38 | +## Request Flow |
| 39 | + |
| 40 | +1. Client sends request to `/anthropic/v1/messages` or `/openai/v1/chat/completions` |
| 41 | +2. **Actor extraction**: Request must have an actor in context (via `AsActor()`). |
| 42 | +3. **Upstream call**: Request forwarded to the AI provider |
| 43 | +4. **Response relay**: Response streamed/sent to client |
| 44 | +5. **Recording**: Token usage, prompts, and tool invocations recorded |
| 45 | + |
| 46 | +**With MCP enabled**: Tools from configured MCP servers are centrally defined and injected into requests (prefixed `bmcp_`). Allowlist/denylist regex patterns control which tools are available. When the model selects an injected tool, the gateway invokes it in an inner agentic loop, and continues the conversation loop until complete. |
| 47 | + |
| 48 | +Passthrough routes (`/v1/models`, `/v1/messages/count_tokens`) are reverse-proxied directly. |
| 49 | + |
| 50 | +## Observability |
| 51 | + |
| 52 | +### Prometheus Metrics |
| 53 | + |
| 54 | +Create metrics with `NewMetrics(prometheus.Registerer)`: |
| 55 | + |
| 56 | +| Metric | Type | Description | |
| 57 | +|--------|------|-------------| |
| 58 | +| `interceptions_total` | Counter | Intercepted request count | |
| 59 | +| `interceptions_inflight` | Gauge | Currently processing requests | |
| 60 | +| `interceptions_duration_seconds` | Histogram | Request duration | |
| 61 | +| `tokens_total` | Counter | Token usage (input/output) | |
| 62 | +| `prompts_total` | Counter | User prompt count | |
| 63 | +| `injected_tool_invocations_total` | Counter | MCP tool invocations | |
| 64 | +| `passthrough_total` | Counter | Non-intercepted requests | |
| 65 | + |
| 66 | +### Recorder Interface |
| 67 | + |
| 68 | +Implement `Recorder` to persist usage data to your database. The example uses SQLite ([example/recorder.go](example/recorder.go)): |
| 69 | + |
| 70 | +- `aibridge_interceptions` - request metadata (provider, model, initiator, timestamps) |
| 71 | +- `aibridge_token_usages` - input/output token counts per response |
| 72 | +- `aibridge_user_prompts` - user prompts |
| 73 | +- `aibridge_tool_usages` - tool invocations (injected and client-defined) |
| 74 | + |
| 75 | +```go |
| 76 | +type Recorder interface { |
| 77 | + RecordInterception(ctx context.Context, req *InterceptionRecord) error |
| 78 | + RecordInterceptionEnded(ctx context.Context, req *InterceptionRecordEnded) error |
| 79 | + RecordTokenUsage(ctx context.Context, req *TokenUsageRecord) error |
| 80 | + RecordPromptUsage(ctx context.Context, req *PromptUsageRecord) error |
| 81 | + RecordToolUsage(ctx context.Context, req *ToolUsageRecord) error |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Example |
| 86 | + |
| 87 | +See [example/](example/) for a complete runnable example with SQLite persistence and [DeepWiki](https://mcp.deepwiki.com) MCP integration. |
| 88 | + |
| 89 | +### Setup |
| 90 | + |
| 91 | +1. **Get API keys** from the provider consoles: |
| 92 | + - Anthropic: https://console.anthropic.com/settings/keys |
| 93 | + - OpenAI: https://platform.openai.com/api-keys |
| 94 | + |
| 95 | +2. **Set environment variables**: |
| 96 | + ```bash |
| 97 | + export ANTHROPIC_API_KEY="sk-ant-..." |
| 98 | + export OPENAI_API_KEY="sk-..." |
| 99 | + ``` |
| 100 | + |
| 101 | +3. **Run the example**: |
| 102 | + ```bash |
| 103 | + cd example && go run . |
| 104 | + ``` |
| 105 | + |
| 106 | +4. **Test with curl**: |
| 107 | + ```bash |
| 108 | + curl -X POST http://localhost:8080/anthropic/v1/messages \ |
| 109 | + -H "Content-Type: application/json" \ |
| 110 | + -d '{ |
| 111 | + "model": "claude-sonnet-4-20250514", |
| 112 | + "max_tokens": 1024, |
| 113 | + "messages": [{"role": "user", "content": "Hello!"}], |
| 114 | + "stream": true |
| 115 | + }' |
| 116 | + ``` |
| 117 | + |
| 118 | +5. **Test with Claude Code**: |
| 119 | + Claude Code allows a base URL override via `ANTHROPIC_BASE_URL`. |
| 120 | + |
| 121 | +  |
| 122 | + |
| 123 | +## Supported Routes |
| 124 | + |
| 125 | +| Provider | Route | Type | |
| 126 | +|----------|-------|------| |
| 127 | +| Anthropic | `/anthropic/v1/messages` | Bridged (intercepted) | |
| 128 | +| Anthropic | `/anthropic/v1/models` | Passthrough | |
| 129 | +| Anthropic | `/anthropic/v1/messages/count_tokens` | Passthrough | |
| 130 | +| OpenAI | `/openai/v1/chat/completions` | Bridged (intercepted) | |
| 131 | +| OpenAI | `/openai/v1/models` | Passthrough | |
0 commit comments