mcp-server-core 0.3.0

A batteries-included base for Model Context Protocol (MCP) servers over Streamable HTTP: OAuth 2.1 sign-in, TLS, per-service routing, abuse limits and observability on top of rmcp.
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
//! Read-only services and the write audit log, applied once around every service's MCP handler.
//!
//! Tools are classified by their `read_only_hint` annotation: a tool marked
//! `read_only_hint = true` reads; every other tool, including one without annotations, writes.
//! On a read-only service write tools are hidden from `tools/list` and refused if called anyway.
//! Every write tool call, made or refused, is recorded in the audit log: the service, tool,
//! outcome, error kind and caller, never arguments, ids or results.
//!
//! [`Guarded`] wraps one service's handler. Its policy is either given when it is built
//! ([`Guarded::with_policy`], for handlers composed outside a service's own route) or read per
//! request ([`Guarded::new`]): [`Server`](crate::app::Server) attaches a [`ToolPolicy`] to each
//! service's router as a request extension and [`crate::service::mcp_service`] wraps every
//! handler, reading the policy from the HTTP request parts that rmcp puts into each request
//! context. Without a policy the wrapper changes nothing.
//!
//! Tool errors are classified by a `kind: ` prefix on their first text block, such as
//! `not_found: no event with that id`, which the audit log records as `error_kind`.

// The wrapper forwards every handler method, including the legacy (SEP-2577 deprecated) ones.
#![expect(deprecated)]

use crate::caller::Caller;
use crate::credential::Credential;
use crate::jwt::Claims;
use axum::http::request::Parts;
use chrono::{SecondsFormat, Utc};
use rmcp::model::*;
use rmcp::service::{
    MaybeSendFuture, NotificationContext, RequestContext, RoleServer, SubscriptionContext,
};
use rmcp::{ErrorData as McpError, ServerHandler};
use serde::Serialize;
use std::borrow::Cow;
use std::fs::{File, OpenOptions};
use std::io::Write;
#[cfg(unix)]
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
use std::path::PathBuf;
use std::sync::{Arc, Mutex, PoisonError};
use std::time::{Duration, Instant};

/// Tracing target of audit events; the binary keeps it enabled at info whatever `LOG_LEVEL` says.
pub const AUDIT_TARGET: &str = "audit";
const WARN_EVERY: Duration = Duration::from_secs(60);

/// What the server enforces and records for one service's tools on one endpoint.
#[derive(Clone)]
pub struct ToolPolicy {
    /// The service the tools belong to.
    pub service: String,
    /// The path the calls arrive on, such as `/weather` or `/mcp`.
    pub endpoint: String,
    /// Hide and refuse write tools.
    pub read_only: bool,
    /// Where write tool calls are recorded.
    pub audit: Arc<AuditLog>,
}

/// One audit record: never arguments, ids or results.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AuditLine {
    /// When the call finished, RFC 3339 in UTC with milliseconds.
    pub ts: String,
    /// The service the tool belongs to.
    pub service: String,
    /// The path the call arrived on.
    pub endpoint: String,
    /// The tool's own name, without a combined-endpoint prefix.
    pub tool: String,
    /// `ok`, `error` or `refused`.
    pub outcome: &'static str,
    /// For an error, its kind: the tool error's `kind: ` prefix, `protocol`, or `read_only`.
    pub error_kind: Option<String>,
    /// `oauth:<client_id>`, `oauth` (a token without a client id), `credential` (a credential
    /// sent as the bearer token) or `static` (the static token, or a request with no caller at
    /// all, which only happens without the bearer guard).
    pub caller: Option<String>,
    /// Who signed in ([`Caller::subject`]): `owner`, `key:<16 hex digits>`, `static` or a custom
    /// verifier's subject. Never a credential.
    pub subject: Option<String>,
    /// How long the call took.
    pub elapsed_ms: u64,
}

/// Emits audit lines on the `audit` target and, when configured, appends them to a JSON lines file.
pub struct AuditLog {
    file: Option<Mutex<AuditFile>>,
}

struct AuditFile {
    path: PathBuf,
    file: Option<File>,
    last_warn: Option<Instant>,
}

impl AuditLog {
    /// `path`: the `AUDIT_LOG_PATH` file, opened on the first write. On Unix it is created with
    /// mode 0600 and an existing file is narrowed to 0600.
    pub fn new(path: Option<PathBuf>) -> Self {
        Self {
            file: path.map(|path| {
                Mutex::new(AuditFile {
                    path,
                    file: None,
                    last_warn: None,
                })
            }),
        }
    }

