flymcprust-core 0.1.1

Runtime and transports of FlyMCPRust, a high-performance MCP server SDK for Rust. Use the `flymcprust` crate.
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
//! MCP Apps (`io.modelcontextprotocol/ui`): tools that render an
//! interactive HTML interface in the host.
//!
//! A server pairs a tool with a UI resource:
//!
//! - the resource has a `ui://` URI and the `text/html;profile=mcp-app` MIME
//!   type, and returns an HTML5 document. Mark it with
//!   [`ResourceDef::app`](crate::ResourceDef::app) (or `#[resource(.., app)]`),
//!   which also describes what the sandboxed page may load ([`AppUi`]);
//! - the tool names the resource with [`ToolDef::ui`](crate::ToolDef::ui)
//!   (or `#[tool(ui = "ui://..")]`), and may restrict who can call it with
//!   [`ToolDef::visibility`](crate::ToolDef::visibility).
//!
//! The server declares the extension in `server/discover` automatically.
//! App-only tools (`visibility = ["app"]`) are listed and callable for every
//! client: hosts that render interfaces hide them from the model, but a host
//! without MCP Apps shows them like any tool. Visibility is not access
//! control: protect sensitive tools with OAuth scopes.
//!
//! The page runs in the host's sandbox but can call tools through it: do not
//! paste untrusted data into its HTML (cross-site scripting would let an
//! attacker call tools as the user). Send data in tool results instead.
//! Tools with a UI must still return meaningful `content`: hosts without
//! MCP Apps support show that text, and the model reads it. Check
//! [`Ctx::client_supports_apps`](crate::Ctx::client_supports_apps) to adapt
//! a result.

use serde_json::{Map, Value, json};

use crate::context::Ctx;

/// The extension identifier.
pub const EXTENSION_ID: &str = "io.modelcontextprotocol/ui";

/// The MIME type of UI resources.
pub const MIME_TYPE: &str = "text/html;profile=mcp-app";

/// The URI scheme of UI resources.
pub(crate) const URI_SCHEME: &str = "ui://";

/// Who may call a tool.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Visibility {
    /// The model (the tool is in the agent's tool list).
    Model,
    /// The app's interface (the page can call it through the host).
    App,
}

impl Visibility {
    /// The wire value.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Model => "model",
            Self::App => "app",
        }
    }
}

/// A browser capability a UI asks the host for. Hosts may refuse.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Permission {
    /// The camera.
    Camera,
    /// The microphone.
    Microphone,
    /// The device's location.
    Geolocation,
    /// Writing to the clipboard.
    ClipboardWrite,
}

impl Permission {
    /// The wire key.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Camera => "camera",
            Self::Microphone => "microphone",
            Self::Geolocation => "geolocation",
            Self::ClipboardWrite => "clipboardWrite",
        }
    }
}

/// How the host should sandbox a UI resource (`_meta.ui`).
///
/// By default the page may load nothing from the network, embed no frames
/// and gets no extra permissions: declare every origin it needs. Hosts
/// enforce this with a Content Security Policy.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct AppUi {
    connect_domains: Vec<String>,
    resource_domains: Vec<String>,
    frame_domains: Vec<String>,
    base_uri_domains: Vec<String>,
    permissions: Vec<Permission>,
    domain: Option<String>,
    prefers_border: Option<bool>,
}

fn strings<I, S>(items: I) -> Vec<String>
where
    I: IntoIterator<Item = S>,
    S: Into<String>,
{
    items.into_iter().map(Into::into).collect()
}

impl AppUi {
    /// No network access, no frames, no permissions.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Origins the page may connect to with `fetch`, XHR or WebSocket
    /// (`connect-src`), e.g. `https://api.example.com`.
    #[must_use]
    pub fn connect_domains<I, S>(mut self, origins: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.connect_domains = strings(origins);
        self
    }

    /// Origins of scripts, styles, images, fonts and media; wildcard
    /// subdomains such as `https://*.example.com` are allowed.
    #[must_use]
    pub fn resource_domains<I, S>(mut self, origins: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.resource_domains = strings(origins);
        self
    }

