HimanshuGoyal2004 commited on
Commit
c9a56ff
·
1 Parent(s): 3230875

removed json dump

Browse files
Files changed (2) hide show
  1. app.py +80 -46
  2. requirements.txt +0 -6
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import json
2
  import os
3
  import base64
4
- from typing import Dict, List
5
  import requests
6
  import gradio as gr
7
  from dotenv import load_dotenv
@@ -22,7 +22,7 @@ class GitHubMCPServer:
22
  "Accept": "application/vnd.github.v3+json"
23
  }
24
 
25
- def get_repository_info(self, owner: str, repo: str) -> str:
26
  """Get basic repository information"""
27
  try:
28
  url = f"https://api.github.com/repos/{owner}/{repo}"
@@ -30,33 +30,35 @@ class GitHubMCPServer:
30
 
31
  if response.status_code == 200:
32
  data = response.json()
33
- result = {
34
  "success": True,
35
- "data": {
36
- "name": data["name"],
37
- "full_name": data["full_name"],
38
- "description": data.get("description", ""),
39
- "language": data.get("language", ""),
40
- "size": data["size"],
41
- "stars": data["stargazers_count"],
42
- "forks": data["forks_count"],
43
- "default_branch": data["default_branch"],
44
- "created_at": data["created_at"],
45
- "updated_at": data["updated_at"]
46
- }
47
  }
48
  else:
49
- result = {
50
  "success": False,
51
- "error": f"GitHub API error: {response.status_code}"
 
52
  }
53
 
54
- return json.dumps(result, indent=2)
55
-
56
  except Exception as e:
57
- return json.dumps({"success": False, "error": str(e)}, indent=2)
 
 
 
58
 
59
- def get_file_content(self, owner: str, repo: str, path: str) -> str:
60
  """Get content of a specific file"""
61
  try:
62
  url = f"https://api.github.com/repos/{owner}/{repo}/contents/{path}"
@@ -66,52 +68,84 @@ class GitHubMCPServer:
66
  data = response.json()
67
  if data["type"] == "file" and "content" in data:
68
  # Decode base64 content
69
- content = base64.b64decode(data["content"]).decode('utf-8')
70
- result = {
71
- "success": True,
72
- "data": {
73
- "path": path,
74
- "content": content[:2000] + "..." if len(content) > 2000 else content, # Truncate for display
75
- "size": len(content),
 
 
 
 
 
76
  "sha": data["sha"],
77
- "encoding": data.get("encoding", "base64")
 
 
 
 
 
 
 
 
78
  }
79
- }
80
  else:
81
- result = {
82
  "success": False,
83
- "error": "Not a file or content not available"
 
84
  }
85
  else:
86
- result = {
87
  "success": False,
88
- "error": f"GitHub API error: {response.status_code}"
 
 
89
  }
90
 
91
- return json.dumps(result, indent=2)
92
-
93
  except Exception as e:
94
- return json.dumps({"success": False, "error": str(e)}, indent=2)
 
 
 
 
95
 
96
- def scan_repository(self, owner: str, repo: str, extensions: str = ".py,.js,.ts,.php,.java") -> str:
97
  """Scan repository for code files"""
98
  try:
99
- ext_list = [ext.strip() for ext in extensions.split(",")]
100
  all_files = []
101
  self._scan_directory_sync(owner, repo, "", ext_list, all_files)
102
 
103
- result = {
 
 
 
 
 
 
 
 
104
  "success": True,
105
- "data": {
 
 
 
 
106
  "total_files": len(all_files),
107
- "files": all_files[:50] # Limit to first 50 files for display
 
108
  }
109
  }
110
 
111
- return json.dumps(result, indent=2)
112
-
113
  except Exception as e:
114
- return json.dumps({"success": False, "error": str(e)}, indent=2)
 
 
 
 
115
 
116
  def _scan_directory_sync(self, owner: str, repo: str, path: str, extensions: List[str], all_files: List[Dict]):
117
  """Recursively scan directory for files"""
@@ -191,4 +225,4 @@ if __name__ == "__main__":
191
  print("📡 Server will provide GitHub repository access via MCP")
192
  print("🛠️ Available tools: repository info, file content, repository scanner")
193
 
194
- demo.launch(share=True, mcp_server=True)
 
1
  import json
2
  import os
3
  import base64
4
+ from typing import Dict, List, Any
5
  import requests
6
  import gradio as gr
7
  from dotenv import load_dotenv
 
22
  "Accept": "application/vnd.github.v3+json"
23
  }
24
 
25
+ def get_repository_info(self, owner: str, repo: str) -> dict:
26
  """Get basic repository information"""
27
  try:
28
  url = f"https://api.github.com/repos/{owner}/{repo}"
 
30
 
31
  if response.status_code == 200:
32
  data = response.json()
33
+ return {
34
  "success": True,
35
+ "repository_name": data["name"],
36
+ "full_name": data["full_name"],
37
+ "description": data.get("description", "No description available"),
38
+ "primary_language": data.get("language", "Unknown"),
39
+ "size_kb": data["size"],
40
+ "stars": data["stargazers_count"],
41
+ "forks": data["forks_count"],
42
+ "default_branch": data["default_branch"],
43
+ "created_date": data["created_at"][:10],
44
+ "last_updated": data["updated_at"][:10],
45
+ "is_private": data["private"],
46
+ "clone_url": data["clone_url"]
47
  }
