Commit
·
ecf0796
1
Parent(s):
e7abdcc
add tool to retrieve diff of commits.
Browse files
pmcp/mcp_server/github_server/services/contents.py
CHANGED
|
@@ -24,6 +24,23 @@ class ContentService:
|
|
| 24 |
"""
|
| 25 |
params = {"sha": branch, "per_page": per_page}
|
| 26 |
return await self.client.GET(f"{owner}/{repo}/commits", params=params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
async def get_file(
|
| 29 |
self, owner: str, repo: str, path: str, ref: str | None = None
|
|
|
|
| 24 |
"""
|
| 25 |
params = {"sha": branch, "per_page": per_page}
|
| 26 |
return await self.client.GET(f"{owner}/{repo}/commits", params=params)
|
| 27 |
+
|
| 28 |
+
async def get_commit_details(
|
| 29 |
+
self, owner: str, repo: str, sha: str
|
| 30 |
+
) -> dict:
|
| 31 |
+
"""
|
| 32 |
+
Retrieve a single commit’s metadata *and* its file-level diff.
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
owner (str): Repository owner.
|
| 36 |
+
repo (str): Repository name.
|
| 37 |
+
sha (str): Commit SHA.
|
| 38 |
+
|
| 39 |
+
Returns:
|
| 40 |
+
Full JSON payload for the commit, including `commit.message` and
|
| 41 |
+
a `files` array where each entry has a `patch` (the diff).
|
| 42 |
+
"""
|
| 43 |
+
return await self.client.GET(f"{owner}/{repo}/commits/{sha}")
|
| 44 |
|
| 45 |
async def get_file(
|
| 46 |
self, owner: str, repo: str, path: str, ref: str | None = None
|
pmcp/mcp_server/github_server/tools/contents.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
Commit and file-content tools.
|
| 3 |
"""
|
| 4 |
import base64
|
| 5 |
-
from typing import List, Dict
|
| 6 |
from mcp.server.fastmcp import Context
|
| 7 |
from pmcp.mcp_server.github_server.services.contents import ContentService
|
| 8 |
from pmcp.mcp_server.github_server.github import github_client
|
|
@@ -28,8 +28,13 @@ async def get_recent_commits(
|
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
commits = await service.recent_commits(owner, repo, branch, per_page)
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
except Exception as exc:
|
| 34 |
error_msg = f"Error while getting the commits for branch {branch} in {repo}. Error {str(exc)}"
|
| 35 |
await ctx.error(str(exc))
|
|
@@ -59,4 +64,39 @@ async def get_file_contents(
|
|
| 59 |
except Exception as exc:
|
| 60 |
error_msg = f"Error while getting the content of file {path} in repository {repo}. Error: {exc}"
|
| 61 |
await ctx.error(str(error_msg))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
raise
|
|
|
|
| 2 |
Commit and file-content tools.
|
| 3 |
"""
|
| 4 |
import base64
|
| 5 |
+
from typing import Any, List, Dict
|
| 6 |
from mcp.server.fastmcp import Context
|
| 7 |
from pmcp.mcp_server.github_server.services.contents import ContentService
|
| 8 |
from pmcp.mcp_server.github_server.github import github_client
|
|
|
|
| 28 |
"""
|
| 29 |
try:
|
| 30 |
commits = await service.recent_commits(owner, repo, branch, per_page)
|
| 31 |
+
# return a list of { sha, message }
|
| 32 |
+
items = [
|
| 33 |
+
{"sha": c["sha"], "message": c["commit"]["message"]}
|
| 34 |
+
for c in commits
|
| 35 |
+
]
|
| 36 |
+
return {"commits": items}
|
| 37 |
+
|
| 38 |
except Exception as exc:
|
| 39 |
error_msg = f"Error while getting the commits for branch {branch} in {repo}. Error {str(exc)}"
|
| 40 |
await ctx.error(str(exc))
|
|
|
|
| 64 |
except Exception as exc:
|
| 65 |
error_msg = f"Error while getting the content of file {path} in repository {repo}. Error: {exc}"
|
| 66 |
await ctx.error(str(error_msg))
|
| 67 |
+
raise
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
async def get_commit_details(
|
| 71 |
+
ctx: Context, owner: str, repo: str, sha: str
|
| 72 |
+
) -> Dict[str, Any]:
|
| 73 |
+
"""
|
| 74 |
+
Retrieves a commit by SHA, including its commit message and the diff.
|
| 75 |
+
|
| 76 |
+
Args:
|
| 77 |
+
ctx: FastMCP request context.
|
| 78 |
+
owner: Repository owner.
|
| 79 |
+
repo: Repository name.
|
| 80 |
+
sha: Commit SHA.
|
| 81 |
+
|
| 82 |
+
Returns:
|
| 83 |
+
{
|
| 84 |
+
"sha": "...",
|
| 85 |
+
"message": "...",
|
| 86 |
+
"files": [
|
| 87 |
+
{ "filename": "...", "patch": "diff text..." },
|
| 88 |
+
…
|
| 89 |
+
]
|
| 90 |
+
}
|
| 91 |
+
"""
|
| 92 |
+
try:
|
| 93 |
+
commit = await service.get_commit_details(owner, repo, sha)
|
| 94 |
+
message = commit["commit"]["message"]
|
| 95 |
+
files = [
|
| 96 |
+
{"filename": f["filename"], "patch": f.get("patch", "")}
|
| 97 |
+
for f in commit.get("files", [])
|
| 98 |
+
]
|
| 99 |
+
return {"sha": sha, "message": message, "files": files}
|
| 100 |
+
except Exception as exc:
|
| 101 |
+
await ctx.error(str(exc))
|
| 102 |
raise
|
pmcp/mcp_server/github_server/tools/tools.py
CHANGED
|
@@ -25,6 +25,7 @@ def register_tools(mcp):
|
|
| 25 |
mcp.add_tool(branches.list_branches)
|
| 26 |
mcp.add_tool(contents.get_recent_commits)
|
| 27 |
mcp.add_tool(contents.get_file_contents)
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
|
|
|
|
| 25 |
mcp.add_tool(branches.list_branches)
|
| 26 |
mcp.add_tool(contents.get_recent_commits)
|
| 27 |
mcp.add_tool(contents.get_file_contents)
|
| 28 |
+
mcp.add_tool(contents.get_commit_details)
|
| 29 |
|
| 30 |
|
| 31 |
|