File size: 2,843 Bytes
f52d137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Functions which rely on gradio components io and change the interface"""

import json

from jsonschema import validate, ValidationError


def validate_mcp_file(mcp_file_content: str, token: str) -> str:
    """Validates the user uploaded MCP file

    The JSON file should follow this structure (we only allow SSE type for the demo,
    but if you run locally you can add stdio to your json file):

    ```json
    {
        "mcpServers": {
            "app-name-1": {
                "type": "sse",
                "url": "https://api.example.com/mcp",
                "headers": {
                    "Authorization": "Bearer your-token-here"
                }
            }
        }
    }
    ```
    """
    # Define the JSON schema
    mcp_schema = {
        "$schema": "https://json-schema.org/draft/2020-12/json-schema-core.html",
        "type": "object",
        "properties": {
            "mcpServers": {
                "type": "object",
                "patternProperties": {
                    "^[\w-]+$": {
                        "type": "object",
                        "properties": {
                            "type": {"type": "string", "enum": ["sse"]},
                            "url": {"type": "string", "format": "uri"},
                            "headers": {
                                "type": "object",
                                "patternProperties": {"^[\w-]+$": {"type": "string"}},
                                "additionalProperties": False,
                            },
                        },
                        "required": ["type", "url"],
                        "additionalProperties": False,
                    }
                },
                "additionalProperties": False,
                "minProperties": 1,
            }
        },
        "required": ["mcpServers"],
        "additionalProperties": False,
    }

    try:
        mcp_data = json.loads(mcp_file_content)
        validate(instance=mcp_data, schema=mcp_schema)

        for server in mcp_data.get("mcpServers", {}).values():
            try:
                if "HF_TOKEN" in server["headers"]["Authorization"]:
                    server["headers"]["Authorization"] = server["headers"][
                        "Authorization"
                    ].replace("HF_TOKEN", token or "")
            except KeyError:
                continue

        return mcp_data

    except json.JSONDecodeError as e:
        raise ValueError(f"Invalid JSON format: {str(e)}")
    except ValidationError as e:
        # Provide more user-friendly error messages
        error_path = " -> ".join(str(p) for p in e.path) if e.path else "root"
        raise ValueError(f"Invalid MCP file structure at {error_path}: {e.message}")
    except Exception as e:
        raise ValueError(f"Error validating MCP file: {str(e)}")