jdewitte commited on
Commit
34a3e61
·
1 Parent(s): e1876af

Finalize stack

Browse files
Files changed (1) hide show
  1. prestashop_mcp/server.py +14 -25
prestashop_mcp/server.py CHANGED
@@ -197,41 +197,30 @@ class PrestaShopMCPServer:
197
  return DEFAULT_API_BASE
198
 
199
  async def _make_api_request(self, endpoint: str, params: Dict[str, Any] = None, client_token: str = None) -> Dict[str, Any]:
200
- """Make HTTP request to PrestaShop Agent API"""
201
 
202
- # Resolve client_token to get the actual shop URL
203
  if not client_token:
204
  raise ValueError("client_token is required")
205
-
206
- api_base_url = await self._resolve_client_token(client_token)
207
-
208
- # Use resolved API base URL - call agent API directly
209
- base_url = api_base_url.rstrip('/')
210
 
211
  # Extract action from params for JSON body
212
  action = params.get('action') if params else None
213
  if not action:
214
  raise ValueError("action is required in params")
215
 
216
- # Build URL with only routing parameters (no action)
217
- url_params = {
218
- "fc": "module",
219
- "module": "ambaiagent",
220
- "controller": "api"
221
- }
222
 
223
- # Prepare JSON body with action and all parameters
224
- json_body = {}
 
 
225
  if params:
226
- json_body = params.copy() # Include action in JSON body
227
-
228
- url = f"{base_url}/index.php"
229
 
230
  async with httpx.AsyncClient(timeout=DEFAULT_TIMEOUT) as client:
231
  try:
232
  response = await client.post(
233
- url,
234
- params=url_params,
235
  json=json_body,
236
  headers={
237
  "Content-Type": "application/json",
@@ -242,22 +231,22 @@ class PrestaShopMCPServer:
242
 
243
  if response.status_code != 200:
244
  error_text = response.text[:500] if response.text else "No error details"
245
- raise Exception(f"Agent API error {response.status_code}: {error_text}")
246
 
247
  data = response.json()
248
 
249
- # Handle agent API response format
250
  if data.get('error'):
251
- raise Exception(f"Agent API error: {data.get('error')}")
252
 
253
  return data
254
 
255
  except httpx.TimeoutException:
256
- raise Exception("Request timeout - Agent API took too long to respond")
257
  except httpx.HTTPStatusError as e:
258
  raise Exception(f"HTTP error {e.response.status_code}: {e.response.text}")
259
  except json.JSONDecodeError as e:
260
- raise Exception(f"Invalid JSON response from Agent API: {e}")
261
 
262
  async def _get_product_details(self, arguments: Dict[str, Any]) -> List[TextContent]:
263
  """Get detailed product information"""
 
197
  return DEFAULT_API_BASE
198
 
199
  async def _make_api_request(self, endpoint: str, params: Dict[str, Any] = None, client_token: str = None) -> Dict[str, Any]:
200
+ """Make HTTP request to PrestaShop via Hub intermediary"""
201
 
 
202
  if not client_token:
203
  raise ValueError("client_token is required")
 
 
 
 
 
204
 
205
  # Extract action from params for JSON body
206
  action = params.get('action') if params else None
207
  if not action:
208
  raise ValueError("action is required in params")
209
 
210
+ # Always call the hub - it will route to the correct agent based on token
211
+ hub_url = f"{DEFAULT_API_BASE}/index.php?fc=module&module=ambmcpclient&controller=api"
 
 
 
 
212
 
213
+ # Prepare JSON body with token, action and all parameters
214
+ json_body = {
215
+ "client_token": client_token
216
+ }
217
  if params:
218
+ json_body.update(params) # Include action and all other parameters
 
 
219
 
220
  async with httpx.AsyncClient(timeout=DEFAULT_TIMEOUT) as client:
221
  try:
222
  response = await client.post(
223
+ hub_url,
 
224
  json=json_body,
225
  headers={
226
  "Content-Type": "application/json",
 
231
 
232
  if response.status_code != 200:
233
  error_text = response.text[:500] if response.text else "No error details"
234
+ raise Exception(f"Hub API error {response.status_code}: {error_text}")
235
 
236
  data = response.json()
237
 
238
+ # Handle hub API response format
239
  if data.get('error'):
240
+ raise Exception(f"Hub API error: {data.get('error')}")
241
 
242
  return data
243
 
244
  except httpx.TimeoutException:
245
+ raise Exception("Request timeout - Hub API took too long to respond")
246
  except httpx.HTTPStatusError as e:
247
  raise Exception(f"HTTP error {e.response.status_code}: {e.response.text}")
248
  except json.JSONDecodeError as e:
249
+ raise Exception(f"Invalid JSON response from Hub API: {e}")
250
 
251
  async def _get_product_details(self, arguments: Dict[str, Any]) -> List[TextContent]:
252
  """Get detailed product information"""