|
| 1 | +--- |
| 2 | +title: LangChain Agent Auth |
| 3 | +--- |
| 4 | +LangChain Agent Auth enables secure access from agents to any other system using OAuth 2.0 credentials. With LangChain Agent Auth, build your agents to securely connect to any system on a person's behalf. |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +LangChain Agent Auth solves the challenge of secure authentication for autonomous agents. Instead of embedding credentials in agent code or requiring manual intervention for each API call, agents use the OAuth flow to obtain time-limited access tokens for external services. The system includes an authentication server for managing OAuth flows and storing credentials, as well as a Python client SDK for integrating with agents. |
| 9 | + |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Install directly from the wheel file for use with LangGraph: |
| 14 | + |
| 15 | +```bash |
| 16 | +pip install https://docs.langchain.com/langchain_auth-0.1.0-py3-none-any.whl[langgraph] |
| 17 | +``` |
| 18 | + |
| 19 | +For use without LangGraph: |
| 20 | + |
| 21 | +```bash |
| 22 | +pip install https://docs.langchain.com/langchain_auth-0.1.0-py3-none-any.whl |
| 23 | +``` |
| 24 | + |
| 25 | + |
| 26 | +## Quickstart |
| 27 | + |
| 28 | +### 1. Initialize the client |
| 29 | + |
| 30 | +```python |
| 31 | +from langchain_auth import Client |
| 32 | + |
| 33 | +client = Client(api_key="your-langsmith-api-key") |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. Set up OAuth providers |
| 37 | + |
| 38 | +Before agents can authenticate, you need to configure an OAuth provider: |
| 39 | + |
| 40 | +```python |
| 41 | +# Create GitHub provider |
| 42 | +github_provider = await client.create_oauth_provider( |
| 43 | + provider_id="github", |
| 44 | + name="GitHub", |
| 45 | + client_id="your-github-client-id", |
| 46 | + client_secret="your-github-client-secret", |
| 47 | + auth_url="https://github.com/login/oauth/authorize", |
| 48 | + token_url="https://github.com/login/oauth/access_token", |
| 49 | + user_info_url="https://api.github.com/user", |
| 50 | + scopes=["repo"] # Provide one or more scopes you would like to be available for this provider |
| 51 | +) |
| 52 | +``` |
| 53 | + |
| 54 | +### 3. Authenticate from an agent |
| 55 | + |
| 56 | +The client `authenticate()` API is used to get OAuth tokens from pre-configured providers. On the first call, it takes the caller through an OAuth 2.0 auth flow. |
| 57 | + |
| 58 | +#### In LangGraph context |
| 59 | + |
| 60 | +By default, tokens are scoped to the calling agent using the Assistant ID parameter. |
| 61 | + |
| 62 | +```python |
| 63 | +auth_result = await client.authenticate( |
| 64 | + provider="github", |
| 65 | + scopes=["repo"], |
| 66 | + user_id="your_user_id" # Any unique identifier to scope this token to the human caller |
| 67 | +) |
| 68 | + |
| 69 | +# Or if you'd like a token that can be used by any agent, set agent_scoped=False |
| 70 | +auth_result = await client.authenticate( |
| 71 | + provider="github", |
| 72 | + scopes=["repo"], |
| 73 | + user_id="your_user_id", |
| 74 | + agent_scoped=False |
| 75 | +) |
| 76 | +``` |
| 77 | + |
| 78 | +During execution, if authentication is required, the SDK will throw an [interrupt](https://langchain-ai.github.io/langgraph/how-tos/human_in_the_loop/add-human-in-the-loop/#pause-using-interrupt). The agent execution pauses and presents the OAuth URL to the user: |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +After the user completes OAuth authentication and we receive the callback from the provider, they will see the auth success page. |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +The agent then resumes execution from the point it left off at, and the token can be used for any API calls. We store and refresh OAuth tokens so that future uses of the service by either the user or agent do not require an OAuth flow. |
| 87 | + |
| 88 | +```python |
| 89 | +token = auth_result.token |
| 90 | +``` |
| 91 | + |
| 92 | +#### Outside LangGraph context |
| 93 | + |
| 94 | +Provide the `auth_url` to the user for out-of-band OAuth flows. |
| 95 | + |
| 96 | + |
| 97 | +```python |
| 98 | +# Default: user-scoped token (works for any agent under this user) |
| 99 | +auth_result = await client.authenticate( |
| 100 | + provider="github", |
| 101 | + scopes=["repo"], |
| 102 | + user_id="your_user_id" |
| 103 | +) |
| 104 | + |
| 105 | +if auth_result.needs_auth: |
| 106 | + print(f"Complete OAuth at: {auth_result.auth_url}") |
| 107 | + # Wait for completion |
| 108 | + completed_auth = await client.wait_for_completion(auth_result.auth_id) |
| 109 | + token = completed_auth.token |
| 110 | +else: |
| 111 | + token = auth_result.token |
| 112 | +``` |
0 commit comments