22from fastmcp import FastMCP , settings
33import json
44import os
5+ from .config import Config
56import logging
67import time
78from .tool_helpers import (
1516logger = logging .getLogger (__name__ )
1617logger .setLevel (logging .DEBUG )
1718
18- WEB_SERVICE = os .environ .get ("WEB_SERVICE" , "crapi-web" )
19- IDENTITY_SERVICE = os .environ .get ("IDENTITY_SERVICE" , "crapi-identity:8080" )
20- TLS_ENABLED = os .environ .get ("TLS_ENABLED" , "false" ).lower () in ("true" , "1" , "yes" )
21- BASE_URL = f"{ 'https' if TLS_ENABLED else 'http' } ://{ WEB_SERVICE } "
22- BASE_IDENTITY_URL = f"{ 'https' if TLS_ENABLED else 'http' } ://{ IDENTITY_SERVICE } "
23-
24- API_USER = os .environ .get ("API_USER" , "admin@example.com" )
25- API_PASSWORD = os .environ .get ("API_PASSWORD" , "Admin!123" )
26- API_URL = f"{ 'https' if TLS_ENABLED else 'http' } ://{ WEB_SERVICE } "
27-
19+ BASE_URL = f"{ 'https' if Config .TLS_ENABLED else 'http' } ://{ Config .WEB_SERVICE } "
20+ BASE_IDENTITY_URL = f"{ 'https' if Config .TLS_ENABLED else 'http' } ://{ Config .IDENTITY_SERVICE } "
2821API_KEY = None
29- API_AUTH_TYPE = "ApiKey"
3022
3123def get_api_key ():
3224 global API_KEY
33- # Try 5 times to get API key
25+ # Try 5 times to get client auth
3426 MAX_ATTEMPTS = 5
3527 for i in range (MAX_ATTEMPTS ):
3628 logger .info (f"Attempt { i + 1 } to get API key..." )
3729 if API_KEY is None :
38- login_body = {"email" : API_USER , "password" : API_PASSWORD }
39- apikey_url = f"{ BASE_IDENTITY_URL } /identity/management/user/apikey"
30+ login_body = {"email" : Config . API_USER , "password" : Config . API_PASSWORD }
31+ auth_url = f"{ BASE_IDENTITY_URL } /identity/management/user/apikey"
4032 headers = {
4133 "Content-Type" : "application/json" ,
4234 }
4335 with httpx .Client (
44- base_url = API_URL ,
36+ base_url = BASE_URL ,
4537 headers = headers ,
4638 ) as client :
47- response = client .post (apikey_url , json = login_body )
39+ response = client .post (auth_url , json = login_body )
4840 if response .status_code != 200 :
4941 if i == MAX_ATTEMPTS - 1 :
5042 logger .error (f"Failed to get API key after { i + 1 } attempts: { response .status_code } { response .text } " )
@@ -54,24 +46,23 @@ def get_api_key():
5446 response_json = response .json ()
5547 logger .info (f"Response: { response_json } " )
5648 API_KEY = response_json .get ("apiKey" )
57- logger .info (f"Chatbot API Key: { API_KEY } " )
49+ logger .info (f"MCP Server API Key: { API_KEY } " )
5850 return API_KEY
5951 return API_KEY
6052
61-
6253# Async HTTP client for API calls
6354def get_http_client ():
6455 """Create and configure the HTTP client with appropriate authentication."""
6556 headers = {
6657 "Authorization" : "ApiKey " + get_api_key (),
6758 }
6859 return httpx .AsyncClient (
69- base_url = API_URL ,
60+ base_url = BASE_URL ,
7061 headers = headers ,
7162 )
7263
7364# Load your OpenAPI spec
74- with open ("/app/resources/crapi-openapi-spec.json" , "r" ) as f :
65+ with open (Config . OPENAPI_SPEC , "r" ) as f :
7566 openapi_spec = json .load (f )
7667
7768# Create the MCP server
0 commit comments