GitHub MCP agent

Using the GitHub MCP server to create an Agent that can explore, analyze and provide insights about GitHub repositories:

1"""🐙 MCP GitHub Agent - Your Personal GitHub Explorer!
2
3This example shows how to create a GitHub agent that uses MCP to explore,
4analyze, and provide insights about GitHub repositories. The agent leverages the Model
5Context Protocol (MCP) to interact with GitHub, allowing it to answer questions
6about issues, pull requests, repository details and more.
7
8Example prompts to try:
9- "List open issues in the repository"
10- "Show me recent pull requests"
11- "What are the repository statistics?"
12- "Find issues labeled as bugs"
13- "Show me contributor activity"
14
15Run: `uv pip install kern-ai mcp openai` to install the dependencies
16Environment variables needed:
17- Create a GitHub personal access token following these steps:
18 - https://github.com/modelcontextprotocol/servers/tree/main/src/github#setup
19- export GITHUB_TOKEN: Your GitHub personal access token
20"""
21
22import asyncio
23import os
24from textwrap import dedent
25
26from kern.agent import Agent
27from kern.tools.mcp import MCPTools
28from mcp import StdioServerParameters
29
30
31async def run_agent(message: str) -> None:
32 """Run the GitHub agent with the given message."""
33
34 # Initialize the MCP server
35 server_params = StdioServerParameters(
36 command="npx",
37 args=["-y", "@modelcontextprotocol/server-github"],
38 )
39
40 # Create a client session to connect to the MCP server
41 async with MCPTools(server_params=server_params) as mcp_tools:
42 agent = Agent(
43 tools=[mcp_tools],
44 instructions=dedent("""\
45 You are a GitHub assistant. Help users explore repositories and their activity.
46
47 - Use headings to organize your responses
48 - Be concise and focus on relevant information\
49 """),
50 markdown=True,
51 )
52
53 # Run the agent
54 await agent.aprint_response(message, stream=True)
55
56
57# Example usage
58if __name__ == "__main__":
59 # Pull request example
60 asyncio.run(
61 run_agent(
62 "Tell me about Kern. Github repo: https://github.com/kern-ai/kern. You can read the README for more information."
63 )
64 )
65
66
67# More example prompts to explore:
68"""
69Issue queries:
701. "Find issues needing attention"
712. "Show me issues by label"
723. "What issues are being actively discussed?"
734. "Find related issues"
745. "Analyze issue resolution patterns"
75
76Pull request queries:
771. "What PRs need review?"
782. "Show me recent merged PRs"
793. "Find PRs with conflicts"
804. "What features are being developed?"
815. "Analyze PR review patterns"
82
83Repository queries:
841. "Show repository health metrics"
852. "What are the contribution guidelines?"
863. "Find documentation gaps"
874. "Analyze code quality trends"
885. "Show repository activity patterns"
89"""