-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_server.py
More file actions
133 lines (117 loc) · 4.6 KB
/
github_server.py
File metadata and controls
133 lines (117 loc) · 4.6 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
#!/usr/bin/env python3
import asyncio
import json
import sys
from typing import Any, Dict, List
import requests
import os
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent
# GitHub API base URL
GITHUB_API_BASE = "https://api.github.com"
app = Server("github-mcp-server")
@app.list_tools()
async def list_tools() -> List[Tool]:
"""List available GitHub tools"""
return [
Tool(
name="get_repo_info",
description="Get information about a GitHub repository",
inputSchema={
"type": "object",
"properties": {
"owner": {"type": "string", "description": "Repository owner"},
"repo": {"type": "string", "description": "Repository name"}
},
"required": ["owner", "repo"]
}
),
Tool(
name="list_repo_files",
description="List files in a GitHub repository",
inputSchema={
"type": "object",
"properties": {
"owner": {"type": "string", "description": "Repository owner"},
"repo": {"type": "string", "description": "Repository name"},
"path": {"type": "string", "description": "Path in repository", "default": ""}
},
"required": ["owner", "repo"]
}
),
Tool(
name="get_file_content",
description="Get content of a file from GitHub repository",
inputSchema={
"type": "object",
"properties": {
"owner": {"type": "string", "description": "Repository owner"},
"repo": {"type": "string", "description": "Repository name"},
"path": {"type": "string", "description": "File path"}
},
"required": ["owner", "repo", "path"]
}
)
]
@app.call_tool()
async def call_tool(name: str, arguments: Dict[str, Any]) -> List[TextContent]:
"""Handle tool calls"""
if name == "get_repo_info":
owner = arguments["owner"]
repo = arguments["repo"]
url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
result = {
"name": data["name"],
"description": data["description"],
"stars": data["stargazers_count"],
"forks": data["forks_count"],
"language": data["language"],
"url": data["html_url"]
}
return [TextContent(type="text", text=json.dumps(result, indent=2))]
else:
return [TextContent(type="text", text=f"Error: {response.status_code}")]
elif name == "list_repo_files":
owner = arguments["owner"]
repo = arguments["repo"]
path = arguments.get("path", "")
url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}/contents/{path}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
files = []
for item in data:
files.append({
"name": item["name"],
"type": item["type"],
"path": item["path"]
})
return [TextContent(type="text", text=json.dumps(files, indent=2))]
else:
return [TextContent(type="text", text=f"Error: {response.status_code}")]
elif name == "get_file_content":
owner = arguments["owner"]
repo = arguments["repo"]
path = arguments["path"]
url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}/contents/{path}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if data["type"] == "file":
import base64
content = base64.b64decode(data["content"]).decode('utf-8')
return [TextContent(type="text", text=content)]
else:
return [TextContent(type="text", text="Not a file")]
else:
return [TextContent(type="text", text=f"Error: {response.status_code}")]
return [TextContent(type="text", text="Unknown tool")]
async def main():
async with stdio_server() as (read_stream, write_stream):
await app.run(read_stream, write_stream, app.create_initialization_options())
if __name__ == "__main__":
asyncio.run(main())