Skip to content

Commit 3da0850

Browse files
committed
added authentication and stateful session support
1 parent 1b52f96 commit 3da0850

File tree

4 files changed

+352
-78
lines changed

4 files changed

+352
-78
lines changed

.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ WEBSOCKET_PING_INTERVAL=30
140140
# SSE client retry timeout (milliseconds)
141141
SSE_RETRY_TIMEOUT=5000
142142

143+
144+
#####################################
145+
# Streamabe HTTP Transport Configuration
146+
#####################################
147+
148+
# Set False to use stateless sessions without event store and True for stateful sessions
149+
USE_STATEFUL_SESSIONS=false
150+
151+
# Set true for JSON responses, false for SSE streams
152+
JSON_RESPONSE_ENABLED=true
153+
154+
143155
#####################################
144156
# Federation
145157
#####################################

mcpgateway/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ def _parse_federation_peers(cls, v):
191191
session_ttl: int = 3600
192192
message_ttl: int = 600
193193

194+
# streamable http transport
195+
use_stateful_sessions: bool = False # Set to False to use stateless sessions without event store
196+
json_response_enabled: bool = True # Enable JSON responses instead of SSE streams
197+
194198
# Development
195199
dev_mode: bool = False
196200
reload: bool = False

mcpgateway/main.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
ToolService,
106106
)
107107
from mcpgateway.transports.sse_transport import SSETransport
108-
from mcpgateway.transports.streamablehttp_transport import handle_streamable_http, session_manager
108+
from mcpgateway.transports.streamablehttp_transport import SessionManagerWrapper, JWTAuthMiddlewareStreamableHttp
109109
from mcpgateway.types import (
110110
InitializeRequest,
111111
InitializeResult,
@@ -146,6 +146,9 @@
146146
sampling_handler = SamplingHandler()
147147
server_service = ServerService()
148148

149+
# Initialize session manager for Streamable HTTP transport
150+
streamable_http_session = SessionManagerWrapper()
151+
149152

150153
# Initialize session registry
151154
session_registry = SessionRegistry(
@@ -192,11 +195,8 @@ async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
192195
await logging_service.initialize()
193196
await sampling_handler.initialize()
194197
await resource_cache.initialize()
198+
await streamable_http_session.start()
195199

196-
from contextlib import AsyncExitStack
197-
stack = AsyncExitStack()
198-
await stack.enter_async_context(session_manager.run())
199-
# await start_streamablehttp()
200200
logger.info("All services initialized successfully")
201201
yield
202202
except Exception as e:
@@ -215,6 +215,7 @@ async def lifespan(_app: FastAPI) -> AsyncIterator[None]:
215215
prompt_service,
216216
resource_service,
217217
tool_service,
218+
streamable_http_session
218219
]:
219220
try:
220221
await service.shutdown()
@@ -284,6 +285,9 @@ async def dispatch(self, request: Request, call_next):
284285
# Add custom DocsAuthMiddleware
285286
app.add_middleware(DocsAuthMiddleware)
286287

288+
# Add streamable HTTP middleware for JWT auth
289+
app.add_middleware(JWTAuthMiddlewareStreamableHttp)
290+
287291
# Set up Jinja2 templates and store in app state for later use
288292
templates = Jinja2Templates(directory=str(settings.templates_dir))
289293
app.state.templates = templates
@@ -2027,7 +2031,7 @@ async def readiness_check(db: Session = Depends(get_db)):
20272031
logger.warning("Admin API routes not mounted - Admin API disabled via MCPGATEWAY_ADMIN_API_ENABLED=False")
20282032

20292033
# Streamable http Mount
2030-
app.mount("/mcp", app=handle_streamable_http)
2034+
app.mount("/mcp", app=streamable_http_session.handle_streamable_http)
20312035

20322036
# Conditional static files mounting and root redirect
20332037
if UI_ENABLED:

0 commit comments

Comments
 (0)