Commit graph

10 commits

Author SHA1 Message Date
Yûki VACHOT
d35e12b6ae add sse transport type 2025-11-30 01:54:46 +01:00
Claude
7c162691db
Fix middleware and schema cache issues
This commit addresses two critical issues in the MCP server:

1. CORS Middleware Fix:
   - Move CORS middleware outside the auth check so it's always enabled
   - CORS is required for browser-based MCP clients, regardless of auth
   - Fixes "OPTIONS /mcp HTTP/1.1 405 Method Not Allowed" error

2. Schema Cache Improvements:
   - Add specific handling for corrupted JSON cache files
   - Automatically remove corrupted cache files and log the action
   - Prevents startup failures due to malformed JSON
   - Better error messages to help diagnose cache issues

3. Configuration Updates:
   - Change default SCHEMA_CACHE_DIR from /app/data/schemas to ./data/schemas
   - Better default for local development (Docker still uses /app/data/schemas)
   - Update .env.example with clearer documentation

These fixes improve robustness and make local development easier.
2025-11-30 00:09:55 +00:00
Yûki VACHOT
dc40ab87ec Reformat with black for one file 2025-11-30 00:57:27 +01:00
Yûki VACHOT
c04ba73c8a Fix test for multi user session, add middleware based on app type + modify docker compose for development 2025-11-30 00:56:28 +01:00
Claude
482daa73e0
Fix CI/CD issues and add comprehensive tests for multi-user features
This commit fixes three critical issues identified in CI/CD and adds
comprehensive test coverage for the new multi-user functionality.

## Fixes

### 1. FastMCP Middleware Registration Error

**Problem**: `AttributeError: 'FastMCP' object has no attribute 'app'`

**Solution**: Implemented robust middleware registration that:
- Tries multiple possible locations where FastMCP might store the app
- Gracefully handles cases where app isn't immediately available
- Wraps the run() method to defer middleware addition if needed
- Attempts: _transport.app, sse.app, http_server.app, _app, _asgi_app
- Falls back gracefully with warning if middleware can't be added

**Files Changed**:
- src/geoguessr_mcp/main.py: Added smart middleware registration logic

### 2. Test Permission Errors

**Problem**: `PermissionError: [Errno 13] Permission denied: '/app'`
Schema registry tried to create /app/data/schemas in CI without permission

**Solution**: Made schema cache directory creation fault-tolerant:
- Catches PermissionError and OSError when creating cache directory
- Falls back to temporary directory (tempfile.mkdtemp) if permission denied
- Logs clear warning messages about fallback behavior
- Tests can now run in restricted environments

**Files Changed**:
- src/geoguessr_mcp/monitoring/schema/schema_registry.py: Added fallback logic

### 3. Black Formatting Issues

**Problem**: 10 files needed reformatting

**Solution**: Ran `black src/ --line-length 100` on all source files

**Files Formatted**:
- src/geoguessr_mcp/config.py
- src/geoguessr_mcp/api/dynamic_response.py
- src/geoguessr_mcp/middleware/auth.py
- src/geoguessr_mcp/main.py
- src/geoguessr_mcp/auth/multi_user_session.py
- src/geoguessr_mcp/tools/auth_tools.py
- src/tests/integration/test_auth_flow.py
- src/tests/unit/services/*.py (3 files)

## New Tests

Added comprehensive test coverage for multi-user features:

### test_user_context.py
- Tests UserContext creation with/without sessions
- Tests authentication status checking
- Tests session expiration handling
- Tests string representation
- Tests API key hashing for anonymous users
- Tests consistency of anonymous user IDs

### test_multi_user_session.py
- Tests MultiUserSessionManager initialization
- Tests session manager creation per API key
- Tests session manager reuse for same API key
- Tests isolation between different users
- Tests auth status reporting
- Tests context creation and retrieval

### test_request_context.py
- Tests context variable get/set operations
- Tests require_user_context() error handling
- Tests context isolation between requests
- Tests context updates and clearing
- Tests None handling

## Code Quality

All changes pass:
-  Python syntax checks (py_compile)
-  Black formatting (line-length 100)
-  Test structure validation
-  Import resolution

## CI/CD Impact

These fixes should resolve:
-  Test execution failures (permission errors)
-  Black formatting check failures
-  Runtime errors when starting server with auth enabled

Tests can now run in CI environment without requiring:
- Root permissions
- /app directory access
- Pre-created cache directories
2025-11-29 23:11:32 +00:00
Claude
07b1cb84b2
Add MCP server authentication and update Docker configuration
This commit implements several key improvements to the GeoGuessr MCP server:

## MCP Server Authentication
- Add Bearer token authentication for MCP server access control
- New middleware in src/geoguessr_mcp/middleware/auth.py
- Configuration via MCP_AUTH_ENABLED and MCP_API_KEYS environment variables
- Support for multiple API keys (comma-separated)
- Optional authentication - can be disabled for trusted deployments
- Clients connect using Authorization: Bearer YOUR_API_KEY header

## Docker Configuration Updates
- Update to use official pre-built image: nyxiumyuuki/geoguessr-mcp:latest
- Remove DOCKER_USERNAME and IMAGE_TAG from environment variables
- Simplify docker-compose.yml and docker-compose.prod.yml
- Remove healthcheck configuration (not necessary for the deployment)

## Deployment Improvements
- Move deploy.sh to scripts/deploy.sh for better organization
- Update deploy.sh to use official Docker image
- Add authentication validation in deployment script
- Improve deployment logging and error messages

## Documentation Updates
- Update README.md with authentication configuration examples
- Add MCP server authentication section with setup instructions
- Update environment variables table
- Simplify deployment instructions
- Update CLAUDE.md with new authentication architecture
- Add .env.example configuration for MCP authentication

## Technical Details
- Authentication middleware integrates with FastMCP's Starlette ASGI app
- Middleware validates Bearer tokens on all requests except /health
- Logs authentication attempts and failures
- Returns proper 401/403 HTTP status codes
- Validates configuration on startup to prevent misconfiguration

Resolves TODO items:
- [x] Fix Docker username in compose files and env vars
- [x] Add authentication to MCP server to allow access only to specific users
2025-11-29 22:16:01 +00:00
Yûki VACHOT
283d7deee4 Add production Docker Compose setup with Nginx and SSL; enhance test fixtures and environment variables for monitoring, logging, and schema caching; and clean up unused imports. 2025-11-29 02:41:39 +01:00
Yûki VACHOT
ec0fe38861 Code cleanup: standardized imports, refined formatting for consistency, and resolved minor redundancies in services, models, monitoring, and tools modules. 2025-11-29 00:49:36 +01:00
Yûki VACHOT
aad2bc93ea Enhance main module: added comprehensive server instructions, background API monitoring tasks, dynamic schema adaptation, and detailed logging configuration. Updated Settings with extended environment-based config options. 2025-11-29 00:12:51 +01:00
Yûki VACHOT
cfe4a641a6 Rework 2025-11-28 22:15:27 +01:00