FastMcp JWT Auth - JWT Authentication Extension for FastMcp RackTransport #147
Pepan
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
👋 Hi everyone!
I'm excited to share a new extension for the amazing FastMcp gem that adds JWT authentication support to the RackTransport.
The Problem
While FastMcp provides excellent MCP (Model Context Protocol) server functionality for Rails applications, it doesn't have built-in support for JWT authentication via Authorization headers. This becomes essential when integrating with external MCP clients that need to authenticate users through JWT tokens.
Without proper JWT authentication, you can't:
Current.userbased on token payloadThe Solution
FastMcp JWT Auth is a lightweight gem that monkey patches
FastMcp::Transports::RackTransportto add seamless JWT authentication support.🚀 Key Benefits:
Quick Setup
1. Installation
Add to your Gemfile:
2. Configuration
Create an initializer:
3. Client Configuration
Configure MCP clients with JWT authentication:
{ "mcpServers": { "your-rails-app": { "type": "sse", "name": "Your Rails MCP Server", "url": "https://your-app.com/mcp/sse", "headers": { "Authorization": "Bearer ${JWT_TOKEN}" } } } }Requirements
Features
✅ Automatic JWT Authentication - Extracts and validates JWT tokens from Authorization headers
✅ Configurable Callbacks - Customize token decoding, user lookup, and validation logic
✅ Thread Safety - Works seamlessly in multi-threaded Rails applications
✅ Current User Context - Automatically sets and cleans up
Current.user✅ Error Recovery - Graceful fallback when authentication fails
✅ Rails Integration - Automatic setup via Railtie, respects Rails logging
✅ Security Best Practices - Proper token handling and context cleanup
✅ Zero Dependencies - Only requires Rails and FastMcp
Real-World Usage
We're using this gem in production at WorkVector to enable secure MCP client authentication with our Rails application. It seamlessly handles JWT token validation and user context for thousands of MCP requests.
The gem arose from a practical need to integrate external MCP clients (like Claude Code) with our Rails application while maintaining proper user authentication and authorization.
How It Works
FastMcp::Transports::RackTransport#send_messageAuthorization: Bearer <token>headerCurrent.userfor request durationLinks
Acknowledgments
Huge thanks to @yjacquin for creating the excellent FastMcp gem that made this extension possible! 🙏
Would love to hear your thoughts, questions, or feedback! This gem was born from a real production need, and I'm excited to see how the community might use it.
Has anyone else been looking for JWT authentication with FastMcp? What authentication patterns are you using with MCP servers?
Beta Was this translation helpful? Give feedback.
All reactions