Files
gitea-mcp-remote/src/gitea_mcp/auth.py
lmiranda 1e0d896d87 feat: implement MCP server core and authentication
Implemented MCP server core infrastructure with authentication and HTTP client:

- Created auth.py for API token management
  - Loads GITEA_API_URL and GITEA_API_TOKEN from environment
  - Uses python-dotenv for .env file support
  - Validates required configuration on initialization
  - Provides authentication headers for API requests

- Created client.py with base HTTP client
  - GiteaClient class using httpx AsyncClient
  - Async HTTP methods: get(), post(), patch(), delete()
  - Comprehensive error handling for HTTP status codes
  - Custom exception hierarchy for different error types
  - Configurable timeout (default 30s)

- Updated server.py with MCP server setup
  - Initialized MCP server with StdioServerTransport
  - Integrated AuthConfig and GiteaClient
  - Registered placeholder tool handlers (list_repositories, create_issue, create_pull_request)
  - Added CLI with --help and --version options
  - Proper error handling for configuration failures

- Updated pyproject.toml
  - Added console script entry point: gitea-mcp

- Created comprehensive unit tests
  - test_auth.py: Tests for AuthConfig validation and headers
  - test_client.py: Tests for GiteaClient initialization and error handling

All acceptance criteria met:
- MCP server initializes with StdioServerTransport
- Authentication loads from environment variables
- Base HTTP client with auth headers implemented
- Error handling for API connection failures
- Server reports available tools (placeholders for future issues)

Closes #2

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 15:05:25 -05:00

52 lines
1.5 KiB
Python

"""Authentication and configuration management for Gitea MCP server."""
import os
from typing import Optional
from dotenv import load_dotenv
class AuthConfig:
"""Manages authentication configuration for Gitea API."""
def __init__(self):
"""Initialize authentication configuration from environment variables."""
load_dotenv()
self.api_url: Optional[str] = os.getenv("GITEA_API_URL")
self.api_token: Optional[str] = os.getenv("GITEA_API_TOKEN")
self._validate()
def _validate(self) -> None:
"""Validate that required configuration is present.
Raises:
ValueError: If required environment variables are missing.
"""
if not self.api_url:
raise ValueError(
"GITEA_API_URL environment variable is required. "
"Please set it in your .env file or environment."
)
if not self.api_token:
raise ValueError(
"GITEA_API_TOKEN environment variable is required. "
"Please set it in your .env file or environment."
)
# Remove trailing slash from URL if present
if self.api_url.endswith("/"):
self.api_url = self.api_url[:-1]
def get_auth_headers(self) -> dict[str, str]:
"""Get authentication headers for API requests.
Returns:
dict: HTTP headers with authorization token.
"""
return {
"Authorization": f"token {self.api_token}",
"Content-Type": "application/json",
}