@aigne/example-mcp-sqlite 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 +160 -0
- package/index.ts +55 -0
- package/package.json +27 -0
- package/usages.ts +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Sqlite MCP Server Demo
|
|
2
|
+
|
|
3
|
+
This is a demonstration of using [AIGNE Framework](https://github.com/AIGNE-io/aigne-framework) and [MCP Server SQlite](https://github.com/modelcontextprotocol/servers/tree/main/src/sqlite) to interact with SQLite databases.
|
|
4
|
+
|
|
5
|
+
```mermaid
|
|
6
|
+
flowchart LR
|
|
7
|
+
|
|
8
|
+
in(In)
|
|
9
|
+
out(Out)
|
|
10
|
+
agent(Agent)
|
|
11
|
+
sqlite(SQLite MCP Server)
|
|
12
|
+
read_query(Read Query)
|
|
13
|
+
write_query(Write Query)
|
|
14
|
+
create_table(Create Table)
|
|
15
|
+
list_tables(List Tables)
|
|
16
|
+
describe_table(Describe Table)
|
|
17
|
+
|
|
18
|
+
subgraph SQLite MCP Server
|
|
19
|
+
sqlite <--> read_query
|
|
20
|
+
sqlite <--> write_query
|
|
21
|
+
sqlite <--> create_table
|
|
22
|
+
sqlite <--> list_tables
|
|
23
|
+
sqlite <--> describe_table
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
in --> agent <--> sqlite
|
|
27
|
+
agent --> out
|
|
28
|
+
|
|
29
|
+
classDef inputOutput fill:#f9f0ed,stroke:#debbae,stroke-width:2px,color:#b35b39,font-weight:bolder;
|
|
30
|
+
classDef processing fill:#F0F4EB,stroke:#C2D7A7,stroke-width:2px,color:#6B8F3C,font-weight:bolder;
|
|
31
|
+
|
|
32
|
+
class in inputOutput
|
|
33
|
+
class out inputOutput
|
|
34
|
+
class agent processing
|
|
35
|
+
class sqlite processing
|
|
36
|
+
class read_query processing
|
|
37
|
+
class write_query processing
|
|
38
|
+
class create_table processing
|
|
39
|
+
class list_tables processing
|
|
40
|
+
class describe_table processing
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Following is a sequence diagram of the workflow to interact with an SQLite database:
|
|
44
|
+
|
|
45
|
+
```mermaid
|
|
46
|
+
sequenceDiagram
|
|
47
|
+
participant User
|
|
48
|
+
participant AI as AI Agent
|
|
49
|
+
participant S as SQLite MCP Server
|
|
50
|
+
participant R as Read Query
|
|
51
|
+
|
|
52
|
+
User ->> AI: How many products?
|
|
53
|
+
AI ->> S: read_query("SELECT COUNT(*) FROM products")
|
|
54
|
+
S ->> R: execute("SELECT COUNT(*) FROM products")
|
|
55
|
+
R ->> S: 10
|
|
56
|
+
S ->> AI: 10
|
|
57
|
+
AI ->> User: There are 10 products in the database.
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Prerequisites
|
|
61
|
+
|
|
62
|
+
- [Node.js](https://nodejs.org) and npm installed on your machine
|
|
63
|
+
- [OpenAI API key](https://platform.openai.com/api-keys) used to interact with OpenAI API
|
|
64
|
+
- [uv](https://github.com/astral-sh/uv) python environment for running [MCP Server SQlite](https://github.com/modelcontextprotocol/servers/tree/main/src/sqlite)
|
|
65
|
+
- [Pnpm](https://pnpm.io) [Optional] if you want to run the example from source code
|
|
66
|
+
|
|
67
|
+
## Try without Installation
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
export OPENAI_API_KEY=YOUR_OPENAI_API_KEY # setup your OpenAI API key
|
|
71
|
+
|
|
72
|
+
npx -y @aigne/example-mcp-sqlite # run the example
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Installation
|
|
76
|
+
|
|
77
|
+
### Clone the Repository
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
git clone https://github.com/AIGNE-io/aigne-framework
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Install Dependencies
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
cd aigne-framework/examples/mcp-sqlite
|
|
87
|
+
|
|
88
|
+
pnpm install
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Setup Environment Variables
|
|
92
|
+
|
|
93
|
+
Setup your OpenAI API key in the `.env.local` file:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
OPENAI_API_KEY="" # setup your OpenAI API key here
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Run the Example
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
pnpm start
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Example
|
|
106
|
+
|
|
107
|
+
The following example demonstrates how to interact with an SQLite database:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import assert from "node:assert";
|
|
111
|
+
import { join } from "node:path";
|
|
112
|
+
import { AIAgent, OpenAIChatModel, ExecutionEngine, MCPAgent } from "@aigne/core";
|
|
113
|
+
|
|
114
|
+
const { OPENAI_API_KEY } = process.env;
|
|
115
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
116
|
+
|
|
117
|
+
const model = new OpenAIChatModel({
|
|
118
|
+
apiKey: OPENAI_API_KEY,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const sqlite = await MCPAgent.from({
|
|
122
|
+
command: "uvx",
|
|
123
|
+
args: ["-q", "mcp-server-sqlite", "--db-path", join(process.cwd(), "usages.db")],
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const engine = new ExecutionEngine({
|
|
127
|
+
model,
|
|
128
|
+
tools: [sqlite],
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const agent = AIAgent.from({
|
|
132
|
+
instructions: "You are a database administrator",
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
console.log(
|
|
136
|
+
await engine.call(agent, "create a product table with columns name description and createdAt"),
|
|
137
|
+
);
|
|
138
|
+
// output:
|
|
139
|
+
// {
|
|
140
|
+
// $message: "The product table has been created successfully with the columns: `name`, `description`, and `createdAt`.",
|
|
141
|
+
// }
|
|
142
|
+
|
|
143
|
+
console.log(await engine.call(agent, "create 10 products for test"));
|
|
144
|
+
// output:
|
|
145
|
+
// {
|
|
146
|
+
// $message: "I have successfully created 10 test products in the database. Here are the products that were added:\n\n1. Product 1: $10.99 - Description for Product 1\n2. Product 2: $15.99 - Description for Product 2\n3. Product 3: $20.99 - Description for Product 3\n4. Product 4: $25.99 - Description for Product 4\n5. Product 5: $30.99 - Description for Product 5\n6. Product 6: $35.99 - Description for Product 6\n7. Product 7: $40.99 - Description for Product 7\n8. Product 8: $45.99 - Description for Product 8\n9. Product 9: $50.99 - Description for Product 9\n10. Product 10: $55.99 - Description for Product 10\n\nIf you need any further assistance or operations, feel free to ask!",
|
|
147
|
+
// }
|
|
148
|
+
|
|
149
|
+
console.log(await engine.call(agent, "how many products?"));
|
|
150
|
+
// output:
|
|
151
|
+
// {
|
|
152
|
+
// $message: "There are 10 products in the database.",
|
|
153
|
+
// }
|
|
154
|
+
|
|
155
|
+
await engine.shutdown();
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
|
|
160
|
+
This project is licensed under the MIT License.
|
package/index.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
#!/usr/bin/env npx -y bun
|
|
2
|
+
|
|
3
|
+
import assert from "node:assert";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
AIAgent,
|
|
7
|
+
ExecutionEngine,
|
|
8
|
+
MCPAgent,
|
|
9
|
+
OpenAIChatModel,
|
|
10
|
+
PromptBuilder,
|
|
11
|
+
getMessage,
|
|
12
|
+
logger,
|
|
13
|
+
runChatLoopInTerminal,
|
|
14
|
+
} from "@aigne/core";
|
|
15
|
+
|
|
16
|
+
const { OPENAI_API_KEY } = process.env;
|
|
17
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
18
|
+
|
|
19
|
+
logger.enable(`aigne:mcp,${process.env.DEBUG}`);
|
|
20
|
+
|
|
21
|
+
const model = new OpenAIChatModel({
|
|
22
|
+
apiKey: OPENAI_API_KEY,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const sqlite = await MCPAgent.from({
|
|
26
|
+
command: "uvx",
|
|
27
|
+
args: [
|
|
28
|
+
"-q",
|
|
29
|
+
"mcp-server-sqlite",
|
|
30
|
+
"--db-path",
|
|
31
|
+
join(process.cwd(), "aigne-example-sqlite-mcp-server.db"),
|
|
32
|
+
],
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const prompt = await sqlite.prompts["mcp-demo"]?.call({ topic: "product service" });
|
|
36
|
+
if (!prompt) throw new Error("Prompt mcp-demo not found");
|
|
37
|
+
|
|
38
|
+
const engine = new ExecutionEngine({
|
|
39
|
+
model,
|
|
40
|
+
tools: [sqlite],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const agent = AIAgent.from({
|
|
44
|
+
instructions: PromptBuilder.from(prompt),
|
|
45
|
+
memory: true,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const userAgent = engine.call(agent);
|
|
49
|
+
|
|
50
|
+
await runChatLoopInTerminal(userAgent, {
|
|
51
|
+
initialCall: {},
|
|
52
|
+
onResponse: (response) => console.log(getMessage(response)),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
process.exit(0);
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aigne/example-mcp-sqlite",
|
|
3
|
+
"version": "1.3.1",
|
|
4
|
+
"description": "A demonstration of using AIGNE Framework and Sqlite MCP Server to interact with a SQLite database",
|
|
5
|
+
"author": "Arcblock <blocklet@arcblock.io> https://github.com/blocklet",
|
|
6
|
+
"homepage": "https://github.com/AIGNE-io/aigne-framework/tree/main/examples/mcp-sqlite",
|
|
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,46 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { AIAgent, ExecutionEngine, MCPAgent, OpenAIChatModel } from "@aigne/core";
|
|
4
|
+
|
|
5
|
+
const { OPENAI_API_KEY } = process.env;
|
|
6
|
+
assert(OPENAI_API_KEY, "Please set the OPENAI_API_KEY environment variable");
|
|
7
|
+
|
|
8
|
+
const model = new OpenAIChatModel({
|
|
9
|
+
apiKey: OPENAI_API_KEY,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const sqlite = await MCPAgent.from({
|
|
13
|
+
command: "uvx",
|
|
14
|
+
args: ["-q", "mcp-sqlite", "--db-path", join(process.cwd(), "usages.db")],
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const engine = new ExecutionEngine({
|
|
18
|
+
model,
|
|
19
|
+
tools: [sqlite],
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const agent = AIAgent.from({
|
|
23
|
+
instructions: "You are a database administrator",
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
console.log(
|
|
27
|
+
await engine.call(agent, "create a product table with columns name description and createdAt"),
|
|
28
|
+
);
|
|
29
|
+
// output:
|
|
30
|
+
// {
|
|
31
|
+
// $message: "The product table has been created successfully with the columns: `name`, `description`, and `createdAt`.",
|
|
32
|
+
// }
|
|
33
|
+
|
|
34
|
+
console.log(await engine.call(agent, "create 10 products for test"));
|
|
35
|
+
// output:
|
|
36
|
+
// {
|
|
37
|
+
// $message: "I have successfully created 10 test products in the database. Here are the products that were added:\n\n1. Product 1: $10.99 - Description for Product 1\n2. Product 2: $15.99 - Description for Product 2\n3. Product 3: $20.99 - Description for Product 3\n4. Product 4: $25.99 - Description for Product 4\n5. Product 5: $30.99 - Description for Product 5\n6. Product 6: $35.99 - Description for Product 6\n7. Product 7: $40.99 - Description for Product 7\n8. Product 8: $45.99 - Description for Product 8\n9. Product 9: $50.99 - Description for Product 9\n10. Product 10: $55.99 - Description for Product 10\n\nIf you need any further assistance or operations, feel free to ask!",
|
|
38
|
+
// }
|
|
39
|
+
|
|
40
|
+
console.log(await engine.call(agent, "how many products?"));
|
|
41
|
+
// output:
|
|
42
|
+
// {
|
|
43
|
+
// $message: "There are 10 products in the database.",
|
|
44
|
+
// }
|
|
45
|
+
|
|
46
|
+
await engine.shutdown();
|