nab 0.7.1

Token-optimized HTTP client for LLMs — fetches any URL as clean markdown
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Elicitation helpers for `nab-mcp`.
//!
//! Contains all interactive prompts sent to the MCP client:
//! - Credential form (username + password)
//! - Credential choice (when multiple 1Password entries match)
//! - OAuth/SSO URL elicitation
//! - Browser cookie-source multi-select
//! - Cookie resolution logic that stitches the above together

use std::collections::BTreeMap;
use std::fmt::Write as FmtWrite;
use std::sync::Arc;

use rust_mcp_sdk::McpServer;
use rust_mcp_sdk::schema::{
    CallToolResult, ElicitFormSchema, ElicitRequestFormParams, ElicitRequestParams,
    ElicitRequestUrlParams, ElicitResultAction, ElicitResultContent, ElicitResultContentPrimitive,
    LegacyTitledEnumSchema, PrimitiveSchemaDefinition, StringSchema, TextContent,
    TitledMultiSelectEnumSchema, TitledMultiSelectEnumSchemaItems,
    TitledMultiSelectEnumSchemaItemsAnyOfItem, schema_utils::CallToolError,
};

use nab::CookieSource;
use nab::content::ContentRouter;

use crate::structured::truncate_markdown;
use crate::tools::get_client;

// ─── Elicitation helpers ──────────────────────────────────────────────────────

/// Ask the user to provide a username and password when no stored credential exists.
pub(crate) async fn elicit_credentials(
    runtime: &Arc<dyn McpServer>,
    url: &str,
) -> Result<(String, String), CallToolError> {
    let mut properties = BTreeMap::new();
    properties.insert(
        "username".into(),
        PrimitiveSchemaDefinition::StringSchema(StringSchema::new(
            None,
            Some("Your username or email address".into()),
            None,
            None,
            None,
            Some("Username".into()),
        )),
    );
    properties.insert(
        "password".into(),
        PrimitiveSchemaDefinition::StringSchema(StringSchema::new(
            None,
            Some("Your password".into()),
            None,
            None,
            None,
            Some("Password".into()),
        )),
    );

    let schema =
        ElicitFormSchema::new(properties, vec!["username".into(), "password".into()], None);

    let result = runtime
        .request_elicitation(ElicitRequestParams::FormParams(
            ElicitRequestFormParams::new(
                format!("No stored credentials found for {url}. Please enter your login details:"),
                schema,
                None,
                None,
            ),
        ))
        .await
        .map_err(|e| CallToolError::from_message(e.to_string()))?;

    match result.action {
        ElicitResultAction::Accept => {
            let content = result.content.unwrap_or_default();
            let username = extract_string_field(&content, "username")?;
            let password = extract_string_field(&content, "password")?;
            Ok((username, password))
        }
        ElicitResultAction::Decline | ElicitResultAction::Cancel => {
            Err(CallToolError::from_message("Login cancelled by user"))
        }
    }
}

/// Ask the user to choose one credential when multiple match the domain.
pub(crate) async fn elicit_credential_choice(
    runtime: &Arc<dyn McpServer>,
    url: &str,
    credentials: &[nab::auth::Credential],
) -> Result<String, CallToolError> {
    let titles: Vec<String> = credentials.iter().map(|c| c.title.clone()).collect();
    let title_labels: Vec<String> = titles
        .iter()
        .enumerate()
        .map(|(i, t)| {
            let username = credentials[i]
                .username
                .as_deref()
                .map(|u| format!(" ({u})"))
                .unwrap_or_default();
            format!("{t}{username}")
        })
        .collect();

    let mut properties = BTreeMap::new();
    properties.insert(
        "credential".into(),
        PrimitiveSchemaDefinition::LegacyTitledEnumSchema(LegacyTitledEnumSchema::new(
            titles.clone(),
            title_labels,
            None,
            Some("Select the credential to use for login".into()),
            Some("Credential".into()),
        )),
    );

    let schema = ElicitFormSchema::new(properties, vec!["credential".into()], None);

    let result = runtime
        .request_elicitation(ElicitRequestParams::FormParams(
            ElicitRequestFormParams::new(
                format!("Multiple credentials found for {url}. Which one would you like to use?"),
                schema,
                None,
                None,
            ),
        ))
        .await
        .map_err(|e| CallToolError::from_message(e.to_string()))?;

    match result.action {
        ElicitResultAction::Accept => {
            let content = result.content.unwrap_or_default();
            extract_string_field(&content, "credential")
        }
        ElicitResultAction::Decline | ElicitResultAction::Cancel => {
            Err(CallToolError::from_message("Login cancelled by user"))
        }
    }
}

