Skip to content
Closed
4 changes: 3 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ windows-sys = {version = "0.61", features = [
"Win32_Graphics_Gdi",
"Win32_Security",
"Win32_Storage_FileSystem",
"Win32_System_DataExchange",
"Win32_System_Diagnostics_ToolHelp",
"Win32_System_Pipes",
"Win32_System_JobObjects",
"Win32_System_LibraryLoader",
"Win32_System_Memory",
"Win32_System_Pipes",
"Win32_System_Power",
"Win32_System_SystemInformation",
"Win32_System_Threading",
Expand Down
26 changes: 26 additions & 0 deletions src-tauri/src/platform/macos/settings_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct Ivars {
excluded: RefCell<Option<Retained<NSTextView>>>,
payload: RefCell<Option<Retained<NSTextField>>>,
memory_path: RefCell<Option<Retained<NSTextField>>>,
mcp_url: RefCell<Option<Retained<NSTextField>>>,
character: RefCell<Option<Retained<NSPopUpButton>>>,
harness: RefCell<Option<Retained<NSPopUpButton>>>,
harness_state: RefCell<Option<Retained<NSTextField>>>,
Expand Down Expand Up @@ -277,6 +278,7 @@ define_class!(
form::RowOperation::ClearKey => self.do_clear_key(),
form::RowOperation::Apply => self.do_apply(),
form::RowOperation::Cancel => self.do_cancel(),
form::RowOperation::CopyMcpToken => self.do_copy_mcp_token(),
}
}

Expand Down Expand Up @@ -444,6 +446,19 @@ impl SettingsController {
self.draw(true);
}

fn do_copy_mcp_token(&self) {
use objc2_app_kit::NSPasteboard;

if let Some(endpoint) = crate::mcp_http::endpoint() {
let pb = NSPasteboard::generalPasteboard();
pb.clearContents();
let token_string = NSString::from_str(&endpoint.authorization());
unsafe {
pb.setString_forType(&token_string, objc2_app_kit::NSPasteboardTypeString);
}
}
}

/// The field comes off the row itself rather than a literal, so the blur
/// and the row cannot disagree about which field the text belongs to.
fn commit_excluded(&self) {
Expand Down Expand Up @@ -572,6 +587,9 @@ impl SettingsController {
if let Some(field) = self.ivars().memory_path.borrow().clone() {
field.setStringValue(&NSString::from_str(&view.memory_path));
}
if let Some(field) = self.ivars().mcp_url.borrow().clone() {
field.setStringValue(&NSString::from_str(&view.mcp_url));
}
if let Some(field) = self.ivars().payload.borrow().clone() {
field.setStringValue(&NSString::from_str(
view.last_payload.as_deref().unwrap_or("Nothing sent yet."),
Expand Down Expand Up @@ -727,6 +745,7 @@ fn build(mtm: MainThreadMarker, session: SettingsSession) -> Retained<SettingsCo
let mut excluded_text = None;
let mut payload_field = None;
let mut memory_path_field = None;
let mut mcp_url_field = None;
let mut character_popup = None;
let mut harness_popup = None;
let mut harness_state_field = None;
Expand Down Expand Up @@ -928,6 +947,12 @@ fn build(mtm: MainThreadMarker, session: SettingsSession) -> Retained<SettingsCo
cursor.place(&field, 44.0);
harness_state_field = Some(field);
}
form::MCP_URL_ID => {
let field = inspect_line(mtm);
field.setSelectable(true);
cursor.place(&field, 22.0);
mcp_url_field = Some(field);
}
_ => {}
}

Expand Down Expand Up @@ -1147,6 +1172,7 @@ fn build(mtm: MainThreadMarker, session: SettingsSession) -> Retained<SettingsCo
*controller.ivars().excluded.borrow_mut() = excluded_text;
*controller.ivars().payload.borrow_mut() = payload_field;
*controller.ivars().memory_path.borrow_mut() = memory_path_field;
*controller.ivars().mcp_url.borrow_mut() = mcp_url_field;
*controller.ivars().character.borrow_mut() = character_popup;
*controller.ivars().harness.borrow_mut() = harness_popup;
*controller.ivars().harness_state.borrow_mut() = harness_state_field;
Expand Down
40 changes: 40 additions & 0 deletions src-tauri/src/platform/windows/settings_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ impl SettingsWindow {
}
let text = match id.as_str() {
form::MEMORY_PATH_ID => view.memory_path.clone(),
form::MCP_URL_ID => view.mcp_url.clone(),
form::HOTKEY_ID => view.hide_hotkey.clone(),
form::PAYLOAD_ID => view
.last_payload
Expand Down Expand Up @@ -341,6 +342,7 @@ impl SettingsWindow {
RowOperation::ClearKey => self.do_clear_key(),
RowOperation::Apply => self.do_apply(),
RowOperation::Cancel => self.do_cancel(),
RowOperation::CopyMcpToken => self.do_copy_mcp_token(),
}
}
}
Expand Down Expand Up @@ -483,6 +485,43 @@ impl SettingsWindow {
self.draw(true);
}

fn do_copy_mcp_token(&self) {
use windows_sys::Win32::System::DataExchange::{
CloseClipboard, EmptyClipboard, OpenClipboard, SetClipboardData,
};
use windows_sys::Win32::System::Memory::{
GlobalAlloc, GlobalLock, GlobalUnlock, GMEM_MOVEABLE,
};

const CF_TEXT: u32 = 1;

if let Some(endpoint) = crate::mcp_http::endpoint() {
let token = endpoint.authorization();
let token_bytes = token.as_bytes();

unsafe {
if OpenClipboard(self.hwnd) != 0 {
EmptyClipboard();
let h_mem = GlobalAlloc(GMEM_MOVEABLE, token_bytes.len() + 1);
if !h_mem.is_null() {
let p_mem = GlobalLock(h_mem);
if !p_mem.is_null() {
ptr::copy_nonoverlapping(
token_bytes.as_ptr(),
p_mem as *mut u8,
token_bytes.len(),
);
*(p_mem.add(token_bytes.len()) as *mut u8) = 0;
GlobalUnlock(h_mem);
SetClipboardData(CF_TEXT, h_mem as _);
}
}
CloseClipboard();
}
}
}
}

fn update_tab_visibility(&self) {
let current_tab = *self.current_tab.borrow();
let controls = self.controls.borrow();
Expand Down Expand Up @@ -610,6 +649,7 @@ fn set_window_text(hwnd: HWND, text: &str) {
/// heading/comment labels (`section_heading_*`, `section_comment_*`).
fn should_update_label_text(id: &str) -> bool {
if id == form::MEMORY_PATH_ID
|| id == form::MCP_URL_ID
|| id == form::HOTKEY_ID
|| id == form::PAYLOAD_ID
|| id == form::HARNESS_STATE_ID
Expand Down
22 changes: 22 additions & 0 deletions src-tauri/src/platform/x11/settings_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,25 @@ impl SettingsWindow {
eprintln!("settings: {e}");
}
}
RowOperation::CopyMcpToken => {
if let Some(endpoint) =
crate::mcp_http::endpoint()
{
if let Some(display) =
gtk::gdk::Display::default()
{
if let Some(clipboard) =
gtk::Clipboard::default(
&display,
)
{
clipboard.set_text(
&endpoint.authorization(),
);
}
}
}
}
_ => {}
}
}
Expand Down Expand Up @@ -932,6 +951,9 @@ impl SettingsWindow {
if let Some(Control::Label(label)) = controls.get(form::MEMORY_PATH_ID) {
label.set_text(&view.memory_path);
}
if let Some(Control::Label(label)) = controls.get(form::MCP_URL_ID) {
label.set_text(&view.mcp_url);
}
if let Some(Control::Label(label)) = controls.get(form::PAYLOAD_ID) {
label.set_text(view.last_payload.as_deref().unwrap_or("Nothing sent yet."));
}
Expand Down
42 changes: 42 additions & 0 deletions src-tauri/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ pub struct SettingsView {
pub consent: Vec<ConsentRow>,
/// The name Privacy & Security will show for this process.
pub consent_listed_as: String,
/// The MCP server URL, when the server is running.
pub mcp_url: String,
/// Whether the MCP token can be copied (server is running and has a token).
pub mcp_token_available: bool,
}

/// The Development switches, by row id, as the window must draw them.
Expand Down Expand Up @@ -247,6 +251,10 @@ impl SettingsView {
harness: Option<crate::harness::HarnessInspect>,
) -> Self {
let (api_key_set, api_key_fingerprint, api_key_error) = api_key;
let (mcp_url, mcp_token_available) = match crate::mcp_http::endpoint() {
Some(endpoint) => (endpoint.url, true),
None => ("Not running".to_string(), false),
};
Self {
// The value in force, as the Development rows show theirs: an
// exported switch reads as it exported, however the file has it.
Expand Down Expand Up @@ -277,6 +285,8 @@ impl SettingsView {
development_texts: development_texts(settings),
consent: consent::rows(|id| settings.wants_consent(id)),
consent_listed_as: String::new(),
mcp_url,
mcp_token_available,
}
}

Expand Down Expand Up @@ -2741,6 +2751,38 @@ mod tests {
});
}

/// The MCP URL is populated when the server is running, and the token
/// never appears in the view.
#[test]
fn mcp_url_is_shown_but_token_is_never_visible() {
let view = endpoint_view(&Settings::default());
// The URL field is populated (either with a URL or "Not running").
assert!(!view.mcp_url.is_empty(), "MCP URL must be populated");
// The token availability flag reflects whether there's an endpoint.
assert_eq!(
view.mcp_token_available,
crate::mcp_http::endpoint().is_some(),
"Token availability must match endpoint presence"
);
// The token itself never appears in any field.
let view_debug = format!("{view:?}");
assert!(
!view_debug.contains("Bearer"),
"Token must not appear in Debug output"
);
if let Some(endpoint) = crate::mcp_http::endpoint() {
let token = endpoint.authorization();
assert!(
!view.mcp_url.contains(&token),
"Token must not appear in the URL field"
);
assert!(
!view_debug.contains(&token),
"Token must not appear in Debug output"
);
}
}

/// Every Development control the window draws has to find its value in
/// the view. A row whose id is missing renders off while the switch is on
/// (#273), and the first click on a persisted `true` then sends
Expand Down
42 changes: 40 additions & 2 deletions src-tauri/src/settings/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum RowOperation {
Apply,
/// Redraw the Director tab from live state, writing nothing.
Cancel,
CopyMcpToken,
}

/// One section of the settings form.
Expand Down Expand Up @@ -316,6 +317,8 @@ pub const HARNESS_COMMAND_ID: &str = "harness_command";
pub const HARNESS_STATE_ID: &str = "harness_state";
pub const HARNESS_AUTH_RETRY_SECS_ID: &str = "harness_auth_retry_secs";
pub const MCP_BIN_ID: &str = "mcp_bin";
pub const MCP_URL_ID: &str = "mcp_url";
pub const MCP_COPY_TOKEN_ID: &str = "mcp_copy_token";

/// The two Completer-source titles the file does not spell the same way: Off
/// is the empty string and Custom is `custom`, which defers to the command
Expand Down Expand Up @@ -1089,6 +1092,34 @@ fn development_sections() -> Vec<FormSection> {
},
],
},
FormSection {
heading: "MCP".to_string(),
comment: Some(
"These rows are for development and testing: for pointing a Harness \
you run yourself at ai-buddy. The URL and token change on every launch."
.to_string(),
),
rows: vec![
FormRow::InspectBlock {
id: MCP_URL_ID.to_string(),
label: Some("URL".to_string()),
help: None,
},
FormRow::Composite {
id: "mcp_token_row".to_string(),
controls: vec![CompositeControl::Button {
id: MCP_COPY_TOKEN_ID.to_string(),
label: "Copy token to clipboard".to_string(),
frozen: false,
}],
help: Some(
"The token never appears on screen. Copying it to the \
clipboard is the trade ADR-0026 makes."
.to_string(),
),
},
],
},
]
}

