File size: 2,311 Bytes
4efde5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import List, Dict, Any
from urllib.parse import unquote

from .credential_service import MCPRequirement, MCPCredential


def validate_config_not_empty(config: Dict[str, Any]) -> Dict[str, Any]:
    if not config:
        raise ValueError('Config cannot be empty')
    return config


def validate_credential_mappings(
    mappings: Dict[str, str], 
    requirements: List[MCPRequirement]
) -> List[str]:
    missing_mappings = []
    
    for req in requirements:
        if req.qualified_name not in mappings:
            missing_mappings.append(req.qualified_name)
    
    return missing_mappings


def get_missing_credentials_advanced(
    user_credentials: List[MCPCredential], 
    requirements: List[MCPRequirement]
) -> List[MCPRequirement]:
    user_mcp_names = {cred.mcp_qualified_name for cred in user_credentials}
    
    missing = []
    for req in requirements:
        if req.custom_type:
            custom_pattern = f"custom_{req.custom_type}_"
            found = any(
                cred_name.startswith(custom_pattern) and 
                req.display_name.lower().replace(' ', '_') in cred_name
                for cred_name in user_mcp_names
            )
            if not found:
                missing.append(req)
        else:
            if req.qualified_name not in user_mcp_names:
                missing.append(req)
    
    return missing


def decode_mcp_qualified_name(encoded_name: str) -> str:
    return unquote(encoded_name)


def encode_mcp_qualified_name(qualified_name: str) -> str:
    from urllib.parse import quote
    return quote(qualified_name, safe='')


def extract_config_keys(config: Dict[str, Any]) -> List[str]:
    return list(config.keys()) if config else []


def sanitize_display_name(display_name: str) -> str:
    return display_name.lower().replace(' ', '_').replace('-', '_')


def build_custom_qualified_name(custom_type: str, display_name: str) -> str:
    sanitized_name = sanitize_display_name(display_name)
    return f"custom_{custom_type}_{sanitized_name}"


def matches_custom_pattern(qualified_name: str, pattern: str, display_name: str) -> bool:
    if not qualified_name.startswith(pattern):
        return False
    
    sanitized_display = sanitize_display_name(display_name)
    return sanitized_display in qualified_name