    /// Origins the page may embed in frames (`frame-src`).
    #[must_use]
    pub fn frame_domains<I, S>(mut self, origins: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.frame_domains = strings(origins);
        self
    }

    /// Origins allowed in the document's `<base>` element (`base-uri`).
    #[must_use]
    pub fn base_uri_domains<I, S>(mut self, origins: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.base_uri_domains = strings(origins);
        self
    }

    /// Browser capabilities the page asks for.
    #[must_use]
    pub fn permissions(mut self, permissions: impl IntoIterator<Item = Permission>) -> Self {
        self.permissions = permissions.into_iter().collect();
        self
    }

    /// A dedicated origin for the page; the format is host-specific (see
    /// the host's documentation).
    #[must_use]
    pub fn domain(mut self, domain: impl Into<String>) -> Self {
        self.domain = Some(domain.into());
        self
    }

    /// Whether the host should draw a border around the page.
    #[must_use]
    pub fn prefers_border(mut self, border: bool) -> Self {
        self.prefers_border = Some(border);
        self
    }

    /// Checks the origins: hosts paste them into a Content Security
    /// Policy, so separators or quotes could widen it.
    pub(crate) fn validate(&self) -> Result<(), String> {
        let lists = [
            ("connect", &self.connect_domains),
            ("resource", &self.resource_domains),
            ("frame", &self.frame_domains),
            ("base URI", &self.base_uri_domains),
        ];
        for (kind, origins) in lists {
            for origin in origins {
                if !is_origin(origin) {
                    return Err(format!(
                        "invalid {kind} origin `{origin}`: expected `scheme://host[:port]`, \
                         such as `https://api.example.com` or `https://*.example.com`"
                    ));
                }
            }
        }
        if let Some(domain) = &self.domain
            && (domain.is_empty() || !domain.chars().all(is_safe_char))
        {
            return Err(format!("invalid domain `{domain}`"));
        }
        Ok(())
    }

    /// The `_meta.ui` object of the resource.
    pub(crate) fn to_meta(&self) -> Value {
        let mut ui = Map::new();
        let mut csp = Map::new();
        for (key, origins) in [
            ("connectDomains", &self.connect_domains),
            ("resourceDomains", &self.resource_domains),
            ("frameDomains", &self.frame_domains),
            ("baseUriDomains", &self.base_uri_domains),
        ] {
            if !origins.is_empty() {
                csp.insert(key.to_owned(), json!(origins));
            }
        }
        if !csp.is_empty() {
            ui.insert("csp".to_owned(), Value::Object(csp));
        }
        if !self.permissions.is_empty() {
            let permissions: Map<String, Value> = self
                .permissions
                .iter()
                .map(|p| (p.as_str().to_owned(), json!({})))
                .collect();
            ui.insert("permissions".to_owned(), Value::Object(permissions));
        }
        if let Some(domain) = &self.domain {
            ui.insert("domain".to_owned(), Value::from(domain.clone()));
        }
        if let Some(border) = self.prefers_border {
            ui.insert("prefersBorder".to_owned(), Value::from(border));
        }
        Value::Object(ui)
    }
}

/// Characters that cannot break out of a CSP source expression.
fn is_safe_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':' | '*' | '_' | '[' | ']')
}

/// `scheme://host[:port]`, with an optional `*.` wildcard label.
fn is_origin(origin: &str) -> bool {
    let Some((scheme, host)) = origin.split_once("://") else {
        return false;
    };
    // Web origins only: other schemes have no place in these directives.
    let scheme_ok = matches!(scheme, "https" | "wss" | "http" | "ws");
    let host_ok = !host.is_empty()
        && host.chars().all(is_safe_char)
        && match host.strip_prefix("*.") {
            // A wildcard must keep at least two labels (`*.example.com`),
            // not open a whole top-level domain (`*.com`).
            Some(rest) => {
                !rest.contains('*') && rest.split(':').next().is_some_and(|h| h.contains('.'))
            }
            None => !host.contains('*'),
        };
    scheme_ok && host_ok
}

