diff --git a/compose.yaml b/compose.yaml index 3d6512d..aa7c785 100644 --- a/compose.yaml +++ b/compose.yaml @@ -19,10 +19,11 @@ services: - TRANSPORT=SSE - AWS_REGION=${AWS_REGION} - TARGET_EKS_CLUSTER_NAME=${TARGET_EKS_CLUSTER_NAME} + github: build: - context: sre_agent - dockerfile: servers/github/Dockerfile + context: sre_agent/servers/github-mcp-server + dockerfile: Dockerfile environment: - GITHUB_PERSONAL_ACCESS_TOKEN=${GITHUB_PERSONAL_ACCESS_TOKEN} - TRANSPORT=SSE diff --git a/pyproject.toml b/pyproject.toml index 99379ea..3caecde 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ dependencies = [] ci = [ "anthropic>=0.49.0", "fastapi>=0.115.12", - "mcp>=1.6.0", + "mcp==1.6.0", "pydantic>=2.11.3", "pydantic-settings>=2.9.1", "python-dotenv>=1.1.0", diff --git a/sre_agent/servers/README.md b/sre_agent/servers/README.md index b02357d..424982f 100644 --- a/sre_agent/servers/README.md +++ b/sre_agent/servers/README.md @@ -12,7 +12,7 @@ The current MCP servers we deploy are: The following MCP servers are based off of existing implementations: -1. GitHub: https://github.com/modelcontextprotocol/servers/tree/main/src/github (MIT License) +1. GitHub: https://github.com/github/github-mcp-server (MIT License) 2. Slack: https://github.com/modelcontextprotocol/servers/tree/main/src/slack (MIT License) 3. Kubernetes: https://github.com/Flux159/mcp-server-kubernetes (MIT License) diff --git a/sre_agent/servers/github-mcp-server/.gitignore b/sre_agent/servers/github-mcp-server/.gitignore new file mode 100644 index 0000000..1264936 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/.gitignore @@ -0,0 +1,13 @@ +.idea +cmd/github-mcp-server/github-mcp-server + +# VSCode +.vscode/* +!.vscode/launch.json + +# Added by goreleaser init: +dist/ +__debug_bin* + +# Go +vendor diff --git a/sre_agent/servers/github-mcp-server/.golangci.yml b/sre_agent/servers/github-mcp-server/.golangci.yml new file mode 100644 index 0000000..43e3d62 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/.golangci.yml @@ -0,0 +1,28 @@ +run: + timeout: 5m + tests: true + concurrency: 4 + +linters: + enable: + - govet + - errcheck + - staticcheck + - gofmt + - goimports + - revive + - ineffassign + - typecheck + - unused + - gosimple + - misspell + - nakedret + - bodyclose + - gocritic + - makezero + - gosec + +output: + formats: colored-line-number + print-issued-lines: true + print-linter-name: true diff --git a/sre_agent/servers/github-mcp-server/CODE_OF_CONDUCT.md b/sre_agent/servers/github-mcp-server/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..cea5751 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +GitHub. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/sre_agent/servers/github-mcp-server/CONTRIBUTING.md b/sre_agent/servers/github-mcp-server/CONTRIBUTING.md new file mode 100644 index 0000000..fe307d1 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/CONTRIBUTING.md @@ -0,0 +1,41 @@ +## Contributing + +[fork]: https://github.com/github/github-mcp-server/fork +[pr]: https://github.com/github/github-mcp-server/compare +[style]: https://github.com/github/github-mcp-server/blob/main/.golangci.yml + +Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great. + +Contributions to this project are [released](https://help.github.com/articles/github-terms-of-service/#6-contributions-under-repository-license) to the public under the [project's open source license](LICENSE). + +Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms. + +## Prerequisites for running and testing code + +These are one time installations required to be able to test your changes locally as part of the pull request (PR) submission process. + +1. install Go [through download](https://go.dev/doc/install) | [through Homebrew](https://formulae.brew.sh/formula/go) +1. [install golangci-lint](https://golangci-lint.run/welcome/install/#local-installation) + +## Submitting a pull request + +1. [Fork][fork] and clone the repository +1. Make sure the tests pass on your machine: `go test -v ./...` +1. Make sure linter passes on your machine: `golangci-lint run` +1. Create a new branch: `git checkout -b my-branch-name` +1. Make your change, add tests, and make sure the tests and linter still pass +1. Push to your fork and [submit a pull request][pr] +1. Pat yourself on the back and wait for your pull request to be reviewed and merged. + +Here are a few things you can do that will increase the likelihood of your pull request being accepted: + +- Follow the [style guide][style]. +- Write tests. +- Keep your change as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests. +- Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html). + +## Resources + +- [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/) +- [Using Pull Requests](https://help.github.com/articles/about-pull-requests/) +- [GitHub Help](https://help.github.com) diff --git a/sre_agent/servers/github-mcp-server/Dockerfile b/sre_agent/servers/github-mcp-server/Dockerfile new file mode 100644 index 0000000..5a85fb6 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/Dockerfile @@ -0,0 +1,30 @@ +ARG VERSION="dev" +# This is the version of the commit that was built +ARG COMMIT="92d95e4dbe841e9e3b8c6741ffdcfbff300870d2" + +FROM golang:1.24.2 AS build +# allow this step access to build arg +ARG VERSION +ARG COMMIT +# Set the working directory +WORKDIR /build + +RUN go env -w GOMODCACHE=/root/.cache/go-build + +# Install dependencies +COPY go.mod go.sum ./ +RUN --mount=type=cache,target=/root/.cache/go-build go mod download + +COPY . ./ +# Build the server +RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags="-s -w -X main.version=${VERSION} -X main.commit=${COMMIT} -X main.date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \ + -o github-mcp-server cmd/github-mcp-server/main.go + +# Make a stage to run the app +FROM gcr.io/distroless/base-debian12 +# Set the working directory +WORKDIR /server +# Copy the binary from the build stage +COPY --from=build /build/github-mcp-server . +# Command to run the server +CMD ["./github-mcp-server", "sse"] diff --git a/sre_agent/servers/github-mcp-server/LICENSE b/sre_agent/servers/github-mcp-server/LICENSE new file mode 100644 index 0000000..9a9cc50 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 GitHub + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/README.md b/sre_agent/servers/github-mcp-server/README.md new file mode 100644 index 0000000..eacaef2 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/README.md @@ -0,0 +1,615 @@ +# GitHub MCP Server + +The GitHub MCP Server is a [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) +server that provides seamless integration with GitHub APIs, enabling advanced +automation and interaction capabilities for developers and tools. + +[![Install with Docker in VS Code](https://img.shields.io/badge/VS_Code-Install_Server-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect/mcp/install?name=github&inputs=%5B%7B%22id%22%3A%22github_token%22%2C%22type%22%3A%22promptString%22%2C%22description%22%3A%22GitHub%20Personal%20Access%20Token%22%2C%22password%22%3Atrue%7D%5D&config=%7B%22command%22%3A%22docker%22%2C%22args%22%3A%5B%22run%22%2C%22-i%22%2C%22--rm%22%2C%22-e%22%2C%22GITHUB_PERSONAL_ACCESS_TOKEN%22%2C%22ghcr.io%2Fgithub%2Fgithub-mcp-server%22%5D%2C%22env%22%3A%7B%22GITHUB_PERSONAL_ACCESS_TOKEN%22%3A%22%24%7Binput%3Agithub_token%7D%22%7D%7D) [![Install with Docker in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install_Server-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect/mcp/install?name=github&inputs=%5B%7B%22id%22%3A%22github_token%22%2C%22type%22%3A%22promptString%22%2C%22description%22%3A%22GitHub%20Personal%20Access%20Token%22%2C%22password%22%3Atrue%7D%5D&config=%7B%22command%22%3A%22docker%22%2C%22args%22%3A%5B%22run%22%2C%22-i%22%2C%22--rm%22%2C%22-e%22%2C%22GITHUB_PERSONAL_ACCESS_TOKEN%22%2C%22ghcr.io%2Fgithub%2Fgithub-mcp-server%22%5D%2C%22env%22%3A%7B%22GITHUB_PERSONAL_ACCESS_TOKEN%22%3A%22%24%7Binput%3Agithub_token%7D%22%7D%7D&quality=insiders) + +## Use Cases + +- Automating GitHub workflows and processes. +- Extracting and analyzing data from GitHub repositories. +- Building AI powered tools and applications that interact with GitHub's ecosystem. + +## Prerequisites + +1. To run the server in a container, you will need to have [Docker](https://www.docker.com/) installed. +2. Once Docker is installed, you will also need to ensure Docker is running. The image is public; if you get errors on pull, you may have an expired token and need to `docker logout ghcr.io`. +3. Lastly you will need to [Create a GitHub Personal Access Token](https://github.com/settings/personal-access-tokens/new). +The MCP server can use many of the GitHub APIs, so enable the permissions that you feel comfortable granting your AI tools (to learn more about access tokens, please check out the [documentation](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)). + +## Installation + +### Usage with VS Code + +For quick installation, use one of the one-click install buttons at the top of this README. Once you complete that flow, toggle Agent mode (located by the Copilot Chat text input) and the server will start. + +For manual installation, add the following JSON block to your User Settings (JSON) file in VS Code. You can do this by pressing `Ctrl + Shift + P` and typing `Preferences: Open User Settings (JSON)`. + +```json +{ + "mcp": { + "inputs": [ + { + "type": "promptString", + "id": "github_token", + "description": "GitHub Personal Access Token", + "password": true + } + ], + "servers": { + "github": { + "command": "docker", + "args": [ + "run", + "-i", + "--rm", + "-e", + "GITHUB_PERSONAL_ACCESS_TOKEN", + "ghcr.io/github/github-mcp-server" + ], + "env": { + "GITHUB_PERSONAL_ACCESS_TOKEN": "${input:github_token}" + } + } + } + } +} +``` + +Optionally, you can add a similar example (i.e. without the mcp key) to a file called `.vscode/mcp.json` in your workspace. This will allow you to share the configuration with others. + + +```json +{ + "inputs": [ + { + "type": "promptString", + "id": "github_token", + "description": "GitHub Personal Access Token", + "password": true + } + ], + "servers": { + "github": { + "command": "docker", + "args": [ + "run", + "-i", + "--rm", + "-e", + "GITHUB_PERSONAL_ACCESS_TOKEN", + "ghcr.io/github/github-mcp-server" + ], + "env": { + "GITHUB_PERSONAL_ACCESS_TOKEN": "${input:github_token}" + } + } + } +} + +``` + +More about using MCP server tools in VS Code's [agent mode documentation](https://code.visualstudio.com/docs/copilot/chat/mcp-servers). + +### Usage with Claude Desktop + +```json +{ + "mcpServers": { + "github": { + "command": "docker", + "args": [ + "run", + "-i", + "--rm", + "-e", + "GITHUB_PERSONAL_ACCESS_TOKEN", + "ghcr.io/github/github-mcp-server" + ], + "env": { + "GITHUB_PERSONAL_ACCESS_TOKEN": "" + } + } + } +} +``` + +### Build from source + +If you don't have Docker, you can use `go build` to build the binary in the +`cmd/github-mcp-server` directory, and use the `github-mcp-server stdio` command with the `GITHUB_PERSONAL_ACCESS_TOKEN` environment variable set to your token. To specify the output location of the build, use the `-o` flag. You should configure your server to use the built executable as its `command`. For example: + +```JSON +{ + "mcp": { + "servers": { + "github": { + "command": "/path/to/github-mcp-server", + "args": ["stdio"], + "env": { + "GITHUB_PERSONAL_ACCESS_TOKEN": "" + } + } + } + } +} +``` + +## Tool Configuration + +The GitHub MCP Server supports enabling or disabling specific groups of functionalities via the `--toolsets` flag. This allows you to control which GitHub API capabilities are available to your AI tools. Enabling only the toolsets that you need can help the LLM with tool choice and reduce the context size. + +### Available Toolsets + +The following sets of tools are available (all are on by default): + +| Toolset | Description | +| ----------------------- | ------------------------------------------------------------- | +| `repos` | Repository-related tools (file operations, branches, commits) | +| `issues` | Issue-related tools (create, read, update, comment) | +| `users` | Anything relating to GitHub Users | +| `pull_requests` | Pull request operations (create, merge, review) | +| `code_security` | Code scanning alerts and security features | +| `experiments` | Experimental features (not considered stable) | + +#### Specifying Toolsets + +To specify toolsets you want available to the LLM, you can pass an allow-list in two ways: + +1. **Using Command Line Argument**: + + ```bash + github-mcp-server --toolsets repos,issues,pull_requests,code_security + ``` + +2. **Using Environment Variable**: + ```bash + GITHUB_TOOLSETS="repos,issues,pull_requests,code_security" ./github-mcp-server + ``` + +The environment variable `GITHUB_TOOLSETS` takes precedence over the command line argument if both are provided. + +### Using Toolsets With Docker + +When using Docker, you can pass the toolsets as environment variables: + +```bash +docker run -i --rm \ + -e GITHUB_PERSONAL_ACCESS_TOKEN= \ + -e GITHUB_TOOLSETS="repos,issues,pull_requests,code_security,experiments" \ + ghcr.io/github/github-mcp-server +``` + +### The "all" Toolset + +The special toolset `all` can be provided to enable all available toolsets regardless of any other configuration: + +```bash +./github-mcp-server --toolsets all +``` + +Or using the environment variable: + +```bash +GITHUB_TOOLSETS="all" ./github-mcp-server +``` + +## Dynamic Tool Discovery + +**Note**: This feature is currently in beta and may not be available in all environments. Please test it out and let us know if you encounter any issues. + +Instead of starting with all tools enabled, you can turn on dynamic toolset discovery. Dynamic toolsets allow the MCP host to list and enable toolsets in response to a user prompt. This should help to avoid situations where the model gets confused by the shear number of tools available. + +### Using Dynamic Tool Discovery + +When using the binary, you can pass the `--dynamic-toolsets` flag. + +```bash +./github-mcp-server --dynamic-toolsets +``` + +When using Docker, you can pass the toolsets as environment variables: + +```bash +docker run -i --rm \ + -e GITHUB_PERSONAL_ACCESS_TOKEN= \ + -e GITHUB_DYNAMIC_TOOLSETS=1 \ + ghcr.io/github/github-mcp-server +``` + +## GitHub Enterprise Server + +The flag `--gh-host` and the environment variable `GITHUB_HOST` can be used to set +the GitHub Enterprise Server hostname. + +## i18n / Overriding Descriptions + +The descriptions of the tools can be overridden by creating a +`github-mcp-server-config.json` file in the same directory as the binary. + +The file should contain a JSON object with the tool names as keys and the new +descriptions as values. For example: + +```json +{ + "TOOL_ADD_ISSUE_COMMENT_DESCRIPTION": "an alternative description", + "TOOL_CREATE_BRANCH_DESCRIPTION": "Create a new branch in a GitHub repository" +} +``` + +You can create an export of the current translations by running the binary with +the `--export-translations` flag. + +This flag will preserve any translations/overrides you have made, while adding +any new translations that have been added to the binary since the last time you +exported. + +```sh +./github-mcp-server --export-translations +cat github-mcp-server-config.json +``` + +You can also use ENV vars to override the descriptions. The environment +variable names are the same as the keys in the JSON file, prefixed with +`GITHUB_MCP_` and all uppercase. + +For example, to override the `TOOL_ADD_ISSUE_COMMENT_DESCRIPTION` tool, you can +set the following environment variable: + +```sh +export GITHUB_MCP_TOOL_ADD_ISSUE_COMMENT_DESCRIPTION="an alternative description" +``` + +## Tools + +### Users + +- **get_me** - Get details of the authenticated user + - No parameters required + +### Issues + +- **get_issue** - Gets the contents of an issue within a repository + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `issue_number`: Issue number (number, required) + +- **get_issue_comments** - Get comments for a GitHub issue + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `issue_number`: Issue number (number, required) + +- **create_issue** - Create a new issue in a GitHub repository + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `title`: Issue title (string, required) + - `body`: Issue body content (string, optional) + - `assignees`: Usernames to assign to this issue (string[], optional) + - `labels`: Labels to apply to this issue (string[], optional) + +- **add_issue_comment** - Add a comment to an issue + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `issue_number`: Issue number (number, required) + - `body`: Comment text (string, required) + +- **list_issues** - List and filter repository issues + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `state`: Filter by state ('open', 'closed', 'all') (string, optional) + - `labels`: Labels to filter by (string[], optional) + - `sort`: Sort by ('created', 'updated', 'comments') (string, optional) + - `direction`: Sort direction ('asc', 'desc') (string, optional) + - `since`: Filter by date (ISO 8601 timestamp) (string, optional) + - `page`: Page number (number, optional) + - `perPage`: Results per page (number, optional) + +- **update_issue** - Update an existing issue in a GitHub repository + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `issue_number`: Issue number to update (number, required) + - `title`: New title (string, optional) + - `body`: New description (string, optional) + - `state`: New state ('open' or 'closed') (string, optional) + - `labels`: New labels (string[], optional) + - `assignees`: New assignees (string[], optional) + - `milestone`: New milestone number (number, optional) + +- **search_issues** - Search for issues and pull requests + - `query`: Search query (string, required) + - `sort`: Sort field (string, optional) + - `order`: Sort order (string, optional) + - `page`: Page number (number, optional) + - `perPage`: Results per page (number, optional) + +### Pull Requests + +- **get_pull_request** - Get details of a specific pull request + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `pullNumber`: Pull request number (number, required) + +- **list_pull_requests** - List and filter repository pull requests + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `state`: PR state (string, optional) + - `sort`: Sort field (string, optional) + - `direction`: Sort direction (string, optional) + - `perPage`: Results per page (number, optional) + - `page`: Page number (number, optional) + +- **merge_pull_request** - Merge a pull request + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `pullNumber`: Pull request number (number, required) + - `commit_title`: Title for the merge commit (string, optional) + - `commit_message`: Message for the merge commit (string, optional) + - `merge_method`: Merge method (string, optional) + +- **get_pull_request_files** - Get the list of files changed in a pull request + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `pullNumber`: Pull request number (number, required) + +- **get_pull_request_status** - Get the combined status of all status checks for a pull request + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `pullNumber`: Pull request number (number, required) + +- **update_pull_request_branch** - Update a pull request branch with the latest changes from the base branch + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `pullNumber`: Pull request number (number, required) + - `expectedHeadSha`: The expected SHA of the pull request's HEAD ref (string, optional) + +- **get_pull_request_comments** - Get the review comments on a pull request + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `pullNumber`: Pull request number (number, required) + +- **get_pull_request_reviews** - Get the reviews on a pull request + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `pullNumber`: Pull request number (number, required) + +- **create_pull_request_review** - Create a review on a pull request review + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `pullNumber`: Pull request number (number, required) + - `body`: Review comment text (string, optional) + - `event`: Review action ('APPROVE', 'REQUEST_CHANGES', 'COMMENT') (string, required) + - `commitId`: SHA of commit to review (string, optional) + - `comments`: Line-specific comments array of objects to place comments on pull request changes (array, optional) + - For inline comments: provide `path`, `position` (or `line`), and `body` + - For multi-line comments: provide `path`, `start_line`, `line`, optional `side`/`start_side`, and `body` + +- **create_pull_request** - Create a new pull request + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `title`: PR title (string, required) + - `body`: PR description (string, optional) + - `head`: Branch containing changes (string, required) + - `base`: Branch to merge into (string, required) + - `draft`: Create as draft PR (boolean, optional) + - `maintainer_can_modify`: Allow maintainer edits (boolean, optional) + +- **add_pull_request_review_comment** - Add a review comment to a pull request or reply to an existing comment + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `pull_number`: Pull request number (number, required) + - `body`: The text of the review comment (string, required) + - `commit_id`: The SHA of the commit to comment on (string, required unless using in_reply_to) + - `path`: The relative path to the file that necessitates a comment (string, required unless using in_reply_to) + - `line`: The line of the blob in the pull request diff that the comment applies to (number, optional) + - `side`: The side of the diff to comment on (LEFT or RIGHT) (string, optional) + - `start_line`: For multi-line comments, the first line of the range (number, optional) + - `start_side`: For multi-line comments, the starting side of the diff (LEFT or RIGHT) (string, optional) + - `subject_type`: The level at which the comment is targeted (line or file) (string, optional) + - `in_reply_to`: The ID of the review comment to reply to (number, optional). When specified, only body is required and other parameters are ignored. + +- **update_pull_request** - Update an existing pull request in a GitHub repository + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `pullNumber`: Pull request number to update (number, required) + - `title`: New title (string, optional) + - `body`: New description (string, optional) + - `state`: New state ('open' or 'closed') (string, optional) + - `base`: New base branch name (string, optional) + - `maintainer_can_modify`: Allow maintainer edits (boolean, optional) + +### Repositories + +- **create_or_update_file** - Create or update a single file in a repository + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `path`: File path (string, required) + - `message`: Commit message (string, required) + - `content`: File content (string, required) + - `branch`: Branch name (string, optional) + - `sha`: File SHA if updating (string, optional) + +- **list_branches** - List branches in a GitHub repository + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `page`: Page number (number, optional) + - `perPage`: Results per page (number, optional) + +- **push_files** - Push multiple files in a single commit + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `branch`: Branch to push to (string, required) + - `files`: Files to push, each with path and content (array, required) + - `message`: Commit message (string, required) + +- **search_repositories** - Search for GitHub repositories + - `query`: Search query (string, required) + - `sort`: Sort field (string, optional) + - `order`: Sort order (string, optional) + - `page`: Page number (number, optional) + - `perPage`: Results per page (number, optional) + +- **create_repository** - Create a new GitHub repository + - `name`: Repository name (string, required) + - `description`: Repository description (string, optional) + - `private`: Whether the repository is private (boolean, optional) + - `autoInit`: Auto-initialize with README (boolean, optional) + +- **get_file_contents** - Get contents of a file or directory + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `path`: File path (string, required) + - `ref`: Git reference (string, optional) + +- **fork_repository** - Fork a repository + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `organization`: Target organization name (string, optional) + +- **create_branch** - Create a new branch + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `branch`: New branch name (string, required) + - `sha`: SHA to create branch from (string, required) + +- **list_commits** - Get a list of commits of a branch in a repository + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `sha`: Branch name, tag, or commit SHA (string, optional) + - `path`: Only commits containing this file path (string, optional) + - `page`: Page number (number, optional) + - `perPage`: Results per page (number, optional) + +- **get_commit** - Get details for a commit from a repository + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `sha`: Commit SHA, branch name, or tag name (string, required) + - `page`: Page number, for files in the commit (number, optional) + - `perPage`: Results per page, for files in the commit (number, optional) + +- **search_code** - Search for code across GitHub repositories + - `query`: Search query (string, required) + - `sort`: Sort field (string, optional) + - `order`: Sort order (string, optional) + - `page`: Page number (number, optional) + - `perPage`: Results per page (number, optional) + +### Users + +- **search_users** - Search for GitHub users + - `q`: Search query (string, required) + - `sort`: Sort field (string, optional) + - `order`: Sort order (string, optional) + - `page`: Page number (number, optional) + - `perPage`: Results per page (number, optional) + +### Code Scanning + +- **get_code_scanning_alert** - Get a code scanning alert + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `alertNumber`: Alert number (number, required) + +- **list_code_scanning_alerts** - List code scanning alerts for a repository + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `ref`: Git reference (string, optional) + - `state`: Alert state (string, optional) + - `severity`: Alert severity (string, optional) + - `tool_name`: The name of the tool used for code scanning (string, optional) + +### Secret Scanning + +- **get_secret_scanning_alert** - Get a secret scanning alert + + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `alertNumber`: Alert number (number, required) + +- **list_secret_scanning_alerts** - List secret scanning alerts for a repository + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `state`: Alert state (string, optional) + - `secret_type`: The secret types to be filtered for in a comma-separated list (string, optional) + - `resolution`: The resolution status (string, optional) + +## Resources + +### Repository Content + +- **Get Repository Content** + Retrieves the content of a repository at a specific path. + + - **Template**: `repo://{owner}/{repo}/contents{/path*}` + - **Parameters**: + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `path`: File or directory path (string, optional) + +- **Get Repository Content for a Specific Branch** + Retrieves the content of a repository at a specific path for a given branch. + + - **Template**: `repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}` + - **Parameters**: + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `branch`: Branch name (string, required) + - `path`: File or directory path (string, optional) + +- **Get Repository Content for a Specific Commit** + Retrieves the content of a repository at a specific path for a given commit. + + - **Template**: `repo://{owner}/{repo}/sha/{sha}/contents{/path*}` + - **Parameters**: + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `sha`: Commit SHA (string, required) + - `path`: File or directory path (string, optional) + +- **Get Repository Content for a Specific Tag** + Retrieves the content of a repository at a specific path for a given tag. + + - **Template**: `repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}` + - **Parameters**: + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `tag`: Tag name (string, required) + - `path`: File or directory path (string, optional) + +- **Get Repository Content for a Specific Pull Request** + Retrieves the content of a repository at a specific path for a given pull request. + + - **Template**: `repo://{owner}/{repo}/refs/pull/{prNumber}/head/contents{/path*}` + - **Parameters**: + - `owner`: Repository owner (string, required) + - `repo`: Repository name (string, required) + - `prNumber`: Pull request number (string, required) + - `path`: File or directory path (string, optional) + +## Library Usage + +The exported Go API of this module should currently be considered unstable, and subject to breaking changes. In the future, we may offer stability; please file an issue if there is a use case where this would be valuable. + +## License + +This project is licensed under the terms of the MIT open source license. Please refer to [MIT](./LICENSE) for the full terms. diff --git a/sre_agent/servers/github-mcp-server/SECURITY.md b/sre_agent/servers/github-mcp-server/SECURITY.md new file mode 100644 index 0000000..abe011d --- /dev/null +++ b/sre_agent/servers/github-mcp-server/SECURITY.md @@ -0,0 +1,31 @@ +Thanks for helping make GitHub safe for everyone. + +# Security + +GitHub takes the security of our software products and services seriously, including all of the open source code repositories managed through our GitHub organizations, such as [GitHub](https://github.com/GitHub). + +Even though [open source repositories are outside of the scope of our bug bounty program](https://bounty.github.com/index.html#scope) and therefore not eligible for bounty rewards, we will ensure that your finding gets passed along to the appropriate maintainers for remediation. + +## Reporting Security Issues + +If you believe you have found a security vulnerability in any GitHub-owned repository, please report it to us through coordinated disclosure. + +**Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.** + +Instead, please send an email to opensource-security[@]github.com. + +Please include as much of the information listed below as you can to help us better understand and resolve the issue: + + * The type of issue (e.g., buffer overflow, SQL injection, or cross-site scripting) + * Full paths of source file(s) related to the manifestation of the issue + * The location of the affected source code (tag/branch/commit or direct URL) + * Any special configuration required to reproduce the issue + * Step-by-step instructions to reproduce the issue + * Proof-of-concept or exploit code (if possible) + * Impact of the issue, including how an attacker might exploit the issue + +This information will help us triage your report more quickly. + +## Policy + +See [GitHub's Safe Harbor Policy](https://docs.github.com/en/site-policy/security-policies/github-bug-bounty-program-legal-safe-harbor#1-safe-harbor-terms) diff --git a/sre_agent/servers/github-mcp-server/SUPPORT.md b/sre_agent/servers/github-mcp-server/SUPPORT.md new file mode 100644 index 0000000..bc46c78 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/SUPPORT.md @@ -0,0 +1,13 @@ +# Support + +## How to file issues and get help + +This project uses GitHub issues to track bugs and feature requests. Please search the existing issues before filing new issues to avoid duplicates. For new issues, file your bug or feature request as a new issue. + +For help or questions about using this project, please open an issue. + +- The `github-mcp-server` is under active development and maintained by GitHub staff **AND THE COMMUNITY**. We will do our best to respond to support, feature requests, and community questions in a timely manner. + +## GitHub Support Policy + +Support for this project is limited to the resources listed above. diff --git a/sre_agent/servers/github-mcp-server/cmd/github-mcp-server/main.go b/sre_agent/servers/github-mcp-server/cmd/github-mcp-server/main.go new file mode 100644 index 0000000..ca168f4 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/cmd/github-mcp-server/main.go @@ -0,0 +1,327 @@ +package main + +import ( + "context" + "fmt" + "io" + stdlog "log" + "os" + "os/signal" + "syscall" + "net/http" + + "github.com/github/github-mcp-server/pkg/github" + iolog "github.com/github/github-mcp-server/pkg/log" + "github.com/github/github-mcp-server/pkg/translations" + gogithub "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var version = "version" +var commit = "commit" +var date = "date" + +var ( + rootCmd = &cobra.Command{ + Use: "server", + Short: "GitHub MCP Server", + Long: `A GitHub MCP server that handles various tools and resources.`, + Version: fmt.Sprintf("Version: %s\nCommit: %s\nBuild Date: %s", version, commit, date), + } + + stdioCmd = &cobra.Command{ + Use: "stdio", + Short: "Start stdio server", + Long: `Start a server that communicates via standard input/output streams using JSON-RPC messages.`, + Run: func(_ *cobra.Command, _ []string) { + logFile := viper.GetString("log-file") + readOnly := viper.GetBool("read-only") + exportTranslations := viper.GetBool("export-translations") + logger, err := initLogger(logFile) + if err != nil { + stdlog.Fatal("Failed to initialize logger:", err) + } + + // If you're wondering why we're not using viper.GetStringSlice("toolsets"), + // it's because viper doesn't handle comma-separated values correctly for env + // vars when using GetStringSlice. + // https://github.com/spf13/viper/issues/380 + var enabledToolsets []string + err = viper.UnmarshalKey("toolsets", &enabledToolsets) + if err != nil { + stdlog.Fatal("Failed to unmarshal toolsets:", err) + } + + logCommands := viper.GetBool("enable-command-logging") + cfg := runConfig{ + readOnly: readOnly, + logger: logger, + logCommands: logCommands, + exportTranslations: exportTranslations, + enabledToolsets: enabledToolsets, + } + if err := runStdioServer(cfg); err != nil { + stdlog.Fatal("failed to run stdio server:", err) + } + }, + } + + sseCmd = &cobra.Command{ + Use: "sse", + Short: "Start SSE server", + Long: `Start a server that communicates via Server-Sent Event (SSE) streams using JSON-RPC messages.`, + Run: func(_ *cobra.Command, _ []string) { + logFile := viper.GetString("log-file") + readOnly := viper.GetBool("read-only") + exportTranslations := viper.GetBool("export-translations") + logger, err := initLogger(logFile) + if err != nil { + stdlog.Fatal("Failed to initialize logger:", err) + } + + // If you're wondering why we're not using viper.GetStringSlice("toolsets"), + // it's because viper doesn't handle comma-separated values correctly for env + // vars when using GetStringSlice. + // https://github.com/spf13/viper/issues/380 + var enabledToolsets []string + err = viper.UnmarshalKey("toolsets", &enabledToolsets) + if err != nil { + stdlog.Fatal("Failed to unmarshal toolsets:", err) + } + + logCommands := viper.GetBool("enable-command-logging") + cfg := runConfig{ + readOnly: readOnly, + logger: logger, + logCommands: logCommands, + exportTranslations: exportTranslations, + enabledToolsets: enabledToolsets, + } + if err := runSSEServer(cfg); err != nil { + stdlog.Fatal("failed to run stdio server:", err) + } + }, + } +) + +func init() { + cobra.OnInitialize(initConfig) + + rootCmd.SetVersionTemplate("{{.Short}}\n{{.Version}}\n") + + // Add global flags that will be shared by all commands + rootCmd.PersistentFlags().StringSlice("toolsets", github.DefaultTools, "An optional comma separated list of groups of tools to allow, defaults to enabling all") + rootCmd.PersistentFlags().Bool("dynamic-toolsets", false, "Enable dynamic toolsets") + rootCmd.PersistentFlags().Bool("read-only", false, "Restrict the server to read-only operations") + rootCmd.PersistentFlags().String("log-file", "", "Path to log file") + rootCmd.PersistentFlags().Bool("enable-command-logging", false, "When enabled, the server will log all command requests and responses to the log file") + rootCmd.PersistentFlags().Bool("export-translations", false, "Save translations to a JSON file") + rootCmd.PersistentFlags().String("gh-host", "", "Specify the GitHub hostname (for GitHub Enterprise etc.)") + + // Bind flag to viper + _ = viper.BindPFlag("toolsets", rootCmd.PersistentFlags().Lookup("toolsets")) + _ = viper.BindPFlag("dynamic_toolsets", rootCmd.PersistentFlags().Lookup("dynamic-toolsets")) + _ = viper.BindPFlag("read-only", rootCmd.PersistentFlags().Lookup("read-only")) + _ = viper.BindPFlag("log-file", rootCmd.PersistentFlags().Lookup("log-file")) + _ = viper.BindPFlag("enable-command-logging", rootCmd.PersistentFlags().Lookup("enable-command-logging")) + _ = viper.BindPFlag("export-translations", rootCmd.PersistentFlags().Lookup("export-translations")) + _ = viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("gh-host")) + + // Add subcommands + rootCmd.AddCommand(stdioCmd) + rootCmd.AddCommand(sseCmd) +} + +func initConfig() { + // Initialize Viper configuration + viper.SetEnvPrefix("github") + viper.AutomaticEnv() +} + +func initLogger(outPath string) (*log.Logger, error) { + if outPath == "" { + return log.New(), nil + } + + file, err := os.OpenFile(outPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + if err != nil { + return nil, fmt.Errorf("failed to open log file: %w", err) + } + + logger := log.New() + logger.SetLevel(log.DebugLevel) + logger.SetOutput(file) + + return logger, nil +} + +type runConfig struct { + readOnly bool + logger *log.Logger + logCommands bool + exportTranslations bool + enabledToolsets []string +} + + +func createGhServer(cfg runConfig) (*server.MCPServer, error, func()) { + // Create GH client + token := viper.GetString("personal_access_token") + if token == "" { + cfg.logger.Fatal("GITHUB_PERSONAL_ACCESS_TOKEN not set") + } + ghClient := gogithub.NewClient(nil).WithAuthToken(token) + ghClient.UserAgent = fmt.Sprintf("github-mcp-server/%s", version) + + host := viper.GetString("host") + + if host != "" { + var err error + ghClient, err = ghClient.WithEnterpriseURLs(host, host) + if err != nil { + return nil, fmt.Errorf("failed to create GitHub client with host: %w", err), nil + } + } + + t, dumpTranslations := translations.TranslationHelper() + + beforeInit := func(_ context.Context, _ any, message *mcp.InitializeRequest) { + ghClient.UserAgent = fmt.Sprintf("github-mcp-server/%s (%s/%s)", version, message.Params.ClientInfo.Name, message.Params.ClientInfo.Version) + } + + getClient := func(_ context.Context) (*gogithub.Client, error) { + return ghClient, nil // closing over client + } + + hooks := &server.Hooks{ + OnBeforeInitialize: []server.OnBeforeInitializeFunc{beforeInit}, + } + // Create server + ghServer := github.NewServer(version, server.WithHooks(hooks)) + + enabled := cfg.enabledToolsets + dynamic := viper.GetBool("dynamic_toolsets") + if dynamic { + // filter "all" from the enabled toolsets + enabled = make([]string, 0, len(cfg.enabledToolsets)) + for _, toolset := range cfg.enabledToolsets { + if toolset != "all" { + enabled = append(enabled, toolset) + } + } + } + + // Create default toolsets + toolsets, err := github.InitToolsets(enabled, cfg.readOnly, getClient, t) + context := github.InitContextToolset(getClient, t) + + if err != nil { + stdlog.Fatal("Failed to initialize toolsets:", err) + } + + // Register resources with the server + github.RegisterResources(ghServer, getClient, t) + // Register the tools with the server + toolsets.RegisterTools(ghServer) + context.RegisterTools(ghServer) + + if dynamic { + dynamic := github.InitDynamicToolset(ghServer, toolsets, t) + dynamic.RegisterTools(ghServer) + } + + return ghServer, nil, dumpTranslations +} + + +func runStdioServer(cfg runConfig) error { + // Create app context + ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + + ghServer, err, dumpTranslations := createGhServer(cfg) + + if err != nil { + return err + } + + stdioServer := server.NewStdioServer(ghServer) + + stdLogger := stdlog.New(cfg.logger.Writer(), "stdioserver", 0) + stdioServer.SetErrorLogger(stdLogger) + + if cfg.exportTranslations { + // Once server is initialized, all translations are loaded + dumpTranslations() + } + + // Start listening for messages + errC := make(chan error, 1) + go func() { + in, out := io.Reader(os.Stdin), io.Writer(os.Stdout) + + if cfg.logCommands { + loggedIO := iolog.NewIOLogger(in, out, cfg.logger) + in, out = loggedIO, loggedIO + } + + errC <- stdioServer.Listen(ctx, in, out) + }() + + // Output github-mcp-server string + _, _ = fmt.Fprintf(os.Stderr, "GitHub MCP Server running on stdio\n") + + // Wait for shutdown signal + select { + case <-ctx.Done(): + cfg.logger.Infof("shutting down server...") + case err := <-errC: + if err != nil { + return fmt.Errorf("error running server: %w", err) + } + } + + return nil +} + + +func runSSEServer(cfg runConfig) error { + // Create app context + _, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) + defer stop() + + ghServer, err, _ := createGhServer(cfg) + + if err != nil { + return err + } + + sseServer := server.NewSSEServer( + ghServer, + server.WithBaseURL("http://github:3001"), + server.WithUseFullURLForMessageEndpoint(true), + ) + + mux := http.NewServeMux() + mux.Handle("/sse", sseServer.SSEHandler()) + mux.Handle("/message", sseServer.MessageHandler()) + + log.Printf("Dynamic SSE server listening on %s", ":3001") + if err := http.ListenAndServe(":3001", mux); err != nil { + log.Fatalf("Server error: %v", err) + } + + return nil +} + + +func main() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/sre_agent/servers/github-mcp-server/cmd/mcpcurl/README.md b/sre_agent/servers/github-mcp-server/cmd/mcpcurl/README.md new file mode 100644 index 0000000..86f9174 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/cmd/mcpcurl/README.md @@ -0,0 +1,130 @@ +# mcpcurl + +A CLI tool that dynamically builds commands based on schemas retrieved from MCP servers that can +be executed against the configured MCP server. + +## Overview + +`mcpcurl` is a command-line interface that: + +1. Connects to an MCP server via stdio +2. Dynamically retrieves the available tools schema +3. Generates CLI commands corresponding to each tool +4. Handles parameter validation based on the schema +5. Executes commands and displays responses + +## Installation + +## Usage + +```bash +mcpcurl --stdio-server-cmd="" [flags] +``` + +The `--stdio-server-cmd` flag is required for all commands and specifies the command to run the MCP server. + +### Available Commands + +- `tools`: Contains all dynamically generated tool commands from the schema +- `schema`: Fetches and displays the raw schema from the MCP server +- `help`: Shows help for any command + +### Examples + +List available tools in Anthropic's MCP server: + +```bash +% ./mcpcurl --stdio-server-cmd "docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN mcp/github" tools --help +Contains all dynamically generated tool commands from the schema + +Usage: + mcpcurl tools [command] + +Available Commands: + add_issue_comment Add a comment to an existing issue + create_branch Create a new branch in a GitHub repository + create_issue Create a new issue in a GitHub repository + create_or_update_file Create or update a single file in a GitHub repository + create_pull_request Create a new pull request in a GitHub repository + create_repository Create a new GitHub repository in your account + fork_repository Fork a GitHub repository to your account or specified organization + get_file_contents Get the contents of a file or directory from a GitHub repository + get_issue Get details of a specific issue in a GitHub repository + get_issue_comments Get comments for a GitHub issue + list_commits Get list of commits of a branch in a GitHub repository + list_issues List issues in a GitHub repository with filtering options + push_files Push multiple files to a GitHub repository in a single commit + search_code Search for code across GitHub repositories + search_issues Search for issues and pull requests across GitHub repositories + search_repositories Search for GitHub repositories + search_users Search for users on GitHub + update_issue Update an existing issue in a GitHub repository + +Flags: + -h, --help help for tools + +Global Flags: + --pretty Pretty print MCP response (only for JSON responses) (default true) + --stdio-server-cmd string Shell command to invoke MCP server via stdio (required) + +Use "mcpcurl tools [command] --help" for more information about a command. +``` + +Get help for a specific tool: + +```bash + % ./mcpcurl --stdio-server-cmd "docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN mcp/github" tools get_issue --help +Get details of a specific issue in a GitHub repository + +Usage: + mcpcurl tools get_issue [flags] + +Flags: + -h, --help help for get_issue + --issue_number float + --owner string + --repo string + +Global Flags: + --pretty Pretty print MCP response (only for JSON responses) (default true) + --stdio-server-cmd string Shell command to invoke MCP server via stdio (required) + +``` + +Use one of the tools: + +```bash + % ./mcpcurl --stdio-server-cmd "docker run -i --rm -e GITHUB_PERSONAL_ACCESS_TOKEN mcp/github" tools get_issue --owner golang --repo go --issue_number 1 +{ + "active_lock_reason": null, + "assignee": null, + "assignees": [], + "author_association": "CONTRIBUTOR", + "body": "by **rsc+personal@swtch.com**:\n\n\u003cpre\u003eWhat steps will reproduce the problem?\n1. Run build on Ubuntu 9.10, which uses gcc 4.4.1\n\nWhat is the expected output? What do you see instead?\n\nCgo fails with the following error:\n\n{{{\ngo/misc/cgo/stdio$ make\ncgo file.go\ncould not determine kind of name for C.CString\ncould not determine kind of name for C.puts\ncould not determine kind of name for C.fflushstdout\ncould not determine kind of name for C.free\nthrow: sys·mapaccess1: key not in map\n\npanic PC=0x2b01c2b96a08\nthrow+0x33 /media/scratch/workspace/go/src/pkg/runtime/runtime.c:71\n throw(0x4d2daf, 0x0)\nsys·mapaccess1+0x74 \n/media/scratch/workspace/go/src/pkg/runtime/hashmap.c:769\n sys·mapaccess1(0xc2b51930, 0x2b01)\nmain·*Prog·loadDebugInfo+0xa67 \n/media/scratch/workspace/go/src/cmd/cgo/gcc.go:164\n main·*Prog·loadDebugInfo(0xc2bc0000, 0x2b01)\nmain·main+0x352 \n/media/scratch/workspace/go/src/cmd/cgo/main.go:68\n main·main()\nmainstart+0xf \n/media/scratch/workspace/go/src/pkg/runtime/amd64/asm.s:55\n mainstart()\ngoexit /media/scratch/workspace/go/src/pkg/runtime/proc.c:133\n goexit()\nmake: *** [file.cgo1.go] Error 2\n}}}\n\nPlease use labels and text to provide additional information.\u003c/pre\u003e\n", + "closed_at": "2014-12-08T10:02:16Z", + "closed_by": null, + "comments": 12, + "comments_url": "https://api.github.com/repos/golang/go/issues/1/comments", + "created_at": "2009-10-22T06:07:26Z", + "events_url": "https://api.github.com/repos/golang/go/issues/1/events", + [...] +} +``` + +## Dynamic Commands + +All tools provided by the MCP server are automatically available as subcommands under the `tools` command. Each generated command has: + +- Appropriate flags matching the tool's input schema +- Validation for required parameters +- Type validation +- Enum validation (for string parameters with allowable values) +- Help text generated from the tool's description + +## How It Works + +1. `mcpcurl` makes a JSON-RPC request to the server using the `tools/list` method +2. The server responds with a schema describing all available tools +3. `mcpcurl` dynamically builds a command structure based on this schema +4. When a command is executed, arguments are converted to a JSON-RPC request +5. The request is sent to the server via stdin, and the response is printed to stdout diff --git a/sre_agent/servers/github-mcp-server/cmd/mcpcurl/main.go b/sre_agent/servers/github-mcp-server/cmd/mcpcurl/main.go new file mode 100644 index 0000000..dfc639b --- /dev/null +++ b/sre_agent/servers/github-mcp-server/cmd/mcpcurl/main.go @@ -0,0 +1,459 @@ +package main + +import ( + "bytes" + "crypto/rand" + "encoding/json" + "fmt" + "io" + "math/big" + "os" + "os/exec" + "slices" + "strings" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +type ( + // SchemaResponse represents the top-level response containing tools + SchemaResponse struct { + Result Result `json:"result"` + JSONRPC string `json:"jsonrpc"` + ID int `json:"id"` + } + + // Result contains the list of available tools + Result struct { + Tools []Tool `json:"tools"` + } + + // Tool represents a single command with its schema + Tool struct { + Name string `json:"name"` + Description string `json:"description"` + InputSchema InputSchema `json:"inputSchema"` + } + + // InputSchema defines the structure of a tool's input parameters + InputSchema struct { + Type string `json:"type"` + Properties map[string]Property `json:"properties"` + Required []string `json:"required"` + AdditionalProperties bool `json:"additionalProperties"` + Schema string `json:"$schema"` + } + + // Property defines a single parameter's type and constraints + Property struct { + Type string `json:"type"` + Description string `json:"description"` + Enum []string `json:"enum,omitempty"` + Minimum *float64 `json:"minimum,omitempty"` + Maximum *float64 `json:"maximum,omitempty"` + Items *PropertyItem `json:"items,omitempty"` + } + + // PropertyItem defines the type of items in an array property + PropertyItem struct { + Type string `json:"type"` + Properties map[string]Property `json:"properties,omitempty"` + Required []string `json:"required,omitempty"` + AdditionalProperties bool `json:"additionalProperties,omitempty"` + } + + // JSONRPCRequest represents a JSON-RPC 2.0 request + JSONRPCRequest struct { + JSONRPC string `json:"jsonrpc"` + ID int `json:"id"` + Method string `json:"method"` + Params RequestParams `json:"params"` + } + + // RequestParams contains the tool name and arguments + RequestParams struct { + Name string `json:"name"` + Arguments map[string]interface{} `json:"arguments"` + } + + // Define structure to match the response format + Content struct { + Type string `json:"type"` + Text string `json:"text"` + } + + ResponseResult struct { + Content []Content `json:"content"` + } + + Response struct { + Result ResponseResult `json:"result"` + JSONRPC string `json:"jsonrpc"` + ID int `json:"id"` + } +) + +var ( + // Create root command + rootCmd = &cobra.Command{ + Use: "mcpcurl", + Short: "CLI tool with dynamically generated commands", + Long: "A CLI tool for interacting with MCP API based on dynamically loaded schemas", + PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { + // Skip validation for help and completion commands + if cmd.Name() == "help" || cmd.Name() == "completion" { + return nil + } + + // Check if the required global flag is provided + serverCmd, _ := cmd.Flags().GetString("stdio-server-cmd") + if serverCmd == "" { + return fmt.Errorf("--stdio-server-cmd is required") + } + return nil + }, + } + + // Add schema command + schemaCmd = &cobra.Command{ + Use: "schema", + Short: "Fetch schema from MCP server", + Long: "Fetches the tools schema from the MCP server specified by --stdio-server-cmd", + RunE: func(cmd *cobra.Command, _ []string) error { + serverCmd, _ := cmd.Flags().GetString("stdio-server-cmd") + if serverCmd == "" { + return fmt.Errorf("--stdio-server-cmd is required") + } + + // Build the JSON-RPC request for tools/list + jsonRequest, err := buildJSONRPCRequest("tools/list", "", nil) + if err != nil { + return fmt.Errorf("failed to build JSON-RPC request: %w", err) + } + + // Execute the server command and pass the JSON-RPC request + response, err := executeServerCommand(serverCmd, jsonRequest) + if err != nil { + return fmt.Errorf("error executing server command: %w", err) + } + + // Output the response + fmt.Println(response) + return nil + }, + } + + // Create the tools command + toolsCmd = &cobra.Command{ + Use: "tools", + Short: "Access available tools", + Long: "Contains all dynamically generated tool commands from the schema", + } +) + +func main() { + rootCmd.AddCommand(schemaCmd) + + // Add global flag for stdio server command + rootCmd.PersistentFlags().String("stdio-server-cmd", "", "Shell command to invoke MCP server via stdio (required)") + _ = rootCmd.MarkPersistentFlagRequired("stdio-server-cmd") + + // Add global flag for pretty printing + rootCmd.PersistentFlags().Bool("pretty", true, "Pretty print MCP response (only for JSON or JSONL responses)") + + // Add the tools command to the root command + rootCmd.AddCommand(toolsCmd) + + // Execute the root command once to parse flags + _ = rootCmd.ParseFlags(os.Args[1:]) + + // Get pretty flag + prettyPrint, err := rootCmd.Flags().GetBool("pretty") + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "Error getting pretty flag: %v\n", err) + os.Exit(1) + } + // Get server command + serverCmd, err := rootCmd.Flags().GetString("stdio-server-cmd") + if err == nil && serverCmd != "" { + // Fetch schema from server + jsonRequest, err := buildJSONRPCRequest("tools/list", "", nil) + if err == nil { + response, err := executeServerCommand(serverCmd, jsonRequest) + if err == nil { + // Parse the schema response + var schemaResp SchemaResponse + if err := json.Unmarshal([]byte(response), &schemaResp); err == nil { + // Add all the generated commands as subcommands of tools + for _, tool := range schemaResp.Result.Tools { + addCommandFromTool(toolsCmd, &tool, prettyPrint) + } + } + } + } + } + + // Execute + if err := rootCmd.Execute(); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "Error executing command: %v\n", err) + os.Exit(1) + } +} + +// addCommandFromTool creates a cobra command from a tool schema +func addCommandFromTool(toolsCmd *cobra.Command, tool *Tool, prettyPrint bool) { + // Create command from tool + cmd := &cobra.Command{ + Use: tool.Name, + Short: tool.Description, + Run: func(cmd *cobra.Command, _ []string) { + // Build a map of arguments from flags + arguments, err := buildArgumentsMap(cmd, tool) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "failed to build arguments map: %v\n", err) + return + } + + jsonData, err := buildJSONRPCRequest("tools/call", tool.Name, arguments) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "failed to build JSONRPC request: %v\n", err) + return + } + + // Execute the server command + serverCmd, err := cmd.Flags().GetString("stdio-server-cmd") + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "failed to get stdio-server-cmd: %v\n", err) + return + } + response, err := executeServerCommand(serverCmd, jsonData) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "error executing server command: %v\n", err) + return + } + if err := printResponse(response, prettyPrint); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "error printing response: %v\n", err) + return + } + }, + } + + // Initialize viper for this command + viperInit := func() { + viper.Reset() + viper.AutomaticEnv() + viper.SetEnvPrefix(strings.ToUpper(tool.Name)) + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) + } + + // We'll call the init function directly instead of with cobra.OnInitialize + // to avoid conflicts between commands + viperInit() + + // Add flags based on schema properties + for name, prop := range tool.InputSchema.Properties { + isRequired := slices.Contains(tool.InputSchema.Required, name) + + // Enhance description to indicate if parameter is optional + description := prop.Description + if !isRequired { + description += " (optional)" + } + + switch prop.Type { + case "string": + cmd.Flags().String(name, "", description) + if len(prop.Enum) > 0 { + // Add validation in PreRun for enum values + cmd.PreRunE = func(cmd *cobra.Command, _ []string) error { + for flagName, property := range tool.InputSchema.Properties { + if len(property.Enum) > 0 { + value, _ := cmd.Flags().GetString(flagName) + if value != "" && !slices.Contains(property.Enum, value) { + return fmt.Errorf("%s must be one of: %s", flagName, strings.Join(property.Enum, ", ")) + } + } + } + return nil + } + } + case "number": + cmd.Flags().Float64(name, 0, description) + case "boolean": + cmd.Flags().Bool(name, false, description) + case "array": + if prop.Items != nil { + if prop.Items.Type == "string" { + cmd.Flags().StringSlice(name, []string{}, description) + } else if prop.Items.Type == "object" { + // For complex objects in arrays, we'll use a JSON string that users can provide + cmd.Flags().String(name+"-json", "", description+" (provide as JSON array)") + } + } + } + + if isRequired { + _ = cmd.MarkFlagRequired(name) + } + + // Bind flag to viper + _ = viper.BindPFlag(name, cmd.Flags().Lookup(name)) + } + + // Add command to root + toolsCmd.AddCommand(cmd) +} + +// buildArgumentsMap extracts flag values into a map of arguments +func buildArgumentsMap(cmd *cobra.Command, tool *Tool) (map[string]interface{}, error) { + arguments := make(map[string]interface{}) + + for name, prop := range tool.InputSchema.Properties { + switch prop.Type { + case "string": + if value, _ := cmd.Flags().GetString(name); value != "" { + arguments[name] = value + } + case "number": + if value, _ := cmd.Flags().GetFloat64(name); value != 0 { + arguments[name] = value + } + case "boolean": + // For boolean, we need to check if it was explicitly set + if cmd.Flags().Changed(name) { + value, _ := cmd.Flags().GetBool(name) + arguments[name] = value + } + case "array": + if prop.Items != nil { + if prop.Items.Type == "string" { + if values, _ := cmd.Flags().GetStringSlice(name); len(values) > 0 { + arguments[name] = values + } + } else if prop.Items.Type == "object" { + if jsonStr, _ := cmd.Flags().GetString(name + "-json"); jsonStr != "" { + var jsonArray []interface{} + if err := json.Unmarshal([]byte(jsonStr), &jsonArray); err != nil { + return nil, fmt.Errorf("error parsing JSON for %s: %w", name, err) + } + arguments[name] = jsonArray + } + } + } + } + } + + return arguments, nil +} + +// buildJSONRPCRequest creates a JSON-RPC request with the given tool name and arguments +func buildJSONRPCRequest(method, toolName string, arguments map[string]interface{}) (string, error) { + id, err := rand.Int(rand.Reader, big.NewInt(10000)) + if err != nil { + return "", fmt.Errorf("failed to generate random ID: %w", err) + } + request := JSONRPCRequest{ + JSONRPC: "2.0", + ID: int(id.Int64()), // Random ID between 0 and 9999 + Method: method, + Params: RequestParams{ + Name: toolName, + Arguments: arguments, + }, + } + jsonData, err := json.Marshal(request) + if err != nil { + return "", fmt.Errorf("failed to marshal JSON request: %w", err) + } + return string(jsonData), nil +} + +// executeServerCommand runs the specified command, sends the JSON request to stdin, +// and returns the response from stdout +func executeServerCommand(cmdStr, jsonRequest string) (string, error) { + // Split the command string into command and arguments + cmdParts := strings.Fields(cmdStr) + if len(cmdParts) == 0 { + return "", fmt.Errorf("empty command") + } + + cmd := exec.Command(cmdParts[0], cmdParts[1:]...) //nolint:gosec //mcpcurl is a test command that needs to execute arbitrary shell commands + + // Setup stdin pipe + stdin, err := cmd.StdinPipe() + if err != nil { + return "", fmt.Errorf("failed to create stdin pipe: %w", err) + } + + // Setup stdout and stderr pipes + var stdout, stderr bytes.Buffer + cmd.Stdout = &stdout + cmd.Stderr = &stderr + + // Start the command + if err := cmd.Start(); err != nil { + return "", fmt.Errorf("failed to start command: %w", err) + } + + // Write the JSON request to stdin + if _, err := io.WriteString(stdin, jsonRequest+"\n"); err != nil { + return "", fmt.Errorf("failed to write to stdin: %w", err) + } + _ = stdin.Close() + + // Wait for the command to complete + if err := cmd.Wait(); err != nil { + return "", fmt.Errorf("command failed: %w, stderr: %s", err, stderr.String()) + } + + return stdout.String(), nil +} + +func printResponse(response string, prettyPrint bool) error { + if !prettyPrint { + fmt.Println(response) + return nil + } + + // Parse the JSON response + var resp Response + if err := json.Unmarshal([]byte(response), &resp); err != nil { + return fmt.Errorf("failed to parse JSON: %w", err) + } + + // Extract text from content items of type "text" + for _, content := range resp.Result.Content { + if content.Type == "text" { + var textContentObj map[string]interface{} + err := json.Unmarshal([]byte(content.Text), &textContentObj) + + if err == nil { + prettyText, err := json.MarshalIndent(textContentObj, "", " ") + if err != nil { + return fmt.Errorf("failed to pretty print text content: %w", err) + } + fmt.Println(string(prettyText)) + continue + } + + // Fallback parsing as JSONL + var textContentList []map[string]interface{} + if err := json.Unmarshal([]byte(content.Text), &textContentList); err != nil { + return fmt.Errorf("failed to parse text content as a list: %w", err) + } + prettyText, err := json.MarshalIndent(textContentList, "", " ") + if err != nil { + return fmt.Errorf("failed to pretty print array content: %w", err) + } + fmt.Println(string(prettyText)) + } + } + + // If no text content found, print the original response + if len(resp.Result.Content) == 0 { + fmt.Println(response) + } + + return nil +} diff --git a/sre_agent/servers/github-mcp-server/e2e/README.md b/sre_agent/servers/github-mcp-server/e2e/README.md new file mode 100644 index 0000000..bb93b32 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/e2e/README.md @@ -0,0 +1,86 @@ +# End To End (e2e) Tests + +The purpose of the E2E tests is to have a simple (currently) test that gives maintainers some confidence in the black box behavior of our artifacts. It does this by: + * Building the `github-mcp-server` docker image + * Running the image + * Interacting with the server via stdio + * Issuing requests that interact with the live GitHub API + +## Running the Tests + +A service must be running that supports image building and container creation via the `docker` CLI. + +Since these tests require a token to interact with real resources on the GitHub API, it is gated behind the `e2e` build flag. + +``` +GITHUB_MCP_SERVER_E2E_TOKEN= go test -v --tags e2e ./e2e +``` + +The `GITHUB_MCP_SERVER_E2E_TOKEN` environment variable is mapped to `GITHUB_PERSONAL_ACCESS_TOKEN` internally, but separated to avoid accidental reuse of credentials. + +## Example + +The following diff adjusts the `get_me` tool to return `foobar` as the user login. + +```diff +diff --git a/pkg/github/context_tools.go b/pkg/github/context_tools.go +index 1c91d70..ac4ef2b 100644 +--- a/pkg/github/context_tools.go ++++ b/pkg/github/context_tools.go +@@ -39,6 +39,8 @@ func GetMe(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mc + return mcp.NewToolResultError(fmt.Sprintf("failed to get user: %s", string(body))), nil + } + ++ user.Login = sPtr("foobar") ++ + r, err := json.Marshal(user) + if err != nil { + return nil, fmt.Errorf("failed to marshal user: %w", err) +@@ -47,3 +49,7 @@ func GetMe(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mc + return mcp.NewToolResultText(string(r)), nil + } + } ++ ++func sPtr(s string) *string { ++ return &s ++} +``` + +Running the tests: + +``` +➜ GITHUB_MCP_SERVER_E2E_TOKEN=$(gh auth token) go test -v --tags e2e ./e2e +=== RUN TestE2E + e2e_test.go:92: Building Docker image for e2e tests... + e2e_test.go:36: Starting Stdio MCP client... +=== RUN TestE2E/Initialize +=== RUN TestE2E/CallTool_get_me + e2e_test.go:85: + Error Trace: /Users/williammartin/workspace/github-mcp-server/e2e/e2e_test.go:85 + Error: Not equal: + expected: "foobar" + actual : "williammartin" + + Diff: + --- Expected + +++ Actual + @@ -1 +1 @@ + -foobar + +williammartin + Test: TestE2E/CallTool_get_me + Messages: expected login to match +--- FAIL: TestE2E (1.05s) + --- PASS: TestE2E/Initialize (0.09s) + --- FAIL: TestE2E/CallTool_get_me (0.46s) +FAIL +FAIL github.com/github/github-mcp-server/e2e 1.433s +FAIL +``` + +## Limitations + +The current test suite is intentionally very limited in scope. This is because the maintenance costs on e2e tests tend to increase significantly over time. To read about some challenges with GitHub integration tests, see [go-github integration tests README](https://github.com/google/go-github/blob/5b75aa86dba5cf4af2923afa0938774f37fa0a67/test/README.md). We will expand this suite circumspectly! + +The tests are quite repetitive and verbose. This is intentional as we want to see them develop more before committing to abstractions. + +Currently, visibility into failures is not particularly good. We're hoping that we can pull apart the mcp-go client and have it hook into streams representing stdio without requiring an exec. This way we can get breakpoints in the debugger easily. diff --git a/sre_agent/servers/github-mcp-server/e2e/e2e_test.go b/sre_agent/servers/github-mcp-server/e2e/e2e_test.go new file mode 100644 index 0000000..5da6379 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/e2e/e2e_test.go @@ -0,0 +1,344 @@ +//go:build e2e + +package e2e_test + +import ( + "context" + "encoding/json" + "fmt" + "os" + "os/exec" + "slices" + "sync" + "testing" + "time" + + "github.com/google/go-github/v69/github" + mcpClient "github.com/mark3labs/mcp-go/client" + "github.com/mark3labs/mcp-go/mcp" + "github.com/stretchr/testify/require" +) + +var ( + // Shared variables and sync.Once instances to ensure one-time execution + getTokenOnce sync.Once + token string + + buildOnce sync.Once + buildError error +) + +// getE2EToken ensures the environment variable is checked only once and returns the token +func getE2EToken(t *testing.T) string { + getTokenOnce.Do(func() { + token = os.Getenv("GITHUB_MCP_SERVER_E2E_TOKEN") + if token == "" { + t.Fatalf("GITHUB_MCP_SERVER_E2E_TOKEN environment variable is not set") + } + }) + return token +} + +// ensureDockerImageBuilt makes sure the Docker image is built only once across all tests +func ensureDockerImageBuilt(t *testing.T) { + buildOnce.Do(func() { + t.Log("Building Docker image for e2e tests...") + cmd := exec.Command("docker", "build", "-t", "github/e2e-github-mcp-server", ".") + cmd.Dir = ".." // Run this in the context of the root, where the Dockerfile is located. + output, err := cmd.CombinedOutput() + buildError = err + if err != nil { + t.Logf("Docker build output: %s", string(output)) + } + }) + + // Check if the build was successful + require.NoError(t, buildError, "expected to build Docker image successfully") +} + +// ClientOpts holds configuration options for the MCP client setup +type ClientOpts struct { + // Environment variables to set before starting the client + EnvVars map[string]string +} + +// ClientOption defines a function type for configuring ClientOpts +type ClientOption func(*ClientOpts) + +// WithEnvVars returns an option that adds environment variables to the client options +func WithEnvVars(envVars map[string]string) ClientOption { + return func(opts *ClientOpts) { + opts.EnvVars = envVars + } +} + +// setupMCPClient sets up the test environment and returns an initialized MCP client +// It handles token retrieval, Docker image building, and applying the provided options +func setupMCPClient(t *testing.T, options ...ClientOption) *mcpClient.Client { + // Get token and ensure Docker image is built + token := getE2EToken(t) + ensureDockerImageBuilt(t) + + // Create and configure options + opts := &ClientOpts{ + EnvVars: make(map[string]string), + } + + // Apply all options to configure the opts struct + for _, option := range options { + option(opts) + } + + // Prepare Docker arguments + args := []string{ + "docker", + "run", + "-i", + "--rm", + "-e", + "GITHUB_PERSONAL_ACCESS_TOKEN", // Personal access token is all required + } + + // Add all environment variables to the Docker arguments + for key := range opts.EnvVars { + args = append(args, "-e", key) + } + + // Add the image name + args = append(args, "github/e2e-github-mcp-server") + + // Construct the env vars for the MCP Client to execute docker with + dockerEnvVars := make([]string, 0, len(opts.EnvVars)+1) + dockerEnvVars = append(dockerEnvVars, fmt.Sprintf("GITHUB_PERSONAL_ACCESS_TOKEN=%s", token)) + for key, value := range opts.EnvVars { + dockerEnvVars = append(dockerEnvVars, fmt.Sprintf("%s=%s", key, value)) + } + + // Create the client + t.Log("Starting Stdio MCP client...") + client, err := mcpClient.NewStdioMCPClient(args[0], dockerEnvVars, args[1:]...) + require.NoError(t, err, "expected to create client successfully") + t.Cleanup(func() { + require.NoError(t, client.Close(), "expected to close client successfully") + }) + + // Initialize the client + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + request := mcp.InitializeRequest{} + request.Params.ProtocolVersion = "2025-03-26" + request.Params.ClientInfo = mcp.Implementation{ + Name: "e2e-test-client", + Version: "0.0.1", + } + + result, err := client.Initialize(ctx, request) + require.NoError(t, err, "failed to initialize client") + require.Equal(t, "github-mcp-server", result.ServerInfo.Name, "unexpected server name") + + return client +} + +func TestGetMe(t *testing.T) { + t.Parallel() + + mcpClient := setupMCPClient(t) + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + // When we call the "get_me" tool + request := mcp.CallToolRequest{} + request.Params.Name = "get_me" + + response, err := mcpClient.CallTool(ctx, request) + require.NoError(t, err, "expected to call 'get_me' tool successfully") + + require.False(t, response.IsError, "expected result not to be an error") + require.Len(t, response.Content, 1, "expected content to have one item") + + textContent, ok := response.Content[0].(mcp.TextContent) + require.True(t, ok, "expected content to be of type TextContent") + + var trimmedContent struct { + Login string `json:"login"` + } + err = json.Unmarshal([]byte(textContent.Text), &trimmedContent) + require.NoError(t, err, "expected to unmarshal text content successfully") + + // Then the login in the response should match the login obtained via the same + // token using the GitHub API. + ghClient := github.NewClient(nil).WithAuthToken(getE2EToken(t)) + user, _, err := ghClient.Users.Get(context.Background(), "") + require.NoError(t, err, "expected to get user successfully") + require.Equal(t, trimmedContent.Login, *user.Login, "expected login to match") + +} + +func TestToolsets(t *testing.T) { + t.Parallel() + + mcpClient := setupMCPClient( + t, + WithEnvVars(map[string]string{ + "GITHUB_TOOLSETS": "repos,issues", + }), + ) + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + request := mcp.ListToolsRequest{} + response, err := mcpClient.ListTools(ctx, request) + require.NoError(t, err, "expected to list tools successfully") + + // We could enumerate the tools here, but we'll need to expose that information + // declaratively in the MCP server, so for the moment let's just check the existence + // of an issue and repo tool, and the non-existence of a pull_request tool. + var toolsContains = func(expectedName string) bool { + return slices.ContainsFunc(response.Tools, func(tool mcp.Tool) bool { + return tool.Name == expectedName + }) + } + + require.True(t, toolsContains("get_issue"), "expected to find 'get_issue' tool") + require.True(t, toolsContains("list_branches"), "expected to find 'list_branches' tool") + require.False(t, toolsContains("get_pull_request"), "expected not to find 'get_pull_request' tool") +} + +func TestTags(t *testing.T) { + mcpClient := setupMCPClient(t) + + ctx := context.Background() + + // First, who am I + getMeRequest := mcp.CallToolRequest{} + getMeRequest.Params.Name = "get_me" + + t.Log("Getting current user...") + resp, err := mcpClient.CallTool(ctx, getMeRequest) + require.NoError(t, err, "expected to call 'get_me' tool successfully") + require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp)) + + require.False(t, resp.IsError, "expected result not to be an error") + require.Len(t, resp.Content, 1, "expected content to have one item") + + textContent, ok := resp.Content[0].(mcp.TextContent) + require.True(t, ok, "expected content to be of type TextContent") + + var trimmedGetMeText struct { + Login string `json:"login"` + } + err = json.Unmarshal([]byte(textContent.Text), &trimmedGetMeText) + require.NoError(t, err, "expected to unmarshal text content successfully") + + currentOwner := trimmedGetMeText.Login + + // Then create a repository with a README (via autoInit) + repoName := fmt.Sprintf("github-mcp-server-e2e-%s-%d", t.Name(), time.Now().UnixMilli()) + createRepoRequest := mcp.CallToolRequest{} + createRepoRequest.Params.Name = "create_repository" + createRepoRequest.Params.Arguments = map[string]any{ + "name": repoName, + "private": true, + "autoInit": true, + } + + t.Logf("Creating repository %s/%s...", currentOwner, repoName) + _, err = mcpClient.CallTool(ctx, createRepoRequest) + require.NoError(t, err, "expected to call 'get_me' tool successfully") + require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp)) + + // Cleanup the repository after the test + t.Cleanup(func() { + // MCP Server doesn't support deletions, but we can use the GitHub Client + ghClient := github.NewClient(nil).WithAuthToken(getE2EToken(t)) + t.Logf("Deleting repository %s/%s...", currentOwner, repoName) + _, err := ghClient.Repositories.Delete(context.Background(), currentOwner, repoName) + require.NoError(t, err, "expected to delete repository successfully") + }) + + // Then create a tag + // MCP Server doesn't support tag creation, but we can use the GitHub Client + ghClient := github.NewClient(nil).WithAuthToken(getE2EToken(t)) + t.Logf("Creating tag %s/%s:%s...", currentOwner, repoName, "v0.0.1") + ref, _, err := ghClient.Git.GetRef(context.Background(), currentOwner, repoName, "refs/heads/main") + require.NoError(t, err, "expected to get ref successfully") + + tagObj, _, err := ghClient.Git.CreateTag(context.Background(), currentOwner, repoName, &github.Tag{ + Tag: github.Ptr("v0.0.1"), + Message: github.Ptr("v0.0.1"), + Object: &github.GitObject{ + SHA: ref.Object.SHA, + Type: github.Ptr("commit"), + }, + }) + require.NoError(t, err, "expected to create tag object successfully") + + _, _, err = ghClient.Git.CreateRef(context.Background(), currentOwner, repoName, &github.Reference{ + Ref: github.Ptr("refs/tags/v0.0.1"), + Object: &github.GitObject{ + SHA: tagObj.SHA, + }, + }) + require.NoError(t, err, "expected to create tag ref successfully") + + // List the tags + listTagsRequest := mcp.CallToolRequest{} + listTagsRequest.Params.Name = "list_tags" + listTagsRequest.Params.Arguments = map[string]any{ + "owner": currentOwner, + "repo": repoName, + } + + t.Logf("Listing tags for %s/%s...", currentOwner, repoName) + resp, err = mcpClient.CallTool(ctx, listTagsRequest) + require.NoError(t, err, "expected to call 'list_tags' tool successfully") + require.False(t, resp.IsError, fmt.Sprintf("expected result not to be an error: %+v", resp)) + + require.False(t, resp.IsError, "expected result not to be an error") + require.Len(t, resp.Content, 1, "expected content to have one item") + + textContent, ok = resp.Content[0].(mcp.TextContent) + require.True(t, ok, "expected content to be of type TextContent") + + var trimmedTags []struct { + Name string `json:"name"` + Commit struct { + SHA string `json:"sha"` + } `json:"commit"` + } + err = json.Unmarshal([]byte(textContent.Text), &trimmedTags) + require.NoError(t, err, "expected to unmarshal text content successfully") + + require.Len(t, trimmedTags, 1, "expected to find one tag") + require.Equal(t, "v0.0.1", trimmedTags[0].Name, "expected tag name to match") + require.Equal(t, *ref.Object.SHA, trimmedTags[0].Commit.SHA, "expected tag SHA to match") + + // And fetch an individual tag + getTagRequest := mcp.CallToolRequest{} + getTagRequest.Params.Name = "get_tag" + getTagRequest.Params.Arguments = map[string]any{ + "owner": currentOwner, + "repo": repoName, + "tag": "v0.0.1", + } + + t.Logf("Getting tag %s/%s:%s...", currentOwner, repoName, "v0.0.1") + resp, err = mcpClient.CallTool(ctx, getTagRequest) + require.NoError(t, err, "expected to call 'get_tag' tool successfully") + require.False(t, resp.IsError, "expected result not to be an error") + + var trimmedTag []struct { // don't understand why this is an array + Name string `json:"name"` + Commit struct { + SHA string `json:"sha"` + } `json:"commit"` + } + err = json.Unmarshal([]byte(textContent.Text), &trimmedTag) + require.NoError(t, err, "expected to unmarshal text content successfully") + require.Len(t, trimmedTag, 1, "expected to find one tag") + require.Equal(t, "v0.0.1", trimmedTag[0].Name, "expected tag name to match") + require.Equal(t, *ref.Object.SHA, trimmedTag[0].Commit.SHA, "expected tag SHA to match") +} diff --git a/sre_agent/servers/github-mcp-server/go.mod b/sre_agent/servers/github-mcp-server/go.mod new file mode 100644 index 0000000..4ff0bd6 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/go.mod @@ -0,0 +1,41 @@ +module github.com/github/github-mcp-server + +go 1.23.7 + +require ( + github.com/google/go-github/v69 v69.2.0 + github.com/mark3labs/mcp-go v0.25.0 + github.com/migueleliasweb/go-github-mock v1.1.0 + github.com/sirupsen/logrus v1.9.3 + github.com/spf13/cobra v1.9.1 + github.com/spf13/viper v1.20.1 + github.com/stretchr/testify v1.10.0 +) + +require ( + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/fsnotify/fsnotify v1.8.0 // indirect + github.com/go-viper/mapstructure/v2 v2.2.1 // indirect + github.com/google/go-cmp v0.7.0 // indirect + github.com/google/go-github/v64 v64.0.0 // indirect + github.com/google/go-querystring v1.1.0 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/gorilla/mux v1.8.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.3 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + github.com/rogpeppe/go-internal v1.13.1 // indirect + github.com/sagikazarmark/locafero v0.9.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.14.0 // indirect + github.com/spf13/cast v1.7.1 // indirect + github.com/spf13/pflag v1.0.6 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + github.com/yosida95/uritemplate/v3 v3.0.2 // indirect + go.uber.org/multierr v1.11.0 // indirect + golang.org/x/sys v0.31.0 // indirect + golang.org/x/text v0.23.0 // indirect + golang.org/x/time v0.5.0 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/sre_agent/servers/github-mcp-server/go.sum b/sre_agent/servers/github-mcp-server/go.sum new file mode 100644 index 0000000..9ad5d46 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/go.sum @@ -0,0 +1,85 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= +github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss= +github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/go-github/v64 v64.0.0 h1:4G61sozmY3eiPAjjoOHponXDBONm+utovTKbyUb2Qdg= +github.com/google/go-github/v64 v64.0.0/go.mod h1:xB3vqMQNdHzilXBiO2I+M7iEFtHf+DP/omBOv6tQzVo= +github.com/google/go-github/v69 v69.2.0 h1:wR+Wi/fN2zdUx9YxSmYE0ktiX9IAR/BeePzeaUUbEHE= +github.com/google/go-github/v69 v69.2.0/go.mod h1:xne4jymxLR6Uj9b7J7PyTpkMYstEMMwGZa0Aehh1azM= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mark3labs/mcp-go v0.25.0 h1:UUpcMT3L5hIhuDy7aifj4Bphw4Pfx1Rf8mzMXDe8RQw= +github.com/mark3labs/mcp-go v0.25.0/go.mod h1:rXqOudj/djTORU/ThxYx8fqEVj/5pvTuuebQ2RC7uk4= +github.com/migueleliasweb/go-github-mock v1.1.0 h1:GKaOBPsrPGkAKgtfuWY8MclS1xR6MInkx1SexJucMwE= +github.com/migueleliasweb/go-github-mock v1.1.0/go.mod h1:pYe/XlGs4BGMfRY4vmeixVsODHnVDDhJ9zoi0qzSMHc= +github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= +github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.9.0 h1:GbgQGNtTrEmddYDSAH9QLRyfAHY12md+8YFTqyMTC9k= +github.com/sagikazarmark/locafero v0.9.0/go.mod h1:UBUyz37V+EdMS3hDF3QWIiVr/2dPrx49OMO0Bn0hJqk= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.14.0 h1:9tH6MapGnn/j0eb0yIXiLjERO8RB6xIVZRDCX7PtqWA= +github.com/spf13/afero v1.14.0/go.mod h1:acJQ8t0ohCGuMN3O+Pv0V0hgMxNYDlvdk+VTfyZmbYo= +github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y= +github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo= +github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0= +github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= +github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.20.1 h1:ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4= +github.com/spf13/viper v1.20.1/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4= +github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= +golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= +golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= +golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/sre_agent/servers/github-mcp-server/pkg/github/code_scanning.go b/sre_agent/servers/github-mcp-server/pkg/github/code_scanning.go new file mode 100644 index 0000000..93e7e0e --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/code_scanning.go @@ -0,0 +1,160 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +func GetCodeScanningAlert(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_code_scanning_alert", + mcp.WithDescription(t("TOOL_GET_CODE_SCANNING_ALERT_DESCRIPTION", "Get details of a specific code scanning alert in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_CODE_SCANNING_ALERT_USER_TITLE", "Get code scanning alert"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("The owner of the repository."), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("The name of the repository."), + ), + mcp.WithNumber("alertNumber", + mcp.Required(), + mcp.Description("The number of the alert."), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + alertNumber, err := RequiredInt(request, "alertNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + + alert, resp, err := client.CodeScanning.GetAlert(ctx, owner, repo, int64(alertNumber)) + if err != nil { + return nil, fmt.Errorf("failed to get alert: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get alert: %s", string(body))), nil + } + + r, err := json.Marshal(alert) + if err != nil { + return nil, fmt.Errorf("failed to marshal alert: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +func ListCodeScanningAlerts(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("list_code_scanning_alerts", + mcp.WithDescription(t("TOOL_LIST_CODE_SCANNING_ALERTS_DESCRIPTION", "List code scanning alerts in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_LIST_CODE_SCANNING_ALERTS_USER_TITLE", "List code scanning alerts"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("The owner of the repository."), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("The name of the repository."), + ), + mcp.WithString("ref", + mcp.Description("The Git reference for the results you want to list."), + ), + mcp.WithString("state", + mcp.Description("Filter code scanning alerts by state. Defaults to open"), + mcp.DefaultString("open"), + mcp.Enum("open", "closed", "dismissed", "fixed"), + ), + mcp.WithString("severity", + mcp.Description("Filter code scanning alerts by severity"), + mcp.Enum("critical", "high", "medium", "low", "warning", "note", "error"), + ), + mcp.WithString("tool_name", + mcp.Description("The name of the tool used for code scanning."), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + ref, err := OptionalParam[string](request, "ref") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + state, err := OptionalParam[string](request, "state") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + severity, err := OptionalParam[string](request, "severity") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + toolName, err := OptionalParam[string](request, "tool_name") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + alerts, resp, err := client.CodeScanning.ListAlertsForRepo(ctx, owner, repo, &github.AlertListOptions{Ref: ref, State: state, Severity: severity, ToolName: toolName}) + if err != nil { + return nil, fmt.Errorf("failed to list alerts: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to list alerts: %s", string(body))), nil + } + + r, err := json.Marshal(alerts) + if err != nil { + return nil, fmt.Errorf("failed to marshal alerts: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/code_scanning_test.go b/sre_agent/servers/github-mcp-server/pkg/github/code_scanning_test.go new file mode 100644 index 0000000..40dabeb --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/code_scanning_test.go @@ -0,0 +1,240 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "testing" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/migueleliasweb/go-github-mock/src/mock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_GetCodeScanningAlert(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetCodeScanningAlert(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_code_scanning_alert", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "alertNumber") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "alertNumber"}) + + // Setup mock alert for success case + mockAlert := &github.Alert{ + Number: github.Ptr(42), + State: github.Ptr("open"), + Rule: &github.Rule{ID: github.Ptr("test-rule"), Description: github.Ptr("Test Rule Description")}, + HTMLURL: github.Ptr("https://github.com/owner/repo/security/code-scanning/42"), + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedAlert *github.Alert + expectedErrMsg string + }{ + { + name: "successful alert fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber, + mockAlert, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "alertNumber": float64(42), + }, + expectError: false, + expectedAlert: mockAlert, + }, + { + name: "alert fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposCodeScanningAlertsByOwnerByRepoByAlertNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "alertNumber": float64(9999), + }, + expectError: true, + expectedErrMsg: "failed to get alert", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetCodeScanningAlert(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedAlert github.Alert + err = json.Unmarshal([]byte(textContent.Text), &returnedAlert) + assert.NoError(t, err) + assert.Equal(t, *tc.expectedAlert.Number, *returnedAlert.Number) + assert.Equal(t, *tc.expectedAlert.State, *returnedAlert.State) + assert.Equal(t, *tc.expectedAlert.Rule.ID, *returnedAlert.Rule.ID) + assert.Equal(t, *tc.expectedAlert.HTMLURL, *returnedAlert.HTMLURL) + + }) + } +} + +func Test_ListCodeScanningAlerts(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := ListCodeScanningAlerts(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "list_code_scanning_alerts", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "ref") + assert.Contains(t, tool.InputSchema.Properties, "state") + assert.Contains(t, tool.InputSchema.Properties, "severity") + assert.Contains(t, tool.InputSchema.Properties, "tool_name") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"}) + + // Setup mock alerts for success case + mockAlerts := []*github.Alert{ + { + Number: github.Ptr(42), + State: github.Ptr("open"), + Rule: &github.Rule{ID: github.Ptr("test-rule-1"), Description: github.Ptr("Test Rule 1")}, + HTMLURL: github.Ptr("https://github.com/owner/repo/security/code-scanning/42"), + }, + { + Number: github.Ptr(43), + State: github.Ptr("fixed"), + Rule: &github.Rule{ID: github.Ptr("test-rule-2"), Description: github.Ptr("Test Rule 2")}, + HTMLURL: github.Ptr("https://github.com/owner/repo/security/code-scanning/43"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedAlerts []*github.Alert + expectedErrMsg string + }{ + { + name: "successful alerts listing", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposCodeScanningAlertsByOwnerByRepo, + expectQueryParams(t, map[string]string{ + "ref": "main", + "state": "open", + "severity": "high", + "tool_name": "codeql", + }).andThen( + mockResponse(t, http.StatusOK, mockAlerts), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "ref": "main", + "state": "open", + "severity": "high", + "tool_name": "codeql", + }, + expectError: false, + expectedAlerts: mockAlerts, + }, + { + name: "alerts listing fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposCodeScanningAlertsByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte(`{"message": "Unauthorized access"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + }, + expectError: true, + expectedErrMsg: "failed to list alerts", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := ListCodeScanningAlerts(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedAlerts []*github.Alert + err = json.Unmarshal([]byte(textContent.Text), &returnedAlerts) + assert.NoError(t, err) + assert.Len(t, returnedAlerts, len(tc.expectedAlerts)) + for i, alert := range returnedAlerts { + assert.Equal(t, *tc.expectedAlerts[i].Number, *alert.Number) + assert.Equal(t, *tc.expectedAlerts[i].State, *alert.State) + assert.Equal(t, *tc.expectedAlerts[i].Rule.ID, *alert.Rule.ID) + assert.Equal(t, *tc.expectedAlerts[i].HTMLURL, *alert.HTMLURL) + } + }) + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/context_tools.go b/sre_agent/servers/github-mcp-server/pkg/github/context_tools.go new file mode 100644 index 0000000..3511e23 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/context_tools.go @@ -0,0 +1,53 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +// GetMe creates a tool to get details of the authenticated user. +func GetMe(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_me", + mcp.WithDescription(t("TOOL_GET_ME_DESCRIPTION", "Get details of the authenticated GitHub user. Use this when a request include \"me\", \"my\"...")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_ME_USER_TITLE", "Get my user profile"), + ReadOnlyHint: true, + }), + mcp.WithString("reason", + mcp.Description("Optional: reason the session was created"), + ), + ), + func(ctx context.Context, _ mcp.CallToolRequest) (*mcp.CallToolResult, error) { + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + user, resp, err := client.Users.Get(ctx, "") + if err != nil { + return nil, fmt.Errorf("failed to get user: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get user: %s", string(body))), nil + } + + r, err := json.Marshal(user) + if err != nil { + return nil, fmt.Errorf("failed to marshal user: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/context_tools_test.go b/sre_agent/servers/github-mcp-server/pkg/github/context_tools_test.go new file mode 100644 index 0000000..c9d220d --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/context_tools_test.go @@ -0,0 +1,132 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "testing" + "time" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/migueleliasweb/go-github-mock/src/mock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_GetMe(t *testing.T) { + // Verify tool definition + mockClient := github.NewClient(nil) + tool, _ := GetMe(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_me", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "reason") + assert.Empty(t, tool.InputSchema.Required) // No required parameters + + // Setup mock user response + mockUser := &github.User{ + Login: github.Ptr("testuser"), + Name: github.Ptr("Test User"), + Email: github.Ptr("test@example.com"), + Bio: github.Ptr("GitHub user for testing"), + Company: github.Ptr("Test Company"), + Location: github.Ptr("Test Location"), + HTMLURL: github.Ptr("https://github.com/testuser"), + CreatedAt: &github.Timestamp{Time: time.Now().Add(-365 * 24 * time.Hour)}, + Type: github.Ptr("User"), + Plan: &github.Plan{ + Name: github.Ptr("pro"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedUser *github.User + expectedErrMsg string + }{ + { + name: "successful get user", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetUser, + mockUser, + ), + ), + requestArgs: map[string]interface{}{}, + expectError: false, + expectedUser: mockUser, + }, + { + name: "successful get user with reason", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetUser, + mockUser, + ), + ), + requestArgs: map[string]interface{}{ + "reason": "Testing API", + }, + expectError: false, + expectedUser: mockUser, + }, + { + name: "get user fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetUser, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte(`{"message": "Unauthorized"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{}, + expectError: true, + expectedErrMsg: "failed to get user", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetMe(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse result and get text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedUser github.User + err = json.Unmarshal([]byte(textContent.Text), &returnedUser) + require.NoError(t, err) + + // Verify user details + assert.Equal(t, *tc.expectedUser.Login, *returnedUser.Login) + assert.Equal(t, *tc.expectedUser.Name, *returnedUser.Name) + assert.Equal(t, *tc.expectedUser.Email, *returnedUser.Email) + assert.Equal(t, *tc.expectedUser.Bio, *returnedUser.Bio) + assert.Equal(t, *tc.expectedUser.HTMLURL, *returnedUser.HTMLURL) + assert.Equal(t, *tc.expectedUser.Type, *returnedUser.Type) + }) + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/dynamic_tools.go b/sre_agent/servers/github-mcp-server/pkg/github/dynamic_tools.go new file mode 100644 index 0000000..30dfd4a --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/dynamic_tools.go @@ -0,0 +1,138 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/github/github-mcp-server/pkg/toolsets" + "github.com/github/github-mcp-server/pkg/translations" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +func ToolsetEnum(toolsetGroup *toolsets.ToolsetGroup) mcp.PropertyOption { + toolsetNames := make([]string, 0, len(toolsetGroup.Toolsets)) + for name := range toolsetGroup.Toolsets { + toolsetNames = append(toolsetNames, name) + } + return mcp.Enum(toolsetNames...) +} + +func EnableToolset(s *server.MCPServer, toolsetGroup *toolsets.ToolsetGroup, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("enable_toolset", + mcp.WithDescription(t("TOOL_ENABLE_TOOLSET_DESCRIPTION", "Enable one of the sets of tools the GitHub MCP server provides, use get_toolset_tools and list_available_toolsets first to see what this will enable")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_ENABLE_TOOLSET_USER_TITLE", "Enable a toolset"), + // Not modifying GitHub data so no need to show a warning + ReadOnlyHint: true, + }), + mcp.WithString("toolset", + mcp.Required(), + mcp.Description("The name of the toolset to enable"), + ToolsetEnum(toolsetGroup), + ), + ), + func(_ context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + // We need to convert the toolsets back to a map for JSON serialization + toolsetName, err := requiredParam[string](request, "toolset") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + toolset := toolsetGroup.Toolsets[toolsetName] + if toolset == nil { + return mcp.NewToolResultError(fmt.Sprintf("Toolset %s not found", toolsetName)), nil + } + if toolset.Enabled { + return mcp.NewToolResultText(fmt.Sprintf("Toolset %s is already enabled", toolsetName)), nil + } + + toolset.Enabled = true + + // caution: this currently affects the global tools and notifies all clients: + // + // Send notification to all initialized sessions + // s.sendNotificationToAllClients("notifications/tools/list_changed", nil) + s.AddTools(toolset.GetActiveTools()...) + + return mcp.NewToolResultText(fmt.Sprintf("Toolset %s enabled", toolsetName)), nil + } +} + +func ListAvailableToolsets(toolsetGroup *toolsets.ToolsetGroup, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("list_available_toolsets", + mcp.WithDescription(t("TOOL_LIST_AVAILABLE_TOOLSETS_DESCRIPTION", "List all available toolsets this GitHub MCP server can offer, providing the enabled status of each. Use this when a task could be achieved with a GitHub tool and the currently available tools aren't enough. Call get_toolset_tools with these toolset names to discover specific tools you can call")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_LIST_AVAILABLE_TOOLSETS_USER_TITLE", "List available toolsets"), + ReadOnlyHint: true, + }), + ), + func(_ context.Context, _ mcp.CallToolRequest) (*mcp.CallToolResult, error) { + // We need to convert the toolsetGroup back to a map for JSON serialization + + payload := []map[string]string{} + + for name, ts := range toolsetGroup.Toolsets { + { + t := map[string]string{ + "name": name, + "description": ts.Description, + "can_enable": "true", + "currently_enabled": fmt.Sprintf("%t", ts.Enabled), + } + payload = append(payload, t) + } + } + + r, err := json.Marshal(payload) + if err != nil { + return nil, fmt.Errorf("failed to marshal features: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +func GetToolsetsTools(toolsetGroup *toolsets.ToolsetGroup, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_toolset_tools", + mcp.WithDescription(t("TOOL_GET_TOOLSET_TOOLS_DESCRIPTION", "Lists all the capabilities that are enabled with the specified toolset, use this to get clarity on whether enabling a toolset would help you to complete a task")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_TOOLSET_TOOLS_USER_TITLE", "List all tools in a toolset"), + ReadOnlyHint: true, + }), + mcp.WithString("toolset", + mcp.Required(), + mcp.Description("The name of the toolset you want to get the tools for"), + ToolsetEnum(toolsetGroup), + ), + ), + func(_ context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + // We need to convert the toolsetGroup back to a map for JSON serialization + toolsetName, err := requiredParam[string](request, "toolset") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + toolset := toolsetGroup.Toolsets[toolsetName] + if toolset == nil { + return mcp.NewToolResultError(fmt.Sprintf("Toolset %s not found", toolsetName)), nil + } + payload := []map[string]string{} + + for _, st := range toolset.GetAvailableTools() { + tool := map[string]string{ + "name": st.Tool.Name, + "description": st.Tool.Description, + "can_enable": "true", + "toolset": toolsetName, + } + payload = append(payload, tool) + } + + r, err := json.Marshal(payload) + if err != nil { + return nil, fmt.Errorf("failed to marshal features: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/helper_test.go b/sre_agent/servers/github-mcp-server/pkg/github/helper_test.go new file mode 100644 index 0000000..f241d33 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/helper_test.go @@ -0,0 +1,222 @@ +package github + +import ( + "encoding/json" + "net/http" + "testing" + + "github.com/mark3labs/mcp-go/mcp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// expectPath is a helper function to create a partial mock that expects a +// request with the given path, with the ability to chain a response handler. +func expectPath(t *testing.T, expectedPath string) *partialMock { + return &partialMock{ + t: t, + expectedPath: expectedPath, + } +} + +// expectQueryParams is a helper function to create a partial mock that expects a +// request with the given query parameters, with the ability to chain a response handler. +func expectQueryParams(t *testing.T, expectedQueryParams map[string]string) *partialMock { + return &partialMock{ + t: t, + expectedQueryParams: expectedQueryParams, + } +} + +// expectRequestBody is a helper function to create a partial mock that expects a +// request with the given body, with the ability to chain a response handler. +func expectRequestBody(t *testing.T, expectedRequestBody any) *partialMock { + return &partialMock{ + t: t, + expectedRequestBody: expectedRequestBody, + } +} + +type partialMock struct { + t *testing.T + + expectedPath string + expectedQueryParams map[string]string + expectedRequestBody any +} + +func (p *partialMock) andThen(responseHandler http.HandlerFunc) http.HandlerFunc { + p.t.Helper() + return func(w http.ResponseWriter, r *http.Request) { + if p.expectedPath != "" { + require.Equal(p.t, p.expectedPath, r.URL.Path) + } + + if p.expectedQueryParams != nil { + require.Equal(p.t, len(p.expectedQueryParams), len(r.URL.Query())) + for k, v := range p.expectedQueryParams { + require.Equal(p.t, v, r.URL.Query().Get(k)) + } + } + + if p.expectedRequestBody != nil { + var unmarshaledRequestBody any + err := json.NewDecoder(r.Body).Decode(&unmarshaledRequestBody) + require.NoError(p.t, err) + + require.Equal(p.t, p.expectedRequestBody, unmarshaledRequestBody) + } + + responseHandler(w, r) + } +} + +// mockResponse is a helper function to create a mock HTTP response handler +// that returns a specified status code and marshaled body. +func mockResponse(t *testing.T, code int, body interface{}) http.HandlerFunc { + t.Helper() + return func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(code) + b, err := json.Marshal(body) + require.NoError(t, err) + _, _ = w.Write(b) + } +} + +// createMCPRequest is a helper function to create a MCP request with the given arguments. +func createMCPRequest(args map[string]interface{}) mcp.CallToolRequest { + return mcp.CallToolRequest{ + Params: struct { + Name string `json:"name"` + Arguments map[string]interface{} `json:"arguments,omitempty"` + Meta *struct { + ProgressToken mcp.ProgressToken `json:"progressToken,omitempty"` + } `json:"_meta,omitempty"` + }{ + Arguments: args, + }, + } +} + +// getTextResult is a helper function that returns a text result from a tool call. +func getTextResult(t *testing.T, result *mcp.CallToolResult) mcp.TextContent { + t.Helper() + assert.NotNil(t, result) + require.Len(t, result.Content, 1) + require.IsType(t, mcp.TextContent{}, result.Content[0]) + textContent := result.Content[0].(mcp.TextContent) + assert.Equal(t, "text", textContent.Type) + return textContent +} + +func TestOptionalParamOK(t *testing.T) { + tests := []struct { + name string + args map[string]interface{} + paramName string + expectedVal interface{} + expectedOk bool + expectError bool + errorMsg string + }{ + { + name: "present and correct type (string)", + args: map[string]interface{}{"myParam": "hello"}, + paramName: "myParam", + expectedVal: "hello", + expectedOk: true, + expectError: false, + }, + { + name: "present and correct type (bool)", + args: map[string]interface{}{"myParam": true}, + paramName: "myParam", + expectedVal: true, + expectedOk: true, + expectError: false, + }, + { + name: "present and correct type (number)", + args: map[string]interface{}{"myParam": float64(123)}, + paramName: "myParam", + expectedVal: float64(123), + expectedOk: true, + expectError: false, + }, + { + name: "present but wrong type (string expected, got bool)", + args: map[string]interface{}{"myParam": true}, + paramName: "myParam", + expectedVal: "", // Zero value for string + expectedOk: true, // ok is true because param exists + expectError: true, + errorMsg: "parameter myParam is not of type string, is bool", + }, + { + name: "present but wrong type (bool expected, got string)", + args: map[string]interface{}{"myParam": "true"}, + paramName: "myParam", + expectedVal: false, // Zero value for bool + expectedOk: true, // ok is true because param exists + expectError: true, + errorMsg: "parameter myParam is not of type bool, is string", + }, + { + name: "parameter not present", + args: map[string]interface{}{"anotherParam": "value"}, + paramName: "myParam", + expectedVal: "", // Zero value for string + expectedOk: false, + expectError: false, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + request := createMCPRequest(tc.args) + + // Test with string type assertion + if _, isString := tc.expectedVal.(string); isString || tc.errorMsg == "parameter myParam is not of type string, is bool" { + val, ok, err := OptionalParamOK[string](request, tc.paramName) + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.errorMsg) + assert.Equal(t, tc.expectedOk, ok) // Check ok even on error + assert.Equal(t, tc.expectedVal, val) // Check zero value on error + } else { + require.NoError(t, err) + assert.Equal(t, tc.expectedOk, ok) + assert.Equal(t, tc.expectedVal, val) + } + } + + // Test with bool type assertion + if _, isBool := tc.expectedVal.(bool); isBool || tc.errorMsg == "parameter myParam is not of type bool, is string" { + val, ok, err := OptionalParamOK[bool](request, tc.paramName) + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.errorMsg) + assert.Equal(t, tc.expectedOk, ok) // Check ok even on error + assert.Equal(t, tc.expectedVal, val) // Check zero value on error + } else { + require.NoError(t, err) + assert.Equal(t, tc.expectedOk, ok) + assert.Equal(t, tc.expectedVal, val) + } + } + + // Test with float64 type assertion (for number case) + if _, isFloat := tc.expectedVal.(float64); isFloat { + val, ok, err := OptionalParamOK[float64](request, tc.paramName) + if tc.expectError { + // This case shouldn't happen for float64 in the defined tests + require.Fail(t, "Unexpected error case for float64") + } else { + require.NoError(t, err) + assert.Equal(t, tc.expectedOk, ok) + assert.Equal(t, tc.expectedVal, val) + } + } + }) + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/issues.go b/sre_agent/servers/github-mcp-server/pkg/github/issues.go new file mode 100644 index 0000000..0fcc250 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/issues.go @@ -0,0 +1,736 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "time" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +// GetIssue creates a tool to get details of a specific issue in a GitHub repository. +func GetIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_issue", + mcp.WithDescription(t("TOOL_GET_ISSUE_DESCRIPTION", "Get details of a specific issue in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_ISSUE_USER_TITLE", "Get issue details"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("The owner of the repository"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("The name of the repository"), + ), + mcp.WithNumber("issue_number", + mcp.Required(), + mcp.Description("The number of the issue"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + issueNumber, err := RequiredInt(request, "issue_number") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + issue, resp, err := client.Issues.Get(ctx, owner, repo, issueNumber) + if err != nil { + return nil, fmt.Errorf("failed to get issue: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get issue: %s", string(body))), nil + } + + r, err := json.Marshal(issue) + if err != nil { + return nil, fmt.Errorf("failed to marshal issue: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// AddIssueComment creates a tool to add a comment to an issue. +func AddIssueComment(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("add_issue_comment", + mcp.WithDescription(t("TOOL_ADD_ISSUE_COMMENT_DESCRIPTION", "Add a comment to a specific issue in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_ADD_ISSUE_COMMENT_USER_TITLE", "Add comment to issue"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("issue_number", + mcp.Required(), + mcp.Description("Issue number to comment on"), + ), + mcp.WithString("body", + mcp.Required(), + mcp.Description("Comment content"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + issueNumber, err := RequiredInt(request, "issue_number") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + body, err := requiredParam[string](request, "body") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + comment := &github.IssueComment{ + Body: github.Ptr(body), + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + createdComment, resp, err := client.Issues.CreateComment(ctx, owner, repo, issueNumber, comment) + if err != nil { + return nil, fmt.Errorf("failed to create comment: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusCreated { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to create comment: %s", string(body))), nil + } + + r, err := json.Marshal(createdComment) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// SearchIssues creates a tool to search for issues and pull requests. +func SearchIssues(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("search_issues", + mcp.WithDescription(t("TOOL_SEARCH_ISSUES_DESCRIPTION", "Search for issues in GitHub repositories.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_SEARCH_ISSUES_USER_TITLE", "Search issues"), + ReadOnlyHint: true, + }), + mcp.WithString("q", + mcp.Required(), + mcp.Description("Search query using GitHub issues search syntax"), + ), + mcp.WithString("sort", + mcp.Description("Sort field by number of matches of categories, defaults to best match"), + mcp.Enum( + "comments", + "reactions", + "reactions-+1", + "reactions--1", + "reactions-smile", + "reactions-thinking_face", + "reactions-heart", + "reactions-tada", + "interactions", + "created", + "updated", + ), + ), + mcp.WithString("order", + mcp.Description("Sort order"), + mcp.Enum("asc", "desc"), + ), + WithPagination(), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + query, err := requiredParam[string](request, "q") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + sort, err := OptionalParam[string](request, "sort") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + order, err := OptionalParam[string](request, "order") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pagination, err := OptionalPaginationParams(request) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.SearchOptions{ + Sort: sort, + Order: order, + ListOptions: github.ListOptions{ + PerPage: pagination.perPage, + Page: pagination.page, + }, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + result, resp, err := client.Search.Issues(ctx, query, opts) + if err != nil { + return nil, fmt.Errorf("failed to search issues: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to search issues: %s", string(body))), nil + } + + r, err := json.Marshal(result) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// CreateIssue creates a tool to create a new issue in a GitHub repository. +func CreateIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("create_issue", + mcp.WithDescription(t("TOOL_CREATE_ISSUE_DESCRIPTION", "Create a new issue in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_CREATE_ISSUE_USER_TITLE", "Open new issue"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("title", + mcp.Required(), + mcp.Description("Issue title"), + ), + mcp.WithString("body", + mcp.Description("Issue body content"), + ), + mcp.WithArray("assignees", + mcp.Description("Usernames to assign to this issue"), + mcp.Items( + map[string]interface{}{ + "type": "string", + }, + ), + ), + mcp.WithArray("labels", + mcp.Description("Labels to apply to this issue"), + mcp.Items( + map[string]interface{}{ + "type": "string", + }, + ), + ), + mcp.WithNumber("milestone", + mcp.Description("Milestone number"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + title, err := requiredParam[string](request, "title") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + // Optional parameters + body, err := OptionalParam[string](request, "body") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + // Get assignees + assignees, err := OptionalStringArrayParam(request, "assignees") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + // Get labels + labels, err := OptionalStringArrayParam(request, "labels") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + // Get optional milestone + milestone, err := OptionalIntParam(request, "milestone") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + var milestoneNum *int + if milestone != 0 { + milestoneNum = &milestone + } + + // Create the issue request + issueRequest := &github.IssueRequest{ + Title: github.Ptr(title), + Body: github.Ptr(body), + Assignees: &assignees, + Labels: &labels, + Milestone: milestoneNum, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + issue, resp, err := client.Issues.Create(ctx, owner, repo, issueRequest) + if err != nil { + return nil, fmt.Errorf("failed to create issue: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusCreated { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to create issue: %s", string(body))), nil + } + + r, err := json.Marshal(issue) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// ListIssues creates a tool to list and filter repository issues +func ListIssues(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("list_issues", + mcp.WithDescription(t("TOOL_LIST_ISSUES_DESCRIPTION", "List issues in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_LIST_ISSUES_USER_TITLE", "List issues"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("state", + mcp.Description("Filter by state"), + mcp.Enum("open", "closed", "all"), + ), + mcp.WithArray("labels", + mcp.Description("Filter by labels"), + mcp.Items( + map[string]interface{}{ + "type": "string", + }, + ), + ), + mcp.WithString("sort", + mcp.Description("Sort order"), + mcp.Enum("created", "updated", "comments"), + ), + mcp.WithString("direction", + mcp.Description("Sort direction"), + mcp.Enum("asc", "desc"), + ), + mcp.WithString("since", + mcp.Description("Filter by date (ISO 8601 timestamp)"), + ), + WithPagination(), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.IssueListByRepoOptions{} + + // Set optional parameters if provided + opts.State, err = OptionalParam[string](request, "state") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + // Get labels + opts.Labels, err = OptionalStringArrayParam(request, "labels") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts.Sort, err = OptionalParam[string](request, "sort") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts.Direction, err = OptionalParam[string](request, "direction") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + since, err := OptionalParam[string](request, "since") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if since != "" { + timestamp, err := parseISOTimestamp(since) + if err != nil { + return mcp.NewToolResultError(fmt.Sprintf("failed to list issues: %s", err.Error())), nil + } + opts.Since = timestamp + } + + if page, ok := request.Params.Arguments["page"].(float64); ok { + opts.Page = int(page) + } + + if perPage, ok := request.Params.Arguments["perPage"].(float64); ok { + opts.PerPage = int(perPage) + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + issues, resp, err := client.Issues.ListByRepo(ctx, owner, repo, opts) + if err != nil { + return nil, fmt.Errorf("failed to list issues: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to list issues: %s", string(body))), nil + } + + r, err := json.Marshal(issues) + if err != nil { + return nil, fmt.Errorf("failed to marshal issues: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// UpdateIssue creates a tool to update an existing issue in a GitHub repository. +func UpdateIssue(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("update_issue", + mcp.WithDescription(t("TOOL_UPDATE_ISSUE_DESCRIPTION", "Update an existing issue in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_UPDATE_ISSUE_USER_TITLE", "Edit issue"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("issue_number", + mcp.Required(), + mcp.Description("Issue number to update"), + ), + mcp.WithString("title", + mcp.Description("New title"), + ), + mcp.WithString("body", + mcp.Description("New description"), + ), + mcp.WithString("state", + mcp.Description("New state"), + mcp.Enum("open", "closed"), + ), + mcp.WithArray("labels", + mcp.Description("New labels"), + mcp.Items( + map[string]interface{}{ + "type": "string", + }, + ), + ), + mcp.WithArray("assignees", + mcp.Description("New assignees"), + mcp.Items( + map[string]interface{}{ + "type": "string", + }, + ), + ), + mcp.WithNumber("milestone", + mcp.Description("New milestone number"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + issueNumber, err := RequiredInt(request, "issue_number") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + // Create the issue request with only provided fields + issueRequest := &github.IssueRequest{} + + // Set optional parameters if provided + title, err := OptionalParam[string](request, "title") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if title != "" { + issueRequest.Title = github.Ptr(title) + } + + body, err := OptionalParam[string](request, "body") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if body != "" { + issueRequest.Body = github.Ptr(body) + } + + state, err := OptionalParam[string](request, "state") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if state != "" { + issueRequest.State = github.Ptr(state) + } + + // Get labels + labels, err := OptionalStringArrayParam(request, "labels") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if len(labels) > 0 { + issueRequest.Labels = &labels + } + + // Get assignees + assignees, err := OptionalStringArrayParam(request, "assignees") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if len(assignees) > 0 { + issueRequest.Assignees = &assignees + } + + milestone, err := OptionalIntParam(request, "milestone") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if milestone != 0 { + milestoneNum := milestone + issueRequest.Milestone = &milestoneNum + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + updatedIssue, resp, err := client.Issues.Edit(ctx, owner, repo, issueNumber, issueRequest) + if err != nil { + return nil, fmt.Errorf("failed to update issue: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to update issue: %s", string(body))), nil + } + + r, err := json.Marshal(updatedIssue) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// GetIssueComments creates a tool to get comments for a GitHub issue. +func GetIssueComments(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_issue_comments", + mcp.WithDescription(t("TOOL_GET_ISSUE_COMMENTS_DESCRIPTION", "Get comments for a specific issue in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_ISSUE_COMMENTS_USER_TITLE", "Get issue comments"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("issue_number", + mcp.Required(), + mcp.Description("Issue number"), + ), + mcp.WithNumber("page", + mcp.Description("Page number"), + ), + mcp.WithNumber("per_page", + mcp.Description("Number of records per page"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + issueNumber, err := RequiredInt(request, "issue_number") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + page, err := OptionalIntParamWithDefault(request, "page", 1) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + perPage, err := OptionalIntParamWithDefault(request, "per_page", 30) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.IssueListCommentsOptions{ + ListOptions: github.ListOptions{ + Page: page, + PerPage: perPage, + }, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + comments, resp, err := client.Issues.ListComments(ctx, owner, repo, issueNumber, opts) + if err != nil { + return nil, fmt.Errorf("failed to get issue comments: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get issue comments: %s", string(body))), nil + } + + r, err := json.Marshal(comments) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// parseISOTimestamp parses an ISO 8601 timestamp string into a time.Time object. +// Returns the parsed time or an error if parsing fails. +// Example formats supported: "2023-01-15T14:30:00Z", "2023-01-15" +func parseISOTimestamp(timestamp string) (time.Time, error) { + if timestamp == "" { + return time.Time{}, fmt.Errorf("empty timestamp") + } + + // Try RFC3339 format (standard ISO 8601 with time) + t, err := time.Parse(time.RFC3339, timestamp) + if err == nil { + return t, nil + } + + // Try simple date format (YYYY-MM-DD) + t, err = time.Parse("2006-01-02", timestamp) + if err == nil { + return t, nil + } + + // Return error with supported formats + return time.Time{}, fmt.Errorf("invalid ISO 8601 timestamp: %s (supported formats: YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DD)", timestamp) +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/issues_test.go b/sre_agent/servers/github-mcp-server/pkg/github/issues_test.go new file mode 100644 index 0000000..61ca0ae --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/issues_test.go @@ -0,0 +1,1132 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "testing" + "time" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/migueleliasweb/go-github-mock/src/mock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_GetIssue(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetIssue(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_issue", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "issue_number") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "issue_number"}) + + // Setup mock issue for success case + mockIssue := &github.Issue{ + Number: github.Ptr(42), + Title: github.Ptr("Test Issue"), + Body: github.Ptr("This is a test issue"), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/42"), + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedIssue *github.Issue + expectedErrMsg string + }{ + { + name: "successful issue retrieval", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposIssuesByOwnerByRepoByIssueNumber, + mockIssue, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(42), + }, + expectError: false, + expectedIssue: mockIssue, + }, + { + name: "issue not found", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposIssuesByOwnerByRepoByIssueNumber, + mockResponse(t, http.StatusNotFound, `{"message": "Issue not found"}`), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(999), + }, + expectError: true, + expectedErrMsg: "failed to get issue", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetIssue(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedIssue github.Issue + err = json.Unmarshal([]byte(textContent.Text), &returnedIssue) + require.NoError(t, err) + assert.Equal(t, *tc.expectedIssue.Number, *returnedIssue.Number) + assert.Equal(t, *tc.expectedIssue.Title, *returnedIssue.Title) + assert.Equal(t, *tc.expectedIssue.Body, *returnedIssue.Body) + }) + } +} + +func Test_AddIssueComment(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := AddIssueComment(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "add_issue_comment", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "issue_number") + assert.Contains(t, tool.InputSchema.Properties, "body") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "issue_number", "body"}) + + // Setup mock comment for success case + mockComment := &github.IssueComment{ + ID: github.Ptr(int64(123)), + Body: github.Ptr("This is a test comment"), + User: &github.User{ + Login: github.Ptr("testuser"), + }, + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/42#issuecomment-123"), + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedComment *github.IssueComment + expectedErrMsg string + }{ + { + name: "successful comment creation", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposIssuesCommentsByOwnerByRepoByIssueNumber, + mockResponse(t, http.StatusCreated, mockComment), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(42), + "body": "This is a test comment", + }, + expectError: false, + expectedComment: mockComment, + }, + { + name: "comment creation fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposIssuesCommentsByOwnerByRepoByIssueNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"message": "Invalid request"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(42), + "body": "", + }, + expectError: false, + expectedErrMsg: "missing required parameter: body", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := AddIssueComment(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := mcp.CallToolRequest{ + Params: struct { + Name string `json:"name"` + Arguments map[string]interface{} `json:"arguments,omitempty"` + Meta *struct { + ProgressToken mcp.ProgressToken `json:"progressToken,omitempty"` + } `json:"_meta,omitempty"` + }{ + Arguments: tc.requestArgs, + }, + } + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + if tc.expectedErrMsg != "" { + require.NotNil(t, result) + textContent := getTextResult(t, result) + assert.Contains(t, textContent.Text, tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedComment github.IssueComment + err = json.Unmarshal([]byte(textContent.Text), &returnedComment) + require.NoError(t, err) + assert.Equal(t, *tc.expectedComment.ID, *returnedComment.ID) + assert.Equal(t, *tc.expectedComment.Body, *returnedComment.Body) + assert.Equal(t, *tc.expectedComment.User.Login, *returnedComment.User.Login) + + }) + } +} + +func Test_SearchIssues(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := SearchIssues(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "search_issues", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "q") + assert.Contains(t, tool.InputSchema.Properties, "sort") + assert.Contains(t, tool.InputSchema.Properties, "order") + assert.Contains(t, tool.InputSchema.Properties, "perPage") + assert.Contains(t, tool.InputSchema.Properties, "page") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"q"}) + + // Setup mock search results + mockSearchResult := &github.IssuesSearchResult{ + Total: github.Ptr(2), + IncompleteResults: github.Ptr(false), + Issues: []*github.Issue{ + { + Number: github.Ptr(42), + Title: github.Ptr("Bug: Something is broken"), + Body: github.Ptr("This is a bug report"), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/42"), + Comments: github.Ptr(5), + User: &github.User{ + Login: github.Ptr("user1"), + }, + }, + { + Number: github.Ptr(43), + Title: github.Ptr("Feature: Add new functionality"), + Body: github.Ptr("This is a feature request"), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/43"), + Comments: github.Ptr(3), + User: &github.User{ + Login: github.Ptr("user2"), + }, + }, + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedResult *github.IssuesSearchResult + expectedErrMsg string + }{ + { + name: "successful issues search with all parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchIssues, + expectQueryParams( + t, + map[string]string{ + "q": "repo:owner/repo is:issue is:open", + "sort": "created", + "order": "desc", + "page": "1", + "per_page": "30", + }, + ).andThen( + mockResponse(t, http.StatusOK, mockSearchResult), + ), + ), + ), + requestArgs: map[string]interface{}{ + "q": "repo:owner/repo is:issue is:open", + "sort": "created", + "order": "desc", + "page": float64(1), + "perPage": float64(30), + }, + expectError: false, + expectedResult: mockSearchResult, + }, + { + name: "issues search with minimal parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetSearchIssues, + mockSearchResult, + ), + ), + requestArgs: map[string]interface{}{ + "q": "repo:owner/repo is:issue is:open", + }, + expectError: false, + expectedResult: mockSearchResult, + }, + { + name: "search issues fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchIssues, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"message": "Validation Failed"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "q": "invalid:query", + }, + expectError: true, + expectedErrMsg: "failed to search issues", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := SearchIssues(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedResult github.IssuesSearchResult + err = json.Unmarshal([]byte(textContent.Text), &returnedResult) + require.NoError(t, err) + assert.Equal(t, *tc.expectedResult.Total, *returnedResult.Total) + assert.Equal(t, *tc.expectedResult.IncompleteResults, *returnedResult.IncompleteResults) + assert.Len(t, returnedResult.Issues, len(tc.expectedResult.Issues)) + for i, issue := range returnedResult.Issues { + assert.Equal(t, *tc.expectedResult.Issues[i].Number, *issue.Number) + assert.Equal(t, *tc.expectedResult.Issues[i].Title, *issue.Title) + assert.Equal(t, *tc.expectedResult.Issues[i].State, *issue.State) + assert.Equal(t, *tc.expectedResult.Issues[i].HTMLURL, *issue.HTMLURL) + assert.Equal(t, *tc.expectedResult.Issues[i].User.Login, *issue.User.Login) + } + }) + } +} + +func Test_CreateIssue(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := CreateIssue(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "create_issue", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "title") + assert.Contains(t, tool.InputSchema.Properties, "body") + assert.Contains(t, tool.InputSchema.Properties, "assignees") + assert.Contains(t, tool.InputSchema.Properties, "labels") + assert.Contains(t, tool.InputSchema.Properties, "milestone") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "title"}) + + // Setup mock issue for success case + mockIssue := &github.Issue{ + Number: github.Ptr(123), + Title: github.Ptr("Test Issue"), + Body: github.Ptr("This is a test issue"), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/123"), + Assignees: []*github.User{{Login: github.Ptr("user1")}, {Login: github.Ptr("user2")}}, + Labels: []*github.Label{{Name: github.Ptr("bug")}, {Name: github.Ptr("help wanted")}}, + Milestone: &github.Milestone{Number: github.Ptr(5)}, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedIssue *github.Issue + expectedErrMsg string + }{ + { + name: "successful issue creation with all fields", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposIssuesByOwnerByRepo, + expectRequestBody(t, map[string]any{ + "title": "Test Issue", + "body": "This is a test issue", + "labels": []any{"bug", "help wanted"}, + "assignees": []any{"user1", "user2"}, + "milestone": float64(5), + }).andThen( + mockResponse(t, http.StatusCreated, mockIssue), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "title": "Test Issue", + "body": "This is a test issue", + "assignees": []any{"user1", "user2"}, + "labels": []any{"bug", "help wanted"}, + "milestone": float64(5), + }, + expectError: false, + expectedIssue: mockIssue, + }, + { + name: "successful issue creation with minimal fields", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposIssuesByOwnerByRepo, + mockResponse(t, http.StatusCreated, &github.Issue{ + Number: github.Ptr(124), + Title: github.Ptr("Minimal Issue"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/124"), + State: github.Ptr("open"), + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "title": "Minimal Issue", + "assignees": nil, // Expect no failure with nil optional value. + }, + expectError: false, + expectedIssue: &github.Issue{ + Number: github.Ptr(124), + Title: github.Ptr("Minimal Issue"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/124"), + State: github.Ptr("open"), + }, + }, + { + name: "issue creation fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposIssuesByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"message": "Validation failed"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "title": "", + }, + expectError: false, + expectedErrMsg: "missing required parameter: title", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := CreateIssue(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + if tc.expectedErrMsg != "" { + require.NotNil(t, result) + textContent := getTextResult(t, result) + assert.Contains(t, textContent.Text, tc.expectedErrMsg) + return + } + + require.NoError(t, err) + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedIssue github.Issue + err = json.Unmarshal([]byte(textContent.Text), &returnedIssue) + require.NoError(t, err) + + assert.Equal(t, *tc.expectedIssue.Number, *returnedIssue.Number) + assert.Equal(t, *tc.expectedIssue.Title, *returnedIssue.Title) + assert.Equal(t, *tc.expectedIssue.State, *returnedIssue.State) + assert.Equal(t, *tc.expectedIssue.HTMLURL, *returnedIssue.HTMLURL) + + if tc.expectedIssue.Body != nil { + assert.Equal(t, *tc.expectedIssue.Body, *returnedIssue.Body) + } + + // Check assignees if expected + if len(tc.expectedIssue.Assignees) > 0 { + assert.Equal(t, len(tc.expectedIssue.Assignees), len(returnedIssue.Assignees)) + for i, assignee := range returnedIssue.Assignees { + assert.Equal(t, *tc.expectedIssue.Assignees[i].Login, *assignee.Login) + } + } + + // Check labels if expected + if len(tc.expectedIssue.Labels) > 0 { + assert.Equal(t, len(tc.expectedIssue.Labels), len(returnedIssue.Labels)) + for i, label := range returnedIssue.Labels { + assert.Equal(t, *tc.expectedIssue.Labels[i].Name, *label.Name) + } + } + }) + } +} + +func Test_ListIssues(t *testing.T) { + // Verify tool definition + mockClient := github.NewClient(nil) + tool, _ := ListIssues(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "list_issues", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "state") + assert.Contains(t, tool.InputSchema.Properties, "labels") + assert.Contains(t, tool.InputSchema.Properties, "sort") + assert.Contains(t, tool.InputSchema.Properties, "direction") + assert.Contains(t, tool.InputSchema.Properties, "since") + assert.Contains(t, tool.InputSchema.Properties, "page") + assert.Contains(t, tool.InputSchema.Properties, "perPage") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"}) + + // Setup mock issues for success case + mockIssues := []*github.Issue{ + { + Number: github.Ptr(123), + Title: github.Ptr("First Issue"), + Body: github.Ptr("This is the first test issue"), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/123"), + CreatedAt: &github.Timestamp{Time: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)}, + }, + { + Number: github.Ptr(456), + Title: github.Ptr("Second Issue"), + Body: github.Ptr("This is the second test issue"), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/456"), + Labels: []*github.Label{{Name: github.Ptr("bug")}}, + CreatedAt: &github.Timestamp{Time: time.Date(2023, 2, 1, 0, 0, 0, 0, time.UTC)}, + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedIssues []*github.Issue + expectedErrMsg string + }{ + { + name: "list issues with minimal parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposIssuesByOwnerByRepo, + mockIssues, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + }, + expectError: false, + expectedIssues: mockIssues, + }, + { + name: "list issues with all parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposIssuesByOwnerByRepo, + expectQueryParams(t, map[string]string{ + "state": "open", + "labels": "bug,enhancement", + "sort": "created", + "direction": "desc", + "since": "2023-01-01T00:00:00Z", + "page": "1", + "per_page": "30", + }).andThen( + mockResponse(t, http.StatusOK, mockIssues), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "state": "open", + "labels": []any{"bug", "enhancement"}, + "sort": "created", + "direction": "desc", + "since": "2023-01-01T00:00:00Z", + "page": float64(1), + "perPage": float64(30), + }, + expectError: false, + expectedIssues: mockIssues, + }, + { + name: "invalid since parameter", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposIssuesByOwnerByRepo, + mockIssues, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "since": "invalid-date", + }, + expectError: true, + expectedErrMsg: "invalid ISO 8601 timestamp", + }, + { + name: "list issues fails with error", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposIssuesByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Repository not found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "nonexistent", + "repo": "repo", + }, + expectError: true, + expectedErrMsg: "failed to list issues", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := ListIssues(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + if err != nil { + assert.Contains(t, err.Error(), tc.expectedErrMsg) + } else { + // For errors returned as part of the result, not as an error + assert.NotNil(t, result) + textContent := getTextResult(t, result) + assert.Contains(t, textContent.Text, tc.expectedErrMsg) + } + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedIssues []*github.Issue + err = json.Unmarshal([]byte(textContent.Text), &returnedIssues) + require.NoError(t, err) + + assert.Len(t, returnedIssues, len(tc.expectedIssues)) + for i, issue := range returnedIssues { + assert.Equal(t, *tc.expectedIssues[i].Number, *issue.Number) + assert.Equal(t, *tc.expectedIssues[i].Title, *issue.Title) + assert.Equal(t, *tc.expectedIssues[i].State, *issue.State) + assert.Equal(t, *tc.expectedIssues[i].HTMLURL, *issue.HTMLURL) + } + }) + } +} + +func Test_UpdateIssue(t *testing.T) { + // Verify tool definition + mockClient := github.NewClient(nil) + tool, _ := UpdateIssue(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "update_issue", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "issue_number") + assert.Contains(t, tool.InputSchema.Properties, "title") + assert.Contains(t, tool.InputSchema.Properties, "body") + assert.Contains(t, tool.InputSchema.Properties, "state") + assert.Contains(t, tool.InputSchema.Properties, "labels") + assert.Contains(t, tool.InputSchema.Properties, "assignees") + assert.Contains(t, tool.InputSchema.Properties, "milestone") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "issue_number"}) + + // Setup mock issue for success case + mockIssue := &github.Issue{ + Number: github.Ptr(123), + Title: github.Ptr("Updated Issue Title"), + Body: github.Ptr("Updated issue description"), + State: github.Ptr("closed"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/123"), + Assignees: []*github.User{{Login: github.Ptr("assignee1")}, {Login: github.Ptr("assignee2")}}, + Labels: []*github.Label{{Name: github.Ptr("bug")}, {Name: github.Ptr("priority")}}, + Milestone: &github.Milestone{Number: github.Ptr(5)}, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedIssue *github.Issue + expectedErrMsg string + }{ + { + name: "update issue with all fields", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PatchReposIssuesByOwnerByRepoByIssueNumber, + expectRequestBody(t, map[string]any{ + "title": "Updated Issue Title", + "body": "Updated issue description", + "state": "closed", + "labels": []any{"bug", "priority"}, + "assignees": []any{"assignee1", "assignee2"}, + "milestone": float64(5), + }).andThen( + mockResponse(t, http.StatusOK, mockIssue), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(123), + "title": "Updated Issue Title", + "body": "Updated issue description", + "state": "closed", + "labels": []any{"bug", "priority"}, + "assignees": []any{"assignee1", "assignee2"}, + "milestone": float64(5), + }, + expectError: false, + expectedIssue: mockIssue, + }, + { + name: "update issue with minimal fields", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PatchReposIssuesByOwnerByRepoByIssueNumber, + mockResponse(t, http.StatusOK, &github.Issue{ + Number: github.Ptr(123), + Title: github.Ptr("Only Title Updated"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/123"), + State: github.Ptr("open"), + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(123), + "title": "Only Title Updated", + }, + expectError: false, + expectedIssue: &github.Issue{ + Number: github.Ptr(123), + Title: github.Ptr("Only Title Updated"), + HTMLURL: github.Ptr("https://github.com/owner/repo/issues/123"), + State: github.Ptr("open"), + }, + }, + { + name: "update issue fails with not found", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PatchReposIssuesByOwnerByRepoByIssueNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Issue not found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(999), + "title": "This issue doesn't exist", + }, + expectError: true, + expectedErrMsg: "failed to update issue", + }, + { + name: "update issue fails with validation error", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PatchReposIssuesByOwnerByRepoByIssueNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"message": "Invalid state value"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(123), + "state": "invalid_state", + }, + expectError: true, + expectedErrMsg: "failed to update issue", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := UpdateIssue(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + if err != nil { + assert.Contains(t, err.Error(), tc.expectedErrMsg) + } else { + // For errors returned as part of the result, not as an error + require.NotNil(t, result) + textContent := getTextResult(t, result) + assert.Contains(t, textContent.Text, tc.expectedErrMsg) + } + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedIssue github.Issue + err = json.Unmarshal([]byte(textContent.Text), &returnedIssue) + require.NoError(t, err) + + assert.Equal(t, *tc.expectedIssue.Number, *returnedIssue.Number) + assert.Equal(t, *tc.expectedIssue.Title, *returnedIssue.Title) + assert.Equal(t, *tc.expectedIssue.State, *returnedIssue.State) + assert.Equal(t, *tc.expectedIssue.HTMLURL, *returnedIssue.HTMLURL) + + if tc.expectedIssue.Body != nil { + assert.Equal(t, *tc.expectedIssue.Body, *returnedIssue.Body) + } + + // Check assignees if expected + if len(tc.expectedIssue.Assignees) > 0 { + assert.Len(t, returnedIssue.Assignees, len(tc.expectedIssue.Assignees)) + for i, assignee := range returnedIssue.Assignees { + assert.Equal(t, *tc.expectedIssue.Assignees[i].Login, *assignee.Login) + } + } + + // Check labels if expected + if len(tc.expectedIssue.Labels) > 0 { + assert.Len(t, returnedIssue.Labels, len(tc.expectedIssue.Labels)) + for i, label := range returnedIssue.Labels { + assert.Equal(t, *tc.expectedIssue.Labels[i].Name, *label.Name) + } + } + + // Check milestone if expected + if tc.expectedIssue.Milestone != nil { + assert.NotNil(t, returnedIssue.Milestone) + assert.Equal(t, *tc.expectedIssue.Milestone.Number, *returnedIssue.Milestone.Number) + } + }) + } +} + +func Test_ParseISOTimestamp(t *testing.T) { + tests := []struct { + name string + input string + expectedErr bool + expectedTime time.Time + }{ + { + name: "valid RFC3339 format", + input: "2023-01-15T14:30:00Z", + expectedErr: false, + expectedTime: time.Date(2023, 1, 15, 14, 30, 0, 0, time.UTC), + }, + { + name: "valid date only format", + input: "2023-01-15", + expectedErr: false, + expectedTime: time.Date(2023, 1, 15, 0, 0, 0, 0, time.UTC), + }, + { + name: "empty timestamp", + input: "", + expectedErr: true, + }, + { + name: "invalid format", + input: "15/01/2023", + expectedErr: true, + }, + { + name: "invalid date", + input: "2023-13-45", + expectedErr: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + parsedTime, err := parseISOTimestamp(tc.input) + + if tc.expectedErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expectedTime, parsedTime) + } + }) + } +} + +func Test_GetIssueComments(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetIssueComments(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_issue_comments", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "issue_number") + assert.Contains(t, tool.InputSchema.Properties, "page") + assert.Contains(t, tool.InputSchema.Properties, "per_page") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "issue_number"}) + + // Setup mock comments for success case + mockComments := []*github.IssueComment{ + { + ID: github.Ptr(int64(123)), + Body: github.Ptr("This is the first comment"), + User: &github.User{ + Login: github.Ptr("user1"), + }, + CreatedAt: &github.Timestamp{Time: time.Now().Add(-time.Hour * 24)}, + }, + { + ID: github.Ptr(int64(456)), + Body: github.Ptr("This is the second comment"), + User: &github.User{ + Login: github.Ptr("user2"), + }, + CreatedAt: &github.Timestamp{Time: time.Now().Add(-time.Hour)}, + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedComments []*github.IssueComment + expectedErrMsg string + }{ + { + name: "successful comments retrieval", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposIssuesCommentsByOwnerByRepoByIssueNumber, + mockComments, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(42), + }, + expectError: false, + expectedComments: mockComments, + }, + { + name: "successful comments retrieval with pagination", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposIssuesCommentsByOwnerByRepoByIssueNumber, + expectQueryParams(t, map[string]string{ + "page": "2", + "per_page": "10", + }).andThen( + mockResponse(t, http.StatusOK, mockComments), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(42), + "page": float64(2), + "per_page": float64(10), + }, + expectError: false, + expectedComments: mockComments, + }, + { + name: "issue not found", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposIssuesCommentsByOwnerByRepoByIssueNumber, + mockResponse(t, http.StatusNotFound, `{"message": "Issue not found"}`), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "issue_number": float64(999), + }, + expectError: true, + expectedErrMsg: "failed to get issue comments", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetIssueComments(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedComments []*github.IssueComment + err = json.Unmarshal([]byte(textContent.Text), &returnedComments) + require.NoError(t, err) + assert.Equal(t, len(tc.expectedComments), len(returnedComments)) + if len(returnedComments) > 0 { + assert.Equal(t, *tc.expectedComments[0].Body, *returnedComments[0].Body) + assert.Equal(t, *tc.expectedComments[0].User.Login, *returnedComments[0].User.Login) + } + }) + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/pullrequests.go b/sre_agent/servers/github-mcp-server/pkg/github/pullrequests.go new file mode 100644 index 0000000..9c8fca1 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/pullrequests.go @@ -0,0 +1,1248 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +// GetPullRequest creates a tool to get details of a specific pull request. +func GetPullRequest(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_pull_request", + mcp.WithDescription(t("TOOL_GET_PULL_REQUEST_DESCRIPTION", "Get details of a specific pull request in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_PULL_REQUEST_USER_TITLE", "Get pull request details"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("pullNumber", + mcp.Required(), + mcp.Description("Pull request number"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pullNumber, err := RequiredInt(request, "pullNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + pr, resp, err := client.PullRequests.Get(ctx, owner, repo, pullNumber) + if err != nil { + return nil, fmt.Errorf("failed to get pull request: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get pull request: %s", string(body))), nil + } + + r, err := json.Marshal(pr) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// UpdatePullRequest creates a tool to update an existing pull request. +func UpdatePullRequest(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("update_pull_request", + mcp.WithDescription(t("TOOL_UPDATE_PULL_REQUEST_DESCRIPTION", "Update an existing pull request in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_UPDATE_PULL_REQUEST_USER_TITLE", "Edit pull request"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("pullNumber", + mcp.Required(), + mcp.Description("Pull request number to update"), + ), + mcp.WithString("title", + mcp.Description("New title"), + ), + mcp.WithString("body", + mcp.Description("New description"), + ), + mcp.WithString("state", + mcp.Description("New state"), + mcp.Enum("open", "closed"), + ), + mcp.WithString("base", + mcp.Description("New base branch name"), + ), + mcp.WithBoolean("maintainer_can_modify", + mcp.Description("Allow maintainer edits"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pullNumber, err := RequiredInt(request, "pullNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + // Build the update struct only with provided fields + update := &github.PullRequest{} + updateNeeded := false + + if title, ok, err := OptionalParamOK[string](request, "title"); err != nil { + return mcp.NewToolResultError(err.Error()), nil + } else if ok { + update.Title = github.Ptr(title) + updateNeeded = true + } + + if body, ok, err := OptionalParamOK[string](request, "body"); err != nil { + return mcp.NewToolResultError(err.Error()), nil + } else if ok { + update.Body = github.Ptr(body) + updateNeeded = true + } + + if state, ok, err := OptionalParamOK[string](request, "state"); err != nil { + return mcp.NewToolResultError(err.Error()), nil + } else if ok { + update.State = github.Ptr(state) + updateNeeded = true + } + + if base, ok, err := OptionalParamOK[string](request, "base"); err != nil { + return mcp.NewToolResultError(err.Error()), nil + } else if ok { + update.Base = &github.PullRequestBranch{Ref: github.Ptr(base)} + updateNeeded = true + } + + if maintainerCanModify, ok, err := OptionalParamOK[bool](request, "maintainer_can_modify"); err != nil { + return mcp.NewToolResultError(err.Error()), nil + } else if ok { + update.MaintainerCanModify = github.Ptr(maintainerCanModify) + updateNeeded = true + } + + if !updateNeeded { + return mcp.NewToolResultError("No update parameters provided."), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + pr, resp, err := client.PullRequests.Edit(ctx, owner, repo, pullNumber, update) + if err != nil { + return nil, fmt.Errorf("failed to update pull request: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to update pull request: %s", string(body))), nil + } + + r, err := json.Marshal(pr) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// ListPullRequests creates a tool to list and filter repository pull requests. +func ListPullRequests(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("list_pull_requests", + mcp.WithDescription(t("TOOL_LIST_PULL_REQUESTS_DESCRIPTION", "List pull requests in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_LIST_PULL_REQUESTS_USER_TITLE", "List pull requests"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("state", + mcp.Description("Filter by state"), + mcp.Enum("open", "closed", "all"), + ), + mcp.WithString("head", + mcp.Description("Filter by head user/org and branch"), + ), + mcp.WithString("base", + mcp.Description("Filter by base branch"), + ), + mcp.WithString("sort", + mcp.Description("Sort by"), + mcp.Enum("created", "updated", "popularity", "long-running"), + ), + mcp.WithString("direction", + mcp.Description("Sort direction"), + mcp.Enum("asc", "desc"), + ), + WithPagination(), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + state, err := OptionalParam[string](request, "state") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + head, err := OptionalParam[string](request, "head") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + base, err := OptionalParam[string](request, "base") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + sort, err := OptionalParam[string](request, "sort") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + direction, err := OptionalParam[string](request, "direction") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pagination, err := OptionalPaginationParams(request) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.PullRequestListOptions{ + State: state, + Head: head, + Base: base, + Sort: sort, + Direction: direction, + ListOptions: github.ListOptions{ + PerPage: pagination.perPage, + Page: pagination.page, + }, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + prs, resp, err := client.PullRequests.List(ctx, owner, repo, opts) + if err != nil { + return nil, fmt.Errorf("failed to list pull requests: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to list pull requests: %s", string(body))), nil + } + + r, err := json.Marshal(prs) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// MergePullRequest creates a tool to merge a pull request. +func MergePullRequest(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("merge_pull_request", + mcp.WithDescription(t("TOOL_MERGE_PULL_REQUEST_DESCRIPTION", "Merge a pull request in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_MERGE_PULL_REQUEST_USER_TITLE", "Merge pull request"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("pullNumber", + mcp.Required(), + mcp.Description("Pull request number"), + ), + mcp.WithString("commit_title", + mcp.Description("Title for merge commit"), + ), + mcp.WithString("commit_message", + mcp.Description("Extra detail for merge commit"), + ), + mcp.WithString("merge_method", + mcp.Description("Merge method"), + mcp.Enum("merge", "squash", "rebase"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pullNumber, err := RequiredInt(request, "pullNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + commitTitle, err := OptionalParam[string](request, "commit_title") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + commitMessage, err := OptionalParam[string](request, "commit_message") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + mergeMethod, err := OptionalParam[string](request, "merge_method") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + options := &github.PullRequestOptions{ + CommitTitle: commitTitle, + MergeMethod: mergeMethod, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + result, resp, err := client.PullRequests.Merge(ctx, owner, repo, pullNumber, commitMessage, options) + if err != nil { + return nil, fmt.Errorf("failed to merge pull request: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to merge pull request: %s", string(body))), nil + } + + r, err := json.Marshal(result) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// GetPullRequestFiles creates a tool to get the list of files changed in a pull request. +func GetPullRequestFiles(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_pull_request_files", + mcp.WithDescription(t("TOOL_GET_PULL_REQUEST_FILES_DESCRIPTION", "Get the files changed in a specific pull request.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_PULL_REQUEST_FILES_USER_TITLE", "Get pull request files"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("pullNumber", + mcp.Required(), + mcp.Description("Pull request number"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pullNumber, err := RequiredInt(request, "pullNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + opts := &github.ListOptions{} + files, resp, err := client.PullRequests.ListFiles(ctx, owner, repo, pullNumber, opts) + if err != nil { + return nil, fmt.Errorf("failed to get pull request files: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get pull request files: %s", string(body))), nil + } + + r, err := json.Marshal(files) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// GetPullRequestStatus creates a tool to get the combined status of all status checks for a pull request. +func GetPullRequestStatus(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_pull_request_status", + mcp.WithDescription(t("TOOL_GET_PULL_REQUEST_STATUS_DESCRIPTION", "Get the status of a specific pull request.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_PULL_REQUEST_STATUS_USER_TITLE", "Get pull request status checks"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("pullNumber", + mcp.Required(), + mcp.Description("Pull request number"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pullNumber, err := RequiredInt(request, "pullNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + // First get the PR to find the head SHA + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + pr, resp, err := client.PullRequests.Get(ctx, owner, repo, pullNumber) + if err != nil { + return nil, fmt.Errorf("failed to get pull request: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get pull request: %s", string(body))), nil + } + + // Get combined status for the head SHA + status, resp, err := client.Repositories.GetCombinedStatus(ctx, owner, repo, *pr.Head.SHA, nil) + if err != nil { + return nil, fmt.Errorf("failed to get combined status: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get combined status: %s", string(body))), nil + } + + r, err := json.Marshal(status) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// UpdatePullRequestBranch creates a tool to update a pull request branch with the latest changes from the base branch. +func UpdatePullRequestBranch(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("update_pull_request_branch", + mcp.WithDescription(t("TOOL_UPDATE_PULL_REQUEST_BRANCH_DESCRIPTION", "Update the branch of a pull request with the latest changes from the base branch.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_UPDATE_PULL_REQUEST_BRANCH_USER_TITLE", "Update pull request branch"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("pullNumber", + mcp.Required(), + mcp.Description("Pull request number"), + ), + mcp.WithString("expectedHeadSha", + mcp.Description("The expected SHA of the pull request's HEAD ref"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pullNumber, err := RequiredInt(request, "pullNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + expectedHeadSHA, err := OptionalParam[string](request, "expectedHeadSha") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + opts := &github.PullRequestBranchUpdateOptions{} + if expectedHeadSHA != "" { + opts.ExpectedHeadSHA = github.Ptr(expectedHeadSHA) + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + result, resp, err := client.PullRequests.UpdateBranch(ctx, owner, repo, pullNumber, opts) + if err != nil { + // Check if it's an acceptedError. An acceptedError indicates that the update is in progress, + // and it's not a real error. + if resp != nil && resp.StatusCode == http.StatusAccepted && isAcceptedError(err) { + return mcp.NewToolResultText("Pull request branch update is in progress"), nil + } + return nil, fmt.Errorf("failed to update pull request branch: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusAccepted { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to update pull request branch: %s", string(body))), nil + } + + r, err := json.Marshal(result) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// GetPullRequestComments creates a tool to get the review comments on a pull request. +func GetPullRequestComments(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_pull_request_comments", + mcp.WithDescription(t("TOOL_GET_PULL_REQUEST_COMMENTS_DESCRIPTION", "Get comments for a specific pull request.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_PULL_REQUEST_COMMENTS_USER_TITLE", "Get pull request comments"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("pullNumber", + mcp.Required(), + mcp.Description("Pull request number"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pullNumber, err := RequiredInt(request, "pullNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.PullRequestListCommentsOptions{ + ListOptions: github.ListOptions{ + PerPage: 100, + }, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + comments, resp, err := client.PullRequests.ListComments(ctx, owner, repo, pullNumber, opts) + if err != nil { + return nil, fmt.Errorf("failed to get pull request comments: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get pull request comments: %s", string(body))), nil + } + + r, err := json.Marshal(comments) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// AddPullRequestReviewComment creates a tool to add a review comment to a pull request. +func AddPullRequestReviewComment(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("add_pull_request_review_comment", + mcp.WithDescription(t("TOOL_ADD_PULL_REQUEST_REVIEW_COMMENT_DESCRIPTION", "Add a review comment to a pull request.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_ADD_PULL_REQUEST_REVIEW_COMMENT_USER_TITLE", "Add review comment to pull request"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("pull_number", + mcp.Required(), + mcp.Description("Pull request number"), + ), + mcp.WithString("body", + mcp.Required(), + mcp.Description("The text of the review comment"), + ), + mcp.WithString("commit_id", + mcp.Description("The SHA of the commit to comment on. Required unless in_reply_to is specified."), + ), + mcp.WithString("path", + mcp.Description("The relative path to the file that necessitates a comment. Required unless in_reply_to is specified."), + ), + mcp.WithString("subject_type", + mcp.Description("The level at which the comment is targeted"), + mcp.Enum("line", "file"), + ), + mcp.WithNumber("line", + mcp.Description("The line of the blob in the pull request diff that the comment applies to. For multi-line comments, the last line of the range"), + ), + mcp.WithString("side", + mcp.Description("The side of the diff to comment on"), + mcp.Enum("LEFT", "RIGHT"), + ), + mcp.WithNumber("start_line", + mcp.Description("For multi-line comments, the first line of the range that the comment applies to"), + ), + mcp.WithString("start_side", + mcp.Description("For multi-line comments, the starting side of the diff that the comment applies to"), + mcp.Enum("LEFT", "RIGHT"), + ), + mcp.WithNumber("in_reply_to", + mcp.Description("The ID of the review comment to reply to. When specified, only body is required and all other parameters are ignored"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pullNumber, err := RequiredInt(request, "pull_number") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + body, err := requiredParam[string](request, "body") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + + // Check if this is a reply to an existing comment + if replyToFloat, ok := request.Params.Arguments["in_reply_to"].(float64); ok { + // Use the specialized method for reply comments due to inconsistency in underlying go-github library: https://github.com/google/go-github/pull/950 + commentID := int64(replyToFloat) + createdReply, resp, err := client.PullRequests.CreateCommentInReplyTo(ctx, owner, repo, pullNumber, body, commentID) + if err != nil { + return nil, fmt.Errorf("failed to reply to pull request comment: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusCreated { + respBody, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to reply to pull request comment: %s", string(respBody))), nil + } + + r, err := json.Marshal(createdReply) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } + + // This is a new comment, not a reply + // Verify required parameters for a new comment + commitID, err := requiredParam[string](request, "commit_id") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + path, err := requiredParam[string](request, "path") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + comment := &github.PullRequestComment{ + Body: github.Ptr(body), + CommitID: github.Ptr(commitID), + Path: github.Ptr(path), + } + + subjectType, err := OptionalParam[string](request, "subject_type") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if subjectType != "file" { + line, lineExists := request.Params.Arguments["line"].(float64) + startLine, startLineExists := request.Params.Arguments["start_line"].(float64) + side, sideExists := request.Params.Arguments["side"].(string) + startSide, startSideExists := request.Params.Arguments["start_side"].(string) + + if !lineExists { + return mcp.NewToolResultError("line parameter is required unless using subject_type:file"), nil + } + + comment.Line = github.Ptr(int(line)) + if sideExists { + comment.Side = github.Ptr(side) + } + if startLineExists { + comment.StartLine = github.Ptr(int(startLine)) + } + if startSideExists { + comment.StartSide = github.Ptr(startSide) + } + + if startLineExists && !lineExists { + return mcp.NewToolResultError("if start_line is provided, line must also be provided"), nil + } + if startSideExists && !sideExists { + return mcp.NewToolResultError("if start_side is provided, side must also be provided"), nil + } + } + + createdComment, resp, err := client.PullRequests.CreateComment(ctx, owner, repo, pullNumber, comment) + if err != nil { + return nil, fmt.Errorf("failed to create pull request comment: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusCreated { + respBody, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to create pull request comment: %s", string(respBody))), nil + } + + r, err := json.Marshal(createdComment) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// GetPullRequestReviews creates a tool to get the reviews on a pull request. +func GetPullRequestReviews(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_pull_request_reviews", + mcp.WithDescription(t("TOOL_GET_PULL_REQUEST_REVIEWS_DESCRIPTION", "Get reviews for a specific pull request.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_PULL_REQUEST_REVIEWS_USER_TITLE", "Get pull request reviews"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("pullNumber", + mcp.Required(), + mcp.Description("Pull request number"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pullNumber, err := RequiredInt(request, "pullNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + reviews, resp, err := client.PullRequests.ListReviews(ctx, owner, repo, pullNumber, nil) + if err != nil { + return nil, fmt.Errorf("failed to get pull request reviews: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get pull request reviews: %s", string(body))), nil + } + + r, err := json.Marshal(reviews) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// CreatePullRequestReview creates a tool to submit a review on a pull request. +func CreatePullRequestReview(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("create_pull_request_review", + mcp.WithDescription(t("TOOL_CREATE_PULL_REQUEST_REVIEW_DESCRIPTION", "Create a review for a pull request.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_CREATE_PULL_REQUEST_REVIEW_USER_TITLE", "Submit pull request review"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithNumber("pullNumber", + mcp.Required(), + mcp.Description("Pull request number"), + ), + mcp.WithString("body", + mcp.Description("Review comment text"), + ), + mcp.WithString("event", + mcp.Required(), + mcp.Description("Review action to perform"), + mcp.Enum("APPROVE", "REQUEST_CHANGES", "COMMENT"), + ), + mcp.WithString("commitId", + mcp.Description("SHA of commit to review"), + ), + mcp.WithArray("comments", + mcp.Items( + map[string]interface{}{ + "type": "object", + "additionalProperties": false, + "required": []string{"path", "body", "position", "line", "side", "start_line", "start_side"}, + "properties": map[string]interface{}{ + "path": map[string]interface{}{ + "type": "string", + "description": "path to the file", + }, + "position": map[string]interface{}{ + "anyOf": []interface{}{ + map[string]string{"type": "number"}, + map[string]string{"type": "null"}, + }, + "description": "position of the comment in the diff", + }, + "line": map[string]interface{}{ + "anyOf": []interface{}{ + map[string]string{"type": "number"}, + map[string]string{"type": "null"}, + }, + "description": "line number in the file to comment on. For multi-line comments, the end of the line range", + }, + "side": map[string]interface{}{ + "anyOf": []interface{}{ + map[string]string{"type": "string"}, + map[string]string{"type": "null"}, + }, + "description": "The side of the diff on which the line resides. For multi-line comments, this is the side for the end of the line range. (LEFT or RIGHT)", + }, + "start_line": map[string]interface{}{ + "anyOf": []interface{}{ + map[string]string{"type": "number"}, + map[string]string{"type": "null"}, + }, + "description": "The first line of the range to which the comment refers. Required for multi-line comments.", + }, + "start_side": map[string]interface{}{ + "anyOf": []interface{}{ + map[string]string{"type": "string"}, + map[string]string{"type": "null"}, + }, + "description": "The side of the diff on which the start line resides for multi-line comments. (LEFT or RIGHT)", + }, + "body": map[string]interface{}{ + "type": "string", + "description": "comment body", + }, + }, + }, + ), + mcp.Description("Line-specific comments array of objects to place comments on pull request changes. Requires path and body. For line comments use line or position. For multi-line comments use start_line and line with optional side parameters."), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pullNumber, err := RequiredInt(request, "pullNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + event, err := requiredParam[string](request, "event") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + // Create review request + reviewRequest := &github.PullRequestReviewRequest{ + Event: github.Ptr(event), + } + + // Add body if provided + body, err := OptionalParam[string](request, "body") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if body != "" { + reviewRequest.Body = github.Ptr(body) + } + + // Add commit ID if provided + commitID, err := OptionalParam[string](request, "commitId") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if commitID != "" { + reviewRequest.CommitID = github.Ptr(commitID) + } + + // Add comments if provided + if commentsObj, ok := request.Params.Arguments["comments"].([]interface{}); ok && len(commentsObj) > 0 { + comments := []*github.DraftReviewComment{} + + for _, c := range commentsObj { + commentMap, ok := c.(map[string]interface{}) + if !ok { + return mcp.NewToolResultError("each comment must be an object with path and body"), nil + } + + path, ok := commentMap["path"].(string) + if !ok || path == "" { + return mcp.NewToolResultError("each comment must have a path"), nil + } + + body, ok := commentMap["body"].(string) + if !ok || body == "" { + return mcp.NewToolResultError("each comment must have a body"), nil + } + + _, hasPosition := commentMap["position"].(float64) + _, hasLine := commentMap["line"].(float64) + _, hasSide := commentMap["side"].(string) + _, hasStartLine := commentMap["start_line"].(float64) + _, hasStartSide := commentMap["start_side"].(string) + + switch { + case !hasPosition && !hasLine: + return mcp.NewToolResultError("each comment must have either position or line"), nil + case hasPosition && (hasLine || hasSide || hasStartLine || hasStartSide): + return mcp.NewToolResultError("position cannot be combined with line, side, start_line, or start_side"), nil + case hasStartSide && !hasSide: + return mcp.NewToolResultError("if start_side is provided, side must also be provided"), nil + } + + comment := &github.DraftReviewComment{ + Path: github.Ptr(path), + Body: github.Ptr(body), + } + + if positionFloat, ok := commentMap["position"].(float64); ok { + comment.Position = github.Ptr(int(positionFloat)) + } else if lineFloat, ok := commentMap["line"].(float64); ok { + comment.Line = github.Ptr(int(lineFloat)) + } + if side, ok := commentMap["side"].(string); ok { + comment.Side = github.Ptr(side) + } + if startLineFloat, ok := commentMap["start_line"].(float64); ok { + comment.StartLine = github.Ptr(int(startLineFloat)) + } + if startSide, ok := commentMap["start_side"].(string); ok { + comment.StartSide = github.Ptr(startSide) + } + + comments = append(comments, comment) + } + + reviewRequest.Comments = comments + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + review, resp, err := client.PullRequests.CreateReview(ctx, owner, repo, pullNumber, reviewRequest) + if err != nil { + return nil, fmt.Errorf("failed to create pull request review: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to create pull request review: %s", string(body))), nil + } + + r, err := json.Marshal(review) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// CreatePullRequest creates a tool to create a new pull request. +func CreatePullRequest(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("create_pull_request", + mcp.WithDescription(t("TOOL_CREATE_PULL_REQUEST_DESCRIPTION", "Create a new pull request in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_CREATE_PULL_REQUEST_USER_TITLE", "Open new pull request"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("title", + mcp.Required(), + mcp.Description("PR title"), + ), + mcp.WithString("body", + mcp.Description("PR description"), + ), + mcp.WithString("head", + mcp.Required(), + mcp.Description("Branch containing changes"), + ), + mcp.WithString("base", + mcp.Required(), + mcp.Description("Branch to merge into"), + ), + mcp.WithBoolean("draft", + mcp.Description("Create as draft PR"), + ), + mcp.WithBoolean("maintainer_can_modify", + mcp.Description("Allow maintainer edits"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + title, err := requiredParam[string](request, "title") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + head, err := requiredParam[string](request, "head") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + base, err := requiredParam[string](request, "base") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + body, err := OptionalParam[string](request, "body") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + draft, err := OptionalParam[bool](request, "draft") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + maintainerCanModify, err := OptionalParam[bool](request, "maintainer_can_modify") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + newPR := &github.NewPullRequest{ + Title: github.Ptr(title), + Head: github.Ptr(head), + Base: github.Ptr(base), + } + + if body != "" { + newPR.Body = github.Ptr(body) + } + + newPR.Draft = github.Ptr(draft) + newPR.MaintainerCanModify = github.Ptr(maintainerCanModify) + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + pr, resp, err := client.PullRequests.Create(ctx, owner, repo, newPR) + if err != nil { + return nil, fmt.Errorf("failed to create pull request: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusCreated { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to create pull request: %s", string(body))), nil + } + + r, err := json.Marshal(pr) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/pullrequests_test.go b/sre_agent/servers/github-mcp-server/pkg/github/pullrequests_test.go new file mode 100644 index 0000000..bb37262 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/pullrequests_test.go @@ -0,0 +1,1918 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "testing" + "time" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/migueleliasweb/go-github-mock/src/mock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_GetPullRequest(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetPullRequest(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_pull_request", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "pullNumber") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "pullNumber"}) + + // Setup mock PR for success case + mockPR := &github.PullRequest{ + Number: github.Ptr(42), + Title: github.Ptr("Test PR"), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42"), + Head: &github.PullRequestBranch{ + SHA: github.Ptr("abcd1234"), + Ref: github.Ptr("feature-branch"), + }, + Base: &github.PullRequestBranch{ + Ref: github.Ptr("main"), + }, + Body: github.Ptr("This is a test PR"), + User: &github.User{ + Login: github.Ptr("testuser"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedPR *github.PullRequest + expectedErrMsg string + }{ + { + name: "successful PR fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposPullsByOwnerByRepoByPullNumber, + mockPR, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + }, + expectError: false, + expectedPR: mockPR, + }, + { + name: "PR fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposPullsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(999), + }, + expectError: true, + expectedErrMsg: "failed to get pull request", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetPullRequest(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedPR github.PullRequest + err = json.Unmarshal([]byte(textContent.Text), &returnedPR) + require.NoError(t, err) + assert.Equal(t, *tc.expectedPR.Number, *returnedPR.Number) + assert.Equal(t, *tc.expectedPR.Title, *returnedPR.Title) + assert.Equal(t, *tc.expectedPR.State, *returnedPR.State) + assert.Equal(t, *tc.expectedPR.HTMLURL, *returnedPR.HTMLURL) + }) + } +} + +func Test_UpdatePullRequest(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := UpdatePullRequest(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "update_pull_request", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "pullNumber") + assert.Contains(t, tool.InputSchema.Properties, "title") + assert.Contains(t, tool.InputSchema.Properties, "body") + assert.Contains(t, tool.InputSchema.Properties, "state") + assert.Contains(t, tool.InputSchema.Properties, "base") + assert.Contains(t, tool.InputSchema.Properties, "maintainer_can_modify") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "pullNumber"}) + + // Setup mock PR for success case + mockUpdatedPR := &github.PullRequest{ + Number: github.Ptr(42), + Title: github.Ptr("Updated Test PR Title"), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42"), + Body: github.Ptr("Updated test PR body."), + MaintainerCanModify: github.Ptr(false), + Base: &github.PullRequestBranch{ + Ref: github.Ptr("develop"), + }, + } + + mockClosedPR := &github.PullRequest{ + Number: github.Ptr(42), + Title: github.Ptr("Test PR"), + State: github.Ptr("closed"), // State updated + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedPR *github.PullRequest + expectedErrMsg string + }{ + { + name: "successful PR update (title, body, base, maintainer_can_modify)", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PatchReposPullsByOwnerByRepoByPullNumber, + // Expect the flat string based on previous test failure output and API docs + expectRequestBody(t, map[string]interface{}{ + "title": "Updated Test PR Title", + "body": "Updated test PR body.", + "base": "develop", + "maintainer_can_modify": false, + }).andThen( + mockResponse(t, http.StatusOK, mockUpdatedPR), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "title": "Updated Test PR Title", + "body": "Updated test PR body.", + "base": "develop", + "maintainer_can_modify": false, + }, + expectError: false, + expectedPR: mockUpdatedPR, + }, + { + name: "successful PR update (state)", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PatchReposPullsByOwnerByRepoByPullNumber, + expectRequestBody(t, map[string]interface{}{ + "state": "closed", + }).andThen( + mockResponse(t, http.StatusOK, mockClosedPR), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "state": "closed", + }, + expectError: false, + expectedPR: mockClosedPR, + }, + { + name: "no update parameters provided", + mockedClient: mock.NewMockedHTTPClient(), // No API call expected + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + // No update fields + }, + expectError: false, // Error is returned in the result, not as Go error + expectedErrMsg: "No update parameters provided", + }, + { + name: "PR update fails (API error)", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PatchReposPullsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"message": "Validation Failed"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "title": "Invalid Title Causing Error", + }, + expectError: true, + expectedErrMsg: "failed to update pull request", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := UpdatePullRequest(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content + textContent := getTextResult(t, result) + + // Check for expected error message within the result text + if tc.expectedErrMsg != "" { + assert.Contains(t, textContent.Text, tc.expectedErrMsg) + return + } + + // Unmarshal and verify the successful result + var returnedPR github.PullRequest + err = json.Unmarshal([]byte(textContent.Text), &returnedPR) + require.NoError(t, err) + assert.Equal(t, *tc.expectedPR.Number, *returnedPR.Number) + if tc.expectedPR.Title != nil { + assert.Equal(t, *tc.expectedPR.Title, *returnedPR.Title) + } + if tc.expectedPR.Body != nil { + assert.Equal(t, *tc.expectedPR.Body, *returnedPR.Body) + } + if tc.expectedPR.State != nil { + assert.Equal(t, *tc.expectedPR.State, *returnedPR.State) + } + if tc.expectedPR.Base != nil && tc.expectedPR.Base.Ref != nil { + assert.NotNil(t, returnedPR.Base) + assert.Equal(t, *tc.expectedPR.Base.Ref, *returnedPR.Base.Ref) + } + if tc.expectedPR.MaintainerCanModify != nil { + assert.Equal(t, *tc.expectedPR.MaintainerCanModify, *returnedPR.MaintainerCanModify) + } + }) + } +} + +func Test_ListPullRequests(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := ListPullRequests(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "list_pull_requests", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "state") + assert.Contains(t, tool.InputSchema.Properties, "head") + assert.Contains(t, tool.InputSchema.Properties, "base") + assert.Contains(t, tool.InputSchema.Properties, "sort") + assert.Contains(t, tool.InputSchema.Properties, "direction") + assert.Contains(t, tool.InputSchema.Properties, "perPage") + assert.Contains(t, tool.InputSchema.Properties, "page") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"}) + + // Setup mock PRs for success case + mockPRs := []*github.PullRequest{ + { + Number: github.Ptr(42), + Title: github.Ptr("First PR"), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42"), + }, + { + Number: github.Ptr(43), + Title: github.Ptr("Second PR"), + State: github.Ptr("closed"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/43"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedPRs []*github.PullRequest + expectedErrMsg string + }{ + { + name: "successful PRs listing", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposPullsByOwnerByRepo, + expectQueryParams(t, map[string]string{ + "state": "all", + "sort": "created", + "direction": "desc", + "per_page": "30", + "page": "1", + }).andThen( + mockResponse(t, http.StatusOK, mockPRs), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "state": "all", + "sort": "created", + "direction": "desc", + "perPage": float64(30), + "page": float64(1), + }, + expectError: false, + expectedPRs: mockPRs, + }, + { + name: "PRs listing fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposPullsByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"message": "Invalid request"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "state": "invalid", + }, + expectError: true, + expectedErrMsg: "failed to list pull requests", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := ListPullRequests(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedPRs []*github.PullRequest + err = json.Unmarshal([]byte(textContent.Text), &returnedPRs) + require.NoError(t, err) + assert.Len(t, returnedPRs, 2) + assert.Equal(t, *tc.expectedPRs[0].Number, *returnedPRs[0].Number) + assert.Equal(t, *tc.expectedPRs[0].Title, *returnedPRs[0].Title) + assert.Equal(t, *tc.expectedPRs[0].State, *returnedPRs[0].State) + assert.Equal(t, *tc.expectedPRs[1].Number, *returnedPRs[1].Number) + assert.Equal(t, *tc.expectedPRs[1].Title, *returnedPRs[1].Title) + assert.Equal(t, *tc.expectedPRs[1].State, *returnedPRs[1].State) + }) + } +} + +func Test_MergePullRequest(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := MergePullRequest(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "merge_pull_request", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "pullNumber") + assert.Contains(t, tool.InputSchema.Properties, "commit_title") + assert.Contains(t, tool.InputSchema.Properties, "commit_message") + assert.Contains(t, tool.InputSchema.Properties, "merge_method") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "pullNumber"}) + + // Setup mock merge result for success case + mockMergeResult := &github.PullRequestMergeResult{ + Merged: github.Ptr(true), + Message: github.Ptr("Pull Request successfully merged"), + SHA: github.Ptr("abcd1234efgh5678"), + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedMergeResult *github.PullRequestMergeResult + expectedErrMsg string + }{ + { + name: "successful merge", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PutReposPullsMergeByOwnerByRepoByPullNumber, + expectRequestBody(t, map[string]interface{}{ + "commit_title": "Merge PR #42", + "commit_message": "Merging awesome feature", + "merge_method": "squash", + }).andThen( + mockResponse(t, http.StatusOK, mockMergeResult), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "commit_title": "Merge PR #42", + "commit_message": "Merging awesome feature", + "merge_method": "squash", + }, + expectError: false, + expectedMergeResult: mockMergeResult, + }, + { + name: "merge fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PutReposPullsMergeByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusMethodNotAllowed) + _, _ = w.Write([]byte(`{"message": "Pull request cannot be merged"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + }, + expectError: true, + expectedErrMsg: "failed to merge pull request", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := MergePullRequest(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedResult github.PullRequestMergeResult + err = json.Unmarshal([]byte(textContent.Text), &returnedResult) + require.NoError(t, err) + assert.Equal(t, *tc.expectedMergeResult.Merged, *returnedResult.Merged) + assert.Equal(t, *tc.expectedMergeResult.Message, *returnedResult.Message) + assert.Equal(t, *tc.expectedMergeResult.SHA, *returnedResult.SHA) + }) + } +} + +func Test_GetPullRequestFiles(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetPullRequestFiles(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_pull_request_files", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "pullNumber") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "pullNumber"}) + + // Setup mock PR files for success case + mockFiles := []*github.CommitFile{ + { + Filename: github.Ptr("file1.go"), + Status: github.Ptr("modified"), + Additions: github.Ptr(10), + Deletions: github.Ptr(5), + Changes: github.Ptr(15), + Patch: github.Ptr("@@ -1,5 +1,10 @@"), + }, + { + Filename: github.Ptr("file2.go"), + Status: github.Ptr("added"), + Additions: github.Ptr(20), + Deletions: github.Ptr(0), + Changes: github.Ptr(20), + Patch: github.Ptr("@@ -0,0 +1,20 @@"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedFiles []*github.CommitFile + expectedErrMsg string + }{ + { + name: "successful files fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposPullsFilesByOwnerByRepoByPullNumber, + mockFiles, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + }, + expectError: false, + expectedFiles: mockFiles, + }, + { + name: "files fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposPullsFilesByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(999), + }, + expectError: true, + expectedErrMsg: "failed to get pull request files", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetPullRequestFiles(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedFiles []*github.CommitFile + err = json.Unmarshal([]byte(textContent.Text), &returnedFiles) + require.NoError(t, err) + assert.Len(t, returnedFiles, len(tc.expectedFiles)) + for i, file := range returnedFiles { + assert.Equal(t, *tc.expectedFiles[i].Filename, *file.Filename) + assert.Equal(t, *tc.expectedFiles[i].Status, *file.Status) + assert.Equal(t, *tc.expectedFiles[i].Additions, *file.Additions) + assert.Equal(t, *tc.expectedFiles[i].Deletions, *file.Deletions) + } + }) + } +} + +func Test_GetPullRequestStatus(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetPullRequestStatus(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_pull_request_status", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "pullNumber") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "pullNumber"}) + + // Setup mock PR for successful PR fetch + mockPR := &github.PullRequest{ + Number: github.Ptr(42), + Title: github.Ptr("Test PR"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42"), + Head: &github.PullRequestBranch{ + SHA: github.Ptr("abcd1234"), + Ref: github.Ptr("feature-branch"), + }, + } + + // Setup mock status for success case + mockStatus := &github.CombinedStatus{ + State: github.Ptr("success"), + TotalCount: github.Ptr(3), + Statuses: []*github.RepoStatus{ + { + State: github.Ptr("success"), + Context: github.Ptr("continuous-integration/travis-ci"), + Description: github.Ptr("Build succeeded"), + TargetURL: github.Ptr("https://travis-ci.org/owner/repo/builds/123"), + }, + { + State: github.Ptr("success"), + Context: github.Ptr("codecov/patch"), + Description: github.Ptr("Coverage increased"), + TargetURL: github.Ptr("https://codecov.io/gh/owner/repo/pull/42"), + }, + { + State: github.Ptr("success"), + Context: github.Ptr("lint/golangci-lint"), + Description: github.Ptr("No issues found"), + TargetURL: github.Ptr("https://golangci.com/r/owner/repo/pull/42"), + }, + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedStatus *github.CombinedStatus + expectedErrMsg string + }{ + { + name: "successful status fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposPullsByOwnerByRepoByPullNumber, + mockPR, + ), + mock.WithRequestMatch( + mock.GetReposCommitsStatusByOwnerByRepoByRef, + mockStatus, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + }, + expectError: false, + expectedStatus: mockStatus, + }, + { + name: "PR fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposPullsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(999), + }, + expectError: true, + expectedErrMsg: "failed to get pull request", + }, + { + name: "status fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposPullsByOwnerByRepoByPullNumber, + mockPR, + ), + mock.WithRequestMatchHandler( + mock.GetReposCommitsStatusesByOwnerByRepoByRef, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + }, + expectError: true, + expectedErrMsg: "failed to get combined status", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetPullRequestStatus(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedStatus github.CombinedStatus + err = json.Unmarshal([]byte(textContent.Text), &returnedStatus) + require.NoError(t, err) + assert.Equal(t, *tc.expectedStatus.State, *returnedStatus.State) + assert.Equal(t, *tc.expectedStatus.TotalCount, *returnedStatus.TotalCount) + assert.Len(t, returnedStatus.Statuses, len(tc.expectedStatus.Statuses)) + for i, status := range returnedStatus.Statuses { + assert.Equal(t, *tc.expectedStatus.Statuses[i].State, *status.State) + assert.Equal(t, *tc.expectedStatus.Statuses[i].Context, *status.Context) + assert.Equal(t, *tc.expectedStatus.Statuses[i].Description, *status.Description) + } + }) + } +} + +func Test_UpdatePullRequestBranch(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := UpdatePullRequestBranch(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "update_pull_request_branch", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "pullNumber") + assert.Contains(t, tool.InputSchema.Properties, "expectedHeadSha") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "pullNumber"}) + + // Setup mock update result for success case + mockUpdateResult := &github.PullRequestBranchUpdateResponse{ + Message: github.Ptr("Branch was updated successfully"), + URL: github.Ptr("https://api.github.com/repos/owner/repo/pulls/42"), + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedUpdateResult *github.PullRequestBranchUpdateResponse + expectedErrMsg string + }{ + { + name: "successful branch update", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PutReposPullsUpdateBranchByOwnerByRepoByPullNumber, + expectRequestBody(t, map[string]interface{}{ + "expected_head_sha": "abcd1234", + }).andThen( + mockResponse(t, http.StatusAccepted, mockUpdateResult), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "expectedHeadSha": "abcd1234", + }, + expectError: false, + expectedUpdateResult: mockUpdateResult, + }, + { + name: "branch update without expected SHA", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PutReposPullsUpdateBranchByOwnerByRepoByPullNumber, + expectRequestBody(t, map[string]interface{}{}).andThen( + mockResponse(t, http.StatusAccepted, mockUpdateResult), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + }, + expectError: false, + expectedUpdateResult: mockUpdateResult, + }, + { + name: "branch update fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PutReposPullsUpdateBranchByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusConflict) + _, _ = w.Write([]byte(`{"message": "Merge conflict"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + }, + expectError: true, + expectedErrMsg: "failed to update pull request branch", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := UpdatePullRequestBranch(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + assert.Contains(t, textContent.Text, "is in progress") + }) + } +} + +func Test_GetPullRequestComments(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetPullRequestComments(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_pull_request_comments", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "pullNumber") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "pullNumber"}) + + // Setup mock PR comments for success case + mockComments := []*github.PullRequestComment{ + { + ID: github.Ptr(int64(101)), + Body: github.Ptr("This looks good"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42#discussion_r101"), + User: &github.User{ + Login: github.Ptr("reviewer1"), + }, + Path: github.Ptr("file1.go"), + Position: github.Ptr(5), + CommitID: github.Ptr("abcdef123456"), + CreatedAt: &github.Timestamp{Time: time.Now().Add(-24 * time.Hour)}, + UpdatedAt: &github.Timestamp{Time: time.Now().Add(-24 * time.Hour)}, + }, + { + ID: github.Ptr(int64(102)), + Body: github.Ptr("Please fix this"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42#discussion_r102"), + User: &github.User{ + Login: github.Ptr("reviewer2"), + }, + Path: github.Ptr("file2.go"), + Position: github.Ptr(10), + CommitID: github.Ptr("abcdef123456"), + CreatedAt: &github.Timestamp{Time: time.Now().Add(-12 * time.Hour)}, + UpdatedAt: &github.Timestamp{Time: time.Now().Add(-12 * time.Hour)}, + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedComments []*github.PullRequestComment + expectedErrMsg string + }{ + { + name: "successful comments fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposPullsCommentsByOwnerByRepoByPullNumber, + mockComments, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + }, + expectError: false, + expectedComments: mockComments, + }, + { + name: "comments fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposPullsCommentsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(999), + }, + expectError: true, + expectedErrMsg: "failed to get pull request comments", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetPullRequestComments(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedComments []*github.PullRequestComment + err = json.Unmarshal([]byte(textContent.Text), &returnedComments) + require.NoError(t, err) + assert.Len(t, returnedComments, len(tc.expectedComments)) + for i, comment := range returnedComments { + assert.Equal(t, *tc.expectedComments[i].ID, *comment.ID) + assert.Equal(t, *tc.expectedComments[i].Body, *comment.Body) + assert.Equal(t, *tc.expectedComments[i].User.Login, *comment.User.Login) + assert.Equal(t, *tc.expectedComments[i].Path, *comment.Path) + assert.Equal(t, *tc.expectedComments[i].HTMLURL, *comment.HTMLURL) + } + }) + } +} + +func Test_GetPullRequestReviews(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetPullRequestReviews(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_pull_request_reviews", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "pullNumber") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "pullNumber"}) + + // Setup mock PR reviews for success case + mockReviews := []*github.PullRequestReview{ + { + ID: github.Ptr(int64(201)), + State: github.Ptr("APPROVED"), + Body: github.Ptr("LGTM"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42#pullrequestreview-201"), + User: &github.User{ + Login: github.Ptr("approver"), + }, + CommitID: github.Ptr("abcdef123456"), + SubmittedAt: &github.Timestamp{Time: time.Now().Add(-24 * time.Hour)}, + }, + { + ID: github.Ptr(int64(202)), + State: github.Ptr("CHANGES_REQUESTED"), + Body: github.Ptr("Please address the following issues"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42#pullrequestreview-202"), + User: &github.User{ + Login: github.Ptr("reviewer"), + }, + CommitID: github.Ptr("abcdef123456"), + SubmittedAt: &github.Timestamp{Time: time.Now().Add(-12 * time.Hour)}, + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedReviews []*github.PullRequestReview + expectedErrMsg string + }{ + { + name: "successful reviews fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposPullsReviewsByOwnerByRepoByPullNumber, + mockReviews, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + }, + expectError: false, + expectedReviews: mockReviews, + }, + { + name: "reviews fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposPullsReviewsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(999), + }, + expectError: true, + expectedErrMsg: "failed to get pull request reviews", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetPullRequestReviews(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedReviews []*github.PullRequestReview + err = json.Unmarshal([]byte(textContent.Text), &returnedReviews) + require.NoError(t, err) + assert.Len(t, returnedReviews, len(tc.expectedReviews)) + for i, review := range returnedReviews { + assert.Equal(t, *tc.expectedReviews[i].ID, *review.ID) + assert.Equal(t, *tc.expectedReviews[i].State, *review.State) + assert.Equal(t, *tc.expectedReviews[i].Body, *review.Body) + assert.Equal(t, *tc.expectedReviews[i].User.Login, *review.User.Login) + assert.Equal(t, *tc.expectedReviews[i].HTMLURL, *review.HTMLURL) + } + }) + } +} + +func Test_CreatePullRequestReview(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := CreatePullRequestReview(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "create_pull_request_review", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "pullNumber") + assert.Contains(t, tool.InputSchema.Properties, "body") + assert.Contains(t, tool.InputSchema.Properties, "event") + assert.Contains(t, tool.InputSchema.Properties, "commitId") + assert.Contains(t, tool.InputSchema.Properties, "comments") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "pullNumber", "event"}) + + // Setup mock review for success case + mockReview := &github.PullRequestReview{ + ID: github.Ptr(int64(301)), + State: github.Ptr("APPROVED"), + Body: github.Ptr("Looks good!"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42#pullrequestreview-301"), + User: &github.User{ + Login: github.Ptr("reviewer"), + }, + CommitID: github.Ptr("abcdef123456"), + SubmittedAt: &github.Timestamp{Time: time.Now()}, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedReview *github.PullRequestReview + expectedErrMsg string + }{ + { + name: "successful review creation with body only", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsReviewsByOwnerByRepoByPullNumber, + expectRequestBody(t, map[string]interface{}{ + "body": "Looks good!", + "event": "APPROVE", + }).andThen( + mockResponse(t, http.StatusOK, mockReview), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "body": "Looks good!", + "event": "APPROVE", + }, + expectError: false, + expectedReview: mockReview, + }, + { + name: "successful review creation with commitId", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsReviewsByOwnerByRepoByPullNumber, + expectRequestBody(t, map[string]interface{}{ + "body": "Looks good!", + "event": "APPROVE", + "commit_id": "abcdef123456", + }).andThen( + mockResponse(t, http.StatusOK, mockReview), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "body": "Looks good!", + "event": "APPROVE", + "commitId": "abcdef123456", + }, + expectError: false, + expectedReview: mockReview, + }, + { + name: "successful review creation with comments", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsReviewsByOwnerByRepoByPullNumber, + expectRequestBody(t, map[string]interface{}{ + "body": "Some issues to fix", + "event": "REQUEST_CHANGES", + "comments": []interface{}{ + map[string]interface{}{ + "path": "file1.go", + "position": float64(10), + "body": "This needs to be fixed", + }, + map[string]interface{}{ + "path": "file2.go", + "position": float64(20), + "body": "Consider a different approach here", + }, + }, + }).andThen( + mockResponse(t, http.StatusOK, mockReview), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "body": "Some issues to fix", + "event": "REQUEST_CHANGES", + "comments": []interface{}{ + map[string]interface{}{ + "path": "file1.go", + "position": float64(10), + "body": "This needs to be fixed", + }, + map[string]interface{}{ + "path": "file2.go", + "position": float64(20), + "body": "Consider a different approach here", + }, + }, + }, + expectError: false, + expectedReview: mockReview, + }, + { + name: "invalid comment format", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsReviewsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"message": "Invalid comment format"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "event": "REQUEST_CHANGES", + "comments": []interface{}{ + map[string]interface{}{ + "path": "file1.go", + // missing position + "body": "This needs to be fixed", + }, + }, + }, + expectError: false, + expectedErrMsg: "each comment must have either position or line", + }, + { + name: "successful review creation with line parameter", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsReviewsByOwnerByRepoByPullNumber, + expectRequestBody(t, map[string]interface{}{ + "body": "Code review comments", + "event": "COMMENT", + "comments": []interface{}{ + map[string]interface{}{ + "path": "main.go", + "line": float64(42), + "body": "Consider adding a comment here", + }, + }, + }).andThen( + mockResponse(t, http.StatusOK, mockReview), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "body": "Code review comments", + "event": "COMMENT", + "comments": []interface{}{ + map[string]interface{}{ + "path": "main.go", + "line": float64(42), + "body": "Consider adding a comment here", + }, + }, + }, + expectError: false, + expectedReview: mockReview, + }, + { + name: "successful review creation with multi-line comment", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsReviewsByOwnerByRepoByPullNumber, + expectRequestBody(t, map[string]interface{}{ + "body": "Multi-line comment review", + "event": "COMMENT", + "comments": []interface{}{ + map[string]interface{}{ + "path": "main.go", + "start_line": float64(10), + "line": float64(15), + "side": "RIGHT", + "body": "This entire block needs refactoring", + }, + }, + }).andThen( + mockResponse(t, http.StatusOK, mockReview), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "body": "Multi-line comment review", + "event": "COMMENT", + "comments": []interface{}{ + map[string]interface{}{ + "path": "main.go", + "start_line": float64(10), + "line": float64(15), + "side": "RIGHT", + "body": "This entire block needs refactoring", + }, + }, + }, + expectError: false, + expectedReview: mockReview, + }, + { + name: "invalid multi-line comment - missing line parameter", + mockedClient: mock.NewMockedHTTPClient(), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "event": "COMMENT", + "comments": []interface{}{ + map[string]interface{}{ + "path": "main.go", + "start_line": float64(10), + // missing line parameter + "body": "Invalid multi-line comment", + }, + }, + }, + expectError: false, + expectedErrMsg: "each comment must have either position or line", // Updated error message + }, + { + name: "invalid comment - mixing position with line parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.PostReposPullsReviewsByOwnerByRepoByPullNumber, + mockReview, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "event": "COMMENT", + "comments": []interface{}{ + map[string]interface{}{ + "path": "main.go", + "position": float64(5), + "line": float64(42), + "body": "Invalid parameter combination", + }, + }, + }, + expectError: false, + expectedErrMsg: "position cannot be combined with line, side, start_line, or start_side", + }, + { + name: "invalid multi-line comment - missing side parameter", + mockedClient: mock.NewMockedHTTPClient(), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "event": "COMMENT", + "comments": []interface{}{ + map[string]interface{}{ + "path": "main.go", + "start_line": float64(10), + "line": float64(15), + "start_side": "LEFT", + // missing side parameter + "body": "Invalid multi-line comment", + }, + }, + }, + expectError: false, + expectedErrMsg: "if start_side is provided, side must also be provided", + }, + { + name: "review creation fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsReviewsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"message": "Invalid comment format"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pullNumber": float64(42), + "body": "Looks good!", + "event": "APPROVE", + }, + expectError: true, + expectedErrMsg: "failed to create pull request review", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := CreatePullRequestReview(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // For error messages in the result + if tc.expectedErrMsg != "" { + textContent := getTextResult(t, result) + assert.Contains(t, textContent.Text, tc.expectedErrMsg) + return + } + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedReview github.PullRequestReview + err = json.Unmarshal([]byte(textContent.Text), &returnedReview) + require.NoError(t, err) + assert.Equal(t, *tc.expectedReview.ID, *returnedReview.ID) + assert.Equal(t, *tc.expectedReview.State, *returnedReview.State) + assert.Equal(t, *tc.expectedReview.Body, *returnedReview.Body) + assert.Equal(t, *tc.expectedReview.User.Login, *returnedReview.User.Login) + assert.Equal(t, *tc.expectedReview.HTMLURL, *returnedReview.HTMLURL) + }) + } +} + +func Test_CreatePullRequest(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := CreatePullRequest(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "create_pull_request", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "title") + assert.Contains(t, tool.InputSchema.Properties, "body") + assert.Contains(t, tool.InputSchema.Properties, "head") + assert.Contains(t, tool.InputSchema.Properties, "base") + assert.Contains(t, tool.InputSchema.Properties, "draft") + assert.Contains(t, tool.InputSchema.Properties, "maintainer_can_modify") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "title", "head", "base"}) + + // Setup mock PR for success case + mockPR := &github.PullRequest{ + Number: github.Ptr(42), + Title: github.Ptr("Test PR"), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/repo/pull/42"), + Head: &github.PullRequestBranch{ + SHA: github.Ptr("abcd1234"), + Ref: github.Ptr("feature-branch"), + }, + Base: &github.PullRequestBranch{ + SHA: github.Ptr("efgh5678"), + Ref: github.Ptr("main"), + }, + Body: github.Ptr("This is a test PR"), + Draft: github.Ptr(false), + MaintainerCanModify: github.Ptr(true), + User: &github.User{ + Login: github.Ptr("testuser"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedPR *github.PullRequest + expectedErrMsg string + }{ + { + name: "successful PR creation", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsByOwnerByRepo, + expectRequestBody(t, map[string]interface{}{ + "title": "Test PR", + "body": "This is a test PR", + "head": "feature-branch", + "base": "main", + "draft": false, + "maintainer_can_modify": true, + }).andThen( + mockResponse(t, http.StatusCreated, mockPR), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "title": "Test PR", + "body": "This is a test PR", + "head": "feature-branch", + "base": "main", + "draft": false, + "maintainer_can_modify": true, + }, + expectError: false, + expectedPR: mockPR, + }, + { + name: "missing required parameter", + mockedClient: mock.NewMockedHTTPClient(), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + // missing title, head, base + }, + expectError: true, + expectedErrMsg: "missing required parameter: title", + }, + { + name: "PR creation fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"message":"Validation failed","errors":[{"resource":"PullRequest","code":"invalid"}]}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "title": "Test PR", + "head": "feature-branch", + "base": "main", + }, + expectError: true, + expectedErrMsg: "failed to create pull request", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := CreatePullRequest(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + if err != nil { + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + // If no error returned but in the result + textContent := getTextResult(t, result) + assert.Contains(t, textContent.Text, tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedPR github.PullRequest + err = json.Unmarshal([]byte(textContent.Text), &returnedPR) + require.NoError(t, err) + assert.Equal(t, *tc.expectedPR.Number, *returnedPR.Number) + assert.Equal(t, *tc.expectedPR.Title, *returnedPR.Title) + assert.Equal(t, *tc.expectedPR.State, *returnedPR.State) + assert.Equal(t, *tc.expectedPR.HTMLURL, *returnedPR.HTMLURL) + assert.Equal(t, *tc.expectedPR.Head.SHA, *returnedPR.Head.SHA) + assert.Equal(t, *tc.expectedPR.Base.Ref, *returnedPR.Base.Ref) + assert.Equal(t, *tc.expectedPR.Body, *returnedPR.Body) + assert.Equal(t, *tc.expectedPR.User.Login, *returnedPR.User.Login) + }) + } +} + +func Test_AddPullRequestReviewComment(t *testing.T) { + mockClient := github.NewClient(nil) + tool, _ := AddPullRequestReviewComment(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "add_pull_request_review_comment", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "pull_number") + assert.Contains(t, tool.InputSchema.Properties, "body") + assert.Contains(t, tool.InputSchema.Properties, "commit_id") + assert.Contains(t, tool.InputSchema.Properties, "path") + // Since we've updated commit_id and path to be optional when using in_reply_to + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "pull_number", "body"}) + + mockComment := &github.PullRequestComment{ + ID: github.Ptr(int64(123)), + Body: github.Ptr("Great stuff!"), + Path: github.Ptr("file1.txt"), + Line: github.Ptr(2), + Side: github.Ptr("RIGHT"), + } + + mockReply := &github.PullRequestComment{ + ID: github.Ptr(int64(456)), + Body: github.Ptr("Good point, will fix!"), + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedComment *github.PullRequestComment + expectedErrMsg string + }{ + { + name: "successful line comment creation", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsCommentsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusCreated) + err := json.NewEncoder(w).Encode(mockComment) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pull_number": float64(1), + "body": "Great stuff!", + "commit_id": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "path": "file1.txt", + "line": float64(2), + "side": "RIGHT", + }, + expectError: false, + expectedComment: mockComment, + }, + { + name: "successful reply using in_reply_to", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsCommentsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusCreated) + err := json.NewEncoder(w).Encode(mockReply) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pull_number": float64(1), + "body": "Good point, will fix!", + "in_reply_to": float64(123), + }, + expectError: false, + expectedComment: mockReply, + }, + { + name: "comment creation fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsCommentsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"message": "Validation Failed"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pull_number": float64(1), + "body": "Great stuff!", + "commit_id": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "path": "file1.txt", + "line": float64(2), + }, + expectError: true, + expectedErrMsg: "failed to create pull request comment", + }, + { + name: "reply creation fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposPullsCommentsByOwnerByRepoByPullNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"message": "Comment not found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pull_number": float64(1), + "body": "Good point, will fix!", + "in_reply_to": float64(999), + }, + expectError: true, + expectedErrMsg: "failed to reply to pull request comment", + }, + { + name: "missing required parameters for comment", + mockedClient: mock.NewMockedHTTPClient(), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "pull_number": float64(1), + "body": "Great stuff!", + // missing commit_id and path + }, + expectError: false, + expectedErrMsg: "missing required parameter: commit_id", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + mockClient := github.NewClient(tc.mockedClient) + + _, handler := AddPullRequestReviewComment(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + request := createMCPRequest(tc.requestArgs) + + result, err := handler(context.Background(), request) + + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + assert.NotNil(t, result) + require.Len(t, result.Content, 1) + + textContent := getTextResult(t, result) + if tc.expectedErrMsg != "" { + assert.Contains(t, textContent.Text, tc.expectedErrMsg) + return + } + + var returnedComment github.PullRequestComment + err = json.Unmarshal([]byte(getTextResult(t, result).Text), &returnedComment) + require.NoError(t, err) + + assert.Equal(t, *tc.expectedComment.ID, *returnedComment.ID) + assert.Equal(t, *tc.expectedComment.Body, *returnedComment.Body) + + // Only check Path, Line, and Side if they exist in the expected comment + if tc.expectedComment.Path != nil { + assert.Equal(t, *tc.expectedComment.Path, *returnedComment.Path) + } + if tc.expectedComment.Line != nil { + assert.Equal(t, *tc.expectedComment.Line, *returnedComment.Line) + } + if tc.expectedComment.Side != nil { + assert.Equal(t, *tc.expectedComment.Side, *returnedComment.Side) + } + }) + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/repositories.go b/sre_agent/servers/github-mcp-server/pkg/github/repositories.go new file mode 100644 index 0000000..beaab7c --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/repositories.go @@ -0,0 +1,942 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +func GetCommit(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_commit", + mcp.WithDescription(t("TOOL_GET_COMMITS_DESCRIPTION", "Get details for a commit from a GitHub repository")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_COMMITS_USER_TITLE", "Get commit details"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("sha", + mcp.Required(), + mcp.Description("Commit SHA, branch name, or tag name"), + ), + WithPagination(), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + sha, err := requiredParam[string](request, "sha") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pagination, err := OptionalPaginationParams(request) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.ListOptions{ + Page: pagination.page, + PerPage: pagination.perPage, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + commit, resp, err := client.Repositories.GetCommit(ctx, owner, repo, sha, opts) + if err != nil { + return nil, fmt.Errorf("failed to get commit: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != 200 { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get commit: %s", string(body))), nil + } + + r, err := json.Marshal(commit) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// ListCommits creates a tool to get commits of a branch in a repository. +func ListCommits(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("list_commits", + mcp.WithDescription(t("TOOL_LIST_COMMITS_DESCRIPTION", "Get list of commits of a branch in a GitHub repository")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_LIST_COMMITS_USER_TITLE", "List commits"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("sha", + mcp.Description("SHA or Branch name"), + ), + WithPagination(), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + sha, err := OptionalParam[string](request, "sha") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pagination, err := OptionalPaginationParams(request) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.CommitsListOptions{ + SHA: sha, + ListOptions: github.ListOptions{ + Page: pagination.page, + PerPage: pagination.perPage, + }, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + commits, resp, err := client.Repositories.ListCommits(ctx, owner, repo, opts) + if err != nil { + return nil, fmt.Errorf("failed to list commits: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != 200 { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to list commits: %s", string(body))), nil + } + + r, err := json.Marshal(commits) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// ListBranches creates a tool to list branches in a GitHub repository. +func ListBranches(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("list_branches", + mcp.WithDescription(t("TOOL_LIST_BRANCHES_DESCRIPTION", "List branches in a GitHub repository")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_LIST_BRANCHES_USER_TITLE", "List branches"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + WithPagination(), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pagination, err := OptionalPaginationParams(request) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.BranchListOptions{ + ListOptions: github.ListOptions{ + Page: pagination.page, + PerPage: pagination.perPage, + }, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + + branches, resp, err := client.Repositories.ListBranches(ctx, owner, repo, opts) + if err != nil { + return nil, fmt.Errorf("failed to list branches: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to list branches: %s", string(body))), nil + } + + r, err := json.Marshal(branches) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// CreateOrUpdateFile creates a tool to create or update a file in a GitHub repository. +func CreateOrUpdateFile(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("create_or_update_file", + mcp.WithDescription(t("TOOL_CREATE_OR_UPDATE_FILE_DESCRIPTION", "Create or update a single file in a GitHub repository. If updating, you must provide the SHA of the file you want to update.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_CREATE_OR_UPDATE_FILE_USER_TITLE", "Create or update file"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner (username or organization)"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("path", + mcp.Required(), + mcp.Description("Path where to create/update the file"), + ), + mcp.WithString("content", + mcp.Required(), + mcp.Description("Content of the file"), + ), + mcp.WithString("message", + mcp.Required(), + mcp.Description("Commit message"), + ), + mcp.WithString("branch", + mcp.Required(), + mcp.Description("Branch to create/update the file in"), + ), + mcp.WithString("sha", + mcp.Description("SHA of file being replaced (for updates)"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + path, err := requiredParam[string](request, "path") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + content, err := requiredParam[string](request, "content") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + message, err := requiredParam[string](request, "message") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + branch, err := requiredParam[string](request, "branch") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + // Convert content to base64 + contentBytes := []byte(content) + + // Create the file options + opts := &github.RepositoryContentFileOptions{ + Message: github.Ptr(message), + Content: contentBytes, + Branch: github.Ptr(branch), + } + + // If SHA is provided, set it (for updates) + sha, err := OptionalParam[string](request, "sha") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + if sha != "" { + opts.SHA = github.Ptr(sha) + } + + // Create or update the file + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + fileContent, resp, err := client.Repositories.CreateFile(ctx, owner, repo, path, opts) + if err != nil { + return nil, fmt.Errorf("failed to create/update file: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != 200 && resp.StatusCode != 201 { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to create/update file: %s", string(body))), nil + } + + r, err := json.Marshal(fileContent) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// CreateRepository creates a tool to create a new GitHub repository. +func CreateRepository(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("create_repository", + mcp.WithDescription(t("TOOL_CREATE_REPOSITORY_DESCRIPTION", "Create a new GitHub repository in your account")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_CREATE_REPOSITORY_USER_TITLE", "Create repository"), + ReadOnlyHint: false, + }), + mcp.WithString("name", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("description", + mcp.Description("Repository description"), + ), + mcp.WithBoolean("private", + mcp.Description("Whether repo should be private"), + ), + mcp.WithBoolean("autoInit", + mcp.Description("Initialize with README"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + name, err := requiredParam[string](request, "name") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + description, err := OptionalParam[string](request, "description") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + private, err := OptionalParam[bool](request, "private") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + autoInit, err := OptionalParam[bool](request, "autoInit") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + repo := &github.Repository{ + Name: github.Ptr(name), + Description: github.Ptr(description), + Private: github.Ptr(private), + AutoInit: github.Ptr(autoInit), + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + createdRepo, resp, err := client.Repositories.Create(ctx, "", repo) + if err != nil { + return nil, fmt.Errorf("failed to create repository: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusCreated { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to create repository: %s", string(body))), nil + } + + r, err := json.Marshal(createdRepo) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// GetFileContents creates a tool to get the contents of a file or directory from a GitHub repository. +func GetFileContents(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_file_contents", + mcp.WithDescription(t("TOOL_GET_FILE_CONTENTS_DESCRIPTION", "Get the contents of a file or directory from a GitHub repository")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_FILE_CONTENTS_USER_TITLE", "Get file or directory contents"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner (username or organization)"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("path", + mcp.Required(), + mcp.Description("Path to file/directory"), + ), + mcp.WithString("branch", + mcp.Description("Branch to get contents from"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + path, err := requiredParam[string](request, "path") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + branch, err := OptionalParam[string](request, "branch") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + opts := &github.RepositoryContentGetOptions{Ref: branch} + fileContent, dirContent, resp, err := client.Repositories.GetContents(ctx, owner, repo, path, opts) + if err != nil { + return nil, fmt.Errorf("failed to get file contents: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != 200 { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get file contents: %s", string(body))), nil + } + + var result interface{} + if fileContent != nil { + result = fileContent + } else { + result = dirContent + } + + r, err := json.Marshal(result) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// ForkRepository creates a tool to fork a repository. +func ForkRepository(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("fork_repository", + mcp.WithDescription(t("TOOL_FORK_REPOSITORY_DESCRIPTION", "Fork a GitHub repository to your account or specified organization")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_FORK_REPOSITORY_USER_TITLE", "Fork repository"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("organization", + mcp.Description("Organization to fork to"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + org, err := OptionalParam[string](request, "organization") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.RepositoryCreateForkOptions{} + if org != "" { + opts.Organization = org + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + forkedRepo, resp, err := client.Repositories.CreateFork(ctx, owner, repo, opts) + if err != nil { + // Check if it's an acceptedError. An acceptedError indicates that the update is in progress, + // and it's not a real error. + if resp != nil && resp.StatusCode == http.StatusAccepted && isAcceptedError(err) { + return mcp.NewToolResultText("Fork is in progress"), nil + } + return nil, fmt.Errorf("failed to fork repository: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusAccepted { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to fork repository: %s", string(body))), nil + } + + r, err := json.Marshal(forkedRepo) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// CreateBranch creates a tool to create a new branch. +func CreateBranch(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("create_branch", + mcp.WithDescription(t("TOOL_CREATE_BRANCH_DESCRIPTION", "Create a new branch in a GitHub repository")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_CREATE_BRANCH_USER_TITLE", "Create branch"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("branch", + mcp.Required(), + mcp.Description("Name for new branch"), + ), + mcp.WithString("from_branch", + mcp.Description("Source branch (defaults to repo default)"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + branch, err := requiredParam[string](request, "branch") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + fromBranch, err := OptionalParam[string](request, "from_branch") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + + // Get the source branch SHA + var ref *github.Reference + + if fromBranch == "" { + // Get default branch if from_branch not specified + repository, resp, err := client.Repositories.Get(ctx, owner, repo) + if err != nil { + return nil, fmt.Errorf("failed to get repository: %w", err) + } + defer resp.Body.Close() + + fromBranch = *repository.DefaultBranch + } + + // Get SHA of source branch + ref, resp, err := client.Git.GetRef(ctx, owner, repo, "refs/heads/"+fromBranch) + if err != nil { + return nil, fmt.Errorf("failed to get reference: %w", err) + } + defer resp.Body.Close() + + // Create new branch + newRef := &github.Reference{ + Ref: github.Ptr("refs/heads/" + branch), + Object: &github.GitObject{SHA: ref.Object.SHA}, + } + + createdRef, resp, err := client.Git.CreateRef(ctx, owner, repo, newRef) + if err != nil { + return nil, fmt.Errorf("failed to create branch: %w", err) + } + defer resp.Body.Close() + + r, err := json.Marshal(createdRef) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// PushFiles creates a tool to push multiple files in a single commit to a GitHub repository. +func PushFiles(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("push_files", + mcp.WithDescription(t("TOOL_PUSH_FILES_DESCRIPTION", "Push multiple files to a GitHub repository in a single commit")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_PUSH_FILES_USER_TITLE", "Push files to repository"), + ReadOnlyHint: false, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("branch", + mcp.Required(), + mcp.Description("Branch to push to"), + ), + mcp.WithArray("files", + mcp.Required(), + mcp.Items( + map[string]interface{}{ + "type": "object", + "additionalProperties": false, + "required": []string{"path", "content"}, + "properties": map[string]interface{}{ + "path": map[string]interface{}{ + "type": "string", + "description": "path to the file", + }, + "content": map[string]interface{}{ + "type": "string", + "description": "file content", + }, + }, + }), + mcp.Description("Array of file objects to push, each object with path (string) and content (string)"), + ), + mcp.WithString("message", + mcp.Required(), + mcp.Description("Commit message"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + branch, err := requiredParam[string](request, "branch") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + message, err := requiredParam[string](request, "message") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + // Parse files parameter - this should be an array of objects with path and content + filesObj, ok := request.Params.Arguments["files"].([]interface{}) + if !ok { + return mcp.NewToolResultError("files parameter must be an array of objects with path and content"), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + + // Get the reference for the branch + ref, resp, err := client.Git.GetRef(ctx, owner, repo, "refs/heads/"+branch) + if err != nil { + return nil, fmt.Errorf("failed to get branch reference: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + // Get the commit object that the branch points to + baseCommit, resp, err := client.Git.GetCommit(ctx, owner, repo, *ref.Object.SHA) + if err != nil { + return nil, fmt.Errorf("failed to get base commit: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + // Create tree entries for all files + var entries []*github.TreeEntry + + for _, file := range filesObj { + fileMap, ok := file.(map[string]interface{}) + if !ok { + return mcp.NewToolResultError("each file must be an object with path and content"), nil + } + + path, ok := fileMap["path"].(string) + if !ok || path == "" { + return mcp.NewToolResultError("each file must have a path"), nil + } + + content, ok := fileMap["content"].(string) + if !ok { + return mcp.NewToolResultError("each file must have content"), nil + } + + // Create a tree entry for the file + entries = append(entries, &github.TreeEntry{ + Path: github.Ptr(path), + Mode: github.Ptr("100644"), // Regular file mode + Type: github.Ptr("blob"), + Content: github.Ptr(content), + }) + } + + // Create a new tree with the file entries + newTree, resp, err := client.Git.CreateTree(ctx, owner, repo, *baseCommit.Tree.SHA, entries) + if err != nil { + return nil, fmt.Errorf("failed to create tree: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + // Create a new commit + commit := &github.Commit{ + Message: github.Ptr(message), + Tree: newTree, + Parents: []*github.Commit{{SHA: baseCommit.SHA}}, + } + newCommit, resp, err := client.Git.CreateCommit(ctx, owner, repo, commit, nil) + if err != nil { + return nil, fmt.Errorf("failed to create commit: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + // Update the reference to point to the new commit + ref.Object.SHA = newCommit.SHA + updatedRef, resp, err := client.Git.UpdateRef(ctx, owner, repo, ref, false) + if err != nil { + return nil, fmt.Errorf("failed to update reference: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + r, err := json.Marshal(updatedRef) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// ListTags creates a tool to list tags in a GitHub repository. +func ListTags(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("list_tags", + mcp.WithDescription(t("TOOL_LIST_TAGS_DESCRIPTION", "List git tags in a GitHub repository")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_LIST_TAGS_USER_TITLE", "List tags"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + WithPagination(), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pagination, err := OptionalPaginationParams(request) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.ListOptions{ + Page: pagination.page, + PerPage: pagination.perPage, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + + tags, resp, err := client.Repositories.ListTags(ctx, owner, repo, opts) + if err != nil { + return nil, fmt.Errorf("failed to list tags: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to list tags: %s", string(body))), nil + } + + r, err := json.Marshal(tags) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// GetTag creates a tool to get details about a specific tag in a GitHub repository. +func GetTag(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("get_tag", + mcp.WithDescription(t("TOOL_GET_TAG_DESCRIPTION", "Get details about a specific git tag in a GitHub repository")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_TAG_USER_TITLE", "Get tag details"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("Repository owner"), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("Repository name"), + ), + mcp.WithString("tag", + mcp.Required(), + mcp.Description("Tag name"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + tag, err := requiredParam[string](request, "tag") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + + // First get the tag reference + ref, resp, err := client.Git.GetRef(ctx, owner, repo, "refs/tags/"+tag) + if err != nil { + return nil, fmt.Errorf("failed to get tag reference: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get tag reference: %s", string(body))), nil + } + + // Then get the tag object + tagObj, resp, err := client.Git.GetTag(ctx, owner, repo, *ref.Object.SHA) + if err != nil { + return nil, fmt.Errorf("failed to get tag object: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get tag object: %s", string(body))), nil + } + + r, err := json.Marshal(tagObj) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/repositories_test.go b/sre_agent/servers/github-mcp-server/pkg/github/repositories_test.go new file mode 100644 index 0000000..59d19fc --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/repositories_test.go @@ -0,0 +1,1799 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "testing" + "time" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/migueleliasweb/go-github-mock/src/mock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_GetFileContents(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetFileContents(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_file_contents", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "path") + assert.Contains(t, tool.InputSchema.Properties, "branch") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "path"}) + + // Setup mock file content for success case + mockFileContent := &github.RepositoryContent{ + Type: github.Ptr("file"), + Name: github.Ptr("README.md"), + Path: github.Ptr("README.md"), + Content: github.Ptr("IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku"), // Base64 encoded "# Test Repository\n\nThis is a test repository." + SHA: github.Ptr("abc123"), + Size: github.Ptr(42), + HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/README.md"), + DownloadURL: github.Ptr("https://raw.githubusercontent.com/owner/repo/main/README.md"), + } + + // Setup mock directory content for success case + mockDirContent := []*github.RepositoryContent{ + { + Type: github.Ptr("file"), + Name: github.Ptr("README.md"), + Path: github.Ptr("README.md"), + SHA: github.Ptr("abc123"), + Size: github.Ptr(42), + HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/README.md"), + }, + { + Type: github.Ptr("dir"), + Name: github.Ptr("src"), + Path: github.Ptr("src"), + SHA: github.Ptr("def456"), + HTMLURL: github.Ptr("https://github.com/owner/repo/tree/main/src"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedResult interface{} + expectedErrMsg string + }{ + { + name: "successful file content fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposContentsByOwnerByRepoByPath, + expectQueryParams(t, map[string]string{ + "ref": "main", + }).andThen( + mockResponse(t, http.StatusOK, mockFileContent), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "path": "README.md", + "branch": "main", + }, + expectError: false, + expectedResult: mockFileContent, + }, + { + name: "successful directory content fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposContentsByOwnerByRepoByPath, + expectQueryParams(t, map[string]string{}).andThen( + mockResponse(t, http.StatusOK, mockDirContent), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "path": "src", + }, + expectError: false, + expectedResult: mockDirContent, + }, + { + name: "content fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposContentsByOwnerByRepoByPath, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "path": "nonexistent.md", + "branch": "main", + }, + expectError: true, + expectedErrMsg: "failed to get file contents", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetFileContents(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := mcp.CallToolRequest{ + Params: struct { + Name string `json:"name"` + Arguments map[string]interface{} `json:"arguments,omitempty"` + Meta *struct { + ProgressToken mcp.ProgressToken `json:"progressToken,omitempty"` + } `json:"_meta,omitempty"` + }{ + Arguments: tc.requestArgs, + }, + } + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Verify based on expected type + switch expected := tc.expectedResult.(type) { + case *github.RepositoryContent: + var returnedContent github.RepositoryContent + err = json.Unmarshal([]byte(textContent.Text), &returnedContent) + require.NoError(t, err) + assert.Equal(t, *expected.Name, *returnedContent.Name) + assert.Equal(t, *expected.Path, *returnedContent.Path) + assert.Equal(t, *expected.Type, *returnedContent.Type) + case []*github.RepositoryContent: + var returnedContents []*github.RepositoryContent + err = json.Unmarshal([]byte(textContent.Text), &returnedContents) + require.NoError(t, err) + assert.Len(t, returnedContents, len(expected)) + for i, content := range returnedContents { + assert.Equal(t, *expected[i].Name, *content.Name) + assert.Equal(t, *expected[i].Path, *content.Path) + assert.Equal(t, *expected[i].Type, *content.Type) + } + } + }) + } +} + +func Test_ForkRepository(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := ForkRepository(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "fork_repository", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "organization") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"}) + + // Setup mock forked repo for success case + mockForkedRepo := &github.Repository{ + ID: github.Ptr(int64(123456)), + Name: github.Ptr("repo"), + FullName: github.Ptr("new-owner/repo"), + Owner: &github.User{ + Login: github.Ptr("new-owner"), + }, + HTMLURL: github.Ptr("https://github.com/new-owner/repo"), + DefaultBranch: github.Ptr("main"), + Fork: github.Ptr(true), + ForksCount: github.Ptr(0), + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedRepo *github.Repository + expectedErrMsg string + }{ + { + name: "successful repository fork", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposForksByOwnerByRepo, + mockResponse(t, http.StatusAccepted, mockForkedRepo), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + }, + expectError: false, + expectedRepo: mockForkedRepo, + }, + { + name: "repository fork fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PostReposForksByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusForbidden) + _, _ = w.Write([]byte(`{"message": "Forbidden"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + }, + expectError: true, + expectedErrMsg: "failed to fork repository", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := ForkRepository(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + assert.Contains(t, textContent.Text, "Fork is in progress") + }) + } +} + +func Test_CreateBranch(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := CreateBranch(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "create_branch", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "branch") + assert.Contains(t, tool.InputSchema.Properties, "from_branch") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "branch"}) + + // Setup mock repository for default branch test + mockRepo := &github.Repository{ + DefaultBranch: github.Ptr("main"), + } + + // Setup mock reference for from_branch tests + mockSourceRef := &github.Reference{ + Ref: github.Ptr("refs/heads/main"), + Object: &github.GitObject{ + SHA: github.Ptr("abc123def456"), + }, + } + + // Setup mock created reference + mockCreatedRef := &github.Reference{ + Ref: github.Ptr("refs/heads/new-feature"), + Object: &github.GitObject{ + SHA: github.Ptr("abc123def456"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedRef *github.Reference + expectedErrMsg string + }{ + { + name: "successful branch creation with from_branch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposGitRefByOwnerByRepoByRef, + mockSourceRef, + ), + mock.WithRequestMatch( + mock.PostReposGitRefsByOwnerByRepo, + mockCreatedRef, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "new-feature", + "from_branch": "main", + }, + expectError: false, + expectedRef: mockCreatedRef, + }, + { + name: "successful branch creation with default branch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposByOwnerByRepo, + mockRepo, + ), + mock.WithRequestMatch( + mock.GetReposGitRefByOwnerByRepoByRef, + mockSourceRef, + ), + mock.WithRequestMatchHandler( + mock.PostReposGitRefsByOwnerByRepo, + expectRequestBody(t, map[string]interface{}{ + "ref": "refs/heads/new-feature", + "sha": "abc123def456", + }).andThen( + mockResponse(t, http.StatusCreated, mockCreatedRef), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "new-feature", + }, + expectError: false, + expectedRef: mockCreatedRef, + }, + { + name: "fail to get repository", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Repository not found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "nonexistent-repo", + "branch": "new-feature", + }, + expectError: true, + expectedErrMsg: "failed to get repository", + }, + { + name: "fail to get reference", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposGitRefByOwnerByRepoByRef, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Reference not found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "new-feature", + "from_branch": "nonexistent-branch", + }, + expectError: true, + expectedErrMsg: "failed to get reference", + }, + { + name: "fail to create branch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposGitRefByOwnerByRepoByRef, + mockSourceRef, + ), + mock.WithRequestMatchHandler( + mock.PostReposGitRefsByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"message": "Reference already exists"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "existing-branch", + "from_branch": "main", + }, + expectError: true, + expectedErrMsg: "failed to create branch", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := CreateBranch(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedRef github.Reference + err = json.Unmarshal([]byte(textContent.Text), &returnedRef) + require.NoError(t, err) + assert.Equal(t, *tc.expectedRef.Ref, *returnedRef.Ref) + assert.Equal(t, *tc.expectedRef.Object.SHA, *returnedRef.Object.SHA) + }) + } +} + +func Test_GetCommit(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetCommit(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_commit", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "sha") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "sha"}) + + mockCommit := &github.RepositoryCommit{ + SHA: github.Ptr("abc123def456"), + Commit: &github.Commit{ + Message: github.Ptr("First commit"), + Author: &github.CommitAuthor{ + Name: github.Ptr("Test User"), + Email: github.Ptr("test@example.com"), + Date: &github.Timestamp{Time: time.Now().Add(-48 * time.Hour)}, + }, + }, + Author: &github.User{ + Login: github.Ptr("testuser"), + }, + HTMLURL: github.Ptr("https://github.com/owner/repo/commit/abc123def456"), + Stats: &github.CommitStats{ + Additions: github.Ptr(10), + Deletions: github.Ptr(2), + Total: github.Ptr(12), + }, + Files: []*github.CommitFile{ + { + Filename: github.Ptr("file1.go"), + Status: github.Ptr("modified"), + Additions: github.Ptr(10), + Deletions: github.Ptr(2), + Changes: github.Ptr(12), + Patch: github.Ptr("@@ -1,2 +1,10 @@"), + }, + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedCommit *github.RepositoryCommit + expectedErrMsg string + }{ + { + name: "successful commit fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposCommitsByOwnerByRepoByRef, + mockResponse(t, http.StatusOK, mockCommit), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "sha": "abc123def456", + }, + expectError: false, + expectedCommit: mockCommit, + }, + { + name: "commit fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposCommitsByOwnerByRepoByRef, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "sha": "nonexistent-sha", + }, + expectError: true, + expectedErrMsg: "failed to get commit", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetCommit(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedCommit github.RepositoryCommit + err = json.Unmarshal([]byte(textContent.Text), &returnedCommit) + require.NoError(t, err) + + assert.Equal(t, *tc.expectedCommit.SHA, *returnedCommit.SHA) + assert.Equal(t, *tc.expectedCommit.Commit.Message, *returnedCommit.Commit.Message) + assert.Equal(t, *tc.expectedCommit.Author.Login, *returnedCommit.Author.Login) + assert.Equal(t, *tc.expectedCommit.HTMLURL, *returnedCommit.HTMLURL) + }) + } +} + +func Test_ListCommits(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := ListCommits(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "list_commits", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "sha") + assert.Contains(t, tool.InputSchema.Properties, "page") + assert.Contains(t, tool.InputSchema.Properties, "perPage") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"}) + + // Setup mock commits for success case + mockCommits := []*github.RepositoryCommit{ + { + SHA: github.Ptr("abc123def456"), + Commit: &github.Commit{ + Message: github.Ptr("First commit"), + Author: &github.CommitAuthor{ + Name: github.Ptr("Test User"), + Email: github.Ptr("test@example.com"), + Date: &github.Timestamp{Time: time.Now().Add(-48 * time.Hour)}, + }, + }, + Author: &github.User{ + Login: github.Ptr("testuser"), + }, + HTMLURL: github.Ptr("https://github.com/owner/repo/commit/abc123def456"), + }, + { + SHA: github.Ptr("def456abc789"), + Commit: &github.Commit{ + Message: github.Ptr("Second commit"), + Author: &github.CommitAuthor{ + Name: github.Ptr("Another User"), + Email: github.Ptr("another@example.com"), + Date: &github.Timestamp{Time: time.Now().Add(-24 * time.Hour)}, + }, + }, + Author: &github.User{ + Login: github.Ptr("anotheruser"), + }, + HTMLURL: github.Ptr("https://github.com/owner/repo/commit/def456abc789"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedCommits []*github.RepositoryCommit + expectedErrMsg string + }{ + { + name: "successful commits fetch with default params", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposCommitsByOwnerByRepo, + mockCommits, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + }, + expectError: false, + expectedCommits: mockCommits, + }, + { + name: "successful commits fetch with branch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposCommitsByOwnerByRepo, + expectQueryParams(t, map[string]string{ + "sha": "main", + "page": "1", + "per_page": "30", + }).andThen( + mockResponse(t, http.StatusOK, mockCommits), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "sha": "main", + }, + expectError: false, + expectedCommits: mockCommits, + }, + { + name: "successful commits fetch with pagination", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposCommitsByOwnerByRepo, + expectQueryParams(t, map[string]string{ + "page": "2", + "per_page": "10", + }).andThen( + mockResponse(t, http.StatusOK, mockCommits), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "page": float64(2), + "perPage": float64(10), + }, + expectError: false, + expectedCommits: mockCommits, + }, + { + name: "commits fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposCommitsByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "nonexistent-repo", + }, + expectError: true, + expectedErrMsg: "failed to list commits", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := ListCommits(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedCommits []*github.RepositoryCommit + err = json.Unmarshal([]byte(textContent.Text), &returnedCommits) + require.NoError(t, err) + assert.Len(t, returnedCommits, len(tc.expectedCommits)) + for i, commit := range returnedCommits { + assert.Equal(t, *tc.expectedCommits[i].SHA, *commit.SHA) + assert.Equal(t, *tc.expectedCommits[i].Commit.Message, *commit.Commit.Message) + assert.Equal(t, *tc.expectedCommits[i].Author.Login, *commit.Author.Login) + assert.Equal(t, *tc.expectedCommits[i].HTMLURL, *commit.HTMLURL) + } + }) + } +} + +func Test_CreateOrUpdateFile(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := CreateOrUpdateFile(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "create_or_update_file", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "path") + assert.Contains(t, tool.InputSchema.Properties, "content") + assert.Contains(t, tool.InputSchema.Properties, "message") + assert.Contains(t, tool.InputSchema.Properties, "branch") + assert.Contains(t, tool.InputSchema.Properties, "sha") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "path", "content", "message", "branch"}) + + // Setup mock file content response + mockFileResponse := &github.RepositoryContentResponse{ + Content: &github.RepositoryContent{ + Name: github.Ptr("example.md"), + Path: github.Ptr("docs/example.md"), + SHA: github.Ptr("abc123def456"), + Size: github.Ptr(42), + HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/docs/example.md"), + DownloadURL: github.Ptr("https://raw.githubusercontent.com/owner/repo/main/docs/example.md"), + }, + Commit: github.Commit{ + SHA: github.Ptr("def456abc789"), + Message: github.Ptr("Add example file"), + Author: &github.CommitAuthor{ + Name: github.Ptr("Test User"), + Email: github.Ptr("test@example.com"), + Date: &github.Timestamp{Time: time.Now()}, + }, + HTMLURL: github.Ptr("https://github.com/owner/repo/commit/def456abc789"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedContent *github.RepositoryContentResponse + expectedErrMsg string + }{ + { + name: "successful file creation", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PutReposContentsByOwnerByRepoByPath, + expectRequestBody(t, map[string]interface{}{ + "message": "Add example file", + "content": "IyBFeGFtcGxlCgpUaGlzIGlzIGFuIGV4YW1wbGUgZmlsZS4=", // Base64 encoded content + "branch": "main", + }).andThen( + mockResponse(t, http.StatusOK, mockFileResponse), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "path": "docs/example.md", + "content": "# Example\n\nThis is an example file.", + "message": "Add example file", + "branch": "main", + }, + expectError: false, + expectedContent: mockFileResponse, + }, + { + name: "successful file update with SHA", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PutReposContentsByOwnerByRepoByPath, + expectRequestBody(t, map[string]interface{}{ + "message": "Update example file", + "content": "IyBVcGRhdGVkIEV4YW1wbGUKClRoaXMgZmlsZSBoYXMgYmVlbiB1cGRhdGVkLg==", // Base64 encoded content + "branch": "main", + "sha": "abc123def456", + }).andThen( + mockResponse(t, http.StatusOK, mockFileResponse), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "path": "docs/example.md", + "content": "# Updated Example\n\nThis file has been updated.", + "message": "Update example file", + "branch": "main", + "sha": "abc123def456", + }, + expectError: false, + expectedContent: mockFileResponse, + }, + { + name: "file creation fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.PutReposContentsByOwnerByRepoByPath, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"message": "Invalid request"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "path": "docs/example.md", + "content": "#Invalid Content", + "message": "Invalid request", + "branch": "nonexistent-branch", + }, + expectError: true, + expectedErrMsg: "failed to create/update file", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := CreateOrUpdateFile(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedContent github.RepositoryContentResponse + err = json.Unmarshal([]byte(textContent.Text), &returnedContent) + require.NoError(t, err) + + // Verify content + assert.Equal(t, *tc.expectedContent.Content.Name, *returnedContent.Content.Name) + assert.Equal(t, *tc.expectedContent.Content.Path, *returnedContent.Content.Path) + assert.Equal(t, *tc.expectedContent.Content.SHA, *returnedContent.Content.SHA) + + // Verify commit + assert.Equal(t, *tc.expectedContent.Commit.SHA, *returnedContent.Commit.SHA) + assert.Equal(t, *tc.expectedContent.Commit.Message, *returnedContent.Commit.Message) + }) + } +} + +func Test_CreateRepository(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := CreateRepository(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "create_repository", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "name") + assert.Contains(t, tool.InputSchema.Properties, "description") + assert.Contains(t, tool.InputSchema.Properties, "private") + assert.Contains(t, tool.InputSchema.Properties, "autoInit") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"name"}) + + // Setup mock repository response + mockRepo := &github.Repository{ + Name: github.Ptr("test-repo"), + Description: github.Ptr("Test repository"), + Private: github.Ptr(true), + HTMLURL: github.Ptr("https://github.com/testuser/test-repo"), + CloneURL: github.Ptr("https://github.com/testuser/test-repo.git"), + CreatedAt: &github.Timestamp{Time: time.Now()}, + Owner: &github.User{ + Login: github.Ptr("testuser"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedRepo *github.Repository + expectedErrMsg string + }{ + { + name: "successful repository creation with all parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.EndpointPattern{ + Pattern: "/user/repos", + Method: "POST", + }, + expectRequestBody(t, map[string]interface{}{ + "name": "test-repo", + "description": "Test repository", + "private": true, + "auto_init": true, + }).andThen( + mockResponse(t, http.StatusCreated, mockRepo), + ), + ), + ), + requestArgs: map[string]interface{}{ + "name": "test-repo", + "description": "Test repository", + "private": true, + "autoInit": true, + }, + expectError: false, + expectedRepo: mockRepo, + }, + { + name: "successful repository creation with minimal parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.EndpointPattern{ + Pattern: "/user/repos", + Method: "POST", + }, + expectRequestBody(t, map[string]interface{}{ + "name": "test-repo", + "auto_init": false, + "description": "", + "private": false, + }).andThen( + mockResponse(t, http.StatusCreated, mockRepo), + ), + ), + ), + requestArgs: map[string]interface{}{ + "name": "test-repo", + }, + expectError: false, + expectedRepo: mockRepo, + }, + { + name: "repository creation fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.EndpointPattern{ + Pattern: "/user/repos", + Method: "POST", + }, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnprocessableEntity) + _, _ = w.Write([]byte(`{"message": "Repository creation failed"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "name": "invalid-repo", + }, + expectError: true, + expectedErrMsg: "failed to create repository", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := CreateRepository(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedRepo github.Repository + err = json.Unmarshal([]byte(textContent.Text), &returnedRepo) + assert.NoError(t, err) + + // Verify repository details + assert.Equal(t, *tc.expectedRepo.Name, *returnedRepo.Name) + assert.Equal(t, *tc.expectedRepo.Description, *returnedRepo.Description) + assert.Equal(t, *tc.expectedRepo.Private, *returnedRepo.Private) + assert.Equal(t, *tc.expectedRepo.HTMLURL, *returnedRepo.HTMLURL) + assert.Equal(t, *tc.expectedRepo.Owner.Login, *returnedRepo.Owner.Login) + }) + } +} + +func Test_PushFiles(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := PushFiles(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "push_files", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "branch") + assert.Contains(t, tool.InputSchema.Properties, "files") + assert.Contains(t, tool.InputSchema.Properties, "message") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "branch", "files", "message"}) + + // Setup mock objects + mockRef := &github.Reference{ + Ref: github.Ptr("refs/heads/main"), + Object: &github.GitObject{ + SHA: github.Ptr("abc123"), + URL: github.Ptr("https://api.github.com/repos/owner/repo/git/trees/abc123"), + }, + } + + mockCommit := &github.Commit{ + SHA: github.Ptr("abc123"), + Tree: &github.Tree{ + SHA: github.Ptr("def456"), + }, + } + + mockTree := &github.Tree{ + SHA: github.Ptr("ghi789"), + } + + mockNewCommit := &github.Commit{ + SHA: github.Ptr("jkl012"), + Message: github.Ptr("Update multiple files"), + HTMLURL: github.Ptr("https://github.com/owner/repo/commit/jkl012"), + } + + mockUpdatedRef := &github.Reference{ + Ref: github.Ptr("refs/heads/main"), + Object: &github.GitObject{ + SHA: github.Ptr("jkl012"), + URL: github.Ptr("https://api.github.com/repos/owner/repo/git/trees/jkl012"), + }, + } + + // Define test cases + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedRef *github.Reference + expectedErrMsg string + }{ + { + name: "successful push of multiple files", + mockedClient: mock.NewMockedHTTPClient( + // Get branch reference + mock.WithRequestMatch( + mock.GetReposGitRefByOwnerByRepoByRef, + mockRef, + ), + // Get commit + mock.WithRequestMatch( + mock.GetReposGitCommitsByOwnerByRepoByCommitSha, + mockCommit, + ), + // Create tree + mock.WithRequestMatchHandler( + mock.PostReposGitTreesByOwnerByRepo, + expectRequestBody(t, map[string]interface{}{ + "base_tree": "def456", + "tree": []interface{}{ + map[string]interface{}{ + "path": "README.md", + "mode": "100644", + "type": "blob", + "content": "# Updated README\n\nThis is an updated README file.", + }, + map[string]interface{}{ + "path": "docs/example.md", + "mode": "100644", + "type": "blob", + "content": "# Example\n\nThis is an example file.", + }, + }, + }).andThen( + mockResponse(t, http.StatusCreated, mockTree), + ), + ), + // Create commit + mock.WithRequestMatchHandler( + mock.PostReposGitCommitsByOwnerByRepo, + expectRequestBody(t, map[string]interface{}{ + "message": "Update multiple files", + "tree": "ghi789", + "parents": []interface{}{"abc123"}, + }).andThen( + mockResponse(t, http.StatusCreated, mockNewCommit), + ), + ), + // Update reference + mock.WithRequestMatchHandler( + mock.PatchReposGitRefsByOwnerByRepoByRef, + expectRequestBody(t, map[string]interface{}{ + "sha": "jkl012", + "force": false, + }).andThen( + mockResponse(t, http.StatusOK, mockUpdatedRef), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "main", + "files": []interface{}{ + map[string]interface{}{ + "path": "README.md", + "content": "# Updated README\n\nThis is an updated README file.", + }, + map[string]interface{}{ + "path": "docs/example.md", + "content": "# Example\n\nThis is an example file.", + }, + }, + "message": "Update multiple files", + }, + expectError: false, + expectedRef: mockUpdatedRef, + }, + { + name: "fails when files parameter is invalid", + mockedClient: mock.NewMockedHTTPClient( + // No requests expected + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "main", + "files": "invalid-files-parameter", // Not an array + "message": "Update multiple files", + }, + expectError: false, // This returns a tool error, not a Go error + expectedErrMsg: "files parameter must be an array", + }, + { + name: "fails when files contains object without path", + mockedClient: mock.NewMockedHTTPClient( + // Get branch reference + mock.WithRequestMatch( + mock.GetReposGitRefByOwnerByRepoByRef, + mockRef, + ), + // Get commit + mock.WithRequestMatch( + mock.GetReposGitCommitsByOwnerByRepoByCommitSha, + mockCommit, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "main", + "files": []interface{}{ + map[string]interface{}{ + "content": "# Missing path", + }, + }, + "message": "Update file", + }, + expectError: false, // This returns a tool error, not a Go error + expectedErrMsg: "each file must have a path", + }, + { + name: "fails when files contains object without content", + mockedClient: mock.NewMockedHTTPClient( + // Get branch reference + mock.WithRequestMatch( + mock.GetReposGitRefByOwnerByRepoByRef, + mockRef, + ), + // Get commit + mock.WithRequestMatch( + mock.GetReposGitCommitsByOwnerByRepoByCommitSha, + mockCommit, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "main", + "files": []interface{}{ + map[string]interface{}{ + "path": "README.md", + // Missing content + }, + }, + "message": "Update file", + }, + expectError: false, // This returns a tool error, not a Go error + expectedErrMsg: "each file must have content", + }, + { + name: "fails to get branch reference", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposGitRefByOwnerByRepoByRef, + mockResponse(t, http.StatusNotFound, nil), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "non-existent-branch", + "files": []interface{}{ + map[string]interface{}{ + "path": "README.md", + "content": "# README", + }, + }, + "message": "Update file", + }, + expectError: true, + expectedErrMsg: "failed to get branch reference", + }, + { + name: "fails to get base commit", + mockedClient: mock.NewMockedHTTPClient( + // Get branch reference + mock.WithRequestMatch( + mock.GetReposGitRefByOwnerByRepoByRef, + mockRef, + ), + // Fail to get commit + mock.WithRequestMatchHandler( + mock.GetReposGitCommitsByOwnerByRepoByCommitSha, + mockResponse(t, http.StatusNotFound, nil), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "main", + "files": []interface{}{ + map[string]interface{}{ + "path": "README.md", + "content": "# README", + }, + }, + "message": "Update file", + }, + expectError: true, + expectedErrMsg: "failed to get base commit", + }, + { + name: "fails to create tree", + mockedClient: mock.NewMockedHTTPClient( + // Get branch reference + mock.WithRequestMatch( + mock.GetReposGitRefByOwnerByRepoByRef, + mockRef, + ), + // Get commit + mock.WithRequestMatch( + mock.GetReposGitCommitsByOwnerByRepoByCommitSha, + mockCommit, + ), + // Fail to create tree + mock.WithRequestMatchHandler( + mock.PostReposGitTreesByOwnerByRepo, + mockResponse(t, http.StatusInternalServerError, nil), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "branch": "main", + "files": []interface{}{ + map[string]interface{}{ + "path": "README.md", + "content": "# README", + }, + }, + "message": "Update file", + }, + expectError: true, + expectedErrMsg: "failed to create tree", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := PushFiles(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + if tc.expectedErrMsg != "" { + require.NotNil(t, result) + textContent := getTextResult(t, result) + assert.Contains(t, textContent.Text, tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedRef github.Reference + err = json.Unmarshal([]byte(textContent.Text), &returnedRef) + require.NoError(t, err) + + assert.Equal(t, *tc.expectedRef.Ref, *returnedRef.Ref) + assert.Equal(t, *tc.expectedRef.Object.SHA, *returnedRef.Object.SHA) + }) + } +} + +func Test_ListBranches(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := ListBranches(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "list_branches", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "page") + assert.Contains(t, tool.InputSchema.Properties, "perPage") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"}) + + // Setup mock branches for success case + mockBranches := []*github.Branch{ + { + Name: github.Ptr("main"), + Commit: &github.RepositoryCommit{SHA: github.Ptr("abc123")}, + }, + { + Name: github.Ptr("develop"), + Commit: &github.RepositoryCommit{SHA: github.Ptr("def456")}, + }, + } + + // Test cases + tests := []struct { + name string + args map[string]interface{} + mockResponses []mock.MockBackendOption + wantErr bool + errContains string + }{ + { + name: "success", + args: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "page": float64(2), + }, + mockResponses: []mock.MockBackendOption{ + mock.WithRequestMatch( + mock.GetReposBranchesByOwnerByRepo, + mockBranches, + ), + }, + wantErr: false, + }, + { + name: "missing owner", + args: map[string]interface{}{ + "repo": "repo", + }, + mockResponses: []mock.MockBackendOption{}, + wantErr: false, + errContains: "missing required parameter: owner", + }, + { + name: "missing repo", + args: map[string]interface{}{ + "owner": "owner", + }, + mockResponses: []mock.MockBackendOption{}, + wantErr: false, + errContains: "missing required parameter: repo", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create mock client + mockClient := github.NewClient(mock.NewMockedHTTPClient(tt.mockResponses...)) + _, handler := ListBranches(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + // Create request + request := createMCPRequest(tt.args) + + // Call handler + result, err := handler(context.Background(), request) + if tt.wantErr { + require.Error(t, err) + if tt.errContains != "" { + assert.Contains(t, err.Error(), tt.errContains) + } + return + } + + require.NoError(t, err) + require.NotNil(t, result) + + if tt.errContains != "" { + textContent := getTextResult(t, result) + assert.Contains(t, textContent.Text, tt.errContains) + return + } + + textContent := getTextResult(t, result) + require.NotEmpty(t, textContent.Text) + + // Verify response + var branches []*github.Branch + err = json.Unmarshal([]byte(textContent.Text), &branches) + require.NoError(t, err) + assert.Len(t, branches, 2) + assert.Equal(t, "main", *branches[0].Name) + assert.Equal(t, "develop", *branches[1].Name) + }) + } +} + +func Test_ListTags(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := ListTags(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "list_tags", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"}) + + // Setup mock tags for success case + mockTags := []*github.RepositoryTag{ + { + Name: github.Ptr("v1.0.0"), + Commit: &github.Commit{ + SHA: github.Ptr("v1.0.0-tag-sha"), + URL: github.Ptr("https://api.github.com/repos/owner/repo/commits/abc123"), + }, + ZipballURL: github.Ptr("https://github.com/owner/repo/zipball/v1.0.0"), + TarballURL: github.Ptr("https://github.com/owner/repo/tarball/v1.0.0"), + }, + { + Name: github.Ptr("v0.9.0"), + Commit: &github.Commit{ + SHA: github.Ptr("v0.9.0-tag-sha"), + URL: github.Ptr("https://api.github.com/repos/owner/repo/commits/def456"), + }, + ZipballURL: github.Ptr("https://github.com/owner/repo/zipball/v0.9.0"), + TarballURL: github.Ptr("https://github.com/owner/repo/tarball/v0.9.0"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedTags []*github.RepositoryTag + expectedErrMsg string + }{ + { + name: "successful tags list", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposTagsByOwnerByRepo, + expectPath( + t, + "/repos/owner/repo/tags", + ).andThen( + mockResponse(t, http.StatusOK, mockTags), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + }, + expectError: false, + expectedTags: mockTags, + }, + { + name: "list tags fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposTagsByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + _, _ = w.Write([]byte(`{"message": "Internal Server Error"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + }, + expectError: true, + expectedErrMsg: "failed to list tags", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := ListTags(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Parse and verify the result + var returnedTags []*github.RepositoryTag + err = json.Unmarshal([]byte(textContent.Text), &returnedTags) + require.NoError(t, err) + + // Verify each tag + require.Equal(t, len(tc.expectedTags), len(returnedTags)) + for i, expectedTag := range tc.expectedTags { + assert.Equal(t, *expectedTag.Name, *returnedTags[i].Name) + assert.Equal(t, *expectedTag.Commit.SHA, *returnedTags[i].Commit.SHA) + } + }) + } +} + +func Test_GetTag(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := GetTag(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_tag", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "tag") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "tag"}) + + mockTagRef := &github.Reference{ + Ref: github.Ptr("refs/tags/v1.0.0"), + Object: &github.GitObject{ + SHA: github.Ptr("v1.0.0-tag-sha"), + }, + } + + mockTagObj := &github.Tag{ + SHA: github.Ptr("v1.0.0-tag-sha"), + Tag: github.Ptr("v1.0.0"), + Message: github.Ptr("Release v1.0.0"), + Object: &github.GitObject{ + Type: github.Ptr("commit"), + SHA: github.Ptr("abc123"), + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedTag *github.Tag + expectedErrMsg string + }{ + { + name: "successful tag retrieval", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposGitRefByOwnerByRepoByRef, + expectPath( + t, + "/repos/owner/repo/git/ref/tags/v1.0.0", + ).andThen( + mockResponse(t, http.StatusOK, mockTagRef), + ), + ), + mock.WithRequestMatchHandler( + mock.GetReposGitTagsByOwnerByRepoByTagSha, + expectPath( + t, + "/repos/owner/repo/git/tags/v1.0.0-tag-sha", + ).andThen( + mockResponse(t, http.StatusOK, mockTagObj), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "tag": "v1.0.0", + }, + expectError: false, + expectedTag: mockTagObj, + }, + { + name: "tag reference not found", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposGitRefByOwnerByRepoByRef, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Reference does not exist"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "tag": "v1.0.0", + }, + expectError: true, + expectedErrMsg: "failed to get tag reference", + }, + { + name: "tag object not found", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposGitRefByOwnerByRepoByRef, + mockTagRef, + ), + mock.WithRequestMatchHandler( + mock.GetReposGitTagsByOwnerByRepoByTagSha, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Tag object does not exist"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "tag": "v1.0.0", + }, + expectError: true, + expectedErrMsg: "failed to get tag object", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetTag(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Parse and verify the result + var returnedTag github.Tag + err = json.Unmarshal([]byte(textContent.Text), &returnedTag) + require.NoError(t, err) + + assert.Equal(t, *tc.expectedTag.SHA, *returnedTag.SHA) + assert.Equal(t, *tc.expectedTag.Tag, *returnedTag.Tag) + assert.Equal(t, *tc.expectedTag.Message, *returnedTag.Message) + assert.Equal(t, *tc.expectedTag.Object.Type, *returnedTag.Object.Type) + assert.Equal(t, *tc.expectedTag.Object.SHA, *returnedTag.Object.SHA) + }) + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/repository_resource.go b/sre_agent/servers/github-mcp-server/pkg/github/repository_resource.go new file mode 100644 index 0000000..949157f --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/repository_resource.go @@ -0,0 +1,206 @@ +package github + +import ( + "context" + "encoding/base64" + "errors" + "fmt" + "io" + "mime" + "net/http" + "path/filepath" + "strings" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +// GetRepositoryResourceContent defines the resource template and handler for getting repository content. +func GetRepositoryResourceContent(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) { + return mcp.NewResourceTemplate( + "repo://{owner}/{repo}/contents{/path*}", // Resource template + t("RESOURCE_REPOSITORY_CONTENT_DESCRIPTION", "Repository Content"), + ), + RepositoryResourceContentsHandler(getClient) +} + +// GetRepositoryResourceBranchContent defines the resource template and handler for getting repository content for a branch. +func GetRepositoryResourceBranchContent(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) { + return mcp.NewResourceTemplate( + "repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}", // Resource template + t("RESOURCE_REPOSITORY_CONTENT_BRANCH_DESCRIPTION", "Repository Content for specific branch"), + ), + RepositoryResourceContentsHandler(getClient) +} + +// GetRepositoryResourceCommitContent defines the resource template and handler for getting repository content for a commit. +func GetRepositoryResourceCommitContent(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) { + return mcp.NewResourceTemplate( + "repo://{owner}/{repo}/sha/{sha}/contents{/path*}", // Resource template + t("RESOURCE_REPOSITORY_CONTENT_COMMIT_DESCRIPTION", "Repository Content for specific commit"), + ), + RepositoryResourceContentsHandler(getClient) +} + +// GetRepositoryResourceTagContent defines the resource template and handler for getting repository content for a tag. +func GetRepositoryResourceTagContent(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) { + return mcp.NewResourceTemplate( + "repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}", // Resource template + t("RESOURCE_REPOSITORY_CONTENT_TAG_DESCRIPTION", "Repository Content for specific tag"), + ), + RepositoryResourceContentsHandler(getClient) +} + +// GetRepositoryResourcePrContent defines the resource template and handler for getting repository content for a pull request. +func GetRepositoryResourcePrContent(getClient GetClientFn, t translations.TranslationHelperFunc) (mcp.ResourceTemplate, server.ResourceTemplateHandlerFunc) { + return mcp.NewResourceTemplate( + "repo://{owner}/{repo}/refs/pull/{prNumber}/head/contents{/path*}", // Resource template + t("RESOURCE_REPOSITORY_CONTENT_PR_DESCRIPTION", "Repository Content for specific pull request"), + ), + RepositoryResourceContentsHandler(getClient) +} + +// RepositoryResourceContentsHandler returns a handler function for repository content requests. +func RepositoryResourceContentsHandler(getClient GetClientFn) func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { + return func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { + // the matcher will give []string with one element + // https://github.com/mark3labs/mcp-go/pull/54 + o, ok := request.Params.Arguments["owner"].([]string) + if !ok || len(o) == 0 { + return nil, errors.New("owner is required") + } + owner := o[0] + + r, ok := request.Params.Arguments["repo"].([]string) + if !ok || len(r) == 0 { + return nil, errors.New("repo is required") + } + repo := r[0] + + // path should be a joined list of the path parts + path := "" + p, ok := request.Params.Arguments["path"].([]string) + if ok { + path = strings.Join(p, "/") + } + + opts := &github.RepositoryContentGetOptions{} + + sha, ok := request.Params.Arguments["sha"].([]string) + if ok && len(sha) > 0 { + opts.Ref = sha[0] + } + + branch, ok := request.Params.Arguments["branch"].([]string) + if ok && len(branch) > 0 { + opts.Ref = "refs/heads/" + branch[0] + } + + tag, ok := request.Params.Arguments["tag"].([]string) + if ok && len(tag) > 0 { + opts.Ref = "refs/tags/" + tag[0] + } + prNumber, ok := request.Params.Arguments["prNumber"].([]string) + if ok && len(prNumber) > 0 { + opts.Ref = "refs/pull/" + prNumber[0] + "/head" + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + fileContent, directoryContent, _, err := client.Repositories.GetContents(ctx, owner, repo, path, opts) + if err != nil { + return nil, err + } + + if directoryContent != nil { + var resources []mcp.ResourceContents + for _, entry := range directoryContent { + mimeType := "text/directory" + if entry.GetType() == "file" { + // this is system dependent, and a best guess + ext := filepath.Ext(entry.GetName()) + mimeType = mime.TypeByExtension(ext) + if ext == ".md" { + mimeType = "text/markdown" + } + } + resources = append(resources, mcp.TextResourceContents{ + URI: entry.GetHTMLURL(), + MIMEType: mimeType, + Text: entry.GetName(), + }) + + } + return resources, nil + + } + if fileContent != nil { + if fileContent.Content != nil { + // download the file content from fileContent.GetDownloadURL() and use the content-type header to determine the MIME type + // and return the content as a blob unless it is a text file, where you can return the content as text + req, err := http.NewRequest("GET", fileContent.GetDownloadURL(), nil) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + resp, err := client.Client().Do(req) + if err != nil { + return nil, fmt.Errorf("failed to send request: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return nil, fmt.Errorf("failed to fetch file content: %s", string(body)) + } + + ext := filepath.Ext(fileContent.GetName()) + mimeType := resp.Header.Get("Content-Type") + if ext == ".md" { + mimeType = "text/markdown" + } else if mimeType == "" { + // backstop to the file extension if the content type is not set + mimeType = mime.TypeByExtension(filepath.Ext(fileContent.GetName())) + } + + // if the content is a string, return it as text + if strings.HasPrefix(mimeType, "text") { + content, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to parse the response body: %w", err) + } + + return []mcp.ResourceContents{ + mcp.TextResourceContents{ + URI: request.Params.URI, + MIMEType: mimeType, + Text: string(content), + }, + }, nil + } + // otherwise, read the content and encode it as base64 + decodedContent, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to parse the response body: %w", err) + } + + return []mcp.ResourceContents{ + mcp.BlobResourceContents{ + URI: request.Params.URI, + MIMEType: mimeType, + Blob: base64.StdEncoding.EncodeToString(decodedContent), // Encode content as Base64 + }, + }, nil + } + } + + return nil, errors.New("no repository resource content found") + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/repository_resource_test.go b/sre_agent/servers/github-mcp-server/pkg/github/repository_resource_test.go new file mode 100644 index 0000000..ffd14be --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/repository_resource_test.go @@ -0,0 +1,283 @@ +package github + +import ( + "context" + "net/http" + "testing" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/migueleliasweb/go-github-mock/src/mock" + "github.com/stretchr/testify/require" +) + +var GetRawReposContentsByOwnerByRepoByPath mock.EndpointPattern = mock.EndpointPattern{ + Pattern: "/{owner}/{repo}/main/{path:.+}", + Method: "GET", +} + +func Test_repositoryResourceContentsHandler(t *testing.T) { + mockDirContent := []*github.RepositoryContent{ + { + Type: github.Ptr("file"), + Name: github.Ptr("README.md"), + Path: github.Ptr("README.md"), + SHA: github.Ptr("abc123"), + Size: github.Ptr(42), + HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/README.md"), + DownloadURL: github.Ptr("https://raw.githubusercontent.com/owner/repo/main/README.md"), + }, + { + Type: github.Ptr("dir"), + Name: github.Ptr("src"), + Path: github.Ptr("src"), + SHA: github.Ptr("def456"), + HTMLURL: github.Ptr("https://github.com/owner/repo/tree/main/src"), + DownloadURL: github.Ptr("https://raw.githubusercontent.com/owner/repo/main/src"), + }, + } + expectedDirContent := []mcp.TextResourceContents{ + { + URI: "https://github.com/owner/repo/blob/main/README.md", + MIMEType: "text/markdown", + Text: "README.md", + }, + { + URI: "https://github.com/owner/repo/tree/main/src", + MIMEType: "text/directory", + Text: "src", + }, + } + + mockTextContent := &github.RepositoryContent{ + Type: github.Ptr("file"), + Name: github.Ptr("README.md"), + Path: github.Ptr("README.md"), + Content: github.Ptr("# Test Repository\n\nThis is a test repository."), + SHA: github.Ptr("abc123"), + Size: github.Ptr(42), + HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/README.md"), + DownloadURL: github.Ptr("https://raw.githubusercontent.com/owner/repo/main/README.md"), + } + + mockFileContent := &github.RepositoryContent{ + Type: github.Ptr("file"), + Name: github.Ptr("data.png"), + Path: github.Ptr("data.png"), + Content: github.Ptr("IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku"), // Base64 encoded "# Test Repository\n\nThis is a test repository." + SHA: github.Ptr("abc123"), + Size: github.Ptr(42), + HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/data.png"), + DownloadURL: github.Ptr("https://raw.githubusercontent.com/owner/repo/main/data.png"), + } + + expectedFileContent := []mcp.BlobResourceContents{ + { + Blob: "IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku", + MIMEType: "image/png", + URI: "", + }, + } + + expectedTextContent := []mcp.TextResourceContents{ + { + Text: "# Test Repository\n\nThis is a test repository.", + MIMEType: "text/markdown", + URI: "", + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]any + expectError string + expectedResult any + expectedErrMsg string + }{ + { + name: "missing owner", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposContentsByOwnerByRepoByPath, + mockFileContent, + ), + ), + requestArgs: map[string]any{}, + expectError: "owner is required", + }, + { + name: "missing repo", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposContentsByOwnerByRepoByPath, + mockFileContent, + ), + ), + requestArgs: map[string]any{ + "owner": []string{"owner"}, + }, + expectError: "repo is required", + }, + { + name: "successful blob content fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposContentsByOwnerByRepoByPath, + mockFileContent, + ), + mock.WithRequestMatchHandler( + GetRawReposContentsByOwnerByRepoByPath, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "image/png") + // as this is given as a png, it will return the content as a blob + _, err := w.Write([]byte("# Test Repository\n\nThis is a test repository.")) + require.NoError(t, err) + }), + ), + ), + requestArgs: map[string]any{ + "owner": []string{"owner"}, + "repo": []string{"repo"}, + "path": []string{"data.png"}, + "branch": []string{"main"}, + }, + expectedResult: expectedFileContent, + }, + { + name: "successful text content fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposContentsByOwnerByRepoByPath, + mockTextContent, + ), + mock.WithRequestMatch( + GetRawReposContentsByOwnerByRepoByPath, + []byte("# Test Repository\n\nThis is a test repository."), + ), + ), + requestArgs: map[string]any{ + "owner": []string{"owner"}, + "repo": []string{"repo"}, + "path": []string{"README.md"}, + "branch": []string{"main"}, + }, + expectedResult: expectedTextContent, + }, + { + name: "successful directory content fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposContentsByOwnerByRepoByPath, + mockDirContent, + ), + ), + requestArgs: map[string]any{ + "owner": []string{"owner"}, + "repo": []string{"repo"}, + "path": []string{"src"}, + }, + expectedResult: expectedDirContent, + }, + { + name: "no data", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposContentsByOwnerByRepoByPath, + ), + ), + requestArgs: map[string]any{ + "owner": []string{"owner"}, + "repo": []string{"repo"}, + "path": []string{"src"}, + }, + expectedResult: nil, + expectError: "no repository resource content found", + }, + { + name: "empty data", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposContentsByOwnerByRepoByPath, + []*github.RepositoryContent{}, + ), + ), + requestArgs: map[string]any{ + "owner": []string{"owner"}, + "repo": []string{"repo"}, + "path": []string{"src"}, + }, + expectedResult: nil, + }, + { + name: "content fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposContentsByOwnerByRepoByPath, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]any{ + "owner": []string{"owner"}, + "repo": []string{"repo"}, + "path": []string{"nonexistent.md"}, + "branch": []string{"main"}, + }, + expectError: "404 Not Found", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + client := github.NewClient(tc.mockedClient) + handler := RepositoryResourceContentsHandler((stubGetClientFn(client))) + + request := mcp.ReadResourceRequest{ + Params: struct { + URI string `json:"uri"` + Arguments map[string]any `json:"arguments,omitempty"` + }{ + Arguments: tc.requestArgs, + }, + } + + resp, err := handler(context.TODO(), request) + + if tc.expectError != "" { + require.ErrorContains(t, err, tc.expectedErrMsg) + return + } + + require.NoError(t, err) + require.ElementsMatch(t, resp, tc.expectedResult) + }) + } +} + +func Test_GetRepositoryResourceContent(t *testing.T) { + tmpl, _ := GetRepositoryResourceContent(nil, translations.NullTranslationHelper) + require.Equal(t, "repo://{owner}/{repo}/contents{/path*}", tmpl.URITemplate.Raw()) +} + +func Test_GetRepositoryResourceBranchContent(t *testing.T) { + tmpl, _ := GetRepositoryResourceBranchContent(nil, translations.NullTranslationHelper) + require.Equal(t, "repo://{owner}/{repo}/refs/heads/{branch}/contents{/path*}", tmpl.URITemplate.Raw()) +} +func Test_GetRepositoryResourceCommitContent(t *testing.T) { + tmpl, _ := GetRepositoryResourceCommitContent(nil, translations.NullTranslationHelper) + require.Equal(t, "repo://{owner}/{repo}/sha/{sha}/contents{/path*}", tmpl.URITemplate.Raw()) +} + +func Test_GetRepositoryResourceTagContent(t *testing.T) { + tmpl, _ := GetRepositoryResourceTagContent(nil, translations.NullTranslationHelper) + require.Equal(t, "repo://{owner}/{repo}/refs/tags/{tag}/contents{/path*}", tmpl.URITemplate.Raw()) +} + +func Test_GetRepositoryResourcePrContent(t *testing.T) { + tmpl, _ := GetRepositoryResourcePrContent(nil, translations.NullTranslationHelper) + require.Equal(t, "repo://{owner}/{repo}/refs/pull/{prNumber}/head/contents{/path*}", tmpl.URITemplate.Raw()) +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/resources.go b/sre_agent/servers/github-mcp-server/pkg/github/resources.go new file mode 100644 index 0000000..774261e --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/resources.go @@ -0,0 +1,14 @@ +package github + +import ( + "github.com/github/github-mcp-server/pkg/translations" + "github.com/mark3labs/mcp-go/server" +) + +func RegisterResources(s *server.MCPServer, getClient GetClientFn, t translations.TranslationHelperFunc) { + s.AddResourceTemplate(GetRepositoryResourceContent(getClient, t)) + s.AddResourceTemplate(GetRepositoryResourceBranchContent(getClient, t)) + s.AddResourceTemplate(GetRepositoryResourceCommitContent(getClient, t)) + s.AddResourceTemplate(GetRepositoryResourceTagContent(getClient, t)) + s.AddResourceTemplate(GetRepositoryResourcePrContent(getClient, t)) +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/search.go b/sre_agent/servers/github-mcp-server/pkg/github/search.go new file mode 100644 index 0000000..86a4f43 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/search.go @@ -0,0 +1,224 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "io" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +// SearchRepositories creates a tool to search for GitHub repositories. +func SearchRepositories(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("search_repositories", + mcp.WithDescription(t("TOOL_SEARCH_REPOSITORIES_DESCRIPTION", "Search for GitHub repositories")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_SEARCH_REPOSITORIES_USER_TITLE", "Search repositories"), + ReadOnlyHint: true, + }), + mcp.WithString("query", + mcp.Required(), + mcp.Description("Search query"), + ), + WithPagination(), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + query, err := requiredParam[string](request, "query") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pagination, err := OptionalPaginationParams(request) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.SearchOptions{ + ListOptions: github.ListOptions{ + Page: pagination.page, + PerPage: pagination.perPage, + }, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + result, resp, err := client.Search.Repositories(ctx, query, opts) + if err != nil { + return nil, fmt.Errorf("failed to search repositories: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != 200 { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to search repositories: %s", string(body))), nil + } + + r, err := json.Marshal(result) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// SearchCode creates a tool to search for code across GitHub repositories. +func SearchCode(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("search_code", + mcp.WithDescription(t("TOOL_SEARCH_CODE_DESCRIPTION", "Search for code across GitHub repositories")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_SEARCH_CODE_USER_TITLE", "Search code"), + ReadOnlyHint: true, + }), + mcp.WithString("q", + mcp.Required(), + mcp.Description("Search query using GitHub code search syntax"), + ), + mcp.WithString("sort", + mcp.Description("Sort field ('indexed' only)"), + ), + mcp.WithString("order", + mcp.Description("Sort order"), + mcp.Enum("asc", "desc"), + ), + WithPagination(), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + query, err := requiredParam[string](request, "q") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + sort, err := OptionalParam[string](request, "sort") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + order, err := OptionalParam[string](request, "order") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pagination, err := OptionalPaginationParams(request) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.SearchOptions{ + Sort: sort, + Order: order, + ListOptions: github.ListOptions{ + PerPage: pagination.perPage, + Page: pagination.page, + }, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + + result, resp, err := client.Search.Code(ctx, query, opts) + if err != nil { + return nil, fmt.Errorf("failed to search code: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != 200 { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to search code: %s", string(body))), nil + } + + r, err := json.Marshal(result) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +// SearchUsers creates a tool to search for GitHub users. +func SearchUsers(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool("search_users", + mcp.WithDescription(t("TOOL_SEARCH_USERS_DESCRIPTION", "Search for GitHub users")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_SEARCH_USERS_USER_TITLE", "Search users"), + ReadOnlyHint: true, + }), + mcp.WithString("q", + mcp.Required(), + mcp.Description("Search query using GitHub users search syntax"), + ), + mcp.WithString("sort", + mcp.Description("Sort field by category"), + mcp.Enum("followers", "repositories", "joined"), + ), + mcp.WithString("order", + mcp.Description("Sort order"), + mcp.Enum("asc", "desc"), + ), + WithPagination(), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + query, err := requiredParam[string](request, "q") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + sort, err := OptionalParam[string](request, "sort") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + order, err := OptionalParam[string](request, "order") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + pagination, err := OptionalPaginationParams(request) + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + opts := &github.SearchOptions{ + Sort: sort, + Order: order, + ListOptions: github.ListOptions{ + PerPage: pagination.perPage, + Page: pagination.page, + }, + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + + result, resp, err := client.Search.Users(ctx, query, opts) + if err != nil { + return nil, fmt.Errorf("failed to search users: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != 200 { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to search users: %s", string(body))), nil + } + + r, err := json.Marshal(result) + if err != nil { + return nil, fmt.Errorf("failed to marshal response: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/search_test.go b/sre_agent/servers/github-mcp-server/pkg/github/search_test.go new file mode 100644 index 0000000..b61518e --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/search_test.go @@ -0,0 +1,470 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "testing" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/migueleliasweb/go-github-mock/src/mock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_SearchRepositories(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := SearchRepositories(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "search_repositories", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "query") + assert.Contains(t, tool.InputSchema.Properties, "page") + assert.Contains(t, tool.InputSchema.Properties, "perPage") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"query"}) + + // Setup mock search results + mockSearchResult := &github.RepositoriesSearchResult{ + Total: github.Ptr(2), + IncompleteResults: github.Ptr(false), + Repositories: []*github.Repository{ + { + ID: github.Ptr(int64(12345)), + Name: github.Ptr("repo-1"), + FullName: github.Ptr("owner/repo-1"), + HTMLURL: github.Ptr("https://github.com/owner/repo-1"), + Description: github.Ptr("Test repository 1"), + StargazersCount: github.Ptr(100), + }, + { + ID: github.Ptr(int64(67890)), + Name: github.Ptr("repo-2"), + FullName: github.Ptr("owner/repo-2"), + HTMLURL: github.Ptr("https://github.com/owner/repo-2"), + Description: github.Ptr("Test repository 2"), + StargazersCount: github.Ptr(50), + }, + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedResult *github.RepositoriesSearchResult + expectedErrMsg string + }{ + { + name: "successful repository search", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchRepositories, + expectQueryParams(t, map[string]string{ + "q": "golang test", + "page": "2", + "per_page": "10", + }).andThen( + mockResponse(t, http.StatusOK, mockSearchResult), + ), + ), + ), + requestArgs: map[string]interface{}{ + "query": "golang test", + "page": float64(2), + "perPage": float64(10), + }, + expectError: false, + expectedResult: mockSearchResult, + }, + { + name: "repository search with default pagination", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchRepositories, + expectQueryParams(t, map[string]string{ + "q": "golang test", + "page": "1", + "per_page": "30", + }).andThen( + mockResponse(t, http.StatusOK, mockSearchResult), + ), + ), + ), + requestArgs: map[string]interface{}{ + "query": "golang test", + }, + expectError: false, + expectedResult: mockSearchResult, + }, + { + name: "search fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchRepositories, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"message": "Invalid query"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "query": "invalid:query", + }, + expectError: true, + expectedErrMsg: "failed to search repositories", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := SearchRepositories(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedResult github.RepositoriesSearchResult + err = json.Unmarshal([]byte(textContent.Text), &returnedResult) + require.NoError(t, err) + assert.Equal(t, *tc.expectedResult.Total, *returnedResult.Total) + assert.Equal(t, *tc.expectedResult.IncompleteResults, *returnedResult.IncompleteResults) + assert.Len(t, returnedResult.Repositories, len(tc.expectedResult.Repositories)) + for i, repo := range returnedResult.Repositories { + assert.Equal(t, *tc.expectedResult.Repositories[i].ID, *repo.ID) + assert.Equal(t, *tc.expectedResult.Repositories[i].Name, *repo.Name) + assert.Equal(t, *tc.expectedResult.Repositories[i].FullName, *repo.FullName) + assert.Equal(t, *tc.expectedResult.Repositories[i].HTMLURL, *repo.HTMLURL) + } + + }) + } +} + +func Test_SearchCode(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := SearchCode(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "search_code", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "q") + assert.Contains(t, tool.InputSchema.Properties, "sort") + assert.Contains(t, tool.InputSchema.Properties, "order") + assert.Contains(t, tool.InputSchema.Properties, "perPage") + assert.Contains(t, tool.InputSchema.Properties, "page") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"q"}) + + // Setup mock search results + mockSearchResult := &github.CodeSearchResult{ + Total: github.Ptr(2), + IncompleteResults: github.Ptr(false), + CodeResults: []*github.CodeResult{ + { + Name: github.Ptr("file1.go"), + Path: github.Ptr("path/to/file1.go"), + SHA: github.Ptr("abc123def456"), + HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/path/to/file1.go"), + Repository: &github.Repository{Name: github.Ptr("repo"), FullName: github.Ptr("owner/repo")}, + }, + { + Name: github.Ptr("file2.go"), + Path: github.Ptr("path/to/file2.go"), + SHA: github.Ptr("def456abc123"), + HTMLURL: github.Ptr("https://github.com/owner/repo/blob/main/path/to/file2.go"), + Repository: &github.Repository{Name: github.Ptr("repo"), FullName: github.Ptr("owner/repo")}, + }, + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedResult *github.CodeSearchResult + expectedErrMsg string + }{ + { + name: "successful code search with all parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchCode, + expectQueryParams(t, map[string]string{ + "q": "fmt.Println language:go", + "sort": "indexed", + "order": "desc", + "page": "1", + "per_page": "30", + }).andThen( + mockResponse(t, http.StatusOK, mockSearchResult), + ), + ), + ), + requestArgs: map[string]interface{}{ + "q": "fmt.Println language:go", + "sort": "indexed", + "order": "desc", + "page": float64(1), + "perPage": float64(30), + }, + expectError: false, + expectedResult: mockSearchResult, + }, + { + name: "code search with minimal parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchCode, + expectQueryParams(t, map[string]string{ + "q": "fmt.Println language:go", + "page": "1", + "per_page": "30", + }).andThen( + mockResponse(t, http.StatusOK, mockSearchResult), + ), + ), + ), + requestArgs: map[string]interface{}{ + "q": "fmt.Println language:go", + }, + expectError: false, + expectedResult: mockSearchResult, + }, + { + name: "search code fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchCode, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"message": "Validation Failed"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "q": "invalid:query", + }, + expectError: true, + expectedErrMsg: "failed to search code", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := SearchCode(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedResult github.CodeSearchResult + err = json.Unmarshal([]byte(textContent.Text), &returnedResult) + require.NoError(t, err) + assert.Equal(t, *tc.expectedResult.Total, *returnedResult.Total) + assert.Equal(t, *tc.expectedResult.IncompleteResults, *returnedResult.IncompleteResults) + assert.Len(t, returnedResult.CodeResults, len(tc.expectedResult.CodeResults)) + for i, code := range returnedResult.CodeResults { + assert.Equal(t, *tc.expectedResult.CodeResults[i].Name, *code.Name) + assert.Equal(t, *tc.expectedResult.CodeResults[i].Path, *code.Path) + assert.Equal(t, *tc.expectedResult.CodeResults[i].SHA, *code.SHA) + assert.Equal(t, *tc.expectedResult.CodeResults[i].HTMLURL, *code.HTMLURL) + assert.Equal(t, *tc.expectedResult.CodeResults[i].Repository.FullName, *code.Repository.FullName) + } + }) + } +} + +func Test_SearchUsers(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := SearchUsers(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "search_users", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "q") + assert.Contains(t, tool.InputSchema.Properties, "sort") + assert.Contains(t, tool.InputSchema.Properties, "order") + assert.Contains(t, tool.InputSchema.Properties, "perPage") + assert.Contains(t, tool.InputSchema.Properties, "page") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"q"}) + + // Setup mock search results + mockSearchResult := &github.UsersSearchResult{ + Total: github.Ptr(2), + IncompleteResults: github.Ptr(false), + Users: []*github.User{ + { + Login: github.Ptr("user1"), + ID: github.Ptr(int64(1001)), + HTMLURL: github.Ptr("https://github.com/user1"), + AvatarURL: github.Ptr("https://avatars.githubusercontent.com/u/1001"), + Type: github.Ptr("User"), + Followers: github.Ptr(100), + Following: github.Ptr(50), + }, + { + Login: github.Ptr("user2"), + ID: github.Ptr(int64(1002)), + HTMLURL: github.Ptr("https://github.com/user2"), + AvatarURL: github.Ptr("https://avatars.githubusercontent.com/u/1002"), + Type: github.Ptr("User"), + Followers: github.Ptr(200), + Following: github.Ptr(75), + }, + }, + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedResult *github.UsersSearchResult + expectedErrMsg string + }{ + { + name: "successful users search with all parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchUsers, + expectQueryParams(t, map[string]string{ + "q": "location:finland language:go", + "sort": "followers", + "order": "desc", + "page": "1", + "per_page": "30", + }).andThen( + mockResponse(t, http.StatusOK, mockSearchResult), + ), + ), + ), + requestArgs: map[string]interface{}{ + "q": "location:finland language:go", + "sort": "followers", + "order": "desc", + "page": float64(1), + "perPage": float64(30), + }, + expectError: false, + expectedResult: mockSearchResult, + }, + { + name: "users search with minimal parameters", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchUsers, + expectQueryParams(t, map[string]string{ + "q": "location:finland language:go", + "page": "1", + "per_page": "30", + }).andThen( + mockResponse(t, http.StatusOK, mockSearchResult), + ), + ), + ), + requestArgs: map[string]interface{}{ + "q": "location:finland language:go", + }, + expectError: false, + expectedResult: mockSearchResult, + }, + { + name: "search users fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetSearchUsers, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(`{"message": "Validation Failed"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "q": "invalid:query", + }, + expectError: true, + expectedErrMsg: "failed to search users", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := SearchUsers(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + require.NotNil(t, result) + + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedResult github.UsersSearchResult + err = json.Unmarshal([]byte(textContent.Text), &returnedResult) + require.NoError(t, err) + assert.Equal(t, *tc.expectedResult.Total, *returnedResult.Total) + assert.Equal(t, *tc.expectedResult.IncompleteResults, *returnedResult.IncompleteResults) + assert.Len(t, returnedResult.Users, len(tc.expectedResult.Users)) + for i, user := range returnedResult.Users { + assert.Equal(t, *tc.expectedResult.Users[i].Login, *user.Login) + assert.Equal(t, *tc.expectedResult.Users[i].ID, *user.ID) + assert.Equal(t, *tc.expectedResult.Users[i].HTMLURL, *user.HTMLURL) + assert.Equal(t, *tc.expectedResult.Users[i].AvatarURL, *user.AvatarURL) + assert.Equal(t, *tc.expectedResult.Users[i].Type, *user.Type) + assert.Equal(t, *tc.expectedResult.Users[i].Followers, *user.Followers) + } + }) + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/secret_scanning.go b/sre_agent/servers/github-mcp-server/pkg/github/secret_scanning.go new file mode 100644 index 0000000..cd0fd04 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/secret_scanning.go @@ -0,0 +1,154 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +func GetSecretScanningAlert(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool( + "get_secret_scanning_alert", + mcp.WithDescription(t("TOOL_GET_SECRET_SCANNING_ALERT_DESCRIPTION", "Get details of a specific secret scanning alert in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_GET_SECRET_SCANNING_ALERT_USER_TITLE", "Get secret scanning alert"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("The owner of the repository."), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("The name of the repository."), + ), + mcp.WithNumber("alertNumber", + mcp.Required(), + mcp.Description("The number of the alert."), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + alertNumber, err := RequiredInt(request, "alertNumber") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + + alert, resp, err := client.SecretScanning.GetAlert(ctx, owner, repo, int64(alertNumber)) + if err != nil { + return nil, fmt.Errorf("failed to get alert: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to get alert: %s", string(body))), nil + } + + r, err := json.Marshal(alert) + if err != nil { + return nil, fmt.Errorf("failed to marshal alert: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} + +func ListSecretScanningAlerts(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { + return mcp.NewTool( + "list_secret_scanning_alerts", + mcp.WithDescription(t("TOOL_LIST_SECRET_SCANNING_ALERTS_DESCRIPTION", "List secret scanning alerts in a GitHub repository.")), + mcp.WithToolAnnotation(mcp.ToolAnnotation{ + Title: t("TOOL_LIST_SECRET_SCANNING_ALERTS_USER_TITLE", "List secret scanning alerts"), + ReadOnlyHint: true, + }), + mcp.WithString("owner", + mcp.Required(), + mcp.Description("The owner of the repository."), + ), + mcp.WithString("repo", + mcp.Required(), + mcp.Description("The name of the repository."), + ), + mcp.WithString("state", + mcp.Description("Filter by state"), + mcp.Enum("open", "resolved"), + ), + mcp.WithString("secret_type", + mcp.Description("A comma-separated list of secret types to return. All default secret patterns are returned. To return generic patterns, pass the token name(s) in the parameter."), + ), + mcp.WithString("resolution", + mcp.Description("Filter by resolution"), + mcp.Enum("false_positive", "wont_fix", "revoked", "pattern_edited", "pattern_deleted", "used_in_tests"), + ), + ), + func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { + owner, err := requiredParam[string](request, "owner") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + repo, err := requiredParam[string](request, "repo") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + state, err := OptionalParam[string](request, "state") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + secretType, err := OptionalParam[string](request, "secret_type") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + resolution, err := OptionalParam[string](request, "resolution") + if err != nil { + return mcp.NewToolResultError(err.Error()), nil + } + + client, err := getClient(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get GitHub client: %w", err) + } + alerts, resp, err := client.SecretScanning.ListAlertsForRepo(ctx, owner, repo, &github.SecretScanningAlertListOptions{State: state, SecretType: secretType, Resolution: resolution}) + if err != nil { + return nil, fmt.Errorf("failed to list alerts: %w", err) + } + defer func() { _ = resp.Body.Close() }() + + if resp.StatusCode != http.StatusOK { + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } + return mcp.NewToolResultError(fmt.Sprintf("failed to list alerts: %s", string(body))), nil + } + + r, err := json.Marshal(alerts) + if err != nil { + return nil, fmt.Errorf("failed to marshal alerts: %w", err) + } + + return mcp.NewToolResultText(string(r)), nil + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/secret_scanning_test.go b/sre_agent/servers/github-mcp-server/pkg/github/secret_scanning_test.go new file mode 100644 index 0000000..d32cbca --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/secret_scanning_test.go @@ -0,0 +1,243 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "testing" + + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/migueleliasweb/go-github-mock/src/mock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_GetSecretScanningAlert(t *testing.T) { + mockClient := github.NewClient(nil) + tool, _ := GetSecretScanningAlert(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "get_secret_scanning_alert", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "alertNumber") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo", "alertNumber"}) + + // Setup mock alert for success case + mockAlert := &github.SecretScanningAlert{ + Number: github.Ptr(42), + State: github.Ptr("open"), + HTMLURL: github.Ptr("https://github.com/owner/private-repo/security/secret-scanning/42"), + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedAlert *github.SecretScanningAlert + expectedErrMsg string + }{ + { + name: "successful alert fetch", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatch( + mock.GetReposSecretScanningAlertsByOwnerByRepoByAlertNumber, + mockAlert, + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "alertNumber": float64(42), + }, + expectError: false, + expectedAlert: mockAlert, + }, + { + name: "alert fetch fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposSecretScanningAlertsByOwnerByRepoByAlertNumber, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message": "Not Found"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "alertNumber": float64(9999), + }, + expectError: true, + expectedErrMsg: "failed to get alert", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Setup client with mock + client := github.NewClient(tc.mockedClient) + _, handler := GetSecretScanningAlert(stubGetClientFn(client), translations.NullTranslationHelper) + + // Create call request + request := createMCPRequest(tc.requestArgs) + + // Call handler + result, err := handler(context.Background(), request) + + // Verify results + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + // Parse the result and get the text content if no error + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedAlert github.Alert + err = json.Unmarshal([]byte(textContent.Text), &returnedAlert) + assert.NoError(t, err) + assert.Equal(t, *tc.expectedAlert.Number, *returnedAlert.Number) + assert.Equal(t, *tc.expectedAlert.State, *returnedAlert.State) + assert.Equal(t, *tc.expectedAlert.HTMLURL, *returnedAlert.HTMLURL) + + }) + } +} + +func Test_ListSecretScanningAlerts(t *testing.T) { + // Verify tool definition once + mockClient := github.NewClient(nil) + tool, _ := ListSecretScanningAlerts(stubGetClientFn(mockClient), translations.NullTranslationHelper) + + assert.Equal(t, "list_secret_scanning_alerts", tool.Name) + assert.NotEmpty(t, tool.Description) + assert.Contains(t, tool.InputSchema.Properties, "owner") + assert.Contains(t, tool.InputSchema.Properties, "repo") + assert.Contains(t, tool.InputSchema.Properties, "state") + assert.Contains(t, tool.InputSchema.Properties, "secret_type") + assert.Contains(t, tool.InputSchema.Properties, "resolution") + assert.ElementsMatch(t, tool.InputSchema.Required, []string{"owner", "repo"}) + + // Setup mock alerts for success case + resolvedAlert := github.SecretScanningAlert{ + Number: github.Ptr(2), + HTMLURL: github.Ptr("https://github.com/owner/private-repo/security/secret-scanning/2"), + State: github.Ptr("resolved"), + Resolution: github.Ptr("false_positive"), + SecretType: github.Ptr("adafruit_io_key"), + } + openAlert := github.SecretScanningAlert{ + Number: github.Ptr(2), + HTMLURL: github.Ptr("https://github.com/owner/private-repo/security/secret-scanning/3"), + State: github.Ptr("open"), + Resolution: github.Ptr("false_positive"), + SecretType: github.Ptr("adafruit_io_key"), + } + + tests := []struct { + name string + mockedClient *http.Client + requestArgs map[string]interface{} + expectError bool + expectedAlerts []*github.SecretScanningAlert + expectedErrMsg string + }{ + { + name: "successful resolved alerts listing", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposSecretScanningAlertsByOwnerByRepo, + expectQueryParams(t, map[string]string{ + "state": "resolved", + }).andThen( + mockResponse(t, http.StatusOK, []*github.SecretScanningAlert{&resolvedAlert}), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + "state": "resolved", + }, + expectError: false, + expectedAlerts: []*github.SecretScanningAlert{&resolvedAlert}, + }, + { + name: "successful alerts listing", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposSecretScanningAlertsByOwnerByRepo, + expectQueryParams(t, map[string]string{}).andThen( + mockResponse(t, http.StatusOK, []*github.SecretScanningAlert{&resolvedAlert, &openAlert}), + ), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + }, + expectError: false, + expectedAlerts: []*github.SecretScanningAlert{&resolvedAlert, &openAlert}, + }, + { + name: "alerts listing fails", + mockedClient: mock.NewMockedHTTPClient( + mock.WithRequestMatchHandler( + mock.GetReposSecretScanningAlertsByOwnerByRepo, + http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusUnauthorized) + _, _ = w.Write([]byte(`{"message": "Unauthorized access"}`)) + }), + ), + ), + requestArgs: map[string]interface{}{ + "owner": "owner", + "repo": "repo", + }, + expectError: true, + expectedErrMsg: "failed to list alerts", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + client := github.NewClient(tc.mockedClient) + _, handler := ListSecretScanningAlerts(stubGetClientFn(client), translations.NullTranslationHelper) + + request := createMCPRequest(tc.requestArgs) + + result, err := handler(context.Background(), request) + + if tc.expectError { + require.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + return + } + + require.NoError(t, err) + + textContent := getTextResult(t, result) + + // Unmarshal and verify the result + var returnedAlerts []*github.SecretScanningAlert + err = json.Unmarshal([]byte(textContent.Text), &returnedAlerts) + assert.NoError(t, err) + assert.Len(t, returnedAlerts, len(tc.expectedAlerts)) + for i, alert := range returnedAlerts { + assert.Equal(t, *tc.expectedAlerts[i].Number, *alert.Number) + assert.Equal(t, *tc.expectedAlerts[i].HTMLURL, *alert.HTMLURL) + assert.Equal(t, *tc.expectedAlerts[i].State, *alert.State) + assert.Equal(t, *tc.expectedAlerts[i].Resolution, *alert.Resolution) + assert.Equal(t, *tc.expectedAlerts[i].SecretType, *alert.SecretType) + } + }) + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/server.go b/sre_agent/servers/github-mcp-server/pkg/github/server.go new file mode 100644 index 0000000..e4c2417 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/server.go @@ -0,0 +1,216 @@ +package github + +import ( + "errors" + "fmt" + + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +// NewServer creates a new GitHub MCP server with the specified GH client and logger. + +func NewServer(version string, opts ...server.ServerOption) *server.MCPServer { + // Add default options + defaultOpts := []server.ServerOption{ + server.WithToolCapabilities(true), + server.WithResourceCapabilities(true, true), + server.WithLogging(), + } + opts = append(defaultOpts, opts...) + + // Create a new MCP server + s := server.NewMCPServer( + "github-mcp-server", + version, + opts..., + ) + return s +} + +// OptionalParamOK is a helper function that can be used to fetch a requested parameter from the request. +// It returns the value, a boolean indicating if the parameter was present, and an error if the type is wrong. +func OptionalParamOK[T any](r mcp.CallToolRequest, p string) (value T, ok bool, err error) { + // Check if the parameter is present in the request + val, exists := r.Params.Arguments[p] + if !exists { + // Not present, return zero value, false, no error + return + } + + // Check if the parameter is of the expected type + value, ok = val.(T) + if !ok { + // Present but wrong type + err = fmt.Errorf("parameter %s is not of type %T, is %T", p, value, val) + ok = true // Set ok to true because the parameter *was* present, even if wrong type + return + } + + // Present and correct type + ok = true + return +} + +// isAcceptedError checks if the error is an accepted error. +func isAcceptedError(err error) bool { + var acceptedError *github.AcceptedError + return errors.As(err, &acceptedError) +} + +// requiredParam is a helper function that can be used to fetch a requested parameter from the request. +// It does the following checks: +// 1. Checks if the parameter is present in the request. +// 2. Checks if the parameter is of the expected type. +// 3. Checks if the parameter is not empty, i.e: non-zero value +func requiredParam[T comparable](r mcp.CallToolRequest, p string) (T, error) { + var zero T + + // Check if the parameter is present in the request + if _, ok := r.Params.Arguments[p]; !ok { + return zero, fmt.Errorf("missing required parameter: %s", p) + } + + // Check if the parameter is of the expected type + if _, ok := r.Params.Arguments[p].(T); !ok { + return zero, fmt.Errorf("parameter %s is not of type %T", p, zero) + } + + if r.Params.Arguments[p].(T) == zero { + return zero, fmt.Errorf("missing required parameter: %s", p) + + } + + return r.Params.Arguments[p].(T), nil +} + +// RequiredInt is a helper function that can be used to fetch a requested parameter from the request. +// It does the following checks: +// 1. Checks if the parameter is present in the request. +// 2. Checks if the parameter is of the expected type. +// 3. Checks if the parameter is not empty, i.e: non-zero value +func RequiredInt(r mcp.CallToolRequest, p string) (int, error) { + v, err := requiredParam[float64](r, p) + if err != nil { + return 0, err + } + return int(v), nil +} + +// OptionalParam is a helper function that can be used to fetch a requested parameter from the request. +// It does the following checks: +// 1. Checks if the parameter is present in the request, if not, it returns its zero-value +// 2. If it is present, it checks if the parameter is of the expected type and returns it +func OptionalParam[T any](r mcp.CallToolRequest, p string) (T, error) { + var zero T + + // Check if the parameter is present in the request + if _, ok := r.Params.Arguments[p]; !ok { + return zero, nil + } + + // Check if the parameter is of the expected type + if _, ok := r.Params.Arguments[p].(T); !ok { + return zero, fmt.Errorf("parameter %s is not of type %T, is %T", p, zero, r.Params.Arguments[p]) + } + + return r.Params.Arguments[p].(T), nil +} + +// OptionalIntParam is a helper function that can be used to fetch a requested parameter from the request. +// It does the following checks: +// 1. Checks if the parameter is present in the request, if not, it returns its zero-value +// 2. If it is present, it checks if the parameter is of the expected type and returns it +func OptionalIntParam(r mcp.CallToolRequest, p string) (int, error) { + v, err := OptionalParam[float64](r, p) + if err != nil { + return 0, err + } + return int(v), nil +} + +// OptionalIntParamWithDefault is a helper function that can be used to fetch a requested parameter from the request +// similar to optionalIntParam, but it also takes a default value. +func OptionalIntParamWithDefault(r mcp.CallToolRequest, p string, d int) (int, error) { + v, err := OptionalIntParam(r, p) + if err != nil { + return 0, err + } + if v == 0 { + return d, nil + } + return v, nil +} + +// OptionalStringArrayParam is a helper function that can be used to fetch a requested parameter from the request. +// It does the following checks: +// 1. Checks if the parameter is present in the request, if not, it returns its zero-value +// 2. If it is present, iterates the elements and checks each is a string +func OptionalStringArrayParam(r mcp.CallToolRequest, p string) ([]string, error) { + // Check if the parameter is present in the request + if _, ok := r.Params.Arguments[p]; !ok { + return []string{}, nil + } + + switch v := r.Params.Arguments[p].(type) { + case nil: + return []string{}, nil + case []string: + return v, nil + case []any: + strSlice := make([]string, len(v)) + for i, v := range v { + s, ok := v.(string) + if !ok { + return []string{}, fmt.Errorf("parameter %s is not of type string, is %T", p, v) + } + strSlice[i] = s + } + return strSlice, nil + default: + return []string{}, fmt.Errorf("parameter %s could not be coerced to []string, is %T", p, r.Params.Arguments[p]) + } +} + +// WithPagination returns a ToolOption that adds "page" and "perPage" parameters to the tool. +// The "page" parameter is optional, min 1. The "perPage" parameter is optional, min 1, max 100. +func WithPagination() mcp.ToolOption { + return func(tool *mcp.Tool) { + mcp.WithNumber("page", + mcp.Description("Page number for pagination (min 1)"), + mcp.Min(1), + )(tool) + + mcp.WithNumber("perPage", + mcp.Description("Results per page for pagination (min 1, max 100)"), + mcp.Min(1), + mcp.Max(100), + )(tool) + } +} + +type PaginationParams struct { + page int + perPage int +} + +// OptionalPaginationParams returns the "page" and "perPage" parameters from the request, +// or their default values if not present, "page" default is 1, "perPage" default is 30. +// In future, we may want to make the default values configurable, or even have this +// function returned from `withPagination`, where the defaults are provided alongside +// the min/max values. +func OptionalPaginationParams(r mcp.CallToolRequest) (PaginationParams, error) { + page, err := OptionalIntParamWithDefault(r, "page", 1) + if err != nil { + return PaginationParams{}, err + } + perPage, err := OptionalIntParamWithDefault(r, "perPage", 30) + if err != nil { + return PaginationParams{}, err + } + return PaginationParams{ + page: page, + perPage: perPage, + }, nil +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/server_test.go b/sre_agent/servers/github-mcp-server/pkg/github/server_test.go new file mode 100644 index 0000000..58bcb9d --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/server_test.go @@ -0,0 +1,519 @@ +package github + +import ( + "context" + "fmt" + "testing" + + "github.com/google/go-github/v69/github" + "github.com/stretchr/testify/assert" +) + +func stubGetClientFn(client *github.Client) GetClientFn { + return func(_ context.Context) (*github.Client, error) { + return client, nil + } +} + +func Test_IsAcceptedError(t *testing.T) { + tests := []struct { + name string + err error + expectAccepted bool + }{ + { + name: "github AcceptedError", + err: &github.AcceptedError{}, + expectAccepted: true, + }, + { + name: "regular error", + err: fmt.Errorf("some other error"), + expectAccepted: false, + }, + { + name: "nil error", + err: nil, + expectAccepted: false, + }, + { + name: "wrapped AcceptedError", + err: fmt.Errorf("wrapped: %w", &github.AcceptedError{}), + expectAccepted: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + result := isAcceptedError(tc.err) + assert.Equal(t, tc.expectAccepted, result) + }) + } +} + +func Test_RequiredStringParam(t *testing.T) { + tests := []struct { + name string + params map[string]interface{} + paramName string + expected string + expectError bool + }{ + { + name: "valid string parameter", + params: map[string]interface{}{"name": "test-value"}, + paramName: "name", + expected: "test-value", + expectError: false, + }, + { + name: "missing parameter", + params: map[string]interface{}{}, + paramName: "name", + expected: "", + expectError: true, + }, + { + name: "empty string parameter", + params: map[string]interface{}{"name": ""}, + paramName: "name", + expected: "", + expectError: true, + }, + { + name: "wrong type parameter", + params: map[string]interface{}{"name": 123}, + paramName: "name", + expected: "", + expectError: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + request := createMCPRequest(tc.params) + result, err := requiredParam[string](request, tc.paramName) + + if tc.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) + } + }) + } +} + +func Test_OptionalStringParam(t *testing.T) { + tests := []struct { + name string + params map[string]interface{} + paramName string + expected string + expectError bool + }{ + { + name: "valid string parameter", + params: map[string]interface{}{"name": "test-value"}, + paramName: "name", + expected: "test-value", + expectError: false, + }, + { + name: "missing parameter", + params: map[string]interface{}{}, + paramName: "name", + expected: "", + expectError: false, + }, + { + name: "empty string parameter", + params: map[string]interface{}{"name": ""}, + paramName: "name", + expected: "", + expectError: false, + }, + { + name: "wrong type parameter", + params: map[string]interface{}{"name": 123}, + paramName: "name", + expected: "", + expectError: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + request := createMCPRequest(tc.params) + result, err := OptionalParam[string](request, tc.paramName) + + if tc.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) + } + }) + } +} + +func Test_RequiredNumberParam(t *testing.T) { + tests := []struct { + name string + params map[string]interface{} + paramName string + expected int + expectError bool + }{ + { + name: "valid number parameter", + params: map[string]interface{}{"count": float64(42)}, + paramName: "count", + expected: 42, + expectError: false, + }, + { + name: "missing parameter", + params: map[string]interface{}{}, + paramName: "count", + expected: 0, + expectError: true, + }, + { + name: "wrong type parameter", + params: map[string]interface{}{"count": "not-a-number"}, + paramName: "count", + expected: 0, + expectError: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + request := createMCPRequest(tc.params) + result, err := RequiredInt(request, tc.paramName) + + if tc.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) + } + }) + } +} + +func Test_OptionalNumberParam(t *testing.T) { + tests := []struct { + name string + params map[string]interface{} + paramName string + expected int + expectError bool + }{ + { + name: "valid number parameter", + params: map[string]interface{}{"count": float64(42)}, + paramName: "count", + expected: 42, + expectError: false, + }, + { + name: "missing parameter", + params: map[string]interface{}{}, + paramName: "count", + expected: 0, + expectError: false, + }, + { + name: "zero value", + params: map[string]interface{}{"count": float64(0)}, + paramName: "count", + expected: 0, + expectError: false, + }, + { + name: "wrong type parameter", + params: map[string]interface{}{"count": "not-a-number"}, + paramName: "count", + expected: 0, + expectError: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + request := createMCPRequest(tc.params) + result, err := OptionalIntParam(request, tc.paramName) + + if tc.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) + } + }) + } +} + +func Test_OptionalNumberParamWithDefault(t *testing.T) { + tests := []struct { + name string + params map[string]interface{} + paramName string + defaultVal int + expected int + expectError bool + }{ + { + name: "valid number parameter", + params: map[string]interface{}{"count": float64(42)}, + paramName: "count", + defaultVal: 10, + expected: 42, + expectError: false, + }, + { + name: "missing parameter", + params: map[string]interface{}{}, + paramName: "count", + defaultVal: 10, + expected: 10, + expectError: false, + }, + { + name: "zero value", + params: map[string]interface{}{"count": float64(0)}, + paramName: "count", + defaultVal: 10, + expected: 10, + expectError: false, + }, + { + name: "wrong type parameter", + params: map[string]interface{}{"count": "not-a-number"}, + paramName: "count", + defaultVal: 10, + expected: 0, + expectError: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + request := createMCPRequest(tc.params) + result, err := OptionalIntParamWithDefault(request, tc.paramName, tc.defaultVal) + + if tc.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) + } + }) + } +} + +func Test_OptionalBooleanParam(t *testing.T) { + tests := []struct { + name string + params map[string]interface{} + paramName string + expected bool + expectError bool + }{ + { + name: "true value", + params: map[string]interface{}{"flag": true}, + paramName: "flag", + expected: true, + expectError: false, + }, + { + name: "false value", + params: map[string]interface{}{"flag": false}, + paramName: "flag", + expected: false, + expectError: false, + }, + { + name: "missing parameter", + params: map[string]interface{}{}, + paramName: "flag", + expected: false, + expectError: false, + }, + { + name: "wrong type parameter", + params: map[string]interface{}{"flag": "not-a-boolean"}, + paramName: "flag", + expected: false, + expectError: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + request := createMCPRequest(tc.params) + result, err := OptionalParam[bool](request, tc.paramName) + + if tc.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) + } + }) + } +} + +func TestOptionalStringArrayParam(t *testing.T) { + tests := []struct { + name string + params map[string]interface{} + paramName string + expected []string + expectError bool + }{ + { + name: "parameter not in request", + params: map[string]any{}, + paramName: "flag", + expected: []string{}, + expectError: false, + }, + { + name: "valid any array parameter", + params: map[string]any{ + "flag": []any{"v1", "v2"}, + }, + paramName: "flag", + expected: []string{"v1", "v2"}, + expectError: false, + }, + { + name: "valid string array parameter", + params: map[string]any{ + "flag": []string{"v1", "v2"}, + }, + paramName: "flag", + expected: []string{"v1", "v2"}, + expectError: false, + }, + { + name: "wrong type parameter", + params: map[string]any{ + "flag": 1, + }, + paramName: "flag", + expected: []string{}, + expectError: true, + }, + { + name: "wrong slice type parameter", + params: map[string]any{ + "flag": []any{"foo", 2}, + }, + paramName: "flag", + expected: []string{}, + expectError: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + request := createMCPRequest(tc.params) + result, err := OptionalStringArrayParam(request, tc.paramName) + + if tc.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) + } + }) + } +} + +func TestOptionalPaginationParams(t *testing.T) { + tests := []struct { + name string + params map[string]any + expected PaginationParams + expectError bool + }{ + { + name: "no pagination parameters, default values", + params: map[string]any{}, + expected: PaginationParams{ + page: 1, + perPage: 30, + }, + expectError: false, + }, + { + name: "page parameter, default perPage", + params: map[string]any{ + "page": float64(2), + }, + expected: PaginationParams{ + page: 2, + perPage: 30, + }, + expectError: false, + }, + { + name: "perPage parameter, default page", + params: map[string]any{ + "perPage": float64(50), + }, + expected: PaginationParams{ + page: 1, + perPage: 50, + }, + expectError: false, + }, + { + name: "page and perPage parameters", + params: map[string]any{ + "page": float64(2), + "perPage": float64(50), + }, + expected: PaginationParams{ + page: 2, + perPage: 50, + }, + expectError: false, + }, + { + name: "invalid page parameter", + params: map[string]any{ + "page": "not-a-number", + }, + expected: PaginationParams{}, + expectError: true, + }, + { + name: "invalid perPage parameter", + params: map[string]any{ + "perPage": "not-a-number", + }, + expected: PaginationParams{}, + expectError: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + request := createMCPRequest(tc.params) + result, err := OptionalPaginationParams(request) + + if tc.expectError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) + } + }) + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/github/tools.go b/sre_agent/servers/github-mcp-server/pkg/github/tools.go new file mode 100644 index 0000000..3776a12 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/github/tools.go @@ -0,0 +1,126 @@ +package github + +import ( + "context" + + "github.com/github/github-mcp-server/pkg/toolsets" + "github.com/github/github-mcp-server/pkg/translations" + "github.com/google/go-github/v69/github" + "github.com/mark3labs/mcp-go/server" +) + +type GetClientFn func(context.Context) (*github.Client, error) + +var DefaultTools = []string{"all"} + +func InitToolsets(passedToolsets []string, readOnly bool, getClient GetClientFn, t translations.TranslationHelperFunc) (*toolsets.ToolsetGroup, error) { + // Create a new toolset group + tsg := toolsets.NewToolsetGroup(readOnly) + + // Define all available features with their default state (disabled) + // Create toolsets + repos := toolsets.NewToolset("repos", "GitHub Repository related tools"). + AddReadTools( + toolsets.NewServerTool(SearchRepositories(getClient, t)), + toolsets.NewServerTool(GetFileContents(getClient, t)), + toolsets.NewServerTool(ListCommits(getClient, t)), + toolsets.NewServerTool(SearchCode(getClient, t)), + toolsets.NewServerTool(GetCommit(getClient, t)), + toolsets.NewServerTool(ListBranches(getClient, t)), + toolsets.NewServerTool(ListTags(getClient, t)), + toolsets.NewServerTool(GetTag(getClient, t)), + ). + AddWriteTools( + toolsets.NewServerTool(CreateOrUpdateFile(getClient, t)), + toolsets.NewServerTool(CreateRepository(getClient, t)), + toolsets.NewServerTool(ForkRepository(getClient, t)), + toolsets.NewServerTool(CreateBranch(getClient, t)), + toolsets.NewServerTool(PushFiles(getClient, t)), + ) + issues := toolsets.NewToolset("issues", "GitHub Issues related tools"). + AddReadTools( + toolsets.NewServerTool(GetIssue(getClient, t)), + toolsets.NewServerTool(SearchIssues(getClient, t)), + toolsets.NewServerTool(ListIssues(getClient, t)), + toolsets.NewServerTool(GetIssueComments(getClient, t)), + ). + AddWriteTools( + toolsets.NewServerTool(CreateIssue(getClient, t)), + toolsets.NewServerTool(AddIssueComment(getClient, t)), + toolsets.NewServerTool(UpdateIssue(getClient, t)), + ) + users := toolsets.NewToolset("users", "GitHub User related tools"). + AddReadTools( + toolsets.NewServerTool(SearchUsers(getClient, t)), + ) + pullRequests := toolsets.NewToolset("pull_requests", "GitHub Pull Request related tools"). + AddReadTools( + toolsets.NewServerTool(GetPullRequest(getClient, t)), + toolsets.NewServerTool(ListPullRequests(getClient, t)), + toolsets.NewServerTool(GetPullRequestFiles(getClient, t)), + toolsets.NewServerTool(GetPullRequestStatus(getClient, t)), + toolsets.NewServerTool(GetPullRequestComments(getClient, t)), + toolsets.NewServerTool(GetPullRequestReviews(getClient, t)), + ). + AddWriteTools( + toolsets.NewServerTool(MergePullRequest(getClient, t)), + toolsets.NewServerTool(UpdatePullRequestBranch(getClient, t)), + toolsets.NewServerTool(CreatePullRequestReview(getClient, t)), + toolsets.NewServerTool(CreatePullRequest(getClient, t)), + toolsets.NewServerTool(UpdatePullRequest(getClient, t)), + toolsets.NewServerTool(AddPullRequestReviewComment(getClient, t)), + ) + codeSecurity := toolsets.NewToolset("code_security", "Code security related tools, such as GitHub Code Scanning"). + AddReadTools( + toolsets.NewServerTool(GetCodeScanningAlert(getClient, t)), + toolsets.NewServerTool(ListCodeScanningAlerts(getClient, t)), + ) + secretProtection := toolsets.NewToolset("secret_protection", "Secret protection related tools, such as GitHub Secret Scanning"). + AddReadTools( + toolsets.NewServerTool(GetSecretScanningAlert(getClient, t)), + toolsets.NewServerTool(ListSecretScanningAlerts(getClient, t)), + ) + // Keep experiments alive so the system doesn't error out when it's always enabled + experiments := toolsets.NewToolset("experiments", "Experimental features that are not considered stable yet") + + // Add toolsets to the group + tsg.AddToolset(repos) + tsg.AddToolset(issues) + tsg.AddToolset(users) + tsg.AddToolset(pullRequests) + tsg.AddToolset(codeSecurity) + tsg.AddToolset(secretProtection) + tsg.AddToolset(experiments) + // Enable the requested features + + if err := tsg.EnableToolsets(passedToolsets); err != nil { + return nil, err + } + + return tsg, nil +} + +func InitContextToolset(getClient GetClientFn, t translations.TranslationHelperFunc) *toolsets.Toolset { + // Create a new context toolset + contextTools := toolsets.NewToolset("context", "Tools that provide context about the current user and GitHub context you are operating in"). + AddReadTools( + toolsets.NewServerTool(GetMe(getClient, t)), + ) + contextTools.Enabled = true + return contextTools +} + +// InitDynamicToolset creates a dynamic toolset that can be used to enable other toolsets, and so requires the server and toolset group as arguments +func InitDynamicToolset(s *server.MCPServer, tsg *toolsets.ToolsetGroup, t translations.TranslationHelperFunc) *toolsets.Toolset { + // Create a new dynamic toolset + // Need to add the dynamic toolset last so it can be used to enable other toolsets + dynamicToolSelection := toolsets.NewToolset("dynamic", "Discover GitHub MCP tools that can help achieve tasks by enabling additional sets of tools, you can control the enablement of any toolset to access its tools when this toolset is enabled."). + AddReadTools( + toolsets.NewServerTool(ListAvailableToolsets(tsg, t)), + toolsets.NewServerTool(GetToolsetsTools(tsg, t)), + toolsets.NewServerTool(EnableToolset(s, tsg, t)), + ) + + dynamicToolSelection.Enabled = true + return dynamicToolSelection +} diff --git a/sre_agent/servers/github-mcp-server/pkg/log/io.go b/sre_agent/servers/github-mcp-server/pkg/log/io.go new file mode 100644 index 0000000..de22102 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/log/io.go @@ -0,0 +1,45 @@ +package log + +import ( + "io" + + log "github.com/sirupsen/logrus" +) + +// IOLogger is a wrapper around io.Reader and io.Writer that can be used +// to log the data being read and written from the underlying streams +type IOLogger struct { + reader io.Reader + writer io.Writer + logger *log.Logger +} + +// NewIOLogger creates a new IOLogger instance +func NewIOLogger(r io.Reader, w io.Writer, logger *log.Logger) *IOLogger { + return &IOLogger{ + reader: r, + writer: w, + logger: logger, + } +} + +// Read reads data from the underlying io.Reader and logs it. +func (l *IOLogger) Read(p []byte) (n int, err error) { + if l.reader == nil { + return 0, io.EOF + } + n, err = l.reader.Read(p) + if n > 0 { + l.logger.Infof("[stdin]: received %d bytes: %s", n, string(p[:n])) + } + return n, err +} + +// Write writes data to the underlying io.Writer and logs it. +func (l *IOLogger) Write(p []byte) (n int, err error) { + if l.writer == nil { + return 0, io.ErrClosedPipe + } + l.logger.Infof("[stdout]: sending %d bytes: %s", len(p), string(p)) + return l.writer.Write(p) +} diff --git a/sre_agent/servers/github-mcp-server/pkg/log/io_test.go b/sre_agent/servers/github-mcp-server/pkg/log/io_test.go new file mode 100644 index 0000000..0d0cd89 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/log/io_test.go @@ -0,0 +1,65 @@ +package log + +import ( + "bytes" + "strings" + "testing" + + log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" +) + +func TestLoggedReadWriter(t *testing.T) { + t.Run("Read method logs and passes data", func(t *testing.T) { + // Setup + inputData := "test input data" + reader := strings.NewReader(inputData) + + // Create logger with buffer to capture output + var logBuffer bytes.Buffer + logger := log.New() + logger.SetOutput(&logBuffer) + logger.SetFormatter(&log.TextFormatter{ + DisableTimestamp: true, + }) + + lrw := NewIOLogger(reader, nil, logger) + + // Test Read + buf := make([]byte, 100) + n, err := lrw.Read(buf) + + // Assertions + assert.NoError(t, err) + assert.Equal(t, len(inputData), n) + assert.Equal(t, inputData, string(buf[:n])) + assert.Contains(t, logBuffer.String(), "[stdin]") + assert.Contains(t, logBuffer.String(), inputData) + }) + + t.Run("Write method logs and passes data", func(t *testing.T) { + // Setup + outputData := "test output data" + var writeBuffer bytes.Buffer + + // Create logger with buffer to capture output + var logBuffer bytes.Buffer + logger := log.New() + logger.SetOutput(&logBuffer) + logger.SetFormatter(&log.TextFormatter{ + DisableTimestamp: true, + }) + + lrw := NewIOLogger(nil, &writeBuffer, logger) + + // Test Write + n, err := lrw.Write([]byte(outputData)) + + // Assertions + assert.NoError(t, err) + assert.Equal(t, len(outputData), n) + assert.Equal(t, outputData, writeBuffer.String()) + assert.Contains(t, logBuffer.String(), "[stdout]") + assert.Contains(t, logBuffer.String(), outputData) + }) +} diff --git a/sre_agent/servers/github-mcp-server/pkg/toolsets/toolsets.go b/sre_agent/servers/github-mcp-server/pkg/toolsets/toolsets.go new file mode 100644 index 0000000..b316aae --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/toolsets/toolsets.go @@ -0,0 +1,168 @@ +package toolsets + +import ( + "fmt" + + "github.com/mark3labs/mcp-go/mcp" + "github.com/mark3labs/mcp-go/server" +) + +func NewServerTool(tool mcp.Tool, handler server.ToolHandlerFunc) server.ServerTool { + return server.ServerTool{Tool: tool, Handler: handler} +} + +type Toolset struct { + Name string + Description string + Enabled bool + readOnly bool + writeTools []server.ServerTool + readTools []server.ServerTool +} + +func (t *Toolset) GetActiveTools() []server.ServerTool { + if t.Enabled { + if t.readOnly { + return t.readTools + } + return append(t.readTools, t.writeTools...) + } + return nil +} + +func (t *Toolset) GetAvailableTools() []server.ServerTool { + if t.readOnly { + return t.readTools + } + return append(t.readTools, t.writeTools...) +} + +func (t *Toolset) RegisterTools(s *server.MCPServer) { + if !t.Enabled { + return + } + for _, tool := range t.readTools { + s.AddTool(tool.Tool, tool.Handler) + } + if !t.readOnly { + for _, tool := range t.writeTools { + s.AddTool(tool.Tool, tool.Handler) + } + } +} + +func (t *Toolset) SetReadOnly() { + // Set the toolset to read-only + t.readOnly = true +} + +func (t *Toolset) AddWriteTools(tools ...server.ServerTool) *Toolset { + // Silently ignore if the toolset is read-only to avoid any breach of that contract + for _, tool := range tools { + if tool.Tool.Annotations.ReadOnlyHint { + panic(fmt.Sprintf("tool (%s) is incorrectly annotated as read-only", tool.Tool.Name)) + } + } + if !t.readOnly { + t.writeTools = append(t.writeTools, tools...) + } + return t +} + +func (t *Toolset) AddReadTools(tools ...server.ServerTool) *Toolset { + for _, tool := range tools { + if !tool.Tool.Annotations.ReadOnlyHint { + panic(fmt.Sprintf("tool (%s) must be annotated as read-only", tool.Tool.Name)) + } + tool.Tool.Annotations = mcp.ToolAnnotation{ + ReadOnlyHint: true, + Title: tool.Tool.Annotations.Title, + } + } + t.readTools = append(t.readTools, tools...) + return t +} + +type ToolsetGroup struct { + Toolsets map[string]*Toolset + everythingOn bool + readOnly bool +} + +func NewToolsetGroup(readOnly bool) *ToolsetGroup { + return &ToolsetGroup{ + Toolsets: make(map[string]*Toolset), + everythingOn: false, + readOnly: readOnly, + } +} + +func (tg *ToolsetGroup) AddToolset(ts *Toolset) { + if tg.readOnly { + ts.SetReadOnly() + } + tg.Toolsets[ts.Name] = ts +} + +func NewToolset(name string, description string) *Toolset { + return &Toolset{ + Name: name, + Description: description, + Enabled: false, + readOnly: false, + } +} + +func (tg *ToolsetGroup) IsEnabled(name string) bool { + // If everythingOn is true, all features are enabled + if tg.everythingOn { + return true + } + + feature, exists := tg.Toolsets[name] + if !exists { + return false + } + return feature.Enabled +} + +func (tg *ToolsetGroup) EnableToolsets(names []string) error { + // Special case for "all" + for _, name := range names { + if name == "all" { + tg.everythingOn = true + break + } + err := tg.EnableToolset(name) + if err != nil { + return err + } + } + // Do this after to ensure all toolsets are enabled if "all" is present anywhere in list + if tg.everythingOn { + for name := range tg.Toolsets { + err := tg.EnableToolset(name) + if err != nil { + return err + } + } + return nil + } + return nil +} + +func (tg *ToolsetGroup) EnableToolset(name string) error { + toolset, exists := tg.Toolsets[name] + if !exists { + return fmt.Errorf("toolset %s does not exist", name) + } + toolset.Enabled = true + tg.Toolsets[name] = toolset + return nil +} + +func (tg *ToolsetGroup) RegisterTools(s *server.MCPServer) { + for _, toolset := range tg.Toolsets { + toolset.RegisterTools(s) + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/toolsets/toolsets_test.go b/sre_agent/servers/github-mcp-server/pkg/toolsets/toolsets_test.go new file mode 100644 index 0000000..7ece1df --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/toolsets/toolsets_test.go @@ -0,0 +1,230 @@ +package toolsets + +import ( + "testing" +) + +func TestNewToolsetGroup(t *testing.T) { + tsg := NewToolsetGroup(false) + if tsg == nil { + t.Fatal("Expected NewToolsetGroup to return a non-nil pointer") + } + if tsg.Toolsets == nil { + t.Fatal("Expected Toolsets map to be initialized") + } + if len(tsg.Toolsets) != 0 { + t.Fatalf("Expected Toolsets map to be empty, got %d items", len(tsg.Toolsets)) + } + if tsg.everythingOn { + t.Fatal("Expected everythingOn to be initialized as false") + } +} + +func TestAddToolset(t *testing.T) { + tsg := NewToolsetGroup(false) + + // Test adding a toolset + toolset := NewToolset("test-toolset", "A test toolset") + toolset.Enabled = true + tsg.AddToolset(toolset) + + // Verify toolset was added correctly + if len(tsg.Toolsets) != 1 { + t.Errorf("Expected 1 toolset, got %d", len(tsg.Toolsets)) + } + + toolset, exists := tsg.Toolsets["test-toolset"] + if !exists { + t.Fatal("Feature was not added to the map") + } + + if toolset.Name != "test-toolset" { + t.Errorf("Expected toolset name to be 'test-toolset', got '%s'", toolset.Name) + } + + if toolset.Description != "A test toolset" { + t.Errorf("Expected toolset description to be 'A test toolset', got '%s'", toolset.Description) + } + + if !toolset.Enabled { + t.Error("Expected toolset to be enabled") + } + + // Test adding another toolset + anotherToolset := NewToolset("another-toolset", "Another test toolset") + tsg.AddToolset(anotherToolset) + + if len(tsg.Toolsets) != 2 { + t.Errorf("Expected 2 toolsets, got %d", len(tsg.Toolsets)) + } + + // Test overriding existing toolset + updatedToolset := NewToolset("test-toolset", "Updated description") + tsg.AddToolset(updatedToolset) + + toolset = tsg.Toolsets["test-toolset"] + if toolset.Description != "Updated description" { + t.Errorf("Expected toolset description to be updated to 'Updated description', got '%s'", toolset.Description) + } + + if toolset.Enabled { + t.Error("Expected toolset to be disabled after update") + } +} + +func TestIsEnabled(t *testing.T) { + tsg := NewToolsetGroup(false) + + // Test with non-existent toolset + if tsg.IsEnabled("non-existent") { + t.Error("Expected IsEnabled to return false for non-existent toolset") + } + + // Test with disabled toolset + disabledToolset := NewToolset("disabled-toolset", "A disabled toolset") + tsg.AddToolset(disabledToolset) + if tsg.IsEnabled("disabled-toolset") { + t.Error("Expected IsEnabled to return false for disabled toolset") + } + + // Test with enabled toolset + enabledToolset := NewToolset("enabled-toolset", "An enabled toolset") + enabledToolset.Enabled = true + tsg.AddToolset(enabledToolset) + if !tsg.IsEnabled("enabled-toolset") { + t.Error("Expected IsEnabled to return true for enabled toolset") + } +} + +func TestEnableFeature(t *testing.T) { + tsg := NewToolsetGroup(false) + + // Test enabling non-existent toolset + err := tsg.EnableToolset("non-existent") + if err == nil { + t.Error("Expected error when enabling non-existent toolset") + } + + // Test enabling toolset + testToolset := NewToolset("test-toolset", "A test toolset") + tsg.AddToolset(testToolset) + + if tsg.IsEnabled("test-toolset") { + t.Error("Expected toolset to be disabled initially") + } + + err = tsg.EnableToolset("test-toolset") + if err != nil { + t.Errorf("Expected no error when enabling toolset, got: %v", err) + } + + if !tsg.IsEnabled("test-toolset") { + t.Error("Expected toolset to be enabled after EnableFeature call") + } + + // Test enabling already enabled toolset + err = tsg.EnableToolset("test-toolset") + if err != nil { + t.Errorf("Expected no error when enabling already enabled toolset, got: %v", err) + } +} + +func TestEnableToolsets(t *testing.T) { + tsg := NewToolsetGroup(false) + + // Prepare toolsets + toolset1 := NewToolset("toolset1", "Feature 1") + toolset2 := NewToolset("toolset2", "Feature 2") + tsg.AddToolset(toolset1) + tsg.AddToolset(toolset2) + + // Test enabling multiple toolsets + err := tsg.EnableToolsets([]string{"toolset1", "toolset2"}) + if err != nil { + t.Errorf("Expected no error when enabling toolsets, got: %v", err) + } + + if !tsg.IsEnabled("toolset1") { + t.Error("Expected toolset1 to be enabled") + } + + if !tsg.IsEnabled("toolset2") { + t.Error("Expected toolset2 to be enabled") + } + + // Test with non-existent toolset in the list + err = tsg.EnableToolsets([]string{"toolset1", "non-existent"}) + if err == nil { + t.Error("Expected error when enabling list with non-existent toolset") + } + + // Test with empty list + err = tsg.EnableToolsets([]string{}) + if err != nil { + t.Errorf("Expected no error with empty toolset list, got: %v", err) + } + + // Test enabling everything through EnableToolsets + tsg = NewToolsetGroup(false) + err = tsg.EnableToolsets([]string{"all"}) + if err != nil { + t.Errorf("Expected no error when enabling 'all', got: %v", err) + } + + if !tsg.everythingOn { + t.Error("Expected everythingOn to be true after enabling 'all' via EnableToolsets") + } +} + +func TestEnableEverything(t *testing.T) { + tsg := NewToolsetGroup(false) + + // Add a disabled toolset + testToolset := NewToolset("test-toolset", "A test toolset") + tsg.AddToolset(testToolset) + + // Verify it's disabled + if tsg.IsEnabled("test-toolset") { + t.Error("Expected toolset to be disabled initially") + } + + // Enable "all" + err := tsg.EnableToolsets([]string{"all"}) + if err != nil { + t.Errorf("Expected no error when enabling 'eall', got: %v", err) + } + + // Verify everythingOn was set + if !tsg.everythingOn { + t.Error("Expected everythingOn to be true after enabling 'eall'") + } + + // Verify the previously disabled toolset is now enabled + if !tsg.IsEnabled("test-toolset") { + t.Error("Expected toolset to be enabled when everythingOn is true") + } + + // Verify a non-existent toolset is also enabled + if !tsg.IsEnabled("non-existent") { + t.Error("Expected non-existent toolset to be enabled when everythingOn is true") + } +} + +func TestIsEnabledWithEverythingOn(t *testing.T) { + tsg := NewToolsetGroup(false) + + // Enable "everything" + err := tsg.EnableToolsets([]string{"all"}) + if err != nil { + t.Errorf("Expected no error when enabling 'all', got: %v", err) + } + + // Test that any toolset name returns true with IsEnabled + if !tsg.IsEnabled("some-toolset") { + t.Error("Expected IsEnabled to return true for any toolset when everythingOn is true") + } + + if !tsg.IsEnabled("another-toolset") { + t.Error("Expected IsEnabled to return true for any toolset when everythingOn is true") + } +} diff --git a/sre_agent/servers/github-mcp-server/pkg/translations/translations.go b/sre_agent/servers/github-mcp-server/pkg/translations/translations.go new file mode 100644 index 0000000..741ee2b --- /dev/null +++ b/sre_agent/servers/github-mcp-server/pkg/translations/translations.go @@ -0,0 +1,79 @@ +package translations + +import ( + "encoding/json" + "fmt" + "log" + "os" + "strings" + + "github.com/spf13/viper" +) + +type TranslationHelperFunc func(key string, defaultValue string) string + +func NullTranslationHelper(_ string, defaultValue string) string { + return defaultValue +} + +func TranslationHelper() (TranslationHelperFunc, func()) { + var translationKeyMap = map[string]string{} + v := viper.New() + + // Load from JSON file + v.SetConfigName("github-mcp-server-config") + v.SetConfigType("json") + v.AddConfigPath(".") + + if err := v.ReadInConfig(); err != nil { + // ignore error if file not found as it is not required + if _, ok := err.(viper.ConfigFileNotFoundError); !ok { + log.Printf("Could not read JSON config: %v", err) + } + } + + // create a function that takes both a key, and a default value and returns either the default value or an override value + return func(key string, defaultValue string) string { + key = strings.ToUpper(key) + if value, exists := translationKeyMap[key]; exists { + return value + } + // check if the env var exists + if value, exists := os.LookupEnv("GITHUB_MCP_" + key); exists { + // TODO I could not get Viper to play ball reading the env var + translationKeyMap[key] = value + return value + } + + v.SetDefault(key, defaultValue) + translationKeyMap[key] = v.GetString(key) + return translationKeyMap[key] + }, func() { + // dump the translationKeyMap to a json file + if err := DumpTranslationKeyMap(translationKeyMap); err != nil { + log.Fatalf("Could not dump translation key map: %v", err) + } + } +} + +// dump translationKeyMap to a json file called github-mcp-server-config.json +func DumpTranslationKeyMap(translationKeyMap map[string]string) error { + file, err := os.Create("github-mcp-server-config.json") + if err != nil { + return fmt.Errorf("error creating file: %v", err) + } + defer func() { _ = file.Close() }() + + // marshal the map to json + jsonData, err := json.MarshalIndent(translationKeyMap, "", " ") + if err != nil { + return fmt.Errorf("error marshaling map to JSON: %v", err) + } + + // write the json data to the file + if _, err := file.Write(jsonData); err != nil { + return fmt.Errorf("error writing to file: %v", err) + } + + return nil +} diff --git a/sre_agent/servers/github-mcp-server/script/get-me b/sre_agent/servers/github-mcp-server/script/get-me new file mode 100755 index 0000000..46339ae --- /dev/null +++ b/sre_agent/servers/github-mcp-server/script/get-me @@ -0,0 +1,3 @@ +#!/bin/bash + +echo '{"jsonrpc":"2.0","id":3,"params":{"name":"get_me"},"method":"tools/call"}' | go run cmd/github-mcp-server/main.go stdio | jq . diff --git a/sre_agent/servers/github-mcp-server/script/licenses b/sre_agent/servers/github-mcp-server/script/licenses new file mode 100755 index 0000000..51854d3 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/script/licenses @@ -0,0 +1,20 @@ +#!/bin/bash + +go install github.com/google/go-licenses@latest + +rm -rf third-party +mkdir -p third-party +export TEMPDIR="$(mktemp -d)" + +trap "rm -fr ${TEMPDIR}" EXIT + +for goos in linux darwin windows ; do + # Note: we ignore warnings because we want the command to succeed, however the output should be checked + # for any new warnings, and potentially we may need to add license information. + # + # Normally these warnings are packages containing non go code, which may or may not require explicit attribution, + # depending on the license. + GOOS="${goos}" go-licenses save ./... --save_path="${TEMPDIR}/${goos}" --force || echo "Ignore warnings" + GOOS="${goos}" go-licenses report ./... --template .github/licenses.tmpl > third-party-licenses.${goos}.md || echo "Ignore warnings" + cp -fR "${TEMPDIR}/${goos}"/* third-party/ +done diff --git a/sre_agent/servers/github-mcp-server/script/licenses-check b/sre_agent/servers/github-mcp-server/script/licenses-check new file mode 100755 index 0000000..680bd2a --- /dev/null +++ b/sre_agent/servers/github-mcp-server/script/licenses-check @@ -0,0 +1,18 @@ +#!/bin/bash + +go install github.com/google/go-licenses@latest + +for goos in linux darwin windows ; do + # Note: we ignore warnings because we want the command to succeed, however the output should be checked + # for any new warnings, and potentially we may need to add license information. + # + # Normally these warnings are packages containing non go code, which may or may not require explicit attribution, + # depending on the license. + GOOS="${goos}" go-licenses report ./... --template .github/licenses.tmpl > third-party-licenses.${goos}.copy.md || echo "Ignore warnings" + if ! diff -s third-party-licenses.${goos}.copy.md third-party-licenses.${goos}.md; then + echo "License check failed.\n\nPlease update the license file by running \`.script/licenses\` and committing the output." + rm -f third-party-licenses.${goos}.copy.md + exit 1 + fi + rm -f third-party-licenses.${goos}.copy.md +done diff --git a/sre_agent/servers/github-mcp-server/script/prettyprint-log b/sre_agent/servers/github-mcp-server/script/prettyprint-log new file mode 100755 index 0000000..16c6e4f --- /dev/null +++ b/sre_agent/servers/github-mcp-server/script/prettyprint-log @@ -0,0 +1,78 @@ +#!/bin/bash + +# Script to pretty print the output of the github-mcp-server +# log. +# +# It uses colored output when running on a terminal. + +# show script help +show_help() { + cat <&2 + exit 1 + fi + input="$1" +else + input="/dev/stdin" +fi + +# check if we are in a terminal for showing colors +if test -t 1; then + is_terminal="1" +else + is_terminal="0" +fi + +# Processs each log line, print whether is stdin or stdout, using different +# colors if we output to a terminal, and pretty print json data using jq +sed -nE 's/^.*\[(stdin|stdout)\]:.* ([0-9]+) bytes: (.*)\\n"$/\1 \2 \3/p' $input | +while read -r io bytes json; do + # Unescape the JSON string safely + unescaped=$(echo "$json" | awk '{ print "echo -e \"" $0 "\" | jq ." }' | bash) + echo "$(color $io)($bytes bytes):$(reset)" + echo "$unescaped" | jq . + echo +done diff --git a/sre_agent/servers/github-mcp-server/third-party-licenses.darwin.md b/sre_agent/servers/github-mcp-server/third-party-licenses.darwin.md new file mode 100644 index 0000000..cdb19b5 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party-licenses.darwin.md @@ -0,0 +1,32 @@ +# GitHub MCP Server dependencies + +The following open source dependencies are used to build the [github/github-mcp-server][] GitHub Model Context Protocol Server. + +## Go Packages + +Some packages may only be included on certain architectures or operating systems. + + + - [github.com/fsnotify/fsnotify](https://pkg.go.dev/github.com/fsnotify/fsnotify) ([BSD-3-Clause](https://github.com/fsnotify/fsnotify/blob/v1.8.0/LICENSE)) + - [github.com/github/github-mcp-server](https://pkg.go.dev/github.com/github/github-mcp-server) ([MIT](https://github.com/github/github-mcp-server/blob/HEAD/LICENSE)) + - [github.com/go-viper/mapstructure/v2](https://pkg.go.dev/github.com/go-viper/mapstructure/v2) ([MIT](https://github.com/go-viper/mapstructure/blob/v2.2.1/LICENSE)) + - [github.com/google/go-github/v69/github](https://pkg.go.dev/github.com/google/go-github/v69/github) ([BSD-3-Clause](https://github.com/google/go-github/blob/v69.2.0/LICENSE)) + - [github.com/google/go-querystring/query](https://pkg.go.dev/github.com/google/go-querystring/query) ([BSD-3-Clause](https://github.com/google/go-querystring/blob/v1.1.0/LICENSE)) + - [github.com/google/uuid](https://pkg.go.dev/github.com/google/uuid) ([BSD-3-Clause](https://github.com/google/uuid/blob/v1.6.0/LICENSE)) + - [github.com/mark3labs/mcp-go](https://pkg.go.dev/github.com/mark3labs/mcp-go) ([MIT](https://github.com/mark3labs/mcp-go/blob/v0.22.0/LICENSE)) + - [github.com/pelletier/go-toml/v2](https://pkg.go.dev/github.com/pelletier/go-toml/v2) ([MIT](https://github.com/pelletier/go-toml/blob/v2.2.3/LICENSE)) + - [github.com/sagikazarmark/locafero](https://pkg.go.dev/github.com/sagikazarmark/locafero) ([MIT](https://github.com/sagikazarmark/locafero/blob/v0.9.0/LICENSE)) + - [github.com/sirupsen/logrus](https://pkg.go.dev/github.com/sirupsen/logrus) ([MIT](https://github.com/sirupsen/logrus/blob/v1.9.3/LICENSE)) + - [github.com/sourcegraph/conc](https://pkg.go.dev/github.com/sourcegraph/conc) ([MIT](https://github.com/sourcegraph/conc/blob/v0.3.0/LICENSE)) + - [github.com/spf13/afero](https://pkg.go.dev/github.com/spf13/afero) ([Apache-2.0](https://github.com/spf13/afero/blob/v1.14.0/LICENSE.txt)) + - [github.com/spf13/cast](https://pkg.go.dev/github.com/spf13/cast) ([MIT](https://github.com/spf13/cast/blob/v1.7.1/LICENSE)) + - [github.com/spf13/cobra](https://pkg.go.dev/github.com/spf13/cobra) ([Apache-2.0](https://github.com/spf13/cobra/blob/v1.9.1/LICENSE.txt)) + - [github.com/spf13/pflag](https://pkg.go.dev/github.com/spf13/pflag) ([BSD-3-Clause](https://github.com/spf13/pflag/blob/v1.0.6/LICENSE)) + - [github.com/spf13/viper](https://pkg.go.dev/github.com/spf13/viper) ([MIT](https://github.com/spf13/viper/blob/v1.20.1/LICENSE)) + - [github.com/subosito/gotenv](https://pkg.go.dev/github.com/subosito/gotenv) ([MIT](https://github.com/subosito/gotenv/blob/v1.6.0/LICENSE)) + - [github.com/yosida95/uritemplate/v3](https://pkg.go.dev/github.com/yosida95/uritemplate/v3) ([BSD-3-Clause](https://github.com/yosida95/uritemplate/blob/v3.0.2/LICENSE)) + - [golang.org/x/sys/unix](https://pkg.go.dev/golang.org/x/sys/unix) ([BSD-3-Clause](https://cs.opensource.google/go/x/sys/+/v0.31.0:LICENSE)) + - [golang.org/x/text](https://pkg.go.dev/golang.org/x/text) ([BSD-3-Clause](https://cs.opensource.google/go/x/text/+/v0.23.0:LICENSE)) + - [gopkg.in/yaml.v3](https://pkg.go.dev/gopkg.in/yaml.v3) ([MIT](https://github.com/go-yaml/yaml/blob/v3.0.1/LICENSE)) + +[github/github-mcp-server]: https://github.com/github/github-mcp-server diff --git a/sre_agent/servers/github-mcp-server/third-party-licenses.linux.md b/sre_agent/servers/github-mcp-server/third-party-licenses.linux.md new file mode 100644 index 0000000..cdb19b5 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party-licenses.linux.md @@ -0,0 +1,32 @@ +# GitHub MCP Server dependencies + +The following open source dependencies are used to build the [github/github-mcp-server][] GitHub Model Context Protocol Server. + +## Go Packages + +Some packages may only be included on certain architectures or operating systems. + + + - [github.com/fsnotify/fsnotify](https://pkg.go.dev/github.com/fsnotify/fsnotify) ([BSD-3-Clause](https://github.com/fsnotify/fsnotify/blob/v1.8.0/LICENSE)) + - [github.com/github/github-mcp-server](https://pkg.go.dev/github.com/github/github-mcp-server) ([MIT](https://github.com/github/github-mcp-server/blob/HEAD/LICENSE)) + - [github.com/go-viper/mapstructure/v2](https://pkg.go.dev/github.com/go-viper/mapstructure/v2) ([MIT](https://github.com/go-viper/mapstructure/blob/v2.2.1/LICENSE)) + - [github.com/google/go-github/v69/github](https://pkg.go.dev/github.com/google/go-github/v69/github) ([BSD-3-Clause](https://github.com/google/go-github/blob/v69.2.0/LICENSE)) + - [github.com/google/go-querystring/query](https://pkg.go.dev/github.com/google/go-querystring/query) ([BSD-3-Clause](https://github.com/google/go-querystring/blob/v1.1.0/LICENSE)) + - [github.com/google/uuid](https://pkg.go.dev/github.com/google/uuid) ([BSD-3-Clause](https://github.com/google/uuid/blob/v1.6.0/LICENSE)) + - [github.com/mark3labs/mcp-go](https://pkg.go.dev/github.com/mark3labs/mcp-go) ([MIT](https://github.com/mark3labs/mcp-go/blob/v0.22.0/LICENSE)) + - [github.com/pelletier/go-toml/v2](https://pkg.go.dev/github.com/pelletier/go-toml/v2) ([MIT](https://github.com/pelletier/go-toml/blob/v2.2.3/LICENSE)) + - [github.com/sagikazarmark/locafero](https://pkg.go.dev/github.com/sagikazarmark/locafero) ([MIT](https://github.com/sagikazarmark/locafero/blob/v0.9.0/LICENSE)) + - [github.com/sirupsen/logrus](https://pkg.go.dev/github.com/sirupsen/logrus) ([MIT](https://github.com/sirupsen/logrus/blob/v1.9.3/LICENSE)) + - [github.com/sourcegraph/conc](https://pkg.go.dev/github.com/sourcegraph/conc) ([MIT](https://github.com/sourcegraph/conc/blob/v0.3.0/LICENSE)) + - [github.com/spf13/afero](https://pkg.go.dev/github.com/spf13/afero) ([Apache-2.0](https://github.com/spf13/afero/blob/v1.14.0/LICENSE.txt)) + - [github.com/spf13/cast](https://pkg.go.dev/github.com/spf13/cast) ([MIT](https://github.com/spf13/cast/blob/v1.7.1/LICENSE)) + - [github.com/spf13/cobra](https://pkg.go.dev/github.com/spf13/cobra) ([Apache-2.0](https://github.com/spf13/cobra/blob/v1.9.1/LICENSE.txt)) + - [github.com/spf13/pflag](https://pkg.go.dev/github.com/spf13/pflag) ([BSD-3-Clause](https://github.com/spf13/pflag/blob/v1.0.6/LICENSE)) + - [github.com/spf13/viper](https://pkg.go.dev/github.com/spf13/viper) ([MIT](https://github.com/spf13/viper/blob/v1.20.1/LICENSE)) + - [github.com/subosito/gotenv](https://pkg.go.dev/github.com/subosito/gotenv) ([MIT](https://github.com/subosito/gotenv/blob/v1.6.0/LICENSE)) + - [github.com/yosida95/uritemplate/v3](https://pkg.go.dev/github.com/yosida95/uritemplate/v3) ([BSD-3-Clause](https://github.com/yosida95/uritemplate/blob/v3.0.2/LICENSE)) + - [golang.org/x/sys/unix](https://pkg.go.dev/golang.org/x/sys/unix) ([BSD-3-Clause](https://cs.opensource.google/go/x/sys/+/v0.31.0:LICENSE)) + - [golang.org/x/text](https://pkg.go.dev/golang.org/x/text) ([BSD-3-Clause](https://cs.opensource.google/go/x/text/+/v0.23.0:LICENSE)) + - [gopkg.in/yaml.v3](https://pkg.go.dev/gopkg.in/yaml.v3) ([MIT](https://github.com/go-yaml/yaml/blob/v3.0.1/LICENSE)) + +[github/github-mcp-server]: https://github.com/github/github-mcp-server diff --git a/sre_agent/servers/github-mcp-server/third-party-licenses.windows.md b/sre_agent/servers/github-mcp-server/third-party-licenses.windows.md new file mode 100644 index 0000000..b34d7e6 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party-licenses.windows.md @@ -0,0 +1,33 @@ +# GitHub MCP Server dependencies + +The following open source dependencies are used to build the [github/github-mcp-server][] GitHub Model Context Protocol Server. + +## Go Packages + +Some packages may only be included on certain architectures or operating systems. + + + - [github.com/fsnotify/fsnotify](https://pkg.go.dev/github.com/fsnotify/fsnotify) ([BSD-3-Clause](https://github.com/fsnotify/fsnotify/blob/v1.8.0/LICENSE)) + - [github.com/github/github-mcp-server](https://pkg.go.dev/github.com/github/github-mcp-server) ([MIT](https://github.com/github/github-mcp-server/blob/HEAD/LICENSE)) + - [github.com/go-viper/mapstructure/v2](https://pkg.go.dev/github.com/go-viper/mapstructure/v2) ([MIT](https://github.com/go-viper/mapstructure/blob/v2.2.1/LICENSE)) + - [github.com/google/go-github/v69/github](https://pkg.go.dev/github.com/google/go-github/v69/github) ([BSD-3-Clause](https://github.com/google/go-github/blob/v69.2.0/LICENSE)) + - [github.com/google/go-querystring/query](https://pkg.go.dev/github.com/google/go-querystring/query) ([BSD-3-Clause](https://github.com/google/go-querystring/blob/v1.1.0/LICENSE)) + - [github.com/google/uuid](https://pkg.go.dev/github.com/google/uuid) ([BSD-3-Clause](https://github.com/google/uuid/blob/v1.6.0/LICENSE)) + - [github.com/inconshreveable/mousetrap](https://pkg.go.dev/github.com/inconshreveable/mousetrap) ([Apache-2.0](https://github.com/inconshreveable/mousetrap/blob/v1.1.0/LICENSE)) + - [github.com/mark3labs/mcp-go](https://pkg.go.dev/github.com/mark3labs/mcp-go) ([MIT](https://github.com/mark3labs/mcp-go/blob/v0.22.0/LICENSE)) + - [github.com/pelletier/go-toml/v2](https://pkg.go.dev/github.com/pelletier/go-toml/v2) ([MIT](https://github.com/pelletier/go-toml/blob/v2.2.3/LICENSE)) + - [github.com/sagikazarmark/locafero](https://pkg.go.dev/github.com/sagikazarmark/locafero) ([MIT](https://github.com/sagikazarmark/locafero/blob/v0.9.0/LICENSE)) + - [github.com/sirupsen/logrus](https://pkg.go.dev/github.com/sirupsen/logrus) ([MIT](https://github.com/sirupsen/logrus/blob/v1.9.3/LICENSE)) + - [github.com/sourcegraph/conc](https://pkg.go.dev/github.com/sourcegraph/conc) ([MIT](https://github.com/sourcegraph/conc/blob/v0.3.0/LICENSE)) + - [github.com/spf13/afero](https://pkg.go.dev/github.com/spf13/afero) ([Apache-2.0](https://github.com/spf13/afero/blob/v1.14.0/LICENSE.txt)) + - [github.com/spf13/cast](https://pkg.go.dev/github.com/spf13/cast) ([MIT](https://github.com/spf13/cast/blob/v1.7.1/LICENSE)) + - [github.com/spf13/cobra](https://pkg.go.dev/github.com/spf13/cobra) ([Apache-2.0](https://github.com/spf13/cobra/blob/v1.9.1/LICENSE.txt)) + - [github.com/spf13/pflag](https://pkg.go.dev/github.com/spf13/pflag) ([BSD-3-Clause](https://github.com/spf13/pflag/blob/v1.0.6/LICENSE)) + - [github.com/spf13/viper](https://pkg.go.dev/github.com/spf13/viper) ([MIT](https://github.com/spf13/viper/blob/v1.20.1/LICENSE)) + - [github.com/subosito/gotenv](https://pkg.go.dev/github.com/subosito/gotenv) ([MIT](https://github.com/subosito/gotenv/blob/v1.6.0/LICENSE)) + - [github.com/yosida95/uritemplate/v3](https://pkg.go.dev/github.com/yosida95/uritemplate/v3) ([BSD-3-Clause](https://github.com/yosida95/uritemplate/blob/v3.0.2/LICENSE)) + - [golang.org/x/sys/windows](https://pkg.go.dev/golang.org/x/sys/windows) ([BSD-3-Clause](https://cs.opensource.google/go/x/sys/+/v0.31.0:LICENSE)) + - [golang.org/x/text](https://pkg.go.dev/golang.org/x/text) ([BSD-3-Clause](https://cs.opensource.google/go/x/text/+/v0.23.0:LICENSE)) + - [gopkg.in/yaml.v3](https://pkg.go.dev/gopkg.in/yaml.v3) ([MIT](https://github.com/go-yaml/yaml/blob/v3.0.1/LICENSE)) + +[github/github-mcp-server]: https://github.com/github/github-mcp-server diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/fsnotify/fsnotify/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/fsnotify/fsnotify/LICENSE new file mode 100644 index 0000000..fb03ade --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/fsnotify/fsnotify/LICENSE @@ -0,0 +1,25 @@ +Copyright © 2012 The Go Authors. All rights reserved. +Copyright © fsnotify Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. +* Neither the name of Google Inc. nor the names of its contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/github/github-mcp-server/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/github/github-mcp-server/LICENSE new file mode 100644 index 0000000..9a9cc50 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/github/github-mcp-server/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 GitHub + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/go-viper/mapstructure/v2/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/go-viper/mapstructure/v2/LICENSE new file mode 100644 index 0000000..f9c841a --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/go-viper/mapstructure/v2/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013 Mitchell Hashimoto + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/google/go-github/v69/github/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/google/go-github/v69/github/LICENSE new file mode 100644 index 0000000..28b6486 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/google/go-github/v69/github/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2013 The go-github AUTHORS. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/google/go-querystring/query/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/google/go-querystring/query/LICENSE new file mode 100644 index 0000000..ae121a1 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/google/go-querystring/query/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2013 Google. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/google/uuid/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/google/uuid/LICENSE new file mode 100644 index 0000000..5dc6826 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/google/uuid/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2009,2014 Google Inc. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/inconshreveable/mousetrap/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/inconshreveable/mousetrap/LICENSE new file mode 100644 index 0000000..5f920e9 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/inconshreveable/mousetrap/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2022 Alan Shreve (@inconshreveable) + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/mark3labs/mcp-go/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/mark3labs/mcp-go/LICENSE new file mode 100644 index 0000000..3d48435 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/mark3labs/mcp-go/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Anthropic, PBC + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/pelletier/go-toml/v2/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/pelletier/go-toml/v2/LICENSE new file mode 100644 index 0000000..991e2ae --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/pelletier/go-toml/v2/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +go-toml v2 +Copyright (c) 2021 - 2023 Thomas Pelletier + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/sagikazarmark/locafero/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/sagikazarmark/locafero/LICENSE new file mode 100644 index 0000000..a70b0f2 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/sagikazarmark/locafero/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2023 Márk Sági-Kazár + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/sirupsen/logrus/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/sirupsen/logrus/LICENSE new file mode 100644 index 0000000..f090cb4 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/sirupsen/logrus/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Simon Eskildsen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/sourcegraph/conc/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/sourcegraph/conc/LICENSE new file mode 100644 index 0000000..1081f4e --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/sourcegraph/conc/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Sourcegraph + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/afero/LICENSE.txt b/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/afero/LICENSE.txt new file mode 100644 index 0000000..298f0e2 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/afero/LICENSE.txt @@ -0,0 +1,174 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/cast/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/cast/LICENSE new file mode 100644 index 0000000..27457cd --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/cast/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Steve Francia + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/cobra/LICENSE.txt b/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/cobra/LICENSE.txt new file mode 100644 index 0000000..298f0e2 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/cobra/LICENSE.txt @@ -0,0 +1,174 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/pflag/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/pflag/LICENSE new file mode 100644 index 0000000..63ed1cf --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/pflag/LICENSE @@ -0,0 +1,28 @@ +Copyright (c) 2012 Alex Ogier. All rights reserved. +Copyright (c) 2012 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/viper/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/viper/LICENSE new file mode 100644 index 0000000..27457cd --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/spf13/viper/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Steve Francia + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/subosito/gotenv/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/subosito/gotenv/LICENSE new file mode 100644 index 0000000..f64ccae --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/subosito/gotenv/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013 Alif Rachmawadi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/sre_agent/servers/github-mcp-server/third-party/github.com/yosida95/uritemplate/v3/LICENSE b/sre_agent/servers/github-mcp-server/third-party/github.com/yosida95/uritemplate/v3/LICENSE new file mode 100644 index 0000000..79e8f87 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/github.com/yosida95/uritemplate/v3/LICENSE @@ -0,0 +1,25 @@ +Copyright (C) 2016, Kohei YOSHIDA . All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/sre_agent/servers/github-mcp-server/third-party/golang.org/x/sys/unix/LICENSE b/sre_agent/servers/github-mcp-server/third-party/golang.org/x/sys/unix/LICENSE new file mode 100644 index 0000000..2a7cf70 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/golang.org/x/sys/unix/LICENSE @@ -0,0 +1,27 @@ +Copyright 2009 The Go Authors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google LLC nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/sre_agent/servers/github-mcp-server/third-party/golang.org/x/sys/windows/LICENSE b/sre_agent/servers/github-mcp-server/third-party/golang.org/x/sys/windows/LICENSE new file mode 100644 index 0000000..2a7cf70 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/golang.org/x/sys/windows/LICENSE @@ -0,0 +1,27 @@ +Copyright 2009 The Go Authors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google LLC nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/sre_agent/servers/github-mcp-server/third-party/golang.org/x/text/LICENSE b/sre_agent/servers/github-mcp-server/third-party/golang.org/x/text/LICENSE new file mode 100644 index 0000000..2a7cf70 --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/golang.org/x/text/LICENSE @@ -0,0 +1,27 @@ +Copyright 2009 The Go Authors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google LLC nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/sre_agent/servers/github-mcp-server/third-party/gopkg.in/yaml.v3/LICENSE b/sre_agent/servers/github-mcp-server/third-party/gopkg.in/yaml.v3/LICENSE new file mode 100644 index 0000000..2683e4b --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/gopkg.in/yaml.v3/LICENSE @@ -0,0 +1,50 @@ + +This project is covered by two different licenses: MIT and Apache. + +#### MIT License #### + +The following files were ported to Go from C files of libyaml, and thus +are still covered by their original MIT license, with the additional +copyright staring in 2011 when the project was ported over: + + apic.go emitterc.go parserc.go readerc.go scannerc.go + writerc.go yamlh.go yamlprivateh.go + +Copyright (c) 2006-2010 Kirill Simonov +Copyright (c) 2006-2011 Kirill Simonov + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +### Apache License ### + +All the remaining project files are covered by the Apache license: + +Copyright (c) 2011-2019 Canonical Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/sre_agent/servers/github-mcp-server/third-party/gopkg.in/yaml.v3/NOTICE b/sre_agent/servers/github-mcp-server/third-party/gopkg.in/yaml.v3/NOTICE new file mode 100644 index 0000000..866d74a --- /dev/null +++ b/sre_agent/servers/github-mcp-server/third-party/gopkg.in/yaml.v3/NOTICE @@ -0,0 +1,13 @@ +Copyright 2011-2016 Canonical Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/sre_agent/servers/github/Dockerfile b/sre_agent/servers/github/Dockerfile deleted file mode 100644 index 73dc891..0000000 --- a/sre_agent/servers/github/Dockerfile +++ /dev/null @@ -1,24 +0,0 @@ -FROM node:22.12-alpine AS builder - -# Must be entire project because `prepare` script is run during `npm install` and requires all files. -COPY servers/github /app -COPY tsconfig.json /tsconfig.json - -WORKDIR /app - -RUN --mount=type=cache,target=/root/.npm npm install - -FROM node:22.12-alpine AS release - -COPY --from=builder /app/dist /app/dist -COPY --from=builder /app/package.json /app/package.json -COPY --from=builder /app/package-lock.json /app/package-lock.json - -ENV NODE_ENV=production -ENV PORT=3001 - -WORKDIR /app - -RUN npm ci --ignore-scripts --omit-dev - -ENTRYPOINT ["node", "dist/index.js"] diff --git a/sre_agent/servers/github/README.md b/sre_agent/servers/github/README.md deleted file mode 100644 index 0bc6bd0..0000000 --- a/sre_agent/servers/github/README.md +++ /dev/null @@ -1,364 +0,0 @@ -# GitHub MCP Server - -**Deprecation Notice:** Development for this project has been moved to GitHub in the http://github.com/github/github-mcp-server repo. - ---- - -MCP Server for the GitHub API, enabling file operations, repository management, search functionality, and more. - -### Features - -- **Automatic Branch Creation**: When creating/updating files or pushing changes, branches are automatically created if they don't exist -- **Comprehensive Error Handling**: Clear error messages for common issues -- **Git History Preservation**: Operations maintain proper Git history without force pushing -- **Batch Operations**: Support for both single-file and multi-file operations -- **Advanced Search**: Support for searching code, issues/PRs, and users - - -## Tools - -1. `create_or_update_file` - - Create or update a single file in a repository - - Inputs: - - `owner` (string): Repository owner (username or organization) - - `repo` (string): Repository name - - `path` (string): Path where to create/update the file - - `content` (string): Content of the file - - `message` (string): Commit message - - `branch` (string): Branch to create/update the file in - - `sha` (optional string): SHA of file being replaced (for updates) - - Returns: File content and commit details - -2. `push_files` - - Push multiple files in a single commit - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `branch` (string): Branch to push to - - `files` (array): Files to push, each with `path` and `content` - - `message` (string): Commit message - - Returns: Updated branch reference - -3. `search_repositories` - - Search for GitHub repositories - - Inputs: - - `query` (string): Search query - - `page` (optional number): Page number for pagination - - `perPage` (optional number): Results per page (max 100) - - Returns: Repository search results - -4. `create_repository` - - Create a new GitHub repository - - Inputs: - - `name` (string): Repository name - - `description` (optional string): Repository description - - `private` (optional boolean): Whether repo should be private - - `autoInit` (optional boolean): Initialize with README - - Returns: Created repository details - -5. `get_file_contents` - - Get contents of a file or directory - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `path` (string): Path to file/directory - - `branch` (optional string): Branch to get contents from - - Returns: File/directory contents - -6. `create_issue` - - Create a new issue - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `title` (string): Issue title - - `body` (optional string): Issue description - - `assignees` (optional string[]): Usernames to assign - - `labels` (optional string[]): Labels to add - - `milestone` (optional number): Milestone number - - Returns: Created issue details - -7. `create_pull_request` - - Create a new pull request - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `title` (string): PR title - - `body` (optional string): PR description - - `head` (string): Branch containing changes - - `base` (string): Branch to merge into - - `draft` (optional boolean): Create as draft PR - - `maintainer_can_modify` (optional boolean): Allow maintainer edits - - Returns: Created pull request details - -8. `fork_repository` - - Fork a repository - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `organization` (optional string): Organization to fork to - - Returns: Forked repository details - -9. `create_branch` - - Create a new branch - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `branch` (string): Name for new branch - - `from_branch` (optional string): Source branch (defaults to repo default) - - Returns: Created branch reference - -10. `list_issues` - - List and filter repository issues - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `state` (optional string): Filter by state ('open', 'closed', 'all') - - `labels` (optional string[]): Filter by labels - - `sort` (optional string): Sort by ('created', 'updated', 'comments') - - `direction` (optional string): Sort direction ('asc', 'desc') - - `since` (optional string): Filter by date (ISO 8601 timestamp) - - `page` (optional number): Page number - - `per_page` (optional number): Results per page - - Returns: Array of issue details - -11. `update_issue` - - Update an existing issue - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `issue_number` (number): Issue number to update - - `title` (optional string): New title - - `body` (optional string): New description - - `state` (optional string): New state ('open' or 'closed') - - `labels` (optional string[]): New labels - - `assignees` (optional string[]): New assignees - - `milestone` (optional number): New milestone number - - Returns: Updated issue details - -12. `add_issue_comment` - - Add a comment to an issue - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `issue_number` (number): Issue number to comment on - - `body` (string): Comment text - - Returns: Created comment details - -13. `search_code` - - Search for code across GitHub repositories - - Inputs: - - `q` (string): Search query using GitHub code search syntax - - `sort` (optional string): Sort field ('indexed' only) - - `order` (optional string): Sort order ('asc' or 'desc') - - `per_page` (optional number): Results per page (max 100) - - `page` (optional number): Page number - - Returns: Code search results with repository context - -14. `search_issues` - - Search for issues and pull requests - - Inputs: - - `q` (string): Search query using GitHub issues search syntax - - `sort` (optional string): Sort field (comments, reactions, created, etc.) - - `order` (optional string): Sort order ('asc' or 'desc') - - `per_page` (optional number): Results per page (max 100) - - `page` (optional number): Page number - - Returns: Issue and pull request search results - -15. `search_users` - - Search for GitHub users - - Inputs: - - `q` (string): Search query using GitHub users search syntax - - `sort` (optional string): Sort field (followers, repositories, joined) - - `order` (optional string): Sort order ('asc' or 'desc') - - `per_page` (optional number): Results per page (max 100) - - `page` (optional number): Page number - - Returns: User search results - -16. `list_commits` - - Gets commits of a branch in a repository - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `page` (optional string): page number - - `per_page` (optional string): number of record per page - - `sha` (optional string): branch name - - Returns: List of commits - -17. `get_issue` - - Gets the contents of an issue within a repository - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `issue_number` (number): Issue number to retrieve - - Returns: Github Issue object & details - -18. `get_pull_request` - - Get details of a specific pull request - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `pull_number` (number): Pull request number - - Returns: Pull request details including diff and review status - -19. `list_pull_requests` - - List and filter repository pull requests - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `state` (optional string): Filter by state ('open', 'closed', 'all') - - `head` (optional string): Filter by head user/org and branch - - `base` (optional string): Filter by base branch - - `sort` (optional string): Sort by ('created', 'updated', 'popularity', 'long-running') - - `direction` (optional string): Sort direction ('asc', 'desc') - - `per_page` (optional number): Results per page (max 100) - - `page` (optional number): Page number - - Returns: Array of pull request details - -20. `create_pull_request_review` - - Create a review on a pull request - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `pull_number` (number): Pull request number - - `body` (string): Review comment text - - `event` (string): Review action ('APPROVE', 'REQUEST_CHANGES', 'COMMENT') - - `commit_id` (optional string): SHA of commit to review - - `comments` (optional array): Line-specific comments, each with: - - `path` (string): File path - - `position` (number): Line position in diff - - `body` (string): Comment text - - Returns: Created review details - -21. `merge_pull_request` - - Merge a pull request - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `pull_number` (number): Pull request number - - `commit_title` (optional string): Title for merge commit - - `commit_message` (optional string): Extra detail for merge commit - - `merge_method` (optional string): Merge method ('merge', 'squash', 'rebase') - - Returns: Merge result details - -22. `get_pull_request_files` - - Get the list of files changed in a pull request - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `pull_number` (number): Pull request number - - Returns: Array of changed files with patch and status details - -23. `get_pull_request_status` - - Get the combined status of all status checks for a pull request - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `pull_number` (number): Pull request number - - Returns: Combined status check results and individual check details - -24. `update_pull_request_branch` - - Update a pull request branch with the latest changes from the base branch (equivalent to GitHub's "Update branch" button) - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `pull_number` (number): Pull request number - - `expected_head_sha` (optional string): The expected SHA of the pull request's HEAD ref - - Returns: Success message when branch is updated - -25. `get_pull_request_comments` - - Get the review comments on a pull request - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `pull_number` (number): Pull request number - - Returns: Array of pull request review comments with details like the comment text, author, and location in the diff - -26. `get_pull_request_reviews` - - Get the reviews on a pull request - - Inputs: - - `owner` (string): Repository owner - - `repo` (string): Repository name - - `pull_number` (number): Pull request number - - Returns: Array of pull request reviews with details like the review state (APPROVED, CHANGES_REQUESTED, etc.), reviewer, and review body - -## Search Query Syntax - -### Code Search -- `language:javascript`: Search by programming language -- `repo:owner/name`: Search in specific repository -- `path:app/src`: Search in specific path -- `extension:js`: Search by file extension -- Example: `q: "import express" language:typescript path:src/` - -### Issues Search -- `is:issue` or `is:pr`: Filter by type -- `is:open` or `is:closed`: Filter by state -- `label:bug`: Search by label -- `author:username`: Search by author -- Example: `q: "memory leak" is:issue is:open label:bug` - -### Users Search -- `type:user` or `type:org`: Filter by account type -- `followers:>1000`: Filter by followers -- `location:London`: Search by location -- Example: `q: "fullstack developer" location:London followers:>100` - -For detailed search syntax, see [GitHub's searching documentation](https://docs.github.com/en/search-github/searching-on-github). - -## Setup - -### Personal Access Token -[Create a GitHub Personal Access Token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens) with appropriate permissions: - - Go to [Personal access tokens](https://github.com/settings/tokens) (in GitHub Settings > Developer settings) - - Select which repositories you'd like this token to have access to (Public, All, or Select) - - Create a token with the `repo` scope ("Full control of private repositories") - - Alternatively, if working only with public repositories, select only the `public_repo` scope - - Copy the generated token - -### Usage with Claude Desktop -To use this with Claude Desktop, add the following to your `claude_desktop_config.json`: - -#### Docker -```json -{ - "mcpServers": { - "github": { - "command": "docker", - "args": [ - "run", - "-i", - "--rm", - "-e", - "GITHUB_PERSONAL_ACCESS_TOKEN", - "mcp/github" - ], - "env": { - "GITHUB_PERSONAL_ACCESS_TOKEN": "" - } - } - } -} -``` - -### NPX - -```json -{ - "mcpServers": { - "github": { - "command": "npx", - "args": [ - "-y", - "@modelcontextprotocol/server-github" - ], - "env": { - "GITHUB_PERSONAL_ACCESS_TOKEN": "" - } - } - } -} -``` - -## License - -This MCP server is licensed under the MIT License. This means you are free to use, modify, and distribute the software, subject to the terms and conditions of the MIT License. For more details, please see the LICENSE file in the project repository. diff --git a/sre_agent/servers/github/common/errors.ts b/sre_agent/servers/github/common/errors.ts deleted file mode 100644 index 7ca3cc8..0000000 --- a/sre_agent/servers/github/common/errors.ts +++ /dev/null @@ -1,89 +0,0 @@ -export class GitHubError extends Error { - constructor( - message: string, - public readonly status: number, - public readonly response: unknown - ) { - super(message); - this.name = "GitHubError"; - } -} - -export class GitHubValidationError extends GitHubError { - constructor(message: string, status: number, response: unknown) { - super(message, status, response); - this.name = "GitHubValidationError"; - } -} - -export class GitHubResourceNotFoundError extends GitHubError { - constructor(resource: string) { - super(`Resource not found: ${resource}`, 404, { message: `${resource} not found` }); - this.name = "GitHubResourceNotFoundError"; - } -} - -export class GitHubAuthenticationError extends GitHubError { - constructor(message = "Authentication failed") { - super(message, 401, { message }); - this.name = "GitHubAuthenticationError"; - } -} - -export class GitHubPermissionError extends GitHubError { - constructor(message = "Insufficient permissions") { - super(message, 403, { message }); - this.name = "GitHubPermissionError"; - } -} - -export class GitHubRateLimitError extends GitHubError { - constructor( - message = "Rate limit exceeded", - public readonly resetAt: Date - ) { - super(message, 429, { message, reset_at: resetAt.toISOString() }); - this.name = "GitHubRateLimitError"; - } -} - -export class GitHubConflictError extends GitHubError { - constructor(message: string) { - super(message, 409, { message }); - this.name = "GitHubConflictError"; - } -} - -export function isGitHubError(error: unknown): error is GitHubError { - return error instanceof GitHubError; -} - -export function createGitHubError(status: number, response: any): GitHubError { - switch (status) { - case 401: - return new GitHubAuthenticationError(response?.message); - case 403: - return new GitHubPermissionError(response?.message); - case 404: - return new GitHubResourceNotFoundError(response?.message || "Resource"); - case 409: - return new GitHubConflictError(response?.message || "Conflict occurred"); - case 422: - return new GitHubValidationError( - response?.message || "Validation failed", - status, - response - ); - case 429: - return new GitHubRateLimitError( - response?.message, - new Date(response?.reset_at || Date.now() + 60000) - ); - default: - return new GitHubError( - response?.message || "GitHub API error", - status, - response - ); - } -} diff --git a/sre_agent/servers/github/common/types.ts b/sre_agent/servers/github/common/types.ts deleted file mode 100644 index 64d5942..0000000 --- a/sre_agent/servers/github/common/types.ts +++ /dev/null @@ -1,259 +0,0 @@ -import { z } from "zod"; - -// Base schemas for common types -export const GitHubAuthorSchema = z.object({ - name: z.string(), - email: z.string(), - date: z.string(), -}); - -export const GitHubOwnerSchema = z.object({ - login: z.string(), - id: z.number(), - node_id: z.string(), - avatar_url: z.string(), - url: z.string(), - html_url: z.string(), - type: z.string(), -}); - -export const GitHubRepositorySchema = z.object({ - id: z.number(), - node_id: z.string(), - name: z.string(), - full_name: z.string(), - private: z.boolean(), - owner: GitHubOwnerSchema, - html_url: z.string(), - description: z.string().nullable(), - fork: z.boolean(), - url: z.string(), - created_at: z.string(), - updated_at: z.string(), - pushed_at: z.string(), - git_url: z.string(), - ssh_url: z.string(), - clone_url: z.string(), - default_branch: z.string(), -}); - -export const GithubFileContentLinks = z.object({ - self: z.string(), - git: z.string().nullable(), - html: z.string().nullable() -}); - -export const GitHubFileContentSchema = z.object({ - name: z.string(), - path: z.string(), - sha: z.string(), - size: z.number(), - url: z.string(), - html_url: z.string(), - git_url: z.string(), - download_url: z.string(), - type: z.string(), - content: z.string().optional(), - encoding: z.string().optional(), - _links: GithubFileContentLinks -}); - -export const GitHubDirectoryContentSchema = z.object({ - type: z.string(), - size: z.number(), - name: z.string(), - path: z.string(), - sha: z.string(), - url: z.string(), - git_url: z.string(), - html_url: z.string(), - download_url: z.string().nullable(), -}); - -export const GitHubContentSchema = z.union([ - GitHubFileContentSchema, - z.array(GitHubDirectoryContentSchema), -]); - -export const GitHubTreeEntrySchema = z.object({ - path: z.string(), - mode: z.enum(["100644", "100755", "040000", "160000", "120000"]), - type: z.enum(["blob", "tree", "commit"]), - size: z.number().optional(), - sha: z.string(), - url: z.string(), -}); - -export const GitHubTreeSchema = z.object({ - sha: z.string(), - url: z.string(), - tree: z.array(GitHubTreeEntrySchema), - truncated: z.boolean(), -}); - -export const GitHubCommitSchema = z.object({ - sha: z.string(), - node_id: z.string(), - url: z.string(), - author: GitHubAuthorSchema, - committer: GitHubAuthorSchema, - message: z.string(), - tree: z.object({ - sha: z.string(), - url: z.string(), - }), - parents: z.array( - z.object({ - sha: z.string(), - url: z.string(), - }) - ), -}); - -export const GitHubListCommitsSchema = z.array(z.object({ - sha: z.string(), - node_id: z.string(), - commit: z.object({ - author: GitHubAuthorSchema, - committer: GitHubAuthorSchema, - message: z.string(), - tree: z.object({ - sha: z.string(), - url: z.string() - }), - url: z.string(), - comment_count: z.number(), - }), - url: z.string(), - html_url: z.string(), - comments_url: z.string() -})); - -export const GitHubReferenceSchema = z.object({ - ref: z.string(), - node_id: z.string(), - url: z.string(), - object: z.object({ - sha: z.string(), - type: z.string(), - url: z.string(), - }), -}); - -// User and assignee schemas -export const GitHubIssueAssigneeSchema = z.object({ - login: z.string(), - id: z.number(), - avatar_url: z.string(), - url: z.string(), - html_url: z.string(), -}); - -// Issue-related schemas -export const GitHubLabelSchema = z.object({ - id: z.number(), - node_id: z.string(), - url: z.string(), - name: z.string(), - color: z.string(), - default: z.boolean(), - description: z.string().nullable().optional(), -}); - -export const GitHubMilestoneSchema = z.object({ - url: z.string(), - html_url: z.string(), - labels_url: z.string(), - id: z.number(), - node_id: z.string(), - number: z.number(), - title: z.string(), - description: z.string(), - state: z.string(), -}); - -export const GitHubIssueSchema = z.object({ - url: z.string(), - repository_url: z.string(), - labels_url: z.string(), - comments_url: z.string(), - events_url: z.string(), - html_url: z.string(), - id: z.number(), - node_id: z.string(), - number: z.number(), - title: z.string(), - user: GitHubIssueAssigneeSchema, - labels: z.array(GitHubLabelSchema), - state: z.string(), - locked: z.boolean(), - assignee: GitHubIssueAssigneeSchema.nullable(), - assignees: z.array(GitHubIssueAssigneeSchema), - milestone: GitHubMilestoneSchema.nullable(), - comments: z.number(), - created_at: z.string(), - updated_at: z.string(), - closed_at: z.string().nullable(), - body: z.string().nullable(), -}); - -// Search-related schemas -export const GitHubSearchResponseSchema = z.object({ - total_count: z.number(), - incomplete_results: z.boolean(), - items: z.array(GitHubRepositorySchema), -}); - -// Pull request schemas -export const GitHubPullRequestRefSchema = z.object({ - label: z.string(), - ref: z.string(), - sha: z.string(), - user: GitHubIssueAssigneeSchema, - repo: GitHubRepositorySchema, -}); - -export const GitHubPullRequestSchema = z.object({ - url: z.string(), - id: z.number(), - node_id: z.string(), - html_url: z.string(), - diff_url: z.string(), - patch_url: z.string(), - issue_url: z.string(), - number: z.number(), - state: z.string(), - locked: z.boolean(), - title: z.string(), - user: GitHubIssueAssigneeSchema, - body: z.string().nullable(), - created_at: z.string(), - updated_at: z.string(), - closed_at: z.string().nullable(), - merged_at: z.string().nullable(), - merge_commit_sha: z.string().nullable(), - assignee: GitHubIssueAssigneeSchema.nullable(), - assignees: z.array(GitHubIssueAssigneeSchema), - requested_reviewers: z.array(GitHubIssueAssigneeSchema), - labels: z.array(GitHubLabelSchema), - head: GitHubPullRequestRefSchema, - base: GitHubPullRequestRefSchema, -}); - -// Export types -export type GitHubAuthor = z.infer; -export type GitHubRepository = z.infer; -export type GitHubFileContent = z.infer; -export type GitHubDirectoryContent = z.infer; -export type GitHubContent = z.infer; -export type GitHubTree = z.infer; -export type GitHubCommit = z.infer; -export type GitHubListCommits = z.infer; -export type GitHubReference = z.infer; -export type GitHubIssueAssignee = z.infer; -export type GitHubLabel = z.infer; -export type GitHubMilestone = z.infer; -export type GitHubIssue = z.infer; -export type GitHubSearchResponse = z.infer; -export type GitHubPullRequest = z.infer; -export type GitHubPullRequestRef = z.infer; diff --git a/sre_agent/servers/github/common/utils.ts b/sre_agent/servers/github/common/utils.ts deleted file mode 100644 index 7f332bf..0000000 --- a/sre_agent/servers/github/common/utils.ts +++ /dev/null @@ -1,138 +0,0 @@ -import { getUserAgent } from "universal-user-agent"; -import { createGitHubError } from "./errors.js"; -import { VERSION } from "./version.js"; - -type RequestOptions = { - method?: string; - body?: unknown; - headers?: Record; -} - -async function parseResponseBody(response: Response): Promise { - const contentType = response.headers.get("content-type"); - if (contentType?.includes("application/json")) { - return response.json(); - } - return response.text(); -} - -export function buildUrl(baseUrl: string, params: Record): string { - const url = new URL(baseUrl); - Object.entries(params).forEach(([key, value]) => { - if (value !== undefined) { - url.searchParams.append(key, value.toString()); - } - }); - return url.toString(); -} - -const USER_AGENT = `modelcontextprotocol/servers/github/v${VERSION} ${getUserAgent()}`; - -export async function githubRequest( - url: string, - options: RequestOptions = {} -): Promise { - const headers: Record = { - "Accept": "application/vnd.github.v3+json", - "Content-Type": "application/json", - "User-Agent": USER_AGENT, - ...options.headers, - }; - - if (process.env.GITHUB_PERSONAL_ACCESS_TOKEN) { - headers["Authorization"] = `Bearer ${process.env.GITHUB_PERSONAL_ACCESS_TOKEN}`; - } - - const response = await fetch(url, { - method: options.method || "GET", - headers, - body: options.body ? JSON.stringify(options.body) : undefined, - }); - - const responseBody = await parseResponseBody(response); - - if (!response.ok) { - throw createGitHubError(response.status, responseBody); - } - - return responseBody; -} - -export function validateBranchName(branch: string): string { - const sanitized = branch.trim(); - if (!sanitized) { - throw new Error("Branch name cannot be empty"); - } - if (sanitized.includes("..")) { - throw new Error("Branch name cannot contain '..'"); - } - if (/[\s~^:?*[\\\]]/.test(sanitized)) { - throw new Error("Branch name contains invalid characters"); - } - if (sanitized.startsWith("/") || sanitized.endsWith("/")) { - throw new Error("Branch name cannot start or end with '/'"); - } - if (sanitized.endsWith(".lock")) { - throw new Error("Branch name cannot end with '.lock'"); - } - return sanitized; -} - -export function validateRepositoryName(name: string): string { - const sanitized = name.trim().toLowerCase(); - if (!sanitized) { - throw new Error("Repository name cannot be empty"); - } - if (!/^[a-z0-9_.-]+$/.test(sanitized)) { - throw new Error( - "Repository name can only contain lowercase letters, numbers, hyphens, periods, and underscores" - ); - } - if (sanitized.startsWith(".") || sanitized.endsWith(".")) { - throw new Error("Repository name cannot start or end with a period"); - } - return sanitized; -} - -export function validateOwnerName(owner: string): string { - const sanitized = owner.trim().toLowerCase(); - if (!sanitized) { - throw new Error("Owner name cannot be empty"); - } - if (!/^[a-z0-9](?:[a-z0-9]|-(?=[a-z0-9])){0,38}$/.test(sanitized)) { - throw new Error( - "Owner name must start with a letter or number and can contain up to 39 characters" - ); - } - return sanitized; -} - -export async function checkBranchExists( - owner: string, - repo: string, - branch: string -): Promise { - try { - await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/branches/${branch}` - ); - return true; - } catch (error) { - if (error && typeof error === "object" && "status" in error && error.status === 404) { - return false; - } - throw error; - } -} - -export async function checkUserExists(username: string): Promise { - try { - await githubRequest(`https://api.github.com/users/${username}`); - return true; - } catch (error) { - if (error && typeof error === "object" && "status" in error && error.status === 404) { - return false; - } - throw error; - } -} diff --git a/sre_agent/servers/github/common/version.ts b/sre_agent/servers/github/common/version.ts deleted file mode 100644 index 068643d..0000000 --- a/sre_agent/servers/github/common/version.ts +++ /dev/null @@ -1,3 +0,0 @@ -// If the format of this file changes, so it doesn't simply export a VERSION constant, -// this will break .github/workflows/version-check.yml. -export const VERSION = "0.6.2"; diff --git a/sre_agent/servers/github/index.ts b/sre_agent/servers/github/index.ts deleted file mode 100644 index a444511..0000000 --- a/sre_agent/servers/github/index.ts +++ /dev/null @@ -1,670 +0,0 @@ -#!/usr/bin/env node -import express, { - Request as ExpressRequest, - Response as ExpressResponse, -} from "express"; -import { Server } from "@modelcontextprotocol/sdk/server/index.js"; -import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js"; -import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; -import { - CallToolRequestSchema, - ListToolsRequestSchema, -} from "@modelcontextprotocol/sdk/types.js"; -import { z } from "zod"; -import { zodToJsonSchema } from "zod-to-json-schema"; -import fetch, { Request, Response } from "node-fetch"; - -import * as repository from "./operations/repository.js"; -import * as files from "./operations/files.js"; -import * as issues from "./operations/issues.js"; -import * as pulls from "./operations/pulls.js"; -import * as branches from "./operations/branches.js"; -import * as search from "./operations/search.js"; -import * as commits from "./operations/commits.js"; -import { - GitHubError, - GitHubValidationError, - GitHubResourceNotFoundError, - GitHubAuthenticationError, - GitHubPermissionError, - GitHubRateLimitError, - GitHubConflictError, - isGitHubError, -} from "./common/errors.js"; -import { VERSION } from "./common/version.js"; -import logger from "./utils/logger.js"; - -// If fetch doesn't exist in global scope, add it -if (!globalThis.fetch) { - globalThis.fetch = fetch as unknown as typeof global.fetch; -} - -const server = new Server( - { - name: "github-mcp-server", - version: VERSION, - }, - { - capabilities: { - tools: {}, - }, - }, -); - -function formatGitHubError(error: GitHubError): string { - let message = `GitHub API Error: ${error.message}`; - - if (error instanceof GitHubValidationError) { - message = `Validation Error: ${error.message}`; - if (error.response) { - message += `\nDetails: ${JSON.stringify(error.response)}`; - } - } else if (error instanceof GitHubResourceNotFoundError) { - message = `Not Found: ${error.message}`; - } else if (error instanceof GitHubAuthenticationError) { - message = `Authentication Failed: ${error.message}`; - } else if (error instanceof GitHubPermissionError) { - message = `Permission Denied: ${error.message}`; - } else if (error instanceof GitHubRateLimitError) { - message = `Rate Limit Exceeded: ${error.message}\nResets at: ${error.resetAt.toISOString()}`; - } else if (error instanceof GitHubConflictError) { - message = `Conflict: ${error.message}`; - } - - return message; -} - -server.setRequestHandler(ListToolsRequestSchema, async () => { - logger.debug("Received ListToolsRequest"); - return { - tools: [ - { - name: "create_or_update_file", - description: "Create or update a single file in a GitHub repository", - inputSchema: zodToJsonSchema(files.CreateOrUpdateFileSchema), - }, - { - name: "search_repositories", - description: "Search for GitHub repositories", - inputSchema: zodToJsonSchema(repository.SearchRepositoriesSchema), - }, - { - name: "create_repository", - description: "Create a new GitHub repository in your account", - inputSchema: zodToJsonSchema(repository.CreateRepositoryOptionsSchema), - }, - { - name: "get_file_contents", - description: - "Get the contents of a file or directory from a GitHub repository", - inputSchema: zodToJsonSchema(files.GetFileContentsSchema), - }, - { - name: "push_files", - description: - "Push multiple files to a GitHub repository in a single commit", - inputSchema: zodToJsonSchema(files.PushFilesSchema), - }, - { - name: "create_issue", - description: "Create a new issue in a GitHub repository", - inputSchema: zodToJsonSchema(issues.CreateIssueSchema), - }, - { - name: "create_pull_request", - description: "Create a new pull request in a GitHub repository", - inputSchema: zodToJsonSchema(pulls.CreatePullRequestSchema), - }, - { - name: "fork_repository", - description: - "Fork a GitHub repository to your account or specified organization", - inputSchema: zodToJsonSchema(repository.ForkRepositorySchema), - }, - { - name: "create_branch", - description: "Create a new branch in a GitHub repository", - inputSchema: zodToJsonSchema(branches.CreateBranchSchema), - }, - { - name: "list_commits", - description: "Get list of commits of a branch in a GitHub repository", - inputSchema: zodToJsonSchema(commits.ListCommitsSchema), - }, - { - name: "list_issues", - description: - "List issues in a GitHub repository with filtering options", - inputSchema: zodToJsonSchema(issues.ListIssuesOptionsSchema), - }, - { - name: "update_issue", - description: "Update an existing issue in a GitHub repository", - inputSchema: zodToJsonSchema(issues.UpdateIssueOptionsSchema), - }, - { - name: "add_issue_comment", - description: "Add a comment to an existing issue", - inputSchema: zodToJsonSchema(issues.IssueCommentSchema), - }, - { - name: "search_code", - description: "Search for code across GitHub repositories", - inputSchema: zodToJsonSchema(search.SearchCodeSchema), - }, - { - name: "search_issues", - description: - "Search for issues and pull requests across GitHub repositories", - inputSchema: zodToJsonSchema(search.SearchIssuesSchema), - }, - { - name: "search_users", - description: "Search for users on GitHub", - inputSchema: zodToJsonSchema(search.SearchUsersSchema), - }, - { - name: "get_issue", - description: "Get details of a specific issue in a GitHub repository.", - inputSchema: zodToJsonSchema(issues.GetIssueSchema), - }, - { - name: "get_pull_request", - description: "Get details of a specific pull request", - inputSchema: zodToJsonSchema(pulls.GetPullRequestSchema), - }, - { - name: "list_pull_requests", - description: "List and filter repository pull requests", - inputSchema: zodToJsonSchema(pulls.ListPullRequestsSchema), - }, - { - name: "create_pull_request_review", - description: "Create a review on a pull request", - inputSchema: zodToJsonSchema(pulls.CreatePullRequestReviewSchema), - }, - { - name: "merge_pull_request", - description: "Merge a pull request", - inputSchema: zodToJsonSchema(pulls.MergePullRequestSchema), - }, - { - name: "get_pull_request_files", - description: "Get the list of files changed in a pull request", - inputSchema: zodToJsonSchema(pulls.GetPullRequestFilesSchema), - }, - { - name: "get_pull_request_status", - description: - "Get the combined status of all status checks for a pull request", - inputSchema: zodToJsonSchema(pulls.GetPullRequestStatusSchema), - }, - { - name: "update_pull_request_branch", - description: - "Update a pull request branch with the latest changes from the base branch", - inputSchema: zodToJsonSchema(pulls.UpdatePullRequestBranchSchema), - }, - { - name: "get_pull_request_comments", - description: "Get the review comments on a pull request", - inputSchema: zodToJsonSchema(pulls.GetPullRequestCommentsSchema), - }, - { - name: "get_pull_request_reviews", - description: "Get the reviews on a pull request", - inputSchema: zodToJsonSchema(pulls.GetPullRequestReviewsSchema), - }, - ], - }; -}); - -server.setRequestHandler(CallToolRequestSchema, async (request) => { - logger.debug("Received CallToolRequest", { request }); - try { - if (!request.params.arguments) { - throw new Error("No arguments provided"); - } - - switch (request.params.name) { - case "fork_repository": { - const args = repository.ForkRepositorySchema.parse( - request.params.arguments, - ); - const fork = await repository.forkRepository( - args.owner, - args.repo, - args.organization, - ); - return { - content: [{ type: "text", text: JSON.stringify(fork, null, 2) }], - }; - } - - case "create_branch": { - const args = branches.CreateBranchSchema.parse( - request.params.arguments, - ); - const branch = await branches.createBranchFromRef( - args.owner, - args.repo, - args.branch, - args.from_branch, - ); - return { - content: [{ type: "text", text: JSON.stringify(branch, null, 2) }], - }; - } - - case "search_repositories": { - const args = repository.SearchRepositoriesSchema.parse( - request.params.arguments, - ); - const results = await repository.searchRepositories( - args.query, - args.page, - args.perPage, - ); - return { - content: [{ type: "text", text: JSON.stringify(results, null, 2) }], - }; - } - - case "create_repository": { - const args = repository.CreateRepositoryOptionsSchema.parse( - request.params.arguments, - ); - const result = await repository.createRepository(args); - return { - content: [{ type: "text", text: JSON.stringify(result, null, 2) }], - }; - } - - case "get_file_contents": { - const args = files.GetFileContentsSchema.parse( - request.params.arguments, - ); - const contents = await files.getFileContents( - args.owner, - args.repo, - args.path, - args.branch, - ); - return { - content: [{ type: "text", text: JSON.stringify(contents, null, 2) }], - }; - } - - case "create_or_update_file": { - const args = files.CreateOrUpdateFileSchema.parse( - request.params.arguments, - ); - const result = await files.createOrUpdateFile( - args.owner, - args.repo, - args.path, - args.content, - args.message, - args.branch, - args.sha, - ); - return { - content: [{ type: "text", text: JSON.stringify(result, null, 2) }], - }; - } - - case "push_files": { - const args = files.PushFilesSchema.parse(request.params.arguments); - const result = await files.pushFiles( - args.owner, - args.repo, - args.branch, - args.files, - args.message, - ); - return { - content: [{ type: "text", text: JSON.stringify(result, null, 2) }], - }; - } - - case "create_issue": { - const args = issues.CreateIssueSchema.parse(request.params.arguments); - const { owner, repo, ...options } = args; - - try { - logger.debug( - `Attempting to create issue in ${owner}/${repo}`, - ); - logger.debug( - `Issue options:`, - { options: JSON.stringify(options, null, 2) }, - ); - - const issue = await issues.createIssue(owner, repo, options); - - logger.info(`Issue created successfully`); - return { - content: [{ type: "text", text: JSON.stringify(issue, null, 2) }], - }; - } catch (err) { - // Type guard for Error objects - const error = err instanceof Error ? err : new Error(String(err)); - - logger.error(`Failed to create issue:`, { - error: error.message, - stack: error.stack - }); - - if (error instanceof GitHubResourceNotFoundError) { - throw new Error( - `Repository '${owner}/${repo}' not found. Please verify:\n` + - `1. The repository exists\n` + - `2. You have correct access permissions\n` + - `3. The owner and repository names are spelled correctly`, - ); - } - - // Safely access error properties - throw new Error( - `Failed to create issue: ${error.message}${ - error.stack ? `\nStack: ${error.stack}` : "" - }`, - ); - } - } - - case "create_pull_request": { - const args = pulls.CreatePullRequestSchema.parse( - request.params.arguments, - ); - const pullRequest = await pulls.createPullRequest(args); - return { - content: [ - { type: "text", text: JSON.stringify(pullRequest, null, 2) }, - ], - }; - } - - case "search_code": { - const args = search.SearchCodeSchema.parse(request.params.arguments); - const results = await search.searchCode(args); - return { - content: [{ type: "text", text: JSON.stringify(results, null, 2) }], - }; - } - - case "search_issues": { - const args = search.SearchIssuesSchema.parse(request.params.arguments); - const results = await search.searchIssues(args); - return { - content: [{ type: "text", text: JSON.stringify(results, null, 2) }], - }; - } - - case "search_users": { - const args = search.SearchUsersSchema.parse(request.params.arguments); - const results = await search.searchUsers(args); - return { - content: [{ type: "text", text: JSON.stringify(results, null, 2) }], - }; - } - - case "list_issues": { - const args = issues.ListIssuesOptionsSchema.parse( - request.params.arguments, - ); - const { owner, repo, ...options } = args; - const result = await issues.listIssues(owner, repo, options); - return { - content: [{ type: "text", text: JSON.stringify(result, null, 2) }], - }; - } - - case "update_issue": { - const args = issues.UpdateIssueOptionsSchema.parse( - request.params.arguments, - ); - const { owner, repo, issue_number, ...options } = args; - const result = await issues.updateIssue( - owner, - repo, - issue_number, - options, - ); - return { - content: [{ type: "text", text: JSON.stringify(result, null, 2) }], - }; - } - - case "add_issue_comment": { - const args = issues.IssueCommentSchema.parse(request.params.arguments); - const { owner, repo, issue_number, body } = args; - const result = await issues.addIssueComment( - owner, - repo, - issue_number, - body, - ); - return { - content: [{ type: "text", text: JSON.stringify(result, null, 2) }], - }; - } - - case "list_commits": { - const args = commits.ListCommitsSchema.parse(request.params.arguments); - const results = await commits.listCommits( - args.owner, - args.repo, - args.page, - args.perPage, - args.sha, - ); - return { - content: [{ type: "text", text: JSON.stringify(results, null, 2) }], - }; - } - - case "get_issue": { - const args = issues.GetIssueSchema.parse(request.params.arguments); - const issue = await issues.getIssue( - args.owner, - args.repo, - args.issue_number, - ); - return { - content: [{ type: "text", text: JSON.stringify(issue, null, 2) }], - }; - } - - case "get_pull_request": { - const args = pulls.GetPullRequestSchema.parse(request.params.arguments); - const pullRequest = await pulls.getPullRequest( - args.owner, - args.repo, - args.pull_number, - ); - return { - content: [ - { type: "text", text: JSON.stringify(pullRequest, null, 2) }, - ], - }; - } - - case "list_pull_requests": { - const args = pulls.ListPullRequestsSchema.parse( - request.params.arguments, - ); - const { owner, repo, ...options } = args; - const pullRequests = await pulls.listPullRequests(owner, repo, options); - return { - content: [ - { type: "text", text: JSON.stringify(pullRequests, null, 2) }, - ], - }; - } - - case "create_pull_request_review": { - const args = pulls.CreatePullRequestReviewSchema.parse( - request.params.arguments, - ); - const { owner, repo, pull_number, ...options } = args; - const review = await pulls.createPullRequestReview( - owner, - repo, - pull_number, - options, - ); - return { - content: [{ type: "text", text: JSON.stringify(review, null, 2) }], - }; - } - - case "merge_pull_request": { - const args = pulls.MergePullRequestSchema.parse( - request.params.arguments, - ); - const { owner, repo, pull_number, ...options } = args; - const result = await pulls.mergePullRequest( - owner, - repo, - pull_number, - options, - ); - return { - content: [{ type: "text", text: JSON.stringify(result, null, 2) }], - }; - } - - case "get_pull_request_files": { - const args = pulls.GetPullRequestFilesSchema.parse( - request.params.arguments, - ); - const files = await pulls.getPullRequestFiles( - args.owner, - args.repo, - args.pull_number, - ); - return { - content: [{ type: "text", text: JSON.stringify(files, null, 2) }], - }; - } - - case "get_pull_request_status": { - const args = pulls.GetPullRequestStatusSchema.parse( - request.params.arguments, - ); - const status = await pulls.getPullRequestStatus( - args.owner, - args.repo, - args.pull_number, - ); - return { - content: [{ type: "text", text: JSON.stringify(status, null, 2) }], - }; - } - - case "update_pull_request_branch": { - const args = pulls.UpdatePullRequestBranchSchema.parse( - request.params.arguments, - ); - const { owner, repo, pull_number, expected_head_sha } = args; - await pulls.updatePullRequestBranch( - owner, - repo, - pull_number, - expected_head_sha, - ); - return { - content: [ - { type: "text", text: JSON.stringify({ success: true }, null, 2) }, - ], - }; - } - - case "get_pull_request_comments": { - const args = pulls.GetPullRequestCommentsSchema.parse( - request.params.arguments, - ); - const comments = await pulls.getPullRequestComments( - args.owner, - args.repo, - args.pull_number, - ); - return { - content: [{ type: "text", text: JSON.stringify(comments, null, 2) }], - }; - } - - case "get_pull_request_reviews": { - const args = pulls.GetPullRequestReviewsSchema.parse( - request.params.arguments, - ); - const reviews = await pulls.getPullRequestReviews( - args.owner, - args.repo, - args.pull_number, - ); - return { - content: [{ type: "text", text: JSON.stringify(reviews, null, 2) }], - }; - } - - default: - throw new Error(`Unknown tool: ${request.params.name}`); - } - } catch (error) { - if (error instanceof z.ZodError) { - throw new Error(`Invalid input: ${JSON.stringify(error.errors)}`); - } - if (isGitHubError(error)) { - throw new Error(formatGitHubError(error)); - } - throw error; - } -}); - -async function runServer() { - if ((process.env.TRANSPORT = "SSE")) { - logger.info("Connecting server through SSE transport"); - const app = express(); - - // to support multiple simultaneous connections we have a lookup object from - // sessionId to transport - const transports: { [sessionId: string]: SSEServerTransport } = {}; - - app.get("/sse", async (_: ExpressRequest, res: ExpressResponse) => { - const transport = new SSEServerTransport("/messages", res); - transports[transport.sessionId] = transport; - res.on("close", () => { - delete transports[transport.sessionId]; - }); - await server.connect(transport); - }); - - app.post("/messages", async (req: ExpressRequest, res: ExpressResponse) => { - const sessionId = req.query.sessionId as string; - const transport = transports[sessionId]; - if (transport) { - await transport.handlePostMessage(req, res); - } else { - res.status(400).send("No transport found for sessionId"); - } - }); - - const port = process.env.PORT || 3001; - app.listen(port); - logger.info(`Server listening on port ${port}`); - } else { - logger.info("Connecting server through stdio transport"); - const transport = new StdioServerTransport(); - await server.connect(transport); - } -} - -runServer().catch((error) => { - logger.error("Fatal error in main()", { - error: error instanceof Error ? error.message : String(error), - stack: error instanceof Error ? error.stack : undefined - }); - process.exit(1); -}); diff --git a/sre_agent/servers/github/operations/branches.ts b/sre_agent/servers/github/operations/branches.ts deleted file mode 100644 index 9b7033b..0000000 --- a/sre_agent/servers/github/operations/branches.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { z } from "zod"; -import { githubRequest } from "../common/utils.js"; -import { GitHubReferenceSchema } from "../common/types.js"; - -// Schema definitions -export const CreateBranchOptionsSchema = z.object({ - ref: z.string(), - sha: z.string(), -}); - -export const CreateBranchSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - branch: z.string().describe("Name for the new branch"), - from_branch: z.string().optional().describe("Optional: source branch to create from (defaults to the repository's default branch)"), -}); - -// Type exports -export type CreateBranchOptions = z.infer; - -// Function implementations -export async function getDefaultBranchSHA(owner: string, repo: string): Promise { - try { - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/main` - ); - const data = GitHubReferenceSchema.parse(response); - return data.object.sha; - } catch (error) { - const masterResponse = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/master` - ); - if (!masterResponse) { - throw new Error("Could not find default branch (tried 'main' and 'master')"); - } - const data = GitHubReferenceSchema.parse(masterResponse); - return data.object.sha; - } -} - -export async function createBranch( - owner: string, - repo: string, - options: CreateBranchOptions -): Promise> { - const fullRef = `refs/heads/${options.ref}`; - - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/git/refs`, - { - method: "POST", - body: { - ref: fullRef, - sha: options.sha, - }, - } - ); - - return GitHubReferenceSchema.parse(response); -} - -export async function getBranchSHA( - owner: string, - repo: string, - branch: string -): Promise { - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}` - ); - - const data = GitHubReferenceSchema.parse(response); - return data.object.sha; -} - -export async function createBranchFromRef( - owner: string, - repo: string, - newBranch: string, - fromBranch?: string -): Promise> { - let sha: string; - if (fromBranch) { - sha = await getBranchSHA(owner, repo, fromBranch); - } else { - sha = await getDefaultBranchSHA(owner, repo); - } - - return createBranch(owner, repo, { - ref: newBranch, - sha, - }); -} - -export async function updateBranch( - owner: string, - repo: string, - branch: string, - sha: string -): Promise> { - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`, - { - method: "PATCH", - body: { - sha, - force: true, - }, - } - ); - - return GitHubReferenceSchema.parse(response); -} diff --git a/sre_agent/servers/github/operations/commits.ts b/sre_agent/servers/github/operations/commits.ts deleted file mode 100644 index db1fec1..0000000 --- a/sre_agent/servers/github/operations/commits.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { z } from "zod"; -import { githubRequest, buildUrl } from "../common/utils.js"; - -export const ListCommitsSchema = z.object({ - owner: z.string(), - repo: z.string(), - sha: z.string().optional(), - page: z.number().optional(), - perPage: z.number().optional() -}); - -export async function listCommits( - owner: string, - repo: string, - page?: number, - perPage?: number, - sha?: string -) { - return githubRequest( - buildUrl(`https://api.github.com/repos/${owner}/${repo}/commits`, { - page: page?.toString(), - per_page: perPage?.toString(), - sha - }) - ); -} diff --git a/sre_agent/servers/github/operations/files.ts b/sre_agent/servers/github/operations/files.ts deleted file mode 100644 index 9517946..0000000 --- a/sre_agent/servers/github/operations/files.ts +++ /dev/null @@ -1,219 +0,0 @@ -import { z } from "zod"; -import { githubRequest } from "../common/utils.js"; -import { - GitHubContentSchema, - GitHubAuthorSchema, - GitHubTreeSchema, - GitHubCommitSchema, - GitHubReferenceSchema, - GitHubFileContentSchema, -} from "../common/types.js"; - -// Schema definitions -export const FileOperationSchema = z.object({ - path: z.string(), - content: z.string(), -}); - -export const CreateOrUpdateFileSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - path: z.string().describe("Path where to create/update the file"), - content: z.string().describe("Content of the file"), - message: z.string().describe("Commit message"), - branch: z.string().describe("Branch to create/update the file in"), - sha: z.string().optional().describe("SHA of the file being replaced (required when updating existing files)"), -}); - -export const GetFileContentsSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - path: z.string().describe("Path to the file or directory"), - branch: z.string().optional().describe("Branch to get contents from"), -}); - -export const PushFilesSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - branch: z.string().describe("Branch to push to (e.g., 'main' or 'master')"), - files: z.array(FileOperationSchema).describe("Array of files to push"), - message: z.string().describe("Commit message"), -}); - -export const GitHubCreateUpdateFileResponseSchema = z.object({ - content: GitHubFileContentSchema.nullable(), - commit: z.object({ - sha: z.string(), - node_id: z.string(), - url: z.string(), - html_url: z.string(), - author: GitHubAuthorSchema, - committer: GitHubAuthorSchema, - message: z.string(), - tree: z.object({ - sha: z.string(), - url: z.string(), - }), - parents: z.array( - z.object({ - sha: z.string(), - url: z.string(), - html_url: z.string(), - }) - ), - }), -}); - -// Type exports -export type FileOperation = z.infer; -export type GitHubCreateUpdateFileResponse = z.infer; - -// Function implementations -export async function getFileContents( - owner: string, - repo: string, - path: string, - branch?: string -) { - let url = `https://api.github.com/repos/${owner}/${repo}/contents/${path}`; - if (branch) { - url += `?ref=${branch}`; - } - - const response = await githubRequest(url); - const data = GitHubContentSchema.parse(response); - - // If it's a file, decode the content - if (!Array.isArray(data) && data.content) { - data.content = Buffer.from(data.content, "base64").toString("utf8"); - } - - return data; -} - -export async function createOrUpdateFile( - owner: string, - repo: string, - path: string, - content: string, - message: string, - branch: string, - sha?: string -) { - const encodedContent = Buffer.from(content).toString("base64"); - - let currentSha = sha; - if (!currentSha) { - try { - const existingFile = await getFileContents(owner, repo, path, branch); - if (!Array.isArray(existingFile)) { - currentSha = existingFile.sha; - } - } catch (error) { - console.error("Note: File does not exist in branch, will create new file"); - } - } - - const url = `https://api.github.com/repos/${owner}/${repo}/contents/${path}`; - const body = { - message, - content: encodedContent, - branch, - ...(currentSha ? { sha: currentSha } : {}), - }; - - const response = await githubRequest(url, { - method: "PUT", - body, - }); - - return GitHubCreateUpdateFileResponseSchema.parse(response); -} - -async function createTree( - owner: string, - repo: string, - files: FileOperation[], - baseTree?: string -) { - const tree = files.map((file) => ({ - path: file.path, - mode: "100644" as const, - type: "blob" as const, - content: file.content, - })); - - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/git/trees`, - { - method: "POST", - body: { - tree, - base_tree: baseTree, - }, - } - ); - - return GitHubTreeSchema.parse(response); -} - -async function createCommit( - owner: string, - repo: string, - message: string, - tree: string, - parents: string[] -) { - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/git/commits`, - { - method: "POST", - body: { - message, - tree, - parents, - }, - } - ); - - return GitHubCommitSchema.parse(response); -} - -async function updateReference( - owner: string, - repo: string, - ref: string, - sha: string -) { - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/git/refs/${ref}`, - { - method: "PATCH", - body: { - sha, - force: true, - }, - } - ); - - return GitHubReferenceSchema.parse(response); -} - -export async function pushFiles( - owner: string, - repo: string, - branch: string, - files: FileOperation[], - message: string -) { - const refResponse = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}` - ); - - const ref = GitHubReferenceSchema.parse(refResponse); - const commitSha = ref.object.sha; - - const tree = await createTree(owner, repo, files, commitSha); - const commit = await createCommit(owner, repo, message, tree.sha, [commitSha]); - return await updateReference(owner, repo, `heads/${branch}`, commit.sha); -} diff --git a/sre_agent/servers/github/operations/issues.ts b/sre_agent/servers/github/operations/issues.ts deleted file mode 100644 index ea11494..0000000 --- a/sre_agent/servers/github/operations/issues.ts +++ /dev/null @@ -1,118 +0,0 @@ -import { z } from "zod"; -import { githubRequest, buildUrl } from "../common/utils.js"; - -export const GetIssueSchema = z.object({ - owner: z.string(), - repo: z.string(), - issue_number: z.number(), -}); - -export const IssueCommentSchema = z.object({ - owner: z.string(), - repo: z.string(), - issue_number: z.number(), - body: z.string(), -}); - -export const CreateIssueOptionsSchema = z.object({ - title: z.string(), - body: z.string().optional(), - assignees: z.array(z.string()).optional(), - milestone: z.number().optional(), - labels: z.array(z.string()).optional(), -}); - -export const CreateIssueSchema = z.object({ - owner: z.string(), - repo: z.string(), - ...CreateIssueOptionsSchema.shape, -}); - -export const ListIssuesOptionsSchema = z.object({ - owner: z.string(), - repo: z.string(), - direction: z.enum(["asc", "desc"]).optional(), - labels: z.array(z.string()).optional(), - page: z.number().optional(), - per_page: z.number().optional(), - since: z.string().optional(), - sort: z.enum(["created", "updated", "comments"]).optional(), - state: z.enum(["open", "closed", "all"]).optional(), -}); - -export const UpdateIssueOptionsSchema = z.object({ - owner: z.string(), - repo: z.string(), - issue_number: z.number(), - title: z.string().optional(), - body: z.string().optional(), - assignees: z.array(z.string()).optional(), - milestone: z.number().optional(), - labels: z.array(z.string()).optional(), - state: z.enum(["open", "closed"]).optional(), -}); - -export async function getIssue(owner: string, repo: string, issue_number: number) { - return githubRequest(`https://api.github.com/repos/${owner}/${repo}/issues/${issue_number}`); -} - -export async function addIssueComment( - owner: string, - repo: string, - issue_number: number, - body: string -) { - return githubRequest(`https://api.github.com/repos/${owner}/${repo}/issues/${issue_number}/comments`, { - method: "POST", - body: { body }, - }); -} - -export async function createIssue( - owner: string, - repo: string, - options: z.infer -) { - return githubRequest( - `https://api.github.com/repos/${owner}/${repo}/issues`, - { - method: "POST", - body: options, - } - ); -} - -export async function listIssues( - owner: string, - repo: string, - options: Omit, "owner" | "repo"> -) { - const urlParams: Record = { - direction: options.direction, - labels: options.labels?.join(","), - page: options.page?.toString(), - per_page: options.per_page?.toString(), - since: options.since, - sort: options.sort, - state: options.state - }; - - return githubRequest( - buildUrl(`https://api.github.com/repos/${owner}/${repo}/issues`, urlParams) - ); -} - -export async function updateIssue( - owner: string, - repo: string, - issue_number: number, - options: Omit, "owner" | "repo" | "issue_number"> -) { - return githubRequest( - `https://api.github.com/repos/${owner}/${repo}/issues/${issue_number}`, - { - method: "PATCH", - body: options, - } - ); -} diff --git a/sre_agent/servers/github/operations/pulls.ts b/sre_agent/servers/github/operations/pulls.ts deleted file mode 100644 index c84b6fb..0000000 --- a/sre_agent/servers/github/operations/pulls.ts +++ /dev/null @@ -1,311 +0,0 @@ -import { z } from "zod"; -import { githubRequest } from "../common/utils.js"; -import { - GitHubPullRequestSchema, - GitHubIssueAssigneeSchema, - GitHubRepositorySchema, -} from "../common/types.js"; - -// Schema definitions -export const PullRequestFileSchema = z.object({ - sha: z.string(), - filename: z.string(), - status: z.enum(['added', 'removed', 'modified', 'renamed', 'copied', 'changed', 'unchanged']), - additions: z.number(), - deletions: z.number(), - changes: z.number(), - blob_url: z.string(), - raw_url: z.string(), - contents_url: z.string(), - patch: z.string().optional() -}); - -export const StatusCheckSchema = z.object({ - url: z.string(), - state: z.enum(['error', 'failure', 'pending', 'success']), - description: z.string().nullable(), - target_url: z.string().nullable(), - context: z.string(), - created_at: z.string(), - updated_at: z.string() -}); - -export const CombinedStatusSchema = z.object({ - state: z.enum(['error', 'failure', 'pending', 'success']), - statuses: z.array(StatusCheckSchema), - sha: z.string(), - total_count: z.number() -}); - -export const PullRequestCommentSchema = z.object({ - url: z.string(), - id: z.number(), - node_id: z.string(), - pull_request_review_id: z.number().nullable(), - diff_hunk: z.string(), - path: z.string().nullable(), - position: z.number().nullable(), - original_position: z.number().nullable(), - commit_id: z.string(), - original_commit_id: z.string(), - user: GitHubIssueAssigneeSchema, - body: z.string(), - created_at: z.string(), - updated_at: z.string(), - html_url: z.string(), - pull_request_url: z.string(), - author_association: z.string(), - _links: z.object({ - self: z.object({ href: z.string() }), - html: z.object({ href: z.string() }), - pull_request: z.object({ href: z.string() }) - }) -}); - -export const PullRequestReviewSchema = z.object({ - id: z.number(), - node_id: z.string(), - user: GitHubIssueAssigneeSchema, - body: z.string().nullable(), - state: z.enum(['APPROVED', 'CHANGES_REQUESTED', 'COMMENTED', 'DISMISSED', 'PENDING']), - html_url: z.string(), - pull_request_url: z.string(), - commit_id: z.string(), - submitted_at: z.string().nullable(), - author_association: z.string() -}); - -// Input schemas -export const CreatePullRequestSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - title: z.string().describe("Pull request title"), - body: z.string().optional().describe("Pull request body/description"), - head: z.string().describe("The name of the branch where your changes are implemented"), - base: z.string().describe("The name of the branch you want the changes pulled into"), - draft: z.boolean().optional().describe("Whether to create the pull request as a draft"), - maintainer_can_modify: z.boolean().optional().describe("Whether maintainers can modify the pull request") -}); - -export const GetPullRequestSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - pull_number: z.number().describe("Pull request number") -}); - -export const ListPullRequestsSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - state: z.enum(['open', 'closed', 'all']).optional().describe("State of the pull requests to return"), - head: z.string().optional().describe("Filter by head user or head organization and branch name"), - base: z.string().optional().describe("Filter by base branch name"), - sort: z.enum(['created', 'updated', 'popularity', 'long-running']).optional().describe("What to sort results by"), - direction: z.enum(['asc', 'desc']).optional().describe("The direction of the sort"), - per_page: z.number().optional().describe("Results per page (max 100)"), - page: z.number().optional().describe("Page number of the results") -}); - -export const CreatePullRequestReviewSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - pull_number: z.number().describe("Pull request number"), - commit_id: z.string().optional().describe("The SHA of the commit that needs a review"), - body: z.string().describe("The body text of the review"), - event: z.enum(['APPROVE', 'REQUEST_CHANGES', 'COMMENT']).describe("The review action to perform"), - comments: z.array( - z.union([ - z.object({ - path: z.string().describe("The relative path to the file being commented on"), - position: z.number().describe("The position in the diff where you want to add a review comment"), - body: z.string().describe("Text of the review comment") - }), - z.object({ - path: z.string().describe("The relative path to the file being commented on"), - line: z.number().describe("The line number in the file where you want to add a review comment"), - body: z.string().describe("Text of the review comment") - }) - ]) - ).optional().describe("Comments to post as part of the review (specify either position or line, not both)") -}); - -export const MergePullRequestSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - pull_number: z.number().describe("Pull request number"), - commit_title: z.string().optional().describe("Title for the automatic commit message"), - commit_message: z.string().optional().describe("Extra detail to append to automatic commit message"), - merge_method: z.enum(['merge', 'squash', 'rebase']).optional().describe("Merge method to use") -}); - -export const GetPullRequestFilesSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - pull_number: z.number().describe("Pull request number") -}); - -export const GetPullRequestStatusSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - pull_number: z.number().describe("Pull request number") -}); - -export const UpdatePullRequestBranchSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - pull_number: z.number().describe("Pull request number"), - expected_head_sha: z.string().optional().describe("The expected SHA of the pull request's HEAD ref") -}); - -export const GetPullRequestCommentsSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - pull_number: z.number().describe("Pull request number") -}); - -export const GetPullRequestReviewsSchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - pull_number: z.number().describe("Pull request number") -}); - -// Function implementations -export async function createPullRequest( - params: z.infer -): Promise> { - const { owner, repo, ...options } = CreatePullRequestSchema.parse(params); - - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/pulls`, - { - method: "POST", - body: options, - } - ); - - return GitHubPullRequestSchema.parse(response); -} - -export async function getPullRequest( - owner: string, - repo: string, - pullNumber: number -): Promise> { - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}` - ); - return GitHubPullRequestSchema.parse(response); -} - -export async function listPullRequests( - owner: string, - repo: string, - options: Omit, 'owner' | 'repo'> -): Promise[]> { - const url = new URL(`https://api.github.com/repos/${owner}/${repo}/pulls`); - - if (options.state) url.searchParams.append('state', options.state); - if (options.head) url.searchParams.append('head', options.head); - if (options.base) url.searchParams.append('base', options.base); - if (options.sort) url.searchParams.append('sort', options.sort); - if (options.direction) url.searchParams.append('direction', options.direction); - if (options.per_page) url.searchParams.append('per_page', options.per_page.toString()); - if (options.page) url.searchParams.append('page', options.page.toString()); - - const response = await githubRequest(url.toString()); - return z.array(GitHubPullRequestSchema).parse(response); -} - -export async function createPullRequestReview( - owner: string, - repo: string, - pullNumber: number, - options: Omit, 'owner' | 'repo' | 'pull_number'> -): Promise> { - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/reviews`, - { - method: 'POST', - body: options, - } - ); - return PullRequestReviewSchema.parse(response); -} - -export async function mergePullRequest( - owner: string, - repo: string, - pullNumber: number, - options: Omit, 'owner' | 'repo' | 'pull_number'> -): Promise { - return githubRequest( - `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/merge`, - { - method: 'PUT', - body: options, - } - ); -} - -export async function getPullRequestFiles( - owner: string, - repo: string, - pullNumber: number -): Promise[]> { - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/files` - ); - return z.array(PullRequestFileSchema).parse(response); -} - -export async function updatePullRequestBranch( - owner: string, - repo: string, - pullNumber: number, - expectedHeadSha?: string -): Promise { - await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/update-branch`, - { - method: "PUT", - body: expectedHeadSha ? { expected_head_sha: expectedHeadSha } : undefined, - } - ); -} - -export async function getPullRequestComments( - owner: string, - repo: string, - pullNumber: number -): Promise[]> { - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/comments` - ); - return z.array(PullRequestCommentSchema).parse(response); -} - -export async function getPullRequestReviews( - owner: string, - repo: string, - pullNumber: number -): Promise[]> { - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/reviews` - ); - return z.array(PullRequestReviewSchema).parse(response); -} - -export async function getPullRequestStatus( - owner: string, - repo: string, - pullNumber: number -): Promise> { - // First get the PR to get the head SHA - const pr = await getPullRequest(owner, repo, pullNumber); - const sha = pr.head.sha; - - // Then get the combined status for that SHA - const response = await githubRequest( - `https://api.github.com/repos/${owner}/${repo}/commits/${sha}/status` - ); - return CombinedStatusSchema.parse(response); -} diff --git a/sre_agent/servers/github/operations/repository.ts b/sre_agent/servers/github/operations/repository.ts deleted file mode 100644 index 4cf0ab9..0000000 --- a/sre_agent/servers/github/operations/repository.ts +++ /dev/null @@ -1,65 +0,0 @@ -import { z } from "zod"; -import { githubRequest } from "../common/utils.js"; -import { GitHubRepositorySchema, GitHubSearchResponseSchema } from "../common/types.js"; - -// Schema definitions -export const CreateRepositoryOptionsSchema = z.object({ - name: z.string().describe("Repository name"), - description: z.string().optional().describe("Repository description"), - private: z.boolean().optional().describe("Whether the repository should be private"), - autoInit: z.boolean().optional().describe("Initialize with README.md"), -}); - -export const SearchRepositoriesSchema = z.object({ - query: z.string().describe("Search query (see GitHub search syntax)"), - page: z.number().optional().describe("Page number for pagination (default: 1)"), - perPage: z.number().optional().describe("Number of results per page (default: 30, max: 100)"), -}); - -export const ForkRepositorySchema = z.object({ - owner: z.string().describe("Repository owner (username or organization)"), - repo: z.string().describe("Repository name"), - organization: z.string().optional().describe("Optional: organization to fork to (defaults to your personal account)"), -}); - -// Type exports -export type CreateRepositoryOptions = z.infer; - -// Function implementations -export async function createRepository(options: CreateRepositoryOptions) { - const response = await githubRequest("https://api.github.com/user/repos", { - method: "POST", - body: options, - }); - return GitHubRepositorySchema.parse(response); -} - -export async function searchRepositories( - query: string, - page: number = 1, - perPage: number = 30 -) { - const url = new URL("https://api.github.com/search/repositories"); - url.searchParams.append("q", query); - url.searchParams.append("page", page.toString()); - url.searchParams.append("per_page", perPage.toString()); - - const response = await githubRequest(url.toString()); - return GitHubSearchResponseSchema.parse(response); -} - -export async function forkRepository( - owner: string, - repo: string, - organization?: string -) { - const url = organization - ? `https://api.github.com/repos/${owner}/${repo}/forks?organization=${organization}` - : `https://api.github.com/repos/${owner}/${repo}/forks`; - - const response = await githubRequest(url, { method: "POST" }); - return GitHubRepositorySchema.extend({ - parent: GitHubRepositorySchema, - source: GitHubRepositorySchema, - }).parse(response); -} diff --git a/sre_agent/servers/github/operations/search.ts b/sre_agent/servers/github/operations/search.ts deleted file mode 100644 index c30db8e..0000000 --- a/sre_agent/servers/github/operations/search.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { z } from "zod"; -import { githubRequest, buildUrl } from "../common/utils.js"; - -export const SearchOptions = z.object({ - q: z.string(), - order: z.enum(["asc", "desc"]).optional(), - page: z.number().min(1).optional(), - per_page: z.number().min(1).max(100).optional(), -}); - -export const SearchUsersOptions = SearchOptions.extend({ - sort: z.enum(["followers", "repositories", "joined"]).optional(), -}); - -export const SearchIssuesOptions = SearchOptions.extend({ - sort: z.enum([ - "comments", - "reactions", - "reactions-+1", - "reactions--1", - "reactions-smile", - "reactions-thinking_face", - "reactions-heart", - "reactions-tada", - "interactions", - "created", - "updated", - ]).optional(), -}); - -export const SearchCodeSchema = SearchOptions; -export const SearchUsersSchema = SearchUsersOptions; -export const SearchIssuesSchema = SearchIssuesOptions; - -export async function searchCode(params: z.infer) { - return githubRequest(buildUrl("https://api.github.com/search/code", params)); -} - -export async function searchIssues(params: z.infer) { - return githubRequest(buildUrl("https://api.github.com/search/issues", params)); -} - -export async function searchUsers(params: z.infer) { - return githubRequest(buildUrl("https://api.github.com/search/users", params)); -} diff --git a/sre_agent/servers/github/package.json b/sre_agent/servers/github/package.json deleted file mode 100644 index d47d6c1..0000000 --- a/sre_agent/servers/github/package.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "name": "@modelcontextprotocol/server-github", - "version": "0.6.2", - "description": "MCP server for using the GitHub API", - "license": "MIT", - "author": "Anthropic, PBC (https://anthropic.com)", - "homepage": "https://modelcontextprotocol.io", - "bugs": "https://github.com/modelcontextprotocol/servers/issues", - "type": "module", - "bin": { - "mcp-server-github": "dist/index.js" - }, - "files": [ - "dist" - ], - "scripts": { - "build": "tsc && shx chmod +x dist/*.js", - "prepare": "npm run build", - "watch": "tsc --watch" - }, - "dependencies": { - "@modelcontextprotocol/sdk": "1.0.1", - "@types/node": "^22", - "@types/node-fetch": "^2.6.12", - "node-fetch": "^3.3.2", - "universal-user-agent": "^7.0.2", - "winston": "3.11.0", - "zod": "^3.22.4", - "zod-to-json-schema": "^3.23.5", - "express": "^5.0.1" - }, - "devDependencies": { - "shx": "^0.3.4", - "typescript": "^5.6.2", - "@types/express": "^5.0.1" - } -} diff --git a/sre_agent/servers/github/tsconfig.json b/sre_agent/servers/github/tsconfig.json deleted file mode 100644 index 087f641..0000000 --- a/sre_agent/servers/github/tsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "../../tsconfig.json", - "compilerOptions": { - "outDir": "./dist", - "rootDir": "." - }, - "include": [ - "./**/*.ts" - ] - } diff --git a/sre_agent/servers/github/utils/logger.ts b/sre_agent/servers/github/utils/logger.ts deleted file mode 100644 index cca4d58..0000000 --- a/sre_agent/servers/github/utils/logger.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { createLogger, format, transports, Logger } from 'winston'; - -// Define log levels -const levels = { - error: 0, - warn: 1, - info: 2, - debug: 3, -}; - -// Define log colors -const colors = { - error: 'red', - warn: 'yellow', - info: 'green', - debug: 'blue', -}; - -// Create the logger -const logger: Logger = createLogger({ - levels, - format: format.combine( - format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), - format.errors({ stack: true }), - format.splat(), - format.json() - ), - defaultMeta: { service: 'github-server' }, - transports: [ - // Console transport - new transports.Console({ - format: format.combine( - format.colorize({ colors }), - format.printf( - (info: any) => { - const { level, message, timestamp, ...meta } = info; - return `${timestamp} [${level}]: ${message} ${Object.keys(meta).length ? JSON.stringify(meta, null, 2) : ''}`; - } - ) - ), - }), - ], -}); - -export default logger; diff --git a/sre_agent/servers/prompt_server/pyproject.toml b/sre_agent/servers/prompt_server/pyproject.toml index b871119..f340a19 100644 --- a/sre_agent/servers/prompt_server/pyproject.toml +++ b/sre_agent/servers/prompt_server/pyproject.toml @@ -4,5 +4,5 @@ version = "0.1.0" description = "An MCP server containing prompts for the SRE agent." requires-python = ">=3.12, <4.0" dependencies = [ - "mcp[cli]>=1.6.0", + "mcp[cli]==1.6.0", ]