-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmcp-server-redis.py
More file actions
693 lines (596 loc) · 31.1 KB
/
Copy pathmcp-server-redis.py
File metadata and controls
693 lines (596 loc) · 31.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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#!/usr/bin/env python3
"""
Complete SplitMind Agent Communication MCP Server with Redis Backend
Implements full A2AMCP API specification
"""
import asyncio
import json
import logging
import os
from datetime import datetime, timedelta
from typing import Dict, List, Any, Optional, Union
import redis.asyncio as redis
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('splitmind-mcp')
class AgentCommunicationServer:
"""Complete MCP Server implementing full A2AMCP API with Redis backend"""
def __init__(self):
self.server = Server("splitmind-coordination")
self.redis_client: Optional[redis.Redis] = None
self._setup_tools()
async def initialize(self):
"""Initialize Redis connection and wait for readiness"""
redis_url = os.getenv('REDIS_URL', 'redis://localhost:6379')
logger.info(f"Connecting to Redis at: {redis_url}")
self.redis_client = redis.from_url(redis_url, decode_responses=True)
# Wait for Redis to be ready
max_retries = 30
for i in range(max_retries):
try:
await self.redis_client.ping()
logger.info("Connected to Redis successfully")
break
except Exception as e:
if i < max_retries - 1:
logger.info(f"Waiting for Redis... ({i+1}/{max_retries})")
await asyncio.sleep(1)
else:
logger.error(f"Failed to connect to Redis after {max_retries} attempts: {e}")
raise
async def cleanup(self):
"""Clean up Redis connection"""
if self.redis_client:
await self.redis_client.aclose()
def _get_key(self, project_id: str, *parts: str) -> str:
"""Generate Redis key with proper namespace"""
return f"splitmind:{project_id}:{':'.join(parts)}"
def _response(self, status: str, message: str, data: Any = None) -> str:
"""Generate A2AMCP response format"""
response = {
"status": status,
"message": message,
"data": data or {}
}
return json.dumps(response)
def _setup_tools(self):
"""Register all MCP tools according to A2AMCP API specification"""
@self.server.list_tools()
async def list_tools() -> List[Tool]:
return [
# Agent Management
Tool(
name="register_agent",
description="Register an agent for a specific project",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"},
"task_id": {"type": "string"},
"branch": {"type": "string"},
"description": {"type": "string"}
},
"required": ["project_id", "session_name", "task_id", "branch", "description"]
}
),
Tool(
name="unregister_agent",
description="Unregister agent and clean up",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"}
},
"required": ["project_id", "session_name"]
}
),
Tool(
name="heartbeat",
description="Send periodic heartbeat",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"}
},
"required": ["project_id", "session_name"]
}
),
Tool(
name="list_active_agents",
description="List all active agents in a project",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"}
},
"required": ["project_id"]
}
),
# Todo Management
Tool(
name="add_todo",
description="Add a todo item",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"},
"task": {"type": "string"},
"priority": {"type": "string", "enum": ["high", "medium", "low"], "default": "medium"}
},
"required": ["project_id", "session_name", "task"]
}
),
Tool(
name="update_todo",
description="Update todo status",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"},
"todo_id": {"type": "string"},
"status": {"type": "string", "enum": ["pending", "in_progress", "completed", "cancelled"]}
},
"required": ["project_id", "session_name", "todo_id", "status"]
}
),
Tool(
name="get_my_todos",
description="Get agent's todos",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"}
},
"required": ["project_id", "session_name"]
}
),
# Communication
Tool(
name="query_agent",
description="Send query to another agent",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"},
"target_session": {"type": "string"},
"query": {"type": "string"}
},
"required": ["project_id", "session_name", "target_session", "query"]
}
),
Tool(
name="check_messages",
description="Check and retrieve messages",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"}
},
"required": ["project_id", "session_name"]
}
),
Tool(
name="respond_to_query",
description="Respond to a specific query",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"},
"query_id": {"type": "string"},
"response": {"type": "string"}
},
"required": ["project_id", "session_name", "query_id", "response"]
}
),
# File Coordination
Tool(
name="announce_file_change",
description="Lock a file before editing",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"},
"file_path": {"type": "string"},
"operation": {"type": "string", "enum": ["create", "modify", "delete"]}
},
"required": ["project_id", "session_name", "file_path", "operation"]
}
),
Tool(
name="release_file_lock",
description="Release file lock after editing",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"},
"file_path": {"type": "string"}
},
"required": ["project_id", "session_name", "file_path"]
}
),
Tool(
name="get_recent_changes",
description="Get recent file changes",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"minutes": {"type": "integer", "default": 30}
},
"required": ["project_id"]
}
),
# Shared Definitions
Tool(
name="register_interface",
description="Share a type/interface definition",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"},
"name": {"type": "string"},
"definition": {"type": "string"},
"description": {"type": "string"}
},
"required": ["project_id", "session_name", "name", "definition"]
}
),
Tool(
name="query_interface",
description="Get shared interface definition",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"name": {"type": "string"}
},
"required": ["project_id", "name"]
}
),
Tool(
name="list_interfaces",
description="List all shared interfaces",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"}
},
"required": ["project_id"]
}
),
# Task Completion
Tool(
name="mark_task_completed",
description="Mark a task as completed",
inputSchema={
"type": "object",
"properties": {
"project_id": {"type": "string"},
"session_name": {"type": "string"},
"task_id": {"type": "string"}
},
"required": ["project_id", "session_name", "task_id"]
}
)
]
@self.server.call_tool()
async def call_tool(name: str, arguments: dict) -> List[TextContent]:
try:
result = ""
if name == "register_agent":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
task_id = arguments["task_id"]
branch = arguments["branch"]
description = arguments["description"]
agent_data = {
"task_id": task_id,
"branch": branch,
"description": description,
"status": "active",
"started_at": datetime.now().isoformat(),
"project_id": project_id
}
agents_key = self._get_key(project_id, "agents")
await self.redis_client.hset(agents_key, session_name, json.dumps(agent_data))
heartbeat_key = self._get_key(project_id, "heartbeat")
await self.redis_client.hset(heartbeat_key, session_name, datetime.now().isoformat())
result = self._response("success", f"Agent {session_name} registered successfully", {
"agent_id": session_name,
"project_id": project_id
})
elif name == "unregister_agent":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
# Clean up agent data
agents_key = self._get_key(project_id, "agents")
await self.redis_client.hdel(agents_key, session_name)
heartbeat_key = self._get_key(project_id, "heartbeat")
await self.redis_client.hdel(heartbeat_key, session_name)
# Clean up todos, messages, file locks
todos_key = self._get_key(project_id, "todos", session_name)
await self.redis_client.delete(todos_key)
messages_key = self._get_key(project_id, "messages", session_name)
await self.redis_client.delete(messages_key)
result = self._response("success", f"Agent {session_name} unregistered successfully")
elif name == "heartbeat":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
heartbeat_key = self._get_key(project_id, "heartbeat")
await self.redis_client.hset(heartbeat_key, session_name, datetime.now().isoformat())
result = self._response("success", "Heartbeat recorded")
elif name == "list_active_agents":
project_id = arguments["project_id"]
agents_key = self._get_key(project_id, "agents")
agents = await self.redis_client.hgetall(agents_key)
active_agents = []
for session, data in agents.items():
agent_info = json.loads(data)
active_agents.append({
"session_name": session,
"task_id": agent_info["task_id"],
"description": agent_info["description"],
"branch": agent_info["branch"]
})
result = self._response("success", f"Found {len(active_agents)} active agents", {
"agents": active_agents
})
elif name == "add_todo":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
task = arguments["task"]
priority = arguments.get("priority", "medium")
todo_id = f"todo_{int(datetime.now().timestamp() * 1000)}"
todo_data = {
"id": todo_id,
"task": task,
"priority": priority,
"status": "pending",
"created_at": datetime.now().isoformat()
}
todos_key = self._get_key(project_id, "todos", session_name)
await self.redis_client.hset(todos_key, todo_id, json.dumps(todo_data))
result = self._response("success", "Todo added successfully", {
"todo_id": todo_id
})
elif name == "update_todo":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
todo_id = arguments["todo_id"]
status = arguments["status"]
todos_key = self._get_key(project_id, "todos", session_name)
todo_data = await self.redis_client.hget(todos_key, todo_id)
if todo_data:
todo = json.loads(todo_data)
todo["status"] = status
todo["updated_at"] = datetime.now().isoformat()
await self.redis_client.hset(todos_key, todo_id, json.dumps(todo))
result = self._response("success", f"Todo {todo_id} updated to {status}")
else:
result = self._response("error", f"Todo {todo_id} not found")
elif name == "get_my_todos":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
todos_key = self._get_key(project_id, "todos", session_name)
todos = await self.redis_client.hgetall(todos_key)
todo_list = []
for todo_id, todo_data in todos.items():
todo = json.loads(todo_data)
todo_list.append(todo)
result = self._response("success", f"Retrieved {len(todo_list)} todos", {
"todos": todo_list
})
elif name == "mark_task_completed":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
task_id = arguments["task_id"]
completion_key = self._get_key(project_id, "completed_tasks")
completion_data = {
"task_id": task_id,
"session_name": session_name,
"completed_at": datetime.now().isoformat()
}
await self.redis_client.hset(completion_key, task_id, json.dumps(completion_data))
result = self._response("success", f"Task {task_id} marked as completed")
elif name == "get_recent_changes":
project_id = arguments["project_id"]
minutes = arguments.get("minutes", 30)
changes_key = self._get_key(project_id, "recent_changes")
all_changes = await self.redis_client.lrange(changes_key, 0, -1)
cutoff_time = datetime.now() - timedelta(minutes=minutes)
recent_changes = []
for change_str in all_changes:
change = json.loads(change_str)
change_time = datetime.fromisoformat(change["timestamp"])
if change_time >= cutoff_time:
recent_changes.append(change)
result = self._response("success", f"Found {len(recent_changes)} changes in last {minutes} minutes", {
"changes": recent_changes
})
elif name == "query_agent":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
target_session = arguments["target_session"]
query = arguments["query"]
# Check if target agent exists
agents_key = self._get_key(project_id, "agents")
if not await self.redis_client.hexists(agents_key, target_session):
result = self._response("error", f"Agent {target_session} not found")
else:
query_id = f"query_{int(datetime.now().timestamp() * 1000)}"
query_data = {
"id": query_id,
"from": session_name,
"query": query,
"timestamp": datetime.now().isoformat()
}
messages_key = self._get_key(project_id, "messages", target_session)
await self.redis_client.rpush(messages_key, json.dumps(query_data))
result = self._response("success", f"Query sent to {target_session}", {
"query_id": query_id
})
elif name == "check_messages":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
messages_key = self._get_key(project_id, "messages", session_name)
messages = await self.redis_client.lrange(messages_key, 0, -1)
# Clear the queue after reading
await self.redis_client.delete(messages_key)
message_list = []
for msg_str in messages:
message_list.append(json.loads(msg_str))
result = self._response("success", f"Retrieved {len(message_list)} messages", {
"messages": message_list
})
elif name == "respond_to_query":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
query_id = arguments["query_id"]
response = arguments["response"]
# Extract the original sender from query_id format
# Assuming query_id contains info about the sender
response_data = {
"id": f"response_{query_id}",
"from": session_name,
"response": response,
"query_id": query_id,
"timestamp": datetime.now().isoformat()
}
# Store response (would need to track original sender)
result = self._response("success", "Response sent", {
"response_id": response_data["id"]
})
elif name == "announce_file_change":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
file_path = arguments["file_path"]
operation = arguments["operation"]
locks_key = self._get_key(project_id, "file_locks")
existing_lock = await self.redis_client.hget(locks_key, file_path)
if existing_lock:
lock_info = json.loads(existing_lock)
if lock_info["session"] != session_name:
result = self._response("error", f"File is locked by {lock_info['session']}", {
"lock_info": lock_info
})
else:
result = self._response("success", "File already locked by you")
else:
lock_data = {
"session": session_name,
"operation": operation,
"locked_at": datetime.now().isoformat()
}
await self.redis_client.hset(locks_key, file_path, json.dumps(lock_data))
# Add to recent changes
changes_key = self._get_key(project_id, "recent_changes")
change_record = {
"session": session_name,
"file_path": file_path,
"operation": operation,
"timestamp": datetime.now().isoformat()
}
await self.redis_client.lpush(changes_key, json.dumps(change_record))
await self.redis_client.ltrim(changes_key, 0, 99) # Keep last 100
result = self._response("success", f"File {file_path} locked for {operation}")
elif name == "release_file_lock":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
file_path = arguments["file_path"]
locks_key = self._get_key(project_id, "file_locks")
existing_lock = await self.redis_client.hget(locks_key, file_path)
if not existing_lock:
result = self._response("error", "File is not locked")
else:
lock_info = json.loads(existing_lock)
if lock_info["session"] != session_name:
result = self._response("error", f"File is locked by {lock_info['session']}, not you")
else:
await self.redis_client.hdel(locks_key, file_path)
result = self._response("success", f"File {file_path} lock released")
elif name == "register_interface":
project_id = arguments["project_id"]
session_name = arguments["session_name"]
name_param = arguments["name"]
definition = arguments["definition"]
description = arguments.get("description", "")
interfaces_key = self._get_key(project_id, "interfaces")
interface_data = {
"definition": definition,
"description": description,
"registered_by": session_name,
"timestamp": datetime.now().isoformat()
}
await self.redis_client.hset(interfaces_key, name_param, json.dumps(interface_data))
result = self._response("success", f"Interface {name_param} registered")
elif name == "query_interface":
project_id = arguments["project_id"]
name_param = arguments["name"]
interfaces_key = self._get_key(project_id, "interfaces")
interface_data = await self.redis_client.hget(interfaces_key, name_param)
if interface_data:
interface = json.loads(interface_data)
result = self._response("success", f"Found interface {name_param}", {
"interface": interface
})
else:
# Try to find similar names
all_interfaces = await self.redis_client.hkeys(interfaces_key)
similar = [n for n in all_interfaces if name_param.lower() in n.lower()]
result = self._response("error", f"Interface {name_param} not found", {
"similar": similar
})
elif name == "list_interfaces":
project_id = arguments["project_id"]
interfaces_key = self._get_key(project_id, "interfaces")
all_interfaces = await self.redis_client.hgetall(interfaces_key)
interfaces_list = {}
for name, data in all_interfaces.items():
interfaces_list[name] = json.loads(data)
result = self._response("success", f"Found {len(interfaces_list)} interfaces", {
"interfaces": interfaces_list
})
else:
result = self._response("error", f"Tool '{name}' not yet implemented")
return [TextContent(type="text", text=result)]
except Exception as e:
logger.error(f"Error calling tool {name}: {e}")
error_response = self._response("error", f"Tool execution failed: {str(e)}")
return [TextContent(type="text", text=error_response)]
async def run(self):
"""Run the MCP server"""
logger.info("Starting Complete SplitMind Agent Communication Server with Redis")
await self.initialize()
try:
async with stdio_server() as (read_stream, write_stream):
await self.server.run(
read_stream,
write_stream,
self.server.create_initialization_options()
)
finally:
await self.cleanup()
async def run_server():
"""Run the server"""
server = AgentCommunicationServer()
await server.run()
if __name__ == "__main__":
asyncio.run(run_server())