Skip to main content
Glama
2025-11-28-middleware-implementation.md6.16 kB
# Middleware Implementation Session **Date:** 2025-11-28 **Project:** scout_mcp **Duration:** Full session ## Session Overview Implemented a complete middleware stack for the Scout MCP server following FastMCP middleware patterns. The implementation includes logging, error handling, and timing middleware with comprehensive test coverage (23 tests) using strict TDD methodology. ## Timeline 1. **Research Phase** - Queried FastMCP middleware documentation via Pulse MCP 2. **Planning Phase** - Created detailed implementation plan at `docs/plans/2025-11-28-middleware-implementation.md` 3. **Task 1** - Created middleware package structure (`base.py`, `__init__.py`) 4. **Task 2** - Implemented `TimingMiddleware` for request duration tracking 5. **Task 3** - Implemented `LoggingMiddleware` for request/response logging 6. **Task 4** - Implemented `ErrorHandlingMiddleware` with stats tracking 7. **Task 5** - Added `DetailedTimingMiddleware` with per-operation stats 8. **Task 6** - Integrated middleware into `server.py` 9. **Task 7** - Verified full test suite and type checking ## Key Findings ### FastMCP Middleware Architecture - Middleware follows pipeline model (first added = innermost) - Hooks available: `on_message`, `on_request`, `on_call_tool`, `on_read_resource`, etc. - Base class `Middleware` from `fastmcp.server.middleware` - Context object provides `method`, `source`, `message` attributes ### Implementation Patterns - All middleware extend `ScoutMiddleware` base class (`scout_mcp/middleware/base.py:11`) - Error middleware uses `defaultdict(int)` for stats (`scout_mcp/middleware/errors.py:43`) - Timing uses `time.perf_counter()` for high-precision measurement (`scout_mcp/middleware/timing.py:49`) - Payload truncation uses JSON serialization with fallback (`scout_mcp/middleware/logging.py:47-53`) ## Technical Decisions ### Middleware Stack Order ``` ErrorHandling (innermost) → Timing → Logging (outermost) ``` - **Rationale:** Error handling wraps everything to catch all exceptions. Timing measures actual execution. Logging at outermost layer captures full request/response cycle. ### Environment Variable Configuration - `SCOUT_LOG_PAYLOADS` - Enable payload logging (default: false) - `SCOUT_SLOW_THRESHOLD_MS` - Slow request threshold (default: 1000ms) - `SCOUT_INCLUDE_TRACEBACK` - Include tracebacks in error logs (default: false) - **Rationale:** Runtime configuration without code changes, suitable for different environments. ### DetailedTimingMiddleware Stats Structure ```python TimingStats(count, total_ms, min_ms, max_ms, avg_ms) ``` - **Rationale:** Provides comprehensive metrics for performance analysis without external dependencies. ## Files Modified ### Created | File | Purpose | |------|---------| | `scout_mcp/middleware/__init__.py` | Package exports | | `scout_mcp/middleware/base.py` | `ScoutMiddleware` base class | | `scout_mcp/middleware/timing.py` | `TimingMiddleware`, `DetailedTimingMiddleware`, `TimingStats` | | `scout_mcp/middleware/logging.py` | `LoggingMiddleware` with truncation | | `scout_mcp/middleware/errors.py` | `ErrorHandlingMiddleware` with stats | | `tests/test_middleware/__init__.py` | Test package marker | | `tests/test_middleware/test_base.py` | Base class tests (2 tests) | | `tests/test_middleware/test_timing.py` | Timing middleware tests (7 tests) | | `tests/test_middleware/test_logging.py` | Logging middleware tests (5 tests) | | `tests/test_middleware/test_errors.py` | Error handling tests (6 tests) | | `tests/test_middleware/test_integration.py` | Server integration tests (3 tests) | | `docs/plans/2025-11-28-middleware-implementation.md` | Implementation plan | ### Modified | File | Changes | |------|---------| | `scout_mcp/server.py:7-41` | Added `configure_middleware()` function and middleware imports | ## Commands Executed ```bash # Test execution (TDD cycles) .venv/bin/python -m pytest tests/test_middleware/test_base.py -v .venv/bin/python -m pytest tests/test_middleware/test_timing.py -v .venv/bin/python -m pytest tests/test_middleware/test_logging.py -v .venv/bin/python -m pytest tests/test_middleware/test_errors.py -v .venv/bin/python -m pytest tests/test_middleware/test_integration.py -v # Full test suite .venv/bin/python -m pytest tests/test_middleware/ -v # 23 passed # Type checking .venv/bin/python -m mypy scout_mcp/middleware/ --strict # Success: no issues # Linting .venv/bin/python -m ruff check scout_mcp/middleware/ tests/test_middleware/ # All checks passed ``` ## Commits ``` 0ca99a9 feat(middleware): integrate middleware stack into server ccd0dee feat(middleware): add DetailedTimingMiddleware with per-operation stats 8df1700 feat(middleware): add ErrorHandlingMiddleware with stats tracking 5a1c817 feat(middleware): add LoggingMiddleware for request/response logging c634ab8 feat(middleware): add TimingMiddleware for request duration tracking ec4a579 chore(middleware): fix linting issues in base module 4832729 feat(middleware): add base middleware package structure ``` ## Test Coverage | Module | Tests | Status | |--------|-------|--------| | base.py | 2 | PASS | | timing.py | 7 | PASS | | logging.py | 5 | PASS | | errors.py | 6 | PASS | | integration | 3 | PASS | | **Total** | **23** | **PASS** | ## Next Steps 1. Consider adding `RateLimitingMiddleware` if needed 2. Consider adding `CachingMiddleware` for frequently accessed resources 3. Add metrics export (Prometheus, StatsD) if observability is needed 4. Monitor slow request thresholds in production and adjust ## Usage Examples ```python from scout_mcp.middleware import ( ErrorHandlingMiddleware, LoggingMiddleware, TimingMiddleware, DetailedTimingMiddleware, ) # Default configuration (already in server.py) configure_middleware(mcp) # Access statistics at runtime error_mw = next(m for m in mcp.middleware if isinstance(m, ErrorHandlingMiddleware)) print(error_mw.get_error_stats()) # {"ValueError": 3, "TimeoutError": 1} timing_mw = next(m for m in mcp.middleware if isinstance(m, DetailedTimingMiddleware)) print(timing_mw.get_timing_stats()) # {"tool:scout": {"count": 10, "avg_ms": 45.2}} ```

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jmagar/scout_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server