Skip to content

Commit f349d6f

Browse files
committed
Fix formatting
1 parent c018a82 commit f349d6f

File tree

9 files changed

+118
-207
lines changed

9 files changed

+118
-207
lines changed

src/mcp/server/fastmcp/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class Settings(BaseSettings, Generic[LifespanResultT]):
119119
)
120120

121121
auth: AuthSettings | None = None
122-
122+
123123
# Transport security settings (DNS rebinding protection)
124124
transport_security: TransportSecuritySettings | None = None
125125

src/mcp/server/sse.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,11 @@ class SseServerTransport:
7777
_read_stream_writers: dict[UUID, MemoryObjectSendStream[SessionMessage | Exception]]
7878
_security: TransportSecurityMiddleware
7979

80-
def __init__(
81-
self, endpoint: str, security_settings: TransportSecuritySettings | None = None
82-
) -> None:
80+
def __init__(self, endpoint: str, security_settings: TransportSecuritySettings | None = None) -> None:
8381
"""
8482
Creates a new SSE server transport, which will direct the client to POST
8583
messages to the relative or absolute URL given.
86-
84+
8785
Args:
8886
endpoint: The relative or absolute URL for POST messages.
8987
security_settings: Optional security settings for DNS rebinding protection.
@@ -178,7 +176,7 @@ async def response_wrapper(scope: Scope, receive: Receive, send: Send):
178176
async def handle_post_message(self, scope: Scope, receive: Receive, send: Send) -> None:
179177
logger.debug("Handling POST message")
180178
request = Request(scope, receive)
181-
179+
182180
# Validate request headers for DNS rebinding protection
183181
error_response = await self._security.validate_request(request, is_post=True)
184182
if error_response:

src/mcp/server/streamable_http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ async def _clean_up_memory_streams(self, request_id: RequestId) -> None:
256256
async def handle_request(self, scope: Scope, receive: Receive, send: Send) -> None:
257257
"""Application entry point that handles all HTTP requests"""
258258
request = Request(scope, receive)
259-
259+
260260
# Validate request headers for DNS rebinding protection
261261
is_post = request.method == "POST"
262262
error_response = await self._security.validate_request(request, is_post=is_post)
263263
if error_response:
264264
await error_response(scope, receive, send)
265265
return
266-
266+
267267
if self._terminated:
268268
# If the session has been terminated, return 404 Not Found
269269
response = self._create_error_response(

src/mcp/server/transport_security.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class TransportSecuritySettings(BaseModel):
1313
"""Settings for MCP transport security features.
14-
14+
1515
These settings help protect against DNS rebinding attacks by validating
1616
incoming request headers.
1717
"""
@@ -40,9 +40,7 @@ class TransportSecurityMiddleware:
4040
def __init__(self, settings: TransportSecuritySettings | None = None):
4141
# If not specified, disable DNS rebinding protection by default
4242
# for backwards compatibility
43-
self.settings = settings or TransportSecuritySettings(
44-
enable_dns_rebinding_protection=False
45-
)
43+
self.settings = settings or TransportSecuritySettings(enable_dns_rebinding_protection=False)
4644

4745
def _validate_host(self, host: str | None) -> bool:
4846
"""Validate the Host header against allowed values."""
@@ -101,9 +99,7 @@ def _validate_content_type(self, content_type: str | None) -> bool:
10199

102100
return True
103101

104-
async def validate_request(
105-
self, request: Request, is_post: bool = False
106-
) -> Response | None:
102+
async def validate_request(self, request: Request, is_post: bool = False) -> Response | None:
107103
"""Validate request headers for DNS rebinding protection.
108104
109105
Returns None if validation passes, or an error Response if validation fails.

tests/server/fastmcp/test_integration.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ def stateless_http_server_url(stateless_http_server_port: int) -> str:
8484
def make_fastmcp_app():
8585
"""Create a FastMCP server without auth settings."""
8686
transport_security = TransportSecuritySettings(
87-
allowed_hosts=["127.0.0.1:*", "localhost:*"],
88-
allowed_origins=["http://127.0.0.1:*", "http://localhost:*"]
87+
allowed_hosts=["127.0.0.1:*", "localhost:*"], allowed_origins=["http://127.0.0.1:*", "http://localhost:*"]
8988
)
9089
mcp = FastMCP(name="NoAuthServer", transport_security=transport_security)
9190

@@ -101,10 +100,9 @@ def echo(message: str) -> str:
101100

102101

103102
def make_everything_fastmcp() -> FastMCP:
104-
"""Create a FastMCP server with all features enabled for testing."""
103+
"""Create a FastMCP server with all features enabled for testing."""
105104
transport_security = TransportSecuritySettings(
106-
allowed_hosts=["127.0.0.1:*", "localhost:*"],
107-
allowed_origins=["http://127.0.0.1:*", "http://localhost:*"]
105+
allowed_hosts=["127.0.0.1:*", "localhost:*"], allowed_origins=["http://127.0.0.1:*", "http://localhost:*"]
108106
)
109107
mcp = FastMCP(name="EverythingServer", transport_security=transport_security)
110108

@@ -237,10 +235,9 @@ def make_everything_fastmcp_app():
237235

238236

239237
def make_fastmcp_streamable_http_app():
240-
"""Create a FastMCP server with StreamableHTTP transport."""
238+
"""Create a FastMCP server with StreamableHTTP transport."""
241239
transport_security = TransportSecuritySettings(
242-
allowed_hosts=["127.0.0.1:*", "localhost:*"],
243-
allowed_origins=["http://127.0.0.1:*", "http://localhost:*"]
240+
allowed_hosts=["127.0.0.1:*", "localhost:*"], allowed_origins=["http://127.0.0.1:*", "http://localhost:*"]
244241
)
245242
mcp = FastMCP(name="NoAuthServer", transport_security=transport_security)
246243

@@ -266,16 +263,11 @@ def make_everything_fastmcp_streamable_http_app():
266263

267264

268265
def make_fastmcp_stateless_http_app():
269-
"""Create a FastMCP server with stateless StreamableHTTP transport."""
266+
"""Create a FastMCP server with stateless StreamableHTTP transport."""
270267
transport_security = TransportSecuritySettings(
271-
allowed_hosts=["127.0.0.1:*", "localhost:*"],
272-
allowed_origins=["http://127.0.0.1:*", "http://localhost:*"]
273-
)
274-
mcp = FastMCP(
275-
name="StatelessServer",
276-
stateless_http=True,
277-
transport_security=transport_security
268+
allowed_hosts=["127.0.0.1:*", "localhost:*"], allowed_origins=["http://127.0.0.1:*", "http://localhost:*"]
278269
)
270+
mcp = FastMCP(name="StatelessServer", stateless_http=True, transport_security=transport_security)
279271

280272
# Add a simple tool
281273
@mcp.tool(description="A simple echo tool")

0 commit comments

Comments
 (0)