litellm-rs 0.4.16

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
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
//! MCP Permission Control
//!
//! Access control for MCP servers and tools based on API keys, teams, and organizations.

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

use super::error::{McpError, McpResult};

/// Permission level for MCP access
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum PermissionLevel {
    /// No access
    Deny,
    /// Read-only access (list tools, view info)
    Read,
    /// Execute access (can call tools)
    #[default]
    Execute,
    /// Full access (including admin operations)
    Admin,
}

impl PermissionLevel {
    /// Check if this level allows reading
    pub fn can_read(&self) -> bool {
        !matches!(self, PermissionLevel::Deny)
    }

    /// Check if this level allows execution
    pub fn can_execute(&self) -> bool {
        matches!(self, PermissionLevel::Execute | PermissionLevel::Admin)
    }

    /// Check if this level allows admin operations
    pub fn is_admin(&self) -> bool {
        matches!(self, PermissionLevel::Admin)
    }
}

/// Permission rule for a specific server or tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionRule {
    /// Server name pattern (supports wildcards: * for any)
    pub server_pattern: String,

    /// Tool name pattern (supports wildcards, None means all tools)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_pattern: Option<String>,

    /// Permission level
    pub level: PermissionLevel,
}

impl PermissionRule {
    /// Create a new permission rule
    pub fn new(server: impl Into<String>, level: PermissionLevel) -> Self {
        Self {
            server_pattern: server.into(),
            tool_pattern: None,
            level,
        }
    }

    /// Add tool pattern
    pub fn for_tool(mut self, tool: impl Into<String>) -> Self {
        self.tool_pattern = Some(tool.into());
        self
    }

    /// Check if this rule matches a server/tool combination
    pub fn matches(&self, server: &str, tool: Option<&str>) -> bool {
        // Check server pattern
        if !pattern_matches(&self.server_pattern, server) {
            return false;
        }

        // Check tool pattern if specified
        match (&self.tool_pattern, tool) {
            (Some(pattern), Some(tool_name)) => pattern_matches(pattern, tool_name),
            (Some(_), None) => false, // Rule requires tool but none provided
            (None, _) => true,        // Rule applies to all tools
        }
    }
}

/// Check if a pattern matches a value (supports * wildcard)
fn pattern_matches(pattern: &str, value: &str) -> bool {
    if pattern == "*" {
        return true;
    }

    if pattern.contains('*') {
        // Simple wildcard matching
        let parts: Vec<&str> = pattern.split('*').collect();
        if parts.len() == 2 {
            let (prefix, suffix) = (parts[0], parts[1]);
            return value.starts_with(prefix) && value.ends_with(suffix);
        }
    }

    pattern == value
}

/// Permission policy for an API key, team, or organization
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PermissionPolicy {
    /// Policy name
    pub name: String,

    /// Description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Default permission level for unspecified servers
    #[serde(default)]
    pub default_level: PermissionLevel,

    /// Specific permission rules (evaluated in order)
    #[serde(default)]
    pub rules: Vec<PermissionRule>,

    /// Allowed servers (whitelist, if empty all are allowed)
    #[serde(default, skip_serializing_if = "HashSet::is_empty")]
    pub allowed_servers: HashSet<String>,

    /// Denied servers (blacklist)
    #[serde(default, skip_serializing_if = "HashSet::is_empty")]
    pub denied_servers: HashSet<String>,

    /// Rate limit override (requests per minute)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rate_limit_rpm: Option<u32>,
}

impl PermissionPolicy {
    /// Create a new policy with a name
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ..Default::default()
        }
    }

    /// Create an allow-all policy
    pub fn allow_all(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            default_level: PermissionLevel::Execute,
            ..Default::default()
        }
    }

    /// Create a deny-all policy
    pub fn deny_all(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            default_level: PermissionLevel::Deny,
            ..Default::default()
        }
    }

    /// Add a permission rule
    pub fn with_rule(mut self, rule: PermissionRule) -> Self {
        self.rules.push(rule);
        self
    }

    /// Allow a specific server
    pub fn allow_server(mut self, server: impl Into<String>) -> Self {
        self.allowed_servers.insert(server.into());
        self
    }

    /// Deny a specific server
    pub fn deny_server(mut self, server: impl Into<String>) -> Self {
        self.denied_servers.insert(server.into());
        self
    }

    /// Check permission for a server access
    pub fn check_server_access(&self, server: &str) -> PermissionLevel {
        // Check deny list first
        if self.denied_servers.contains(server) {
            return PermissionLevel::Deny;
        }

        // Check allow list if not empty
        if !self.allowed_servers.is_empty() && !self.allowed_servers.contains(server) {
            return PermissionLevel::Deny;
        }

        // Check rules in order
        for rule in &self.rules {
            if rule.matches(server, None) {
                return rule.level;
            }
        }

        // Return default level
        self.default_level
    }

    /// Check permission for a tool access
    pub fn check_tool_access(&self, server: &str, tool: &str) -> PermissionLevel {
        // First check server-level access
        let server_level = self.check_server_access(server);
        if server_level == PermissionLevel::Deny {
            return PermissionLevel::Deny;
        }

        // Check tool-specific rules
        for rule in &self.rules {
            if rule.matches(server, Some(tool)) {
                return rule.level;
            }
        }

        // Fall back to server-level permission
        server_level
    }
}

