Update mcp_client.py
Browse files- mcp_client.py +65 -0
mcp_client.py
CHANGED
|
@@ -69,6 +69,10 @@ class UniversalMCPClient:
|
|
| 69 |
"""Set generation parameters for chat completions (OpenAI-compatible)."""
|
| 70 |
# Clean None values to avoid sending invalid fields
|
| 71 |
cleaned = {k: v for k, v in params.items() if v is not None}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
self.generation_params = cleaned
|
| 73 |
logger.info(f"🔧 Updated generation params: {list(self.generation_params.keys())}")
|
| 74 |
|
|
@@ -257,6 +261,11 @@ class UniversalMCPClient:
|
|
| 257 |
# Add any remaining kwargs (highest precedence)
|
| 258 |
params.update(kwargs)
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
# Add reasoning effort only for GPT-OSS models
|
| 261 |
if AppConfig.is_gpt_oss_model(self.current_model):
|
| 262 |
reasoning_effort = kwargs.pop("reasoning_effort", self.generation_params.get("reasoning_effort", AppConfig.DEFAULT_REASONING_EFFORT))
|
|
@@ -287,6 +296,62 @@ class UniversalMCPClient:
|
|
| 287 |
except Exception as e:
|
| 288 |
logger.error(f"HF Inference API call failed: {e}")
|
| 289 |
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
def generate_chat_completion_with_mcp_tools(self, messages: List[Dict[str, Any]], **kwargs) -> Dict[str, Any]:
|
| 292 |
"""Generate chat completion with MCP tool support"""
|
|
|
|
| 69 |
"""Set generation parameters for chat completions (OpenAI-compatible)."""
|
| 70 |
# Clean None values to avoid sending invalid fields
|
| 71 |
cleaned = {k: v for k, v in params.items() if v is not None}
|
| 72 |
+
# Defensive: If no tools provided, drop tool-specific params to avoid 400s
|
| 73 |
+
if "tools" not in cleaned:
|
| 74 |
+
cleaned.pop("tool_choice", None)
|
| 75 |
+
cleaned.pop("tool_prompt", None)
|
| 76 |
self.generation_params = cleaned
|
| 77 |
logger.info(f"🔧 Updated generation params: {list(self.generation_params.keys())}")
|
| 78 |
|
|
|
|
| 261 |
# Add any remaining kwargs (highest precedence)
|
| 262 |
params.update(kwargs)
|
| 263 |
|
| 264 |
+
# Defensive: If tools are absent, ensure tool-specific params are not sent
|
| 265 |
+
if "tools" not in params:
|
| 266 |
+
params.pop("tool_choice", None)
|
| 267 |
+
params.pop("tool_prompt", None)
|
| 268 |
+
|
| 269 |
# Add reasoning effort only for GPT-OSS models
|
| 270 |
if AppConfig.is_gpt_oss_model(self.current_model):
|
| 271 |
reasoning_effort = kwargs.pop("reasoning_effort", self.generation_params.get("reasoning_effort", AppConfig.DEFAULT_REASONING_EFFORT))
|
|
|
|
| 296 |
except Exception as e:
|
| 297 |
logger.error(f"HF Inference API call failed: {e}")
|
| 298 |
raise
|
| 299 |
+
|
| 300 |
+
def generate_chat_completion_stream(self, messages: List[Dict[str, Any]], **kwargs):
|
| 301 |
+
"""Stream chat completion tokens using HuggingFace Inference Providers (OpenAI-compatible)."""
|
| 302 |
+
if not self.hf_client:
|
| 303 |
+
raise ValueError("HuggingFace client not initialized. Please set HF_TOKEN.")
|
| 304 |
+
|
| 305 |
+
if not self.current_provider or not self.current_model:
|
| 306 |
+
raise ValueError("Provider and model must be set before making API calls")
|
| 307 |
+
|
| 308 |
+
model_endpoint = self.get_model_endpoint()
|
| 309 |
+
|
| 310 |
+
# Base params
|
| 311 |
+
params = {
|
| 312 |
+
"model": model_endpoint,
|
| 313 |
+
"messages": messages,
|
| 314 |
+
"max_tokens": kwargs.pop("max_tokens", 8192),
|
| 315 |
+
"temperature": kwargs.get("temperature", 0.3),
|
| 316 |
+
"stream": True,
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
# Merge stored generation params (exclude stream keys)
|
| 320 |
+
for k, v in self.generation_params.items():
|
| 321 |
+
if k not in ("model", "messages", "stream") and k not in params:
|
| 322 |
+
params[k] = v
|
| 323 |
+
|
| 324 |
+
# Add any remaining kwargs (highest precedence)
|
| 325 |
+
params.update(kwargs)
|
| 326 |
+
|
| 327 |
+
# Defensive: If tools are absent, ensure tool-specific params are not sent
|
| 328 |
+
if "tools" not in params:
|
| 329 |
+
params.pop("tool_choice", None)
|
| 330 |
+
params.pop("tool_prompt", None)
|
| 331 |
+
|
| 332 |
+
# Apply reasoning only for GPT-OSS models
|
| 333 |
+
if AppConfig.is_gpt_oss_model(self.current_model):
|
| 334 |
+
reasoning_effort = kwargs.pop("reasoning_effort", self.generation_params.get("reasoning_effort", AppConfig.DEFAULT_REASONING_EFFORT))
|
| 335 |
+
if reasoning_effort:
|
| 336 |
+
system_message = None
|
| 337 |
+
for msg in messages:
|
| 338 |
+
if msg.get("role") == "system":
|
| 339 |
+
system_message = msg
|
| 340 |
+
break
|
| 341 |
+
if system_message:
|
| 342 |
+
system_message["content"] += f"\n\nReasoning: {reasoning_effort}"
|
| 343 |
+
else:
|
| 344 |
+
messages.insert(0, {"role": "system", "content": f"You are a helpful AI assistant. Reasoning: {reasoning_effort}"})
|
| 345 |
+
else:
|
| 346 |
+
params.pop("reasoning_effort", None)
|
| 347 |
+
|
| 348 |
+
try:
|
| 349 |
+
logger.info(f"🤖 Streaming from {model_endpoint} via {self.current_provider}")
|
| 350 |
+
stream = self.hf_client.chat.completions.create(**params)
|
| 351 |
+
return stream
|
| 352 |
+
except Exception as e:
|
| 353 |
+
logger.error(f"HF Inference API streaming failed: {e}")
|
| 354 |
+
raise
|
| 355 |
|
| 356 |
def generate_chat_completion_with_mcp_tools(self, messages: List[Dict[str, Any]], **kwargs) -> Dict[str, Any]:
|
| 357 |
"""Generate chat completion with MCP tool support"""
|