Skip to main content

koan_server/
mcp.rs

1//! MCP (Model Context Protocol) server for koan.
2//!
3//! Exposes the GraphQL schema as MCP tools for Claude Desktop / MCP clients.
4
5use std::path::PathBuf;
6use std::sync::Arc;
7
8use crossbeam_channel::Sender;
9use koan_core::player::commands::PlayerCommand;
10use koan_core::player::state::SharedPlayerState;
11use rmcp::handler::server::router::tool::ToolRouter;
12use rmcp::handler::server::wrapper::Json;
13use rmcp::model::{ServerCapabilities, ServerConfig};
14use rmcp::{ServerHandler, schemars, tool_router};
15use serde::{Deserialize, Serialize};
16
17// ---------------------------------------------------------------------------
18// Parameter types
19// ---------------------------------------------------------------------------
20
21#[derive(Debug, Deserialize, schemars::JsonSchema)]
22pub struct GraphqlParams {
23    #[schemars(
24        description = "GraphQL query or mutation string. Use the schema_sdl tool first to learn available types, queries, mutations, and filter parameters."
25    )]
26    pub query: String,
27    #[schemars(description = "Optional JSON object of query variables")]
28    pub variables: Option<serde_json::Value>,
29}
30
31// ---------------------------------------------------------------------------
32// Response types
33// ---------------------------------------------------------------------------
34
35/// GraphQL execution result wrapper — MCP spec requires outputSchema to be an object type.
36#[derive(Debug, Serialize, schemars::JsonSchema)]
37pub struct GraphqlResponse {
38    /// The GraphQL response JSON (contains data and/or errors fields).
39    pub result: serde_json::Value,
40}
41
42// ---------------------------------------------------------------------------
43// MCP Server
44// ---------------------------------------------------------------------------
45
46/// A koan account, sent by the MCP gateway with every request once the
47/// gateway has signed its user in. Only the HTTP transport reads them, and it
48/// is reachable from the gateway alone.
49pub const USERNAME_HEADER: &str = "x-koan-username";
50pub const PASSWORD_HEADER: &str = "x-koan-password";
51
52#[derive(Clone)]
53pub struct KoanMcpServer {
54    #[allow(dead_code)]
55    tool_router: ToolRouter<Self>,
56    graphql_schema: crate::graphql::KoanSchema,
57    /// Checks account headers; `None` on stdio, which has no headers.
58    users: Option<Arc<crate::auth::password::PasswordVerifier>>,
59}
60
61impl KoanMcpServer {
62    pub fn new(
63        state: Arc<SharedPlayerState>,
64        cmd_tx: Sender<PlayerCommand>,
65        db_path: PathBuf,
66    ) -> Self {
67        let graphql_schema =
68            crate::graphql::build_schema(state.clone(), cmd_tx.clone(), db_path.clone(), None);
69        Self {
70            tool_router: Self::tool_router(),
71            graphql_schema,
72            users: None,
73        }
74    }
75
76    /// The role a request acts with: the account in its headers, or
77    /// `mcp_role()` when it names none. With `KOAN_MCP_REQUIRE_LOGIN=1`, a
78    /// request naming no account is refused.
79    fn role(&self, extensions: &rmcp::model::Extensions) -> Result<koan_core::auth::Role, String> {
80        let Some(users) = &self.users else {
81            return Ok(mcp_role());
82        };
83        let parts = extensions.get::<axum::http::request::Parts>();
84        let get = |h: &str| {
85            parts
86                .and_then(|p| p.headers.get(h))
87                .and_then(|v| v.to_str().ok())
88                .map(str::trim)
89                .filter(|v| !v.is_empty())
90        };
91        match (get(USERNAME_HEADER), get(PASSWORD_HEADER)) {
92            (Some(u), Some(p)) => users
93                .verify(u, p)
94                .ok_or_else(|| "kōan rejected that username and password".to_string()),
95            _ if std::env::var("KOAN_MCP_REQUIRE_LOGIN").is_ok_and(|v| v == "1") => Err(format!(
96                "this kōan needs an account: send {USERNAME_HEADER} and {PASSWORD_HEADER}"
97            )),
98            _ => Ok(mcp_role()),
99        }
100    }
101}
102
103use rmcp::handler::server::wrapper::Parameters;
104use rmcp::tool;
105
106/// Role the MCP `graphql` tool executes at.
107///
108/// The transport carries no credential, so anything reachable here is reachable
109/// by whoever can talk to the MCP process. `User` covers everything the tool
110/// advertises — browsing, playback, queue, favourites, playlists, radio — and
111/// leaves out the admin mutations that move files on disk (`organize*`), rewrite
112/// config, or change the output device. `KOAN_MCP_ADMIN=1` opts back in.
113fn mcp_role() -> koan_core::auth::Role {
114    if std::env::var("KOAN_MCP_ADMIN").is_ok_and(|v| v == "1") {
115        koan_core::auth::Role::Admin
116    } else {
117        koan_core::auth::Role::User
118    }
119}
120
121#[tool_router]
122impl KoanMcpServer {
123    #[tool(
124        description = "The GraphQL schema for the user's music (kōan): their library and the \
125        players they listen on. Call this first, before `graphql`. It covers playing, pausing, \
126        skipping and queueing music on the user's phone and computers, what is playing now, \
127        and searching, browsing and making playlists from the music they own."
128    )]
129    fn schema_sdl(&self) -> Json<GraphqlResponse> {
130        let sdl = self.graphql_schema.sdl();
131        Json(GraphqlResponse {
132            result: serde_json::Value::String(sdl),
133        })
134    }
135
136    #[tool(
137        description = "Control the user's music and search their music library (kōan). Use it \
138        for any request about music they listen to or own: play something, pause, resume, skip, \
139        what's playing, what's next, add to or change the queue, find or recommend from their \
140        collection, playlists, favourites. \"Pause the music on my desktop\", \"play some \
141        jazz on my phone\" and \"what is this song\" are all this tool.\n\n\
142        Call schema_sdl first for the full schema. The user's phones and computers running \
143        kōan are `clients`; commands for them end in `OnClient`.\n\n\
144        Examples:\n\
145        - What's playing, where: { clients { name playing nowPlaying positionMs } }\n\
146        - Pause: mutation { controlClient(action: PAUSE) { ok message } }\n\
147        - Find music: { tracks(search: \"aphex\", first: 20) { edges { node { id title artist album } } } }\n\
148        - Play it: mutation { playOnClient(trackIds: [\"42\", \"43\"]) { ok message } }\n\n\
149        String filters are case-insensitive substrings."
150    )]
151    fn graphql(
152        &self,
153        Parameters(params): Parameters<GraphqlParams>,
154        extensions: rmcp::model::Extensions,
155    ) -> Result<Json<GraphqlResponse>, String> {
156        let schema = self.graphql_schema.clone();
157        let query = params.query;
158        let variables = params.variables;
159        let rt =
160            tokio::runtime::Handle::try_current().map_err(|_| "no tokio runtime".to_string())?;
161        // Inside block_in_place too: a first sign-in runs argon2.
162        let result = tokio::task::block_in_place(|| {
163            let role = self.role(&extensions)?;
164            Ok::<_, String>(rt.block_on(crate::graphql::execute_in_process(
165                &schema, &query, variables, role,
166            )))
167        })?;
168        Ok(Json(GraphqlResponse { result }))
169    }
170}
171
172#[rmcp::tool_handler]
173impl ServerHandler for KoanMcpServer {
174    fn get_info(&self) -> ServerConfig {
175        // Over HTTP this is a server: its own player is headless and nobody
176        // hears it, and what the user listens to is the apps linked to it. On
177        // stdio it is the user's own machine, and its player is the music.
178        let instructions = if self.users.is_some() {
179            SERVER_INSTRUCTIONS
180        } else {
181            LOCAL_INSTRUCTIONS
182        };
183        ServerConfig::new(ServerCapabilities::builder().enable_tools().build())
184            .with_server_info(rmcp::model::Implementation::new(
185                "koan",
186                env!("CARGO_PKG_VERSION"),
187            ))
188            .with_instructions(instructions)
189    }
190}
191
192const SERVER_INSTRUCTIONS: &str = "kōan is the user's music: their whole music library, and the \
193phones and computers they listen on. Use it for anything about music they are playing or own — \
194\"pause the music\", \"play something like Polar Bear on my phone\", \"what's this song\", \
195\"skip to the Phace remix\", \"add their new album when it's downloaded\". Call `schema_sdl` \
196once, then do everything through `graphql`.
197
198## Where the music plays
199The user listens in kōan apps on their devices, linked to this server. Query \
200`clients { name platform playing nowPlaying album positionMs durationMs radio queue { trackId \
201title artist current } }` to see each device, what it is playing and what it has queued. Every \
202command about the user's music goes to a device:
203- `controlClient(action: PAUSE|RESUME|NEXT|PREVIOUS)`, `seekOnClient(positionMs)`
204- `playOnClient(trackIds, startAt)` replaces the queue and plays; `enqueue: true` appends
205- `playNextOnClient(trackIds)`, `jumpOnClient(trackId)` (skip to a track, queued or not), \
206`removeFromClient(trackIds)`, `clearClient`, `setClientRadio(enabled)`, `syncClient`
207- `queueOnClientWhenAdded(artist, album)` queues an album once it reaches the library, e.g. \
208one being downloaded with slsk's `grab`; `clientOrders` lists those waiting
209Leave `client` out unless the user named a device (\"my phone\", \"the desktop\": match it \
210against `clients` names and platforms). Without it the server picks the device that is \
211playing, else the one played most recently; if it answers that it cannot tell, ask the user \
212which device.
213
214**Act on what the user asks; do not second-guess it from reported state.** \"Pause\", \
215\"skip\" and \"resume\" go straight to `controlClient`: the user can hear the device and you \
216cannot, and a report can be stale or, from an older app (`playing: null`), absent.
217
218**Never use the server's own player for the user's music.** `play`, `pause`, `resume`, \
219`next`, `previous`, `seek`, `nowPlaying`, `queue`, `addToQueue`, `replaceQueue`, \
220`playPlaylist` and the radio mutations drive a headless player on the server that nobody \
221hears; `nowPlaying` there reports nothing about what the user is listening to.
222
223## The library
224- `artists`, `albums`, `tracks` with filters (genre, year range, codec, sample rate, bit depth, \
225duration, favourites), `randomTracks`, `similarArtists`, `similarTracks`, `fuzzySearch`
226- Build a set from these, then send its track ids to a device with `playOnClient`. Track ids are \
227integers in queries; pass them to the client mutations as strings.
228- Favourites: `favourite`, `unfavourite`, `toggleFavourite`, `favouritesOnly: true` on queries
229- Playlists: `playlists`, `playlistTracks`, `createPlaylist`, `addToPlaylist`, \
230`setPlaylistTracks`, `renamePlaylist`, `deletePlaylist`
231- History: `playHistory`
232- Sharing: `createShare(trackIds, description)` makes a public link anyone can open without an \
233account; confirm with the user first. `shares`, `updateShare`, `deleteShare` manage them.
234
235## Not available
236`organize*` (moves files on disk), `updateConfig` and `triggerScan` are refused unless \
237`KOAN_MCP_ADMIN=1` is set.";
238
239const LOCAL_INSTRUCTIONS: &str = "kōan is the user's music player on this machine and their \
240music library. Use it for anything about music they are playing or own — \"pause the music\", \
241\"play something like Polar Bear\", \"what's this song\". Call `schema_sdl` once, then do \
242everything through `graphql`.
243
244## Playback
245This player is what the user hears: `play`, `pause`, `resume`, `stop`, `next`, `previous`, \
246`seek`, `nowPlaying`; the queue with `queue`, `addToQueue`, `replaceQueue`, `removeFromQueue`, \
247`moveInQueue`, `clearQueue`, `undo`, `redo`; radio with `enableRadio`, `disableRadio`.
248
249## The library
250- `artists`, `albums`, `tracks` with filters (genre, year range, codec, sample rate, bit depth, \
251duration, favourites), `randomTracks`, `similarArtists`, `similarTracks`, `fuzzySearch`
252- Favourites: `favourite`, `unfavourite`, `toggleFavourite`, `favouritesOnly: true` on queries
253- Playlists: `playlists`, `playlistTracks`, `createPlaylist`, `saveQueueAsPlaylist`, \
254`addToPlaylist`, `setPlaylistTracks`, `renamePlaylist`, `deletePlaylist`, `playPlaylist`
255- History: `playHistory`
256- Sharing: `createShare(trackIds, description)` makes a public link; confirm with the user first.
257
258## Not available
259`organize*` (moves files on disk), `updateConfig`, `triggerScan` and `setDevice` are refused \
260unless `KOAN_MCP_ADMIN=1` is set.
261
262## IDs
263Track IDs are integers from the library; queue item IDs are UUIDs from the queue.";
264
265/// Serve MCP over streamable HTTP at `addr`/mcp, on a thread of its own.
266///
267/// The gateway authenticates its user and then forwards a koan account in
268/// `x-koan-username` / `x-koan-password`, which set the role each request acts
269/// with. The headers are trusted to come from the gateway, so this listener
270/// must not be reachable from anywhere else: bind it to an address only the
271/// gateway can reach, and keep it off the public GraphQL port.
272pub fn spawn_http(
273    addr: std::net::SocketAddr,
274    state: Arc<SharedPlayerState>,
275    cmd_tx: Sender<PlayerCommand>,
276    pool: Arc<koan_core::db::pool::Pool>,
277) -> std::io::Result<std::thread::JoinHandle<()>> {
278    use rmcp::transport::streamable_http_server::{
279        StreamableHttpServerConfig, StreamableHttpService, session::local::LocalSessionManager,
280    };
281    let mut template = KoanMcpServer::new(state, cmd_tx, pool.path().to_path_buf());
282    template.users = Some(Arc::new(crate::auth::password::PasswordVerifier::new(pool)));
283    // Bound here rather than on the thread, so a taken port fails the start.
284    let listener = std::net::TcpListener::bind(addr)?;
285    listener.set_nonblocking(true)?;
286    std::thread::Builder::new()
287        .name("koan-mcp-http".into())
288        .spawn(move || {
289            let rt = tokio::runtime::Builder::new_multi_thread()
290                .enable_all()
291                .build()
292                .expect("failed to create tokio runtime");
293            rt.block_on(async move {
294                let service = StreamableHttpService::new(
295                    move || Ok(template.clone()),
296                    Arc::new(LocalSessionManager::default()),
297                    // The gateway forwards its own Host header, which rmcp's
298                    // DNS-rebinding allowlist would refuse; reachability is
299                    // what guards this listener.
300                    StreamableHttpServerConfig::default().disable_allowed_hosts(),
301                );
302                let app = axum::Router::new().nest_service("/mcp", service);
303                let listener =
304                    tokio::net::TcpListener::from_std(listener).expect("listener from std");
305                if let Err(e) = axum::serve(listener, app).await {
306                    log::error!("MCP HTTP server stopped: {e}");
307                }
308            });
309        })
310}
311
312/// Entry point for `koan mcp` — starts a headless player with an MCP server on stdio.
313pub fn cmd_mcp() {
314    use koan_core::player::Player;
315    use rmcp::ServiceExt;
316
317    // Validate DB is accessible before starting the server.
318    let _db = koan_core::db::connection::Database::open_default().expect("failed to open database");
319    let db_path = koan_core::config::db_path();
320
321    // Spawn the player engine (headless — no TUI).
322    let (state, _timeline, _viz, cmd_tx) = Player::spawn();
323
324    let server = KoanMcpServer::new(state, cmd_tx, db_path);
325
326    // Run the MCP server on the tokio runtime (blocking the main thread).
327    let rt = tokio::runtime::Runtime::new().expect("failed to create tokio runtime");
328    rt.block_on(async {
329        let transport = rmcp::transport::io::stdio();
330        let service = server
331            .serve(transport)
332            .await
333            .expect("failed to start MCP server");
334        let _ = service.waiting().await;
335    });
336}
337
338// ---------------------------------------------------------------------------
339// Tests
340// ---------------------------------------------------------------------------
341
342#[cfg(test)]
343mod tests {
344    use super::*;
345    use koan_core::db::connection::Database;
346    use koan_core::db::queries;
347    use koan_core::player::commands::CommandChannel;
348    use tempfile::TempDir;
349
350    fn test_server() -> (KoanMcpServer, CommandChannel, TempDir) {
351        let tmp = TempDir::new().unwrap();
352        let db_path = tmp.path().join("test.db");
353        let db = Database::open(&db_path).unwrap();
354        koan_core::db::schema::create_tables(&db.conn).unwrap();
355
356        let state = SharedPlayerState::new();
357        let ch = CommandChannel::new();
358        let tx = ch.tx.clone();
359
360        let server = KoanMcpServer::new(state, tx, db_path);
361        (server, ch, tmp)
362    }
363
364    fn with_headers(headers: &[(&str, &str)]) -> rmcp::model::Extensions {
365        let mut req = axum::http::Request::builder();
366        for (k, v) in headers {
367            req = req.header(*k, *v);
368        }
369        let (parts, ()) = req.body(()).unwrap().into_parts();
370        let mut ext = rmcp::model::Extensions::new();
371        ext.insert(parts);
372        ext
373    }
374
375    #[test]
376    fn gateway_headers_act_as_that_account() {
377        use koan_core::auth::Role;
378        let (mut server, _ch, tmp) = test_server();
379        let db_path = tmp.path().join("test.db");
380        let db = Database::open(&db_path).unwrap();
381        queries::auth::create_user(&db.conn, "owner", "sesame", Role::Admin).unwrap();
382        queries::auth::create_user(&db.conn, "mate", "hunter22", Role::Readonly).unwrap();
383        server.users = Some(Arc::new(crate::auth::password::PasswordVerifier::new(
384            Arc::new(koan_core::db::pool::Pool::new(db_path)),
385        )));
386
387        let as_ = |u: &str, p: &str| {
388            server.role(&with_headers(&[(USERNAME_HEADER, u), (PASSWORD_HEADER, p)]))
389        };
390        assert_eq!(as_("owner", "sesame"), Ok(Role::Admin));
391        assert_eq!(as_("mate", "hunter22"), Ok(Role::Readonly));
392        assert!(as_("owner", "wrong").is_err());
393        // No account named: the transport's default role.
394        assert_eq!(server.role(&with_headers(&[])), Ok(mcp_role()));
395    }
396
397    fn insert_test_track(db_path: &std::path::Path, title: &str, artist: &str, album: &str) -> i64 {
398        let db = Database::open(db_path).unwrap();
399        let meta = queries::TrackMeta {
400            title: title.to_string(),
401            artist: artist.to_string(),
402            album_artist: Some(artist.to_string()),
403            album: album.to_string(),
404            track_number: Some(1),
405            disc: Some(1),
406            date: Some("2024".into()),
407            genre: Some("Electronic".into()),
408            duration_ms: Some(240000),
409            path: Some(format!(
410                "/tmp/test/{}.flac",
411                title.to_lowercase().replace(' ', "_")
412            )),
413            codec: Some("FLAC".into()),
414            sample_rate: Some(44100),
415            bit_depth: Some(16),
416            channels: Some(2),
417            bitrate: Some(1411),
418            size_bytes: Some(42_000_000),
419            mtime: Some(1700000000),
420            source: "local".into(),
421            remote_id: None,
422            remote_url: None,
423            album_remote_id: None,
424            artist_remote_id: None,
425            mbid: None,
426            album_mbid: None,
427            album_added_at: None,
428            label: None,
429        };
430        queries::upsert_track(&db.conn, &meta).unwrap()
431    }
432
433    #[test]
434    fn schema_sdl_returns_schema() {
435        let (server, _ch, _tmp) = test_server();
436        let Json(resp) = server.schema_sdl();
437        let sdl = resp.result.as_str().unwrap();
438        assert!(sdl.contains("type QueryRoot"));
439        assert!(sdl.contains("type MutationRoot"));
440        assert!(sdl.contains("artists"));
441        assert!(sdl.contains("nowPlaying"));
442    }
443
444    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
445    async fn graphql_query_works() {
446        let (server, _ch, tmp) = test_server();
447        let db_path = tmp.path().join("test.db");
448        insert_test_track(&db_path, "Windowlicker", "Aphex Twin", "Windowlicker EP");
449
450        let result = server.graphql(
451            Parameters(GraphqlParams {
452                query: r#"{ tracks(search: "aphex") { edges { node { title artist } } } }"#.into(),
453                variables: None,
454            }),
455            Default::default(),
456        );
457        assert!(result.is_ok());
458        let Json(resp) = result.unwrap();
459        let data = &resp.result["data"]["tracks"]["edges"];
460        assert_eq!(data.as_array().unwrap().len(), 1);
461        assert_eq!(data[0]["node"]["title"], "Windowlicker");
462    }
463
464    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
465    async fn graphql_mutation_works() {
466        let (server, _ch, _tmp) = test_server();
467        let result = server.graphql(
468            Parameters(GraphqlParams {
469                query: "mutation { pause { ok message } }".into(),
470                variables: None,
471            }),
472            Default::default(),
473        );
474        assert!(result.is_ok());
475        let Json(resp) = result.unwrap();
476        assert_eq!(resp.result["data"]["pause"]["ok"], true);
477    }
478
479    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
480    async fn graphql_now_playing_stopped() {
481        let (server, _ch, _tmp) = test_server();
482        let result = server.graphql(
483            Parameters(GraphqlParams {
484                query: "{ nowPlaying { state positionMs } }".into(),
485                variables: None,
486            }),
487            Default::default(),
488        );
489        assert!(result.is_ok());
490        let Json(resp) = result.unwrap();
491        assert_eq!(resp.result["data"]["nowPlaying"]["state"], "STOPPED");
492    }
493
494    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
495    async fn graphql_library_stats() {
496        let (server, _ch, tmp) = test_server();
497        let db_path = tmp.path().join("test.db");
498        insert_test_track(&db_path, "T1", "A1", "Album1");
499
500        let result = server.graphql(
501            Parameters(GraphqlParams {
502                query: "{ libraryStats { totalTracks totalArtists totalAlbums } }".into(),
503                variables: None,
504            }),
505            Default::default(),
506        );
507        assert!(result.is_ok());
508        let Json(resp) = result.unwrap();
509        assert_eq!(resp.result["data"]["libraryStats"]["totalTracks"], 1);
510    }
511}