Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
360 lines (312 loc) · 11.9 KB
/
Copy pathserver.py
File metadata and controls
360 lines (312 loc) · 11.9 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""
LLM-Hackathon 2025
This is a tailored version that doesn't rely on ivoryos client,
less convoluted, easier to debug and work with)
"""
# ivoryos_mcp_server.py
import os
from typing import Optional, Dict, List, Any
from mcp.server.fastmcp import FastMCP
import httpx
from dotenv import load_dotenv
# Configuration - Modify these defaults for your setup
load_dotenv()
mcp = FastMCP("IvoryOS MCP")
# Global HTTP client and configuration
client = httpx.Client(follow_redirects=True)
url = os.getenv("IVORYOS_URL", "http://127.0.0.1:8000/ivoryos").rstrip('/')
login_data = {
"username": os.getenv("IVORYOS_USERNAME", "admin"),
"password": os.getenv("IVORYOS_PASSWORD", "admin")
}
def _check_authentication() -> None:
"""Check and handle authentication"""
try:
resp = client.get(f"{url}/", follow_redirects=False)
if resp.status_code == httpx.codes.OK:
return
login_resp = client.post(f"{url}/auth/login", data=login_data)
if login_resp.status_code != 200:
raise Exception(f"Login failed with status {login_resp.status_code}")
except httpx.ConnectError as e:
raise Exception(f"Connection error during authentication: {e}") from e
# Direct MCP tool implementations
@mcp.tool("platform-info")
def get_platform_info() -> str:
"""Get platform information and available functions"""
try:
_check_authentication()
snapshot = client.get(f"{url}/instruments").json()
return (
"workflow execution has 3 blocks, prep, main (iterate) and cleanup.\n"
"one can execute the workflow using one of the 3 options:\n"
"1. simple repeat for static workflow with `run_workflow_repeat`\n"
"2. repeat with kwargs `run_workflow_kwargs`\n"
"3. campaign `run_workflow_campaign`\n"
f"Available functions: {snapshot}"
)
except Exception as e:
return f"Error getting platform info: {str(e)}"
@mcp.tool("execution-status")
def get_execution_status():
"""Get workflow execution status"""
try:
_check_authentication()
resp = client.get(f"{url}/executions/status")
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to get execution status: {resp.status_code}"
except Exception as e:
return f"Error getting workflow status: {str(e)}"
@mcp.tool("execute-task")
def execute_task(component: str, method: str, kwargs: Optional[Dict[str, Any]] = None):
"""Execute a robot task"""
try:
_check_authentication()
if kwargs is None:
kwargs = {}
snapshot = client.get(f"{url}/instruments").json()
if component not in snapshot:
return f"Component {component} does not exist. Available: {list(snapshot.keys())}"
kwargs["hidden_name"] = method
kwargs["hidden_wait"] = False
resp = client.post(f"{url}/instruments/{component}", json=kwargs)
if resp.status_code == httpx.codes.OK:
result = resp.json()
return f"{result}. Use `get-execution-status` to monitor."
else:
return f"Failed to execute task: {resp.status_code}"
except Exception as e:
return f"Error executing task: {str(e)}"
@mcp.tool("list-workflow-scripts")
def list_workflow_scripts(search_key: str = '', deck_name: str = ''):
"""List workflow scripts"""
try:
_check_authentication()
params = {}
if deck_name:
params['deck'] = deck_name
if search_key:
params['keyword'] = search_key
resp = client.get(
f"{url}/library/{deck_name}",
params=params
)
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to list workflow scripts: {resp.status_code}"
except Exception as e:
return f"Error listing workflow scripts: {str(e)}"
@mcp.tool("load-workflow-script")
def load_workflow_script(workflow_name: str):
"""Load a workflow script"""
try:
_check_authentication()
resp = client.get(f"{url}/library/{workflow_name}")
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to load workflow script: {resp.status_code}"
except Exception as e:
return f"Error loading workflow script: {str(e)}"
@mcp.tool("submit-workflow-script")
def submit_workflow_script(workflow_name: str, main_script: str = "",
cleanup_script: str = "", prep_script: str = ""):
"""Submit a workflow script"""
try:
_check_authentication()
resp = client.post(
url=f"{url}/draft/submit_python",
json={
"workflow_name": workflow_name,
"script": main_script,
"cleanup": cleanup_script,
"prep": prep_script
}
)
if resp.status_code == httpx.codes.OK:
return "Workflow script submitted successfully"
else:
return f"Failed to submit workflow script: {resp.status_code}"
except Exception as e:
return f"Error submitting workflow script: {str(e)}"
@mcp.tool("pause-and-resume")
def pause_and_resume():
"""Toggle pause and resume for workflow execution"""
try:
_check_authentication()
resp = client.post(f"{url}/executions/pause-resume")
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to toggle pause/resume: {resp.status_code}"
except Exception as e:
return f"Error toggling workflow pause/resume: {str(e)}"
@mcp.tool("abort-pending-workflow")
def abort_pending_workflow():
"""Abort pending workflow execution"""
try:
_check_authentication()
resp = client.post(f"{url}/executions/abort/next-iteration")
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to abort pending workflow: {resp.status_code}"
except Exception as e:
return f"Error aborting pending workflow: {str(e)}"
@mcp.tool("stop-current-workflow")
def stop_current_workflow():
"""Stop workflow execution after the current step"""
try:
_check_authentication()
resp = client.post(f"{url}/executions/abort/next-task")
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to stop current workflow: {resp.status_code}"
except Exception as e:
return f"Error stopping current workflow: {str(e)}"
@mcp.tool("run-workflow-repeat")
def run_workflow_repeat(repeat_time: Optional[int] = None):
"""Run the loaded workflow with repeat times"""
try:
_check_authentication()
resp = client.post(
f"{url}/executions/config",
json={"repeat": repeat_time if repeat_time is not None else None}
)
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to start workflow execution: {resp.status_code}"
except Exception as e:
return f"Error starting workflow execution: {str(e)}"
@mcp.tool("run-workflow-kwargs")
def run_workflow_kwargs(kwargs_list: Optional[List[Dict[str, Any]]] = None):
"""Run the loaded workflow with a list of keyword arguments"""
try:
_check_authentication()
resp = client.post(
f"{url}/executions/config",
json={"kwargs": kwargs_list}
)
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to start workflow execution: {resp.status_code}"
except Exception as e:
return f"Error starting workflow execution: {str(e)}"
@mcp.tool("run-workflow-campaign")
def run_workflow_campaign(parameters: List[Dict[str, Any]],
objectives: List[Dict[str, Any]],
repeat: int = 25,
parameter_constraints: Optional[List[str]] = None):
"""Run the loaded workflow with ax-platform (credit: Honegumi)"""
try:
_check_authentication()
if parameter_constraints is None:
parameter_constraints = []
resp = client.post(
f"{url}/executions/config",
json={
"parameters": parameters,
"objectives": objectives,
"parameter_constraints": parameter_constraints,
"repeat": repeat,
}
)
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to start workflow campaign: {resp.status_code}"
except Exception as e:
return f"Error starting workflow campaign: {str(e)}"
@mcp.tool("list-workflow-data")
def list_workflow_data(workflow_name: str = ""):
"""List workflow data"""
try:
_check_authentication()
resp = client.get(
f"{url}/executions/records",
params={"keyword": workflow_name}
)
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to list workflow data: {resp.status_code}"
except Exception as e:
return f"Error listing workflow data: {str(e)}"
@mcp.tool("load-workflow-data")
def load_workflow_data(workflow_id: int):
"""Load workflow data"""
try:
_check_authentication()
resp = client.get(f"{url}/executions/records/{workflow_id}")
if resp.status_code == httpx.codes.OK:
return resp.json()
else:
return f"Failed to load workflow data: {resp.status_code}"
except Exception as e:
return f"Error loading workflow data: {str(e)}"
# Prompts
@mcp.prompt("generate-workflow-script")
def generate_custom_script() -> str:
"""Prompt for writing a workflow script. No authentication required"""
return """
find the most appropriate function based on the task description
,and write them into a Python function without need to import the deck.
And write only needed return values as dict
```
def workflow_static():
if True:
results = deck.sdl.analyze(**{'param_1': 1, 'param_2': 2})
time.sleep(1.0)
return {'results':results,}
```
or
```
def workflow_dynamic(param_1, param_2):
if True:
results = deck.sdl.analyze(**{'param_1': param_1, 'param_1': param_2})
time.sleep(1.0)
return {'results':results,}
```
Another example of scripting using building blocks
```
def workflow_using_building_block():
results = blocks.general.analyze()
time.sleep(1.0)
return {'results':results,}
```
Please only use these available action names.
"""
@mcp.prompt("campaign-design")
def ax_campaign_design() -> str:
"""Prompt for writing a workflow campaign. No authentication required (template credit: Honegumi)"""
return """
these are examples code of creating parameters, objectives and constraints
parameters=[
{"name": "x1", "type": "fixed", "value": 10.0},
{"name": "x2", "type": "range", "bounds": [0.0, 10.0]},
{
"name": "c1",
"type": "choice",
"is_ordered": False,
"values": ["A", "B", "C"],
},
]
objectives=[
{"name": "obj_1", "minimize": True},
{"name": "obj_2", "minimize": False},
]
parameter_constraints=[
"x1 + x2 <= 15.0", # example of a sum constraint, which may be redundant/unintended if composition_constraint is also selected
"x1 + x2 <= {total}", # reparameterized compositional constraint, which is a type of sum constraint
"x1 <= x2", # example of an order constraint
"1.0*x1 + 0.5*x2 <= 15.0", # example of a linear constraint. Note the lack of space around the asterisks
],
"""
if __name__ == "__main__":
mcp.run()