fast-agent-mcp 0.2.20__py3-none-any.whl → 0.2.22__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.

Potentially problematic release.


This version of fast-agent-mcp might be problematic. Click here for more details.

@@ -0,0 +1,442 @@
1
+ import json
2
+ from typing import Any, Dict, List, Optional, Tuple, Union
3
+
4
+ from mcp.types import (
5
+ CallToolRequest,
6
+ CallToolRequestParams,
7
+ CallToolResult,
8
+ EmbeddedResource,
9
+ ImageContent,
10
+ TextContent,
11
+ )
12
+ from tensorzero import AsyncTensorZeroGateway
13
+ from tensorzero.types import (
14
+ ChatInferenceResponse,
15
+ JsonInferenceResponse,
16
+ TensorZeroError,
17
+ )
18
+
19
+ from mcp_agent.agents.agent import Agent
20
+ from mcp_agent.core.exceptions import ModelConfigError
21
+ from mcp_agent.core.request_params import RequestParams
22
+ from mcp_agent.llm.augmented_llm import AugmentedLLM
23
+ from mcp_agent.llm.memory import Memory, SimpleMemory
24
+ from mcp_agent.llm.provider_types import Provider
25
+ from mcp_agent.llm.providers.multipart_converter_tensorzero import TensorZeroConverter
26
+ from mcp_agent.mcp.prompt_message_multipart import PromptMessageMultipart
27
+
28
+
29
+ class TensorZeroAugmentedLLM(AugmentedLLM[Dict[str, Any], Any]):
30
+ """
31
+ AugmentedLLM implementation for TensorZero using its native API.
32
+ Uses the Converter pattern for message formatting.
33
+ Implements multi-turn tool calling logic, storing API dicts in history.
34
+ """
35
+
36
+ def __init__(
37
+ self,
38
+ agent: Agent,
39
+ model: str,
40
+ request_params: Optional[RequestParams] = None,
41
+ **kwargs: Any,
42
+ ):
43
+ self._t0_gateway: Optional[AsyncTensorZeroGateway] = None
44
+ self._t0_function_name: str = model
45
+ self._t0_episode_id: Optional[str] = kwargs.get("episode_id")
46
+
47
+ super().__init__(
48
+ agent=agent,
49
+ model=model,
50
+ provider=Provider.TENSORZERO,
51
+ request_params=request_params,
52
+ **kwargs,
53
+ )
54
+
55
+ self.history: Memory[Dict[str, Any]] = SimpleMemory[Dict[str, Any]]()
56
+
57
+ self.logger.info(
58
+ f"TensorZero LLM provider initialized for function '{self._t0_function_name}'. History type: {type(self.history)}"
59
+ )
60
+
61
+ @staticmethod
62
+ def block_to_dict(block: Any) -> Dict[str, Any]:
63
+ if hasattr(block, "model_dump"):
64
+ try:
65
+ dumped = block.model_dump(mode="json")
66
+ if dumped:
67
+ return dumped
68
+ except Exception:
69
+ pass
70
+ if hasattr(block, "__dict__"):
71
+ try:
72
+ block_vars = vars(block)
73
+ if block_vars:
74
+ return block_vars
75
+ except Exception:
76
+ pass
77
+ if isinstance(block, (str, int, float, bool, list, dict, type(None))):
78
+ return {"type": "raw", "content": block}
79
+
80
+ # Basic attribute extraction as fallback
81
+ d = {"type": getattr(block, "type", "unknown")}
82
+ for attr in ["id", "name", "text", "arguments"]:
83
+ if hasattr(block, attr):
84
+ d[attr] = getattr(block, attr)
85
+ if len(d) == 1 and d.get("type") == "unknown":
86
+ d["content"] = str(block)
87
+ return d
88
+
89
+ def _initialize_default_params(self, kwargs: dict) -> RequestParams:
90
+ func_name = kwargs.get("model", self._t0_function_name or "unknown_t0_function")
91
+ return RequestParams(
92
+ model=func_name,
93
+ systemPrompt=self.instruction,
94
+ maxTokens=4096,
95
+ use_history=True,
96
+ max_iterations=10, # Max iterations for tool use loop
97
+ parallel_tool_calls=True,
98
+ )
99
+
100
+ async def _initialize_gateway(self) -> AsyncTensorZeroGateway:
101
+ if self._t0_gateway is None:
102
+ self.logger.debug("Initializing AsyncTensorZeroGateway client...")
103
+ try:
104
+ base_url: Optional[str] = None
105
+ default_url = "http://localhost:3000"
106
+
107
+ if (
108
+ self.context
109
+ and self.context.config
110
+ and hasattr(self.context.config, "tensorzero")
111
+ and self.context.config.tensorzero
112
+ ):
113
+ base_url = getattr(self.context.config.tensorzero, "base_url", None)
114
+
115
+ if not base_url:
116
+ if not self.context:
117
+ # Handle case where context itself is missing, log and use default
118
+ self.logger.warning(
119
+ f"LLM context not found. Cannot read TensorZero Gateway base URL configuration. "
120
+ f"Using default: {default_url}"
121
+ )
122
+ else:
123
+ self.logger.warning(
124
+ f"TensorZero Gateway base URL not configured in context.config.tensorzero.base_url. "
125
+ f"Using default: {default_url}"
126
+ )
127
+
128
+ base_url = default_url
129
+
130
+ self._t0_gateway = await AsyncTensorZeroGateway.build_http(gateway_url=base_url) # type: ignore
131
+ self.logger.info(f"TensorZero Gateway client initialized for URL: {base_url}")
132
+ except Exception as e:
133
+ self.logger.error(f"Failed to initialize TensorZero Gateway: {e}")
134
+ raise ModelConfigError(f"Failed to initialize TensorZero Gateway lazily: {e}")
135
+
136
+ return self._t0_gateway
137
+
138
+ async def _apply_prompt_provider_specific(
139
+ self,
140
+ multipart_messages: List[PromptMessageMultipart],
141
+ request_params: Optional[RequestParams] = None,
142
+ is_template: bool = False,
143
+ ) -> PromptMessageMultipart:
144
+ gateway = await self._initialize_gateway()
145
+ merged_params = self.get_request_params(request_params)
146
+
147
+ # [1] Retrieve history
148
+ current_api_messages: List[Dict[str, Any]] = []
149
+ if merged_params.use_history:
150
+ try:
151
+ current_api_messages = self.history.get() or []
152
+ self.logger.debug(
153
+ f"Retrieved {len(current_api_messages)} API dict messages from history."
154
+ )
155
+ except Exception as e:
156
+ self.logger.error(f"Error retrieving history: {e}")
157
+
158
+ # [2] Convert *new* incoming PromptMessageMultipart messages to API dicts
159
+ for msg in multipart_messages:
160
+ msg_dict = TensorZeroConverter.convert_mcp_to_t0_message(msg)
161
+ if msg_dict:
162
+ current_api_messages.append(msg_dict)
163
+
164
+ t0_system_vars = self._prepare_t0_system_params(merged_params)
165
+ if t0_system_vars:
166
+ t0_api_input_dict = {"system": t0_system_vars}
167
+ else:
168
+ t0_api_input_dict = {}
169
+ available_tools: Optional[List[Dict[str, Any]]] = await self._prepare_t0_tools()
170
+
171
+ # [3] Initialize storage arrays for the text content of the assistant message reply and, optionally, tool calls and results, and begin inference loop
172
+ final_assistant_message: List[Union[TextContent, ImageContent, EmbeddedResource]] = []
173
+ last_executed_results: Optional[List[CallToolResult]] = None
174
+
175
+ for i in range(merged_params.max_iterations):
176
+ use_parallel_calls = merged_params.parallel_tool_calls if available_tools else False
177
+ current_t0_episode_id = self._t0_episode_id
178
+
179
+ try:
180
+ self.logger.debug(
181
+ f"Calling TensorZero inference (Iteration {i + 1}/{merged_params.max_iterations})..."
182
+ )
183
+ t0_api_input_dict["messages"] = current_api_messages # type: ignore
184
+
185
+ # [4] Call the TensorZero inference API
186
+ response_iter_or_completion = await gateway.inference(
187
+ function_name=self._t0_function_name,
188
+ input=t0_api_input_dict,
189
+ additional_tools=available_tools,
190
+ parallel_tool_calls=use_parallel_calls,
191
+ stream=False,
192
+ episode_id=current_t0_episode_id,
193
+ )
194
+
195
+ if not isinstance(
196
+ response_iter_or_completion, (ChatInferenceResponse, JsonInferenceResponse)
197
+ ):
198
+ self.logger.error(
199
+ f"Unexpected TensorZero response type: {type(response_iter_or_completion)}"
200
+ )
201
+ final_assistant_message = [
202
+ TextContent(type="text", text="Unexpected response type")
203
+ ]
204
+ break # Exit loop
205
+
206
+ # [5] quick check to confirm that episode_id is present and being used correctly by TensorZero
207
+ completion = response_iter_or_completion
208
+ if completion.episode_id: #
209
+ self._t0_episode_id = str(completion.episode_id)
210
+ if (
211
+ self._t0_episode_id != current_t0_episode_id
212
+ and current_t0_episode_id is not None
213
+ ):
214
+ raise Exception(
215
+ f"Episode ID mismatch: {self._t0_episode_id} != {current_t0_episode_id}"
216
+ )
217
+
218
+ # [6] Adapt TensorZero inference response to a format compatible with the broader framework
219
+ (
220
+ content_parts_this_turn, # Text/Image content ONLY
221
+ executed_results_this_iter, # Results from THIS iteration
222
+ raw_tool_call_blocks,
223
+ ) = await self._adapt_t0_native_completion(completion, available_tools)
224
+
225
+ last_executed_results = (
226
+ executed_results_this_iter # Track results from this iteration
227
+ )
228
+
229
+ # [7] If a text message was returned from the assistant, format that message using the multipart_converter_tensorzero.py helper methods and add this to the current list of API messages
230
+ assistant_api_content = []
231
+ for part in content_parts_this_turn:
232
+ api_part = TensorZeroConverter._convert_content_part(part)
233
+ if api_part:
234
+ assistant_api_content.append(api_part)
235
+ if raw_tool_call_blocks:
236
+ assistant_api_content.extend(
237
+ [self.block_to_dict(b) for b in raw_tool_call_blocks]
238
+ )
239
+
240
+ if assistant_api_content:
241
+ assistant_api_message_dict = {
242
+ "role": "assistant",
243
+ "content": assistant_api_content,
244
+ }
245
+ current_api_messages.append(assistant_api_message_dict)
246
+ elif executed_results_this_iter:
247
+ self.logger.debug(
248
+ "Assistant turn contained only tool calls, no API message added."
249
+ )
250
+
251
+ final_assistant_message = content_parts_this_turn
252
+
253
+ # [8] If there were no tool calls we're done. If not, format the tool results and add them to the current list of API messages
254
+ if not executed_results_this_iter:
255
+ self.logger.debug(f"Iteration {i + 1}: No tool calls detected. Finishing loop.")
256
+ break
257
+ else:
258
+ user_message_with_results = (
259
+ TensorZeroConverter.convert_tool_results_to_t0_user_message(
260
+ executed_results_this_iter
261
+ )
262
+ )
263
+ if user_message_with_results:
264
+ current_api_messages.append(user_message_with_results)
265
+ else:
266
+ self.logger.error("Converter failed to format tool results, breaking loop.")
267
+ break
268
+
269
+ # Check max iterations: TODO: implement logic in the future to handle this dynamically, checking for the presence of a tool call in the last iteration
270
+ if i == merged_params.max_iterations - 1:
271
+ self.logger.warning(f"Max iterations ({merged_params.max_iterations}) reached.")
272
+ break
273
+
274
+ # --- Error Handling for Inference Call ---
275
+ except TensorZeroError as e:
276
+ error_details = getattr(e, "detail", str(e.args[0] if e.args else e))
277
+ self.logger.error(f"TensorZero Error (HTTP {e.status_code}): {error_details}")
278
+ error_content = TextContent(type="text", text=f"TensorZero Error: {error_details}")
279
+ return PromptMessageMultipart(role="assistant", content=[error_content])
280
+ except Exception as e:
281
+ import traceback
282
+
283
+ self.logger.error(f"Unexpected Error: {e}\n{traceback.format_exc()}")
284
+ error_content = TextContent(type="text", text=f"Unexpected error: {e}")
285
+ return PromptMessageMultipart(role="assistant", content=[error_content])
286
+
287
+ # [9] Construct the final assistant message and update history
288
+ final_message_to_return = PromptMessageMultipart(
289
+ role="assistant", content=final_assistant_message
290
+ )
291
+
292
+ if merged_params.use_history:
293
+ try:
294
+ # Store the final list of API DICTIONARIES in history
295
+ self.history.set(current_api_messages)
296
+ self.logger.debug(
297
+ f"Updated self.history with {len(current_api_messages)} API message dicts."
298
+ )
299
+ except Exception as e:
300
+ self.logger.error(f"Failed to update self.history after loop: {e}")
301
+
302
+ # [10] Post final assistant message to display
303
+ display_text = final_message_to_return.all_text()
304
+ if display_text and display_text != "<no text>":
305
+ title = f"ASSISTANT/{self._t0_function_name}"
306
+ await self.show_assistant_message(message_text=display_text, title=title)
307
+
308
+ elif not final_assistant_message and last_executed_results:
309
+ self.logger.debug("Final assistant turn involved only tool calls, no text to display.")
310
+
311
+ return final_message_to_return
312
+
313
+ def _prepare_t0_system_params(self, merged_params: RequestParams) -> Dict[str, Any]:
314
+ """Prepares the 'system' dictionary part of the main input."""
315
+ t0_func_input = merged_params.template_vars.copy()
316
+
317
+ metadata_args = None
318
+ if merged_params.metadata and isinstance(merged_params.metadata, dict):
319
+ metadata_args = merged_params.metadata.get("tensorzero_arguments")
320
+ if isinstance(metadata_args, dict):
321
+ t0_func_input.update(metadata_args)
322
+ self.logger.debug(f"Merged tensorzero_arguments from metadata: {metadata_args}")
323
+ return t0_func_input
324
+
325
+ async def _prepare_t0_tools(self) -> Optional[List[Dict[str, Any]]]:
326
+ """Fetches and formats tools for the additional_tools parameter."""
327
+ formatted_tools: List[Dict[str, Any]] = []
328
+ try:
329
+ tools_response = await self.aggregator.list_tools()
330
+ if tools_response and hasattr(tools_response, "tools") and tools_response.tools:
331
+ for mcp_tool in tools_response.tools:
332
+ if (
333
+ not isinstance(mcp_tool.inputSchema, dict)
334
+ or mcp_tool.inputSchema.get("type") != "object"
335
+ ):
336
+ self.logger.warning(
337
+ f"Tool '{mcp_tool.name}' has invalid parameters schema. Skipping."
338
+ )
339
+ continue
340
+ t0_tool_dict = {
341
+ "name": mcp_tool.name,
342
+ "description": mcp_tool.description if mcp_tool.description else "",
343
+ "parameters": mcp_tool.inputSchema,
344
+ }
345
+ formatted_tools.append(t0_tool_dict)
346
+ return formatted_tools if formatted_tools else None
347
+ except Exception as e:
348
+ self.logger.error(f"Failed to fetch or format tools: {e}")
349
+ return None
350
+
351
+ async def _adapt_t0_native_completion(
352
+ self,
353
+ completion: Union[ChatInferenceResponse, JsonInferenceResponse],
354
+ available_tools_for_display: Optional[List[Dict[str, Any]]] = None,
355
+ ) -> Tuple[
356
+ List[Union[TextContent, ImageContent, EmbeddedResource]], # Text/Image content ONLY
357
+ List[CallToolResult], # Executed results
358
+ List[Any], # Raw tool_call blocks
359
+ ]:
360
+ content_parts_this_turn: List[Union[TextContent, ImageContent, EmbeddedResource]] = []
361
+ executed_tool_results: List[CallToolResult] = []
362
+ raw_tool_call_blocks_from_t0: List[Any] = []
363
+
364
+ if isinstance(completion, ChatInferenceResponse) and hasattr(completion, "content"):
365
+ for block in completion.content:
366
+ block_type = getattr(block, "type", "UNKNOWN")
367
+
368
+ if block_type == "text":
369
+ text_val = getattr(block, "text", None)
370
+ if text_val is not None:
371
+ content_parts_this_turn.append(TextContent(type="text", text=text_val))
372
+
373
+ elif block_type == "tool_call":
374
+ raw_tool_call_blocks_from_t0.append(block)
375
+ tool_use_id = getattr(block, "id", None)
376
+ tool_name = getattr(block, "name", None)
377
+ tool_input_raw = getattr(block, "arguments", None)
378
+ tool_input = {}
379
+ if isinstance(tool_input_raw, dict):
380
+ tool_input = tool_input_raw
381
+ elif isinstance(tool_input_raw, str):
382
+ try:
383
+ tool_input = json.loads(tool_input_raw)
384
+ except json.JSONDecodeError:
385
+ tool_input = {}
386
+ elif tool_input_raw is not None:
387
+ tool_input = {}
388
+
389
+ if tool_use_id and tool_name:
390
+ self.show_tool_call(
391
+ available_tools_for_display, tool_name, json.dumps(tool_input)
392
+ )
393
+ mcp_tool_request = CallToolRequest(
394
+ method="tools/call",
395
+ params=CallToolRequestParams(name=tool_name, arguments=tool_input),
396
+ )
397
+ try:
398
+ result: CallToolResult = await self.call_tool(
399
+ mcp_tool_request, tool_use_id
400
+ )
401
+ setattr(result, "_t0_tool_use_id_temp", tool_use_id)
402
+ setattr(result, "_t0_tool_name_temp", tool_name)
403
+ setattr(result, "_t0_is_error_temp", False)
404
+ executed_tool_results.append(result)
405
+ self.show_oai_tool_result(str(result))
406
+ except Exception as e:
407
+ self.logger.error(
408
+ f"Error executing tool {tool_name} (id: {tool_use_id}): {e}"
409
+ )
410
+ error_text = f"Error executing tool {tool_name}: {str(e)}"
411
+ error_result = CallToolResult(
412
+ isError=True, content=[TextContent(type="text", text=error_text)]
413
+ )
414
+ setattr(error_result, "_t0_tool_use_id_temp", tool_use_id)
415
+ setattr(error_result, "_t0_tool_name_temp", tool_name)
416
+ setattr(error_result, "_t0_is_error_temp", True)
417
+ executed_tool_results.append(error_result)
418
+ self.show_oai_tool_result(f"ERROR: {error_text}")
419
+
420
+ elif block_type == "thought":
421
+ thought_text = getattr(block, "text", None)
422
+ self.logger.debug(f"TensorZero thought: {thought_text}")
423
+ else:
424
+ self.logger.warning(
425
+ f"TensorZero Adapt: Skipping unknown block type: {block_type}"
426
+ )
427
+
428
+ elif isinstance(completion, JsonInferenceResponse):
429
+ # `completion.output.raw` should always be present unless the LLM provider returns unexpected data
430
+ if completion.output.raw:
431
+ content_parts_this_turn.append(TextContent(type="text", text=completion.output.raw))
432
+
433
+ return content_parts_this_turn, executed_tool_results, raw_tool_call_blocks_from_t0
434
+
435
+ async def shutdown(self):
436
+ """Close the TensorZero gateway client if initialized."""
437
+ if self._t0_gateway:
438
+ try:
439
+ await self._t0_gateway.close()
440
+ self.logger.debug("TensorZero Gateway client closed.")
441
+ except Exception as e:
442
+ self.logger.error(f"Error closing TensorZero Gateway client: {e}")
@@ -0,0 +1,200 @@
1
+ import json
2
+ from typing import Any, Dict, List, Optional, Union
3
+
4
+ from mcp.types import (
5
+ CallToolResult,
6
+ EmbeddedResource,
7
+ ImageContent,
8
+ TextContent,
9
+ )
10
+
11
+ from mcp_agent.logging.logger import get_logger
12
+ from mcp_agent.mcp.helpers.content_helpers import (
13
+ get_text,
14
+ )
15
+ from mcp_agent.mcp.prompt_message_multipart import PromptMessageMultipart
16
+
17
+ _logger = get_logger(__name__)
18
+
19
+
20
+ class TensorZeroConverter:
21
+ """Converts MCP message types to/from TensorZero API format."""
22
+
23
+ @staticmethod
24
+ def _convert_content_part(
25
+ part: Union[TextContent, ImageContent, EmbeddedResource],
26
+ ) -> Optional[Dict[str, Any]]:
27
+ """Converts a single MCP content part to a T0 content block dictionary."""
28
+ if isinstance(part, TextContent):
29
+ text = get_text(part)
30
+ if text is not None:
31
+ return {"type": "text", "text": text}
32
+ elif isinstance(part, ImageContent):
33
+ # Handle Base64: needs data, mimeType (and mimeType must not be empty)
34
+ if hasattr(part, "data") and part.data and hasattr(part, "mimeType") and part.mimeType:
35
+ _logger.debug(
36
+ f"Converting ImageContent as base64 for T0 native: mime={part.mimeType}, data_len={len(part.data) if isinstance(part.data, str) else 'N/A'}"
37
+ )
38
+ supported_mime_types = ["image/jpeg", "image/png", "image/webp"]
39
+ mime_type = getattr(part, "mimeType", "")
40
+
41
+ # Use the provided mime_type if supported, otherwise default to png
42
+ if mime_type not in supported_mime_types:
43
+ _logger.warning(
44
+ f"Unsupported mimeType '{mime_type}' for T0 base64 image, defaulting to image/png."
45
+ )
46
+ mime_type = "image/png"
47
+
48
+ return {
49
+ "type": "image",
50
+ "mime_type": mime_type, # Note: T0 uses mime_type, not media_type
51
+ "data": getattr(part, "data", ""), # Data is direct property
52
+ }
53
+ else:
54
+ # Log cases where it's an ImageContent but doesn't fit Base64 criteria
55
+ _logger.warning(
56
+ f"Skipping ImageContent: Missing required base64 fields (mimeType/data), or mimeType is empty: {part}"
57
+ )
58
+
59
+ elif isinstance(part, EmbeddedResource):
60
+ _logger.warning(f"Skipping EmbeddedResource, T0 conversion not implemented: {part}")
61
+ else:
62
+ _logger.error(
63
+ f"Unsupported content part type for T0 conversion: {type(part)}"
64
+ ) # Changed to error
65
+
66
+ return None # Return None if no block was successfully created
67
+
68
+ @staticmethod
69
+ def _get_text_from_call_tool_result(result: CallToolResult) -> str:
70
+ """Helper to extract combined text from a CallToolResult's content list."""
71
+ texts = []
72
+ if result.content:
73
+ for part in result.content:
74
+ text = get_text(part)
75
+ if text:
76
+ texts.append(text)
77
+ return "\n".join(texts)
78
+
79
+ @staticmethod
80
+ def convert_tool_results_to_t0_user_message(
81
+ results: List[CallToolResult],
82
+ ) -> Optional[Dict[str, Any]]:
83
+ """Formats CallToolResult list into T0's tool_result blocks within a user message dict."""
84
+ t0_tool_result_blocks = []
85
+ for result in results:
86
+ tool_use_id = getattr(result, "_t0_tool_use_id_temp", None)
87
+ tool_name = getattr(result, "_t0_tool_name_temp", None)
88
+
89
+ if tool_use_id and tool_name:
90
+ result_content_str = TensorZeroConverter._get_text_from_call_tool_result(result)
91
+ try:
92
+ # Attempt to treat result as JSON if possible, else use raw string
93
+ try:
94
+ json_result = json.loads(result_content_str)
95
+ except json.JSONDecodeError:
96
+ json_result = result_content_str # Fallback to string if not valid JSON
97
+ except Exception as e:
98
+ _logger.error(f"Unexpected error processing tool result content: {e}")
99
+ json_result = str(result_content_str) # Safest fallback
100
+
101
+ t0_block = {
102
+ "type": "tool_result",
103
+ "id": tool_use_id,
104
+ "name": tool_name,
105
+ "result": json_result, # T0 expects the result directly
106
+ }
107
+ t0_tool_result_blocks.append(t0_block)
108
+
109
+ # Clean up temporary attributes
110
+ try:
111
+ delattr(result, "_t0_tool_use_id_temp")
112
+ delattr(result, "_t0_tool_name_temp")
113
+ if hasattr(result, "_t0_is_error_temp"):
114
+ delattr(result, "_t0_is_error_temp")
115
+ except AttributeError:
116
+ pass
117
+ else:
118
+ _logger.warning(
119
+ f"Could not find id/name temp attributes for CallToolResult: {result}"
120
+ )
121
+
122
+ if not t0_tool_result_blocks:
123
+ return None
124
+
125
+ return {"role": "user", "content": t0_tool_result_blocks}
126
+
127
+ @staticmethod
128
+ def convert_mcp_to_t0_message(msg: PromptMessageMultipart) -> Optional[Dict[str, Any]]:
129
+ """
130
+ Converts a single PromptMessageMultipart to a T0 API message dictionary.
131
+ Handles Text, Image, and embedded CallToolResult content.
132
+ Skips system messages.
133
+ """
134
+ if msg.role == "system":
135
+ return None
136
+
137
+ t0_content_blocks = []
138
+ contains_tool_result = False
139
+
140
+ for part in msg.content:
141
+ # Use the corrected _convert_content_part
142
+ converted_block = TensorZeroConverter._convert_content_part(part)
143
+ if converted_block:
144
+ t0_content_blocks.append(converted_block)
145
+ elif isinstance(part, CallToolResult):
146
+ # Existing logic for handling embedded CallToolResult (seems compatible with T0 tool_result spec)
147
+ contains_tool_result = True
148
+ tool_use_id = getattr(part, "_t0_tool_use_id_temp", None)
149
+ tool_name = getattr(part, "_t0_tool_name_temp", None)
150
+ if tool_use_id and tool_name:
151
+ result_content_str = TensorZeroConverter._get_text_from_call_tool_result(part)
152
+ # Try to format result as JSON object/string
153
+ try:
154
+ json_result = json.loads(result_content_str)
155
+ except json.JSONDecodeError:
156
+ json_result = result_content_str # Fallback
157
+ except Exception as e:
158
+ _logger.error(f"Error processing embedded tool result: {e}")
159
+ json_result = str(result_content_str)
160
+
161
+ t0_content_blocks.append(
162
+ {
163
+ "type": "tool_result",
164
+ "id": tool_use_id,
165
+ "name": tool_name,
166
+ "result": json_result,
167
+ }
168
+ )
169
+ # Clean up temp attributes
170
+ try:
171
+ delattr(part, "_t0_tool_use_id_temp")
172
+ delattr(part, "_t0_tool_name_temp")
173
+ except AttributeError:
174
+ pass
175
+ else:
176
+ _logger.warning(
177
+ f"Found embedded CallToolResult without required temp attributes: {part}"
178
+ )
179
+ # Note: The _convert_content_part handles logging for other skipped/unsupported types
180
+
181
+ if not t0_content_blocks:
182
+ return None
183
+
184
+ # Determine role - logic remains the same
185
+ valid_role = msg.role if msg.role in ["user", "assistant"] else "user"
186
+ if contains_tool_result and all(
187
+ block.get("type") == "tool_result" for block in t0_content_blocks
188
+ ):
189
+ final_role = "user"
190
+ if valid_role != final_role:
191
+ _logger.debug(f"Overriding role to '{final_role}' for tool result message.")
192
+ else:
193
+ final_role = valid_role
194
+ if valid_role != msg.role:
195
+ _logger.warning(f"Mapping message role '{msg.role}' to '{valid_role}' for T0.")
196
+
197
+ return {"role": final_role, "content": t0_content_blocks}
198
+
199
+ # Add methods here if needed to convert *from* T0 format back to MCP types
200
+ # e.g., adapt_t0_response_to_mcp(...) - this logic stays in the LLM class for now
@@ -3,6 +3,7 @@ A derived client session for the MCP Agent framework.
3
3
  It adds logging and supports sampling requests.
4
4
  """
5
5
 
6
+ from datetime import timedelta
6
7
  from typing import TYPE_CHECKING, Optional
7
8
 
8
9
  from mcp import ClientSession
@@ -73,10 +74,13 @@ class MCPAgentClientSession(ClientSession, ContextDependent):
73
74
  self,
74
75
  request: SendRequestT,
75
76
  result_type: type[ReceiveResultT],
77
+ request_read_timeout_seconds: timedelta | None = None,
76
78
  ) -> ReceiveResultT:
77
79
  logger.debug("send_request: request=", data=request.model_dump())
78
80
  try:
79
- result = await super().send_request(request, result_type)
81
+ result = await super().send_request(
82
+ request, result_type, request_read_timeout_seconds=request_read_timeout_seconds
83
+ )
80
84
  logger.debug("send_request: response=", data=result.model_dump())
81
85
  return result
82
86
  except Exception as e: