-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
287 lines (254 loc) · 9.1 KB
/
server.ts
File metadata and controls
287 lines (254 loc) · 9.1 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @file server
* @description MCP server bootstrap supporting stdio and Streamable HTTP transports.
* @remarks Tool registration is sourced from the centralized tool registry.
*/
import * as env from "./env.js";
import * as z from "zod";
import { randomUUID } from "node:crypto";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import { createMcpExpressApp } from "@modelcontextprotocol/sdk/server/express.js";
import MemgraphClient from "./graph/client.js";
import GraphIndexManager from "./graph/index.js";
import { ToolHandlers } from "./tools/tool-handlers.js";
import { toolRegistry } from "./tools/registry.js";
import { loadConfig, type Config } from "./config.js";
import GraphOrchestrator from "./graph/orchestrator.js";
import { runWithRequestContext } from "./request-context.js";
import { logger } from "./utils/logger.js";
// Initialize components
const memgraph = new MemgraphClient({
host: env.MEMGRAPH_HOST,
port: env.MEMGRAPH_PORT,
});
const index = new GraphIndexManager();
let toolHandlers: ToolHandlers;
let config: Config = { architecture: { layers: [], rules: [] } };
let orchestrator: GraphOrchestrator;
/**
* Initializes shared infrastructure required before serving requests.
*
* @remarks
* This connects Memgraph, loads architecture config, and wires `ToolHandlers`
* with the shared index/orchestrator instances.
*/
async function initialize() {
try {
await memgraph.connect();
logger.error("[MCP] Memgraph connected");
// Load architecture config if exists
try {
config = await loadConfig();
logger.error("[MCP] Configuration loaded");
} catch (_err) {
logger.error("[MCP] No configuration file found, using defaults");
config = { architecture: { layers: [], rules: [] } };
}
// Initialize GraphOrchestrator — pass shared index so post-build sync populates it
orchestrator = new GraphOrchestrator(memgraph, false, index);
toolHandlers = new ToolHandlers({
index,
memgraph,
config,
orchestrator: orchestrator,
});
logger.error("[MCP] Tool handlers initialized");
} catch (error) {
logger.error("[MCP] Initialization error:", error);
}
}
// Server implementation info
const serverInfo = {
name: env.LXDIG_SERVER_NAME,
version: "1.0.0",
};
/**
* Creates a configured MCP server instance and binds all registered tools.
*
* @returns A ready-to-connect `McpServer` instance.
*/
function createMcpServerInstance(): McpServer {
const mcpServer = new McpServer(serverInfo);
/**
* Wraps registry-based tool execution into MCP response envelopes.
*/
const invokeRegisteredTool = (toolName: string) => async (args: unknown) => {
if (!toolHandlers) {
return {
content: [{ type: "text" as const, text: "Server not initialized" }],
isError: true,
};
}
try {
const result = await toolHandlers.callTool(toolName, args as Record<string, unknown>);
return { content: [{ type: "text" as const, text: result }] };
} catch (error: unknown) {
return {
content: [{ type: "text" as const, text: `Error: ${(error as Error).message}` }],
isError: true,
};
}
};
/**
* Registers one tool definition with zod input validation.
*/
const registerTool = (name: string, description: string, inputSchema: z.ZodTypeAny) => {
mcpServer.registerTool(
name,
{
description,
inputSchema,
},
invokeRegisteredTool(name),
);
};
// Register all tools from centralized registry.
for (const definition of toolRegistry) {
registerTool(definition.name, definition.description, z.object(definition.inputShape));
}
return mcpServer;
}
/**
* Process entrypoint.
*
* @remarks
* Chooses transport mode (`stdio` or `http`), initializes per-session state,
* and starts serving requests.
*/
async function main() {
await initialize();
const transportMode = env.MCP_TRANSPORT;
if (transportMode === "http") {
const port = env.MCP_PORT;
const app = createMcpExpressApp();
const sessions = new Map<
string,
{ server: McpServer; transport: StreamableHTTPServerTransport }
>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handleMcpRequest = async (req: any, res: any) => {
try {
const headerSessionId = req.headers?.["mcp-session-id"];
const sessionId =
typeof headerSessionId === "string"
? headerSessionId
: Array.isArray(headerSessionId)
? headerSessionId[0]
: undefined;
const isInitialize = req.body?.method === "initialize";
if (isInitialize) {
const sessionServer = createMcpServerInstance();
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: () => randomUUID(),
onsessioninitialized: (newSessionId: string) => {
sessions.set(newSessionId, {
server: sessionServer,
transport,
});
},
});
transport.onclose = () => {
const closedSessionId = transport.sessionId;
if (!closedSessionId) {
return;
}
const existing = sessions.get(closedSessionId);
if (existing?.transport === transport) {
sessions.delete(closedSessionId);
void existing.server.close().catch((closeError) => {
logger.warn(
"[MCP] Failed to close session server after transport close:",
closeError,
);
});
}
};
await sessionServer.connect(transport);
await runWithRequestContext({ sessionId: transport.sessionId }, async () => {
await transport!.handleRequest(req, res, req.body);
});
return;
}
if (!sessionId || !sessions.has(sessionId)) {
res.status(400).json({
jsonrpc: "2.0",
error: {
code: -32000,
message: "Bad Request: Invalid or missing MCP session",
},
id: null,
});
return;
}
const sessionState = sessions.get(sessionId)!;
await runWithRequestContext({ sessionId }, async () => {
await sessionState.transport.handleRequest(req, res, req.body);
});
} catch (error: unknown) {
logger.error("[MCP] HTTP transport error:", error);
if (!res.headersSent) {
res.status(500).json({
jsonrpc: "2.0",
error: {
code: -32603,
message: (error as Error)?.message || "Internal server error",
},
id: null,
});
}
}
};
app.post("/", (req, res) => { void handleMcpRequest(req, res); });
app.post("/mcp", (req, res) => { void handleMcpRequest(req, res); });
app.get("/health", (_req, res) => {
res.status(200).json({ status: "ok", transport: "http" });
});
// A2A Agent Card — Phase 4 / Section 0.4 of AGENT_CONTEXT_ENGINE_PLAN.md
// Allows A2A-aware orchestrators (LangGraph, AutoGen, etc.) to discover
// this server as a memory + coordination specialist agent.
app.get("/.well-known/agent.json", (_req, res) => {
const serverName = env.LXDIG_SERVER_NAME;
res.status(200).json({
"@context": "https://schema.a2aprotocol.dev/v1",
"@type": "Agent",
name: serverName,
description:
"External long-term memory and coordination layer for LLM agent fleets working on software codebases. Provides code graph queries, agent episode memory, multi-agent coordination, and PPR-ranked context packing.",
capabilities: [
"code-graph",
"agent-memory",
"agent-coordination",
"multi-agent-coordination",
"context-packing",
"architecture-validation",
"test-impact-analysis",
],
mcpEndpoint: "/mcp",
transport: "StreamableHTTP",
version: "1.0.0",
});
});
app.listen(port, () => {
logger.error(`[MCP] Server started on HTTP transport (port ${port})`);
logger.error("[MCP] Endpoints: POST / and POST /mcp");
logger.error("[MCP] A2A Agent Card: GET /.well-known/agent.json");
logger.error(
`[MCP] Available tools: 38 (5 GraphRAG + 2 Architecture + 4 Test + 4 Progress + 4 Utility + 5 Vector Search + 2 Docs + 1 Reference + 2 Setup)`,
);
});
return;
}
const mcpServer = createMcpServerInstance();
const stdioTransport = new StdioServerTransport();
await mcpServer.connect(stdioTransport);
logger.error("[MCP] Server started on stdio transport");
logger.error(
`[MCP] Available tools: 38 (5 GraphRAG + 2 Architecture + 4 Test + 4 Progress + 4 Utility + 5 Vector Search + 2 Docs + 1 Reference + 2 Setup)`,
);
}
main().catch((error) => {
logger.error("[MCP] Fatal error:", error);
process.exit(1);
});