summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--servers/gitlab_python/src/mcp_server_gitlab_python/server.py42
1 files changed, 36 insertions, 6 deletions
diff --git a/servers/gitlab_python/src/mcp_server_gitlab_python/server.py b/servers/gitlab_python/src/mcp_server_gitlab_python/server.py
index 9173df1..1b6ba76 100644
--- a/servers/gitlab_python/src/mcp_server_gitlab_python/server.py
+++ b/servers/gitlab_python/src/mcp_server_gitlab_python/server.py
@@ -33,23 +33,45 @@ def parse_gitlab_url_from_remote(remote_url: str) -> Optional[str]:
return f"{parts[0]}//{parts[2]}"
return None
-def get_token_from_glab_config() -> Optional[str]:
+def get_token_from_glab_config(host: str) -> Optional[str]:
+ """
+ Retrieve the GitLab token for a specific host from the glab-cli config file.
+
+ Args:
+ host (str): The GitLab host (e.g., 'gitlab.com').
+
+ Returns:
+ Optional[str]: The token if found, otherwise None.
+ """
config_path = os.path.expanduser("~/.config/glab-cli/config.yml")
if not os.path.exists(config_path):
return None
try:
with open(config_path, "r") as f:
config = yaml.safe_load(f)
- # Try to find a token in the config (glab stores tokens per host)
hosts = config.get('hosts', {})
- for host, data in hosts.items():
- if 'token' in data:
+ # Try direct match
+ if host in hosts and 'token' in hosts[host]:
+ return hosts[host]['token']
+ # Try matching by api_host if present
+ for h, data in hosts.items():
+ if data.get('api_host') == host and 'token' in data:
return data['token']
except Exception as e:
logger.warning(f"Could not parse glab-cli config: {e}")
return None
+
def get_gitlab_settings(working_directory: str) -> tuple[str, str]:
+ """
+ Determine the GitLab URL and token to use, matching the token to the detected host.
+
+ Args:
+ working_directory (str): The working directory to detect the git remote.
+
+ Returns:
+ tuple[str, str]: (url, token)
+ """
# URL
url = os.environ.get("GITLAB_HOST")
if not url:
@@ -58,12 +80,20 @@ def get_gitlab_settings(working_directory: str) -> tuple[str, str]:
url = parse_gitlab_url_from_remote(remote_url)
if not url:
url = "https://gitlab.com" # fallback default
+ # Ensure scheme is present
+ if not url.startswith("http://") and not url.startswith("https://"):
+ url = f"https://{url}"
+ # Extract host from URL
+ from urllib.parse import urlparse
+ parsed = urlparse(url)
+ host = parsed.hostname or url.replace("https://", "").replace("http://", "").split("/")[0]
# Token
token = os.environ.get("GITLAB_TOKEN")
if not token:
- token = get_token_from_glab_config()
+ token = get_token_from_glab_config(host)
if not token:
- raise RuntimeError("No GitLab token found in env or glab-cli config.")
+ logger.error(f"No GitLab token found for host '{host}' in env or glab-cli config.")
+ raise RuntimeError(f"No GitLab token found for host '{host}' in env or glab-cli config.")
return url, token
class GitLabPythonServer: