-
-
Notifications
You must be signed in to change notification settings - Fork 824
Expand file tree
/
Copy pathrouter.py
More file actions
158 lines (134 loc) · 4.28 KB
/
Copy pathrouter.py
File metadata and controls
158 lines (134 loc) · 4.28 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# src/codegraphcontext/api/router.py
import asyncio
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks
from typing import Dict, Any, List
from pathlib import Path
from .schemas import (
IndexRequest,
QueryRequest,
SearchRequest,
ToolCallRequest,
ApiResponse
)
from .auth import require_api_key
from codegraphcontext.server import MCPServer
import socket
# Optional API-key auth is enforced on every route below via require_api_key.
# It is a no-op (backward compatible) unless CGC_API_KEY is configured, in
# which case each request must supply the key. See codegraphcontext.api.auth.
router = APIRouter(dependencies=[Depends(require_api_key)])
# Global server instance (initialized on startup)
_server_instance: MCPServer = None
def get_server() -> MCPServer:
global _server_instance
if _server_instance is None:
# Note: In a real production app, we'd handle initialization better
_server_instance = MCPServer(cwd=Path.cwd())
return _server_instance
def raise_service_unavailable(exc: Exception):
raise HTTPException(
status_code=503,
detail="Database service unavailable",
) from exc
@router.get("/status", response_model=ApiResponse)
async def get_status(server: MCPServer = Depends(get_server)):
try:
status = server.db_manager.is_connected()
except (OSError, socket.error) as exc:
raise_service_unavailable(exc)
return ApiResponse(
status="ok",
message="Connected" if status else "Disconnected",
data={"database": server.resolved_context.database}
)
@router.get("/tools", response_model=ApiResponse)
async def list_tools(server: MCPServer = Depends(get_server)):
return ApiResponse(
status="ok",
data={"tools": list(server.tools.values())}
)
@router.post("/tools/call", response_model=ApiResponse)
async def call_tool(
request: ToolCallRequest,
server: MCPServer = Depends(get_server)
):
try:
result = await server.handle_tool_call(
request.name,
request.arguments
)
if "error" in result:
return ApiResponse(
status="error",
error=result["error"]
)
return ApiResponse(
status="ok",
data=result
)
except (OSError, socket.error) as exc:
raise_service_unavailable(exc)
except Exception as e:
return ApiResponse(
status="error",
error=str(e)
)
@router.post("/index", response_model=ApiResponse)
async def index_repository(
request: IndexRequest,
background_tasks: BackgroundTasks,
server: MCPServer = Depends(get_server)
):
# The add_code_to_graph handler only understands "path" (and is_dependency);
# repo_name/branch/force from the request model are not supported by it.
args = {"path": request.path}
try:
result = await server.handle_tool_call(
"add_code_to_graph",
args
)
except (OSError, socket.error) as exc:
raise_service_unavailable(exc)
if "error" in result:
raise HTTPException(
status_code=400,
detail=result["error"]
)
return ApiResponse(
status="ok",
message="Indexing job started",
data=result
)
@router.post("/query", response_model=ApiResponse)
async def execute_query(
request: QueryRequest,
server: MCPServer = Depends(get_server)
):
try:
result = await server.handle_tool_call(
"execute_cypher_query",
{
"cypher_query": request.query,
"params": request.params,
},
)
except (OSError, socket.error) as exc:
raise_service_unavailable(exc)
if "error" in result:
return ApiResponse(status="error", error=result["error"])
return ApiResponse(status="ok", data=result)
@router.get("/repositories", response_model=ApiResponse)
async def list_repositories(
server: MCPServer = Depends(get_server)
):
try:
result = await server.handle_tool_call(
"list_indexed_repositories",
{}
)
except (OSError, socket.error) as exc:
raise_service_unavailable(exc)
return ApiResponse(
status="ok",
data=result
)