Skip to main content
This example demonstrates how to use JWT middleware with AgentOS for authentication and automatic parameter injection using Authorization headers.

Code

jwt_middleware.py

Usage

1

Set up your virtual environment

2

Set Environment Variables

3

Install dependencies

4

Setup PostgreSQL Database

5

Run Example

The server will start and print a test JWT token to the console.
6

Test JWT Authentication

Test with the generated token:
Test without token (should still work validate=False):
Check the AgentOS API docs: Visit http://localhost:7777/docs to see all available endpoints.

How It Works

  1. JWT Generation: The example creates a test JWT token with user claims
  2. Middleware Setup: JWT middleware extracts claims from the Authorization: Bearer <token> header
  3. Parameter Injection: The middleware automatically injects:
    • user_id from the sub claim
    • session_id from the session_id claim
    • dependencies dict with name, email, and roles
  4. Agent Tools: The agent can access user details through the injected dependencies

Next Steps