|
| 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