mirror of
https://github.com/mattermost/mattermost.git
synced 2026-02-03 20:40:00 -05:00
Add AI development guidance for Mattermost contributors working on the Python plugin gRPC infrastructure. Includes directory structure, key components, proto organization, common patterns, and instructions for adding new API methods and hooks. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
5.1 KiB
5.1 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with the Mattermost Python plugin gRPC infrastructure.
Repository Purpose
This directory contains the Go-side gRPC infrastructure enabling Python plugins to communicate with the Mattermost server. It provides:
- Protocol buffer definitions for Plugin API and hooks
- gRPC server implementation for API callbacks
- Type conversion between Go models and protobuf
- ServeHTTP bidirectional streaming
Key Commands
Code Generation
# From server/ directory:
# Generate Go code from proto files
make -C public proto-gen
# Generate both Go and Python code
make -C public proto-gen-all
Testing
# Run gRPC server tests
go test ./public/pluginapi/grpc/server/... -v
# Run Python supervisor tests
go test ./public/plugin/... -v -run Python
# Run integration tests
go test ./public/pluginapi/grpc/server/... -v -run Integration
Architecture Overview
Directory Structure
server/public/pluginapi/grpc/
├── proto/ # Protocol buffer definitions
│ ├── common.proto # Shared types (AppError, StringMap)
│ ├── user.proto # User type
│ ├── channel.proto # Channel type
│ ├── post.proto # Post type
│ ├── team.proto # Team type
│ ├── file.proto # File types
│ ├── api.proto # PluginAPI service definition
│ ├── api_*.proto # API method groups
│ ├── hooks.proto # PluginHooks service definition
│ └── hooks_*.proto # Hook method groups
├── generated/go/pluginapiv1/ # Generated Go code (do not edit)
└── server/ # gRPC server implementation
├── api_server.go # Server startup and registration
├── handlers_*.go # RPC method handlers
├── convert_*.go # Go model <-> proto conversion
├── serve_http.go # ServeHTTP streaming handler
└── errors.go # Error conversion utilities
Key Components
-
Python Supervisor (
plugin/python_supervisor.go)- Spawns Python plugin subprocess
- Sets up go-plugin gRPC connection
- Passes API server address via env var
-
Hooks gRPC Client (
plugin/hooks_grpc_client.go)- Implements
plugin.Hooksinterface - Dispatches hooks to Python via gRPC
- Converts Go types to proto messages
- Implements
-
API Server (
grpc/server/api_server.go)- gRPC server Python plugins connect to
- Implements PluginAPI service
- Calls actual
plugin.APImethods
-
ServeHTTP (
grpc/server/serve_http.go)- Bidirectional streaming for HTTP
- 64KB chunks for request/response bodies
- Handles flush signals
Proto File Organization
| File | Purpose |
|---|---|
api.proto |
PluginAPI service (all RPC definitions) |
api_user_team.proto |
User and Team API messages |
api_channel_post.proto |
Channel and Post API messages |
api_kv_config.proto |
KV Store and Config messages |
api_file_bot.proto |
File and Bot messages |
api_remaining.proto |
All other API messages |
hooks.proto |
PluginHooks service definition |
hooks_*.proto |
Hook messages by category |
Best Practices
- Proto Changes: Always regenerate Go AND Python code after proto changes
- Type Conversion: Add converters in
convert_*.gofor new types - Error Handling: Use
ToAppErrorProto/FromAppErrorProtofor errors - Testing: Add tests in
*_test.gofor new handlers - JSON Blobs: Use
bytes *_jsonfor complex types (Config, License)
Adding a New API Method
- Add RPC to
api.protoservice definition - Add request/response messages to appropriate
api_*.proto - Run
make -C public proto-gen-all - Add handler method to appropriate
handlers_*.go - Add conversion functions to
convert_*.goif new types - Add test in
handlers_test.go - Update Python SDK client (see python-sdk/CLAUDE.md)
Adding a New Hook
- Add RPC to
hooks.protoservice definition - Add request/response messages to appropriate
hooks_*.proto - Run
make -C public proto-gen-all - Add handler in
plugin/hooks_grpc_client.go - Update Python SDK hooks (see python-sdk/CLAUDE.md)
Common Patterns
Response-Embedded Errors
All RPC responses include optional AppError:
message GetUserResponse {
User user = 1;
AppError error = 2;
}
Handlers check for errors from API calls and populate the error field:
user, appErr := s.api.GetUser(req.UserId)
if appErr != nil {
return &pb.GetUserResponse{Error: ToAppErrorProto(appErr)}, nil
}
return &pb.GetUserResponse{User: ToUserProto(user)}, nil
ServeHTTP Streaming
ServeHTTP uses bidirectional streaming with message types:
HTTPRequestInit- Initial request metadataHTTPRequestBody- Request body chunksHTTPResponseInit- Response status and headersHTTPResponseBody- Response body chunks
See serve_http.go and serve_http_test.go for implementation details.