|
| 1 | + |
1 | 2 | # Plugin Framework Specification |
2 | 3 |
|
3 | 4 | **Version**: 1.0 |
@@ -1010,6 +1011,93 @@ class PluginCondition(BaseModel): |
1010 | 1011 | content_types: Optional[list[str]] = None # Execute for specific content types |
1011 | 1012 | ``` |
1012 | 1013 |
|
| 1014 | +#### Content Type Filtering |
| 1015 | + |
| 1016 | +Plugins can be configured to execute only for specific content types using the `content_types` condition. This enables fine-grained control over when plugins process requests based on the HTTP `Content-Type` header. |
| 1017 | + |
| 1018 | +**Configuration Example:** |
| 1019 | + |
| 1020 | +```yaml |
| 1021 | +plugins: |
| 1022 | + - name: "JsonValidator" |
| 1023 | + kind: "plugins.json_validator.JsonValidator" |
| 1024 | + hooks: ["tool_pre_invoke"] |
| 1025 | + conditions: |
| 1026 | + - content_types: ["application/json"] |
| 1027 | +``` |
| 1028 | +
|
| 1029 | +**Matching Behavior:** |
| 1030 | +
|
| 1031 | +- **Case-insensitive**: `APPLICATION/JSON` matches `application/json` |
| 1032 | +- **Parameter stripping**: `application/json; charset=utf-8` matches `application/json` |
| 1033 | +- **Multiple types**: Supports OR logic - plugin executes if any content type matches |
| 1034 | +- **Permissive default**: If `content_types` is not specified or request has no Content-Type header, plugin executes normally |
| 1035 | + |
| 1036 | +**Common Content Types:** |
| 1037 | + |
| 1038 | +| Content Type | Description | Use Case | |
| 1039 | +|--------------|-------------|----------| |
| 1040 | +| `application/json` | JSON data | API requests, structured data validation | |
| 1041 | +| `text/plain` | Plain text | Simple text processing, logging | |
| 1042 | +| `text/html` | HTML documents | Web scraping, content extraction | |
| 1043 | +| `application/xml` | XML data | Legacy API integration, SOAP services | |
| 1044 | +| `multipart/form-data` | File uploads | File validation, virus scanning | |
| 1045 | +| `application/x-www-form-urlencoded` | Form submissions | Form data validation | |
| 1046 | + |
| 1047 | +**Example: JSON-Only Security Plugin** |
| 1048 | + |
| 1049 | +```yaml |
| 1050 | +plugins: |
| 1051 | + - name: "JsonSecurityScanner" |
| 1052 | + kind: "plugins.security.json_scanner.JsonSecurityScanner" |
| 1053 | + hooks: ["tool_pre_invoke", "tool_post_invoke"] |
| 1054 | + mode: "enforce" |
| 1055 | + priority: 10 |
| 1056 | + conditions: |
| 1057 | + - content_types: ["application/json"] |
| 1058 | + server_ids: ["production-api"] |
| 1059 | +``` |
| 1060 | + |
| 1061 | +**Example: Multi-Format Data Validator** |
| 1062 | + |
| 1063 | +```yaml |
| 1064 | +plugins: |
| 1065 | + - name: "DataValidator" |
| 1066 | + kind: "plugins.validation.data_validator.DataValidator" |
| 1067 | + hooks: ["tool_pre_invoke"] |
| 1068 | + conditions: |
| 1069 | + - content_types: |
| 1070 | + - "application/json" |
| 1071 | + - "application/xml" |
| 1072 | + - "text/csv" |
| 1073 | +``` |
| 1074 | + |
| 1075 | +**Combined Conditions:** |
| 1076 | + |
| 1077 | +Content type filtering works seamlessly with other conditions: |
| 1078 | + |
| 1079 | +```yaml |
| 1080 | +plugins: |
| 1081 | + - name: "TeamJsonProcessor" |
| 1082 | + kind: "plugins.processors.json_processor.JsonProcessor" |
| 1083 | + hooks: ["tool_pre_invoke"] |
| 1084 | + conditions: |
| 1085 | + - content_types: ["application/json"] |
| 1086 | + tenant_ids: ["team-alpha", "team-beta"] |
| 1087 | + tools: ["data_analysis", "report_generation"] |
| 1088 | +``` |
| 1089 | + |
| 1090 | +**Security Considerations:** |
| 1091 | + |
| 1092 | +!!! warning "Content-Type Spoofing" |
| 1093 | + Clients can set arbitrary Content-Type headers. Use `content_types` for filtering and optimization, not as a security boundary. Combine with other conditions (server_ids, tenant_ids) for defense-in-depth. |
| 1094 | + |
| 1095 | +**Performance Benefits:** |
| 1096 | + |
| 1097 | +- **Reduced overhead**: Skip expensive processing for irrelevant content types |
| 1098 | +- **Targeted validation**: Apply format-specific validators only when needed |
| 1099 | +- **Resource optimization**: Prevent unnecessary plugin execution |
| 1100 | + |
1013 | 1101 | ## Hook Reference Documentation |
1014 | 1102 |
|
1015 | 1103 | The plugin framework provides two main categories of hooks, each documented in detail in separate files: |
|
0 commit comments