diff --git a/apps/server/src/main.rs b/apps/server/src/main.rs index 208628657..82633c21b 100644 --- a/apps/server/src/main.rs +++ b/apps/server/src/main.rs @@ -45,6 +45,7 @@ mod infra_routes; mod infra_sqlite; mod kube_routes; mod llm_routes; +mod mcp_brand; mod mcp_oauth; mod mcp_routes; mod memory_routes; @@ -438,6 +439,7 @@ async fn main() -> Result<(), Box> { .merge(llm_routes::router()) .merge(mcp_routes::router()) .merge(mcp_oauth::router()) + .merge(mcp_brand::router()) .merge(runner_routes::router()) .merge(actions_routes::router()) .merge(git_routes::router()) diff --git a/apps/server/src/mcp_brand.rs b/apps/server/src/mcp_brand.rs new file mode 100644 index 000000000..e7c95b3a0 --- /dev/null +++ b/apps/server/src/mcp_brand.rs @@ -0,0 +1,191 @@ +//! Logo public du connecteur MCP DevForge. +//! +//! Grok (Bot et application) et les clients MCP 2025-06-18 lisent l’icône : +//! - avant la session, dans `logo_uri` des métadonnées OAuth (serveur d’autorisation +//! et ressource protégée) ; +//! - après `initialize`, dans `serverInfo.icons` (URLs absolues). +//! +//! Le `logo_uri` d’une réponse DCR (RFC 7591) est le logo du *client* qui s’enregistre. +//! Il est renvoyé tel quel s’il a été fourni : ce n’est pas le logo DevForge. +//! +//! Les PNG viennent de `brand/` (même fichier que `apps/web/public/brand/icon-512.png`). +//! Le SVG est le logo vectoriel déjà référencé par les plugins +//! (`cursor-plugin/*/assets/logo.svg`). `brand/icon.svg` est un PNG encapsulé, trop +//! lourd pour une carte de connecteur. + +use axum::body::Body; +use axum::http::{header, HeaderValue, StatusCode}; +use axum::response::Response; +use axum::routing::get; +use axum::Router; +use serde_json::{json, Value}; + +use crate::state::AppState; + +const PNG_512: &[u8] = include_bytes!("../../../brand/icon-512.png"); +const PNG_256: &[u8] = include_bytes!("../../../brand/icon-256.png"); +const SVG: &[u8] = include_bytes!("../../../cursor-plugin/devforge/assets/logo.svg"); + +pub fn router() -> Router { + icon_router() +} + +fn icon_router() -> Router +where + S: Clone + Send + Sync + 'static, +{ + Router::new() + .route("/icons/devforge-512.png", get(icon_512)) + .route("/icons/devforge-256.png", get(icon_256)) + .route("/icons/devforge.svg", get(icon_svg)) +} + +/// URL absolue annoncée comme `logo_uri` (PNG 512, fond opaque). +pub fn logo_uri(base: &str) -> String { + format!( + "{}/icons/devforge-512.png", + base.trim().trim_end_matches('/') + ) +} + +/// Icônes au format MCP (`serverInfo.icons`). +pub fn icons_json(base: &str) -> Value { + let b = base.trim().trim_end_matches('/'); + json!([ + { "src": format!("{b}/icons/devforge-512.png"), "mimeType": "image/png", "sizes": ["512x512"] }, + { "src": format!("{b}/icons/devforge-256.png"), "mimeType": "image/png", "sizes": ["256x256"] }, + { "src": format!("{b}/icons/devforge.svg"), "mimeType": "image/svg+xml", "sizes": ["any"] } + ]) +} + +pub fn server_info(base: &str) -> Value { + let base = base.trim().trim_end_matches('/'); + let mut info = json!({ + "name": "devforge", + "title": "DevForge", + "version": env!("CARGO_PKG_VERSION"), + }); + if !base.is_empty() { + info["websiteUrl"] = json!(base); + info["icons"] = icons_json(base); + } + info +} + +fn icon(bytes: &'static [u8], mime: &'static str) -> Response { + Response::builder() + .status(StatusCode::OK) + .header(header::CONTENT_TYPE, HeaderValue::from_static(mime)) + .header( + header::CACHE_CONTROL, + HeaderValue::from_static("public, max-age=86400"), + ) + .header( + header::ACCESS_CONTROL_ALLOW_ORIGIN, + HeaderValue::from_static("*"), + ) + .header( + "x-content-type-options", + HeaderValue::from_static("nosniff"), + ) + .body(Body::from(bytes)) + .expect("réponse icône") +} + +async fn icon_512() -> Response { + icon(PNG_512, "image/png") +} + +async fn icon_256() -> Response { + icon(PNG_256, "image/png") +} + +async fn icon_svg() -> Response { + icon(SVG, "image/svg+xml") +} + +#[cfg(test)] +mod tests { + use super::*; + use axum::body::to_bytes; + use axum::http::header; + use tower::ServiceExt; + + fn png_dims(b: &[u8]) -> (u32, u32) { + assert!(b.len() >= 24, "PNG trop court"); + assert_eq!(&b[..8], b"\x89PNG\r\n\x1a\n"); + ( + u32::from_be_bytes(b[16..20].try_into().unwrap()), + u32::from_be_bytes(b[20..24].try_into().unwrap()), + ) + } + + #[test] + fn embedded_icons_match_announced_sizes() { + assert_eq!(png_dims(PNG_512), (512, 512)); + assert_eq!(png_dims(PNG_256), (256, 256)); + let svg = std::str::from_utf8(SVG).unwrap(); + assert!(svg.contains("(); + for (uri, mime, png) in [ + ("/icons/devforge-512.png", "image/png", true), + ("/icons/devforge-256.png", "image/png", true), + ("/icons/devforge.svg", "image/svg+xml", false), + ] { + let res = app + .clone() + .oneshot( + axum::http::Request::builder() + .uri(uri) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(res.status(), StatusCode::OK, "{uri}"); + assert_eq!(res.headers()[header::CONTENT_TYPE], mime, "{uri}"); + assert!(res.headers()[header::CACHE_CONTROL] + .to_str() + .unwrap() + .contains("max-age=86400")); + assert_eq!(res.headers()[header::ACCESS_CONTROL_ALLOW_ORIGIN], "*"); + let bytes = to_bytes(res.into_body(), 1024 * 1024).await.unwrap(); + if png { + let (w, h) = png_dims(&bytes); + assert_eq!(w, h); + assert!(w == 512 || w == 256, "{uri} {w}"); + } else { + assert!(bytes.starts_with(b" Value { "bearer_methods_supported": ["header"], "resource_name": "DevForge", "resource_documentation": base, + "logo_uri": crate::mcp_brand::logo_uri(base), }) } @@ -255,6 +256,8 @@ pub fn authorization_server_doc(base: &str) -> Value { "client_id_metadata_document_supported": true, "authorization_response_iss_parameter_supported": true, "service_documentation": base, + "client_uri": base, + "logo_uri": crate::mcp_brand::logo_uri(base), }) } diff --git a/apps/server/src/mcp_oauth_tests.rs b/apps/server/src/mcp_oauth_tests.rs index 53dcb9c6e..ab69e1d48 100644 --- a/apps/server/src/mcp_oauth_tests.rs +++ b/apps/server/src/mcp_oauth_tests.rs @@ -45,6 +45,10 @@ fn metadata_documents_use_public_https_base() { ); assert_eq!(asm["code_challenge_methods_supported"], json!(["S256"])); assert_eq!(asm["client_id_metadata_document_supported"], true); + let logo = "https://web.jeser.app/icons/devforge-512.png"; + assert_eq!(prm["logo_uri"], logo); + assert_eq!(asm["logo_uri"], logo); + assert_eq!(asm["client_uri"], BASE); } #[test] diff --git a/apps/server/src/mcp_routes.rs b/apps/server/src/mcp_routes.rs index 65cbbe19a..ccfb57a4b 100644 --- a/apps/server/src/mcp_routes.rs +++ b/apps/server/src/mcp_routes.rs @@ -573,10 +573,13 @@ async fn mcp_jsonrpc( } }; + let base = resolve_public_base_url(&state, &headers) + .await + .unwrap_or_default(); if let Value::Array(items) = parsed { let mut out = Vec::new(); for item in items { - if let Some(resp) = mcp_handle_one(&state, &abilities, &workspace, item).await { + if let Some(resp) = mcp_handle_one(&state, &abilities, &workspace, &base, item).await { out.push(resp); } } @@ -585,7 +588,7 @@ async fn mcp_jsonrpc( } return Json(Value::Array(out)).into_response(); } - match mcp_handle_one(&state, &abilities, &workspace, parsed).await { + match mcp_handle_one(&state, &abilities, &workspace, &base, parsed).await { Some(resp) => Json(resp).into_response(), None => axum::http::StatusCode::ACCEPTED.into_response(), } @@ -596,6 +599,7 @@ async fn mcp_handle_one( state: &AppState, abilities: &[String], workspace: &str, + public_base: &str, raw: Value, ) -> Option { let body: JsonRpcRequest = match serde_json::from_value(raw) { @@ -637,11 +641,7 @@ async fn mcp_handle_one( rpc_ok(json!({ "protocolVersion": negotiate_protocol_version(requested), "capabilities": { "tools": { "listChanged": false } }, - "serverInfo": { - "name": "devforge", - "title": "DevForge", - "version": env!("CARGO_PKG_VERSION"), - }, + "serverInfo": crate::mcp_brand::server_info(public_base), "instructions": "DevForge : PaaS auto-hébergé. Outils pour lister les projets, lire/écrire des fichiers, déployer, lire les logs et vérifier la santé des applications.", })) } @@ -1655,7 +1655,7 @@ async fn oauth_client_metadata( "grant_types": ["authorization_code", "refresh_token"], "response_types": ["code"], "client_uri": app_url, - "logo_uri": format!("{}/favicon.ico", app_url), + "logo_uri": crate::mcp_brand::logo_uri(&app_url), }))) }