MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / handle_read_code_components

Function handle_read_code_components

codewiki/mcp/tools/code_reader.py:21–61  ·  view source on GitHub ↗

Write the source code for given component IDs to workspace files. Returns a compact JSON with file paths — no source code inline.

(
    arguments: Dict[str, Any],
    store: SessionStore,
)

Source from the content-addressed store, hash-verified

19
20def handle_read_code_components(
21 arguments: Dict[str, Any],
22 store: SessionStore,
23) -> str:
24 """Write the source code for given component IDs to workspace files.
25
26 Returns a compact JSON with file paths — no source code inline.
27 """
28 session_id = arguments["session_id"]
29 session = store.get(session_id)
30 if session is None:
31 return json.dumps({"error": f"Session {session_id} not found or expired."})
32
33 if session.workspace is None:
34 return json.dumps({"error": "Session workspace not initialized."})
35
36 component_ids: List[str] = arguments["component_ids"]
37 components = session.components
38 workspace = session.workspace
39
40 written_files: Dict[str, str] = {} # filename -> component_id
41 not_found: List[str] = []
42
43 for cid in component_ids:
44 node = components.get(cid)
45 if node is None:
46 not_found.append(cid)
47 continue
48 lang = getattr(node, "language", "")
49 source = (getattr(node, "source_code", None) or "").strip()
50 file_path = workspace.write_component_source(cid, source, lang)
51 written_files[file_path.name] = cid
52
53 result = {
54 "written": len(written_files),
55 "not_found_count": len(not_found),
56 "not_found": not_found,
57 "source_dir": str(workspace.root / "sources"),
58 "files": written_files,
59 }
60 return json.dumps(result, indent=2, ensure_ascii=False)

Callers 1

mainFunction · 0.90

Calls 2

getMethod · 0.80

Tested by 1

mainFunction · 0.72