-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
682 lines (580 loc) · 24.7 KB
/
server.py
File metadata and controls
682 lines (580 loc) · 24.7 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
#!/usr/bin/env python3
"""Global MCP Server for searching Claude Code session history (v1.0).
Works across all projects. Auto-detects current project from CWD,
supports explicit project selection and cross-project search.
"""
import os
import sys
import json
import logging
import re
import subprocess
from datetime import datetime, timedelta, timezone
from pathlib import Path
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent
# Configuration
SESSIONS_DIR = Path(os.path.expanduser(os.getenv("CLAUDE_SESSIONS_DIR", "~/.claude/projects")))
# Logging
log_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "sessions.log")
logger = logging.getLogger("sessions-mcp")
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler(log_file, encoding="utf-8")
file_handler.setLevel(logging.DEBUG)
stderr_handler = logging.StreamHandler(sys.stderr)
stderr_handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
file_handler.setFormatter(formatter)
stderr_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(stderr_handler)
server = Server("sessions")
# ── Helpers ──────────────────────────────────────────────────────────────────
_SYSTEM_TAGS_RE = re.compile(
r'<(?:local-command-caveat|system-reminder|user-prompt-submit-hook|available-deferred-tools|command-name|command-message|command-args|local-command-stdout)[^>]*>.*?'
r'</(?:local-command-caveat|system-reminder|user-prompt-submit-hook|available-deferred-tools|command-name|command-message|command-args|local-command-stdout)>',
re.DOTALL,
)
def _strip_system_tags(text: str) -> str:
return _SYSTEM_TAGS_RE.sub('', text).strip()
def _path_to_project_name(path: str) -> str:
"""Convert filesystem path to Claude project directory name.
/home/user/projects/myapp -> -home-user-projects-myapp
"""
return path.replace("/", "-")
def _project_name_to_path(name: str) -> str:
"""Convert Claude project directory name back to filesystem path.
-home-user-projects-myapp -> /home/user/projects/myapp
"""
return "/" + name.lstrip("-").replace("-", "/") if name.startswith("-") else name
def _project_name_to_short(name: str) -> str:
"""Extract short project name for display.
-home-user-projects-myapp -> myapp
"""
parts = name.lstrip("-").split("-")
# Find index after "project" or "projects" keyword
for keyword in ("project", "projects"):
try:
idx = parts.index(keyword)
return "-".join(parts[idx + 1:]) if idx + 1 < len(parts) else name
except ValueError:
continue
return name
def _resolve_project(project: str | None) -> str | None:
"""Resolve project argument to a project directory name.
Accepts:
- None -> None (caller decides default)
- "myapp" -> finds matching project dir by short name
- "-home-user-projects-myapp" -> used as-is
- "/home/user/projects/myapp" -> converted to dir name
"""
if not project:
return None
# Full directory name
if (SESSIONS_DIR / project).is_dir():
return project
# Full filesystem path
if project.startswith("/"):
name = _path_to_project_name(project)
if (SESSIONS_DIR / name).is_dir():
return name
return None
# Short name — search for matching project (exact suffix)
for d in SESSIONS_DIR.iterdir():
if d.is_dir() and d.name.endswith(f"-{project}"):
return d.name
# Partial match
for d in SESSIONS_DIR.iterdir():
if d.is_dir() and project in d.name:
return d.name
return None
def _all_projects() -> list[str]:
"""List all project directory names, sorted by most recent session."""
if not SESSIONS_DIR.exists():
return []
return sorted(
[d.name for d in SESSIONS_DIR.iterdir() if d.is_dir() and list(d.glob("*.jsonl"))],
key=lambda n: max(
(f.stat().st_mtime for f in (SESSIONS_DIR / n).glob("*.jsonl")),
default=0,
),
reverse=True,
)
def _project_dir(project: str | None) -> Path:
resolved = _resolve_project(project)
if resolved:
return SESSIONS_DIR / resolved
return SESSIONS_DIR / "__none__"
def _session_files(project: str | None = None) -> list[Path]:
d = _project_dir(project)
if not d.exists():
return []
return sorted(d.glob("*.jsonl"), key=lambda f: f.stat().st_mtime, reverse=True)
def _git_dir_for_project(project_name: str) -> str | None:
"""Derive git directory from project name."""
path = _project_name_to_path(project_name)
if os.path.isdir(os.path.join(path, ".git")):
return path
return None
def _parse_messages(path: Path) -> list[dict]:
messages = []
with open(path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
except json.JSONDecodeError:
continue
if entry.get("type") not in ("user", "assistant"):
continue
msg = entry.get("message", {})
role = msg.get("role", entry.get("type"))
content = msg.get("content", "")
if isinstance(content, list):
text_parts = []
for block in content:
if isinstance(block, str):
text_parts.append(block)
elif isinstance(block, dict) and block.get("type") == "text":
text_parts.append(block.get("text", ""))
content = "\n".join(text_parts)
if not content.strip():
continue
ts = entry.get("timestamp")
messages.append({"role": role, "content": content, "timestamp": ts})
return messages
def _session_meta(path: Path) -> dict | None:
first_user_msg = None
msg_count = 0
first_ts = None
last_ts = None
with open(path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
except json.JSONDecodeError:
continue
if entry.get("type") not in ("user", "assistant"):
continue
msg_count += 1
ts = entry.get("timestamp")
if ts:
if first_ts is None:
first_ts = ts
last_ts = ts
if first_user_msg is None and entry.get("type") == "user":
msg = entry.get("message", {})
content = msg.get("content", "")
if isinstance(content, list):
for block in content:
if isinstance(block, str):
cleaned = _strip_system_tags(block)
if cleaned:
first_user_msg = cleaned[:200]
break
elif isinstance(block, dict) and block.get("type") == "text":
cleaned = _strip_system_tags(block.get("text", ""))
if cleaned:
first_user_msg = cleaned[:200]
break
elif isinstance(content, str):
cleaned = _strip_system_tags(content)
if cleaned:
first_user_msg = cleaned[:200]
if msg_count == 0:
return None
return {
"session_id": path.stem,
"date": first_ts or datetime.fromtimestamp(path.stat().st_mtime, tz=timezone.utc).isoformat(),
"first_message": first_user_msg or "(no text)",
"message_count": msg_count,
"first_ts": first_ts,
"last_ts": last_ts,
}
def _extract_content(entry: dict) -> str:
msg = entry.get("message", {})
content = msg.get("content", "")
if isinstance(content, list):
return " ".join(
b.get("text", "") if isinstance(b, dict) else str(b)
for b in content
)
return content
def _line_matches_words(line_lower: str, words: list[str]) -> bool:
return any(w in line_lower for w in words)
def _session_matches_words(path: Path, words: list[str]) -> bool:
found = set()
with open(path, "r", encoding="utf-8") as f:
for line in f:
ll = line.lower()
for w in words:
if w in ll:
found.add(w)
if len(found) == len(words):
return True
return False
def _search_in_project(project_name: str, words: list[str], limit: int) -> list[dict]:
"""Search sessions in a single project."""
results = []
for path in _session_files(project_name):
if not _session_matches_words(path, words):
continue
matches = []
first_user_msg = None
first_ts = None
with open(path, "r", encoding="utf-8") as f:
for line in f:
line_lower = line.lower()
if first_user_msg is None:
try:
entry = json.loads(line.strip())
if entry.get("type") == "user":
c = _strip_system_tags(_extract_content(entry))
if c:
first_user_msg = c[:150]
first_ts = entry.get("timestamp")
except json.JSONDecodeError:
pass
if _line_matches_words(line_lower, words):
try:
entry = json.loads(line.strip())
if entry.get("type") in ("user", "assistant"):
content = _extract_content(entry)
content_lower = content.lower()
for w in words:
idx = content_lower.find(w)
if idx >= 0:
start = max(0, idx - 80)
end = min(len(content), idx + len(w) + 80)
matches.append(f" ...{content[start:end]}...")
break
except json.JSONDecodeError:
pass
if matches:
results.append({
"project": _project_name_to_short(project_name),
"project_full": project_name,
"session_id": path.stem,
"date": first_ts or "?",
"first_message": first_user_msg or "?",
"match_count": len(matches),
"snippets": matches[:3],
})
if len(results) >= limit:
break
return results
# ── Tools ────────────────────────────────────────────────────────────────────
TOOLS = [
Tool(
name="sessions_projects",
description="GLOBAL: List ALL projects that have Claude Code session history. This server has access to sessions from ALL projects, not just the current one.",
inputSchema={
"type": "object",
"properties": {},
},
),
Tool(
name="sessions_list",
description="GLOBAL: List recent Claude Code sessions from ANY project.",
inputSchema={
"type": "object",
"properties": {
"project": {"type": "string", "description": "Project short name, full dir name, or filesystem path. Omit for current project."},
"days": {"type": "integer", "description": "How many days back to look (default: 30)", "default": 30},
"limit": {"type": "integer", "description": "Max sessions to return (default: 20)", "default": 20},
},
},
),
Tool(
name="sessions_search",
description="GLOBAL: Search Claude Code sessions by keyword across ANY or ALL projects. Has access to conversation history from all projects.",
inputSchema={
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search query (words are AND-matched across session)"},
"project": {"type": "string", "description": "Project short name, or '*'/'all' to search ALL projects. Omit for current project."},
"limit": {"type": "integer", "description": "Max results (default: 10)", "default": 10},
},
"required": ["query"],
},
),
Tool(
name="sessions_by_file",
description="GLOBAL: Find sessions that mention a specific file path, across ANY or ALL projects.",
inputSchema={
"type": "object",
"properties": {
"file_path": {"type": "string", "description": "File path to search for"},
"project": {"type": "string", "description": "Project short name, or '*'/'all' to search ALL projects. Omit for current project."},
"limit": {"type": "integer", "description": "Max results (default: 10)", "default": 10},
},
"required": ["file_path"],
},
),
Tool(
name="session_summary",
description="GLOBAL: Get session overview from ANY project: dates, message count, first/last user message, files mentioned.",
inputSchema={
"type": "object",
"properties": {
"session_id": {"type": "string", "description": "Session UUID"},
"project": {"type": "string", "description": "Project short name. Omit to auto-detect."},
},
"required": ["session_id"],
},
),
Tool(
name="session_diff",
description="GLOBAL: Get git commits made during a session's time window, from ANY project.",
inputSchema={
"type": "object",
"properties": {
"session_id": {"type": "string", "description": "Session UUID"},
"project": {"type": "string", "description": "Project short name. Omit to auto-detect."},
},
"required": ["session_id"],
},
),
Tool(
name="session_messages",
description="GLOBAL: Get raw messages from a session in ANY project, optionally filtered by query.",
inputSchema={
"type": "object",
"properties": {
"session_id": {"type": "string", "description": "Session UUID"},
"query": {"type": "string", "description": "Optional filter query"},
"limit": {"type": "integer", "description": "Max messages (default: 50)", "default": 50},
"project": {"type": "string", "description": "Project short name. Omit to auto-detect."},
},
"required": ["session_id"],
},
),
]
@server.list_tools()
async def list_tools() -> list[Tool]:
return TOOLS
@server.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
logger.info(f"Tool call: {name}({json.dumps(arguments, ensure_ascii=False)[:200]})")
try:
result = await _dispatch(name, arguments)
logger.info(f"Tool {name} OK, {len(result)} chars")
return [TextContent(type="text", text=result)]
except Exception as e:
logger.exception(f"Tool {name} failed")
return [TextContent(type="text", text=f"Error: {e}")]
async def _dispatch(name: str, args: dict) -> str:
if name == "sessions_projects":
return _handle_projects(args)
elif name == "sessions_list":
return _handle_sessions_list(args)
elif name == "sessions_search":
return _handle_sessions_search(args)
elif name == "sessions_by_file":
return _handle_sessions_by_file(args)
elif name == "session_summary":
return _handle_session_summary(args)
elif name == "session_diff":
return _handle_session_diff(args)
elif name == "session_messages":
return _handle_session_messages(args)
else:
return f"Unknown tool: {name}"
# ── Tool implementations ─────────────────────────────────────────────────────
def _handle_projects(args: dict) -> str:
projects = _all_projects()
if not projects:
return "No projects found."
lines = []
for p in projects:
short = _project_name_to_short(p)
session_count = len(list((SESSIONS_DIR / p).glob("*.jsonl")))
lines.append(f"- **{short}** ({session_count} sessions) `{p}`")
return "\n".join(lines)
def _handle_sessions_list(args: dict) -> str:
project = args.get("project")
days = args.get("days", 30)
limit = args.get("limit", 20)
cutoff = datetime.now(tz=timezone.utc) - timedelta(days=days)
resolved = _resolve_project(project)
if project and not resolved:
return f"Project '{project}' not found. Use sessions_projects to list available projects."
results = []
for path in _session_files(resolved):
mtime = datetime.fromtimestamp(path.stat().st_mtime, tz=timezone.utc)
if mtime < cutoff:
break
meta = _session_meta(path)
if meta:
results.append(meta)
if len(results) >= limit:
break
if not results:
return "No sessions found."
lines = []
for s in results:
lines.append(f"**{s['session_id']}**\n Date: {s['date']}\n Messages: {s['message_count']}\n First: {s['first_message']}")
return "\n\n".join(lines)
def _is_all_projects(project: str | None) -> bool:
return project in ("*", "all")
def _handle_sessions_search(args: dict) -> str:
query = args["query"]
project = args.get("project")
limit = args.get("limit", 10)
words = [w.lower() for w in query.split() if w.strip()]
if not words:
return "Empty query."
if _is_all_projects(project):
# Search across all projects — collect from each, then sort by date
all_results = []
per_project_limit = max(limit, 5)
for proj_name in _all_projects():
results = _search_in_project(proj_name, words, per_project_limit)
all_results.extend(results)
all_results.sort(key=lambda r: r.get("date", ""), reverse=True)
all_results = all_results[:limit]
if not all_results:
return f"No sessions found matching '{query}' in any project."
lines = []
for r in all_results:
snippets = "\n".join(r["snippets"])
lines.append(f"**[{r['project']}] {r['session_id']}**\n Date: {r['date']}\n First: {r['first_message']}\n Matches: {r['match_count']}\n{snippets}")
return "\n\n".join(lines)
else:
resolved = _resolve_project(project)
if project and not resolved:
return f"Project '{project}' not found. Use sessions_projects to list available projects."
results = _search_in_project(resolved, words, limit)
if not results:
return f"No sessions found matching '{query}'."
lines = []
for r in results:
snippets = "\n".join(r["snippets"])
lines.append(f"**{r['session_id']}**\n Date: {r['date']}\n First: {r['first_message']}\n Matches: {r['match_count']}\n{snippets}")
return "\n\n".join(lines)
def _handle_sessions_by_file(args: dict) -> str:
file_path = args["file_path"]
basename = os.path.basename(file_path)
return _handle_sessions_search({
"query": basename,
"project": args.get("project"),
"limit": args.get("limit", 10),
})
def _find_session(session_id: str, project: str | None) -> Path | None:
"""Find session file, searching in specified project or all projects."""
resolved = _resolve_project(project)
if resolved:
path = SESSIONS_DIR / resolved / f"{session_id}.jsonl"
if path.exists():
return path
return None
# No project specified — search all projects
for proj_name in _all_projects():
path = SESSIONS_DIR / proj_name / f"{session_id}.jsonl"
if path.exists():
return path
return None
def _handle_session_summary(args: dict) -> str:
session_id = args["session_id"]
project = args.get("project")
path = _find_session(session_id, project)
if not path:
return f"Session {session_id} not found."
messages = _parse_messages(path)
if not messages:
return "No messages in session."
file_pattern = re.compile(r'(?:app/|mcp-|mcp/|templates/|tests/|docs/|src/)[\w/._-]+\.(?:py|html|js|ts|tsx|css|json|md|yaml|yml|sh|txt)')
files_mentioned = set()
for m in messages:
files_mentioned.update(file_pattern.findall(m["content"]))
user_msgs = [m for m in messages if m["role"] == "user"]
assistant_msgs = [m for m in messages if m["role"] == "assistant"]
first_msg = user_msgs[0]["content"][:300] if user_msgs else "(no user messages)"
last_msg = user_msgs[-1]["content"][:300] if user_msgs else ""
project_name = path.parent.name
parts = [
f"**Session:** {session_id}",
f"**Project:** {_project_name_to_short(project_name)}",
f"**Date:** {messages[0].get('timestamp', '?')} → {messages[-1].get('timestamp', '?')}",
f"**Messages:** {len(messages)} total ({len(user_msgs)} user, {len(assistant_msgs)} assistant)",
f"\n**First user message:**\n{first_msg}",
]
if last_msg and len(user_msgs) > 1:
parts.append(f"\n**Last user message:**\n{last_msg}")
if files_mentioned:
parts.append(f"\n**Files mentioned ({len(files_mentioned)}):**\n" + "\n".join(f"- {f}" for f in sorted(files_mentioned)[:30]))
return "\n".join(parts)
def _handle_session_diff(args: dict) -> str:
session_id = args["session_id"]
project = args.get("project")
path = _find_session(session_id, project)
if not path:
return f"Session {session_id} not found."
project_name = path.parent.name
git_dir = _git_dir_for_project(project_name)
if not git_dir:
return f"No git repository found for project '{_project_name_to_short(project_name)}' (tried: {_project_name_to_path(project_name)})"
first_ts = None
last_ts = None
with open(path, "r", encoding="utf-8") as f:
for line in f:
try:
entry = json.loads(line.strip())
ts = entry.get("timestamp")
if ts:
if first_ts is None:
first_ts = ts
last_ts = ts
except json.JSONDecodeError:
continue
if not first_ts or not last_ts:
return "No timestamps found in session."
try:
result = subprocess.run(
["git", "-C", git_dir, "log",
f"--after={first_ts}", f"--before={last_ts}",
"--format=%h %ai %s", "--stat"],
capture_output=True, text=True, timeout=10,
)
output = result.stdout.strip()
if not output:
return f"No commits found between {first_ts} and {last_ts}."
return output
except Exception as e:
return f"Git log failed: {e}"
def _handle_session_messages(args: dict) -> str:
session_id = args["session_id"]
project = args.get("project")
query = args.get("query")
limit = args.get("limit", 50)
path = _find_session(session_id, project)
if not path:
return f"Session {session_id} not found."
messages = _parse_messages(path)
if query:
words = [w.lower() for w in query.split() if w.strip()]
messages = [m for m in messages if any(w in m["content"].lower() for w in words)]
messages = messages[:limit]
if not messages:
return "No messages found."
lines = []
for m in messages:
content = m["content"]
if len(content) > 500:
content = content[:500] + "..."
lines.append(f"[{m['role']}] ({m.get('timestamp', '?')}):\n{content}")
return "\n\n---\n\n".join(lines)
# ── Main ─────────────────────────────────────────────────────────────────────
async def main():
logger.info("Sessions MCP server v1.0 starting (global)")
async with stdio_server() as (read_stream, write_stream):
await server.run(read_stream, write_stream, server.create_initialization_options())
if __name__ == "__main__":
import asyncio
asyncio.run(main())