|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import abc |
| 4 | +from typing import TYPE_CHECKING, Any |
| 5 | +from pydantic import AnyUrl |
| 6 | + |
| 7 | +from mcp.shared.context import LifespanContextT, RequestT |
| 8 | + |
| 9 | +if TYPE_CHECKING: |
| 10 | + from mcp.server.fastmcp.server import Context |
| 11 | + from mcp.server.session import ServerSessionT |
| 12 | + |
| 13 | + |
| 14 | +class Authorizer: |
| 15 | + __metaclass__ = abc.ABCMeta |
| 16 | + |
| 17 | + @abc.abstractmethod |
| 18 | + def permit_get_tool(self, name: str) -> bool: |
| 19 | + """Check if the specified tool can be retrieved from the associated mcp server""" |
| 20 | + return False |
| 21 | + |
| 22 | + @abc.abstractmethod |
| 23 | + def permit_list_tool(self, name: str) -> bool: |
| 24 | + """Check if the specified tool can be listed from the associated mcp server""" |
| 25 | + return False |
| 26 | + |
| 27 | + @abc.abstractmethod |
| 28 | + def permit_call_tool( |
| 29 | + self, |
| 30 | + name: str, |
| 31 | + arguments: dict[str, Any], |
| 32 | + context: Context[ServerSessionT, LifespanContextT, RequestT] | None = None, |
| 33 | + ) -> bool: |
| 34 | + """Check if the specified tool can be called from the associated mcp server""" |
| 35 | + return False |
| 36 | + |
| 37 | + @abc.abstractmethod |
| 38 | + def permit_get_resource(self, resource: AnyUrl | str) -> bool: |
| 39 | + """Check if the specified resource can be retrieved from the associated mcp server""" |
| 40 | + return False |
| 41 | + |
| 42 | + @abc.abstractmethod |
| 43 | + def permit_create_resource(self, uri: str, params: dict[str, Any]) -> bool: |
| 44 | + """Check if the specified resource can be created on the associated mcp server""" |
| 45 | + return False |
| 46 | + |
| 47 | + @abc.abstractmethod |
| 48 | + def permit_list_resource(self, resource: AnyUrl | str) -> bool: |
| 49 | + """Check if the specified resource can be listed from the associated mcp server""" |
| 50 | + return False |
| 51 | + |
| 52 | + @abc.abstractmethod |
| 53 | + def permit_list_template(self, resource: AnyUrl | str) -> bool: |
| 54 | + """Check if the specified template can be listed from the associated mcp server""" |
| 55 | + return False |
| 56 | + |
| 57 | + @abc.abstractmethod |
| 58 | + def permit_get_prompt(self, name: str) -> bool: |
| 59 | + """Check if the specified prompt can be retrieved from the associated mcp server""" |
| 60 | + return False |
| 61 | + |
| 62 | + @abc.abstractmethod |
| 63 | + def permit_list_prompt(self, name: str) -> bool: |
| 64 | + """Check if the specified prompt can be listed from the associated mcp server""" |
| 65 | + return False |
| 66 | + |
| 67 | + @abc.abstractmethod |
| 68 | + def permit_render_prompt(self, name: str, arguments: dict[str, Any] | None = None) -> bool: |
| 69 | + """Check if the specified prompt can be rendered from the associated mcp server""" |
| 70 | + return False |
| 71 | + |
| 72 | +class AllAllAuthorizer(Authorizer): |
| 73 | + def permit_get_tool(self, name: str) -> bool: |
| 74 | + return True |
| 75 | + |
| 76 | + def permit_list_tool(self, name: str) -> bool: |
| 77 | + return True |
| 78 | + |
| 79 | + def permit_call_tool( |
| 80 | + self, |
| 81 | + name: str, |
| 82 | + arguments: dict[str, Any], |
| 83 | + context: Context[ServerSessionT, LifespanContextT, RequestT] | None = None, |
| 84 | + ) -> bool: |
| 85 | + return True |
| 86 | + |
| 87 | + def permit_get_resource(self, resource: AnyUrl | str) -> bool: |
| 88 | + return True |
| 89 | + |
| 90 | + def permit_create_resource(self, uri: str, params: dict[str, Any]) -> bool: |
| 91 | + return True |
| 92 | + |
| 93 | + def permit_list_resource(self, resource: AnyUrl | str) -> bool: |
| 94 | + return True |
| 95 | + |
| 96 | + def permit_list_template(self, resource: AnyUrl | str) -> bool: |
| 97 | + return True |
| 98 | + |
| 99 | + def permit_get_prompt(self, name: str) -> bool: |
| 100 | + return True |
| 101 | + |
| 102 | + def permit_list_prompt(self, name: str) -> bool: |
| 103 | + return True |
| 104 | + |
| 105 | + def permit_render_prompt(self, name: str, arguments: dict[str, Any] | None = None) -> bool: |
| 106 | + return True |
| 107 | + |
0 commit comments