-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_mcp_with_localstack.js
More file actions
197 lines (167 loc) · 6.1 KB
/
test_mcp_with_localstack.js
File metadata and controls
197 lines (167 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Test MCP SysOperator Server with LocalStack
// This script demonstrates how to use the MCP SysOperator server with LocalStack
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const os = require('os');
const { runAwsOperation } = require('./localstack_aws_operations.js');
// Helper function to execute shell commands
function runCommand(command) {
console.log(`Executing: ${command}`);
try {
const output = execSync(command, { encoding: 'utf8' });
console.log(output);
return output;
} catch (error) {
console.error(`Error executing command: ${error.message}`);
console.error(error.stderr);
throw error;
}
}
// Create a temporary directory for our test files
function createTempDir() {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mcp-localstack-'));
console.log(`Created temporary directory: ${tempDir}`);
return tempDir;
}
// Create a playbook that uses awslocal instead of aws
function createLocalStackPlaybook(tempDir) {
const playbookContent = `---
# Example Ansible playbook for LocalStack
- name: LocalStack AWS Operations
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: List S3 buckets using awslocal
shell: awslocal s3 ls
register: s3_buckets
- name: Display S3 buckets
debug:
var: s3_buckets.stdout_lines
- name: Create a new S3 bucket
shell: awslocal s3 mb s3://mcp-test-bucket
register: create_bucket
ignore_errors: yes
- name: Display bucket creation result
debug:
var: create_bucket
- name: Upload a file to the bucket
shell: echo "Hello from MCP Ansible" > /tmp/mcp-test.txt && awslocal s3 cp /tmp/mcp-test.txt s3://mcp-test-bucket/test.txt
register: upload_file
- name: Display upload result
debug:
var: upload_file
- name: List objects in the bucket
shell: awslocal s3 ls s3://mcp-test-bucket
register: list_objects
- name: Display objects
debug:
var: list_objects.stdout_lines
- name: Create EC2 instance
shell: awslocal ec2 run-instances --image-id ami-12345678 --instance-type t2.micro --count 1
register: ec2_instance
- name: Display EC2 instance
debug:
var: ec2_instance.stdout
- name: List EC2 instances
shell: awslocal ec2 describe-instances
register: ec2_instances
- name: Display EC2 instances
debug:
var: ec2_instances.stdout
`;
const playbookPath = path.join(tempDir, 'mcp_localstack_playbook.yml');
fs.writeFileSync(playbookPath, playbookContent);
console.log(`Created playbook at: ${playbookPath}`);
return playbookPath;
}
// Create a simple inventory file
function createInventory(tempDir) {
const inventoryContent = `[local]
localhost ansible_connection=local
`;
const inventoryPath = path.join(tempDir, 'inventory.ini');
fs.writeFileSync(inventoryPath, inventoryContent);
console.log(`Created inventory at: ${inventoryPath}`);
return inventoryPath;
}
// Test the MCP SysOperator server with LocalStack
async function testMcpWithLocalStack() {
try {
// Check if LocalStack is running
console.log("Checking if LocalStack is running...");
runCommand('awslocal s3 ls');
console.log("LocalStack is running!");
// Create temporary directory, playbook, and inventory
const tempDir = createTempDir();
const playbookPath = createLocalStackPlaybook(tempDir);
const inventoryPath = createInventory(tempDir);
// Test direct AWS operations with LocalStack
console.log("\nTesting direct AWS operations with LocalStack...");
// List S3 buckets
console.log("\nListing S3 buckets:");
const buckets = runAwsOperation('s3', 'list_buckets');
console.log(buckets);
// Create a bucket
console.log("\nCreating S3 bucket:");
try {
const createBucket = runAwsOperation('s3', 'create_bucket', { bucket: 'mcp-direct-test-bucket' });
console.log(createBucket);
} catch (error) {
console.log("Bucket may already exist, continuing...");
}
// Test MCP SysOperator server with run_playbook
console.log("\nTesting MCP SysOperator server with run_playbook...");
console.log("This would normally use the MCP tool:");
console.log(`
<use_mcp_tool>
<server_name>sysoperator</server_name>
<tool_name>run_playbook</tool_name>
<arguments>
{
"playbook": "${playbookPath}",
"inventory": "${inventoryPath}"
}
</arguments>
</use_mcp_tool>
`);
// Since we can't directly use the MCP tool in this script,
// we'll run the playbook using ansible-playbook directly
console.log("\nRunning playbook with ansible-playbook...");
runCommand(`ansible-playbook ${playbookPath} -i ${inventoryPath}`);
// Test MCP SysOperator server with run_ad_hoc
console.log("\nTesting MCP SysOperator server with run_ad_hoc...");
console.log("This would normally use the MCP tool:");
console.log(`
<use_mcp_tool>
<server_name>sysoperator</server_name>
<tool_name>run_ad_hoc</tool_name>
<arguments>
{
"pattern": "localhost",
"module": "shell",
"args": "awslocal s3 ls"
}
</arguments>
</use_mcp_tool>
`);
// Since we can't directly use the MCP tool in this script,
// we'll run the ad-hoc command using ansible directly
console.log("\nRunning ad-hoc command with ansible...");
runCommand(`ansible localhost -i ${inventoryPath} -m shell -a "awslocal s3 ls"`);
// Clean up
console.log("\nCleaning up...");
fs.rmSync(tempDir, { recursive: true, force: true });
console.log(`Removed temporary directory: ${tempDir}`);
console.log("\nTest completed successfully!");
console.log("\nTo use these tests with the MCP SysOperator server, you would:");
console.log("1. Modify src/sysoperator/common/utils.ts to use awslocal instead of aws");
console.log("2. Rebuild the server with npm run build");
console.log("3. Use the MCP tools as shown in the examples above");
} catch (error) {
console.error("Test failed:", error);
}
}
// Run the test
testMcpWithLocalStack();