Team Ai
Apppublic

suvradeepp/github-mcp

sourceHugging Faceapache-2.0updated 1y agoView on Hugging Face
0likes
app.py154 linesDownload Raw Back to root
1import asyncio2import os3import streamlit as st4from textwrap import dedent5from agno.agent import Agent6from agno.models.groq import Groq7from agno.tools.mcp import MCPTools8from mcp import ClientSession, StdioServerParameters9from mcp.client.stdio import stdio_client10from dotenv import load_dotenv11load_dotenv()  12 13GROQ_API_KEY = os.getenv("GROQ_API_KEY")14 15st.set_page_config(page_title=" GitHub MCP Agent", page_icon="๐Ÿ’ก", layout="wide")16 17 18st.markdown("<h1 class='main-header'> GitHub MCP Agent</h1>", unsafe_allow_html=True)19st.markdown("Explore GitHub repositories with natural language using the Model Context Protocol")20 21# Setup sidebar for API key22with st.sidebar:23    st.header("๐Ÿ”‘ Authentication")24    github_token = st.text_input("GitHub Token", type="password", 25                                help="Create a token with repo scope at github.com/settings/tokens")26    27    if github_token:28        os.environ["GITHUB_TOKEN"] = github_token29    30    st.markdown("---")31    st.markdown("### Example Queries")32    33    st.markdown("**Issues**")34    st.markdown("- Show me issues by label")35    st.markdown("- What issues are being actively discussed?")36    37    st.markdown("**Pull Requests**")38    st.markdown("- What PRs need review?")39    st.markdown("- Show me recent merged PRs")40    41    st.markdown("**Repository**")42    st.markdown("- Show repository health metrics")43    st.markdown("- Show repository activity patterns")44    45    st.markdown("---")46    st.caption("Note: Always specify the repository in your query if not already selected in the main input.")47 48# Query input49col1, col2 = st.columns([3, 1])50with col1:51    repo = st.text_input("Repository", value="princeton-nlp/intercode", help="Format: owner/repo")52with col2:53    query_type = st.selectbox("Query Type", [54        "Issues", "Pull Requests", "Repository Activity", "Custom"55    ])56 57# Create predefined queries based on type58if query_type == "Issues":59    query_template = f"Find issues labeled as bugs in {repo}"60elif query_type == "Pull Requests":61    query_template = f"Show me recent merged PRs in {repo}"62elif query_type == "Repository Activity":63    query_template = f"Analyze code quality trends in {repo}"64else:65    query_template = ""66 67query = st.text_area("Your Query", value=query_template, 68                     placeholder="What would you like to know about this repository?")69 70# Main function to run agent71async def run_github_agent(message):72    if not os.getenv("GITHUB_TOKEN"):73        return "Error: GitHub token not provided"74    75    try:76        server_params = StdioServerParameters(77            command="npx",78            args=["-y", "@modelcontextprotocol/server-github"],79        )80        81        # Create client session82        async with stdio_client(server_params) as (read, write):83            async with ClientSession(read, write) as session:84                # Initialize MCP toolkit85                mcp_tools = MCPTools(session=session)86                await mcp_tools.initialize()87                88                # Create agent89                agent = Agent(90                    model=Groq(id="meta-llama/llama-4-scout-17b-16e-instruct",api_key=GROQ_API_KEY),91                    tools=[mcp_tools],92                    instructions=dedent("""\93                        You are a GitHub assistant. Help users explore repositories and their activity.94                        - Provide organized, concise insights about the repository95                        - Focus on facts and data from the GitHub API96                        - Use markdown formatting for better readability97                        - Present numerical data in tables when appropriate98                        - Include links to relevant GitHub pages when helpful99                    """),100                    markdown=True,101                    show_tool_calls=True,102                )103                104                # Run agent105                response = await agent.arun(message)106                return response.content107    except Exception as e:108        return f"Error: {str(e)}"109 110# Run button111if st.button("๐Ÿš€ Run Query", type="primary", use_container_width=True):112    if not github_token:113        st.error("Please enter your GitHub token in the sidebar")114    elif not query:115        st.error("Please enter a query")116    else:117        with st.spinner("Analyzing GitHub repository..."):118            # Ensure the repository is explicitly mentioned in the query119            if repo and repo not in query:120                full_query = f"{query} in {repo}"121            else:122                full_query = query123                124            result = asyncio.run(run_github_agent(full_query))125        126        # Display results in a nice container127        st.markdown("### Results")128        st.markdown(result)129 130# Display help text for first-time users131if 'result' not in locals():132    st.markdown(133        """<div class='info-box'>134        <h4>How to use this app:</h4>135        <ol>136            <li>Enter your GitHub token in the sidebar</li>137            <li>Specify a repository (e.g., princeton-nlp/intercode)</li>138            <li>Select a query type or write your own</li>139            <li>Click 'Run Query' to see results</li>140        </ol>141        <p><strong>Important Notes:</strong></p>142        <ul>143            <li>The Model Context Protocol (MCP) provides real-time access to GitHub repositories</li>144            <li>Queries work best when they focus on specific aspects like issues, PRs, or repository info</li>145            <li>More specific queries yield better results</li>146            <li>This app requires Node.js to be installed (for the npx command)</li>147        </ul>148        </div>""", 149        unsafe_allow_html=True150    )151 152# Footer153st.markdown("---")154st.write("Built with Streamlit, Agno, and Model Context Protocol โค๏ธ")