-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(mcp) add MCP control for Cap. #1942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
onyedikachi-david
wants to merge
1
commit into
CapSoftware:main
Choose a base branch
from
onyedikachi-david:codex/cap-desktop-mcp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,289 @@ | ||
| use std::io::Write; | ||
|
|
||
| use serde::Deserialize; | ||
| use serde_json::{Value, json}; | ||
| use tokio::io::AsyncBufReadExt; | ||
|
|
||
| const DESKTOP_BUNDLE_IDS: [&str; 2] = ["so.cap.desktop", "so.cap.desktop.dev"]; | ||
| const PROTOCOL_VERSION: &str = "2025-11-25"; | ||
|
|
||
| #[derive(clap::Args)] | ||
| pub struct McpArgs {} | ||
|
|
||
| #[derive(Deserialize)] | ||
| #[serde(rename_all = "camelCase", default)] | ||
| struct DesktopMcpSettings { | ||
| enabled: bool, | ||
| token: Option<String>, | ||
| port: Option<u16>, | ||
| } | ||
|
|
||
| impl Default for DesktopMcpSettings { | ||
| fn default() -> Self { | ||
| Self { | ||
| enabled: false, | ||
| token: None, | ||
| port: None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct McpEndpoint { | ||
| endpoint: String, | ||
| token: String, | ||
| } | ||
|
|
||
| impl McpArgs { | ||
| pub async fn run(self) -> Result<(), String> { | ||
| run_stdio().await | ||
| } | ||
| } | ||
|
|
||
| async fn run_stdio() -> Result<(), String> { | ||
| let endpoint = load_endpoint(); | ||
| let client = reqwest::Client::builder() | ||
| .no_proxy() | ||
| .build() | ||
| .map_err(|err| format!("Failed to create MCP HTTP client: {err}"))?; | ||
| let mut session_id: Option<String> = None; | ||
| let mut lines = tokio::io::BufReader::new(tokio::io::stdin()).lines(); | ||
|
|
||
| while let Some(line) = lines | ||
| .next_line() | ||
| .await | ||
| .map_err(|err| format!("Failed to read MCP stdin: {err}"))? | ||
| { | ||
| if line.trim().is_empty() { | ||
| continue; | ||
| } | ||
|
|
||
| let id = request_id(&line); | ||
| let result = match &endpoint { | ||
| Ok(endpoint) => forward_message(&client, endpoint, &line, session_id.as_deref()).await, | ||
| Err(message) => Err(message.clone()), | ||
| }; | ||
|
|
||
| match result { | ||
| Ok(ForwardedResponse::Accepted) => {} | ||
| Ok(ForwardedResponse::Json { | ||
| body, | ||
| next_session_id, | ||
| }) => { | ||
| if let Some(next_session_id) = next_session_id { | ||
| session_id = Some(next_session_id); | ||
| } | ||
| write_json_line(&body)?; | ||
| } | ||
| Err(message) => { | ||
| if let Some(id) = id { | ||
| write_json_line(&json_rpc_error( | ||
| id, | ||
| -32000, | ||
| format!( | ||
| "{message}. Launch Cap Desktop and enable MCP before using cap mcp." | ||
| ), | ||
| ))?; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if let (Ok(endpoint), Some(session_id)) = (&endpoint, session_id.as_deref()) | ||
| && let Err(err) = delete_session(&client, endpoint, session_id).await | ||
| { | ||
| eprintln!("Failed to close Cap Desktop MCP session: {err}"); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| enum ForwardedResponse { | ||
| Accepted, | ||
| Json { | ||
| body: Value, | ||
| next_session_id: Option<String>, | ||
| }, | ||
| } | ||
|
|
||
| async fn forward_message( | ||
| client: &reqwest::Client, | ||
| endpoint: &McpEndpoint, | ||
| line: &str, | ||
| session_id: Option<&str>, | ||
| ) -> Result<ForwardedResponse, String> { | ||
| let mut request = client | ||
| .post(&endpoint.endpoint) | ||
| .bearer_auth(&endpoint.token) | ||
| .header("Accept", "application/json, text/event-stream") | ||
| .header("Content-Type", "application/json") | ||
| .header("MCP-Protocol-Version", PROTOCOL_VERSION) | ||
| .body(line.to_string()); | ||
|
|
||
| if let Some(session_id) = session_id { | ||
| request = request.header("Mcp-Session-Id", session_id); | ||
| } | ||
|
|
||
| let response = request | ||
| .send() | ||
| .await | ||
| .map_err(|err| format!("Cap Desktop MCP endpoint is unavailable: {err}"))?; | ||
|
|
||
| if response.status() == reqwest::StatusCode::ACCEPTED { | ||
| return Ok(ForwardedResponse::Accepted); | ||
| } | ||
|
|
||
| let status = response.status(); | ||
| let next_session_id = response | ||
| .headers() | ||
| .get("Mcp-Session-Id") | ||
| .and_then(|value| value.to_str().ok()) | ||
| .map(str::to_string); | ||
| let body = response | ||
| .json::<Value>() | ||
| .await | ||
| .map_err(|err| format!("Cap Desktop returned invalid MCP JSON: {err}"))?; | ||
|
|
||
| if !status.is_success() { | ||
| return Err(format!( | ||
| "Cap Desktop MCP request failed with {status}: {body}" | ||
| )); | ||
| } | ||
|
|
||
| Ok(ForwardedResponse::Json { | ||
| body, | ||
| next_session_id, | ||
| }) | ||
| } | ||
|
|
||
| async fn delete_session( | ||
| client: &reqwest::Client, | ||
| endpoint: &McpEndpoint, | ||
| session_id: &str, | ||
| ) -> Result<(), String> { | ||
| let response = delete_session_request(client, endpoint, session_id) | ||
| .send() | ||
| .await | ||
| .map_err(|err| format!("Failed to delete MCP session: {err}"))?; | ||
|
|
||
| if matches!( | ||
| response.status(), | ||
| reqwest::StatusCode::ACCEPTED | reqwest::StatusCode::NOT_FOUND | ||
| ) { | ||
| Ok(()) | ||
| } else { | ||
| Err(format!( | ||
| "Cap Desktop returned {} while deleting the MCP session", | ||
| response.status() | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| fn delete_session_request( | ||
| client: &reqwest::Client, | ||
| endpoint: &McpEndpoint, | ||
| session_id: &str, | ||
| ) -> reqwest::RequestBuilder { | ||
| client | ||
| .delete(&endpoint.endpoint) | ||
| .bearer_auth(&endpoint.token) | ||
| .header("MCP-Protocol-Version", PROTOCOL_VERSION) | ||
| .header("Mcp-Session-Id", session_id) | ||
| } | ||
|
|
||
| fn load_endpoint() -> Result<McpEndpoint, String> { | ||
| let data_dir = dirs::data_dir().ok_or("Cap Desktop data directory was not found")?; | ||
|
|
||
| for bundle_id in DESKTOP_BUNDLE_IDS { | ||
| let path = data_dir.join(bundle_id).join("store"); | ||
| let Ok(bytes) = std::fs::read(&path) else { | ||
| continue; | ||
| }; | ||
| let store: Value = serde_json::from_slice(&bytes) | ||
| .map_err(|err| format!("Failed to parse Cap Desktop store: {err}"))?; | ||
| let settings = store | ||
| .get("mcp") | ||
| .cloned() | ||
| .map(serde_json::from_value::<DesktopMcpSettings>) | ||
| .transpose() | ||
| .map_err(|err| format!("Failed to parse Cap Desktop MCP settings: {err}"))? | ||
| .unwrap_or_default(); | ||
|
|
||
| if !settings.enabled { | ||
| continue; | ||
| } | ||
|
|
||
| let token = settings | ||
| .token | ||
| .filter(|token| !token.is_empty()) | ||
| .ok_or("Cap Desktop MCP token is missing")?; | ||
| let port = settings.port.ok_or("Cap Desktop MCP port is missing")?; | ||
|
|
||
| return Ok(McpEndpoint { | ||
| endpoint: format!("http://127.0.0.1:{port}/mcp"), | ||
| token, | ||
| }); | ||
| } | ||
|
|
||
| Err("Cap Desktop MCP is not enabled or no running endpoint was found".to_string()) | ||
| } | ||
|
|
||
| fn request_id(line: &str) -> Option<Value> { | ||
| serde_json::from_str::<Value>(line) | ||
| .ok() | ||
| .and_then(|value| value.get("id").cloned()) | ||
| } | ||
|
|
||
| fn json_rpc_error(id: Value, code: i64, message: String) -> Value { | ||
| json!({ | ||
| "jsonrpc": "2.0", | ||
| "id": id, | ||
| "error": { | ||
| "code": code, | ||
| "message": message | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| fn write_json_line(value: &Value) -> Result<(), String> { | ||
| let mut stdout = std::io::stdout().lock(); | ||
| serde_json::to_writer(&mut stdout, value) | ||
| .map_err(|err| format!("Failed to write MCP stdout: {err}"))?; | ||
| stdout | ||
| .write_all(b"\n") | ||
| .map_err(|err| format!("Failed to write MCP stdout: {err}"))?; | ||
| stdout | ||
| .flush() | ||
| .map_err(|err| format!("Failed to flush MCP stdout: {err}")) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn builds_authenticated_session_delete_request() { | ||
| let client = reqwest::Client::builder().no_proxy().build().unwrap(); | ||
| let endpoint = McpEndpoint { | ||
| endpoint: "http://127.0.0.1:1234/mcp".to_string(), | ||
| token: "test-token".to_string(), | ||
| }; | ||
| let request = delete_session_request(&client, &endpoint, "test-session") | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(request.method(), reqwest::Method::DELETE); | ||
| assert_eq!(request.url().as_str(), endpoint.endpoint); | ||
| assert_eq!( | ||
| request.headers().get("authorization").unwrap(), | ||
| "Bearer test-token" | ||
| ); | ||
| assert_eq!( | ||
| request.headers().get("mcp-protocol-version").unwrap(), | ||
| PROTOCOL_VERSION | ||
| ); | ||
| assert_eq!( | ||
| request.headers().get("mcp-session-id").unwrap(), | ||
| "test-session" | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ mod hotkeys; | |
| mod http_client; | ||
| mod import; | ||
| mod logging; | ||
| mod mcp; | ||
| mod notifications; | ||
| mod panel_manager; | ||
| mod permissions; | ||
|
|
@@ -2255,6 +2256,7 @@ struct CurrentRecording { | |
| target: CurrentRecordingTarget, | ||
| mode: RecordingMode, | ||
| status: RecordingStatus, | ||
| paused: bool, | ||
| } | ||
|
|
||
| #[tauri::command] | ||
|
|
@@ -2265,15 +2267,18 @@ async fn get_current_recording( | |
| ) -> Result<JsonValue<Option<CurrentRecording>>, ()> { | ||
| let state = state.read().await; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
|
|
||
| let (mode, capture_target, status) = match &state.recording_state { | ||
| let (mode, capture_target, status, paused) = match &state.recording_state { | ||
| RecordingState::None => { | ||
| return Ok(JsonValue::new(&None)); | ||
| } | ||
| RecordingState::Pending { mode, target } => (*mode, target, RecordingStatus::Pending), | ||
| RecordingState::Pending { mode, target } => { | ||
| (*mode, target, RecordingStatus::Pending, false) | ||
| } | ||
| RecordingState::Active(inner) => ( | ||
| inner.mode(), | ||
| inner.capture_target(), | ||
| RecordingStatus::Recording, | ||
| inner.is_paused().await.map_err(|_| ())?, | ||
| ), | ||
| }; | ||
|
|
||
|
|
@@ -2298,6 +2303,7 @@ async fn get_current_recording( | |
| target, | ||
| mode, | ||
| status, | ||
| paused, | ||
| }))) | ||
| } | ||
|
|
||
|
|
@@ -4282,6 +4288,9 @@ pub async fn run(recording_logging_handle: LoggingHandle, logs_dir: PathBuf) { | |
| cli::get_cli_install_status, | ||
| cli::install_cli, | ||
| cli::uninstall_cli, | ||
| mcp::get_mcp_server_config, | ||
| mcp::set_mcp_server_enabled, | ||
| mcp::rotate_mcp_server_token, | ||
| recording::start_recording, | ||
| recording::stop_recording, | ||
| recording::pause_recording, | ||
|
|
@@ -4750,6 +4759,8 @@ pub async fn run(recording_logging_handle: LoggingHandle, logs_dir: PathBuf) { | |
| app.manage(Arc::new(RwLock::new( | ||
| ClipboardContext::new().expect("Failed to create clipboard context"), | ||
| ))); | ||
|
|
||
| mcp::init(&app); | ||
| } | ||
|
|
||
| app.listen_any("main-window-ready", { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You’re advertising
text/event-streamhere but the client always tries to parse the response body as JSON. If the desktop endpoint ever switches to SSE for streaming responses, this will fail; consider restricting theAcceptheader to JSON unless you’re going to implement SSE parsing.