Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 08:39:02

0001 import json
0002 import os
0003 from importlib import import_module
0004 from urllib.parse import urlparse
0005 
0006 import uvicorn
0007 from fastmcp import FastMCP
0008 
0009 from pandaserver.api.v1.http_client import api_url
0010 from pandaserver.config import mcp_config
0011 from pandaserver.pandamcp.mcp_utils import create_tool
0012 
0013 # remove or overwrite environment variables to avoid server's credential being used
0014 for var in ["PANDA_AUTH_ID_TOKEN", "PANDA_AUTH_VO", "X509_USER_PROXY"]:
0015     if var in os.environ:
0016         del os.environ[var]
0017 os.environ["X509_USER_PROXY"] = "/dev/null"
0018 
0019 # extract API's module path
0020 p = urlparse(api_url)
0021 api_module_path = ".".join([seg for seg in p.path.strip("/").split("/") if seg])
0022 
0023 # get a list of API endpoints to expose
0024 endpoints_to_expose = {}
0025 
0026 if os.path.exists(mcp_config.endpoint_list_file):
0027     with open(mcp_config.endpoint_list_file, "r") as f:
0028         endpoints_to_expose = json.load(f)
0029 
0030 # add an example endpoint if the file does not exist
0031 endpoints_to_expose.setdefault("system", [])
0032 if "is_alive" not in endpoints_to_expose["system"]:
0033     endpoints_to_expose["system"].append("is_alive")
0034 
0035 # create FastMCP instance
0036 main_mcp = FastMCP(name="Main")
0037 
0038 # add tools
0039 for mod, func_list in endpoints_to_expose.items():
0040     api_module = import_module(f"pandaserver.{api_module_path}.{mod}_api")
0041     for func_name in func_list:
0042         func = getattr(api_module, func_name)
0043         tool = create_tool(func)
0044         main_mcp.add_tool(tool)
0045 
0046 # get HTTP app
0047 http_app = main_mcp.http_app(transport=mcp_config.transport)
0048 
0049 if __name__ == "__main__":
0050     uvicorn.run(
0051         http_app,
0052         host="0.0.0.0",
0053         port=int(os.getenv("PANDA_SERVER_CONF_PORT_MCP", 25888)),
0054         ssl_keyfile=mcp_config.ssl_keyfile,
0055         ssl_certfile=mcp_config.ssl_certfile,
0056     )