-
-
Notifications
You must be signed in to change notification settings - Fork 823
Expand file tree
/
Copy pathapp.py
More file actions
92 lines (83 loc) · 3.75 KB
/
Copy pathapp.py
File metadata and controls
92 lines (83 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# src/codegraphcontext/api/app.py
import os
from fastapi import Depends, FastAPI
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from .router import router
from .auth import log_auth_status, require_api_key
from .mcp_sse import handle_sse, handle_messages
def create_app() -> FastAPI:
# Log whether API-key auth is active. Emits a prominent security warning
# when the gateway is running unauthenticated (CGC_API_KEY unset).
log_auth_status()
app = FastAPI(
title="CodeGraphContext Gateway",
description="HTTP API gateway for CodeGraphContext MCP server. Enables integration with ChatGPT Actions, Claude, and web frontends.",
version="0.1.0"
)
# Enable CORS for the website/frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, restrict this
# Credentials must stay disabled while origins is a wildcard; the
# combination is rejected by browsers and would leak cookie-authed
# responses to any site.
allow_credentials=False,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(router, prefix="/api/v1")
@app.get("/health")
async def health():
"""Liveness probe for load balancers and k8s."""
return {"status": "ok"}
# MCP-over-SSE Endpoints. These dispatch to the same tools as the REST
# router (execute_cypher_query, add_code_to_graph, delete_repository), so
# they need the same API-key dependency the router applies — without it,
# setting CGC_API_KEY left the SSE transport as an unauthenticated path to
# every tool.
app.add_api_route(
"/api/v1/mcp/sse",
handle_sse,
methods=["GET"],
dependencies=[Depends(require_api_key)],
)
app.add_api_route(
"/api/v1/mcp/messages",
handle_messages,
methods=["POST"],
dependencies=[Depends(require_api_key)],
)
@app.get("/", response_class=HTMLResponse)
async def root():
return """
<!DOCTYPE html>
<html>
<head>
<title>CGC Gateway</title>
<style>
body { font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background: #0f172a; color: white; margin: 0; }
.card { background: #1e293b; padding: 2rem; border-radius: 1rem; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1); text-align: center; max-width: 400px; border: 1px solid #334155; }
h1 { color: #38bdf8; margin-top: 0; }
p { color: #94a3b8; line-height: 1.6; }
.btn { display: inline-block; background: #38bdf8; color: #0f172a; padding: 0.75rem 1.5rem; border-radius: 0.5rem; text-decoration: none; font-weight: bold; margin-top: 1rem; transition: background 0.2s; }
.btn:hover { background: #7dd3fc; }
.links { margin-top: 1.5rem; font-size: 0.9rem; }
.links a { color: #38bdf8; text-decoration: none; margin: 0 0.5rem; }
</style>
</head>
<body>
<div class="card">
<h1>CGC Gateway</h1>
<p>CodeGraphContext HTTP API is running. This gateway allows ChatGPT and Claude to interact with your code graph.</p>
<a href="/docs" class="btn">View API Docs</a>
<div class="links">
<a href="/openapi.json">OpenAPI Spec</a>
<a href="https://github.com/CodeGraphContext/CodeGraphContext" target="_blank">GitHub</a>
</div>
</div>
</body>
</html>
"""
return app
app = create_app()