|
1 | 1 | # The git MCP server: tools that read and change a Git repository through |
2 | | -# GitPython. Repository confinement (`--repository`, `validate_repo_path` on the |
3 | | -# path given, `validate_opened_repo` on the directories git then uses) and the |
4 | | -# flag-injection guards live beside the handlers because every tool call must |
5 | | -# pass them before any git command runs. |
| 2 | +# GitPython, and prompts that embed a repository's changes in a message for the |
| 3 | +# model (#5195). Repository confinement (`--repository`, `validate_repo_path` on |
| 4 | +# the path given, `validate_opened_repo` on the directories git then uses) and |
| 5 | +# the flag-injection guards live beside the handlers because every tool call and |
| 6 | +# prompt must pass them before any git command runs. |
6 | 7 | # |
7 | 8 | # Built on the MCP Python SDK's high-level `MCPServer` (#5174), the API these |
8 | 9 | # reference servers are meant to teach: each tool is a typed function, and the |
@@ -596,6 +597,86 @@ def writes(*, destructive: bool, idempotent: bool) -> ToolAnnotations: |
596 | 597 | ) |
597 | 598 |
|
598 | 599 |
|
| 600 | +class GitPrompts(str, Enum): |
| 601 | + COMMIT_MESSAGE = "git_commit_message" |
| 602 | + SUMMARIZE_CHANGES = "git_summarize_changes" |
| 603 | + |
| 604 | + |
| 605 | +def _fenced(text: str, info: str = "diff") -> str: |
| 606 | + """`text` in a Markdown code fence longer than any backtick run inside it. |
| 607 | +
|
| 608 | + A diff can itself hold a ``` line (a Markdown file's code block), which |
| 609 | + would close a fixed three-backtick fence early. |
| 610 | + """ |
| 611 | + fence = "```" |
| 612 | + while fence in text: |
| 613 | + fence += "`" |
| 614 | + body = text.rstrip("\n") |
| 615 | + return f"{fence}{info}\n{body}\n{fence}" |
| 616 | + |
| 617 | + |
| 618 | +def commit_message_prompt(repo: git.Repo) -> str: |
| 619 | + """The git_commit_message prompt's text: the staged diff and the request.""" |
| 620 | + diff = git_diff_staged(repo) |
| 621 | + if not diff.strip(): |
| 622 | + return ( |
| 623 | + "There are no staged changes in this repository, so there is nothing " |
| 624 | + "to write a commit message for. Tell the user to stage changes first " |
| 625 | + "(git_add); git_status shows what is modified or untracked." |
| 626 | + ) |
| 627 | + return ( |
| 628 | + "Write a commit message for the staged changes below. Start with an " |
| 629 | + "imperative subject line of at most 72 characters, then a blank line and " |
| 630 | + "a short body explaining what changed and why, only if the subject alone " |
| 631 | + "does not say enough. Follow the repository's existing convention if " |
| 632 | + "its history shows one (git_log).\n\n" |
| 633 | + f"Staged changes:\n{_fenced(diff)}" |
| 634 | + ) |
| 635 | + |
| 636 | + |
| 637 | +def summarize_changes_prompt(repo: git.Repo, target: str | None = None) -> str: |
| 638 | + """The git_summarize_changes prompt's text: the changes and the request.""" |
| 639 | + if target: |
| 640 | + # git_diff applies the flag-injection guard and resolves the target. |
| 641 | + diff = git_diff(repo, target) |
| 642 | + scope = f"compared with `{target}`" |
| 643 | + changes = f"Diff against {target}:\n{_fenced(diff)}" if diff.strip() else "" |
| 644 | + else: |
| 645 | + staged = git_diff_staged(repo) |
| 646 | + unstaged = git_diff_unstaged(repo) |
| 647 | + scope = "in the working tree" |
| 648 | + parts = [f"Status:\n{_fenced(git_status(repo), 'text')}"] |
| 649 | + if staged.strip(): |
| 650 | + parts.append(f"Staged changes:\n{_fenced(staged)}") |
| 651 | + if unstaged.strip(): |
| 652 | + parts.append(f"Unstaged changes:\n{_fenced(unstaged)}") |
| 653 | + changes = "\n\n".join(parts) if staged.strip() or unstaged.strip() else "" |
| 654 | + if not changes: |
| 655 | + return f"There are no changes {scope} to summarize. Tell the user so." |
| 656 | + return ( |
| 657 | + f"Summarize the changes {scope} below for a reviewer, in plain " |
| 658 | + "language. Group related changes, explain the intent behind them rather " |
| 659 | + "than restating each line, and point out anything that looks unfinished " |
| 660 | + "or risky.\n\n" |
| 661 | + f"{changes}" |
| 662 | + ) |
| 663 | + |
| 664 | + |
| 665 | +@contextmanager |
| 666 | +def reported_as_invalid_params() -> Iterator[None]: |
| 667 | + """Turn an anticipated failure in a prompt into a JSON-RPC -32602 error. |
| 668 | +
|
| 669 | + `prompts/get` has no `isError` result: the spec's error handling for it is |
| 670 | + a JSON-RPC error, and a repository path, target or guard the request named |
| 671 | + is an invalid argument. Without this, MCPServer hides the message behind |
| 672 | + "Error rendering prompt" (code 0 or -32603, depending on the era). |
| 673 | + """ |
| 674 | + try: |
| 675 | + yield |
| 676 | + except ANTICIPATED_ERRORS as e: |
| 677 | + raise MCPError(code=INVALID_PARAMS, message=str(e)) from e |
| 678 | + |
| 679 | + |
599 | 680 | def create_server(repository: Path | None) -> MCPServer: |
600 | 681 | """The git server, confined to `repository` when one is given.""" |
601 | 682 |
|
@@ -636,6 +717,43 @@ def open_repo(repo_path: str) -> git.Repo: |
636 | 717 | raise |
637 | 718 | return repo |
638 | 719 |
|
| 720 | + # Prompts (#5195): each embeds what the repository holds now in one user |
| 721 | + # message, through the same `open_repo` (and so the same confinement) and |
| 722 | + # the same guarded helpers as the tools, under the same lock. |
| 723 | + @server.prompt( |
| 724 | + name=GitPrompts.COMMIT_MESSAGE.value, |
| 725 | + title="Draft a commit message", |
| 726 | + description="Asks the model for a commit message for the staged changes", |
| 727 | + ) |
| 728 | + def commit_message( |
| 729 | + repo_path: Annotated[str, Field(description="The path to the Git repository.")], |
| 730 | + ) -> str: |
| 731 | + with _GIT_LOCK, reported_as_invalid_params(), open_repo(repo_path) as repo: |
| 732 | + return commit_message_prompt(repo) |
| 733 | + |
| 734 | + @server.prompt( |
| 735 | + name=GitPrompts.SUMMARIZE_CHANGES.value, |
| 736 | + title="Summarize changes", |
| 737 | + description=( |
| 738 | + "Asks the model to summarize the working tree's staged and unstaged " |
| 739 | + "changes, or the diff against a branch or commit" |
| 740 | + ), |
| 741 | + ) |
| 742 | + def summarize_changes( |
| 743 | + repo_path: Annotated[str, Field(description="The path to the Git repository.")], |
| 744 | + target: Annotated[ |
| 745 | + Optional[str], |
| 746 | + Field( |
| 747 | + description=( |
| 748 | + "Branch or commit to diff against. Defaults to the working " |
| 749 | + "tree's staged and unstaged changes" |
| 750 | + ) |
| 751 | + ), |
| 752 | + ] = None, |
| 753 | + ) -> str: |
| 754 | + with _GIT_LOCK, reported_as_invalid_params(), open_repo(repo_path) as repo: |
| 755 | + return summarize_changes_prompt(repo, target) |
| 756 | + |
639 | 757 | @server.tool( |
640 | 758 | name=GitTools.STATUS.value, |
641 | 759 | description="Shows the working tree status", |
|
0 commit comments