suvradeepp commited on
Commit
db18ba4
·
verified ·
1 Parent(s): c78e682

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -0
app.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import os
3
+ import streamlit as st
4
+ from textwrap import dedent
5
+ from agno.agent import Agent
6
+ from agno.tools.mcp import MCPTools
7
+ from mcp import ClientSession, StdioServerParameters
8
+ from mcp.client.stdio import stdio_client
9
+
10
+ st.set_page_config(page_title=" GitHub MCP Agent", page_icon="💡", layout="wide")
11
+
12
+
13
+ st.markdown("<h1 class='main-header'> GitHub MCP Agent</h1>", unsafe_allow_html=True)
14
+ st.markdown("Explore GitHub repositories with natural language using the Model Context Protocol")
15
+
16
+ # Setup sidebar for API key
17
+ with st.sidebar:
18
+ st.header("🔑 Authentication")
19
+ github_token = st.text_input("GitHub Token", type="password",
20
+ help="Create a token with repo scope at github.com/settings/tokens")
21
+
22
+ if github_token:
23
+ os.environ["GITHUB_TOKEN"] = github_token
24
+
25
+ st.markdown("---")
26
+ st.markdown("### Example Queries")
27
+
28
+ st.markdown("**Issues**")
29
+ st.markdown("- Show me issues by label")
30
+ st.markdown("- What issues are being actively discussed?")
31
+
32
+ st.markdown("**Pull Requests**")
33
+ st.markdown("- What PRs need review?")
34
+ st.markdown("- Show me recent merged PRs")
35
+
36
+ st.markdown("**Repository**")
37
+ st.markdown("- Show repository health metrics")
38
+ st.markdown("- Show repository activity patterns")
39
+
40
+ st.markdown("---")
41
+ st.caption("Note: Always specify the repository in your query if not already selected in the main input.")
42
+
43
+ # Query input
44
+ col1, col2 = st.columns([3, 1])
45
+ with col1:
46
+ repo = st.text_input("Repository", value="Shubhamsaboo/awesome-llm-apps", help="Format: owner/repo")
47
+ with col2:
48
+ query_type = st.selectbox("Query Type", [
49
+ "Issues", "Pull Requests", "Repository Activity", "Custom"
50
+ ])
51
+
52
+ # Create predefined queries based on type
53
+ if query_type == "Issues":
54
+ query_template = f"Find issues labeled as bugs in {repo}"
55
+ elif query_type == "Pull Requests":
56
+ query_template = f"Show me recent merged PRs in {repo}"
57
+ elif query_type == "Repository Activity":
58
+ query_template = f"Analyze code quality trends in {repo}"
59
+ else:
60
+ query_template = ""
61
+
62
+ query = st.text_area("Your Query", value=query_template,
63
+ placeholder="What would you like to know about this repository?")
64
+
65
+ # Main function to run agent
66
+ async def run_github_agent(message):
67
+ if not os.getenv("GITHUB_TOKEN"):
68
+ return "Error: GitHub token not provided"
69
+
70
+ try:
71
+ server_params = StdioServerParameters(
72
+ command="npx",
73
+ args=["-y", "@modelcontextprotocol/server-github"],
74
+ )
75
+
76
+ # Create client session
77
+ async with stdio_client(server_params) as (read, write):
78
+ async with ClientSession(read, write) as session:
79
+ # Initialize MCP toolkit
80
+ mcp_tools = MCPTools(session=session)
81
+ await mcp_tools.initialize()
82
+
83
+ # Create agent
84
+ agent = Agent(
85
+ tools=[mcp_tools],
86
+ instructions=dedent("""\
87
+ You are a GitHub assistant. Help users explore repositories and their activity.
88
+ - Provide organized, concise insights about the repository
89
+ - Focus on facts and data from the GitHub API
90
+ - Use markdown formatting for better readability
91
+ - Present numerical data in tables when appropriate
92
+ - Include links to relevant GitHub pages when helpful
93
+ """),
94
+ markdown=True,
95
+ show_tool_calls=True,
96
+ )
97
+
98
+ # Run agent
99
+ response = await agent.arun(message)
100
+ return response.content
101
+ except Exception as e:
102
+ return f"Error: {str(e)}"
103
+
104
+ # Run button
105
+ if st.button("🚀 Run Query", type="primary", use_container_width=True):
106
+ if not github_token:
107
+ st.error("Please enter your GitHub token in the sidebar")
108
+ elif not query:
109
+ st.error("Please enter a query")
110
+ else:
111
+ with st.spinner("Analyzing GitHub repository..."):
112
+ # Ensure the repository is explicitly mentioned in the query
113
+ if repo and repo not in query:
114
+ full_query = f"{query} in {repo}"
115
+ else:
116
+ full_query = query
117
+
118
+ result = asyncio.run(run_github_agent(full_query))
119
+
120
+ # Display results in a nice container
121
+ st.markdown("### Results")
122
+ st.markdown(result)
123
+
124
+ # Display help text for first-time users
125
+ if 'result' not in locals():
126
+ st.markdown(
127
+ """<div class='info-box'>
128
+ <h4>How to use this app:</h4>
129
+ <ol>
130
+ <li>Enter your GitHub token in the sidebar</li>
131
+ <li>Specify a repository (e.g., Shubhamsaboo/awesome-llm-apps)</li>
132
+ <li>Select a query type or write your own</li>
133
+ <li>Click 'Run Query' to see results</li>
134
+ </ol>
135
+ <p><strong>Important Notes:</strong></p>
136
+ <ul>
137
+ <li>The Model Context Protocol (MCP) provides real-time access to GitHub repositories</li>
138
+ <li>Queries work best when they focus on specific aspects like issues, PRs, or repository info</li>
139
+ <li>More specific queries yield better results</li>
140
+ <li>This app requires Node.js to be installed (for the npx command)</li>
141
+ </ul>
142
+ </div>""",
143
+ unsafe_allow_html=True
144
+ )
145
+
146
+ # Footer
147
+ st.markdown("---")
148
+ st.write("Built with Streamlit, Agno, and Model Context Protocol ❤️")