AI Agent Framework Integration

Category: Developer Guides | LangChain / CrewAI / OpenAI

Step-by-step developer guides to wrap tools and ReAct loops with Boundrix runtime guardrails.

1. LangChain & LangGraph Integration

Wrap your LangChain tools with the Boundrix gateway interceptor:

from langchain_core.tools import tool
from boundrix.client import BoundrixClient, AuthorizationDeniedError
from boundrix.gateway import GitHubGateway

# Initialize client & resource gateway
client = BoundrixClient(base_url="http://localhost:8080")
gateway = GitHubGateway(client, agent_id="agent-langchain-01", task_id="task-101")

@tool
def read_repository(repo: str, branch: str) -> str:
    """Reads repository contents on a specific branch."""
    try:
        res = gateway.read_repository(repo, branch)
        return f"Successfully read repo: {res['message']}"
    except AuthorizationDeniedError as e:
        return f"SECURITY BLOCKED: {e.reason}"

@tool
def modify_file(repo: str, branch: str, file_path: str, content: str) -> str:
    """Modifies or creates a file in the repository."""
    try:
        res = gateway.modify_file(repo, branch, file_path, content)
        return f"Successfully modified file: {res['message']}"
    except AuthorizationDeniedError as e:
        return f"SECURITY BLOCKED: {e.reason}"

tools = [read_repository, modify_file]

2. OpenAI & Claude Function Calling

When dispatching native LLM tool calls:

import json
from openai import OpenAI
from boundrix.client import BoundrixClient, AuthorizationDeniedError

openai_client = OpenAI()
boundrix = BoundrixClient(base_url="http://localhost:8080")

def execute_agent_tool_call(tool_call, agent_id="agent-01", task_id="task-101"):
    function_name = tool_call.function.name
    function_args = json.loads(tool_call.function.arguments)

    try:
        # Pre-execution boundary check (< 1ms)
        decision = boundrix.authorize(
            agent_id=agent_id,
            task_id=task_id,
            action=function_name,
            resource=function_args
        )
        if decision.is_allowed:
            return run_real_tool(function_name, function_args)
    except AuthorizationDeniedError as e:
        return json.dumps({
            "error": "AUTHORIZATION_DENIED",
            "reason": e.reason,
            "audit_id": e.audit_id
        })

3. CrewAI Custom Tool Integration

In CrewAI, inherit from `BaseTool`:

from crewai.tools import BaseTool
from boundrix.client import BoundrixClient, AuthorizationDeniedError
from boundrix.gateway import GitHubGateway

class GuardedModifyFileTool(BaseTool):
    name: str = "Guarded Modify File"
    description: str = "Modifies a file in GitHub after verifying Boundrix authorization."
    gateway: GitHubGateway

    def _run(self, repo: str, branch: str, file_path: str, content: str) -> str:
        try:
            res = self.gateway.modify_file(repo, branch, file_path, content)
            return res["message"]
        except AuthorizationDeniedError as e:
            return f"Action DENIED by Boundrix: {e.reason}"