Skip to content

Commit 6930c0f

Browse files
authored
Python: Added GitHub MCP sample with PAT (#2967)
* added github mcp sample with PAT * addressed copilot fixes * env fix
1 parent d83cf93 commit 6930c0f

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

python/samples/getting_started/mcp/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ The Model Context Protocol (MCP) is an open standard for connecting AI agents to
1212
|--------|------|-------------|
1313
| **Agent as MCP Server** | [`agent_as_mcp_server.py`](agent_as_mcp_server.py) | Shows how to expose an Agent Framework agent as an MCP server that other AI applications can connect to |
1414
| **API Key Authentication** | [`mcp_api_key_auth.py`](mcp_api_key_auth.py) | Demonstrates API key authentication with MCP servers |
15+
| **GitHub Integration with PAT** | [`mcp_github_pat.py`](mcp_github_pat.py) | Demonstrates connecting to GitHub's MCP server using Personal Access Token (PAT) authentication |
1516

1617
## Prerequisites
1718

1819
- `OPENAI_API_KEY` environment variable
1920
- `OPENAI_RESPONSES_MODEL_ID` environment variable
21+
22+
For `mcp_github_pat.py`:
23+
- `GITHUB_PAT` - Your GitHub Personal Access Token (create at https://github.com/settings/tokens)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
import os
5+
6+
from agent_framework import ChatAgent, HostedMCPTool
7+
from agent_framework.openai import OpenAIResponsesClient
8+
from dotenv import load_dotenv
9+
10+
"""
11+
MCP GitHub Integration with Personal Access Token (PAT)
12+
13+
This example demonstrates how to connect to GitHub's remote MCP server using a Personal Access
14+
Token (PAT) for authentication. The agent can use GitHub operations like searching repositories,
15+
reading files, creating issues, and more depending on how you scope your token.
16+
17+
Prerequisites:
18+
1. A GitHub Personal Access Token with appropriate scopes
19+
- Create one at: https://github.com/settings/tokens
20+
- For read-only operations, you can use more restrictive scopes
21+
2. Environment variables:
22+
- GITHUB_PAT: Your GitHub Personal Access Token (required)
23+
- OPENAI_API_KEY: Your OpenAI API key (required)
24+
- OPENAI_RESPONSES_MODEL_ID: Your OpenAI model ID (required)
25+
"""
26+
27+
28+
async def github_mcp_example() -> None:
29+
"""Example of using GitHub MCP server with PAT authentication."""
30+
# 1. Load environment variables from .env file if present
31+
load_dotenv()
32+
33+
# 2. Get configuration from environment
34+
github_pat = os.getenv("GITHUB_PAT")
35+
if not github_pat:
36+
raise ValueError(
37+
"GITHUB_PAT environment variable must be set. Create a token at https://github.com/settings/tokens"
38+
)
39+
40+
# 3. Create authentication headers with GitHub PAT
41+
auth_headers = {
42+
"Authorization": f"Bearer {github_pat}",
43+
}
44+
45+
# 4. Create MCP tool with authentication
46+
# HostedMCPTool manages the connection to the MCP server and makes its tools available
47+
# Set approval_mode="never_require" to allow the MCP tool to execute without approval
48+
github_mcp_tool = HostedMCPTool(
49+
name="GitHub",
50+
description="Tool for interacting with GitHub.",
51+
url="https://api.githubcopilot.com/mcp/",
52+
headers=auth_headers,
53+
approval_mode="never_require",
54+
)
55+
56+
# 5. Create agent with the GitHub MCP tool
57+
async with ChatAgent(
58+
chat_client=OpenAIResponsesClient(),
59+
name="GitHubAgent",
60+
instructions=(
61+
"You are a helpful assistant that can help users interact with GitHub. "
62+
"You can search for repositories, read file contents, check issues, and more. "
63+
"Always be clear about what operations you're performing."
64+
),
65+
tools=github_mcp_tool,
66+
) as agent:
67+
# Example 1: Get authenticated user information
68+
query1 = "What is my GitHub username and tell me about my account?"
69+
print(f"\nUser: {query1}")
70+
result1 = await agent.run(query1)
71+
print(f"Agent: {result1.text}")
72+
73+
# Example 2: List my repositories
74+
query2 = "List all the repositories I own on GitHub"
75+
print(f"\nUser: {query2}")
76+
result2 = await agent.run(query2)
77+
print(f"Agent: {result2.text}")
78+
79+
80+
if __name__ == "__main__":
81+
asyncio.run(github_mcp_example())

0 commit comments

Comments
 (0)