Home / Function/ test_middleware_can_add_and_remove_tools() — langchain Function Reference

test_middleware_can_add_and_remove_tools() — langchain Function Reference

Architecture documentation for the test_middleware_can_add_and_remove_tools() function in test_tools.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  751dd8ed_d517_0555_a1c9_bc76a0be6e50["test_middleware_can_add_and_remove_tools()"]
  5e05f5a6_3c9a_6b02_08a5_596bf1a6469a["test_tools.py"]
  751dd8ed_d517_0555_a1c9_bc76a0be6e50 -->|defined in| 5e05f5a6_3c9a_6b02_08a5_596bf1a6469a
  style 751dd8ed_d517_0555_a1c9_bc76a0be6e50 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_tools.py lines 162–212

def test_middleware_can_add_and_remove_tools() -> None:
    """Test that middleware can dynamically add/remove tools based on state."""

    @tool
    def search(query: str) -> str:
        """Search for information."""
        return f"Search results for: {query}"

    @tool
    def admin_tool(command: str) -> str:
        """Admin-only tool."""
        return f"Admin: {command}"

    class AdminState(AgentState[Any]):
        is_admin: bool

    class ConditionalToolMiddleware(AgentMiddleware[AdminState]):
        state_schema = AdminState

        def wrap_model_call(
            self,
            request: ModelRequest,
            handler: Callable[[ModelRequest], ModelResponse],
        ) -> ModelCallResult:
            # Remove admin_tool if not admin
            if not request.state.get("is_admin", False):
                filtered_tools: list[BaseTool | dict[str, Any]] = []
                for t in request.tools:
                    assert isinstance(t, BaseTool)
                    if t.name != "admin_tool":
                        filtered_tools.append(t)
                request = request.override(tools=filtered_tools)
            return handler(request)

    model = FakeToolCallingModel()

    agent = create_agent(
        model=model,
        tools=[search, admin_tool],
        system_prompt="You are a helpful assistant.",
        middleware=[ConditionalToolMiddleware()],
    )

    # Test non-admin user - should not have access to admin_tool
    # We can't directly inspect the bound model, but we can verify the agent runs
    result = agent.invoke({"messages": [HumanMessage("Hello")], "is_admin": False})
    assert "messages" in result

    # Test admin user - should have access to all tools
    result = agent.invoke({"messages": [HumanMessage("Hello")], "is_admin": True})
    assert "messages" in result

Domain

Subdomains

Frequently Asked Questions

What does test_middleware_can_add_and_remove_tools() do?
test_middleware_can_add_and_remove_tools() is a function in the langchain codebase, defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_tools.py.
Where is test_middleware_can_add_and_remove_tools() defined?
test_middleware_can_add_and_remove_tools() is defined in libs/langchain_v1/tests/unit_tests/agents/middleware/core/test_tools.py at line 162.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free