-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-template.py
More file actions
executable file
·64 lines (51 loc) · 1.92 KB
/
tool-template.py
File metadata and controls
executable file
·64 lines (51 loc) · 1.92 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
#!/usr/bin/env python3
import sys
import os
import re
import json
import urllib.request
import urllib.parse
from datetime import datetime
# Toolbox Template - Python with external service integration via built-in urllib
action = os.environ.get('TOOLBOX_ACTION', '')
if action == 'describe':
print("""name: my_example_tool
description: A sample tool that sends text to httpbin and returns response
text: string Text to send to the service""")
elif action == 'execute':
try:
input_data = sys.stdin.read()
# Extract text parameter
text_match = re.search(r'^text:\s*(.*)$', input_data, re.MULTILINE)
text = text_match.group(1) if text_match else ''
if not text:
print("Error: text parameter required", file=sys.stderr)
sys.exit(1)
# Prepare request data
post_data = {
'message': text,
'timestamp': datetime.now().isoformat()
}
# Make HTTP request using built-in urllib
data = json.dumps(post_data).encode('utf-8')
req = urllib.request.Request(
'https://httpbin.org/post',
data=data,
headers={
'Content-Type': 'application/json',
'User-Agent': 'Amp-Toolbox-Python/1.0'
},
method='POST'
)
with urllib.request.urlopen(req) as response:
if response.status != 200:
raise Exception(f"HTTP {response.status}: {response.reason}")
result = json.loads(response.read().decode('utf-8'))
print(f"Service processed: {result['json']['message']}")
print(f"Echo from: {result['origin']}")
except Exception as error:
print(f"Error: {error}", file=sys.stderr)
sys.exit(1)
else:
print("Error: TOOLBOX_ACTION must be 'describe' or 'execute'", file=sys.stderr)
sys.exit(1)