Skip to main content
Glama
GitHub MCP Server

GitHub MCP Server

by valiot
README.md
# GitHub MCP Server

A Model Context Protocol (MCP) server for GitHub operations, enabling LLMs to interact with GitHub repositories through a standardized interface. Deployable to Cloudflare Workers for production use.

## Features

- πŸ”Œ **MCP Protocol Compliant**: Works with any MCP-compatible LLM (OpenAI, Claude, Cursor, etc.)
- πŸš€ **9 GitHub Operations**: Comprehensive PR management capabilities
- βœ… **Type-Safe**: Full TypeScript with runtime validation using Zod
- ☁️ **Cloudflare Workers Ready**: Deploy to the edge with zero configuration
- πŸ§ͺ **Tested**: Golden tests for schema and behavior validation
- πŸ” **Secure**: GitHub Personal Access Token authentication

## Available Tools

1. **list_prs** - List pull requests with filters (state, base, head)
2. **get_pr** - Get detailed PR information
3. **create_pr** - Create new pull request
4. **update_pr** - Update PR title, description, state, or base branch
5. **add_pr_comment** - Add comment to a PR
6. **merge_pr** - Merge PR with specified method (merge/squash/rebase)
7. **request_pr_reviewers** - Request PR reviewers (users or teams)
8. **add_pr_labels** - Add labels to PR
9. **remove_pr_labels** - Remove labels from PR

## Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     LLM     β”‚  (OpenAI, Claude, Cursor, etc.)
β”‚  (Client)   β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚ MCP Protocol
       β”‚ (stdio or HTTP)
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   MCP Server    β”‚
β”‚  (This repo)    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ Tool Registry β”‚
β”‚ β€’ Validation    β”‚
β”‚ β€’ Error Handler β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β”‚ Octokit REST API
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   GitHub API    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Setup

### Prerequisites

- Node.js 18+
- GitHub Personal Access Token with appropriate permissions

### Installation

```bash
# Clone or create project
npm install

# Set GitHub token
export GITHUB_TOKEN="ghp_your_token_here"

# Build project
npm run build
```

### Creating a GitHub Token

1. Go to GitHub Settings β†’ Developer settings β†’ Personal access tokens β†’ Tokens (classic)
2. Generate new token with these scopes:
   - `repo` (Full control of private repositories)
   - `read:org` (if working with organization repos)
3. Copy the token and set as `GITHUB_TOKEN` environment variable

## Usage

### Local Development (stdio)

Run the MCP server locally using stdio transport:

```bash
export GITHUB_TOKEN="your_token"
npm run dev
```

The server will run on stdio and can be connected to by MCP clients.

### Test Client

Run the example test client to see the server in action:

```bash
export GITHUB_TOKEN="your_token"
npm run client
```

This will:
1. Start the MCP server
2. Connect via stdio transport
3. List available tools
4. Execute example operations (list PRs, get PR details)
5. Display results

### Integration with Cursor

To use with Cursor, add to your MCP configuration:

```json
{
  "mcpServers": {
    "github": {
      "command": "node",
      "args": ["path/to/mcp-deployable/dist/server/index.js"],
      "env": {
        "GITHUB_TOKEN": "your_token_here"
      }
    }
  }
}
```

### Integration with OpenAI

```typescript
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
import { spawn } from 'child_process';
import OpenAI from 'openai';

// Start MCP server
const serverProcess = spawn('node', ['dist/server/index.js'], {
  env: { GITHUB_TOKEN: process.env.GITHUB_TOKEN },
});

// Connect MCP client
const client = new Client({
  name: 'openai-github-client',
  version: '1.0.0',
}, { capabilities: {} });

const transport = new StdioClientTransport({ command: serverProcess });
await client.connect(transport);

// Get available tools
const tools = await client.listTools();

// Use with OpenAI
const openai = new OpenAI();
const response = await openai.chat.completions.create({
  model: 'gpt-4',
  messages: [
    { role: 'user', content: 'List open PRs in owner/repo' }
  ],
  tools: tools.tools.map(tool => ({
    type: 'function',
    function: {
      name: tool.name,
      description: tool.description,
      parameters: tool.inputSchema,
    },
  })),
});

// Execute tool if requested
if (response.choices[0].message.tool_calls) {
  const toolCall = response.choices[0].message.tool_calls[0];
  const result = await client.callTool({
    name: toolCall.function.name,
    arguments: JSON.parse(toolCall.function.arguments),
  });
  console.log(result);
}
```

## Cloudflare Workers Deployment

### Prerequisites

- Cloudflare account
- Wrangler CLI installed (`npm install -g wrangler`)

### Deploy

```bash
# Build worker bundle
npm run build:worker

# Set GitHub token as secret
wrangler secret put GITHUB_TOKEN

# Deploy to Cloudflare Workers
npm run deploy
```

### HTTP API

Once deployed, the worker exposes these endpoints:

#### Health Check
```bash
curl https://your-worker.workers.dev/health
```

