|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Base plugin implementation. |
| 3 | +
|
| 4 | +Copyright 2025 |
| 5 | +SPDX-License-Identifier: Apache-2.0 |
| 6 | +Authors: Teryl Taylor |
| 7 | +
|
| 8 | +This module implements the base plugin object. |
| 9 | +It supports pre and post hooks AI safety, security and business processing |
| 10 | +for the following locations in the server: |
| 11 | +server_pre_register / server_post_register - for virtual server verification |
| 12 | +tool_pre_invoke / tool_post_invoke - for guardrails |
| 13 | +prompt_pre_fetch / prompt_post_fetch - for prompt filtering |
| 14 | +resource_pre_fetch / resource_post_fetch - for content filtering |
| 15 | +auth_pre_check / auth_post_check - for custom auth logic |
| 16 | +federation_pre_sync / federation_post_sync - for gateway federation |
| 17 | +""" |
| 18 | + |
| 19 | +# Standard |
| 20 | +import uuid |
| 21 | + |
| 22 | +# First-Party |
| 23 | +from mcpgateway.plugins.framework.models import HookType, PluginCondition, PluginConfig, PluginMode |
| 24 | +from mcpgateway.plugins.framework.plugin_types import ( |
| 25 | + PluginContext, |
| 26 | + PromptPosthookPayload, |
| 27 | + PromptPosthookResult, |
| 28 | + PromptPrehookPayload, |
| 29 | + PromptPrehookResult, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +class Plugin: |
| 34 | + """Base plugin object for pre/post processing of inputs and outputs at various locations throughout the server.""" |
| 35 | + |
| 36 | + def __init__(self, config: PluginConfig) -> None: |
| 37 | + """Initialize a plugin with a configuration and context. |
| 38 | +
|
| 39 | + Args: |
| 40 | + config: The plugin configuration |
| 41 | + """ |
| 42 | + self._config = config |
| 43 | + |
| 44 | + @property |
| 45 | + def priority(self) -> int: |
| 46 | + """Return the plugin's priority. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + Plugin's priority. |
| 50 | + """ |
| 51 | + return self._config.priority |
| 52 | + |
| 53 | + @property |
| 54 | + def mode(self) -> PluginMode: |
| 55 | + """Return the plugin's mode. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + Plugin's mode. |
| 59 | + """ |
| 60 | + return self._config.mode |
| 61 | + |
| 62 | + @property |
| 63 | + def name(self) -> str: |
| 64 | + """Return the plugin's name. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + Plugin's name. |
| 68 | + """ |
| 69 | + return self._config.name |
| 70 | + |
| 71 | + @property |
| 72 | + def hooks(self) -> list[HookType]: |
| 73 | + """Return the plugin's currently configured hooks. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + Plugin's configured hooks. |
| 77 | + """ |
| 78 | + return self._config.hooks |
| 79 | + |
| 80 | + @property |
| 81 | + def tags(self) -> list[str]: |
| 82 | + """Return the plugin's tags. |
| 83 | +
|
| 84 | + Returns: |
| 85 | + Plugin's tags. |
| 86 | + """ |
| 87 | + return self._config.tags |
| 88 | + |
| 89 | + @property |
| 90 | + def conditions(self) -> list[PluginCondition] | None: |
| 91 | + """Return the plugin's conditions for operation. |
| 92 | +
|
| 93 | + Returns: |
| 94 | + Plugin's conditions for executing. |
| 95 | + """ |
| 96 | + return self._config.conditions |
| 97 | + |
| 98 | + async def prompt_pre_fetch(self, payload: PromptPrehookPayload, context: PluginContext) -> PromptPrehookResult: |
| 99 | + """Plugin hook run before a prompt is retrieved and rendered. |
| 100 | +
|
| 101 | + Args: |
| 102 | + payload: The prompt payload to be analyzed. |
| 103 | + context: contextual information about the hook call. Including why it was called. |
| 104 | +
|
| 105 | + Raises: |
| 106 | + NotImplementedError: needs to be implemented by sub class. |
| 107 | + """ |
| 108 | + raise NotImplementedError( |
| 109 | + f"""'prompt_pre_fetch' not implemented for plugin {self._config.name} |
| 110 | + of plugin type {type(self)} |
| 111 | + """ |
| 112 | + ) |
| 113 | + |
| 114 | + async def prompt_post_fetch(self, payload: PromptPosthookPayload, context: PluginContext) -> PromptPosthookResult: |
| 115 | + """Plugin hook run after a prompt is rendered. |
| 116 | +
|
| 117 | + Args: |
| 118 | + payload: The prompt payload to be analyzed. |
| 119 | + context: Contextual information about the hook call. |
| 120 | +
|
| 121 | + Raises: |
| 122 | + NotImplementedError: needs to be implemented by sub class. |
| 123 | + """ |
| 124 | + raise NotImplementedError( |
| 125 | + f"""'prompt_post_fetch' not implemented for plugin {self._config.name} |
| 126 | + of plugin type {type(self)} |
| 127 | + """ |
| 128 | + ) |
| 129 | + |
| 130 | + async def shutdown(self) -> None: |
| 131 | + """Plugin cleanup code.""" |
| 132 | + |
| 133 | + |
| 134 | +class PluginRef: |
| 135 | + """Plugin reference which contains a uuid.""" |
| 136 | + |
| 137 | + def __init__(self, plugin: Plugin): |
| 138 | + """Initialize a plugin reference. |
| 139 | +
|
| 140 | + Args: |
| 141 | + plugin: The plugin to reference. |
| 142 | + """ |
| 143 | + self._plugin = plugin |
| 144 | + self._uuid = uuid.uuid4() |
| 145 | + |
| 146 | + @property |
| 147 | + def plugin(self) -> Plugin: |
| 148 | + """Return the underlying plugin. |
| 149 | +
|
| 150 | + Returns: |
| 151 | + The underlying plugin. |
| 152 | + """ |
| 153 | + return self._plugin |
| 154 | + |
| 155 | + @property |
| 156 | + def uuid(self) -> str: |
| 157 | + """Return the plugin's UUID. |
| 158 | +
|
| 159 | + Returns: |
| 160 | + Plugin's UUID. |
| 161 | + """ |
| 162 | + return self._uuid.hex |
| 163 | + |
| 164 | + @property |
| 165 | + def priority(self) -> int: |
| 166 | + """Returns the plugin's priority. |
| 167 | +
|
| 168 | + Returns: |
| 169 | + Plugin's priority. |
| 170 | + """ |
| 171 | + return self._plugin.priority |
| 172 | + |
| 173 | + @property |
| 174 | + def name(self) -> str: |
| 175 | + """Return the plugin's name. |
| 176 | +
|
| 177 | + Returns: |
| 178 | + Plugin's name. |
| 179 | + """ |
| 180 | + return self._plugin.name |
| 181 | + |
| 182 | + @property |
| 183 | + def hooks(self) -> list[HookType]: |
| 184 | + """Returns the plugin's currently configured hooks. |
| 185 | +
|
| 186 | + Returns: |
| 187 | + Plugin's configured hooks. |
| 188 | + """ |
| 189 | + return self._plugin.hooks |
| 190 | + |
| 191 | + @property |
| 192 | + def tags(self) -> list[str]: |
| 193 | + """Return the plugin's tags. |
| 194 | +
|
| 195 | + Returns: |
| 196 | + Plugin's tags. |
| 197 | + """ |
| 198 | + return self._plugin.tags |
| 199 | + |
| 200 | + @property |
| 201 | + def conditions(self) -> list[PluginCondition] | None: |
| 202 | + """Return the plugin's conditions for operation. |
| 203 | +
|
| 204 | + Returns: |
| 205 | + Plugin's conditions for operation. |
| 206 | + """ |
| 207 | + return self._plugin.conditions |
| 208 | + |
| 209 | + @property |
| 210 | + def mode(self) -> PluginMode: |
| 211 | + """Return the plugin's mode. |
| 212 | +
|
| 213 | + Returns: |
| 214 | + Plugin's mode. |
| 215 | + """ |
| 216 | + return self.plugin.mode |
0 commit comments