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;
12
13use crate::auth;
14use crate::completions;
15use crate::health;
16use crate::mcp;
17use crate::models;
18use crate::state::SharedState;
19
20/// Build the application router with all endpoints
21///
22/// Routes:
23/// - `POST /v1/chat/completions` — Chat completion (streaming and non-streaming)
24/// - `GET /v1/models` — List available models
25/// - `GET /health` — Provider health check
26/// - `POST /mcp` — MCP Streamable HTTP (JSON-RPC 2.0)
27///
28/// The auth middleware is applied to all routes. It only enforces
29/// authentication when `EMBACLE_API_KEY` is set.
30pub fn build(state: SharedState) -> Router {
31    let mcp_server = Arc::new(mcp::server::McpServer::new(
32        Arc::clone(&state),
33        mcp::tools::build_tool_registry(),
34    ));
35
36    let mcp_router = Router::new()
37        .route("/mcp", post(mcp::handler::handle_mcp_post))
38        .with_state(mcp_server);
39
40    Router::new()
41        .route("/v1/chat/completions", post(completions::handle))
42        .route("/v1/models", get(models::handle))
43        .route("/health", get(health::handle))
44        .with_state(state)
45        .merge(mcp_router)
46        .layer(middleware::from_fn(auth::require_auth))
47}