|
| 1 | +"""BasePlugin class providing default implementations for all plugin methods.""" |
| 2 | + |
| 3 | +from google.protobuf.empty_pb2 import Empty |
| 4 | +from grpc import ServicerContext |
| 5 | + |
| 6 | +from mcpd_plugins.v1.plugins.plugin_pb2 import ( |
| 7 | + Capabilities, |
| 8 | + HTTPRequest, |
| 9 | + HTTPResponse, |
| 10 | + Metadata, |
| 11 | + PluginConfig, |
| 12 | +) |
| 13 | +from mcpd_plugins.v1.plugins.plugin_pb2_grpc import PluginServicer |
| 14 | + |
| 15 | + |
| 16 | +class BasePlugin(PluginServicer): |
| 17 | + """Base class for mcpd plugins with sensible default implementations. |
| 18 | +
|
| 19 | + Developers should extend this class and override only the methods they need. |
| 20 | + All methods are async (using async/await pattern) to support asynchronous operations. |
| 21 | +
|
| 22 | + Example: |
| 23 | + ```python |
| 24 | + class MyPlugin(BasePlugin): |
| 25 | + async def GetMetadata(self, request: Empty, context: ServicerContext) -> Metadata: |
| 26 | + return Metadata( |
| 27 | + name="my-plugin", |
| 28 | + version="1.0.0", |
| 29 | + description="My custom plugin" |
| 30 | + ) |
| 31 | +
|
| 32 | + async def HandleRequest(self, request: HTTPRequest, context: ServicerContext) -> HTTPResponse: |
| 33 | + # Process the request |
| 34 | + response = HTTPResponse(**{"continue": True}) |
| 35 | + response.headers["X-My-Plugin"] = "processed" |
| 36 | + return response |
| 37 | + ``` |
| 38 | + """ |
| 39 | + |
| 40 | + async def Configure(self, request: PluginConfig, context: ServicerContext) -> Empty: |
| 41 | + """Configure the plugin with the provided settings. |
| 42 | +
|
| 43 | + Default implementation does nothing. Override to handle configuration. |
| 44 | +
|
| 45 | + Args: |
| 46 | + request: Configuration settings from the host. |
| 47 | + context: gRPC context for the request. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + Empty message indicating successful configuration. |
| 51 | + """ |
| 52 | + return Empty() |
| 53 | + |
| 54 | + async def Stop(self, request: Empty, context: ServicerContext) -> Empty: |
| 55 | + """Stop the plugin and clean up resources. |
| 56 | +
|
| 57 | + Default implementation does nothing. Override to handle cleanup. |
| 58 | +
|
| 59 | + Args: |
| 60 | + request: Empty request message. |
| 61 | + context: gRPC context for the request. |
| 62 | +
|
| 63 | + Returns: |
| 64 | + Empty message indicating successful shutdown. |
| 65 | + """ |
| 66 | + return Empty() |
| 67 | + |
| 68 | + async def GetMetadata(self, request: Empty, context: ServicerContext) -> Metadata: |
| 69 | + """Get plugin metadata (name, version, description). |
| 70 | +
|
| 71 | + Default implementation returns basic metadata. Override to provide actual values. |
| 72 | +
|
| 73 | + Args: |
| 74 | + request: Empty request message. |
| 75 | + context: gRPC context for the request. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + Metadata containing plugin information. |
| 79 | + """ |
| 80 | + return Metadata( |
| 81 | + name="base-plugin", |
| 82 | + version="0.0.0", |
| 83 | + description="Base plugin implementation", |
| 84 | + ) |
| 85 | + |
| 86 | + async def GetCapabilities(self, request: Empty, context: ServicerContext) -> Capabilities: |
| 87 | + """Get plugin capabilities (supported flows). |
| 88 | +
|
| 89 | + Default implementation returns no capabilities. Must override to declare supported flows. |
| 90 | +
|
| 91 | + Args: |
| 92 | + request: Empty request message. |
| 93 | + context: gRPC context for the request. |
| 94 | +
|
| 95 | + Returns: |
| 96 | + Capabilities message listing supported flows. |
| 97 | + """ |
| 98 | + return Capabilities() |
| 99 | + |
| 100 | + async def CheckHealth(self, request: Empty, context: ServicerContext) -> Empty: |
| 101 | + """Health check endpoint. |
| 102 | +
|
| 103 | + Default implementation returns healthy status. Override if custom health checks needed. |
| 104 | +
|
| 105 | + Args: |
| 106 | + request: Empty request message. |
| 107 | + context: gRPC context for the request. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + Empty message indicating healthy status. |
| 111 | + """ |
| 112 | + return Empty() |
| 113 | + |
| 114 | + async def CheckReady(self, request: Empty, context: ServicerContext) -> Empty: |
| 115 | + """Readiness check endpoint. |
| 116 | +
|
| 117 | + Default implementation returns ready status. Override if custom readiness checks needed. |
| 118 | +
|
| 119 | + Args: |
| 120 | + request: Empty request message. |
| 121 | + context: gRPC context for the request. |
| 122 | +
|
| 123 | + Returns: |
| 124 | + Empty message indicating ready status. |
| 125 | + """ |
| 126 | + return Empty() |
| 127 | + |
| 128 | + async def HandleRequest(self, request: HTTPRequest, context: ServicerContext) -> HTTPResponse: |
| 129 | + """Handle incoming HTTP request. |
| 130 | +
|
| 131 | + Default implementation passes through unchanged (continue=True). |
| 132 | +
|
| 133 | + Args: |
| 134 | + request: The incoming HTTP request to process. |
| 135 | + context: gRPC context for the request. |
| 136 | +
|
| 137 | + Returns: |
| 138 | + HTTPResponse indicating how to proceed (continue, modify, or reject). |
| 139 | + """ |
| 140 | + return HTTPResponse(**{"continue": True}) |
| 141 | + |
| 142 | + async def HandleResponse(self, request: HTTPResponse, context: ServicerContext) -> HTTPResponse: |
| 143 | + """Handle outgoing HTTP response. |
| 144 | +
|
| 145 | + Default implementation passes through unchanged (continue=True). |
| 146 | +
|
| 147 | + Args: |
| 148 | + request: The outgoing HTTP response to process. |
| 149 | + context: gRPC context for the request. |
| 150 | +
|
| 151 | + Returns: |
| 152 | + HTTPResponse indicating how to proceed (continue or modify). |
| 153 | + """ |
| 154 | + return HTTPResponse(**{"continue": True}) |
0 commit comments