Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,12 @@
|
|
| 1 |
import asyncio
|
| 2 |
import os
|
|
|
|
| 3 |
from fastapi import FastAPI, Request, Response
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
app = FastAPI()
|
| 6 |
proc = None
|
| 7 |
|
|
@@ -12,9 +17,10 @@ async def startup_event():
|
|
| 12 |
# This is a more secure way to handle the token
|
| 13 |
token = os.environ.get("GITHUB_PERSONAL_ACCESS_TOKEN")
|
| 14 |
if not token:
|
| 15 |
-
|
| 16 |
return
|
| 17 |
|
|
|
|
| 18 |
proc = await asyncio.create_subprocess_exec(
|
| 19 |
'/usr/local/bin/github-mcp-server', 'stdio',
|
| 20 |
stdin=asyncio.subprocess.PIPE,
|
|
@@ -22,45 +28,64 @@ async def startup_event():
|
|
| 22 |
stderr=asyncio.subprocess.PIPE,
|
| 23 |
env={"GITHUB_PERSONAL_ACCESS_TOKEN": token}
|
| 24 |
)
|
|
|
|
| 25 |
asyncio.create_task(log_stderr())
|
| 26 |
|
| 27 |
async def log_stderr():
|
| 28 |
if proc and proc.stderr:
|
| 29 |
while not proc.stderr.at_eof():
|
| 30 |
line = await proc.stderr.readline()
|
| 31 |
-
|
| 32 |
|
| 33 |
@app.post("/")
|
| 34 |
async def proxy(request: Request):
|
| 35 |
if not proc or not proc.stdin or not proc.stdout:
|
|
|
|
| 36 |
return Response(status_code=500, content="Subprocess not running")
|
| 37 |
|
|
|
|
| 38 |
body = await request.body()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
proc.stdin.write(body)
|
| 40 |
await proc.stdin.drain()
|
| 41 |
|
|
|
|
| 42 |
response = bytearray()
|
| 43 |
-
|
| 44 |
while True:
|
| 45 |
try:
|
| 46 |
chunk = await proc.stdout.read(1024)
|
| 47 |
if not chunk:
|
|
|
|
| 48 |
break
|
| 49 |
response.extend(chunk)
|
|
|
|
| 50 |
# Check if we have a complete JSON message
|
| 51 |
try:
|
| 52 |
response_str = bytes(response).decode("utf-8")
|
|
|
|
|
|
|
| 53 |
if response_str.strip().endswith("}") and "\"jsonrpc\"" in response_str and "\"result\"" in response_str:
|
|
|
|
| 54 |
break
|
| 55 |
-
except:
|
| 56 |
-
|
|
|
|
| 57 |
except Exception as e:
|
|
|
|
| 58 |
break
|
| 59 |
|
|
|
|
|
|
|
| 60 |
return Response(content=bytes(response))
|
| 61 |
|
| 62 |
@app.on_event("shutdown")
|
| 63 |
async def shutdown_event():
|
| 64 |
if proc:
|
|
|
|
| 65 |
proc.kill()
|
| 66 |
-
await proc.wait()
|
|
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import os
|
| 3 |
+
import logging
|
| 4 |
from fastapi import FastAPI, Request, Response
|
| 5 |
|
| 6 |
+
# Set up logging
|
| 7 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
app = FastAPI()
|
| 11 |
proc = None
|
| 12 |
|
|
|
|
| 17 |
# This is a more secure way to handle the token
|
| 18 |
token = os.environ.get("GITHUB_PERSONAL_ACCESS_TOKEN")
|
| 19 |
if not token:
|
| 20 |
+
logger.error("GITHUB_PERSONAL_ACCESS_TOKEN environment variable not set")
|
| 21 |
return
|
| 22 |
|
| 23 |
+
logger.info("Starting subprocess with token: %s", token[:10] + "...")
|
| 24 |
proc = await asyncio.create_subprocess_exec(
|
| 25 |
'/usr/local/bin/github-mcp-server', 'stdio',
|
| 26 |
stdin=asyncio.subprocess.PIPE,
|
|
|
|
| 28 |
stderr=asyncio.subprocess.PIPE,
|
| 29 |
env={"GITHUB_PERSONAL_ACCESS_TOKEN": token}
|
| 30 |
)
|
| 31 |
+
logger.info("Subprocess started with PID: %s", proc.pid)
|
| 32 |
asyncio.create_task(log_stderr())
|
| 33 |
|
| 34 |
async def log_stderr():
|
| 35 |
if proc and proc.stderr:
|
| 36 |
while not proc.stderr.at_eof():
|
| 37 |
line = await proc.stderr.readline()
|
| 38 |
+
logger.debug("github-mcp-server stderr: %s", line.decode().strip())
|
| 39 |
|
| 40 |
@app.post("/")
|
| 41 |
async def proxy(request: Request):
|
| 42 |
if not proc or not proc.stdin or not proc.stdout:
|
| 43 |
+
logger.error("Subprocess not running")
|
| 44 |
return Response(status_code=500, content="Subprocess not running")
|
| 45 |
|
| 46 |
+
# Log incoming request
|
| 47 |
body = await request.body()
|
| 48 |
+
logger.info("Received request: %s", body.decode())
|
| 49 |
+
|
| 50 |
+
# Send to subprocess
|
| 51 |
+
logger.info("Sending to subprocess...")
|
| 52 |
proc.stdin.write(body)
|
| 53 |
await proc.stdin.drain()
|
| 54 |
|
| 55 |
+
# Read response
|
| 56 |
response = bytearray()
|
| 57 |
+
logger.info("Reading response...")
|
| 58 |
while True:
|
| 59 |
try:
|
| 60 |
chunk = await proc.stdout.read(1024)
|
| 61 |
if not chunk:
|
| 62 |
+
logger.info("EOF reached")
|
| 63 |
break
|
| 64 |
response.extend(chunk)
|
| 65 |
+
logger.debug("Received chunk: %s", chunk)
|
| 66 |
# Check if we have a complete JSON message
|
| 67 |
try:
|
| 68 |
response_str = bytes(response).decode("utf-8")
|
| 69 |
+
logger.debug("Decoded response: %s", response_str)
|
| 70 |
+
# If we can decode and it looks like a complete JSON-RPC response, break
|
| 71 |
if response_str.strip().endswith("}") and "\"jsonrpc\"" in response_str and "\"result\"" in response_str:
|
| 72 |
+
logger.info("Complete JSON-RPC response detected")
|
| 73 |
break
|
| 74 |
+
except Exception as e:
|
| 75 |
+
logger.warning("Decoding failed: %s", str(e))
|
| 76 |
+
# Continue reading
|
| 77 |
except Exception as e:
|
| 78 |
+
logger.error("Error reading response: %s", str(e))
|
| 79 |
break
|
| 80 |
|
| 81 |
+
# Log final response
|
| 82 |
+
logger.info("Final response: %s", bytes(response).decode())
|
| 83 |
return Response(content=bytes(response))
|
| 84 |
|
| 85 |
@app.on_event("shutdown")
|
| 86 |
async def shutdown_event():
|
| 87 |
if proc:
|
| 88 |
+
logger.info("Shutting down subprocess...")
|
| 89 |
proc.kill()
|
| 90 |
+
await proc.wait()
|
| 91 |
+
logger.info("Subprocess shut down")
|