sarveshpatel commited on
Commit
612b769
Β·
verified Β·
1 Parent(s): c6951d9

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +26 -8
app/main.py CHANGED
@@ -14,7 +14,7 @@ from app.config import get_settings
14
  from app.executor import get_executor
15
  from app.routes import health, mcp, tools
16
 
17
- # ─── Logging Configuration ────────────────────────────────────────
18
 
19
  logging.basicConfig(
20
  level=logging.INFO,
@@ -25,7 +25,7 @@ logging.basicConfig(
25
  logger = logging.getLogger("mcp_code_executor")
26
 
27
 
28
- # ─── Application Lifespan ─────────────────────────────────────────
29
 
30
  @asynccontextmanager
31
  async def lifespan(app: FastAPI):
@@ -38,6 +38,7 @@ async def lifespan(app: FastAPI):
38
  logger.info(" Max concurrent: %d", settings.max_concurrent_executions)
39
  logger.info(" Execution timeout: %ds", settings.execution_timeout)
40
  logger.info(" Python executable: %s", settings.get_python_executable())
 
41
  logger.info("=" * 60)
42
 
43
  yield
@@ -49,13 +50,14 @@ async def lifespan(app: FastAPI):
49
  logger.info("Shutdown complete")
50
 
51
 
52
- # ─── FastAPI Application ──────────────────────────────────────────
53
 
54
  app = FastAPI(
55
  title="MCP Code Executor",
56
  description=(
57
  "An MCP server that allows LLMs to execute Python code within a "
58
- "specified Python environment. Supports incremental code generation, "
 
59
  "package management, and concurrent execution."
60
  ),
61
  version="1.0.0",
@@ -65,7 +67,7 @@ app = FastAPI(
65
  openapi_url="/openapi.json",
66
  )
67
 
68
- # ─── CORS Middleware ───────────────────────────────────────────────
69
 
70
  app.add_middleware(
71
  CORSMiddleware,
@@ -73,10 +75,26 @@ app.add_middleware(
73
  allow_credentials=True,
74
  allow_methods=["*"],
75
  allow_headers=["*"],
 
76
  )
77
 
78
 
79
- # ─── Global Exception Handler ─────────────────────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  @app.exception_handler(Exception)
82
  async def global_exception_handler(request: Request, exc: Exception):
@@ -90,14 +108,14 @@ async def global_exception_handler(request: Request, exc: Exception):
90
  )
91
 
92
 
93
- # ─── Register Routes ──────────────────────────────────────────────
94
 
95
  app.include_router(health.router)
96
  app.include_router(mcp.router)
97
  app.include_router(tools.router)
98
 
99
 
100
- # ─── Direct Run (development) ─────────────────────────────────────
101
 
102
  if __name__ == "__main__":
103
  import uvicorn
 
14
  from app.executor import get_executor
15
  from app.routes import health, mcp, tools
16
 
17
+ # ─── Logging Configuration ────────────────────────────────────
18
 
19
  logging.basicConfig(
20
  level=logging.INFO,
 
25
  logger = logging.getLogger("mcp_code_executor")
26
 
27
 
28
+ # ─── Application Lifespan ─────────────────────────────────────
29
 
30
  @asynccontextmanager
31
  async def lifespan(app: FastAPI):
 
38
  logger.info(" Max concurrent: %d", settings.max_concurrent_executions)
39
  logger.info(" Execution timeout: %ds", settings.execution_timeout)
40
  logger.info(" Python executable: %s", settings.get_python_executable())
41
+ logger.info(" MCP endpoint: /mcp/jsonrpc")
42
  logger.info("=" * 60)
43
 
44
  yield
 
50
  logger.info("Shutdown complete")
51
 
52
 
53
+ # ─── FastAPI Application ──────────────────────────────────────
54
 
55
  app = FastAPI(
56
  title="MCP Code Executor",
57
  description=(
58
  "An MCP server that allows LLMs to execute Python code within a "
59
+ "specified Python environment. Supports the full MCP 2025-03-26 "
60
+ "Streamable HTTP transport protocol, incremental code generation, "
61
  "package management, and concurrent execution."
62
  ),
63
  version="1.0.0",
 
67
  openapi_url="/openapi.json",
68
  )
69
 
70
+ # ─── CORS Middleware ───────────────────────────────────────────
71
 
72
  app.add_middleware(
73
  CORSMiddleware,
 
75
  allow_credentials=True,
76
  allow_methods=["*"],
77
  allow_headers=["*"],
78
+ expose_headers=["*"],
79
  )
80
 
81
 
82
+ # ─── MCP Headers Middleware ────────────────────────────────────
83
+
84
+ @app.middleware("http")
85
+ async def add_mcp_headers(request: Request, call_next):
86
+ """Add MCP-required headers to responses on MCP endpoints."""
87
+ response = await call_next(request)
88
+
89
+ if request.url.path.startswith("/mcp/"):
90
+ # MCP Streamable HTTP requires these headers
91
+ response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
92
+ response.headers["Connection"] = "keep-alive"
93
+
94
+ return response
95
+
96
+
97
+ # ─── Global Exception Handler ─────────────────────────────────
98
 
99
  @app.exception_handler(Exception)
100
  async def global_exception_handler(request: Request, exc: Exception):
 
108
  )
109
 
110
 
111
+ # ─── Register Routes ──────────────────────────────────────────
112
 
113
  app.include_router(health.router)
114
  app.include_router(mcp.router)
115
  app.include_router(tools.router)
116
 
117
 
118
+ # ─── Direct Run (development) ─────────────────────────────────
119
 
120
  if __name__ == "__main__":
121
  import uvicorn