modelcontextprotocol 0.2.0

Filesystem, Fetch, Memory, Shell, Skills, and Agents MCP servers in a single binary, implementing the Model Context Protocol 2026-07-28 specification.
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
use std::time::Duration;

use url::Url;

pub const DEFAULT_USER_AGENT_AUTONOMOUS: &str =
    "ModelContextProtocol/1.0 (Autonomous; +https://github.com/modelcontextprotocol/servers)";
pub const DEFAULT_USER_AGENT_MANUAL: &str =
    "ModelContextProtocol/1.0 (User-Specified; +https://github.com/modelcontextprotocol/servers)";

const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);

/// Hard upper bound on the raw HTTP response body the fetch client will
/// buffer. `max_length` in the fetch tool is character-based output
/// truncation and must never be treated as the network safety limit, so an
/// oversized remote body is aborted here regardless of `max_length`.
pub const MAX_RESPONSE_BODY_BYTES: usize = 8 * 1024 * 1024;

/// Install the `ring` rustls crypto provider, required by reqwest's
/// `rustls-no-provider` feature. Safe to call repeatedly; only the first
/// install in a process takes effect.
fn install_rustls_provider() {
    let _ = rustls::crypto::ring::default_provider().install_default();
}

/// Trust boundary: the fetch server deliberately accepts arbitrary
/// http/https URLs, including localhost, loopback, link-local, and private
/// (RFC 1918) destinations reachable by the MCP process. There is
/// intentionally no SSRF-style destination policy — only enable this server
/// for trusted clients. This is documented product intent (see README and
/// docs/security.md), not an oversight.
///
/// Thin wrapper around an HTTP client with a fixed request timeout.
pub struct FetchClient {
    client: reqwest::Client,
}

impl FetchClient {
    pub fn new(proxy_url: Option<&str>) -> Result<Self, String> {
        install_rustls_provider();
        let mut builder = reqwest::Client::builder();
        if let Some(proxy) = proxy_url {
            let proxy = reqwest::Proxy::all(proxy)
                .map_err(|e| format!("Invalid proxy URL {proxy:?}: {e}"))?;
            builder = builder.proxy(proxy);
        }
        let client = builder
            .build()
            .map_err(|e| format!("Failed to build HTTP client: {e}"))?;
        Ok(Self { client })
    }

    /// GET a URL with the given User-Agent. Returns (body, content-type, status).
    pub async fn get(&self, url: &str, user_agent: &str) -> Result<(String, String, u16), String> {
        let response = self
            .client
            .get(url)
            .header(reqwest::header::USER_AGENT, user_agent)
            .timeout(REQUEST_TIMEOUT)
            .send()
            .await
            .map_err(|e| format!("Failed to fetch {url}: {e:?}"))?;
        read_bounded_response(response, url).await
    }
}

