|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import os |
5 | 6 | from dataclasses import dataclass |
6 | 7 | from functools import lru_cache |
7 | 8 | from pathlib import Path |
8 | 9 | from typing import Any, Dict, List |
9 | 10 |
|
10 | 11 | import mcp.types as types |
11 | 12 | from mcp.server.fastmcp import FastMCP |
| 13 | +from mcp.server.transport_security import TransportSecuritySettings |
12 | 14 | from pydantic import BaseModel, ConfigDict, Field, ValidationError |
13 | 15 |
|
14 | 16 | MIME_TYPE = "text/html+skybridge" |
@@ -105,9 +107,28 @@ class SolarInput(BaseModel): |
105 | 107 | model_config = ConfigDict(populate_by_name=True, extra="forbid") |
106 | 108 |
|
107 | 109 |
|
| 110 | +def _split_env_list(value: str | None) -> List[str]: |
| 111 | + if not value: |
| 112 | + return [] |
| 113 | + return [item.strip() for item in value.split(",") if item.strip()] |
| 114 | + |
| 115 | + |
| 116 | +def _transport_security_settings() -> TransportSecuritySettings: |
| 117 | + allowed_hosts = _split_env_list(os.getenv("MCP_ALLOWED_HOSTS")) |
| 118 | + allowed_origins = _split_env_list(os.getenv("MCP_ALLOWED_ORIGINS")) |
| 119 | + if not allowed_hosts and not allowed_origins: |
| 120 | + return TransportSecuritySettings(enable_dns_rebinding_protection=False) |
| 121 | + return TransportSecuritySettings( |
| 122 | + enable_dns_rebinding_protection=True, |
| 123 | + allowed_hosts=allowed_hosts, |
| 124 | + allowed_origins=allowed_origins, |
| 125 | + ) |
| 126 | + |
| 127 | + |
108 | 128 | mcp = FastMCP( |
109 | 129 | name="solar-system-python", |
110 | 130 | stateless_http=True, |
| 131 | + transport_security=_transport_security_settings(), |
111 | 132 | ) |
112 | 133 |
|
113 | 134 | TOOL_INPUT_SCHEMA: Dict[str, Any] = SolarInput.model_json_schema() |
|
0 commit comments