/// Permission manager for MCP access control
#[derive(Debug, Default)]
pub struct PermissionManager {
    /// Policies by API key
    key_policies: HashMap<String, PermissionPolicy>,

    /// Policies by team ID
    team_policies: HashMap<String, PermissionPolicy>,

    /// Policies by organization ID
    org_policies: HashMap<String, PermissionPolicy>,

    /// Default policy for unauthenticated requests
    default_policy: PermissionPolicy,
}

impl PermissionManager {
    /// Create a new permission manager
    pub fn new() -> Self {
        Self {
            default_policy: PermissionPolicy::deny_all("default"),
            ..Default::default()
        }
    }

    /// Create a permission manager that allows all by default
    pub fn allow_all() -> Self {
        Self {
            default_policy: PermissionPolicy::allow_all("default"),
            ..Default::default()
        }
    }

    /// Set policy for an API key
    pub fn set_key_policy(&mut self, key: impl Into<String>, policy: PermissionPolicy) {
        self.key_policies.insert(key.into(), policy);
    }

    /// Set policy for a team
    pub fn set_team_policy(&mut self, team_id: impl Into<String>, policy: PermissionPolicy) {
        self.team_policies.insert(team_id.into(), policy);
    }

    /// Set policy for an organization
    pub fn set_org_policy(&mut self, org_id: impl Into<String>, policy: PermissionPolicy) {
        self.org_policies.insert(org_id.into(), policy);
    }

    /// Set default policy
    pub fn set_default_policy(&mut self, policy: PermissionPolicy) {
        self.default_policy = policy;
    }

    /// Get the effective policy for a request
    pub fn get_effective_policy(
        &self,
        api_key: Option<&str>,
        team_id: Option<&str>,
        org_id: Option<&str>,
    ) -> &PermissionPolicy {
        // Priority: API key > Team > Organization > Default
        if let Some(key) = api_key
            && let Some(policy) = self.key_policies.get(key)
        {
            return policy;
        }

        if let Some(team) = team_id
            && let Some(policy) = self.team_policies.get(team)
        {
            return policy;
        }

        if let Some(org) = org_id
            && let Some(policy) = self.org_policies.get(org)
        {
            return policy;
        }

        &self.default_policy
    }

    /// Check if access to a server is allowed
    pub fn check_server_access(
        &self,
        server: &str,
        api_key: Option<&str>,
        team_id: Option<&str>,
        org_id: Option<&str>,
    ) -> McpResult<PermissionLevel> {
        let policy = self.get_effective_policy(api_key, team_id, org_id);
        let level = policy.check_server_access(server);

        if level == PermissionLevel::Deny {
            return Err(McpError::AuthorizationError {
                server_name: server.to_string(),
                tool_name: None,
                message: "Access denied by permission policy".to_string(),
            });
        }

        Ok(level)
    }

