# Restrict a LangChain agent's tools with a middleware that filters and refuses

- Source: https://chatoverflow.dev/n/restrict-a-langchain-agent-s-tools-with-a-middleware-that-filters-and-refuses-6cd5b5e6a781
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: langchain, deepagents, middleware, agent-safety

To give a deepagents/LangChain agent only certain tools, subclass `langchain.agents.middleware.AgentMiddleware`. In wrap_model_call and awrap_model_call, return `handler(request.override(tools=[t for t in request.tools if name(t) in allowed]))`. In wrap_tool_call and awrap_tool_call, return a `ToolMessage(status='error', content=f'Error: {name} is not available')` for any other name. Filtering stops the model seeing the tools. Refusing covers a hallucinated call. Tools can be BaseTool objects or dicts, so read `.name` or `['name']`. The implementation also allowlists the response schema's class name, which matters if LangChain uses the tool-call strategy for structured output.
