-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathserver.py
More file actions
625 lines (557 loc) · 24.7 KB
/
Copy pathserver.py
File metadata and controls
625 lines (557 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
"""
CodeWiki MCP Server.
Provides two sets of tools:
**Fine-grained tools (IDE-driven, zero LLM config):**
- ``analyze_repo`` — Parse a repo and build a dependency graph (session-based)
- ``read_code_components`` — Write component source code to workspace files
- ``write_doc_file`` — Create a documentation .md file with Mermaid validation
- ``edit_doc_file`` — Edit a documentation file (str_replace / insert / undo)
- ``save_module_tree`` — Persist IDE agent's module clustering
- ``get_processing_order`` — Get leaf-first documentation order
- ``get_prompt`` — Retrieve CodeWiki's prompt templates
- ``close_session`` — Clean up a session and workspace files
Large analysis results (component index, source code, processing order) are
written to workspace files on disk. The IDE agent reads these files directly
instead of receiving large payloads through the MCP stdio channel.
**Legacy tools (require CodeWiki LLM config):**
- ``generate_docs`` — Full documentation generation (black-box)
- ``get_module_tree`` — Retrieve existing module clustering
Usage:
python -m codewiki.mcp.server
# Cursor / Claude Desktop config:
{
"mcpServers": {
"codewiki": {
"command": "python",
"args": ["-m", "codewiki.mcp.server"]
}
}
}
"""
import asyncio
import json
import logging
import os
from pathlib import Path
from typing import Any
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import TextContent, Tool
from codewiki.mcp.session import SessionState, SessionStore
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Global session store (lives for the lifetime of the MCP server process)
# ---------------------------------------------------------------------------
_store = SessionStore()
# ---------------------------------------------------------------------------
# MCP Server instance
# ---------------------------------------------------------------------------
server = Server("codewiki")
# ===================================================================
# Tool definitions
# ===================================================================
def _fine_grained_tools() -> list[Tool]:
"""Return the zero-config, IDE-driven tool set."""
return [
Tool(
name="analyze_repo",
description=(
"Analyze a code repository's structure, dependencies, and components "
"using Tree-sitter AST parsing. No LLM required. "
"Writes the full component index, leaf nodes, and language stats to "
"workspace files on disk, and returns file paths plus a compact summary. "
"Read the workspace files for complete data. "
"This is the entry point for the wiki generation pipeline. "
"After calling this, use get_prompt('cluster') to learn clustering rules, "
"then save_module_tree to persist your grouping. "
"INCREMENTAL UPDATE: If docs already exist in output_dir (metadata.json + "
"module_tree.json), the response includes a 'changes' field showing which "
"files changed and which modules need updating."
),
inputSchema={
"type": "object",
"properties": {
"repo_path": {
"type": "string",
"description": "Absolute path to the repository to analyze",
},
"output_dir": {
"type": "string",
"description": "Output directory for generated docs (default: <repo>/docs)",
},
"include_patterns": {
"type": "string",
"description": "Comma-separated file patterns to include (e.g., '*.py,*.js')",
},
"exclude_patterns": {
"type": "string",
"description": "Comma-separated patterns to exclude (e.g., '*test*,*spec*')",
},
"use_gitignore": {
"type": "boolean",
"description": "Apply Git ignore rules before analysis (default: true)",
"default": True,
},
},
"required": ["repo_path"],
},
),
Tool(
name="read_code_components",
description=(
"Write the source code for a list of component IDs to workspace files. "
"Component IDs have the form 'file_path::ComponentName'. "
"Each component's full source is written to an individual .src file "
"in the session's sources/ directory. Returns file paths — no truncation."
),
inputSchema={
"type": "object",
"properties": {
"session_id": {
"type": "string",
"description": "Session ID from analyze_repo",
},
"component_ids": {
"type": "array",
"items": {"type": "string"},
"description": "List of component IDs to read",
},
},
"required": ["session_id", "component_ids"],
},
),
Tool(
name="write_doc_file",
description=(
"Create a new markdown documentation file in the output directory. "
"Automatically validates Mermaid diagrams after writing."
),
inputSchema={
"type": "object",
"properties": {
"session_id": {
"type": "string",
"description": "Session ID from analyze_repo",
},
"filename": {
"type": "string",
"description": "Filename for the doc (e.g., 'auth_module.md')",
},
"content": {
"type": "string",
"description": "Markdown content to write",
},
},
"required": ["session_id", "filename", "content"],
},
),
Tool(
name="edit_doc_file",
description=(
"Edit an existing documentation file. Supports str_replace (find-and-replace), "
"insert (add text at a line), and undo (revert last edit). "
"Automatically validates Mermaid diagrams after editing."
),
inputSchema={
"type": "object",
"properties": {
"session_id": {
"type": "string",
"description": "Session ID from analyze_repo",
},
"filename": {
"type": "string",
"description": "Filename of the doc to edit",
},
"command": {
"type": "string",
"enum": ["str_replace", "insert", "undo"],
"description": "Edit command to run",
},
"old_str": {
"type": "string",
"description": "String to find (required for str_replace)",
},
"new_str": {
"type": "string",
"description": "Replacement string (for str_replace/insert)",
},
"insert_line": {
"type": "integer",
"description": "Line number for insert (0-indexed)",
},
},
"required": ["session_id", "filename", "command"],
},
),
Tool(
name="save_module_tree",
description=(
"Save the IDE agent's module clustering result. "
"Accepts a JSON module tree and persists it to disk. "
"Computes the leaf-first processing order and writes it to a workspace file. "
"Returns the file path for the processing order."
),
inputSchema={
"type": "object",
"properties": {
"session_id": {
"type": "string",
"description": "Session ID from analyze_repo",
},
"module_tree": {
"type": "object",
"description": (
"Module tree dict. Each key is a module name with value "
"{'components': [component_ids], 'children': {nested modules}}"
),
},
},
"required": ["session_id", "module_tree"],
},
),
Tool(
name="get_processing_order",
description=(
"Compute and write the leaf-first processing order to a workspace file. "
"Returns the file path. Process leaf modules (is_leaf=true) before parent modules."
),
inputSchema={
"type": "object",
"properties": {
"session_id": {
"type": "string",
"description": "Session ID from analyze_repo",
},
},
"required": ["session_id"],
},
),
Tool(
name="get_prompt",
description=(
"Retrieve CodeWiki's prompt templates for each pipeline stage. "
"Available types: cluster, system_complex, system_leaf, user, "
"overview_module, overview_repo. Optionally pass variables to "
"fill in template placeholders."
),
inputSchema={
"type": "object",
"properties": {
"prompt_type": {
"type": "string",
"enum": [
"cluster",
"system_complex",
"system_leaf",
"user",
"overview_module",
"overview_repo",
],
"description": "Which prompt template to retrieve",
},
"variables": {
"type": "object",
"description": "Optional template variables to fill in",
},
},
"required": ["prompt_type"],
},
),
Tool(
name="close_session",
description="Close and clean up an analysis session to free memory.",
inputSchema={
"type": "object",
"properties": {
"session_id": {
"type": "string",
"description": "Session ID to close",
},
},
"required": ["session_id"],
},
),
]
def _legacy_tools() -> list[Tool]:
"""Return the legacy tools that require CodeWiki LLM configuration."""
return [
Tool(
name="generate_docs",
description=(
"[LEGACY — requires 'codewiki config set' first] "
"Generate full documentation for a repository in one shot. "
"For IDE-driven generation, use the fine-grained tools instead."
),
inputSchema={
"type": "object",
"properties": {
"repo_path": {
"type": "string",
"description": "Absolute path to the repository to document",
},
"output_dir": {
"type": "string",
"description": "Output directory for generated docs (default: ./docs)",
"default": "docs",
},
"doc_type": {
"type": "string",
"enum": ["api", "architecture", "user-guide", "developer"],
"description": "Type of documentation to generate",
},
"include_patterns": {
"type": "string",
"description": "Comma-separated file patterns to include",
},
"exclude_patterns": {
"type": "string",
"description": "Comma-separated patterns to exclude",
},
"use_gitignore": {
"type": "boolean",
"description": "Apply Git ignore rules before analysis (default: true)",
"default": True,
},
},
"required": ["repo_path"],
},
),
Tool(
name="get_module_tree",
description="Get the existing module clustering tree for a repository.",
inputSchema={
"type": "object",
"properties": {
"repo_path": {
"type": "string",
"description": "Absolute path to the repository",
},
"output_dir": {
"type": "string",
"description": "Directory containing generated docs (default: ./docs)",
"default": "docs",
},
},
"required": ["repo_path"],
},
),
]
# ===================================================================
# Tool dispatch
# ===================================================================
@server.list_tools()
async def list_tools() -> list[Tool]:
"""List all available CodeWiki MCP tools."""
return _fine_grained_tools() + _legacy_tools()
@server.call_tool()
async def call_tool(name: str, arguments: dict[str, Any]) -> list[TextContent]:
"""Route tool calls to the appropriate handler."""
try:
# --- Fine-grained tools (no LLM config needed) ---
# Synchronous handlers run via asyncio.to_thread() so they never
# block the event loop (which would hang the MCP stdio server).
if name == "analyze_repo":
from codewiki.mcp.tools.analysis import handle_analyze_repo
# NOTE: Tree-sitter C extensions are not thread-safe, so this
# must run on the main thread (blocking the event loop is
# acceptable for this one-time heavy operation).
return [_text(handle_analyze_repo(arguments, _store))]
elif name == "read_code_components":
from codewiki.mcp.tools.code_reader import handle_read_code_components
return [_text(await asyncio.to_thread(handle_read_code_components, arguments, _store))]
elif name == "write_doc_file":
from codewiki.mcp.tools.doc_writer import handle_write_doc_file
result = await handle_write_doc_file(arguments, _store)
return [_text(result)]
elif name == "edit_doc_file":
from codewiki.mcp.tools.doc_writer import handle_edit_doc_file
result = await handle_edit_doc_file(arguments, _store)
return [_text(result)]
elif name == "save_module_tree":
from codewiki.mcp.tools.module_tree import handle_save_module_tree
return [_text(await asyncio.to_thread(handle_save_module_tree, arguments, _store))]
elif name == "get_processing_order":
from codewiki.mcp.tools.module_tree import handle_get_processing_order
return [_text(await asyncio.to_thread(handle_get_processing_order, arguments, _store))]
elif name == "get_prompt":
from codewiki.mcp.tools.prompt_server import handle_get_prompt
return [_text(await asyncio.to_thread(handle_get_prompt, arguments, _store))]
elif name == "close_session":
sid = arguments["session_id"]
session = _store.get(sid)
if session:
# Only stamp the incremental-update baseline when this
# session actually produced docs — otherwise an aborted
# session would make the next analyze_repo report
# "Documentation is up to date" over missing/stale docs.
if session.docs_written > 0:
_write_generation_metadata(session)
else:
logger.info(
"Session %s wrote no docs; skipping metadata.json baseline update", sid
)
# Clean up workspace files on disk
if session.workspace is not None:
session.workspace.cleanup()
removed = _store.remove(sid)
return [_text(json.dumps({
"status": "closed" if removed else "not_found",
"session_id": sid,
}))]
# --- Legacy tools (require CodeWiki LLM config) ---
elif name == "generate_docs":
return await _legacy_generate_docs(arguments)
elif name == "get_module_tree":
return await _legacy_get_module_tree(arguments)
else:
return [_text(json.dumps({"error": f"Unknown tool: {name}"}))]
except Exception as e:
logger.error("Tool %s failed: %s", name, e, exc_info=True)
return [_text(json.dumps({"error": str(e)}))]
# ===================================================================
# Legacy tool handlers (require _load_config)
# ===================================================================
def _load_config():
"""Load CodeWiki configuration from ~/.codewiki/config.json + keyring."""
from codewiki.cli.config_manager import ConfigManager
manager = ConfigManager()
if not manager.load():
raise RuntimeError(
"CodeWiki not configured. Run 'codewiki config set' first."
)
return manager
async def _legacy_generate_docs(arguments: dict[str, Any]) -> list[TextContent]:
"""Legacy generate_docs — requires CodeWiki LLM configuration."""
repo_path = Path(arguments["repo_path"]).expanduser().resolve()
output_dir = Path(arguments.get("output_dir", "docs")).expanduser().resolve()
if not repo_path.exists():
return [_text(json.dumps({"error": f"Repository not found: {repo_path}"}))]
manager = _load_config()
config = manager.get_config()
api_key = manager.get_api_key()
from codewiki.src.be.backend import is_caw_provider
caw_mode = bool(config) and is_caw_provider(getattr(config, "provider", ""))
if not api_key and not caw_mode:
return [_text(json.dumps({"error": "API key not configured. Run 'codewiki config set --api-key <key>'"}))]
agent_instructions = {}
if arguments.get("doc_type"):
agent_instructions["doc_type"] = arguments["doc_type"]
if arguments.get("include_patterns"):
agent_instructions["include_patterns"] = [p.strip() for p in arguments["include_patterns"].split(",")]
if arguments.get("exclude_patterns"):
agent_instructions["exclude_patterns"] = [p.strip() for p in arguments["exclude_patterns"].split(",")]
from codewiki.src.config import Config as BackendConfig, set_cli_context
set_cli_context(True)
backend_config = BackendConfig.from_cli(
repo_path=str(repo_path),
output_dir=str(output_dir),
llm_base_url=config.base_url,
llm_api_key=api_key,
main_model=config.main_model,
cluster_model=config.cluster_model,
fallback_model=config.fallback_model,
provider=getattr(config, "provider", "openai-compatible"),
aws_region=getattr(config, "aws_region", "us-east-1"),
max_tokens=config.max_tokens,
agent_instructions=agent_instructions or None,
use_gitignore=arguments.get("use_gitignore", True),
)
from codewiki.cli.utils.repo_validator import get_git_commit_hash
from codewiki.src.be.documentation_generator import DocumentationGenerator
doc_gen = DocumentationGenerator(backend_config, commit_id=get_git_commit_hash(repo_path) or None)
await doc_gen.run()
generated_files = []
for f in output_dir.iterdir():
if f.suffix in (".md", ".json", ".html"):
generated_files.append(f.name)
result = {
"status": "success",
"output_dir": str(output_dir),
"files_generated": sorted(generated_files),
"file_count": len(generated_files),
}
return [_text(json.dumps(result, indent=2))]
async def _legacy_get_module_tree(arguments: dict[str, Any]) -> list[TextContent]:
"""Legacy get_module_tree."""
repo_path = Path(arguments["repo_path"]).expanduser().resolve()
output_dir = Path(arguments.get("output_dir", "docs")).expanduser().resolve()
module_tree_path = output_dir / "module_tree.json"
if not module_tree_path.exists():
return [_text(json.dumps({
"error": f"Module tree not found at {module_tree_path}. Run 'codewiki generate' first."
}))]
module_tree = json.loads(module_tree_path.read_text(encoding="utf-8"))
def _summarize_tree(tree, depth=0):
lines = []
for name, info in tree.items():
indent = " " * depth
comp_count = len(info.get("components", []))
children = info.get("children", {})
child_count = len(children) if isinstance(children, dict) else 0
lines.append(f"{indent}- {name} ({comp_count} components, {child_count} children)")
if isinstance(children, dict) and children:
lines.extend(_summarize_tree(children, depth + 1))
return lines
summary = "\n".join(_summarize_tree(module_tree))
result = {
"status": "success",
"module_tree_path": str(module_tree_path),
"total_modules": len(module_tree),
"tree_summary": summary,
}
return [_text(json.dumps(result, indent=2))]
# ===================================================================
# Helpers
# ===================================================================
def _text(content: str) -> TextContent:
return TextContent(type="text", text=content)
def _write_generation_metadata(session: SessionState) -> None:
"""Write ``metadata.json`` to the session's output directory.
Merges the current git commit and timestamp into any existing metadata
so that stats from other workflows (e.g. CLI ``generate_docs``) are
preserved.
"""
try:
output_dir = Path(session.output_dir)
repo_path = Path(session.repo_path)
# Baseline on the commit analyze_repo saw, NOT the current HEAD:
# commits made mid-session were never analyzed, and recording HEAD
# here would silently exclude them from the next incremental run.
commit_id: str | None = session.analyzed_commit
if not commit_id:
from codewiki.cli.utils.repo_validator import get_git_commit_hash
commit_id = get_git_commit_hash(repo_path) or None
from datetime import datetime
# Read existing metadata (if any) and merge
metadata_path = output_dir / "metadata.json"
existing: dict = {}
if metadata_path.exists():
try:
existing = json.loads(metadata_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
pass
existing["generation_info"] = {
**existing.get("generation_info", {}),
"commit_id": commit_id,
"timestamp": datetime.now().isoformat(),
}
metadata_path.write_text(
json.dumps(existing, indent=2, ensure_ascii=False),
encoding="utf-8",
)
except Exception as e:
logger.warning("Failed to write metadata.json: %s", e)
# ===================================================================
# Entry point
# ===================================================================
async def main():
"""Run the MCP server with stdio transport."""
async with stdio_server() as (read_stream, write_stream):
await server.run(
read_stream,
write_stream,
server.create_initialization_options(),
)
if __name__ == "__main__":
asyncio.run(main())