Skip to content

[BOUNTY #2] LangChain Tool Guard β€” APort verification for LangChain tools#94

Open
JustABiologist wants to merge 2 commits into
aporthq:mainfrom
JustABiologist:feat/langchain-tool-guard
Open

[BOUNTY #2] LangChain Tool Guard β€” APort verification for LangChain tools#94
JustABiologist wants to merge 2 commits into
aporthq:mainfrom
JustABiologist:feat/langchain-tool-guard

Conversation

@JustABiologist
Copy link
Copy Markdown

πŸ’° Claims bounty #2 β€” $50 USD

What

APortToolGuard β€” a wrapper for any LangChain BaseTool that gates every
tool invocation behind an APort policy verification check. Before the
tool's logic executes, the agent's passport is verified against the
specified policy.

Features

  • Supports all LangChain tool types: @tool decorator, StructuredTool, classic Tool
  • Configurable agent ID resolution: config metadata β†’ instance attribute β†’ env var β†’ graceful failure
  • Fail-open / fail-closed modes for API error handling
  • Mock client included (drop-in replacement for aporthq-sdk-python when available)
  • Policy-per-tool β€” different tools can be guarded with different policies

Acceptance Criteria

  • Working class in /examples/agent-frameworks/langchain with README and example agent using protected tools
  • APortToolGuard class that wraps LangChain tools
  • Verification before tool execution
  • Comprehensive tests and documentation
  • Example usage with different tool types

Files

examples/agent-frameworks/langchain/
β”œβ”€β”€ tool_guard.py      # APortToolGuard + guard_tool() convenience function
β”œβ”€β”€ example.py         # Working demo: 4 scenarios (unguarded, guarded, multi-tool, config override)
β”œβ”€β”€ test_tool_guard.py # 24 passing pytest cases
└── README.md          # Install, quick start, API reference

Test Evidence

24 passed in 0.18s

Coverage:

  • Tool construction (decorated, structured, classic, convenience)
  • Verification pass-through (sync, structured, convenience)
  • Verification blocking (missing agent ID, fail-open, fail-closed)
  • Agent ID resolution (explicit, env var, config metadata, priority)
  • Mock client behaviour
  • Error messages
  • Metadata delegation (name, description, args_schema)
  • Integration with multi-tool agent

Example Output

============================================================
LangChain + APort Tool Guard β€” Example
============================================================

1. Unguarded tool β€” no APort check:
   βœ… Result: customer_cust_42@example.com

2. Guarded tool β€” APort verification passes β†’ tool executes:
   βœ… Result: Refund of $49.99 issued for transaction txn_abc

3. Guarded tool β€” data export with privacy policy:
   βœ… Result: Exported data for user user_789 in csv format

4. Guarded tool with per-call agent_id via RunnableConfig:
   βœ… Result: Refund of $25.00 issued for transaction txn_dynamic

Quick Start

from tool_guard import guard_tool
from langchain_core.tools import tool

@tool
def refund(amount: float) -> str:
    """Issue a refund."""
    return f"Refunded ${amount:.2f}"

guarded = guard_tool(refund, "finance.payment.refund.v1", agent_id="agt_123")
result = guarded.invoke({"amount": 99.99})  # APort verify runs first

Adds APortToolGuard, a wrapper for any LangChain BaseTool that gates
every invocation behind an APort policy verification call.

Changes:
- `examples/agent-frameworks/langchain/tool_guard.py`
  - APortToolGuard class wrapping BaseTool with pre-execution verify
  - guard_tool() convenience function
  - MockAPortClient for development (replaceable with real SDK)
  - Agent ID resolution: config metadata > instance attribute > env var
  - fail_open/fail_closed modes for API error handling
  - Supports @tool, StructuredTool, and classic Tool types

- `examples/agent-frameworks/langchain/example.py`
  - Working demo agent mixing guarded and unguarded tools
  - Per-call agent_id override via RunnableConfig

- `examples/agent-frameworks/langchain/test_tool_guard.py`
  - 24 pytest cases: construction, verification pass/block, agent ID
    resolution, mock client, error types, metadata delegation, and
    integration with an agent using multiple guarded tools

- `examples/agent-frameworks/langchain/README.md`
  - Install, quick start, tool type support, policies, fail-open mode

Closes aporthq#2
@JustABiologist
Copy link
Copy Markdown
Author

Thanks for reviewing. This PR is my submission for bounty #2 ($50 USD). If accepted/merged and the bounty is still payable, payout can be sent via PayPal to stab.me.papi@gmail.com. Happy to use another maintainer-preferred payout method if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant