-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
317 lines (276 loc) · 10 KB
/
index.ts
File metadata and controls
317 lines (276 loc) · 10 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import { StorageFactory, StorageType, StorageOptions } from './storage/index.js';
import { ContextService } from './context/service.js';
import { createRoutes } from './api/routes.js';
import { KleverMCPServer } from './mcp/server.js';
import { autoIngestKnowledge } from './utils/auto-ingest.js';
import { getVersionInfo } from './version.js';
import { KleverChainClient } from './chain/index.js';
import type { KleverNetwork } from './chain/types.js';
// Load environment variables
dotenv.config({ quiet: true });
/**
* Main entry point for Klever MCP Server
*
* Supports three modes:
* 1. HTTP API server for REST access
* 2. MCP server for AI assistant integration (stdio)
* 3. Public server for hosted MCP + read-only API (HTTP Streamable transport)
*/
function createStorageAndService() {
const storageType = (process.env.STORAGE_TYPE as StorageType) || 'memory';
const storageOptions: StorageOptions = {
redis: {
url: process.env.REDIS_URL,
},
memory: {
maxSize: parseInt(process.env.MEMORY_MAX_SIZE || '10000'),
},
};
const storage = StorageFactory.create(storageType, storageOptions);
const contextService = new ContextService(storage);
return { storageType, contextService };
}
const VALID_NETWORKS = new Set(['mainnet', 'testnet', 'devnet', 'local']);
function createChainClient(): KleverChainClient {
const envNetwork = process.env.KLEVER_NETWORK;
if (envNetwork && !VALID_NETWORKS.has(envNetwork)) {
// In MCP mode stdout is reserved for the JSON-RPC protocol; stderr is the only safe log channel.
console.error(
`[WARN] Invalid KLEVER_NETWORK="${envNetwork}". Valid: mainnet, testnet, devnet, local. Defaulting to mainnet.`
);
}
const network: KleverNetwork =
envNetwork && VALID_NETWORKS.has(envNetwork) ? (envNetwork as KleverNetwork) : 'mainnet';
return new KleverChainClient({
network,
nodeUrl: process.env.KLEVER_NODE_URL,
apiUrl: process.env.KLEVER_API_URL,
timeout: parseInt(process.env.KLEVER_TIMEOUT || '15000'),
});
}
async function startHTTPServer() {
const { storageType, contextService } = createStorageAndService();
// Auto-ingest knowledge if using memory storage
if (storageType === 'memory') {
await autoIngestKnowledge(contextService);
}
// Create Express app
const app = express();
const port = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json({ limit: '10mb' }));
// Routes
app.use('/api', createRoutes(contextService));
// Global error handling
app.use(
(err: Error & { status?: number }, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
console.error('[ERROR]', new Date().toISOString(), err.stack);
// Don't leak error details in production
const isDev = process.env.NODE_ENV === 'development';
res.status(err.status || 500).json({
success: false,
error: err.name || 'Internal server error',
message: isDev ? err.message : 'An error occurred processing your request',
...(isDev && { stack: err.stack }),
});
}
);
// Start server
app.listen(port, () => {
console.log(`Klever MCP HTTP Server running on http://localhost:${port}`);
console.log(`Storage backend: ${storageType}`);
});
}
async function startMCPServer() {
const { storageType, contextService } = createStorageAndService();
// Auto-ingest knowledge if using memory storage
if (storageType === 'memory') {
await autoIngestKnowledge(contextService);
}
// Create and start MCP server
const chainClient = createChainClient();
const mcpServer = new KleverMCPServer(contextService, 'local', chainClient);
await mcpServer.start();
}
async function startPublicServer() {
// Lazy-import SDK transport types (only needed for public mode)
const { StreamableHTTPServerTransport } = await import(
'@modelcontextprotocol/sdk/server/streamableHttp.js'
);
const rateLimit = (await import('express-rate-limit')).default;
// Always use in-memory storage for public mode (read-only, no Redis needed)
const storageOptions: StorageOptions = {
memory: {
maxSize: parseInt(process.env.MEMORY_MAX_SIZE || '10000'),
},
};
const storage = StorageFactory.create('memory', storageOptions);
const contextService = new ContextService(storage);
// Auto-ingest knowledge base
await autoIngestKnowledge(contextService);
// Create Express app
const app = express();
const port = process.env.PORT || 3000;
// Trust proxy (required behind Cloudflare/nginx for correct client IP in rate limiting)
const trustProxy = process.env.TRUST_PROXY;
if (trustProxy !== undefined && trustProxy.toLowerCase() !== 'false') {
app.set('trust proxy', trustProxy.toLowerCase() === 'true' ? 1 : trustProxy);
}
// Security headers
app.use((_req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('X-Frame-Options', 'DENY');
res.setHeader('X-XSS-Protection', '0');
res.setHeader(
'Strict-Transport-Security',
'max-age=31536000; includeSubDomains'
);
res.setHeader(
'Content-Security-Policy',
"default-src 'none'; frame-ancestors 'none'"
);
next();
});
// CORS — restrict to necessary headers
const corsOrigins = process.env.CORS_ORIGINS;
const corsOriginConfig = !corsOrigins || corsOrigins === '*'
? true
: corsOrigins.split(',').map(o => o.trim());
app.use(
cors({
origin: corsOriginConfig,
allowedHeaders: ['Content-Type', 'mcp-session-id', 'Last-Event-ID'],
exposedHeaders: ['mcp-session-id'],
})
);
// Body parser with reduced limit for public mode
const bodyLimit = process.env.BODY_SIZE_LIMIT || '1mb';
app.use(express.json({ limit: bodyLimit }));
// Rate limiting for MCP endpoint
const parsedMcpRate = parseInt(process.env.RATE_LIMIT_MCP || '60');
const mcpRateLimitValue = Number.isFinite(parsedMcpRate) ? parsedMcpRate : 60;
const mcpLimiter = rateLimit({
windowMs: 60 * 1000,
limit: mcpRateLimitValue,
standardHeaders: 'draft-7',
legacyHeaders: false,
message: { error: 'Too many requests, please try again later' },
});
// Rate limiting for API endpoints
const parsedApiRate = parseInt(process.env.RATE_LIMIT_API || '30');
const apiRateLimitValue = Number.isFinite(parsedApiRate) ? parsedApiRate : 30;
const apiLimiter = rateLimit({
windowMs: 60 * 1000,
limit: apiRateLimitValue,
standardHeaders: 'draft-7',
legacyHeaders: false,
message: { error: 'Too many requests, please try again later' },
});
// MCP endpoint — Stateless Streamable HTTP
// Each request creates its own transport+server and is fully self-contained.
// No session tracking needed — eliminates "Session not found" errors after
// container restarts, Watchtower redeployments, or TTL expiry.
app.post('/mcp', mcpLimiter, async (req, res) => {
try {
const transport = new StreamableHTTPServerTransport({
sessionIdGenerator: undefined,
});
res.on('close', () => {
transport.close().catch(() => {});
});
const chainClient = createChainClient();
const mcpServer = new KleverMCPServer(contextService, 'public', chainClient);
await mcpServer.connectTransport(transport);
await transport.handleRequest(req, res, req.body);
} catch (error) {
console.error('[Public] MCP endpoint error:', error);
if (!res.headersSent) {
res.status(500).json({
jsonrpc: '2.0',
error: { code: -32603, message: 'Internal server error' },
id: null,
});
}
}
});
// GET and DELETE are not applicable in stateless mode
app.get('/mcp', mcpLimiter, (_req, res) => {
res.status(405).json({
jsonrpc: '2.0',
error: { code: -32000, message: 'Method not allowed. Use POST for stateless requests.' },
id: null,
});
});
app.delete('/mcp', mcpLimiter, (_req, res) => {
res.status(405).json({
jsonrpc: '2.0',
error: { code: -32000, message: 'Method not allowed. No sessions to terminate in stateless mode.' },
id: null,
});
});
// Read-only API routes
app.use('/api', apiLimiter, createRoutes(contextService, { readOnly: true }));
// Health endpoint at root level
app.get('/health', async (_req, res) => {
try {
const allContexts = await contextService.query({ limit: 1, offset: 0 });
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
mode: 'public',
uptime: process.uptime(),
...getVersionInfo(),
knowledgeBase: {
totalContexts: allContexts.total,
},
});
} catch {
res.status(503).json({
status: 'error',
timestamp: new Date().toISOString(),
mode: 'public',
uptime: process.uptime(),
...getVersionInfo(),
});
}
});
// Global error handling
app.use(
(err: Error & { status?: number }, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
console.error('[ERROR]', new Date().toISOString(), err.stack);
res.status(err.status || 500).json({
success: false,
error: 'Internal server error',
});
}
);
// Start server
const server = app.listen(port, () => {
console.log(`Klever MCP Public Server running on http://localhost:${port}`);
console.log(`MCP endpoint: http://localhost:${port}/mcp`);
console.log(`API endpoint: http://localhost:${port}/api (read-only)`);
console.log(`Health check: http://localhost:${port}/health`);
});
// Graceful shutdown
const shutdown = () => {
console.error('\n[Public] Shutting down gracefully...');
server.close(() => {
process.exit(0);
});
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
}
// Determine which mode to run
const mode = process.env.MODE || 'http';
if (mode === 'mcp') {
startMCPServer().catch(console.error);
} else if (mode === 'public') {
startPublicServer().catch(console.error);
} else {
startHTTPServer().catch(console.error);
}