60 lines
1.4 KiB
Python
60 lines
1.4 KiB
Python
"""Authentication and local LLM integration for MCP Server CLI."""
|
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
class LocalLLMAuth:
|
|
"""Authentication for local LLM providers."""
|
|
|
|
def __init__(self, base_url: str, api_key: Optional[str] = None):
|
|
"""Initialize LLM authentication.
|
|
|
|
Args:
|
|
base_url: Base URL for the LLM API.
|
|
api_key: Optional API key.
|
|
"""
|
|
self.base_url = base_url
|
|
self.api_key = api_key
|
|
|
|
def get_headers(self) -> Dict[str, str]:
|
|
"""Get headers for API requests.
|
|
|
|
Returns:
|
|
Dictionary of headers.
|
|
"""
|
|
headers = {"Content-Type": "application/json"}
|
|
if self.api_key:
|
|
headers["Authorization"] = f"Bearer {self.api_key}"
|
|
return headers
|
|
|
|
def get_chat_endpoint(self) -> str:
|
|
"""Get the chat completions endpoint.
|
|
|
|
Returns:
|
|
Full URL for chat completions.
|
|
"""
|
|
return f"{self.base_url}/v1/chat/completions"
|
|
|
|
|
|
class LLMResponse(BaseModel):
|
|
"""Response from LLM API."""
|
|
|
|
id: str
|
|
object: str
|
|
created: int
|
|
model: str
|
|
choices: list
|
|
usage: Dict[str, int]
|
|
|
|
|
|
class LLMChatRequest(BaseModel):
|
|
"""Request to LLM chat API."""
|
|
|
|
model: str
|
|
messages: list
|
|
temperature: float = 0.7
|
|
max_tokens: int = 2048
|
|
stream: bool = False
|