Expand Down Expand Up @@ -1124,6 +1155,7 @@ pub fn describe() -> FormDescription {
(CLEAR_KEY_ID.to_string(), RowOperation::ClearKey),
(APPLY_ID.to_string(), RowOperation::Apply),
(CANCEL_ID.to_string(), RowOperation::Cancel),
(MCP_COPY_TOKEN_ID.to_string(), RowOperation::CopyMcpToken),
]);

FormDescription { tabs, operations }
Expand Down Expand Up @@ -1171,6 +1203,7 @@ mod tests {
"Instances",
"Last user turn",
"Launch",
"MCP",
"Memory File",
"Traces",
"What the buddy can see",
Expand Down Expand Up @@ -1767,7 +1800,10 @@ mod tests {

fn row_id(row: &FormRow) -> Option<&str> {
match row {
FormRow::Checkbox { id, .. } | FormRow::TextField { id, .. } => Some(id.as_str()),
FormRow::Checkbox { id, .. }
| FormRow::TextField { id, .. }
| FormRow::InspectBlock { id, .. }
| FormRow::Composite { id, .. } => Some(id.as_str()),
_ => None,
}
}
Expand All @@ -1790,8 +1826,10 @@ mod tests {
"{} has to say what its rows are for, got {warning:?}",
section.heading
);
// MCP section has read-only rows (InspectBlock) and action buttons (Composite)
// which are allowed for displaying runtime state and triggering operations.
for row in &section.rows {
row_id(row).expect("every development row is a control that writes");
row_id(row).expect("every development row has an id");
}
}
}
Expand Down