Skip to content

Conversation

@sundargthb
Copy link
Contributor

@sundargthb sundargthb commented Oct 31, 2025

Description of changes:

Add comprehensive middleware support to BedrockAgentCore SDK, enabling developers to inject cross-cutting concerns (authentication, logging, timing, metrics) into the request pipeline.

WHAT'S NEW:

  • ProcessingContext: Namespaced middleware data with collision prevention
  • AgentContext: Unified context combining immutable request + mutable processing
  • StandardNamespaces: Constants for common middleware patterns (auth, timing, etc.)
  • True immutability: RequestContext uses MappingProxyType via @Property
  • Optional validation: Size limits and pattern checks (disabled by default)
  • Backward compatible: Existing handlers work via property delegation

IMPLEMENTATION DETAILS:

  • Namespace isolation uses Dict[str, Dict[str, Any]] structure
  • MappingProxyType enforced via @Property (Pydantic v2 compatible)
  • ClassVar used for class constants (Pydantic v2 requirement)
  • Supports both namespaced and flat middleware data patterns
  • request.state.processing_data integration in _build_request_context

TESTING:

  • 8 comprehensive test suites covering all features
  • Namespace isolation verified
  • True immutability (MappingProxyType) verified
  • Backward compatibility verified
  • Authentication, timing, metrics patterns tested
┌─────────────────────────────────────────────────────────────┐
│                      AgentContext                           │
│  (What handlers receive as 'context' parameter)             │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌──────────────────────────────────────────────────────┐   │
│  │ RequestContext (Immutable - Client Data)             │   │
│  │ - session_id                                         │   │
│  │ - request_headers (MappingProxyType)                 │   │
│  │ - frozen=True (cannot be modified)                   │   │
│  └──────────────────────────────────────────────────────┘   │
│                                                             │
│  ┌──────────────────────────────────────────────────────┐   │
│  │ ProcessingContext (Mutable - Middleware Data)        │   │
│  │ - middleware_data: {                                 │   │
│  │     'auth': {'user_id': 'alice'},                    │   │
│  │     'timing': {'start': 123.45},                     │   │
│  │     'metrics': {'count': 1}                          │   │
│  │   }                                                  │   │
│  │ - processing_metadata: {...}                         │   │
│  └──────────────────────────────────────────────────────┘   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

…mutability

Add comprehensive middleware support to BedrockAgentCore SDK, enabling
developers to inject cross-cutting concerns (authentication, logging,
timing, metrics) into the request pipeline.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant