cohere-toolkit

1. Create an Authentication Class

You need to create a class specifically for your tool’s authentication. For great examples, check out the implementations for GMail, GDrive or Slack.

2. Inheritance

Your authentication class should inherit from:

3. Required Methods

The following methods are mandatory for the authentication class:

a. get_auth_url(self, user_id: str) -> str

Example:

class MyToolAuth(BaseToolAuthentication, ToolAuthenticationCacheMixin):
    def get_auth_url(self, user_id: str) -> str:
        # Build and return the authentication URL for the frontend
        return f"https://mytool.com/oauth2/authorize?client_id={CLIENT_ID}&redirect_uri={REDIRECT_URI}&response_type=code"

b. try_refresh_token(self, session: DBSessionDep, user_id: str, tool_auth: ToolAuth) -> bool

Example:

def try_refresh_token(self, session: DBSessionDep, user_id: str, tool_auth: ToolAuth) -> bool:
    # Example logic to refresh the token
    refresh_token = tool_auth.refresh_token
    if refresh_token:
        response = requests.post("https://mytool.com/oauth2/token", data={
            'client_id': CLIENT_ID,
            'client_secret': CLIENT_SECRET,
            'refresh_token': refresh_token,
            'grant_type': 'refresh_token',
        })
        if response.status_code == 200:
            new_access_token = response.json().get('access_token')
            # Update the tool_auth in the database with the new token
            tool_auth.access_token = new_access_token
            session.commit()
            return True

    return False

c. retrieve_auth_token(self, request: Request, session: DBSessionDep, user_id: str) -> str

Example:

def retrieve_auth_token(self, request: Request, session: DBSessionDep, user_id: str) -> str:
    # Get the authorization code from the query parameters
    auth_code = request.query_params.get("code")
    if not auth_code:
        raise ValueError("Authorization code missing")

    # Exchange the authorization code for an access token
    response = requests.post("https://mytool.com/oauth2/token", data={
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET,
        'code': auth_code,
        'redirect_uri': REDIRECT_URI,
        'grant_type': 'authorization_code',
    })

    if response.status_code != 200:
        raise ValueError("Failed to retrieve access token")

    # Extract the access token from the response
    access_token = response.json().get("access_token")

    # Save the token to the database for the user
    tool_auth = ToolAuth(user_id=user_id, access_token=access_token)
    session.add(tool_auth)
    session.commit()

    return access_token

4. Integrate the Auth Class

Once you’ve created your authentication class, integrate it into your tool’s configuration so that the frontend can use it for authenticating users when interacting with the tool.

To do so, go to your tool’s get_tool_definition method and add: auth_implementation=<YourAuthClass>,