-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathserver.py
More file actions
80 lines (63 loc) · 2.4 KB
/
server.py
File metadata and controls
80 lines (63 loc) · 2.4 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
from pathlib import Path
from typing import Optional
from mcp.server.fastmcp import FastMCP
from main import run_doc_flow
# create FastMCP instance
mcp = FastMCP("doc-writer")
@mcp.tool()
def write_documentation(repo_url: str) -> str:
"""
Generates the documentation for the given GitHub repository URL.
Notify the user when the generation is complete.
Args:
repo_url (str): The URL of the GitHub repo.
Returns:
str: Returns a message when the documentation is completed successfully.
"""
try:
if not repo_url.startswith(("http://", "https://")):
raise ValueError("Invalid repository URL format")
run_doc_flow(repo_url)
return f"Successfully generated documentation for repo {repo_url}"
except Exception as e:
return f"Failed to generate documentation for repo {repo_url} due to {e}"
@mcp.tool()
def list_docs() -> str:
"""
Lists the documentation files that are generated successfully.
Args: None
Returns:
str: Returns a nicely formatted string of generated doc files.
"""
docs_dir = Path("docs")
if not docs_dir.exists():
raise ValueError("No documentation files found")
output_lines = ["Generated documentation files:"]
for doc_file in docs_dir.glob("*.mdx"):
output_lines.append(f"- docs/{doc_file.name}")
return "\n".join(output_lines)
@mcp.tool()
def view_content(file_path: str) -> str:
"""
Displays the content of a generated documentation file.
Args:
file_path (str): Relative path to the documentation file (e.g., 'docs/overview.mdx')
Returns:
str: Content of the file or error message.
"""
try:
if not file_path.startswith("docs/") or "../" in file_path:
raise ValueError("Invalid file path")
path = Path(file_path)
if not path.exists():
raise FileNotFoundError(f"File {file_path} not found")
if not path.is_file():
raise ValueError(f"Path {file_path} is not a file")
if path.suffix not in {".mdx", ".md"}:
raise ValueError("Only documentation files (.mdx/.md) can be viewed")
return path.read_text(encoding="utf-8")
except Exception as e:
return f"Failed to view documentation: {str(e)}"
# Run the server
if __name__ == "__main__":
mcp.run(transport='sse')