@aigne/example-mcp-puppeteer 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.
- package/.env.local.example +5 -0
- package/README.md +146 -0
- package/index.ts +51 -0
- package/package.json +27 -0
- package/usages.ts +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# Puppeteer MCP Server Demo
|
|
2
|
+
|
|
3
|
+
This is a demonstration of using [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework) and [Puppeteer MCP Server](https://github.com/modelcontextprotocol/servers/tree/8bd41eb0b3cf48aea0d1fe5b6c7029736092dcb1/src/puppeteer) to extract content from websites using Puppeteer.
|
|
4
|
+
|
|
5
|
+
```mermaid
|
|
6
|
+
flowchart LR
|
|
7
|
+
|
|
8
|
+
in(In)
|
|
9
|
+
out(Out)
|
|
10
|
+
agent(AI Agent)
|
|
11
|
+
puppeteer(Puppeteer MCP Agent)
|
|
12
|
+
navigate(Navigate to URL)
|
|
13
|
+
evaluate(Evaluate JS)
|
|
14
|
+
|
|
15
|
+
in --> agent <--> puppeteer
|
|
16
|
+
|
|
17
|
+
subgraph MCP Agent
|
|
18
|
+
puppeteer <--> navigate
|
|
19
|
+
puppeteer <--> evaluate
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
agent --> out
|
|
23
|
+
|
|
24
|
+
classDef inputOutput fill:#f9f0ed,stroke:#debbae,stroke-width:2px,color:#b35b39,font-weight:bolder;
|
|
25
|
+
classDef processing fill:#F0F4EB,stroke:#C2D7A7,stroke-width:2px,color:#6B8F3C,font-weight:bolder;
|
|
26
|
+
|
|
27
|
+
class in inputOutput
|
|
28
|
+
class out inputOutput
|
|
29
|
+
class agent processing
|
|
30
|
+
class puppeteer processing
|
|
31
|
+
class navigate processing
|
|
32
|
+
class evaluate processing
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Following is a sequence diagram of the workflow to summarize content from a website:
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
```mermaid
|
|
39
|
+
sequenceDiagram
|
|
40
|
+
participant User
|
|
41
|
+
participant AI as AI Agent
|
|
42
|
+
participant P as Puppeteer MCP Agent
|
|
43
|
+
participant N as Navigate to URL
|
|
44
|
+
participant E as Evaluate JS
|
|
45
|
+
|
|
46
|
+
User ->> AI: summarize content from https://www.arcblock.io
|
|
47
|
+
AI ->> P: extract content from https://www.arcblock.io
|
|
48
|
+
P ->> N: navigate to https://www.arcblock.io
|
|
49
|
+
N ->> P: navigation completed
|
|
50
|
+
P ->> E: evaluate document.body.innerText
|
|
51
|
+
E ->> P: content extracted
|
|
52
|
+
E ->> AI: extracted content as context
|
|
53
|
+
AI ->> User: The content is as follows: ...
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Prerequisites
|
|
57
|
+
|
|
58
|
+
- [Node.js](https://nodejs.org) and npm installed on your machine
|
|
59
|
+
- [OpenAI API key](https://platform.openai.com/api-keys) used to interact with OpenAI API
|
|
60
|
+
- [Pnpm](https://pnpm.io) [Optional] if you want to run the example from source code
|
|
61
|
+
|
|
62
|
+
## Try without Installation
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
export OPENAI_API_KEY=YOUR_OPENAI_API_KEY # setup your OpenAI API key
|
|
66
|
+
|
|
67
|
+
npx -y @aigne/example-mcp-puppeteer # run the example
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Installation
|
|
71
|
+
|
|
72
|
+
### Clone the Repository
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
git clone https://github.com/AIGNE-io/aigne-framework
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Install Dependencies
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
cd aigne-framework/examples/mcp-puppeteer
|
|
82
|
+
|
|
83
|
+
pnpm install
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Setup Environment Variables
|
|
87
|
+
|
|
88
|
+
Setup your OpenAI API key in the `.env.local` file:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
OPENAI_API_KEY="" # setup your OpenAI API key here
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Run the Example
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
pnpm start
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Example
|
|
101
|
+
|
|
102
|
+
The following example demonstrates how to extract content from a website:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import assert from "node:assert";
|
|
106
|
+
import { AIAgent, OpenAIChatModel, ExecutionEngine, MCPAgent } from "@aigne/core";
|
|
107
|
+
|
|
108
|
+
const { OPENAI_API_KEY } = process.env;
|
|
109
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
110
|
+
|
|
111
|
+
const model = new OpenAIChatModel({
|
|
112
|
+
apiKey: OPENAI_API_KEY,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const puppeteerMCPAgent = await MCPAgent.from({
|
|
116
|
+
command: "npx",
|
|
117
|
+
args: ["-y", "@modelcontextprotocol/server-puppeteer"],
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const engine = new ExecutionEngine({
|
|
121
|
+
model,
|
|
122
|
+
tools: [puppeteerMCPAgent],
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const agent = AIAgent.from({
|
|
126
|
+
instructions: `\
|
|
127
|
+
## Steps to extract content from a website
|
|
128
|
+
1. navigate to the url
|
|
129
|
+
2. evaluate document.body.innerText to get the content
|
|
130
|
+
`,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const result = await engine.call(agent, "extract content from https://www.arcblock.io");
|
|
134
|
+
|
|
135
|
+
console.log(result);
|
|
136
|
+
// output:
|
|
137
|
+
// {
|
|
138
|
+
// $message: "The content extracted from the website [ArcBlock](https://www.arcblock.io) is as follows:\n\n---\n\n**Redefining Software Architect and Ecosystems**\n\nA total solution for building decentralized applications ...",
|
|
139
|
+
// }
|
|
140
|
+
|
|
141
|
+
await engine.shutdown();
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
This project is licensed under the MIT License.
|
package/index.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
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 } = process.env;
|
|
15
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
16
|
+
|
|
17
|
+
logger.enable(`aigne:mcp,${process.env.DEBUG}`);
|
|
18
|
+
|
|
19
|
+
const model = new OpenAIChatModel({
|
|
20
|
+
apiKey: OPENAI_API_KEY,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const puppeteer = await MCPAgent.from({
|
|
24
|
+
command: "npx",
|
|
25
|
+
args: ["-y", "@modelcontextprotocol/server-puppeteer"],
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const engine = new ExecutionEngine({
|
|
29
|
+
model,
|
|
30
|
+
tools: [puppeteer],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const agent = AIAgent.from({
|
|
34
|
+
instructions: `\
|
|
35
|
+
## Steps to extract content from a website
|
|
36
|
+
1. navigate to the url
|
|
37
|
+
2. evaluate document.body.innerText to get the content
|
|
38
|
+
`,
|
|
39
|
+
memory: true,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const userAgent = engine.call(agent);
|
|
43
|
+
|
|
44
|
+
await runChatLoopInTerminal(userAgent, {
|
|
45
|
+
welcome:
|
|
46
|
+
"Hello! I'm a chatbot that can extract content from a website. Try asking me a question!",
|
|
47
|
+
defaultQuestion: "What is the content of https://www.arcblock.io",
|
|
48
|
+
onResponse: (response) => console.log(getMessage(response)),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
process.exit(0);
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aigne/example-mcp-puppeteer",
|
|
3
|
+
"version": "1.3.1",
|
|
4
|
+
"description": "A demonstration of using AIGNE Framework and Puppeteer MCP Server to extract content from websites using Puppeteer",
|
|
5
|
+
"author": "Arcblock <blocklet@arcblock.io> https://github.com/blocklet",
|
|
6
|
+
"homepage": "https://github.com/AIGNE-io/aigne-framework/tree/main/examples/mcp-puppeteer",
|
|
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,37 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { AIAgent, ExecutionEngine, MCPAgent, OpenAIChatModel } from "@aigne/core";
|
|
3
|
+
|
|
4
|
+
const { OPENAI_API_KEY } = process.env;
|
|
5
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
6
|
+
|
|
7
|
+
const model = new OpenAIChatModel({
|
|
8
|
+
apiKey: OPENAI_API_KEY,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const puppeteerMCPAgent = await MCPAgent.from({
|
|
12
|
+
command: "npx",
|
|
13
|
+
args: ["-y", "@modelcontextprotocol/server-puppeteer"],
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const engine = new ExecutionEngine({
|
|
17
|
+
model,
|
|
18
|
+
tools: [puppeteerMCPAgent],
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const agent = AIAgent.from({
|
|
22
|
+
instructions: `\
|
|
23
|
+
## Steps to extract content from a website
|
|
24
|
+
1. navigate to the url
|
|
25
|
+
2. evaluate document.body.innerText to get the content
|
|
26
|
+
`,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const result = await engine.call(agent, "extract content from https://www.arcblock.io");
|
|
30
|
+
|
|
31
|
+
console.log(result);
|
|
32
|
+
// output:
|
|
33
|
+
// {
|
|
34
|
+
// $message: "The content extracted from the website [ArcBlock](https://www.arcblock.io) is as follows:\n\n---\n\n**Redefining Software Architect and Ecosystems**\n\nA total solution for building decentralized applications ...",
|
|
35
|
+
// }
|
|
36
|
+
|
|
37
|
+
await engine.shutdown();
|