Skip to content

API Reference parrot.mcp.filtering

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.mcp.filtering

View source

Tool filtering module for dynamic, context-aware MCP tool filtering.

This module provides:

  • ToolPredicate protocol for custom filtering logic
  • Built-in predicates (by_name, by_permission, by_role, by_organization)
  • Predicate combinators (combine_and, combine_or)

Alias AbstractTool

Re-exported from parrot.tools.abstract.AbstractTool.

Alias ReadonlyContext

Re-exported from parrot.mcp.context.ReadonlyContext.

Class ToolPredicate

Protocol for tool filtering logic.

A ToolPredicate is a callable that determines if a tool should be available in a given context. It receives:

  • tool: The AbstractTool to evaluate
  • context: Optional ReadonlyContext with user/org information

Returns True if tool should be available, False otherwise.

Function allow_all_tools

Allow all tools regardless of context.

allow_all_tools(tool: AbstractTool, context: Optional['ReadonlyContext'] = None) -> bool

Parameter Type Default Description
tool AbstractTool
context Optional['ReadonlyContext'] None

Returns: bool

Function by_organization

Create predicate that restricts to specific organizations (multi-tenancy).

by_organization(allowed_org_ids: List[str]) -> ToolPredicate

Parameter Type Default Description
allowed_org_ids List[str] List of organization IDs that can access tools

Returns: ToolPredicate — ToolPredicate that checks organization via context

Function by_permission

Create predicate that requires specific permission.

by_permission(required_permission: str) -> ToolPredicate

Parameter Type Default Description
required_permission str Permission string (e.g., 'use_mcp_tools', 'admin')

Returns: ToolPredicate — ToolPredicate that checks user permission via context

Function by_role

Create predicate that requires specific role.

by_role(required_role: str) -> ToolPredicate

Parameter Type Default Description
required_role str Role name (e.g., 'admin', 'hr', 'finance')

Returns: ToolPredicate — ToolPredicate that checks user role via context

Function by_scope

Create predicate that requires OAuth scope.

by_scope(required_scope: str) -> ToolPredicate

Parameter Type Default Description
required_scope str OAuth scope (e.g., 'read:calendar', 'write:email')

Returns: ToolPredicate — ToolPredicate that checks OAuth scopes via context

Function by_server

Create predicate that filters by MCP server name.

by_server(server_name: str) -> ToolPredicate

Parameter Type Default Description
server_name str Server name (e.g., 'chrome-devtools', 'fireflies')

Returns: ToolPredicate — ToolPredicate that checks if tool belongs to server

Function by_tool_name

Create predicate that filters by tool name (simple allowlist).

by_tool_name(allowed_names: List[str]) -> ToolPredicate

Parameter Type Default Description
allowed_names List[str] List of tool names to allow

Returns: ToolPredicate — ToolPredicate that checks if tool name is in allowed list

Function by_tool_pattern

Create predicate that filters tools by name pattern.

by_tool_pattern(pattern: str) -> ToolPredicate

Parameter Type Default Description
pattern str Glob-style pattern (e.g., 'mcp_read', 'chrome_*')

Returns: ToolPredicate — ToolPredicate that matches tool names against pattern

Function by_user

Create predicate that restricts to specific users.

by_user(allowed_user_ids: List[str]) -> ToolPredicate

Parameter Type Default Description
allowed_user_ids List[str] List of user IDs that can access tools

Returns: ToolPredicate — ToolPredicate that checks user via context

Function combine_and

Combine multiple predicates with AND logic (all must pass).

combine_and(*predicates: ToolPredicate) -> ToolPredicate

Parameter Type Default Description
*predicates ToolPredicate () Variable number of ToolPredicate functions

Returns: ToolPredicate — ToolPredicate that passes only if all predicates pass

Function combine_or

Combine multiple predicates with OR logic (any can pass).

combine_or(*predicates: ToolPredicate) -> ToolPredicate

Parameter Type Default Description
*predicates ToolPredicate () Variable number of ToolPredicate functions

Returns: ToolPredicate — ToolPredicate that passes if any predicate passes

Function deny_all_tools

Deny all tools regardless of context.

deny_all_tools(tool: AbstractTool, context: Optional['ReadonlyContext'] = None) -> bool

Parameter Type Default Description
tool AbstractTool
context Optional['ReadonlyContext'] None

Returns: bool

Function exclude_by_tool_name

Create predicate that blocks specific tool names (blocklist).

exclude_by_tool_name(blocked_names: List[str]) -> ToolPredicate

Parameter Type Default Description
blocked_names List[str] List of tool names to block

Returns: ToolPredicate — ToolPredicate that denies tools in blocked list

Function filter_tools

Filter tools using a predicate or allowlist.

filter_tools(tools: List[AbstractTool], predicate: Optional[Union[ToolPredicate, List[str]]], context: Optional['ReadonlyContext'] = None) -> List[AbstractTool]

Parameter Type Default Description
tools List[AbstractTool] List of tools to filter
predicate Optional[Union[ToolPredicate, List[str]]] ToolPredicate, list of tool names, or None
context Optional['ReadonlyContext'] None Optional ReadonlyContext for context-aware filtering

Returns: List[AbstractTool] — Filtered list of tools

Function negate

Negate a predicate (invert boolean result).

negate(predicate: ToolPredicate) -> ToolPredicate

Parameter Type Default Description
predicate ToolPredicate ToolPredicate to negate

Returns: ToolPredicate — ToolPredicate that returns the opposite of input predicate

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally