@@ -313,18 +313,95 @@ class StreamingAnalysisController < ApplicationController
313313end
314314```
315315
316+ ## OAuth Authentication for Multi-User Applications
317+
318+ For Rails applications with multiple users where each user needs their own MCP connection:
319+
320+ ### Quick Start
321+
322+ ``` bash
323+ # Install OAuth support
324+ rails generate ruby_llm:mcp:oauth:install
325+
326+ # Run migrations
327+ rails db:migrate
328+
329+ # Configure
330+ # .env
331+ DEFAULT_MCP_SERVER_URL=https://mcp.example.com/api
332+ MCP_OAUTH_SCOPES=mcp:read mcp:write
333+ ```
334+
335+ ### Usage Pattern
336+
337+ ``` ruby
338+ # User model
339+ class User < ApplicationRecord
340+ include UserMcpOauth # Adds mcp_connected?, mcp_client, etc.
341+ end
342+
343+ # Background job with per-user permissions
344+ class AiResearchJob < ApplicationJob
345+ def perform (user_id , query )
346+ user = User .find(user_id)
347+ client = user.mcp_client # Uses user's OAuth token!
348+
349+ tools = client.tools
350+ chat = RubyLLM .chat(provider: " anthropic/claude-sonnet-4" )
351+ .with_tools(* tools)
352+
353+ response = chat.ask(query)
354+ # ... save results ...
355+ end
356+ end
357+
358+ # Controller
359+ class ResearchController < ApplicationController
360+ def create
361+ if current_user.mcp_connected?
362+ AiResearchJob .perform_later(current_user.id, params[:query ])
363+ redirect_to research_path, notice: " Research started!"
364+ else
365+ redirect_to connect_mcp_connections_path,
366+ alert: " Please connect MCP server first"
367+ end
368+ end
369+ end
370+ ```
371+
372+ ### Key Features
373+
374+ - ** Per-user OAuth tokens** - Each user has their own credentials
375+ - ** Secure storage** - Encrypted tokens in database
376+ - ** Background jobs** - No browser needed after initial auth
377+ - ** Automatic refresh** - Tokens refresh transparently
378+ - ** Multi-server support** - Users can connect to multiple MCP servers
379+
380+ ### Complete Guide
381+
382+ For detailed implementation including:
383+ - Multi-tenant architecture
384+ - Token lifecycle management
385+ - Security best practices
386+ - Production deployment
387+ - Monitoring and alerts
388+
389+ See the ** [ Rails OAuth Integration Guide] ({% link guides/rails-oauth.md %})**
390+
316391## Next Steps
317392
318393Now that you have comprehensive Rails integration set up:
319394
3203951 . ** Configure your MCP servers** in ` config/mcps.yml `
3213962 . ** Choose your client management strategy** (manual vs automatic)
322- 3 . ** Implement MCP services** for your specific use cases
323- 4 . ** Add proper error handling and monitoring**
324- 5 . ** Set up tests** for your MCP integrations
397+ 3 . ** For multi-user OAuth** , see [ Rails OAuth Integration] ({% link guides/rails-oauth.md %})
398+ 4 . ** Implement MCP services** for your specific use cases
399+ 5 . ** Add proper error handling and monitoring**
400+ 6 . ** Set up tests** for your MCP integrations
325401
326402For more detailed information on specific topics:
327403
404+ - ** [ Rails OAuth Integration] ({% link guides/rails-oauth.md %})** - Multi-user OAuth setup
328405- ** [ Configuration] ({% link configuration.md %})** - Advanced client configuration
329406- ** [ Tools] ({% link server/tools.md %})** - Working with MCP tools
330407- ** [ Resources] ({% link server/resources.md %})** - Managing resources and templates
0 commit comments