A Python SDK for building plugins for the mcpd plugin system. This SDK provides a simple, async-first API for creating gRPC-based plugins that can intercept and transform HTTP requests and responses.
- Simple API: Extend
BasePluginand override only the methods you need - Async/Await: Full async support using Python's asyncio
- Type Hints: Complete type annotations for better IDE support
- gRPC-based: Built on grpcio with protocol buffers
- Minimal Dependencies: Only requires
grpcioandprotobuf - Comprehensive Examples: Five example plugins demonstrating common patterns
# Using uv (recommended)
uv add mcpd-plugins
# Using pip
pip install mcpd-pluginsHere's a minimal plugin that adds a custom header to HTTP requests:
import asyncio
import sys
from mcpd_plugins import BasePlugin, serve
from mcpd_plugins.v1.plugins.plugin_pb2 import (
FLOW_REQUEST,
Capabilities,
HTTPRequest,
HTTPResponse,
Metadata,
)
from google.protobuf.empty_pb2 import Empty
class MyPlugin(BasePlugin):
async def GetMetadata(self, request: Empty, context) -> Metadata:
return Metadata(
name="my-plugin",
version="1.0.0",
description="Adds a custom header to requests"
)
async def GetCapabilities(self, request: Empty, context) -> Capabilities:
return Capabilities(flows=[FLOW_REQUEST])
async def HandleRequest(self, request: HTTPRequest, context) -> HTTPResponse:
response = HTTPResponse(**{"continue": True})
response.modified_request.CopyFrom(request)
response.modified_request.headers["X-My-Plugin"] = "processed"
return response
if __name__ == "__main__":
# Pass sys.argv for mcpd compatibility (handles --address and --network flags)
asyncio.run(serve(MyPlugin(), sys.argv))Run your plugin:
# For mcpd (with --address and --network arguments)
python my_plugin.py --address /tmp/my-plugin.sock --network unix
# For standalone testing (defaults to TCP port 50051)
python my_plugin.pyWhen running under mcpd, the --address and --network flags are required and automatically passed by mcpd. For standalone testing without arguments, the plugin defaults to TCP on port 50051.
The BasePlugin class provides default implementations for all plugin methods:
Configure()- Initialize plugin with configurationStop()- Clean up resources on shutdownGetMetadata()- Return plugin name, version, and descriptionGetCapabilities()- Declare which flows the plugin supportsCheckHealth()- Health check endpointCheckReady()- Readiness check endpointHandleRequest()- Process incoming HTTP requestsHandleResponse()- Process outgoing HTTP responses
Override only the methods your plugin needs.
Plugins can process two types of flows:
- FLOW_REQUEST: Intercept and modify incoming HTTP requests
- FLOW_RESPONSE: Intercept and modify outgoing HTTP responses
Declare your supported flows in GetCapabilities().
When handling requests or responses, you can:
- Pass through unchanged: Return
HTTPResponse(**{"continue": True}) - Modify and continue: Set
**{"continue": True}and modify fields - Reject: Set
**{"continue": False}with a status code and body
The SDK includes five example plugins demonstrating common patterns:
Adds a custom header to all requests.
cd examples/simple_plugin
uv run python main.pyValidates Bearer token authentication and rejects unauthorized requests.
export AUTH_TOKEN="your-secret-token"
cd examples/auth_plugin
uv run python main.pyLogs HTTP request and response details for observability.
cd examples/logging_plugin
uv run python main.pyImplements token bucket rate limiting per client IP.
cd examples/rate_limit_plugin
uv run python main.pyTransforms JSON request bodies by adding metadata fields.
cd examples/transform_plugin
uv run python main.py# Clone the repository
git clone https://github.com/mozilla-ai/mcpd-plugins-sdk-python.git
cd mcpd-plugins-sdk-python
# Setup development environment
make setup# Run all tests
make test
# Run with verbose output
uv run pytest -v
# Run specific test file
uv run pytest tests/unit/test_base_plugin.py# Run all pre-commit hooks
make lint
# Run ruff directly
uv run ruff check src/ tests/
uv run ruff format src/ tests/The proto files are automatically generated from the mcpd-proto repository and committed to this repo. To regenerate:
make generate-protosclass BasePlugin(PluginServicer):
async def Configure(self, request: PluginConfig, context) -> Empty
async def Stop(self, request: Empty, context) -> Empty
async def GetMetadata(self, request: Empty, context) -> Metadata
async def GetCapabilities(self, request: Empty, context) -> Capabilities
async def CheckHealth(self, request: Empty, context) -> Empty
async def CheckReady(self, request: Empty, context) -> Empty
async def HandleRequest(self, request: HTTPRequest, context) -> HTTPResponse
async def HandleResponse(self, response: HTTPResponse, context) -> HTTPResponseasync def serve(
plugin: BasePlugin,
args: Optional[list[str]] = None, # Command-line arguments (typically sys.argv)
grace_period: float = 5.0,
) -> NoneParameters:
plugin: The plugin instance to serveargs: Command-line arguments. When provided (e.g.,sys.argv), enables mcpd compatibility by parsing--addressand--networkflags. WhenNone, runs in standalone mode on TCP port 50051.grace_period: Seconds to wait during graceful shutdown
Command-line flags (when args is provided):
--address: gRPC address (socket path for unix, host:port for tcp) - required--network: Network type (unixortcp) - defaults tounix
PluginError- Base exception for all plugin errorsConfigurationError- Configuration-related errorsServerError- Server startup/shutdown errors
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
For security issues, please see SECURITY.md.
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- mcpd - The mcpd daemon
- mcpd-proto - Protocol buffer definitions
- mcpd-plugins-sdk-go - Go SDK for plugins
- mcpd-plugins-sdk-dotnet - .NET SDK for plugins
- mcpd-sdk-python - Python SDK for mcpd clients