rustapi_mcp/config.rs
1//! Configuration for the MCP server / integration.
2
3use std::collections::HashSet;
4
5/// Configuration for the native MCP server.
6///
7/// This is the primary way users control what gets exposed as tools,
8/// authentication for MCP clients, transport behavior, etc.
9#[derive(Debug, Clone)]
10pub struct McpConfig {
11 /// Human-friendly name of this MCP server (shown to agents).
12 pub name: String,
13 /// Version string.
14 pub version: String,
15 /// Optional description.
16 pub description: Option<String>,
17
18 /// Whether tool discovery and calling is enabled.
19 pub tools_enabled: bool,
20
21 /// Explicitly allowed tags. Only routes that have at least one of these tags
22 /// (via OpenAPI `tags` or future route metadata) will be exposed as tools.
23 ///
24 /// Empty set + no other allow rules = nothing is exposed (safe default).
25 pub allowed_tags: HashSet<String>,
26
27 /// Explicit path prefixes that are allowed to become tools.
28 /// Example: `["/api/public", "/agent"]`
29 pub allowed_path_prefixes: Vec<String>,
30
31 /// Admin / MCP client token.
32 ///
33 /// When set, MCP clients must present this (via header or query param,
34 /// transport dependent) to use discovery or invocation.
35 pub admin_token: Option<String>,
36
37 /// Whether to include detailed error information in tool responses.
38 /// In production you usually want this `false` (similar to RUSTAPI_ENV=production).
39 pub expose_detailed_errors: bool,
40
41 /// Maximum number of tools to advertise in one `tools/list` response.
42 /// Helps protect against very large route sets.
43 pub max_tools: usize,
44
45 /// How `tools/call` should be executed.
46 /// Proxy (default) always goes over HTTP (correct and works for external targets).
47 /// InProcess / Auto are for when an in-process RustApi instance is available.
48 pub invocation_mode: InvocationMode,
49}
50
51/// Controls whether tool invocation goes through the normal HTTP path or
52/// a direct in-memory call (when available).
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
54pub enum InvocationMode {
55 /// Always proxy via the configured `http_base` (safest, works everywhere).
56 #[default]
57 Proxy,
58 /// Use direct in-process invocation when a RustApi runtime is attached.
59 InProcess,
60 /// Choose automatically (InProcess if runtime available, else Proxy).
61 Auto,
62}
63
64impl Default for McpConfig {
65 fn default() -> Self {
66 Self {
67 name: "rustapi-mcp".to_string(),
68 version: "0.0.0".to_string(),
69 description: None,
70 tools_enabled: true,
71 allowed_tags: HashSet::new(),
72 allowed_path_prefixes: vec![],
73 admin_token: None,
74 expose_detailed_errors: false,
75 max_tools: 256,
76 invocation_mode: InvocationMode::Proxy,
77 }
78 }
79}
80
81impl McpConfig {
82 /// Create a new config with reasonable defaults.
83 pub fn new() -> Self {
84 Self::default()
85 }
86
87 /// Set the name advertised to MCP clients.
88 pub fn name(mut self, name: impl Into<String>) -> Self {
89 self.name = name.into();
90 self
91 }
92
93 /// Set the version advertised to MCP clients.
94 pub fn version(mut self, version: impl Into<String>) -> Self {
95 self.version = version.into();
96 self
97 }
98
99 /// Set a human description.
100 pub fn description(mut self, desc: impl Into<String>) -> Self {
101 self.description = Some(desc.into());
102 self
103 }
104
105 /// Enable or disable the tools capability entirely.
106 pub fn enable_tools(mut self, enabled: bool) -> Self {
107 self.tools_enabled = enabled;
108 self
109 }
110
111 /// Allow tools only for routes that carry at least one of the given tags.
112 ///
113 /// This is the recommended way to safely expose a curated surface to agents.
114 pub fn allowed_tags<I, S>(mut self, tags: I) -> Self
115 where
116 I: IntoIterator<Item = S>,
117 S: Into<String>,
118 {
119 self.allowed_tags = tags.into_iter().map(Into::into).collect();
120 self
121 }
122
123 /// Add a path prefix that is allowed to be exposed as tools.
124 pub fn allow_path_prefix(mut self, prefix: impl Into<String>) -> Self {
125 self.allowed_path_prefixes.push(prefix.into());
126 self
127 }
128
129 /// Require this token for MCP clients (discovery + calls).
130 pub fn admin_token(mut self, token: impl Into<String>) -> Self {
131 self.admin_token = Some(token.into());
132 self
133 }
134
135 /// Control whether tool responses include full internal error details.
136 pub fn expose_detailed_errors(mut self, expose: bool) -> Self {
137 self.expose_detailed_errors = expose;
138 self
139 }
140
141 /// Set the maximum number of tools to list.
142 pub fn max_tools(mut self, max: usize) -> Self {
143 self.max_tools = max;
144 self
145 }
146
147 /// Choose invocation strategy for tool calls.
148 pub fn invocation_mode(mut self, mode: InvocationMode) -> Self {
149 self.invocation_mode = mode;
150 self
151 }
152}