mbxai 0.5.19__py3-none-any.whl → 0.5.21__py3-none-any.whl

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.
mbxai/__init__.py CHANGED
@@ -2,4 +2,4 @@
2
2
  MBX AI package.
3
3
  """
4
4
 
5
- __version__ = "0.5.19"
5
+ __version__ = "0.5.21"
mbxai/mcp/client.py CHANGED
@@ -90,7 +90,8 @@ class MCPClient(ToolClient):
90
90
  # Make the HTTP request to the tool's URL
91
91
  response = await self._http_client.post(
92
92
  url,
93
- json={"input": kwargs} if tool.strict else kwargs
93
+ json={"input": kwargs} if tool.strict else kwargs,
94
+ timeout=300.0 # 5 minutes timeout
94
95
  )
95
96
  return response.json()
96
97
 
mbxai/mcp/server.py CHANGED
@@ -31,7 +31,7 @@ class MCPServer:
31
31
  self.app = FastAPI(
32
32
  title=self.name,
33
33
  description=self.description,
34
- version="0.5.19",
34
+ version="0.5.21",
35
35
  )
36
36
 
37
37
  # Initialize MCP server
mbxai/tools/client.py CHANGED
@@ -9,6 +9,7 @@ import json
9
9
  from pydantic import BaseModel
10
10
  from ..openrouter import OpenRouterClient
11
11
  from .types import Tool, ToolCall
12
+ import asyncio
12
13
 
13
14
  logger = logging.getLogger(__name__)
14
15
 
@@ -139,6 +140,62 @@ class ToolClient:
139
140
  # Validate message sequence
140
141
  self._validate_message_sequence(messages, validate_responses)
141
142
 
143
+ async def _process_tool_calls(self, message: Any, messages: list[dict[str, Any]]) -> None:
144
+ """Process all tool calls in a message.
145
+
146
+ Args:
147
+ message: The message containing tool calls
148
+ messages: The list of messages to add responses to
149
+ """
150
+ if not message.tool_calls:
151
+ return
152
+
153
+ # Process all tool calls first
154
+ tool_responses = []
155
+ for tool_call in message.tool_calls:
156
+ tool = self._tools.get(tool_call.function.name)
157
+ if not tool:
158
+ raise ValueError(f"Unknown tool: {tool_call.function.name}")
159
+
160
+ # Parse arguments if they're a string
161
+ arguments = tool_call.function.arguments
162
+ if isinstance(arguments, str):
163
+ try:
164
+ arguments = json.loads(arguments)
165
+ except json.JSONDecodeError as e:
166
+ logger.error(f"Failed to parse tool arguments: {e}")
167
+ raise ValueError(f"Invalid tool arguments format: {arguments}")
168
+
169
+ # Call the tool
170
+ logger.info(f"Calling tool: {tool.name} with args: {self._truncate_dict(arguments)}")
171
+ if inspect.iscoroutinefunction(tool.function):
172
+ result = await tool.function(**arguments)
173
+ else:
174
+ result = tool.function(**arguments)
175
+
176
+ # Convert result to JSON string if it's not already
177
+ if not isinstance(result, str):
178
+ result = json.dumps(result)
179
+
180
+ # Create the tool response
181
+ tool_response = {
182
+ "role": "tool",
183
+ "tool_call_id": tool_call.id,
184
+ "content": result,
185
+ }
186
+ tool_responses.append(tool_response)
187
+ logger.info(f"Created tool response for call ID {tool_call.id}")
188
+
189
+ # Add all tool responses to the messages
190
+ messages.extend(tool_responses)
191
+ logger.info(f"Message count: {len(messages)}, Added {len(tool_responses)} tool responses to messages")
192
+
193
+ # Validate the message sequence
194
+ self._validate_message_sequence(messages, validate_responses=True)
195
+
196
+ # Log the messages we're about to send
197
+ self._log_messages(messages, validate_responses=False)
198
+
142
199
  async def chat(
143
200
  self,
144
201
  messages: list[dict[str, Any]],
@@ -186,13 +243,13 @@ class ToolClient:
186
243
  for tool_call in message.tool_calls
187
244
  ]
188
245
  messages.append(assistant_message)
189
- logger.info(f"Added assistant message with tool calls: {[tc.function.name for tc in message.tool_calls] if message.tool_calls else None}")
246
+ logger.info(f"Message count: {len(messages)}, Added assistant message with tool calls: {[tc.function.name for tc in message.tool_calls] if message.tool_calls else None}")
190
247
 
191
248
  # If there are no tool calls, we're done
192
249
  if not message.tool_calls:
193
250
  return response
194
251
 
195
- # Process all tool calls first
252
+ # Process all tool calls
196
253
  tool_responses = []
197
254
  for tool_call in message.tool_calls:
198
255
  tool = self._tools.get(tool_call.function.name)
@@ -210,10 +267,18 @@ class ToolClient:
210
267
 
211
268
  # Call the tool
212
269
  logger.info(f"Calling tool: {tool.name} with args: {self._truncate_dict(arguments)}")
213
- if inspect.iscoroutinefunction(tool.function):
214
- result = await tool.function(**arguments)
215
- else:
216
- result = tool.function(**arguments)
270
+ try:
271
+ if inspect.iscoroutinefunction(tool.function):
272
+ result = await asyncio.wait_for(tool.function(**arguments), timeout=300.0) # 5 minutes timeout
273
+ else:
274
+ result = tool.function(**arguments)
275
+ logger.info(f"Tool {tool.name} completed successfully")
276
+ except asyncio.TimeoutError:
277
+ logger.error(f"Tool {tool.name} timed out after 5 minutes")
278
+ result = {"error": "Tool execution timed out after 5 minutes"}
279
+ except Exception as e:
280
+ logger.error(f"Error calling tool {tool.name}: {str(e)}")
281
+ result = {"error": f"Tool execution failed: {str(e)}"}
217
282
 
218
283
  # Convert result to JSON string if it's not already
219
284
  if not isinstance(result, str):
@@ -230,7 +295,7 @@ class ToolClient:
230
295
 
231
296
  # Add all tool responses to the messages
232
297
  messages.extend(tool_responses)
233
- logger.info(f"Added {len(tool_responses)} tool responses to messages")
298
+ logger.info(f"Message count: {len(messages)}, Added {len(tool_responses)} tool responses to messages")
234
299
 
235
300
  # Validate the message sequence
236
301
  self._validate_message_sequence(messages, validate_responses=True)
@@ -238,41 +303,8 @@ class ToolClient:
238
303
  # Log the messages we're about to send
239
304
  self._log_messages(messages, validate_responses=False)
240
305
 
241
- # Get a new response from the model with all tool results
242
- response = self._client.chat_completion(
243
- messages=messages,
244
- model=model,
245
- stream=stream,
246
- **kwargs,
247
- )
248
-
249
- if stream:
250
- return response
251
-
252
- message = response.choices[0].message
253
- # Add the assistant's message with tool calls
254
- assistant_message = {
255
- "role": "assistant",
256
- "content": message.content or None, # Ensure content is None if empty
257
- }
258
- if message.tool_calls:
259
- assistant_message["tool_calls"] = [
260
- {
261
- "id": tool_call.id,
262
- "type": "function",
263
- "function": {
264
- "name": tool_call.function.name,
265
- "arguments": tool_call.function.arguments,
266
- },
267
- }
268
- for tool_call in message.tool_calls
269
- ]
270
- messages.append(assistant_message)
271
- logger.info(f"Added assistant message with tool calls: {[tc.function.name for tc in message.tool_calls] if message.tool_calls else None}")
272
-
273
- # If there are no more tool calls, we're done
274
- if not message.tool_calls:
275
- return response
306
+ # Continue the loop to get the next response
307
+ continue
276
308
 
277
309
  async def parse(
278
310
  self,
@@ -337,13 +369,14 @@ class ToolClient:
337
369
  for tool_call in message.tool_calls
338
370
  ]
339
371
  messages.append(assistant_message)
340
- logger.info(f"Assistant message: content='{self._truncate_content(message.content)}', tool_calls={[tc.function.name for tc in message.tool_calls] if message.tool_calls else None}")
372
+ logger.info(f"Message count: {len(messages)}, Added assistant message with tool calls: {[tc.function.name for tc in message.tool_calls] if message.tool_calls else None}")
341
373
 
342
374
  # If there are no tool calls, we're done
343
375
  if not message.tool_calls:
344
376
  return response
345
377
 
346
- # Handle all tool calls before getting the next model response
378
+ # Process all tool calls
379
+ tool_responses = []
347
380
  for tool_call in message.tool_calls:
348
381
  tool = self._tools.get(tool_call.function.name)
349
382
  if not tool:
@@ -369,48 +402,24 @@ class ToolClient:
369
402
  if not isinstance(result, str):
370
403
  result = json.dumps(result)
371
404
 
372
- # Create and add the tool response immediately
405
+ # Create the tool response
373
406
  tool_response = {
374
407
  "role": "tool",
375
408
  "tool_call_id": tool_call.id,
376
409
  "content": result,
377
410
  }
378
- messages.append(tool_response)
379
- logger.info(f"Tool response for call ID {tool_call.id}: {self._truncate_content(result)}")
411
+ tool_responses.append(tool_response)
412
+ logger.info(f"Created tool response for call ID {tool_call.id}")
380
413
 
381
- # Get a new response from the model with all tool results
382
- response = self._client.chat_completion_parse(
383
- messages=messages,
384
- response_format=response_format,
385
- model=model,
386
- stream=stream,
387
- **kwargs,
388
- )
414
+ # Add all tool responses to the messages
415
+ messages.extend(tool_responses)
416
+ logger.info(f"Message count: {len(messages)}, Added {len(tool_responses)} tool responses to messages")
389
417
 
390
- if stream:
391
- return response
418
+ # Validate the message sequence
419
+ self._validate_message_sequence(messages, validate_responses=True)
392
420
 
393
- message = response.choices[0].message
394
- # Add the assistant's message with tool calls
395
- assistant_message = {
396
- "role": "assistant",
397
- "content": message.content or None, # Ensure content is None if empty
398
- }
399
- if message.tool_calls:
400
- assistant_message["tool_calls"] = [
401
- {
402
- "id": tool_call.id,
403
- "type": "function",
404
- "function": {
405
- "name": tool_call.function.name,
406
- "arguments": tool_call.function.arguments,
407
- },
408
- }
409
- for tool_call in message.tool_calls
410
- ]
411
- messages.append(assistant_message)
412
- logger.info(f"Assistant message: content='{self._truncate_content(message.content)}', tool_calls={[tc.function.name for tc in message.tool_calls] if message.tool_calls else None}")
421
+ # Log the messages we're about to send
422
+ self._log_messages(messages, validate_responses=False)
413
423
 
414
- # If there are no more tool calls, we're done
415
- if not message.tool_calls:
416
- return response
424
+ # Continue the loop to get the next response
425
+ continue
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mbxai
3
- Version: 0.5.19
3
+ Version: 0.5.21
4
4
  Summary: MBX AI SDK
5
5
  Project-URL: Homepage, https://www.mibexx.de
6
6
  Project-URL: Documentation, https://www.mibexx.de
@@ -1,18 +1,18 @@
1
- mbxai/__init__.py,sha256=fBHYV_6ZmWukpBBQkb6diHAP4h69-vDoHNwmywdi4G4,48
1
+ mbxai/__init__.py,sha256=_iYdYXR15WHhK5UJKTKwj9MTpmhkiwKKaIJAdJAPsM4,48
2
2
  mbxai/core.py,sha256=WMvmU9TTa7M_m-qWsUew4xH8Ul6xseCZ2iBCXJTW-Bs,196
3
3
  mbxai/mcp/__init__.py,sha256=_ek9iYdYqW5saKetj4qDci11jxesQDiHPJRpHMKkxgU,175
4
- mbxai/mcp/client.py,sha256=hEAVWIrIq758C1zm9aWGf-FiITB3LxtuxZEZ0CcjJ4s,5343
4
+ mbxai/mcp/client.py,sha256=sUdup2ts_Anr5o27y-BZW2hE_xw-2T7Sstds7cZexCw,5395
5
5
  mbxai/mcp/example.py,sha256=oaol7AvvZnX86JWNz64KvPjab5gg1VjVN3G8eFSzuaE,2350
6
- mbxai/mcp/server.py,sha256=rjDCSR3IZOm5Xb-A2eMm-aMuJANSuRmq8uHmy85hjT8,3463
6
+ mbxai/mcp/server.py,sha256=hGlo5m_HdX0pTqxp-DmecCSo_830zVUFZcjvGhe9pVM,3463
7
7
  mbxai/openrouter/__init__.py,sha256=Ito9Qp_B6q-RLGAQcYyTJVWwR2YAZvNqE-HIYXxhtD8,298
8
8
  mbxai/openrouter/client.py,sha256=XLRMRNRJH96Jl6_af0KkzRDdLJnixh8I3RvEEcFuXyg,10840
9
9
  mbxai/openrouter/config.py,sha256=MTX_YHsFrM7JYqovJSkEF6JzVyIdajeI5Dja2CALH58,2874
10
10
  mbxai/openrouter/models.py,sha256=b3IjjtZAjeGOf2rLsdnCD1HacjTnS8jmv_ZXorc-KJQ,2604
11
11
  mbxai/tools/__init__.py,sha256=QUFaXhDm-UKcuAtT1rbKzhBkvyRBVokcQIOf9cxIuwc,160
12
- mbxai/tools/client.py,sha256=6FWaM80OFeHR2tI6vhsUcqln8dObfglRiM36vPAyUB8,16733
12
+ mbxai/tools/client.py,sha256=t7rdITqgCbDXQPFOZhGj6VDDPAwqdilJMKPfCOcJaFo,17279
13
13
  mbxai/tools/example.py,sha256=1HgKK39zzUuwFbnp3f0ThyWVfA_8P28PZcTwaUw5K78,2232
14
14
  mbxai/tools/types.py,sha256=fo5t9UbsHGynhA88vD_ecgDqL8iLvt2E1h1ym43Rrgk,745
15
- mbxai-0.5.19.dist-info/METADATA,sha256=CZyWoVf5vd8iu0_kgCDNYWypoD_MyxggF3xYe9Xy6BE,4108
16
- mbxai-0.5.19.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
17
- mbxai-0.5.19.dist-info/licenses/LICENSE,sha256=hEyhc4FxwYo3NQ40yNgZ7STqwVk-1_XcTXOnAPbGJAw,1069
18
- mbxai-0.5.19.dist-info/RECORD,,
15
+ mbxai-0.5.21.dist-info/METADATA,sha256=vXEAsDBTc5drMfXx2mo9PXvG-BikBYz293NAclpyl54,4108
16
+ mbxai-0.5.21.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
17
+ mbxai-0.5.21.dist-info/licenses/LICENSE,sha256=hEyhc4FxwYo3NQ40yNgZ7STqwVk-1_XcTXOnAPbGJAw,1069
18
+ mbxai-0.5.21.dist-info/RECORD,,
File without changes