@esankhan3/code-search-mcp 0.4.5 → 0.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -22,8 +22,9 @@ class InProcessBackend {
|
|
|
22
22
|
async search(query, opts) {
|
|
23
23
|
const started = Date.now();
|
|
24
24
|
let outcome = "ok";
|
|
25
|
+
let retriever;
|
|
25
26
|
try {
|
|
26
|
-
|
|
27
|
+
retriever = await getRetriever(this.project, this.knowledge);
|
|
27
28
|
const modeMap = {
|
|
28
29
|
hybrid: "vector+bm25+graph",
|
|
29
30
|
vector: "vector",
|
|
@@ -53,6 +54,7 @@ class InProcessBackend {
|
|
|
53
54
|
metrics.errors.inc({ kind: "search" });
|
|
54
55
|
throw err;
|
|
55
56
|
} finally {
|
|
57
|
+
retriever?.close?.();
|
|
56
58
|
const elapsed = (Date.now() - started) / 1e3;
|
|
57
59
|
metrics.queriesTotal.inc({ mode: opts.mode, outcome });
|
|
58
60
|
metrics.queryDuration.observe(elapsed, { mode: opts.mode });
|
|
@@ -8,8 +8,10 @@ async function startHttpTransport(opts) {
|
|
|
8
8
|
const authenticate = createAuthMiddleware(config);
|
|
9
9
|
const startedAt = Date.now();
|
|
10
10
|
const sessions = /* @__PURE__ */ new Map();
|
|
11
|
-
const
|
|
12
|
-
const
|
|
11
|
+
const ttlMin = parseInt(process.env.CODE_SEARCH_SESSION_TTL_MINUTES ?? "", 10);
|
|
12
|
+
const SESSION_TTL_MS = (Number.isFinite(ttlMin) && ttlMin > 0 ? ttlMin : 120) * 60 * 1e3;
|
|
13
|
+
const maxEnv = parseInt(process.env.CODE_SEARCH_MAX_SESSIONS ?? "", 10);
|
|
14
|
+
const MAX_SESSIONS = Number.isFinite(maxEnv) && maxEnv > 0 ? maxEnv : 100;
|
|
13
15
|
setInterval(() => {
|
|
14
16
|
const now = Date.now();
|
|
15
17
|
for (const [id, session] of sessions) {
|
|
@@ -123,8 +125,8 @@ async function startHttpTransport(opts) {
|
|
|
123
125
|
return;
|
|
124
126
|
}
|
|
125
127
|
if (sessionId && !sessions.has(sessionId)) {
|
|
126
|
-
res.writeHead(
|
|
127
|
-
res.end(JSON.stringify({ error: "
|
|
128
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
129
|
+
res.end(JSON.stringify({ error: "Session expired or unknown \u2014 reinitialize." }));
|
|
128
130
|
return;
|
|
129
131
|
}
|
|
130
132
|
if (sessions.size >= MAX_SESSIONS) {
|
|
@@ -162,11 +164,12 @@ async function startHttpTransport(opts) {
|
|
|
162
164
|
if (req.method === "GET") {
|
|
163
165
|
const sessionId = req.headers["mcp-session-id"];
|
|
164
166
|
if (sessionId && sessions.has(sessionId)) {
|
|
167
|
+
sessions.get(sessionId).lastActivity = Date.now();
|
|
165
168
|
await sessions.get(sessionId).transport.handleRequest(req, res);
|
|
166
169
|
return;
|
|
167
170
|
}
|
|
168
|
-
res.writeHead(400, { "Content-Type": "application/json" });
|
|
169
|
-
res.end(JSON.stringify({ error: "Session ID required for GET requests" }));
|
|
171
|
+
res.writeHead(sessionId ? 404 : 400, { "Content-Type": "application/json" });
|
|
172
|
+
res.end(JSON.stringify({ error: sessionId ? "Session expired or unknown \u2014 reinitialize." : "Session ID required for GET requests" }));
|
|
170
173
|
return;
|
|
171
174
|
}
|
|
172
175
|
if (req.method === "DELETE") {
|
|
@@ -15,7 +15,7 @@ class RemoteConnection {
|
|
|
15
15
|
this.apiKey = config.apiKey;
|
|
16
16
|
}
|
|
17
17
|
/** Send a JSON-RPC request to the remote MCP server */
|
|
18
|
-
async request(method, params = {}, id = 1) {
|
|
18
|
+
async request(method, params = {}, id = 1, _retried = false) {
|
|
19
19
|
const headers = {
|
|
20
20
|
"Content-Type": "application/json",
|
|
21
21
|
"Accept": "application/json, text/event-stream"
|
|
@@ -38,6 +38,11 @@ class RemoteConnection {
|
|
|
38
38
|
body
|
|
39
39
|
});
|
|
40
40
|
if (!response.ok) {
|
|
41
|
+
if ((response.status === 404 || response.status === 400) && this.sessionId && method !== "initialize" && !_retried) {
|
|
42
|
+
this.sessionId = null;
|
|
43
|
+
await this.initialize();
|
|
44
|
+
return this.request(method, params, id, true);
|
|
45
|
+
}
|
|
41
46
|
const errBody = await response.text().catch(() => "");
|
|
42
47
|
throw new Error(`Remote server error (${response.status}): ${errBody.slice(0, 300)}`);
|
|
43
48
|
}
|