Skip to content

401 Unauthorized / Unexpected Endpoint when connecting to LM Studio with Server Auth or MCP enabled #96

Description

@almostfunctional

Description: Recent updates to LM Studio (0.4.0+) enforce a strict token authentication scheme when Server Authentication or Model Context Protocol (MCP) tools are active. This causes Orpheus-FastAPI to crash with an Error: API request failed with status code 401 (invalid_api_key) because the outbound requests.Session doesn't pass a bearer token.

Additionally, if the base URL points strictly to /v1, LM Studio warns of an Unexpected endpoint or method (POST /v1) and fails to generate tokens.

Here is a tested walkthrough to modify the inference engine to dynamically inject the bearer token and fix the endpoint routing path.
Fix:

  1. Generate your API token in LM Studio

  2. Open: your Orpheus project directory and edit the inference engine file at: tts_engine/inference.py

  3. Locate the while retry_count < max_retries: loop block.

Replace the standard session.post execution block with the following logic to handle the token handshake and endpoint appending:

    while retry_count < max_retries:
        try:
            # 1. Clone the headers dictionary and force inject the LM Studio Token with Bearer scheme
            auth_headers = HEADERS.copy() if isinstance(HEADERS, dict) else {}
            auth_headers["Authorization"] = "Bearer <YOUR_LM_STUDIO_TOKEN_HERE>"

            # 2. Fix the broken base path by forcing the legacy completions endpoint
            corrected_url = API_URL
            if corrected_url.endswith("/v1"):
                corrected_url = f"{corrected_url}/completions"
            elif corrected_url.endswith("/v1/"):
                corrected_url = f"{corrected_url}completions"

            # 3. Submit the network request to the proper authenticated endpoint
            response = session.post(
                corrected_url, 
                headers=auth_headers,
                json=payload,
                stream=True,
                timeout=REQUEST_TIMEOUT
            )
            
            if response.status_code != 200:
                # ... [The rest of your original streaming loop logic continues here unchanged] ...

Save inference.py and restart your Orpheus terminal application

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions