Skip to main content

MCP Server with LangGraph with OpenFGA & Infisical

Complete integration guide for fine-grained authorization and secrets management.

🔐 Architecture Overview

🚀 Quick Start

1. Start Infrastructure

Services:

2. Setup OpenFGA

This creates:
  • Authorization store
  • Authorization model (users, organizations, tools, conversations, roles)
  • Sample relationship tuples
  • Runs verification tests
Important: Save the OPENFGA_STORE_ID and OPENFGA_MODEL_ID to your .env file!
To use Infisical:
  1. Sign up at https://app.infisical.com
  2. Create a project
  3. Generate Universal Auth credentials
  4. Add to .env:

4. Install Dependencies

5. Configure Environment

Required variables:

🔑 OpenFGA Authorization Model

Type Definitions

Relations

Inheritance Rules

  • Tool Execution: Users can execute tools if:
    • Directly granted executor relation
    • They own the tool (ownerexecutor)
    • They’re members of the tool’s organization
  • Conversation Access: Users can view conversations if:
    • Directly granted viewer relation
    • They own the conversation (ownerviewer)

Example Relationships

🔒 Infisical Secrets Management

Configuration

Secrets are loaded with this priority:
  1. Infisical (if configured)
  2. Environment variables (fallback)
  3. Default values (last resort)

Usage in Code

Automatic Loading

Secrets are automatically loaded in src/mcp_server_langgraph/core/config.py:

🛠️ MCP Server Integration

Running the Server

Available Tools

1. chat

Chat with the AI agent. Requirements:
  • User must have executor relation on tool:chat
  • User must have editor relation on target conversation
Input:

2. get_conversation

Retrieve conversation history. Requirements:
  • User must have viewer relation on target conversation
Input:

3. list_conversations

List all accessible conversations. Requirements:
  • Authenticated user
Input:

Authorization Flow

📊 Testing

Test Authorization Rules

This demonstrates:
  • Tool access checks
  • Conversation ownership/viewing
  • Organization-based permissions
  • Listing accessible resources
  • Adding/removing relationships
  • Expanding relationships

Test MCP Server

Tests:
  • Authentication (success/failure)
  • Tool execution authorization
  • Conversation access control
  • Listing conversations
  • Unauthorized access attempts

🔧 Adding Users and Permissions

Grant User Access to Tool

Grant User Access to Conversation

Add User to Organization

Remove Permissions

📈 Observability

All authorization checks are traced and logged:

Distributed Tracing

Every request creates spans:
  • mcp.call_tool - Tool invocation
  • auth.authenticate - User authentication
  • auth.authorize - OpenFGA check
  • openfga.check - Permission check
  • agent.chat - Agent execution
View in Jaeger: http://localhost:16686

Metrics

  • agent.tool.calls - Tool invocation count
  • auth.failures - Authentication failures
  • authz.failures - Authorization failures (by resource)
  • agent.calls.successful - Successful operations
  • agent.calls.failed - Failed operations
Query in Prometheus: http://localhost:9090

Logs

Structured logs with trace correlation:

🏗️ Production Deployment

OpenFGA

Use PostgreSQL backend instead of in-memory:

Infisical

  1. Create production project in Infisical
  2. Use environment-specific credentials
  3. Enable secret versioning
  4. Set up secret rotation
  5. Use machine identities for service accounts

Security Checklist

  • Rotate JWT secret keys
  • Use production Infisical project
  • Enable OpenFGA audit logs
  • Set up secret rotation
  • Configure HTTPS for all services
  • Enable rate limiting
  • Set up monitoring alerts
  • Implement backup strategy
  • Review and minimize permissions
  • Enable MFA for admin accounts

🎯 Use Cases

Multi-Tenant SaaS

Role-Based Access Control

Conversation Sharing

🆘 Troubleshooting

OpenFGA Not Working

Infisical Connection Issues

Authorization Always Fails

  1. Check OpenFGA relationships exist
  2. Verify user_id format (user:alice)
  3. Check resource format (tool:chat, conversation:thread_1)
  4. Review logs for specific errors
  5. Test with setup script first

📚 Additional Resources

🤝 Support

For issues or questions:
  1. Check the troubleshooting section
  2. Review example scripts
  3. Check observability dashboards
  4. File an issue with logs and trace IDs