@aigne/example-mcp-github 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ # Change the name of this file to .env.local and fill in the following values
2
+
3
+ DEBUG=aigne:mcp
4
+
5
+ OPENAI_API_KEY="" # Your OpenAI API key
6
+
7
+ GITHUB_PERSONAL_ACCESS_TOKEN="" # Your GitHub Personal Access Token
package/README.md ADDED
@@ -0,0 +1,218 @@
1
+ # GitHub MCP Server Integration
2
+
3
+ This is a demonstration of using [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework) and [GitHub MCP Server](https://github.com/modelcontextprotocol/servers/tree/main/src/github) to interact with GitHub repositories.
4
+
5
+ ```mermaid
6
+ flowchart LR
7
+
8
+ in(In)
9
+ out(Out)
10
+ agent(AI Agent)
11
+ github(GitHub MCP Agent)
12
+ searchRepos(Search Repositories)
13
+ getContents(Get Contents)
14
+ createFile(Create/Update File)
15
+ issue(Issues & PRs)
16
+
17
+ in --> agent <--> github
18
+
19
+ subgraph MCP Agent
20
+ github <--> searchRepos
21
+ github <--> getContents
22
+ github <--> createFile
23
+ github <--> issue
24
+ end
25
+
26
+ agent --> out
27
+
28
+ classDef inputOutput fill:#f9f0ed,stroke:#debbae,stroke-width:2px,color:#b35b39,font-weight:bolder;
29
+ classDef processing fill:#F0F4EB,stroke:#C2D7A7,stroke-width:2px,color:#6B8F3C,font-weight:bolder;
30
+
31
+ class in inputOutput
32
+ class out inputOutput
33
+ class agent processing
34
+ class github processing
35
+ class searchRepos processing
36
+ class getContents processing
37
+ class createFile processing
38
+ class issue processing
39
+ ```
40
+
41
+ Following is a sequence diagram of the workflow to search for repositories and access contents:
42
+
43
+ ```mermaid
44
+ sequenceDiagram
45
+ participant User
46
+ participant AI as AI Agent
47
+ participant G as GitHub MCP Agent
48
+ participant SR as Search Repositories
49
+ participant GC as Get Contents
50
+
51
+ User ->> AI: Search for repositories related to 'modelcontextprotocol'
52
+ AI ->> G: Use search_repositories function
53
+ G ->> SR: Execute search with query='modelcontextprotocol'
54
+ SR ->> G: Return repository list results
55
+ G ->> AI: Formatted repository information
56
+ AI ->> User: Found these repositories: modelcontextprotocol/servers, etc.
57
+ User ->> AI: Get README from the servers repo
58
+ AI ->> G: Use get_file_contents function
59
+ G ->> GC: Access README.md in modelcontextprotocol/servers
60
+ GC ->> G: Return file content
61
+ G ->> AI: Formatted file content
62
+ AI ->> User: Here's the README content: ...
63
+ ```
64
+
65
+ ## Prerequisites
66
+
67
+ - [Node.js](https://nodejs.org) and npm installed on your machine
68
+ - [OpenAI API key](https://platform.openai.com/api-keys) used to interact with OpenAI API
69
+ - [GitHub Personal Access Token](https://github.com/settings/tokens) with appropriate permissions
70
+ - [Pnpm](https://pnpm.io) [Optional] if you want to run the example from source code
71
+
72
+ ## Try without Installation
73
+
74
+ ```bash
75
+ export OPENAI_API_KEY=YOUR_OPENAI_API_KEY # Setup your OpenAI API key
76
+ export GITHUB_PERSONAL_ACCESS_TOKEN=YOUR_GITHUB_TOKEN # Setup your GitHub token
77
+
78
+ npx -y @aigne/example-mcp-github # Run the example
79
+ ```
80
+
81
+ ## Installation
82
+
83
+ ### Clone the Repository
84
+
85
+ ```bash
86
+ git clone https://github.com/AIGNE-io/aigne-framework
87
+ ```
88
+
89
+ ### Install Dependencies
90
+
91
+ ```bash
92
+ cd aigne-framework/examples/mcp-github
93
+
94
+ pnpm install
95
+ ```
96
+
97
+ ### Setup Environment Variables
98
+
99
+ Setup your API keys in the `.env.local` file:
100
+
101
+ ```bash
102
+ OPENAI_API_KEY="" # Your OpenAI API key
103
+ GITHUB_PERSONAL_ACCESS_TOKEN="" # Your GitHub Personal Access Token
104
+ ```
105
+
106
+ ### Run the Example
107
+
108
+ ```bash
109
+ pnpm start # Interactive chat interface
110
+ # or
111
+ pnpm example # Run predefined examples
112
+ ```
113
+
114
+ ## Example
115
+
116
+ The following example demonstrates how to use the GitHub MCP server to search for repositories:
117
+
118
+ ```typescript
119
+ import {
120
+ AIAgent,
121
+ OpenAIChatModel,
122
+ ExecutionEngine,
123
+ MCPAgent,
124
+ } from "@aigne/core";
125
+
126
+ // Load environment variables
127
+ const { OPENAI_API_KEY, GITHUB_PERSONAL_ACCESS_TOKEN } = process.env;
128
+
129
+ // Initialize OpenAI model
130
+ const model = new OpenAIChatModel({
131
+ apiKey: OPENAI_API_KEY,
132
+ });
133
+
134
+ // Initialize GitHub MCP agent
135
+ const githubMCPAgent = await MCPAgent.from({
136
+ command: "npx",
137
+ args: ["-y", "@modelcontextprotocol/server-github"],
138
+ env: {
139
+ GITHUB_PERSONAL_ACCESS_TOKEN,
140
+ },
141
+ });
142
+
143
+ // Create execution engine
144
+ const engine = new ExecutionEngine({
145
+ model,
146
+ tools: [githubMCPAgent],
147
+ });
148
+
149
+ // Create AI agent with GitHub-specific instructions
150
+ const agent = AIAgent.from({
151
+ instructions: `\
152
+ ## GitHub Interaction Assistant
153
+ You are an assistant that helps users interact with GitHub repositories.
154
+ You can perform various GitHub operations like:
155
+ 1. Searching repositories
156
+ 2. Getting file contents
157
+ 3. Creating or updating files
158
+ 4. Creating issues and pull requests
159
+ 5. And many more GitHub operations
160
+
161
+ Always provide clear, concise responses with relevant information from GitHub.
162
+ `,
163
+ });
164
+
165
+ // Example: Search for repositories
166
+ const result = await engine.call(
167
+ agent,
168
+ "Search for repositories related to 'modelcontextprotocol'",
169
+ );
170
+
171
+ console.log(result);
172
+ // Output:
173
+ // I found several repositories related to 'modelcontextprotocol':
174
+ //
175
+ // 1. **modelcontextprotocol/servers** - MCP servers for various APIs and services
176
+ // 2. **modelcontextprotocol/modelcontextprotocol** - The main ModelContextProtocol repository
177
+ // ...
178
+
179
+ // Shutdown the engine when done
180
+ await engine.shutdown();
181
+ ```
182
+
183
+ ## Available GitHub Operations
184
+
185
+ The GitHub MCP server provides a wide range of operations including:
186
+
187
+ 1. **Repository Operations**:
188
+
189
+ - Search repositories
190
+ - Create repositories
191
+ - Get repository information
192
+
193
+ 2. **File Operations**:
194
+
195
+ - Get file contents
196
+ - Create or update files
197
+ - Push multiple files in a single commit
198
+
199
+ 3. **Issue and PR Operations**:
200
+
201
+ - Create issues
202
+ - Create pull requests
203
+ - Add comments
204
+ - Merge pull requests
205
+
206
+ 4. **Search Operations**:
207
+
208
+ - Search code
209
+ - Search issues
210
+ - Search users
211
+
212
+ 5. **Commit Operations**:
213
+ - List commits
214
+ - Get commit details
215
+
216
+ ## License
217
+
218
+ This project is licensed under the MIT License.
package/index.ts ADDED
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env npx -y bun
2
+
3
+ import assert from "node:assert";
4
+ import {
5
+ AIAgent,
6
+ ExecutionEngine,
7
+ MCPAgent,
8
+ OpenAIChatModel,
9
+ getMessage,
10
+ logger,
11
+ runChatLoopInTerminal,
12
+ } from "@aigne/core";
13
+
14
+ const { OPENAI_API_KEY, GITHUB_PERSONAL_ACCESS_TOKEN } = process.env;
15
+ assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
16
+ assert(
17
+ GITHUB_PERSONAL_ACCESS_TOKEN,
18
+ "Please set the GITHUB_PERSONAL_ACCESS_TOKEN environment variable",
19
+ );
20
+
21
+ logger.enable(`aigne:mcp,${process.env.DEBUG}`);
22
+
23
+ const model = new OpenAIChatModel({
24
+ apiKey: OPENAI_API_KEY,
25
+ });
26
+
27
+ const github = await MCPAgent.from({
28
+ command: "npx",
29
+ args: ["-y", "@modelcontextprotocol/server-github"],
30
+ env: {
31
+ GITHUB_PERSONAL_ACCESS_TOKEN,
32
+ },
33
+ });
34
+
35
+ const engine = new ExecutionEngine({
36
+ model,
37
+ tools: [github],
38
+ });
39
+
40
+ const agent = AIAgent.from({
41
+ instructions: `\
42
+ ## GitHub Interaction Assistant
43
+ You are an assistant that helps users interact with GitHub repositories.
44
+ You can perform various GitHub operations like:
45
+ 1. Searching repositories
46
+ 2. Getting file contents
47
+ 3. Creating or updating files
48
+ 4. Creating issues and pull requests
49
+ 5. And many more GitHub operations
50
+
51
+ Always provide clear, concise responses with relevant information from GitHub.
52
+ `,
53
+ memory: true,
54
+ });
55
+
56
+ const userAgent = engine.call(agent);
57
+
58
+ await runChatLoopInTerminal(userAgent, {
59
+ welcome:
60
+ "Hello! I'm a chatbot that can help you interact with GitHub. Try asking me a question about GitHub repositories!",
61
+ defaultQuestion: "Search for repositories related to 'aigne-framework'",
62
+ onResponse: (response) => console.log(getMessage(response)),
63
+ });
64
+
65
+ process.exit(0);
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@aigne/example-mcp-github",
3
+ "version": "1.3.1",
4
+ "description": "A demonstration of using AIGNE Framework and GitHub MCP Server to interact with GitHub repositories",
5
+ "author": "Arcblock <blocklet@arcblock.io> https://github.com/blocklet",
6
+ "homepage": "https://github.com/AIGNE-io/aigne-framework/tree/main/examples/mcp-github",
7
+ "license": "ISC",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/AIGNE-io/aigne-framework"
11
+ },
12
+ "bin": "index.ts",
13
+ "files": [
14
+ ".env.local.example",
15
+ "*.ts",
16
+ "README.md"
17
+ ],
18
+ "dependencies": {
19
+ "openai": "^4.89.0",
20
+ "zod": "^3.24.2",
21
+ "@aigne/core": "^1.3.1"
22
+ },
23
+ "scripts": {
24
+ "start": "npx -y bun run index.ts",
25
+ "lint": "tsc --noEmit"
26
+ }
27
+ }
package/usages.ts ADDED
@@ -0,0 +1,69 @@
1
+ import assert from "node:assert";
2
+ import { AIAgent, ExecutionEngine, MCPAgent, OpenAIChatModel } from "@aigne/core";
3
+
4
+ const { OPENAI_API_KEY, GITHUB_PERSONAL_ACCESS_TOKEN } = process.env;
5
+ assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
6
+ assert(
7
+ GITHUB_PERSONAL_ACCESS_TOKEN,
8
+ "Please set the GITHUB_PERSONAL_ACCESS_TOKEN environment variable",
9
+ );
10
+
11
+ const model = new OpenAIChatModel({
12
+ apiKey: OPENAI_API_KEY,
13
+ });
14
+
15
+ const githubMCPAgent = await MCPAgent.from({
16
+ command: "npx",
17
+ args: ["-y", "@modelcontextprotocol/server-github"],
18
+ env: {
19
+ GITHUB_PERSONAL_ACCESS_TOKEN,
20
+ },
21
+ });
22
+
23
+ const engine = new ExecutionEngine({
24
+ model,
25
+ tools: [githubMCPAgent],
26
+ });
27
+
28
+ const agent = AIAgent.from({
29
+ instructions: `\
30
+ ## GitHub Interaction Assistant
31
+ You are an assistant that helps users interact with GitHub repositories.
32
+ You can perform various GitHub operations like:
33
+ 1. Searching repositories
34
+ 2. Getting file contents
35
+ 3. Creating or updating files
36
+ 4. Creating issues and pull requests
37
+ 5. And many more GitHub operations
38
+
39
+ Always provide clear, concise responses with relevant information from GitHub.
40
+ `,
41
+ });
42
+
43
+ // Example 1: Search for repositories
44
+ console.log("Example 1: Searching for repositories");
45
+ const searchResult = await engine.call(
46
+ agent,
47
+ "Search for repositories related to 'modelcontextprotocol' and limit to 3 results",
48
+ );
49
+ console.log(searchResult);
50
+ console.log("\n------------------------\n");
51
+
52
+ // Example 2: Get file contents
53
+ console.log("Example 2: Getting file contents");
54
+ const fileResult = await engine.call(
55
+ agent,
56
+ "Get the content of README.md from modelcontextprotocol/servers repository",
57
+ );
58
+ console.log(fileResult);
59
+ console.log("\n------------------------\n");
60
+
61
+ // Example 3: List commits
62
+ console.log("Example 3: Listing commits");
63
+ const commitsResult = await engine.call(
64
+ agent,
65
+ "List the latest 3 commits from the modelcontextprotocol/servers repository",
66
+ );
67
+ console.log(commitsResult);
68
+
69
+ await engine.shutdown();