    /// Check if access to a tool is allowed
    pub fn check_tool_access(
        &self,
        server: &str,
        tool: &str,
        api_key: Option<&str>,
        team_id: Option<&str>,
        org_id: Option<&str>,
    ) -> McpResult<PermissionLevel> {
        let policy = self.get_effective_policy(api_key, team_id, org_id);
        let level = policy.check_tool_access(server, tool);

        if level == PermissionLevel::Deny {
            return Err(McpError::AuthorizationError {
                server_name: server.to_string(),
                tool_name: Some(tool.to_string()),
                message: "Access denied by permission policy".to_string(),
            });
        }

        if !level.can_execute() {
            return Err(McpError::AuthorizationError {
                server_name: server.to_string(),
                tool_name: Some(tool.to_string()),
                message: "Execute permission required".to_string(),
            });
        }

        Ok(level)
    }
}

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

    #[test]
    fn test_permission_level_hierarchy() {
        assert!(!PermissionLevel::Deny.can_read());
        assert!(PermissionLevel::Read.can_read());
        assert!(PermissionLevel::Execute.can_read());
        assert!(PermissionLevel::Admin.can_read());

        assert!(!PermissionLevel::Deny.can_execute());
        assert!(!PermissionLevel::Read.can_execute());
        assert!(PermissionLevel::Execute.can_execute());
        assert!(PermissionLevel::Admin.can_execute());

        assert!(!PermissionLevel::Execute.is_admin());
        assert!(PermissionLevel::Admin.is_admin());
    }

    #[test]
    fn test_pattern_matching() {
        assert!(pattern_matches("*", "anything"));
        assert!(pattern_matches("github", "github"));
        assert!(!pattern_matches("github", "gitlab"));

        assert!(pattern_matches("git*", "github"));
        assert!(pattern_matches("git*", "gitlab"));
        assert!(!pattern_matches("git*", "mercurial"));

        assert!(pattern_matches("*_mcp", "github_mcp"));
        assert!(!pattern_matches("*_mcp", "github"));
    }

    #[test]
    fn test_permission_rule_matching() {
        let rule = PermissionRule::new("github", PermissionLevel::Execute);
        assert!(rule.matches("github", None));
        assert!(!rule.matches("gitlab", None));

        let rule_with_tool =
            PermissionRule::new("github", PermissionLevel::Execute).for_tool("get_repo");
        assert!(rule_with_tool.matches("github", Some("get_repo")));
        assert!(!rule_with_tool.matches("github", Some("delete_repo")));
        assert!(!rule_with_tool.matches("github", None));
    }

    #[test]
    fn test_policy_deny_list() {
        let policy = PermissionPolicy::allow_all("test").deny_server("dangerous_server");

        assert_eq!(
            policy.check_server_access("github"),
            PermissionLevel::Execute
        );
        assert_eq!(
            policy.check_server_access("dangerous_server"),
            PermissionLevel::Deny
        );
    }

    #[test]
    fn test_policy_allow_list() {
        let policy = PermissionPolicy::deny_all("test")
            .allow_server("github")
            .with_rule(PermissionRule::new("github", PermissionLevel::Execute));

        assert_eq!(
            policy.check_server_access("github"),
            PermissionLevel::Execute
        );
        assert_eq!(policy.check_server_access("gitlab"), PermissionLevel::Deny);
    }

    #[test]
    fn test_policy_rules_order() {
        let policy = PermissionPolicy::new("test")
            .with_rule(PermissionRule::new("*", PermissionLevel::Read))
            .with_rule(PermissionRule::new("github", PermissionLevel::Execute));

        // First matching rule wins
        assert_eq!(
            policy.check_server_access("github"),
            PermissionLevel::Read // "*" matches first
        );
    }

    #[test]
    fn test_policy_tool_access() {
        // Tool-specific rules should come before general rules
        let policy = PermissionPolicy::new("test")
            .with_rule(PermissionRule::new("github", PermissionLevel::Deny).for_tool("delete_repo"))
            .with_rule(PermissionRule::new("github", PermissionLevel::Execute));

        assert_eq!(
            policy.check_tool_access("github", "get_repo"),
            PermissionLevel::Execute
        );
        assert_eq!(
            policy.check_tool_access("github", "delete_repo"),
            PermissionLevel::Deny
        );
    }

    #[test]
    fn test_permission_manager_key_policy() {
        let mut manager = PermissionManager::new();
        manager.set_key_policy("sk-test123", PermissionPolicy::allow_all("test_key"));

        let level = manager.check_server_access("github", Some("sk-test123"), None, None);
        assert!(level.is_ok());
        assert_eq!(level.unwrap(), PermissionLevel::Execute);
    }

    #[test]
    fn test_permission_manager_default_deny() {
        let manager = PermissionManager::new();

        let result = manager.check_server_access("github", None, None, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_permission_manager_priority() {
        let mut manager = PermissionManager::new();

        // Set org policy (lowest priority)
        manager.set_org_policy(
            "org1",
            PermissionPolicy::new("org").with_rule(PermissionRule::new("*", PermissionLevel::Read)),
        );

        // Set key policy (highest priority)
        manager.set_key_policy(
            "sk-admin",
            PermissionPolicy::new("admin")
                .with_rule(PermissionRule::new("*", PermissionLevel::Admin)),
        );

        // Key policy takes precedence
        let policy = manager.get_effective_policy(Some("sk-admin"), None, Some("org1"));
        assert_eq!(policy.name, "admin");

        // Falls back to org policy
        let policy = manager.get_effective_policy(None, None, Some("org1"));
        assert_eq!(policy.name, "org");
    }

    #[test]
    fn test_check_tool_access_requires_execute() {
        let mut manager = PermissionManager::new();
        manager.set_key_policy(
            "sk-reader",
            PermissionPolicy::new("reader")
                .with_rule(PermissionRule::new("*", PermissionLevel::Read)),
        );

        // Read-only should not allow tool execution
        let result =
            manager.check_tool_access("github", "create_issue", Some("sk-reader"), None, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_permission_policy_serialization() {
        let policy = PermissionPolicy::new("test")
            .with_rule(PermissionRule::new("github", PermissionLevel::Execute));

        let json = serde_json::to_string(&policy).unwrap();
        let deserialized: PermissionPolicy = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.name, "test");
        assert_eq!(deserialized.rules.len(), 1);
    }
}