/// Perform a credential-based login using a manually-provided username + password.
///
/// This path is used when no 1Password entry exists and the user supplies
/// credentials via elicitation.  The `LoginFlow` cannot be used here because
/// it pulls credentials from 1Password internally, so we fall back to the
/// form-submission path (`SubmitTool`-style).
pub(crate) async fn run_login_with_credentials(
    url: &str,
    username: &str,
    password: &str,
    mut output: String,
) -> Result<CallToolResult, CallToolError> {
    let client = get_client().await;

    let page_html = client
        .fetch_text(url)
        .await
        .map_err(|e| CallToolError::from_message(e.to_string()))?;

    let mut forms =
        nab::Form::parse_all(&page_html).map_err(|e| CallToolError::from_message(e.to_string()))?;

    if forms.is_empty() {
        return Err(CallToolError::from_message("No login form found on page"));
    }

    let mut form = forms.remove(0);
    let _ = writeln!(output, "   Form: {} {}", form.method, form.action);

    // Best-effort field injection: typical login forms use username/email + password.
    form.fields
        .entry("username".into())
        .or_insert_with(|| username.to_string());
    form.fields
        .entry("email".into())
        .or_insert_with(|| username.to_string());
    form.fields.insert("password".into(), password.to_string());

    let action_url = form
        .resolve_action(url)
        .map_err(|e| CallToolError::from_message(e.to_string()))?;
    let form_data = form.encode_urlencoded();

    let response = client
        .inner()
        .post(&action_url)
        .header("Content-Type", form.content_type())
        .body(form_data)
        .send()
        .await
        .map_err(|e| CallToolError::from_message(e.to_string()))?;

    let status = response.status();
    let body = response
        .text()
        .await
        .map_err(|e| CallToolError::from_message(e.to_string()))?;

    let _ = writeln!(output, "   Status: {status}\n");

    let router = ContentRouter::new();
    let conversion = router
        .convert(body.as_bytes(), "text/html")
        .map_err(|e| CallToolError::from_message(e.to_string()))?;

    output.push_str(&truncate_markdown(&conversion.markdown, 4000));
    Ok(CallToolResult::text_content(vec![TextContent::from(
        output,
    )]))
}

/// Extract a string value from the elicitation response content map.
pub(crate) fn extract_string_field(
    content: &BTreeMap<String, ElicitResultContent>,
    field: &str,
) -> Result<String, CallToolError> {
    match content.get(field) {
        Some(ElicitResultContent::Primitive(ElicitResultContentPrimitive::String(s))) => {
            Ok(s.clone())
        }
        Some(_) => Err(CallToolError::from_message(format!(
            "Unexpected type for elicitation field '{field}'"
        ))),
        None => Err(CallToolError::from_message(format!(
            "Missing required elicitation field '{field}'"
        ))),
    }
}

// ─── OAuth URL elicitation ────────────────────────────────────────────────────

/// Known OAuth/SSO redirect hostname patterns.
const OAUTH_HOSTS: &[&str] = &[
    "accounts.google.com",
    "github.com/login/oauth",
    "login.microsoftonline.com",
    "appleid.apple.com",
    "facebook.com/login",
    "twitter.com/i/oauth",
    "x.com/i/oauth",
    "linkedin.com/oauth",
    "auth0.com",
    "okta.com",
    "pingidentity.com",
    "onelogin.com",
    "login.live.com",
];

/// Returns `true` when `url` looks like an OAuth/SSO provider redirect.
pub(crate) fn is_oauth_redirect(url: &str) -> bool {
    let lower = url.to_lowercase();
    OAUTH_HOSTS.iter().any(|&host| lower.contains(host))
}

/// Extract a human-readable service name from an OAuth URL for display in
/// the elicitation message.
pub(crate) fn oauth_service_name(url: &str) -> String {
    let lower = url.to_lowercase();
    if lower.contains("google") {
        "Google"
    } else if lower.contains("github") {
        "GitHub"
    } else if lower.contains("microsoft") || lower.contains("live.com") {
        "Microsoft"
    } else if lower.contains("apple") {
        "Apple"
    } else if lower.contains("facebook") {
        "Facebook"
    } else if lower.contains("twitter") || lower.contains("x.com") {
        "X (Twitter)"
    } else if lower.contains("linkedin") {
        "LinkedIn"
    } else if lower.contains("auth0") {
        "Auth0"
    } else if lower.contains("okta") {
        "Okta"
    } else {
        "the OAuth provider"
    }
    .to_string()
}

/// Send a URL elicitation to guide the user through an OAuth/SSO flow.
///
/// The 2025-11-25 protocol's `ElicitRequestUrlParams` lets the client open a
/// URL directly in the user's browser rather than showing a form.  After the
/// user completes the OAuth flow the server can capture the resulting cookies
/// via a follow-up form elicitation.
///
/// Returns the elicitation result action so the caller can branch on
/// accept/cancel.
pub(crate) async fn elicit_oauth_url(
    runtime: &Arc<dyn McpServer>,
    oauth_url: &str,
    service_name: &str,
) -> Result<ElicitResultAction, CallToolError> {
    // elicitation_id must be unique per request; use a short random suffix.
    let elicitation_id = format!(
        "oauth-{}-{}",
        service_name,
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.subsec_millis())
            .unwrap_or(0)
    );

    let result = runtime
        .request_elicitation(ElicitRequestParams::UrlParams(ElicitRequestUrlParams::new(
            elicitation_id,
            format!(
                "OAuth login required for {service_name}. \
                     Please complete the sign-in in your browser. \
                     The page will reload once authentication is complete."
            ),
            oauth_url.to_string(),
            None,
            None,
        )))
        .await
        .map_err(|e| CallToolError::from_message(e.to_string()))?;

    Ok(result.action)
}