48
  else:
49
+ return {
50
  "success": False,
51
+ "error": f"Repository not found or inaccessible (HTTP {response.status_code})",
52
+ "status_code": response.status_code
53
  }
54
 
 
 
55
  except Exception as e:
56
+ return {
57
+ "success": False,
58
+ "error": f"Failed to fetch repository information: {str(e)}"
59
+ }
60
 
61
+ def get_file_content(self, owner: str, repo: str, path: str) -> dict:
62
  """Get content of a specific file"""
63
  try:
64
  url = f"https://api.github.com/repos/{owner}/{repo}/contents/{path}"
 
68
  data = response.json()
69
  if data["type"] == "file" and "content" in data:
70
  # Decode base64 content
71
+ try:
72
+ content = base64.b64decode(data["content"]).decode('utf-8')
73
+ file_extension = path.split('.')[-1].lower() if '.' in path else 'unknown'
74
+
75
+ return {
76
+ "success": True,
77
+ "file_path": path,
78
+ "file_name": path.split('/')[-1],
79
+ "file_extension": file_extension,
80
+ "content": content,
81
+ "content_length": len(content),
82
+ "line_count": len(content.split('\n')),
83
  "sha": data["sha"],
84
+ "download_url": data.get("download_url", ""),
85
+ "is_binary": False
86
+ }
87
+ except UnicodeDecodeError:
88
+ return {
89
+ "success": False,
90
+ "error": "File contains binary data that cannot be decoded as text",
91
+ "file_path": path,
92
+ "is_binary": True
93
  }
 
94
  else:
95
+ return {
96
  "success": False,
97
+ "error": f"Path '{path}' is not a file or content is not available",
98
+ "item_type": data.get("type", "unknown")
99
  }
100
  else:
101
+ return {
102
  "success": False,
103
+ "error": f"File not found or inaccessible (HTTP {response.status_code})",
104
+ "file_path": path,
105
+ "status_code": response.status_code
106
  }
107
 
 
 
108
  except Exception as e:
109
+ return {
110
+ "success": False,
111
+ "error": f"Failed to fetch file content: {str(e)}",
112
+ "file_path": path
113
+ }
114
 
115
+ def scan_repository(self, owner: str, repo: str, extensions: str = ".py,.js,.ts,.php,.java") -> dict:
116
  """Scan repository for code files"""
117
  try:
118
+ ext_list = [ext.strip() for ext in extensions.split(",") if ext.strip()]
119
  all_files = []
120
  self._scan_directory_sync(owner, repo, "", ext_list, all_files)
121
 
122
+ # Group files by extension for better organization
123
+ files_by_extension = {}
124
+ for file_info in all_files:
125
+ ext = file_info.get('name', '').split('.')[-1].lower() if '.' in file_info.get('name', '') else 'no_extension'
126
+ if ext not in files_by_extension:
127
+ files_by_extension[ext] = []
128
+ files_by_extension[ext].append(file_info)
129
+
130
+ return {
131
  "success": True,
132
+ "total_files_found": len(all_files),
133
+ "extensions_scanned": ext_list,
134
+ "files_by_extension": files_by_extension,
135
+ "all_files": all_files[:100], # Limit to first 100 files
136
+ "scan_summary": {
137
  "total_files": len(all_files),
138
+ "extensions_found": list(files_by_extension.keys()),
139
+ "largest_file": max(all_files, key=lambda x: x.get('size', 0)) if all_files else None
140
  }
141
  }
142
 
 
 
143
  except Exception as e:
144
+ return {
145
+ "success": False,
146
+ "error": f"Failed to scan repository: {str(e)}",
147
+ "total_files_found": 0
148
+ }
149
 
150
  def _scan_directory_sync(self, owner: str, repo: str, path: str, extensions: List[str], all_files: List[Dict]):
151
  """Recursively scan directory for files"""
 
225
  print("📡 Server will provide GitHub repository access via MCP")
226
  print("🛠️ Available tools: repository info, file content, repository scanner")
227
 
228
+ demo.launch(mcp_server=True)
requirements.txt CHANGED
@@ -1,14 +1,8 @@
1
- # core
2
  gradio[oauth,mcp]==5.45.0
3
  fastapi==0.115.2
4
  uvicorn==0.24.0
5
-
6
- # MCP
7
  mcp==1.10.1
8
-
9
- # other deps
10
  smolagents>=0.1.0
11
  requests>=2.28.0
12
  python-dotenv>=1.0.0
13
-
14
  pydantic>=2.11,<2.12
 
 
1
  gradio[oauth,mcp]==5.45.0
2
  fastapi==0.115.2
3
  uvicorn==0.24.0
 
 
4
  mcp==1.10.1
 
 
5
  smolagents>=0.1.0
6
  requests>=2.28.0
7
  python-dotenv>=1.0.0
 
8
  pydantic>=2.11,<2.12