Skip to main content

embacle_server/
router.rs

1// ABOUTME: Axum router wiring OpenAI-compatible and MCP endpoints
2// ABOUTME: Mounts completions, models, health, and MCP routes with optional auth middleware
3//
4// SPDX-License-Identifier: Apache-2.0
5// Copyright (c) 2026 dravr.ai
6
7use std::sync::Arc;
8
9use axum::middleware;
10use axum::routing::{get, post};
11use axum::Router;
12use dravr_tronc::mcp::transport::http::mcp_router as build_mcp_router;
13use dravr_tronc::McpServer;
14
15use crate::auth;
16use crate::completions;
17use crate::health;
18use crate::models;
19use crate::state::AppState;
20
21/// Build the application router with all endpoints
22///
23/// Routes:
24/// - `POST /v1/chat/completions` — Chat completion (streaming and non-streaming)
25/// - `GET /v1/models` — List available models
26/// - `GET /health` — Provider health check
27/// - `POST /mcp` — MCP Streamable HTTP (JSON-RPC 2.0, via dravr-tronc)
28///
29/// The auth middleware is applied to all routes. It only enforces
30/// authentication when `EMBACLE_API_KEY` is set.
31pub fn build(state: AppState) -> Router {
32    let mcp_server = Arc::new(McpServer::new(
33        "embacle-mcp",
34        env!("CARGO_PKG_VERSION"),
35        embacle_mcp::build_tool_registry(),
36        Arc::clone(&state.shared),
37    ));
38
39    let mcp_router = build_mcp_router(mcp_server);
40
41    Router::new()
42        .route("/v1/chat/completions", post(completions::handle))
43        .route("/v1/models", get(models::handle))
44        .route("/health", get(health::handle))
45        .with_state(state)
46        .merge(mcp_router)
47        .layer(middleware::from_fn(auth::require_auth))
48}