    /// Emits `line` on the [`AUDIT_TARGET`] tracing target and appends it to the file, if any. A
    /// failed write logs a warning at most once a minute and never fails the tool call.
    pub fn record(&self, line: &AuditLine) {
        tracing::info!(
            target: AUDIT_TARGET,
            service = %line.service,
            endpoint = %line.endpoint,
            tool = %line.tool,
            outcome = line.outcome,
            error_kind = line.error_kind.as_deref(),
            caller = line.caller.as_deref(),
            subject = line.subject.as_deref(),
            elapsed_ms = line.elapsed_ms,
            "tool write"
        );
        if let Some(file) = &self.file {
            file.lock()
                .unwrap_or_else(PoisonError::into_inner)
                .append(line);
        }
    }
}

impl AuditFile {
    /// A failed write logs a warning at most once a minute and never fails the tool call.
    fn append(&mut self, line: &AuditLine) {
        let mut text = serde_json::to_string(line).expect("audit lines serialize");
        text.push('\n');
        let written = self.open().and_then(|f| f.write_all(text.as_bytes()));
        let Err(e) = written else {
            return;
        };
        self.file = None;
        let now = Instant::now();
        if self
            .last_warn
            .is_some_and(|t| now.duration_since(t) < WARN_EVERY)
        {
            return;
        }
        self.last_warn = Some(now);
        tracing::warn!(path = %self.path.display(), error = %e, "could not write the audit log");
    }

    fn open(&mut self) -> std::io::Result<&mut File> {
        if self.file.is_none() {
            let mut options = OpenOptions::new();
            options.create(true).append(true);
            #[cfg(unix)]
            options.mode(0o600);
            let file = options.open(&self.path)?;
            #[cfg(unix)]
            file.set_permissions(std::fs::Permissions::from_mode(0o600))?;
            self.file = Some(file);
        }
        Ok(self.file.as_mut().expect("opened above"))
    }
}

fn parts(ctx: &RequestContext<RoleServer>) -> Option<&Parts> {
    ctx.extensions.get::<Parts>()
}

fn request_policy(ctx: &RequestContext<RoleServer>) -> Option<ToolPolicy> {
    parts(ctx)?.extensions.get::<ToolPolicy>().cloned()
}

/// The bearer guard stores OAuth claims for an OAuth token and the credential for a credential
/// sent directly; the static token leaves neither.
fn caller(ctx: &RequestContext<RoleServer>) -> Option<String> {
    let parts = parts(ctx)?;
    if let Some(c) = parts.extensions.get::<Caller>() {
        return Some(c.audit_label());
    }
    Some(match parts.extensions.get::<Claims>() {
        Some(claims) => match &claims.payload.client_id {
            Some(id) => format!("oauth:{id}"),
            None => "oauth".into(),
        },
        None if parts.extensions.get::<Credential>().is_some() => "credential".into(),
        None => "static".into(),
    })
}

/// Whether a tool declares `read_only_hint = true`; an unknown tool or one without annotations is
/// treated as a write.
pub fn is_read_only(tool: Option<&Tool>) -> bool {
    tool.and_then(|t| t.annotations.as_ref())
        .and_then(|a| a.read_only_hint)
        == Some(true)
}

/// The kind of a tool error result (`conflict` for `conflict: ...`).
fn error_kind(result: &CallToolResult) -> Option<String> {
    let text = result.content.first()?.as_text()?.text.as_str();
    let (kind, _) = text.split_once(": ")?;
    let valid = !kind.is_empty() && kind.bytes().all(|b| b.is_ascii_lowercase() || b == b'_');
    valid.then(|| kind.to_string())
}

fn outcome(response: &Result<CallToolResponse, McpError>) -> (&'static str, Option<String>) {
    match response {
        Ok(CallToolResponse::Complete(r)) if r.is_error == Some(true) => ("error", error_kind(r)),
        Ok(_) => ("ok", None),
        Err(_) => ("error", Some("protocol".into())),
    }
}

/// The tools a client may see: all of them, or only the read-only ones on a read-only service.
pub fn visible_tools(mut tools: Vec<Tool>, read_only: bool) -> Vec<Tool> {
    if read_only {
        tools.retain(|t| is_read_only(Some(t)));
    }
    tools
}

/// A service handler with the read-only rule and the audit log applied.
pub struct Guarded<S> {
    inner: S,
    policy: Option<ToolPolicy>,
}

impl<S> Guarded<S> {
    /// Reads the policy from each request's extensions (set on the service's router).
    pub fn new(inner: S) -> Self {
        Self {
            inner,
            policy: None,
        }
    }

    /// Uses `policy` for every request.
    pub fn with_policy(inner: S, policy: ToolPolicy) -> Self {
        Self {
            inner,
            policy: Some(policy),
        }
    }

    fn policy(&self, ctx: &RequestContext<RoleServer>) -> Option<ToolPolicy> {
        self.policy.clone().or_else(|| request_policy(ctx))
    }
}

