|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code when working with the `fast_mcp_jwt_auth` gem. |
| 4 | + |
| 5 | +## Gem Overview |
| 6 | + |
| 7 | +FastMCp Jwt Auth provides JWT authentication for FastMcp RackTransport, enabling secure user authentication in MCP requests. |
| 8 | +It integrates seamlessly with Rails, allowing you to authenticate users using JWT tokens in a Rails application. |
| 9 | + |
| 10 | +## Code Conventions |
| 11 | + |
| 12 | +### Code Quality |
| 13 | +- Max 200 chars/line (soft limit - prefer readability over strict compliance) |
| 14 | + - breaking Ruby chain calls destroys the natural sentence flow and readability |
| 15 | +- 14 lines/method, 110 lines/class |
| 16 | +- Comments and tests in English |
| 17 | +- KEEP CODE DRY (Don't Repeat Yourself) |
| 18 | + |
| 19 | +### Ruby/Rails Philosophy |
| 20 | +- **DO IT RUBY WAY OR RAILS WAY** - it's not Python, Java or PHP! |
| 21 | +- Strong use of Ruby metaprogramming techniques |
| 22 | +- code line should look like human sentence (e.g. `3.times do` not `for i in 0..2 do` - Ruby syntax reads like English) |
| 23 | +- keep code raising exceptions when it's programmer's fault - DO NOT validate method parameters, expect them to be correct! Only validate user input |
| 24 | +- do not repeat name of parameter in method name (e.g. `def create_new_user_from_user(user)` should be `def create_new_user_from(user)`) |
| 25 | +- do not use extra variable if used only once - saves memory and reduces GC pressure under high traffic (e.g. `user = User.find(params[:id]); user.update(...)` should be `User.find(params[:id]).update(...)`) - use `.tap do` for chaining when you need to use the object later |
| 26 | +- use metaprogramming instead of case statements (e.g. `self.send(method_name, params)` instead of `case method_name; when "find_slot"...` - let Ruby handle method dispatch and NoMethodError) |
| 27 | +- PREFER FUNCTIONAL STYLE: use flat_map, map, select over loops and temp variables (e.g. `items.flat_map(&:children).uniq` not `results = []; items.each { |i| results.concat(i.children) }; results.uniq`) |
| 28 | +- USE PATTERN MATCHING: Ruby 3.0+ `case/in` for complex conditionals instead of if/elsif chains - more expressive and catches unhandled cases |
| 29 | +- ONE CLEAR RESPONSIBILITY: each method should do one thing well - if method has "and" in description, split it (e.g. `normalize_and_search` → `normalize` + `search`) |
| 30 | +- FOLLOW KISS PRINCIPLE: Keep It Simple, Stupid - avoid unnecessary complexity, use simple solutions first |
| 31 | +- ALWAYS TEST YOUR CODE |
| 32 | + |
| 33 | +### Error Handling |
| 34 | +- Use meaningful exception classes (not generic StandardError) |
| 35 | +- Log errors with context using the configured logger |
| 36 | +- Proper error propagation with fallback mechanisms |
| 37 | +- Use `rescue_from` for common exceptions in Rails integration |
| 38 | + |
| 39 | +### Performance Considerations |
| 40 | +- Use database connection pooling efficiently |
| 41 | +- Avoid blocking operations in main threads |
| 42 | +- Cache expensive operations |
| 43 | +- Monitor thread lifecycle and cleanup |
| 44 | + |
| 45 | +### Thread Safety |
| 46 | +- All operations must be thread-safe for cluster mode |
| 47 | +- Use proper synchronization when accessing shared resources |
| 48 | +- Handle thread lifecycle correctly (creation, monitoring, cleanup) |
| 49 | +- Use connection checkout/checkin pattern for database operations |
| 50 | + |
| 51 | +### Gem Specific Guidelines |
| 52 | + |
| 53 | +#### Configuration |
| 54 | +- Use configuration object pattern for all settings |
| 55 | +- Provide sensible defaults that work out of the box |
| 56 | +- Make all components configurable but not required |
| 57 | +- Support both programmatic and initializer-based configuration |
| 58 | + |
| 59 | +#### Rails Integration |
| 60 | +- Use Railtie for automatic Rails integration |
| 61 | +- Hook into appropriate Rails lifecycle events |
| 62 | +- Respect Rails conventions for logging and error handling |
| 63 | +- Provide manual configuration options for non-Rails usage |
| 64 | + |
| 65 | +#### Error Recovery |
| 66 | +- Implement automatic retry with backoff for transient errors |
| 67 | +- Provide fallback mechanisms when PubSub fails |
| 68 | +- Log errors appropriately without flooding logs |
| 69 | +- Handle connection failures gracefully |
| 70 | + |
| 71 | +#### Testing |
| 72 | +- Test all public interfaces |
| 73 | +- Mock external dependencies (PostgreSQL, FastMcp) |
| 74 | +- Test error conditions and edge cases |
| 75 | +- Provide test helpers for gem users |
| 76 | +- Test both Rails and non-Rails usage |
| 77 | + |
| 78 | +## Architecture |
| 79 | + |
| 80 | +### Components |
| 81 | + |
| 82 | +1. **FastMcpJwtAuth::Service** - Core JWT authentication service |
| 83 | + - Handles JWT token generation and validation |
| 84 | + - Integrates with FastMcp RackTransport for secure requests |
| 85 | +2. **FastMcpJwtAuth::Configuration** - Configuration management |
| 86 | + - Manages settings like JWT secret, expiration, and algorithm |
| 87 | +3. **FastMcpJwtAuth::RackTransportPatch** - Monkey patch for FastMcp transport |
| 88 | + - Overrides `send_message` to include JWT authentication |
| 89 | +4. **FastMcpJwtAuth::Railtie** - Rails integration and lifecycle management |
| 90 | + - Automatically patches FastMcp::Transports::RackTransport during Rails initialization |
| 91 | + |
| 92 | +### Message Flow |
| 93 | + |
| 94 | +1. **MCP Request Received** - FastMcp RackTransport receives HTTP request with Authorization header |
| 95 | +2. **JWT Extraction** - Extract Bearer token from Authorization header (`HTTP_AUTHORIZATION`) |
| 96 | +3. **Token Decoding** - Use configured `jwt_decoder` callback to decode JWT token |
| 97 | +4. **Token Validation** - Validate token expiration and other claims using `token_validator` callback |
| 98 | +5. **User Lookup** - Find user from decoded token using `user_finder` callback |
| 99 | +6. **User Assignment** - Set current user in context using `current_user_setter` callback |
| 100 | +7. **Request Processing** - Continue with normal MCP request handling |
| 101 | +8. **Cleanup** - Clear current user context using `current_resetter` callback |
| 102 | + |
| 103 | +### Thread Management |
| 104 | + |
| 105 | +The gem is designed to be thread-safe for use in Rails applications: |
| 106 | + |
| 107 | +- **Request Isolation** - Each MCP request runs in its own thread context |
| 108 | +- **Current User Context** - Uses thread-local storage via Rails `Current` class for user context |
| 109 | +- **Monkey Patching Safety** - Patch is applied only once using thread-safe flag checking |
| 110 | +- **No Shared State** - All operations are stateless except for configuration (immutable after initialization) |
| 111 | +- **Callback Thread Safety** - User-provided callbacks (`jwt_decoder`, `user_finder`, etc.) must be thread-safe |
| 112 | +- **Automatic Cleanup** - Current user context is always cleared after request processing (even on exceptions) |
| 113 | + |
| 114 | +## Dependencies |
| 115 | + |
| 116 | +### Runtime Dependencies |
| 117 | +- **rails** (>= 7.0) - Required for Rails integration, Current class, and logger support |
| 118 | + |
| 119 | +### Development Dependencies |
| 120 | +- **jwt** (~> 2.0) - Used in tests for JWT token generation and decoding examples |
| 121 | +- **minitest** (~> 5.16) - Test framework |
| 122 | +- **rubocop** (~> 1.21) - Ruby code style enforcement |
| 123 | +- **rubocop-minitest** (~> 0.25) - Minitest-specific RuboCop rules |
| 124 | +- **rubocop-rails** (~> 2.0) - Rails-specific RuboCop rules |
| 125 | + |
| 126 | +### External Dependencies |
| 127 | +- **FastMcp** - The gem monkey patches `FastMcp::Transports::RackTransport` (not declared as dependency to avoid circular dependencies) |
| 128 | +- **JWT Library** - Users must provide their own JWT decoder implementation (commonly `jwt` gem) |
| 129 | + |
| 130 | +## Development |
| 131 | + |
| 132 | +### Running Tests |
| 133 | +```bash |
| 134 | +bundle exec rake test |
| 135 | +``` |
| 136 | + |
| 137 | +### Linting |
| 138 | +```bash |
| 139 | +bundle exec rubocop |
| 140 | +``` |
| 141 | + |
| 142 | +### Console |
| 143 | +```bash |
| 144 | +bundle exec rake console |
| 145 | +``` |
| 146 | + |
| 147 | +## WorkVector Task Access |
| 148 | +- To read a task from WorkVector, use the workvector-production MCP server: |
| 149 | + 1. Use `ListMcpResourcesTool` to get all available resources |
| 150 | + 2. Load template using `ReadMcpResourceTool` with URI "template://task" |
| 151 | + 3. Parse the task URL (e.g., https://workvector.com/jchsoft/tasks/8383) to extract account_code and task_id |
| 152 | + 4. Load task content using the template with account_code and task_id parameters |
| 153 | +- To log work progress, use `mcp__workvector-production__LogWorkProgressTool` with account_code, task_id, description and progress_percent. Log progress incrementally as you work on the task! |
| 154 | +- **IMPORTANT**: Always set progress_percent to max 90% on first task completion - leave a few percent for potential follow-ups and adjustments |
0 commit comments