/// The `_meta` entries of a tool with a UI or a restricted visibility.
pub(crate) fn tool_meta(
    meta: &mut Option<Map<String, Value>>,
    resource_uri: Option<&str>,
    visibility: Option<&[Visibility]>,
) {
    if resource_uri.is_none() && visibility.is_none() {
        return;
    }
    let mut ui = Map::new();
    if let Some(uri) = resource_uri {
        ui.insert("resourceUri".to_owned(), Value::from(uri));
    }
    if let Some(visibility) = visibility {
        ui.insert(
            "visibility".to_owned(),
            json!(visibility.iter().map(|v| v.as_str()).collect::<Vec<_>>()),
        );
    }
    let meta = meta.get_or_insert_with(Map::new);
    meta.insert("ui".to_owned(), Value::Object(ui));
    if let Some(uri) = resource_uri {
        // Deprecated flat key, still read by hosts that predate `_meta.ui`;
        // the specification drops it before its final release.
        meta.insert("ui/resourceUri".to_owned(), Value::from(uri));
    }
}

/// Repeats an interface's `_meta.ui` in its read results and sets the MIME
/// type when the handler left it out.
pub(crate) fn mark_contents(contents: &mut [crate::resource::ResourceContents], ui: &Value) {
    use crate::spec::ReadResourceResultContentsItem as Item;
    for item in contents {
        let (meta, mime) = match item {
            Item::Text(t) => (&mut t.meta, &mut t.mime_type),
            Item::Blob(b) => (&mut b.meta, &mut b.mime_type),
        };
        // Hosts render only this type; a handler cannot change it.
        *mime = Some(MIME_TYPE.to_owned());
        meta.get_or_insert_with(Map::new)
            .entry("ui")
            .or_insert_with(|| ui.clone());
    }
}

/// Whether client capabilities declare MCP Apps with its MIME type.
fn supported_by(capabilities: &crate::spec::ClientCapabilities) -> bool {
    capabilities
        .extensions
        .as_ref()
        .and_then(|e| e.get(EXTENSION_ID))
        .and_then(|settings| settings.get("mimeTypes"))
        .and_then(Value::as_array)
        .is_some_and(|types| types.iter().any(|t| t.as_str() == Some(MIME_TYPE)))
}

impl Ctx {
    /// Whether the client can render MCP Apps interfaces (it declared the
    /// extension with the `text/html;profile=mcp-app` MIME type). A tool
    /// can return a richer text answer when it cannot.
    #[must_use]
    pub fn client_supports_apps(&self) -> bool {
        supported_by(self.client_capabilities())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn metadata_shapes() {
        let ui = AppUi::new()
            .connect_domains(["https://api.example.com"])
            .resource_domains(["https://*.cdn.example.com"])
            .permissions([Permission::ClipboardWrite])
            .prefers_border(true);
        assert!(ui.validate().is_ok());
        assert_eq!(
            ui.to_meta(),
            json!({
                "csp": {
                    "connectDomains": ["https://api.example.com"],
                    "resourceDomains": ["https://*.cdn.example.com"],
                },
                "permissions": { "clipboardWrite": {} },
                "prefersBorder": true,
            })
        );
        assert_eq!(AppUi::new().to_meta(), json!({}));

        let mut meta = None;
        tool_meta(&mut meta, Some("ui://x/view"), Some(&[Visibility::App]));
        assert_eq!(
            Value::Object(meta.unwrap()),
            json!({
                "ui": { "resourceUri": "ui://x/view", "visibility": ["app"] },
                "ui/resourceUri": "ui://x/view",
            })
        );
    }

    #[test]
    fn origins_cannot_widen_the_policy() {
        for bad in [
            "api.example.com",
            "https://a.example.com; script-src *",
            "https://a.example.com 'unsafe-inline'",
            "https://a.example.com/path",
            "https://*",
            "https://a.*.example.com",
            "https://*.com",
            "javascript://x",
            "data://x",
            "",
        ] {
            assert!(
                AppUi::new().connect_domains([bad]).validate().is_err(),
                "{bad}"
            );
        }
        for good in [
            "https://api.example.com",
            "wss://live.example.com:8443",
            "https://*.example.com",
            "http://localhost:3000",
        ] {
            assert!(
                AppUi::new().connect_domains([good]).validate().is_ok(),
                "{good}"
            );
        }
    }
}