Alibrown commited on
Commit
9b296b2
Β·
verified Β·
1 Parent(s): 0f37933

Update app/app.py

Browse files
Files changed (1) hide show
  1. app/app.py +42 -12
app/app.py CHANGED
@@ -55,6 +55,29 @@ logger_tools = logging.getLogger('tools')
55
  logger_providers = logging.getLogger('providers')
56
  logger_models = logging.getLogger('models')
57
  logger_db_sync = logging.getLogger('db_sync')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  # =============================================================================
59
  # Quart app instance
60
  # =============================================================================
@@ -125,16 +148,6 @@ async def crypto_endpoint():
125
  return jsonify({"status": "not_implemented"}), 501
126
 
127
 
128
- @app.route("/mcp", methods=["GET", "POST"])
129
- async def mcp_endpoint():
130
- """
131
- MCP SSE Transport endpoint β€” routed through Quart/hypercorn.
132
- All MCP traffic passes through here β€” enables interception, logging,
133
- auth checks, rate limiting, payload transformation before reaching MCP.
134
- """
135
- return await mcp.handle_request(request)
136
-
137
-
138
  # Future routes (uncomment when ready):
139
  # @app.route("/discord", methods=["POST"])
140
  # async def discord_interactions():
@@ -206,8 +219,25 @@ async def start_application(fundaments: Dict[str, Any]) -> None:
206
  await db_sync.initialize()
207
  await mcp.initialize()
208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  # --- Read PORT from app/.pyfun [HUB] ---
210
- port = int(app_config.get_hub().get("HUB_PORT", "7860"))
211
 
212
  # --- Configure hypercorn ---
213
  config = Config()
@@ -235,4 +265,4 @@ if __name__ == '__main__':
235
  "security": None,
236
  }
237
 
238
- asyncio.run(start_application(test_fundaments))
 
55
  logger_providers = logging.getLogger('providers')
56
  logger_models = logging.getLogger('models')
57
  logger_db_sync = logging.getLogger('db_sync')
58
+
59
+
60
+
61
+ # ── NEU: nach den Imports, vor app = Quart(__name__) ──────────────────────────
62
+
63
+ def _make_mount_middleware(outer_app, path_prefix: str, inner_app):
64
+ """
65
+ Minimale ASGI-Middleware: leitet Requests mit path_prefix an inner_app
66
+ (FastMCP Streamable HTTP) weiter, alles andere geht an outer_app (Quart).
67
+ Nur aktiv bei HUB_TRANSPORT = "streamable-http".
68
+ """
69
+ async def middleware(scope, receive, send):
70
+ path = scope.get("path", "")
71
+ if path == path_prefix or path.startswith(path_prefix + "/"):
72
+ scope = dict(scope)
73
+ stripped = path[len(path_prefix):] or "/"
74
+ scope["path"] = stripped
75
+ scope["raw_path"] = stripped.encode()
76
+ await inner_app(scope, receive, send)
77
+ else:
78
+ await outer_app(scope, receive, send)
79
+ return middleware
80
+
81
  # =============================================================================
82
  # Quart app instance
83
  # =============================================================================
 
148
  return jsonify({"status": "not_implemented"}), 501
149
 
150
 
 
 
 
 
 
 
 
 
 
 
151
  # Future routes (uncomment when ready):
152
  # @app.route("/discord", methods=["POST"])
153
  # async def discord_interactions():
 
219
  await db_sync.initialize()
220
  await mcp.initialize()
221
 
222
+ # --- Transport-abhΓ€ngiges MCP-Routing ---
223
+ hub_cfg = app_config.get_hub()
224
+ transport = hub_cfg.get("HUB_TRANSPORT", "streamable-http").lower()
225
+
226
+ if transport == "streamable-http":
227
+ # ASGI-Mount: FastMCP ΓΌbernimmt /mcp direkt β€” kein Quart-Overhead
228
+ app.asgi_app = _make_mount_middleware(app.asgi_app, "/mcp", mcp.get_asgi_app())
229
+ logger.info("MCP transport: Streamable HTTP β†’ /mcp")
230
+ else:
231
+ # SSE legacy β€” Quart-Route dynamisch registrieren
232
+ @app.route("/mcp", methods=["GET", "POST"])
233
+ async def mcp_endpoint():
234
+ """MCP SSE legacy transport β€” interceptor point fΓΌr auth/logging."""
235
+ return await mcp.handle_request(request)
236
+ logger.info("MCP transport: SSE (legacy) β†’ /mcp")
237
+
238
+
239
  # --- Read PORT from app/.pyfun [HUB] ---
240
+ port = int(hub_cfg.get("HUB_PORT", "7860")) # hub_cfg bereits gelesen, kein zweiter get_hub()-Call
241
 
242
  # --- Configure hypercorn ---
243
  config = Config()
 
265
  "security": None,
266
  }
267
 
268
+ asyncio.run(start_application(test_fundaments))