/// Read a response body with the hard [`MAX_RESPONSE_BODY_BYTES`] bound,
/// mirroring the bounded provider-response reader used by the agents
/// runtime: reject an oversized `Content-Length` up front (without relying
/// on it), then read chunk-by-chunk and abort as soon as the limit would be
/// exceeded. Never allocates an unbounded buffer and never includes remote
/// body content in error messages.
async fn read_bounded_response(
    mut response: reqwest::Response,
    url: &str,
) -> Result<(String, String, u16), String> {
    let status = response.status().as_u16();
    let content_type = response
        .headers()
        .get(reqwest::header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .unwrap_or_default()
        .to_string();

    if response
        .content_length()
        .is_some_and(|length| length > MAX_RESPONSE_BODY_BYTES as u64)
    {
        return Err(response_too_large(url));
    }

    let mut body = Vec::with_capacity(
        response
            .content_length()
            .map(|length| length.min(MAX_RESPONSE_BODY_BYTES as u64) as usize)
            .unwrap_or(0),
    );
    loop {
        let chunk = response
            .chunk()
            .await
            .map_err(|e| format!("Failed to read response body from {url}: {e}"))?;
        let Some(chunk) = chunk else {
            break;
        };
        if body.len().saturating_add(chunk.len()) > MAX_RESPONSE_BODY_BYTES {
            return Err(response_too_large(url));
        }
        body.extend_from_slice(&chunk);
    }
    Ok((decode_text(&body), content_type, status))
}

fn response_too_large(url: &str) -> String {
    format!("Response body from {url} exceeds the {MAX_RESPONSE_BODY_BYTES} byte safety limit")
}

/// Decode a bounded response body as text. Strips a UTF-8 byte-order mark
/// and decodes lossily (invalid sequences become U+FFFD), matching the
/// previous `reqwest::Response::text()` behavior for UTF-8 responses.
/// Declared non-UTF-8 charsets fall back to UTF-8 decoding; this is a
/// deliberate tradeoff to avoid pulling in an encoding library for an edge
/// case of a model-facing fetcher.
fn decode_text(body: &[u8]) -> String {
    let body = body.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(body);
    String::from_utf8_lossy(body).into_owned()
}

/// Build the robots.txt URL for a website URL.
pub fn robots_txt_url(url: &str) -> Result<String, String> {
    let parsed = Url::parse(url).map_err(|e| format!("Invalid URL {url}: {e}"))?;
    Ok(format!(
        "{}://{}/robots.txt",
        parsed.scheme(),
        parsed.authority()
    ))
}

/// Check that the given user agent is allowed to autonomously fetch `url`,
/// according to the site's robots.txt. Returns an error describing the
/// rejection. Mirrors the reference Python server's behavior:
///  - robots.txt unreachable -> error
///  - 401/403 from robots.txt -> error (assume fetching is not allowed)
///  - other 4xx from robots.txt -> allowed
///  - otherwise parse the robots.txt and match `url` against the user agent
pub async fn check_may_fetch(
    client: &FetchClient,
    url: &str,
    user_agent: &str,
) -> Result<(), String> {
    let robots_url = robots_txt_url(url)?;
    let (body, _content_type, status) = client.get(&robots_url, user_agent).await.map_err(|e| {
        format!("Failed to fetch robots.txt {robots_url} due to a connection issue: {e}")
    })?;

    if status == 401 || status == 403 {
        return Err(format!(
            "When fetching robots.txt ({robots_url}), received status {status} so assuming \
             that autonomous fetching is not allowed, the user can try manually fetching \
             by using the fetch prompt"
        ));
    }
    if (400..500).contains(&status) {
        return Ok(());
    }

    // Strip comments before parsing, like the reference implementation.
    let processed: String = body
        .lines()
        .filter(|line| !line.trim_start().starts_with('#'))
        .collect::<Vec<_>>()
        .join("\n");

    let mut matcher = robotstxt::DefaultMatcher::default();
    if !matcher.one_agent_allowed_by_robots(&processed, user_agent, url) {
        return Err(format!(
            "The sites robots.txt ({robots_url}), specifies that autonomous fetching of \
             this page is not allowed, \
             <useragent>{user_agent}</useragent>\n\
             <url>{url}</url>\n\
             <robots>\n{body}\n</robots>\n\
             The assistant must let the user know that it failed to view the page. \
             The assistant may provide further guidance based on the above information. \
             The assistant can tell the user that they can try manually fetching the page \
             by using the fetch prompt within their UI."
        ));
    }
    Ok(())
}

/// Fetch a URL and return content ready for the model, plus an optional
/// status prefix (used when the body could not be simplified to markdown).
pub async fn fetch_url(
    client: &FetchClient,
    url: &str,
    user_agent: &str,
    force_raw: bool,
) -> Result<(String, String), String> {
    let (page_raw, content_type, status) = client.get(url, user_agent).await?;
    if status >= 400 {
        return Err(format!("Failed to fetch {url} - status code {status}"));
    }

    let first_100: String = page_raw.chars().take(100).collect();
    let is_page_html = first_100.contains("<html")
        || content_type.contains("text/html")
        || content_type.is_empty();

    if is_page_html && !force_raw {
        Ok((extract_content_from_html(&page_raw), String::new()))
    } else {
        Ok((
            page_raw,
            format!(
                "Content type {content_type} cannot be simplified to markdown, but here is \
                 the raw content:\n"
            ),
        ))
    }
}

/// Convert HTML to a simplified markdown form. Returns an error marker when
/// nothing could be extracted.
pub fn extract_content_from_html(html: &str) -> String {
    let content = parse_html(html);
    let content = content.trim();
    if content.is_empty() {
        "<error>Page failed to be simplified from HTML</error>".to_string()
    } else {
        content.to_string()
    }
}

/// HTML→markdown conversion that drops non-content tags (`style`,
/// `script`, ...) instead of leaking their raw text into the output.
fn parse_html(html: &str) -> String {
    let mut custom: std::collections::HashMap<String, Box<dyn html2md::TagHandlerFactory>> =
        std::collections::HashMap::new();
    for tag in ["style", "script", "noscript", "template"] {
        custom.insert(tag.to_string(), Box::new(SuppressFactory));
    }
    html2md::parse_html_custom(html, &custom)
}

/// Renders a tag and all of its descendants as nothing.
#[derive(Default)]
struct SuppressHandler;

impl html2md::TagHandler for SuppressHandler {
    fn handle(&mut self, _tag: &html2md::Handle, _printer: &mut html2md::StructuredPrinter) {}
    fn after_handle(&mut self, _printer: &mut html2md::StructuredPrinter) {}
    fn skip_descendants(&self) -> bool {
        true
    }
}

#[derive(Default)]
struct SuppressFactory;

impl html2md::TagHandlerFactory for SuppressFactory {
    fn instantiate(&self) -> Box<dyn html2md::TagHandler> {
        Box::new(SuppressHandler)
    }
}

/// Apply `max_length`/`start_index` truncation semantics, mirroring the
/// reference server: character-based indexing, an error marker when no more
/// content is available, and a hint telling the model how to continue when
/// the content was truncated.
pub fn truncate(content: &str, start_index: usize, max_length: usize) -> String {
    let chars: Vec<char> = content.chars().collect();
    if start_index >= chars.len() {
        return "<error>No more content available.</error>".to_string();
    }
    let end = (start_index + max_length).min(chars.len());
    let actual = end - start_index;
    let mut out: String = chars[start_index..end].iter().collect();
    if actual == max_length && end < chars.len() {
        let next_start = start_index + actual;
        out.push_str(&format!(
            "\n\n<error>Content truncated. Call the fetch tool with a start_index of \
             {next_start} to get more content.</error>"
        ));
    }
    out
}

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

    #[test]
    fn robots_url_construction() {
        assert_eq!(
            robots_txt_url("https://example.com/page").unwrap(),
            "https://example.com/robots.txt"
        );
        assert_eq!(
            robots_txt_url("https://example.com/some/deep/path/page.html").unwrap(),
            "https://example.com/robots.txt"
        );
        assert_eq!(
            robots_txt_url("https://example.com/page?foo=bar&baz=qux").unwrap(),
            "https://example.com/robots.txt"
        );
        assert_eq!(
            robots_txt_url("https://example.com:8080/page").unwrap(),
            "https://example.com:8080/robots.txt"
        );
        assert_eq!(
            robots_txt_url("https://example.com/page#section").unwrap(),
            "https://example.com/robots.txt"
        );
        assert_eq!(
            robots_txt_url("http://example.com/page").unwrap(),
            "http://example.com/robots.txt"
        );
    }

    #[test]
    fn robots_url_rejects_garbage() {
        assert!(robots_txt_url("not a url").is_err());
    }

    #[test]
    fn extract_simple_html() {
        let html = "<html><body><article><h1>Hello World</h1><p>This is a test paragraph.</p></article></body></html>";
        let result = extract_content_from_html(html);
        assert!(result.contains("Hello World"), "got: {result}");
        assert!(result.contains("test paragraph"), "got: {result}");
    }

    #[test]
    fn extract_html_with_link() {
        let html = "<html><body><article><p>Visit <a href=\"https://example.com\">Example</a> for more.</p></article></body></html>";
        let result = extract_content_from_html(html);
        assert!(result.contains("Example"), "got: {result}");
    }

    #[test]
    fn extract_empty_returns_error() {
        let result = extract_content_from_html("");
        assert!(result.contains("<error>"));
    }

    #[test]
    fn extract_drops_style_and_script_noise() {
        let html = "<html><head><style>body{color:red;background:#eee}</style>\
        <script>console.log('x')</script></head>\
        <body><article><h1>Real Title</h1><p>Real content.</p></article></body></html>";
        let result = extract_content_from_html(html);
        assert!(result.contains("Real Title"), "got: {result}");
        assert!(result.contains("Real content"), "got: {result}");
        assert!(!result.contains("color:red"), "style leaked: {result}");
        assert!(!result.contains("console.log"), "script leaked: {result}");
    }

    #[test]
    fn truncate_basic_slice() {
        // Content fully consumed: no continuation hint.
        assert_eq!(truncate("abc", 0, 3), "abc");
        // Partial consumption with more content: hint added.
        let result = truncate("abcdef", 0, 3);
        assert!(result.starts_with("abc"));
        assert!(result.contains("start_index of 3"));
        let result = truncate("abcdef", 2, 2);
        assert!(result.starts_with("cd"));
        assert!(result.contains("start_index of 4"));
    }

    #[test]
    fn truncate_with_continuation_hint() {
        let result = truncate("abcdefgh", 0, 5);
        assert!(result.starts_with("abcde"));
        assert!(result.contains("Content truncated"));
        assert!(result.contains("start_index of 5"));
    }

    #[test]
    fn truncate_exact_boundary_has_no_hint() {
        let result = truncate("abcdef", 0, 6);
        assert_eq!(result, "abcdef");
    }

    #[test]
    fn truncate_past_end_returns_error() {
        assert_eq!(
            truncate("abcdef", 6, 5),
            "<error>No more content available.</error>"
        );
        assert_eq!(
            truncate("abcdef", 100, 5),
            "<error>No more content available.</error>"
        );
    }

    #[test]
    fn truncate_is_character_indexed() {
        // Multibyte characters: "héllo" is 5 chars but more bytes.
        let result = truncate("héllo wörld", 0, 5);
        assert!(result.starts_with("héllo"));
        assert!(result.contains("start_index of 5"));
        // Re-fetch from the continuation point.
        let result = truncate("héllo wörld", 6, 5);
        assert_eq!(result, "wörld");
    }
}