Skip to content

Commit 46bc0f3

Browse files
committed
chore: raise an error instead of starting the server and print a warning log
Signed-off-by: Rai Siqueira <rai93siqueira@gmail.com>
1 parent b8876f8 commit 46bc0f3

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ django-ai-boost --settings myproject.settings --transport sse --auth-token "your
139139
- **Transport Support**:
140140
-**SSE Transport**: Full authentication support (HTTP-based)
141141
-**Stdio Transport**: No authentication (local-only, trusted environments)
142+
- **Error on Mismatch**: If you provide `--auth-token` with `--transport stdio`, the server will exit with an error to prevent false security assumptions
142143

143144
#### Production Deployment
144145

@@ -206,7 +207,9 @@ curl -H "Authorization: Bearer your-secret-token" http://127.0.0.1:8000/sse
206207
- Set `DJANGO_MCP_AUTH_TOKEN` environment variable or use `--auth-token`
207208

208209
**"Authentication token provided but transport is 'stdio'"**
209-
- Authentication only works with `--transport sse`. Use SSE transport for authenticated access.
210+
- **This is now an error** that stops the server from starting
211+
- Authentication only works with `--transport sse`
212+
- Either use `--transport sse` with your token, or remove the `--auth-token` argument for stdio
210213

211214
**"Running in production mode with stdio transport"**
212215
- This is OK for local/trusted environments, but stdio has no authentication capability

src/django_ai_boost/server_fastmcp.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,16 @@ def validate_and_create_auth(token: str | None, is_production: bool, transport:
104104
)
105105
return None
106106

107-
# Token with stdio - warn but allow
107+
# Token with non-SSE transport - error
108108
if transport != "sse":
109-
logger.warning(
110-
"Authentication token provided but transport is '%s'. "
111-
"Authentication only works with SSE transport (--transport sse). "
112-
"Token will be ignored.",
113-
transport,
109+
raise ValueError(
110+
f"Authentication token provided but transport is '{transport}'.\n"
111+
"Bearer tokens only work with SSE transport.\n"
112+
"\n"
113+
"Choose one:\n"
114+
" 1. Use SSE with authentication: --transport sse --auth-token <token>\n"
115+
f" 2. Use {transport} without authentication: --transport {transport} (remove --auth-token)\n"
114116
)
115-
return None
116117

117118
# Valid: token + SSE
118119
logger.info("Authentication enabled with bearer token for SSE transport")
@@ -822,8 +823,8 @@ def run_server(
822823
Args:
823824
settings_module: Django settings module path
824825
transport: Transport type (stdio or sse)
825-
host: Host to bind to for SSE transport
826-
port: Port to bind to for SSE transport
826+
host: Host to bind to for SSE transport (default: 127.0.0.1)
827+
port: Port to bind to for SSE transport (default: 8000)
827828
auth_token: Bearer token for authentication (optional)
828829
"""
829830
# Initialize Django before starting the server

test_auth_logic.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,28 @@ def test_validation_logic():
8989
print(f" Prod+SSE+token: {type(result).__name__ if result else None}")
9090
assert result is not None, "Should create auth provider"
9191

92-
# Prod mode, token, stdio - Warning but OK
93-
result = validate_and_create_auth("token", True, "stdio")
94-
print(f" Prod+stdio+token: {result} (should warn)")
95-
assert result is None, "Should return None (auth doesn't work with stdio)"
92+
# Dev mode, token, SSE - OK (creates provider)
93+
result = validate_and_create_auth("token", False, "sse")
94+
print(f" Dev+SSE+token: {type(result).__name__ if result else None}")
95+
assert result is not None, "Should create auth provider in dev mode too"
96+
97+
# Prod mode, token, stdio - ERROR
98+
try:
99+
result = validate_and_create_auth("token", True, "stdio")
100+
print(f" Prod+stdio+token: Should have raised error!")
101+
assert False, "Should have raised ValueError"
102+
except ValueError as e:
103+
print(f" Prod+stdio+token: Raised ValueError ✓")
104+
assert "Bearer tokens only work with SSE transport" in str(e), "Error message should mention SSE requirement"
105+
106+
# Dev mode, token, stdio - ERROR (same as prod)
107+
try:
108+
result = validate_and_create_auth("token", False, "stdio")
109+
print(f" Dev+stdio+token: Should have raised error!")
110+
assert False, "Should have raised ValueError"
111+
except ValueError as e:
112+
print(f" Dev+stdio+token: Raised ValueError ✓")
113+
assert "Bearer tokens only work with SSE transport" in str(e), "Error message should mention SSE requirement"
96114

97115
# Prod mode, no token, SSE - ERROR
98116
try:

0 commit comments

Comments
 (0)