-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmcp-server.js
More file actions
executable file
·470 lines (411 loc) · 12.8 KB
/
Copy pathmcp-server.js
File metadata and controls
executable file
·470 lines (411 loc) · 12.8 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env node
/**
* ChittyConnect MCP Server
*
* Standalone Model Context Protocol server for Claude Desktop/Code integration.
* Provides ContextConsciousness™ and MemoryCloude™ capabilities through
* a comprehensive set of tools for the ChittyOS ecosystem.
*
* Usage:
* node mcp-server.js
*
* Environment Variables:
* CHITTYCONNECT_URL - ChittyConnect API URL (default: https://connect.chitty.cc)
* CHITTY_AUTH_SERVICE_TOKEN - Authentication token for ChittyConnect
* ENABLE_STREAMING - Enable SSE streaming (default: true)
* SESSION_PERSISTENCE - Enable session persistence (default: true)
* PLATFORM - Platform type: desktop, code, web (default: desktop)
* DEBUG - Enable debug logging (default: false)
*/
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import fetch from 'node-fetch';
import { MCP_TOOLS } from './src/mcp/tool-registry.js';
// Configuration from environment
const config = {
chittyconnectUrl: process.env.CHITTYCONNECT_URL || 'https://connect.chitty.cc',
authToken: process.env.CHITTY_AUTH_SERVICE_TOKEN,
enableStreaming: process.env.ENABLE_STREAMING !== 'false',
sessionPersistence: process.env.SESSION_PERSISTENCE !== 'false',
platform: process.env.PLATFORM || 'desktop',
debug: process.env.DEBUG === 'true'
};
// Validate configuration
if (!config.authToken) {
console.error('[ChittyConnect MCP] Error: CHITTY_AUTH_SERVICE_TOKEN is required');
console.error('Please set the CHITTY_AUTH_SERVICE_TOKEN environment variable');
process.exit(1);
}
// Debug logging
function debug(...args) {
if (config.debug) {
console.error('[ChittyConnect MCP Debug]', ...args);
}
}
// Create MCP server
const server = new Server(
{
name: 'chittyconnect',
version: '2.0.0',
capabilities: {
tools: {},
resources: {},
prompts: {},
sampling: {} // Enable for advanced features
}
},
{
onError: (error) => console.error('[ChittyConnect MCP Error]', error),
}
);
// Session management
const sessions = new Map();
let currentSessionId = null;
function getOrCreateSession() {
if (!currentSessionId) {
currentSessionId = `mcp-${Date.now()}-${Math.random().toString(36).substring(7)}`;
sessions.set(currentSessionId, {
id: currentSessionId,
created: Date.now(),
toolHistory: [],
context: {}
});
}
return currentSessionId;
}
// Helper function to call ChittyConnect API
async function callChittyConnect(endpoint, method = 'GET', body = null) {
const url = `${config.chittyconnectUrl}${endpoint}`;
debug(`Calling ChittyConnect: ${method} ${url}`);
const options = {
method,
headers: {
'Authorization': `Bearer ${config.authToken}`,
'Content-Type': 'application/json',
'X-MCP-Platform': config.platform,
'X-MCP-Session': getOrCreateSession()
}
};
if (body) {
options.body = JSON.stringify(body);
}
try {
const response = await fetch(url, options);
if (!response.ok) {
const error = await response.text();
throw new Error(`ChittyConnect API error (${response.status}): ${error}`);
}
return await response.json();
} catch (error) {
console.error(`[ChittyConnect MCP] API call failed: ${error.message}`);
throw error;
}
}
// Tool registration — uses unified tool registry (no network call needed)
server.setRequestHandler('tools/list', async () => {
debug('Listing tools');
return { tools: MCP_TOOLS };
});
// Tool execution
server.setRequestHandler('tools/call', async (request) => {
const { name, arguments: args } = request.params;
debug(`Executing tool: ${name}`);
// Track tool usage in session
const sessionId = getOrCreateSession();
const session = sessions.get(sessionId);
session.toolHistory.push({
tool: name,
timestamp: Date.now(),
args: args
});
// Special handling for status check
if (name === 'chittyconnect_status') {
try {
const health = await callChittyConnect('/health');
return {
content: [{
type: 'text',
text: JSON.stringify({
status: 'connected',
health: health,
session: sessionId,
platform: config.platform
}, null, 2)
}]
};
} catch (error) {
return {
content: [{
type: 'text',
text: JSON.stringify({
status: 'error',
error: error.message,
session: sessionId,
platform: config.platform
}, null, 2)
}],
isError: true
};
}
}
// Forward tool execution to ChittyConnect
try {
const response = await callChittyConnect('/mcp/tools/call', 'POST', {
name,
arguments: args,
context: {
sessionId,
platform: config.platform,
timestamp: Date.now()
}
});
// Persist session if enabled
if (config.sessionPersistence && session.toolHistory.length % 5 === 0) {
await persistSession(session);
}
return response;
} catch (error) {
return {
content: [{
type: 'text',
text: `Error executing tool ${name}: ${error.message}`
}],
isError: true
};
}
});
// Resource listing
server.setRequestHandler('resources/list', async () => {
debug('Listing resources');
try {
const response = await callChittyConnect('/mcp/resources/list');
return response;
} catch (error) {
console.error('[ChittyConnect MCP] Failed to list resources:', error);
return { resources: [] };
}
});
// Resource reading
server.setRequestHandler('resources/read', async (request) => {
const { uri } = request.params;
debug(`Reading resource: ${uri}`);
try {
const response = await callChittyConnect(`/mcp/resources/read?uri=${encodeURIComponent(uri)}`);
return response;
} catch (error) {
return {
contents: [{
uri,
mimeType: 'text/plain',
text: `Error reading resource: ${error.message}`
}]
};
}
});
// Prompt listing
server.setRequestHandler('prompts/list', async () => {
debug('Listing prompts');
return {
prompts: [
{
name: 'chitty_analyze',
description: 'Analyze content with ContextConsciousness™',
arguments: [
{
name: 'content',
description: 'Content to analyze',
required: true
},
{
name: 'depth',
description: 'Analysis depth (quick, standard, deep)',
required: false
}
]
},
{
name: 'chitty_case_setup',
description: 'Set up a complete legal case with all entities',
arguments: [
{
name: 'case_type',
description: 'Type of legal case',
required: true
},
{
name: 'parties',
description: 'Parties involved in the case',
required: true
}
]
},
{
name: 'chitty_credential_workflow',
description: 'Secure credential provisioning workflow',
arguments: [
{
name: 'service',
description: 'Target service for credentials',
required: true
},
{
name: 'purpose',
description: 'Purpose of credential usage',
required: true
}
]
}
]
};
});
// Prompt execution
server.setRequestHandler('prompts/get', async (request) => {
const { name, arguments: args } = request.params;
debug(`Getting prompt: ${name}`);
const prompts = {
chitty_analyze: {
messages: [
{
role: 'system',
content: `You are using ChittyConnect's ContextConsciousness™ to analyze content.
Use the chitty_intelligence_analyze tool to perform deep analysis with entity extraction,
sentiment analysis, and contextual understanding. The analysis should consider the
broader ChittyOS ecosystem context and any relevant session history.`
},
{
role: 'user',
content: `Please analyze the following content with ${args.depth || 'standard'} depth:
${args.content}
Use ContextConsciousness™ to provide insights about entities, relationships, sentiment,
and any legal or financial implications.`
}
]
},
chitty_case_setup: {
messages: [
{
role: 'system',
content: `You are setting up a complete legal case in the ChittyOS ecosystem.
This involves creating ChittyIDs for all entities, establishing the case structure,
and preparing for evidence ingestion. Use the appropriate ChittyConnect tools in sequence.`
},
{
role: 'user',
content: `Please set up a ${args.case_type} case with the following parties:
${args.parties}
Steps to complete:
1. Mint ChittyIDs for all parties and entities
2. Create the case with chitty_case_create
3. Set up chronicle logging for audit trail
4. Prepare evidence ingestion structure
5. Provide a summary of the created case structure`
}
]
},
chitty_credential_workflow: {
messages: [
{
role: 'system',
content: `You are managing secure credential provisioning through ChittyConnect's
chittysecrets integration. Follow security best practices and validate all requests through
ContextConsciousness™ before retrieving or provisioning credentials.`
},
{
role: 'user',
content: `Please handle credential provisioning for:
Service: ${args.service}
Purpose: ${args.purpose}
Workflow:
1. Validate the request context using ContextConsciousness™
2. Check if credentials already exist using chitty_credential_audit
3. If needed, provision new credentials with appropriate scopes
4. Store audit trail in ChittyChronicle
5. Provide usage instructions for the credentials`
}
]
}
};
const prompt = prompts[name];
if (!prompt) {
throw new Error(`Unknown prompt: ${name}`);
}
return prompt;
});
// Session persistence
async function persistSession(session) {
if (!config.sessionPersistence) return;
debug(`Persisting session: ${session.id}`);
try {
await callChittyConnect('/mcp/session/persist', 'POST', {
sessionId: session.id,
toolHistory: session.toolHistory,
context: session.context,
platform: config.platform
});
} catch (error) {
console.error('[ChittyConnect MCP] Failed to persist session:', error);
}
}
// Streaming support (if enabled)
if (config.enableStreaming) {
server.setRequestHandler('streaming/start', async (request) => {
const { sessionId } = request.params;
debug(`Starting stream for session: ${sessionId}`);
// This would establish SSE connection to ChittyConnect
// Implementation depends on MCP SDK streaming support
return {
streamId: `stream-${sessionId}`,
url: `${config.chittyconnectUrl}/mcp/stream/${sessionId}`
};
});
}
// Sampling support (for advanced features)
server.setRequestHandler('sampling/sample', async (request) => {
const { messages, maxTokens = 1000 } = request.params;
debug('Sampling request received');
// Forward to ChittyConnect's AI endpoint
try {
const response = await callChittyConnect('/mcp/sampling/sample', 'POST', {
messages,
maxTokens,
sessionId: getOrCreateSession()
});
return response;
} catch (error) {
throw new Error(`Sampling failed: ${error.message}`);
}
});
// Graceful shutdown
process.on('SIGINT', async () => {
console.error('[ChittyConnect MCP] Shutting down...');
// Persist all active sessions
for (const [sessionId, session] of sessions) {
await persistSession(session);
}
process.exit(0);
});
// Start server
async function main() {
console.error('[ChittyConnect MCP] Starting server...');
console.error(`[ChittyConnect MCP] Platform: ${config.platform}`);
console.error(`[ChittyConnect MCP] API URL: ${config.chittyconnectUrl}`);
console.error(`[ChittyConnect MCP] Streaming: ${config.enableStreaming ? 'enabled' : 'disabled'}`);
console.error(`[ChittyConnect MCP] Session Persistence: ${config.sessionPersistence ? 'enabled' : 'disabled'}`);
// Test connection to ChittyConnect
try {
await callChittyConnect('/health');
console.error('[ChittyConnect MCP] Successfully connected to ChittyConnect API');
} catch (error) {
console.error('[ChittyConnect MCP] Warning: Could not connect to ChittyConnect API');
console.error('[ChittyConnect MCP] The server will start but may have limited functionality');
}
// Connect stdio transport
const transport = new StdioServerTransport();
await server.connect(transport);
console.error('[ChittyConnect MCP] Server ready for connections');
}
// Run the server
main().catch((error) => {
console.error('[ChittyConnect MCP] Fatal error:', error);
process.exit(1);
});