// ─── Cookie resolution ───────────────────────────────────────────────────────

/// Resolve the cookie header to use for login.
///
/// When `explicit_cookies` is provided (the tool's `cookies` parameter), use it
/// directly.  Otherwise, offer multi-select elicitation so the user can choose
/// one or more browser cookie stores; cookies from all selected stores are
/// concatenated with `"; "` as separator.
///
/// # Errors
///
/// Returns `CallToolError` if the elicitation RPC fails.
pub(crate) async fn resolve_login_cookies(
    url: &str,
    explicit_cookies: Option<&str>,
    runtime: &Arc<dyn McpServer>,
) -> Result<Option<String>, CallToolError> {
    if let Some(cookie) = explicit_cookies {
        return Ok(Some(cookie.to_string()));
    }

    let sources = elicit_cookie_sources(runtime, url).await?;
    if sources.is_empty() {
        return Ok(None);
    }

    let domain = url::Url::parse(url)
        .ok()
        .and_then(|u| u.host_str().map(std::string::ToString::to_string))
        .unwrap_or_default();

    let combined = sources
        .iter()
        .filter_map(|s| {
            let source = CookieSource::from_browser_name(s);
            source.get_cookie_header(&domain).ok()
        })
        .collect::<Vec<_>>()
        .join("; ");

    Ok(if combined.is_empty() {
        None
    } else {
        Some(combined)
    })
}

// ─── Multi-select cookie source elicitation ───────────────────────────────────

/// Ask the user which browser cookie stores to use for the login, allowing
/// multiple sources to be selected simultaneously.
///
/// Uses `TitledMultiSelectEnumSchema` from the 2025-11-25 protocol spec.
/// Returns the selected browser names (e.g. `["brave", "chrome"]`).
pub(crate) async fn elicit_cookie_sources(
    runtime: &Arc<dyn McpServer>,
    url: &str,
) -> Result<Vec<String>, CallToolError> {
    let options: &[(&str, &str)] = &[
        ("brave", "Brave Browser"),
        ("chrome", "Google Chrome"),
        ("firefox", "Mozilla Firefox"),
        ("safari", "Apple Safari"),
    ];

    let items = TitledMultiSelectEnumSchemaItems {
        any_of: options
            .iter()
            .map(
                |&(value, label)| TitledMultiSelectEnumSchemaItemsAnyOfItem {
                    const_: value.to_string(),
                    title: label.to_string(),
                },
            )
            .collect(),
    };

    let multi_select = TitledMultiSelectEnumSchema::new(
        vec!["brave".to_string()], // default: Brave
        items,
        Some("Cookie stores to import for authentication".into()),
        None, // max_items
        None, // min_items
        Some("Cookie Sources".into()),
    );

    let mut properties = BTreeMap::new();
    properties.insert(
        "sources".into(),
        PrimitiveSchemaDefinition::TitledMultiSelectEnumSchema(multi_select),
    );

    let schema = ElicitFormSchema::new(properties, vec!["sources".into()], None);

    let result = runtime
        .request_elicitation(ElicitRequestParams::FormParams(
            ElicitRequestFormParams::new(
                format!(
                    "Which browser cookie stores should be used for login to {url}? \
                 Select all that apply."
                ),
                schema,
                None,
                None,
            ),
        ))
        .await
        .map_err(|e| CallToolError::from_message(e.to_string()))?;

    match result.action {
        ElicitResultAction::Accept => {
            // The multi-select result comes back as a JSON array of selected values.
            // The SDK encodes it as ElicitResultContent::Primitive::String (JSON-serialised
            // array) or as individual entries — extract and decode defensively.
            let content = result.content.unwrap_or_default();
            let sources = extract_multiselect_field(&content, "sources");
            Ok(sources)
        }
        ElicitResultAction::Decline | ElicitResultAction::Cancel => {
            // User skipped — return empty list so caller can fall back to no cookies.
            Ok(vec![])
        }
    }
}

/// Extract a multi-select field (array of strings) from elicitation result content.
///
/// Clients MAY encode the array as a JSON string `"[\"a\",\"b\"]"` or as a
/// comma-separated string `"a,b"`.  Both forms are handled here.  Returns an
/// empty `Vec` when the field is absent or has an unexpected type.
pub(crate) fn extract_multiselect_field(
    content: &BTreeMap<String, ElicitResultContent>,
    field: &str,
) -> Vec<String> {
    match content.get(field) {
        Some(ElicitResultContent::Primitive(ElicitResultContentPrimitive::String(s))) => {
            // Try JSON array first.
            if let Ok(vals) = serde_json::from_str::<Vec<String>>(s) {
                return vals;
            }
            // Fall back to comma-separated.
            s.split(',')
                .map(|v| v.trim().to_string())
                .filter(|v| !v.is_empty())
                .collect()
        }
        Some(_) | None => vec![],
    }
}