-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (65 loc) · 1.9 KB
/
main.py
File metadata and controls
77 lines (65 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from typing import Dict, Optional
import httpx
from fastmcp import FastMCP
import json
import os
from dotenv import load_dotenv
import sys
import logging
from logging_config import setup_logging
logger = setup_logging()
def get_auth_headers():
auth_token = os.getenv('AUTH_TOKEN')
if not auth_token:
logger.error("AUTH_TOKEN environment variable is not set")
sys.exit(1)
return {
'Authorization': f'Bearer {auth_token}',
}
def load_openapi_schema():
schema_path = os.getenv('OPENAPI_SCHEMA_PATH')
if not schema_path:
logger.error("OPENAPI_SCHEMA_PATH environment variable is not set")
sys.exit(1)
try:
with open(schema_path, 'r') as f:
return json.load(f)
except Exception as e:
logger.error(f"Failed to read or parse OpenAPI schema file: {e}")
sys.exit(1)
try:
return json.loads(schema_json)
except json.JSONDecodeError as e:
logger.error(f"Failed to parse OpenAPI schema: {str(e)}")
sys.exit(1)
def get_base_url():
base_url = os.getenv('API_BASE_URL')
if not base_url:
logger.error("API_BASE_URL environment variable is not set")
sys.exit(1)
return base_url
def main():
load_dotenv()
auth_headers = get_auth_headers()
openapi_spec = load_openapi_schema()
base_url = get_base_url()
client = httpx.AsyncClient(
base_url=base_url,
headers=auth_headers,
timeout=30.0
)
try:
mcp = FastMCP.from_openapi(
openapi_spec=openapi_spec,
client=client,
name="OpenOps API Server",
all_routes_as_tools=True,
default_headers=auth_headers,
)
mcp.run()
return mcp
except Exception as e:
logger.error(f"Failed to create OpenOps MCP client: {str(e)}", exc_info=True)
sys.exit(1)
if __name__ == "__main__":
main()