[PR #29368] test: add comprehensive unit tests for MCP controller #32388

Open
opened 2026-02-21 20:51:18 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/29368

State: open
Merged: No


Description

This PR adds comprehensive unit tests for the MCP (Model Context Protocol) controller that was previously missing test coverage. The MCP controller handles JSON-RPC formatted requests according to the Model Context Protocol specification.

Changes

MCP Controller Tests

  • Test JSON-RPC request parsing and validation
  • Test server code validation (existence, status)
  • Test request vs notification handling
  • Test user input form extraction and conversion
  • Test error handling for various failure scenarios
  • Test end user management (creation, retrieval)
  • Test app availability validation
  • Test different request types (initialize, tools/list, tools/call, ping)
  • Test notification handling (notifications/initialized)
  • Test invalid request formats and error responses

File: api/tests/unit_tests/controllers/mcp/test_mcp.py
Test Cases: 20 tests covering all major functionality

Test Coverage Details

  1. JSON-RPC Request Handling

    • Valid initialize request processing
    • Valid tools/list request processing
    • Valid tools/call request processing
    • Valid ping request processing
    • Invalid JSON-RPC format handling
    • Request without ID field handling
  2. Server Code Validation

    • Server not found error handling
    • App not found error handling
    • Server inactive status validation
    • Server active status validation
  3. Request vs Notification Handling

    • Valid notification (notifications/initialized) processing
    • Invalid notification method rejection
    • HTTP 202 Accepted response for notifications
    • Request ID requirement validation
  4. User Input Form Integration

    • Form extraction for workflow mode apps
    • Form extraction for chat mode apps
    • Form conversion to VariableEntity objects
    • Invalid form structure validation
  5. App Availability Validation

    • Workflow app without workflow config error
    • Chat app without app_model_config error
  6. End User Management

    • End user creation on initialize request
    • End user retrieval for existing users
    • User name construction from client info

Testing Approach

  • All tests use proper mocking to avoid external dependencies
  • Tests follow existing patterns in the codebase
  • Tests validate JSON-RPC protocol compliance
  • Tests cover both success and error scenarios
  • Tests validate Pydantic model validation
  • Extensive comments throughout explaining test purpose, setup, and assertions

Test Coverage

  • MCP Controller: 20 test cases covering all API endpoints and error scenarios
  • Total: 1377+ lines of well-documented test code

Comments and Documentation

As requested, all tests include extensive comments explaining:

  • Test purpose and what is being verified
  • Test setup and arrangement (Arrange-Act-Assert pattern)
  • Expected behavior and assertions
  • Edge cases and error conditions
  • Parameter descriptions in docstrings

Checklist

  • Tests follow the project's testing guidelines
  • All tests pass locally
  • Code follows existing patterns and conventions
  • No linter errors
  • Tests use proper mocking to avoid external dependencies
  • Extensive comments added throughout test file

Related

Part of the effort to improve test coverage across the Dify codebase.

Close: #29367

Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=157775043

**Original Pull Request:** https://github.com/langgenius/dify/pull/29368 **State:** open **Merged:** No --- ## Description This PR adds comprehensive unit tests for the MCP (Model Context Protocol) controller that was previously missing test coverage. The MCP controller handles JSON-RPC formatted requests according to the Model Context Protocol specification. ## Changes ### MCP Controller Tests - ✅ Test JSON-RPC request parsing and validation - ✅ Test server code validation (existence, status) - ✅ Test request vs notification handling - ✅ Test user input form extraction and conversion - ✅ Test error handling for various failure scenarios - ✅ Test end user management (creation, retrieval) - ✅ Test app availability validation - ✅ Test different request types (initialize, tools/list, tools/call, ping) - ✅ Test notification handling (notifications/initialized) - ✅ Test invalid request formats and error responses **File**: `api/tests/unit_tests/controllers/mcp/test_mcp.py` **Test Cases**: 20 tests covering all major functionality ### Test Coverage Details 1. **JSON-RPC Request Handling** - Valid initialize request processing - Valid tools/list request processing - Valid tools/call request processing - Valid ping request processing - Invalid JSON-RPC format handling - Request without ID field handling 2. **Server Code Validation** - Server not found error handling - App not found error handling - Server inactive status validation - Server active status validation 3. **Request vs Notification Handling** - Valid notification (notifications/initialized) processing - Invalid notification method rejection - HTTP 202 Accepted response for notifications - Request ID requirement validation 4. **User Input Form Integration** - Form extraction for workflow mode apps - Form extraction for chat mode apps - Form conversion to VariableEntity objects - Invalid form structure validation 5. **App Availability Validation** - Workflow app without workflow config error - Chat app without app_model_config error 6. **End User Management** - End user creation on initialize request - End user retrieval for existing users - User name construction from client info ## Testing Approach - All tests use proper mocking to avoid external dependencies - Tests follow existing patterns in the codebase - Tests validate JSON-RPC protocol compliance - Tests cover both success and error scenarios - Tests validate Pydantic model validation - **Extensive comments throughout** explaining test purpose, setup, and assertions ## Test Coverage - **MCP Controller**: 20 test cases covering all API endpoints and error scenarios - **Total**: 1377+ lines of well-documented test code ## Comments and Documentation As requested, all tests include extensive comments explaining: - Test purpose and what is being verified - Test setup and arrangement (Arrange-Act-Assert pattern) - Expected behavior and assertions - Edge cases and error conditions - Parameter descriptions in docstrings ## Checklist - [x] Tests follow the project's testing guidelines - [x] All tests pass locally - [x] Code follows existing patterns and conventions - [x] No linter errors - [x] Tests use proper mocking to avoid external dependencies - [x] Extensive comments added throughout test file ## Related Part of the effort to improve test coverage across the Dify codebase. Close: #29367 Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=157775043
yindo added the pull-request label 2026-02-21 20:51:18 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32388