#### List Tools
```bash
curl https://your-worker.workers.dev/tools
```

#### Invoke Tool
```bash
curl -X POST https://your-worker.workers.dev/invoke \
  -H "Content-Type: application/json" \
  -d '{
    "tool": "list_prs",
    "arguments": {
      "owner": "octocat",
      "repo": "hello-world",
      "state": "open"
    }
  }'
```

## Development

### Project Structure

```
mcp-deployable/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ server/
β”‚   β”‚   β”œβ”€β”€ index.ts          # MCP server (stdio)
β”‚   β”‚   β”œβ”€β”€ tools/
β”‚   β”‚   β”‚   └── github-tools.ts  # GitHub API operations
β”‚   β”‚   └── schemas/
β”‚   β”‚       └── tools.ts      # Zod schemas
β”‚   β”œβ”€β”€ client/
β”‚   β”‚   └── test-client.ts    # Example client
β”‚   β”œβ”€β”€ types/
β”‚   β”‚   └── github.ts         # TypeScript types
β”‚   └── worker.ts             # Cloudflare Workers entry
β”œβ”€β”€ tests/
β”‚   └── golden/
β”‚       β”œβ”€β”€ github-tools.test.ts  # Golden tests
β”‚       └── fixtures/         # Mock responses
β”œβ”€β”€ wrangler.toml             # Workers config
β”œβ”€β”€ package.json
└── tsconfig.json
```

### Running Tests

```bash
# Run all tests
npm test

# Run golden tests only
npm run test:golden

# Run tests in watch mode
npm test -- --watch
```

### Scripts

- `npm run build` - Build TypeScript and worker bundle
- `npm run dev` - Run MCP server locally (stdio)
- `npm run client` - Run test client
- `npm test` - Run test suite
- `npm run deploy` - Deploy to Cloudflare Workers

## Tool Examples

### List Pull Requests

```json
{
  "tool": "list_prs",
  "arguments": {
    "owner": "modelcontextprotocol",
    "repo": "typescript-sdk",
    "state": "open",
    "per_page": 10
  }
}
```

### Get Pull Request Details

```json
{
  "tool": "get_pr",
  "arguments": {
    "owner": "modelcontextprotocol",
    "repo": "typescript-sdk",
    "pull_number": 42
  }
}
```

### Create Pull Request

```json
{
  "tool": "create_pr",
  "arguments": {
    "owner": "myorg",
    "repo": "myrepo",
    "title": "Add new feature",
    "body": "This PR adds feature X",
    "head": "feature-branch",
    "base": "main",
    "draft": false
  }
}
```

### Update Pull Request

```json
{
  "tool": "update_pr",
  "arguments": {
    "owner": "myorg",
    "repo": "myrepo",
    "pull_number": 42,
    "title": "Updated title",
    "body": "Updated description"
  }
}
```

### Add Comment

```json
{
  "tool": "add_pr_comment",
  "arguments": {
    "owner": "myorg",
    "repo": "myrepo",
    "pull_number": 42,
    "body": "Great work! LGTM πŸ‘"
  }
}
```

### Merge Pull Request

```json
{
  "tool": "merge_pr",
  "arguments": {
    "owner": "myorg",
    "repo": "myrepo",
    "pull_number": 42,
    "merge_method": "squash",
    "commit_title": "feat: add new feature"
  }
}
```

### Request Reviewers

```json
{
  "tool": "request_pr_reviewers",
  "arguments": {
    "owner": "myorg",
    "repo": "myrepo",
    "pull_number": 42,
    "reviewers": ["user1", "user2"],
    "team_reviewers": ["team-alpha"]
  }
}
```

### Manage Labels

```json
{
  "tool": "add_pr_labels",
  "arguments": {
    "owner": "myorg",
    "repo": "myrepo",
    "pull_number": 42,
    "labels": ["bug", "high-priority"]
  }
}
```

## Error Handling

All tools include comprehensive error handling:

- **Validation Errors**: Input parameters are validated using Zod schemas
- **GitHub API Errors**: HTTP errors from GitHub API are caught and formatted
- **Rate Limiting**: GitHub API rate limit errors are properly surfaced
- **Authentication Errors**: Missing or invalid tokens return clear error messages

Example error response:

```json
{
  "error": "GitHub API error (404): Not Found",
  "code": "INTERNAL_ERROR"
}
```

## Security

- **Authentication**: Uses GitHub Personal Access Token (PAT)
- **Least Privilege**: Configure tokens with only necessary scopes
- **Environment Variables**: Secrets stored in env vars, never committed
- **Input Validation**: All inputs validated before GitHub API calls

## License

MIT

## Contributing

Contributions welcome! Please:

1. Fork the repository
2. Create a feature branch
3. Add tests for new functionality
4. Ensure all tests pass
5. Submit a pull request

## Support

For issues or questions:
- Open an issue on GitHub
- Check existing issues for solutions
- Review MCP protocol documentation: https://modelcontextprotocol.io