impl<S: ServerHandler> ServerHandler for Guarded<S> {
    fn ping(
        &self,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
        self.inner.ping(context)
    }

    fn initialize(
        &self,
        request: InitializeRequestParams,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<InitializeResult, McpError>> + MaybeSendFuture + '_ {
        self.inner.initialize(request, context)
    }

    fn negotiate_initialize(
        &self,
        request: &InitializeRequestParams,
    ) -> Result<InitializeResult, McpError> {
        self.inner.negotiate_initialize(request)
    }

    fn supported_protocol_versions(&self) -> Cow<'static, [ProtocolVersion]> {
        self.inner.supported_protocol_versions()
    }

    fn discover(
        &self,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<DiscoverResult, McpError>> + MaybeSendFuture + '_ {
        self.inner.discover(context)
    }

    fn complete(
        &self,
        request: CompleteRequestParams,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<CompleteResult, McpError>> + MaybeSendFuture + '_ {
        self.inner.complete(request, context)
    }

    fn set_level(
        &self,
        request: SetLevelRequestParams,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
        self.inner.set_level(request, context)
    }

    fn get_prompt(
        &self,
        request: GetPromptRequestParams,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<GetPromptResponse, McpError>> + MaybeSendFuture + '_ {
        self.inner.get_prompt(request, context)
    }

    fn list_prompts(
        &self,
        request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<ListPromptsResult, McpError>> + MaybeSendFuture + '_ {
        self.inner.list_prompts(request, context)
    }

    fn list_resources(
        &self,
        request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<ListResourcesResult, McpError>> + MaybeSendFuture + '_ {
        self.inner.list_resources(request, context)
    }

    fn list_resource_templates(
        &self,
        request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<ListResourceTemplatesResult, McpError>> + MaybeSendFuture + '_
    {
        self.inner.list_resource_templates(request, context)
    }

    fn read_resource(
        &self,
        request: ReadResourceRequestParams,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<ReadResourceResponse, McpError>> + MaybeSendFuture + '_ {
        self.inner.read_resource(request, context)
    }

    fn accepted_subscription_filter(
        &self,
        requested: &SubscriptionFilter,
    ) -> Option<SubscriptionFilter> {
        self.inner.accepted_subscription_filter(requested)
    }

    fn listen(
        &self,
        context: SubscriptionContext,
    ) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
        self.inner.listen(context)
    }

    fn subscribe(
        &self,
        request: SubscribeRequestParams,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
        self.inner.subscribe(request, context)
    }

    fn unsubscribe(
        &self,
        request: UnsubscribeRequestParams,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
        self.inner.unsubscribe(request, context)
    }

    /// Read-only tools pass through. Other tools are refused on a read-only service and are
    /// audited either way.
    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResponse, McpError> {
        let Some(policy) = self.policy(&context) else {
            return self.inner.call_tool(request, context).await;
        };
        if is_read_only(self.inner.get_tool(&request.name).as_ref()) {
            return self.inner.call_tool(request, context).await;
        }
        let tool = request.name.to_string();
        let caller = caller(&context);
        let subject = parts(&context)
            .and_then(|p| p.extensions.get::<Caller>())
            .map(|c| c.subject.clone());
        let started = Instant::now();
        let (outcome, error_kind, response) = if policy.read_only {
            let text = format!(
                "read_only: the {} service is read-only on this server",
                policy.service
            );
            let refused = CallToolResult::error(vec![ContentBlock::text(text)]);
            ("refused", Some("read_only".to_string()), Ok(refused.into()))
        } else {
            let response = self.inner.call_tool(request, context).await;
            let (outcome, kind) = outcome(&response);
            (outcome, kind, response)
        };
        policy.audit.record(&AuditLine {
            ts: Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true),
            service: policy.service.clone(),
            endpoint: policy.endpoint.clone(),
            tool,
            outcome,
            error_kind,
            caller,
            subject,
            elapsed_ms: started.elapsed().as_millis() as u64,
        });
        response
    }

    /// A read-only service lists only the tools annotated `read_only_hint = true`.
    async fn list_tools(
        &self,
        request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, McpError> {
        let read_only = self.policy(&context).is_some_and(|p| p.read_only);
        let mut result = self.inner.list_tools(request, context).await?;
        result.tools = visible_tools(result.tools, read_only);
        Ok(result)
    }

    fn get_tool(&self, name: &str) -> Option<Tool> {
        self.inner.get_tool(name)
    }

    fn on_custom_request(
        &self,
        request: CustomRequest,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<CustomResult, McpError>> + MaybeSendFuture + '_ {
        self.inner.on_custom_request(request, context)
    }

    fn on_cancelled(
        &self,
        notification: CancelledNotificationParam,
        context: NotificationContext<RoleServer>,
    ) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
        self.inner.on_cancelled(notification, context)
    }

    fn on_progress(
        &self,
        notification: ProgressNotificationParam,
        context: NotificationContext<RoleServer>,
    ) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
        self.inner.on_progress(notification, context)
    }

    fn on_initialized(
        &self,
        context: NotificationContext<RoleServer>,
    ) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
        self.inner.on_initialized(context)
    }

    fn on_roots_list_changed(
        &self,
        context: NotificationContext<RoleServer>,
    ) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
        self.inner.on_roots_list_changed(context)
    }

    fn on_custom_notification(
        &self,
        notification: CustomNotification,
        context: NotificationContext<RoleServer>,
    ) -> impl Future<Output = ()> + MaybeSendFuture + '_ {
        self.inner.on_custom_notification(notification, context)
    }

    fn get_info(&self) -> ServerConfig {
        self.inner.get_info()
    }

    fn get_task(
        &self,
        request: GetTaskParams,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<GetTaskResult, McpError>> + MaybeSendFuture + '_ {
        self.inner.get_task(request, context)
    }

    fn update_task(
        &self,
        request: UpdateTaskParams,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
        self.inner.update_task(request, context)
    }

    fn cancel_task(
        &self,
        request: CancelTaskParams,
        context: RequestContext<RoleServer>,
    ) -> impl Future<Output = Result<(), McpError>> + MaybeSendFuture + '_ {
        self.inner.cancel_task(request, context)
    }
}

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

    fn line() -> AuditLine {
        AuditLine {
            ts: "2026-10-18T10:00:00.000Z".into(),
            service: "svc".into(),
            endpoint: "/svc".into(),
            tool: "change".into(),
            outcome: "ok",
            error_kind: None,
            caller: Some("static".into()),
            subject: Some("static".into()),
            elapsed_ms: 1,
        }
    }

    #[test]
    fn write_failures_warn_at_most_once_a_minute() {
        let dir = tempfile::tempdir().unwrap();
        let mut file = AuditFile {
            path: dir.path().join("missing").join("audit.jsonl"),
            file: None,
            last_warn: None,
        };
        file.append(&line());
        let first = file.last_warn.expect("the first failure warns");
        file.append(&line());
        assert_eq!(
            file.last_warn,
            Some(first),
            "a second failure within a minute is quiet"
        );
        file.last_warn = Some(first - WARN_EVERY);
        file.append(&line());
        assert!(
            file.last_warn.unwrap() > first,
            "warns again after a minute"
        );
    }

    #[cfg(unix)]
    #[test]
    fn an_existing_audit_file_is_narrowed_to_0600() {
        use std::fs::Permissions;
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("audit.jsonl");
        std::fs::write(&path, "").unwrap();
        std::fs::set_permissions(&path, Permissions::from_mode(0o644)).unwrap();
        let mut file = AuditFile {
            path: path.clone(),
            file: None,
            last_warn: None,
        };
        file.append(&line());
        let mode = std::fs::metadata(&path).unwrap().permissions().mode();
        assert_eq!(mode & 0o777, 0o600);
    }

    #[test]
    fn a_failed_write_reopens_the_file_next_time() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("later").join("audit.jsonl");
        let mut file = AuditFile {
            path: path.clone(),
            file: None,
            last_warn: None,
        };
        file.append(&line());
        assert!(file.file.is_none());
        std::fs::create_dir(dir.path().join("later")).unwrap();
        file.append(&line());
        assert_eq!(std::fs::read_to_string(&path).unwrap().lines().count(), 1);
    }

    #[test]
    fn error_kinds_are_read_from_the_tool_error_text() {
        let r = |t: &str| CallToolResult::error(vec![ContentBlock::text(t)]);
        assert_eq!(
            error_kind(&r("not_found: no such id")).as_deref(),
            Some("not_found")
        );
        assert_eq!(error_kind(&r("Something: else")), None);
        assert_eq!(error_kind(&r("no colon")), None);
    }

    #[test]
    fn visible_tools_keeps_only_read_only_tools_when_read_only() {
        let tool = |name: &'static str, ro: Option<bool>| {
            let mut t = Tool::new(name, "d", Arc::new(serde_json::Map::new()));
            t.annotations = ro.map(|r| ToolAnnotations::new().read_only(r));
            t
        };
        let tools = vec![
            tool("look", Some(true)),
            tool("change", Some(false)),
            tool("plain", None),
        ];
        let names = |v: Vec<Tool>| {
            v.into_iter()
                .map(|t| t.name.to_string())
                .collect::<Vec<_>>()
        };
        assert_eq!(
            names(visible_tools(tools.clone(), false)),
            ["look", "change", "plain"]
        );
        assert_eq!(names(visible_tools(tools, true)), ["look"]);
    }
}