mcp-mesh 0.8.1__py3-none-any.whl → 0.9.0b2__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.
mesh/helpers.py CHANGED
@@ -6,7 +6,7 @@ mesh decorators to simplify common patterns like zero-code LLM providers.
6
6
  """
7
7
 
8
8
  import logging
9
- from typing import Any, Dict, List, Optional
9
+ from typing import Any, Optional
10
10
 
11
11
  from _mcp_mesh.shared.logging_config import format_log_value
12
12
 
@@ -214,62 +214,55 @@ def llm_provider(
214
214
  f"(requested by consumer)"
215
215
  )
216
216
 
217
+ # Get vendor handler once - used for both structured output and system prompt formatting
218
+ from _mcp_mesh.engine.provider_handlers import ProviderHandlerRegistry
219
+
220
+ handler = ProviderHandlerRegistry.get_handler(vendor)
221
+
217
222
  # Issue #459: Handle output_schema for vendor-specific structured output
218
- # Convert to response_format for vendors that support it
223
+ # Use provider handler pattern for vendor-specific behavior
219
224
  output_schema = model_params_copy.pop("output_schema", None)
220
225
  output_type_name = model_params_copy.pop("output_type_name", None)
221
226
 
222
- # Vendors that support structured output via response_format
223
- supported_structured_output_vendors = (
224
- "openai",
225
- "azure", # Azure OpenAI uses same format as OpenAI
226
- "gemini",
227
- "vertex_ai", # Vertex AI Gemini uses same format as Gemini
228
- "anthropic",
229
- )
230
-
231
227
  if output_schema:
232
- if vendor in supported_structured_output_vendors:
233
- # Apply vendor-specific response_format for structured output
234
- from _mcp_mesh.engine.provider_handlers import make_schema_strict
235
-
236
- if vendor == "anthropic":
237
- # Claude: doesn't require all properties in 'required', uses strict=False
238
- schema = make_schema_strict(
239
- output_schema, add_all_required=False
228
+ # Include messages so handler can modify system prompt (e.g., HINT mode injection)
229
+ model_params_copy["messages"] = request.messages
230
+ handler.apply_structured_output(
231
+ output_schema, output_type_name, model_params_copy
232
+ )
233
+ # Remove messages to avoid duplication in completion_args
234
+ model_params_copy.pop("messages", None)
235
+ logger.debug(
236
+ f"🎯 Applied {vendor} structured output via handler: "
237
+ f"{output_type_name}"
238
+ )
239
+
240
+ # Use vendor handler to format system prompt when tools are present
241
+ messages = request.messages
242
+ if request.tools:
243
+
244
+ # Find and format system message
245
+ formatted_messages = []
246
+ for msg in messages:
247
+ if msg.get("role") == "system":
248
+ # Format system prompt with vendor-specific instructions
249
+ base_prompt = msg.get("content", "")
250
+ formatted_content = handler.format_system_prompt(
251
+ base_prompt=base_prompt,
252
+ tool_schemas=request.tools,
253
+ output_type=str, # Provider returns raw string
240
254
  )
241
- strict_mode = False
242
- else:
243
- # OpenAI/Azure/Gemini/Vertex: require all properties in 'required', uses strict=True
244
- schema = make_schema_strict(
245
- output_schema, add_all_required=True
255
+ formatted_messages.append(
256
+ {"role": "system", "content": formatted_content}
246
257
  )
247
- strict_mode = True
248
-
249
- model_params_copy["response_format"] = {
250
- "type": "json_schema",
251
- "json_schema": {
252
- "name": output_type_name or "Response",
253
- "schema": schema,
254
- "strict": strict_mode,
255
- },
256
- }
257
- logger.debug(
258
- f"🎯 Applied {vendor} response_format for structured output: "
259
- f"{output_type_name} (strict={strict_mode})"
260
- )
261
- else:
262
- # Vendor doesn't support structured output - warn user
263
- logger.warning(
264
- f"⚠️ Structured output schema '{output_type_name or 'Response'}' "
265
- f"was provided but vendor '{vendor}' does not support response_format. "
266
- f"The schema will be ignored and the LLM may return unstructured output."
267
- )
258
+ else:
259
+ formatted_messages.append(msg)
260
+ messages = formatted_messages
268
261
 
269
262
  # Build litellm.completion arguments
270
263
  completion_args: dict[str, Any] = {
271
264
  "model": effective_model,
272
- "messages": request.messages,
265
+ "messages": messages,
273
266
  **litellm_kwargs,
274
267
  }
275
268