Coverage for mcpgateway / llm_provider_configs.py: 100%
60 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-11 07:10 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-02-11 07:10 +0000
1# -*- coding: utf-8 -*-
2"""Location: ./mcpgateway/llm_provider_configs.py
3Copyright 2025
4SPDX-License-Identifier: Apache-2.0
5Authors: Keval Mahajan
7LLM Provider-Specific Configuration Definitions.
9This module contains Pydantic models that define configuration parameters
10required for different LLM providers. Each provider
11has its own configuration schema to capture authentication details,
12regional settings, deployment identifiers, and provider-specific options.
14These configuration objects are intended to be used by the MCP Gateway
15to initialize and manage provider clients in a consistent and type-safe
16manner.
17"""
19# Standard
20from typing import Any, Dict, List, Optional
22# Third-Party
23from pydantic import BaseModel, Field
25# ---------------------------------------------------------------------------
26# Provider-Specific Configuration Models
27# ---------------------------------------------------------------------------
30class AWSBedrockConfig(BaseModel):
31 """AWS Bedrock-specific configuration.
33 Attributes:
34 region: AWS region where Bedrock is hosted (e.g., "us-east-1").
35 access_key_id: AWS access key ID. Optional if using IAM roles.
36 secret_access_key: AWS secret access key. Optional if using IAM roles.
37 session_token: AWS session token for temporary credentials.
38 profile_name: AWS profile name from ~/.aws/credentials.
40 """
42 region: str = Field(..., description="AWS region (e.g., us-east-1)")
43 access_key_id: Optional[str] = Field(None, description="AWS access key ID (optional if using IAM role)")
44 secret_access_key: Optional[str] = Field(None, description="AWS secret access key (optional if using IAM role)")
45 session_token: Optional[str] = Field(None, description="AWS session token for temporary credentials")
46 profile_name: Optional[str] = Field(None, description="AWS profile name from ~/.aws/credentials")
49class IBMWatsonXConfig(BaseModel):
50 """IBM Watson X AI-specific configuration.
52 Attributes:
53 project_id: Watson X project ID.
54 space_id: Watson X deployment space ID.
55 deployment_id: Deployment ID for a specific model deployment.
56 instance_id: Watson X service instance ID.
57 version: Watson X API version string.
58 url: Watson X service URL, overriding the default API base.
59 """
61 project_id: Optional[str] = Field(None, description="Watson X project ID")
62 space_id: Optional[str] = Field(None, description="Watson X deployment space ID")
63 deployment_id: Optional[str] = Field(None, description="Deployment ID for specific model deployment")
64 instance_id: Optional[str] = Field(None, description="Watson X instance ID")
65 version: str = Field(default="2023-05-29", description="Watson X API version")
66 url: Optional[str] = Field(None, description="Watson X service URL (overrides api_base)")
69class AzureOpenAIConfig(BaseModel):
70 """Azure OpenAI-specific configuration.
72 Attributes:
73 deployment_name: Azure OpenAI deployment name.
74 resource_name: Azure OpenAI resource name.
75 api_version: Azure OpenAI API version.
76 """
78 deployment_name: str = Field(..., description="Azure OpenAI deployment name")
79 resource_name: str = Field(..., description="Azure resource name")
80 api_version: str = Field(default="2024-02-15-preview", description="Azure OpenAI API version")
83class GoogleVertexAIConfig(BaseModel):
84 """Google Vertex AI-specific configuration.
86 Attributes:
87 project_id: Google Cloud project ID.
88 location: Google Cloud region or location.
89 credentials_path: Path to a service account JSON credentials file.
90 credentials_json: Service account credentials as a JSON string.
91 """
93 project_id: str = Field(..., description="Google Cloud project ID")
94 location: str = Field(default="us-central1", description="Google Cloud region/location")
95 credentials_path: Optional[str] = Field(None, description="Path to service account JSON file")
96 credentials_json: Optional[str] = Field(None, description="Service account JSON as string")
99class AnthropicConfig(BaseModel):
100 """Anthropic-specific configuration.
102 Attributes:
103 anthropic_version: Anthropic API version identifier.
104 """
106 anthropic_version: str = Field(default="2023-06-01", description="Anthropic API version header")
109class CohereConfig(BaseModel):
110 """Cohere-specific configuration.
112 Attributes:
113 truncate: Truncation strategy to apply. Valid values include
114 "NONE", "START", or "END".
115 """
117 truncate: Optional[str] = Field(None, description="Truncation strategy: NONE, START, END")
120class HuggingFaceConfig(BaseModel):
121 """Hugging Face-specific configuration.
123 Attributes:
124 task: Task type (e.g., "text-generation", "conversational").
125 use_cache: Whether to use cached inference results.
126 wait_for_model: Whether to wait if the model is still loading.
127 """
129 task: Optional[str] = Field(None, description="Task type (e.g., text-generation, conversational)")
130 use_cache: bool = Field(default=True, description="Whether to use cached results")
131 wait_for_model: bool = Field(default=False, description="Wait if model is loading")
134# ---------------------------------------------------------------------------
135# Provider Field Definitions for UI
136# ---------------------------------------------------------------------------
139class ProviderFieldDefinition(BaseModel):
140 """Definition of a provider-specific configuration field for UI rendering.
142 This model represents metadata for a single configuration field used
143 by a provider. It is primarily intended to drive dynamic UI generation
144 and validation for provider configuration forms.
146 Attributes:
147 name: Field name used as the key in the provider configuration dict.
148 label: Human-readable display label for the field.
149 field_type: Input type for the field (e.g., "text", "password",
150 "number", "select", "textarea").
151 required: Whether this field is mandatory.
152 default_value: Default value for the field, if any.
153 placeholder: Placeholder text shown in the input field.
154 help_text: Additional help or description displayed to the user.
155 options: List of selectable options for "select" field types.
156 Each option is typically a dict with keys like "label" and "value".
157 validation_pattern: Regular expression used to validate input.
158 min_value: Minimum allowed value for numeric fields.
159 max_value: Maximum allowed value for numeric fields.
160 """
162 name: str = Field(..., description="Field name (key in config dict)")
163 label: str = Field(..., description="Display label for the field")
164 field_type: str = Field(..., description="Input type: text, password, number, select, textarea")
165 required: bool = Field(default=False, description="Whether field is required")
166 default_value: Optional[Any] = Field(None, description="Default value")
167 placeholder: Optional[str] = Field(None, description="Placeholder text")
168 help_text: Optional[str] = Field(None, description="Help text to display")
169 options: Optional[List[Dict[str, str]]] = Field(None, description="Options for select fields")
170 validation_pattern: Optional[str] = Field(None, description="Regex pattern for validation")
171 min_value: Optional[float] = Field(None, description="Minimum value for number fields")
172 max_value: Optional[float] = Field(None, description="Maximum value for number fields")
175class ProviderConfigDefinition(BaseModel):
176 """Complete configuration definition for an LLM provider type.
178 This model defines all metadata required to describe how a provider
179 should be configured, including API requirements and provider-specific
180 configuration fields. It is commonly used to drive UI rendering,
181 validation, and onboarding flows for new providers.
183 Attributes:
184 provider_type: Unique identifier for the provider (e.g., "openai",
185 "azure_openai", "aws_bedrock").
186 display_name: Human-readable provider name displayed in the UI.
187 description: Short description of the provider and its purpose.
188 requires_api_key: Whether the provider requires an API key.
189 api_key_label: Label used when displaying the API key input field.
190 api_key_help: Help text shown alongside the API key input field.
191 requires_api_base: Whether the provider requires an API base URL.
192 api_base_default: Default API base URL, if applicable.
193 api_base_help: Help text shown for the API base URL field.
194 config_fields: List of provider-specific configuration field
195 definitions used for UI rendering and validation.
196 """
198 provider_type: str
199 display_name: str
200 description: str
201 requires_api_key: bool
202 api_key_label: str = "API Key"
203 api_key_help: Optional[str] = None
204 requires_api_base: bool = True
205 api_base_default: Optional[str] = None
206 api_base_help: Optional[str] = None
207 config_fields: List[ProviderFieldDefinition] = Field(default_factory=list)
210# ---------------------------------------------------------------------------
211# Provider Configuration Registry
212# ---------------------------------------------------------------------------
215PROVIDER_CONFIGS: Dict[str, ProviderConfigDefinition] = {
216 "openai": ProviderConfigDefinition(
217 provider_type="openai",
218 display_name="OpenAI",
219 description="OpenAI GPT models (GPT-4, GPT-4o, etc.)",
220 requires_api_key=True,
221 api_key_label="OpenAI API Key",
222 api_key_help="Get your API key from https://platform.openai.com/api-keys",
223 requires_api_base=True,
224 api_base_default="https://api.openai.com/v1",
225 api_base_help="Default: https://api.openai.com/v1",
226 config_fields=[],
227 ),
228 "azure_openai": ProviderConfigDefinition(
229 provider_type="azure_openai",
230 display_name="Azure OpenAI",
231 description="Azure OpenAI Service",
232 requires_api_key=True,
233 api_key_label="Azure API Key",
234 api_key_help="Azure OpenAI API key from Azure Portal",
235 requires_api_base=True,
236 api_base_default="https://{resource}.openai.azure.com",
237 api_base_help="Format: https://{resource-name}.openai.azure.com",
238 config_fields=[
239 ProviderFieldDefinition(
240 name="deployment_name",
241 label="Deployment Name",
242 field_type="text",
243 required=True,
244 placeholder="my-gpt4-deployment",
245 help_text="The name of your Azure OpenAI deployment",
246 ),
247 ProviderFieldDefinition(
248 name="resource_name",
249 label="Resource Name",
250 field_type="text",
251 required=True,
252 placeholder="my-openai-resource",
253 help_text="Your Azure OpenAI resource name",
254 ),
255 ProviderFieldDefinition(
256 name="api_version",
257 label="API Version",
258 field_type="text",
259 required=False,
260 default_value="2024-02-15-preview",
261 placeholder="2024-02-15-preview",
262 help_text="Azure OpenAI API version",
263 ),
264 ],
265 ),
266 "anthropic": ProviderConfigDefinition(
267 provider_type="anthropic",
268 display_name="Anthropic",
269 description="Anthropic Claude models",
270 requires_api_key=True,
271 api_key_label="Anthropic API Key",
272 api_key_help="Get your API key from https://console.anthropic.com/",
273 requires_api_base=True,
274 api_base_default="https://api.anthropic.com",
275 config_fields=[
276 ProviderFieldDefinition(
277 name="anthropic_version",
278 label="Anthropic Version",
279 field_type="text",
280 required=False,
281 default_value="2023-06-01",
282 placeholder="2023-06-01",
283 help_text="Anthropic API version header",
284 ),
285 ],
286 ),
287 "bedrock": ProviderConfigDefinition(
288 provider_type="bedrock",
289 display_name="AWS Bedrock",
290 description="AWS Bedrock (uses IAM credentials)",
291 requires_api_key=False,
292 requires_api_base=False,
293 config_fields=[
294 ProviderFieldDefinition(
295 name="region",
296 label="AWS Region",
297 field_type="text",
298 required=True,
299 default_value="us-east-1",
300 placeholder="us-east-1",
301 help_text="AWS region where Bedrock is available",
302 ),
303 ProviderFieldDefinition(
304 name="access_key_id",
305 label="AWS Access Key ID",
306 field_type="text",
307 required=False,
308 placeholder="AKIAIOSFODNN7EXAMPLE",
309 help_text="Optional: Leave empty to use IAM role or default credentials",
310 ),
311 ProviderFieldDefinition(
312 name="secret_access_key",
313 label="AWS Secret Access Key",
314 field_type="password",
315 required=False,
316 placeholder="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
317 help_text="Optional: Leave empty to use IAM role or default credentials",
318 ),
319 ProviderFieldDefinition(
320 name="session_token",
321 label="AWS Session Token",
322 field_type="password",
323 required=False,
324 help_text="Optional: For temporary credentials",
325 ),
326 ProviderFieldDefinition(
327 name="profile_name",
328 label="AWS Profile Name",
329 field_type="text",
330 required=False,
331 placeholder="default",
332 help_text="Optional: AWS profile from ~/.aws/credentials",
333 ),
334 ],
335 ),
336 "google_vertex": ProviderConfigDefinition(
337 provider_type="google_vertex",
338 display_name="Google Vertex AI",
339 description="Google Vertex AI (uses service account)",
340 requires_api_key=False,
341 requires_api_base=False,
342 config_fields=[
343 ProviderFieldDefinition(
344 name="project_id",
345 label="Project ID",
346 field_type="text",
347 required=True,
348 placeholder="my-gcp-project",
349 help_text="Google Cloud project ID",
350 ),
351 ProviderFieldDefinition(
352 name="location",
353 label="Location",
354 field_type="text",
355 required=True,
356 default_value="us-central1",
357 placeholder="us-central1",
358 help_text="Google Cloud region/location",
359 ),
360 ProviderFieldDefinition(
361 name="credentials_path",
362 label="Credentials File Path",
363 field_type="text",
364 required=False,
365 placeholder="/path/to/service-account.json",
366 help_text="Path to service account JSON file (leave empty to use default credentials)",
367 ),
368 ProviderFieldDefinition(
369 name="credentials_json",
370 label="Credentials JSON",
371 field_type="textarea",
372 required=False,
373 placeholder='{"type": "service_account", ...}',
374 help_text="Service account JSON content (alternative to file path)",
375 ),
376 ],
377 ),
378 "watsonx": ProviderConfigDefinition(
379 provider_type="watsonx",
380 display_name="IBM watsonx.ai",
381 description="IBM watsonx.ai",
382 requires_api_key=True,
383 api_key_label="IBM Cloud API Key",
384 api_key_help="IBM Cloud API key or Watson X API key",
385 requires_api_base=True,
386 api_base_default="https://us-south.ml.cloud.ibm.com",
387 api_base_help="Watson X service URL",
388 config_fields=[
389 ProviderFieldDefinition(
390 name="project_id",
391 label="Project ID",
392 field_type="text",
393 required=False,
394 placeholder="12345678-1234-1234-1234-123456789012",
395 help_text="Watson X project ID (required if not using space_id)",
396 ),
397 ProviderFieldDefinition(
398 name="space_id",
399 label="Space ID",
400 field_type="text",
401 required=False,
402 placeholder="12345678-1234-1234-1234-123456789012",
403 help_text="Watson X deployment space ID (required if not using project_id)",
404 ),
405 ProviderFieldDefinition(
406 name="deployment_id",
407 label="Deployment ID",
408 field_type="text",
409 required=False,
410 placeholder="12345678-1234-1234-1234-123456789012",
411 help_text="Optional: Specific deployment ID",
412 ),
413 ProviderFieldDefinition(
414 name="instance_id",
415 label="Instance ID",
416 field_type="text",
417 required=False,
418 help_text="Optional: Watson X instance ID",
419 ),
420 ProviderFieldDefinition(
421 name="version",
422 label="API Version",
423 field_type="text",
424 required=False,
425 default_value="2023-05-29",
426 placeholder="2023-05-29",
427 help_text="Watson X API version",
428 ),
429 ],
430 ),
431 "ollama": ProviderConfigDefinition(
432 provider_type="ollama",
433 display_name="Ollama",
434 description="Local Ollama server (OpenAI-compatible)",
435 requires_api_key=False,
436 requires_api_base=True,
437 api_base_default="http://localhost:11434/v1",
438 api_base_help="Ollama server URL (use /v1 suffix for OpenAI compatibility)",
439 config_fields=[],
440 ),
441 "openai_compatible": ProviderConfigDefinition(
442 provider_type="openai_compatible",
443 display_name="OpenAI Compatible",
444 description="Any OpenAI-compatible API server",
445 requires_api_key=False,
446 requires_api_base=True,
447 api_base_default="http://localhost:8080/v1",
448 api_base_help="Base URL of your OpenAI-compatible server",
449 config_fields=[],
450 ),
451 "cohere": ProviderConfigDefinition(
452 provider_type="cohere",
453 display_name="Cohere",
454 description="Cohere Command models",
455 requires_api_key=True,
456 api_key_label="Cohere API Key",
457 api_key_help="Get your API key from https://dashboard.cohere.com/api-keys",
458 requires_api_base=True,
459 api_base_default="https://api.cohere.ai/v1",
460 config_fields=[
461 ProviderFieldDefinition(
462 name="truncate",
463 label="Truncate Strategy",
464 field_type="select",
465 required=False,
466 help_text="How to handle inputs that exceed the model's context length",
467 options=[
468 {"value": "NONE", "label": "None - Return error"},
469 {"value": "START", "label": "Start - Truncate from start"},
470 {"value": "END", "label": "End - Truncate from end"},
471 ],
472 ),
473 ],
474 ),
475 "mistral": ProviderConfigDefinition(
476 provider_type="mistral",
477 display_name="Mistral AI",
478 description="Mistral AI models",
479 requires_api_key=True,
480 api_key_label="Mistral API Key",
481 api_key_help="Get your API key from https://console.mistral.ai/",
482 requires_api_base=True,
483 api_base_default="https://api.mistral.ai/v1",
484 config_fields=[],
485 ),
486 "groq": ProviderConfigDefinition(
487 provider_type="groq",
488 display_name="Groq",
489 description="Groq high-speed inference",
490 requires_api_key=True,
491 api_key_label="Groq API Key",
492 api_key_help="Get your API key from https://console.groq.com/keys",
493 requires_api_base=True,
494 api_base_default="https://api.groq.com/openai/v1",
495 config_fields=[],
496 ),
497 "together": ProviderConfigDefinition(
498 provider_type="together",
499 display_name="Together AI",
500 description="Together AI inference",
501 requires_api_key=True,
502 api_key_label="Together API Key",
503 api_key_help="Get your API key from https://api.together.xyz/settings/api-keys",
504 requires_api_base=True,
505 api_base_default="https://api.together.xyz/v1",
506 config_fields=[],
507 ),
508}
511def get_provider_config(provider_type: str) -> Optional[ProviderConfigDefinition]:
512 """Get configuration definition for a provider type.
514 Args:
515 provider_type: Provider type string.
517 Returns:
518 ProviderConfigDefinition or None if not found.
519 """
520 return PROVIDER_CONFIGS.get(provider_type)
523def get_all_provider_configs() -> Dict[str, ProviderConfigDefinition]:
524 """Get all provider configuration definitions.
526 Returns:
527 Dictionary of provider type to configuration definition.
528 """
529 return PROVIDER_CONFIGS