super-voice-daemon/src/daemon/http/mcp/mod.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! `/v1/mcp` — Super Voice's MCP server, so an AI assistant can speak to the |
3 | | //! user and hear them: the tools `speak` and `listen`, over MCP's Streamable |
4 | | //! HTTP transport. |
5 | | //! |
6 | | //! An assistant reaches it on the TCP listener with the key the daemon made |
7 | | //! it when the user connected it ([`crate::assistants`]). It is a program, |
8 | | //! not a page, so it sends no `Origin` and cannot pass the origin gate the |
9 | | //! rest of `/v1` is behind. This router answers before that gate, and is |
10 | | //! behind the engine's `require_assistant_caller` instead, which admits a |
11 | | //! TCP caller by its key alone: no `Origin`, a loopback `Host`, and an |
12 | | //! assistant key as the bearer. A caller on the Unix socket passes it with a |
13 | | //! session token, as a test or the CLI does. |
14 | | //! |
15 | | //! The scope each tool needs is checked against the caller's: `speak` takes |
16 | | //! `speak`, `listen` takes `transcribe`, and `speak` too for a question. |
17 | | //! A connected assistant also gets its plugin's tools, `stop`, `status`, |
18 | | //! `announce`, `session` and `turn`, kept from its model: marked for the app |
19 | | //! for Codex, hidden by the plugin for the others. |
20 | | //! |
21 | | //! Each call names the session it comes from (`identity`), and the speech |
22 | | //! and the recording a session starts are its own |
23 | | //! ([`crate::assistants::sessions`]). |
24 | | //! |
25 | | //! MCP is JSON-RPC, a method in the body rather than a path, so `/v1/mcp` is |
26 | | //! not in the `OpenAPI` document. `SURFACE` lists what it serves, for the URL |
27 | | //! surface contract. |
28 | | //! |
29 | | //! Contract: `docs/protocol/mcp.md`. |
30 | | |
31 | | mod handler; |
32 | | mod identity; |
33 | | pub(crate) mod protocol; |
34 | | mod tools; |
35 | | |
36 | | use crate::assistants::MCP_PATH; |
37 | | use crate::daemon::http::state::AppState; |
38 | | use axum::Router; |
39 | | use axum::middleware; |
40 | | use axum::routing::post; |
41 | | use super_engine_daemon::auth::middleware::{ |
42 | | require_any_authenticated, require_assistant_caller, require_rate_limit, |
43 | | }; |
44 | | |
45 | | /// Every path this router serves, with the methods it answers on. Any other |
46 | | /// method on it is `405`. What the URL surface contract checks the hand-kept |
47 | | /// list of paths outside the `OpenAPI` document against. |
48 | | #[cfg(test)] |
49 | | pub(crate) const SURFACE: &[(&str, &str)] = &[(MCP_PATH, "post")]; |
50 | | |
51 | | /// The live `/v1/mcp` router, its guards applied and its state bound. |
52 | | /// |
53 | | /// The assistant gate is outermost, since it is what gives an assistant's |
54 | | /// request its identity; then the rate limit, which counts each assistant by |
55 | | /// its key; then the token guard. A token of any scope gets in, and each tool |
56 | | /// checks the scope it needs. |
57 | 200 | pub(crate) fn router(state: AppState) -> Router { |
58 | 200 | let auth = state.auth.clone(); |
59 | 200 | Router::new() |
60 | 200 | .route(MCP_PATH, post(handler::post)) |
61 | 200 | .with_state(state) |
62 | 200 | .layer(middleware::from_fn_with_state( |
63 | 200 | auth.clone(), |
64 | 200 | require_any_authenticated, |
65 | 200 | )) |
66 | 200 | .layer(middleware::from_fn_with_state( |
67 | 200 | auth.clone(), |
68 | 200 | require_rate_limit, |
69 | 200 | )) |
70 | 200 | .layer(middleware::from_fn_with_state( |
71 | 200 | auth, |
72 | 200 | require_assistant_caller, |
73 | 200 | )) |
74 | 200 | } |