Skip to content

Conversation

alejojperez
Copy link

🚀 Add Resource Template Support

Summary

This PR introduces comprehensive support for resource templates in the MCP server, enabling dynamic URI matching and path parameter extraction. The implementation includes a new ListResourceTemplates method, enhanced URI matching capabilities, and extensive testing coverage.

🎯 Key Features Added

New Resource Template Functionality

  • ListResourceTemplates Method: New server method to retrieve available resource templates
  • Dynamic URI Matching: Resources can now match URI templates with path parameters (e.g., /users/{id})
  • Path Parameter Extraction: Automatic extraction and population of path variables from matched URIs
  • URI Validation System: Comprehensive validation framework with scheme and URI pattern validators

Enhanced Resource Management

  • Template Matching Logic: Resources can now determine if they match specific URI patterns
  • Flexible URI Handling: Support for both exact matches and template-based matching
  • Path Variable Population: Automatic injection of extracted path parameters into resource context

Testing Infrastructure Improvements

  • Enhanced Test Support: Improved testing utilities with URI override capabilities
  • Comprehensive Test Coverage: Added extensive unit and feature tests for all new functionality
  • Test Response Enhancements: Better support for testing resource template scenarios

📁 Files Changed

15 files changed, 790 insertions(+), 18 deletions(-)

Core Implementation

  • src/Server.php - Added ListResourceTemplates method registration
  • src/Server/Methods/ListResourceTemplates.php - NEW: Main implementation of resource template listing
  • src/Server/Methods/ReadResource.php - Enhanced to handle path parameter population
  • src/Server/Resource.php - Major enhancements for template matching and URI handling
  • src/Server/ServerContext.php - Added support for path variable management

URI Matching System

  • src/Server/Resources/Uri.php - NEW: Comprehensive URI handling and template matching (283+ lines)
  • src/Server/Resources/Matching/ValidatorInterface.php - NEW: Base interface for URI validators
  • src/Server/Resources/Matching/SchemeValidator.php - NEW: Scheme validation implementation
  • src/Server/Resources/Matching/UriValidator.php - NEW: URI pattern validation implementation

Testing Enhancements

  • src/Server/Testing/PendingTestResponse.php - Added parameter override functionality
  • src/Server/Testing/TestResponse.php - Enhanced with URI testing support
  • tests/TestCase.php - Refactored for better test utilities
  • tests/Unit/Resources/ListResourceTemplatesTest.php - NEW: Comprehensive unit tests (143+ lines)
  • tests/Unit/Resources/ReadResourceTest.php - NEW: Read resource testing
  • tests/Feature/Testing/Resources/AssertSeeTest.php - NEW: Feature tests for resource assertions

🔧 Technical Details

  • URI Template Matching: Supports parameterized URIs like /api/users/{userId}/posts/{postId}
  • Path Parameter Injection: Extracted parameters are automatically available in resource methods
  • Backward Compatibility: All existing functionality remains unchanged
  • Validation Framework: Extensible validation system for different URI schemes and patterns

✅ Testing

  • All existing tests continue to pass
  • New tests were added to support the functionality

This enhancement significantly expands the MCP server's capabilities for handling dynamic resources while maintaining full backward compatibility with existing implementations.

@alejojperez
Copy link
Author

PR number 80 has a better implementation. I'm closing this PR.

@alejojperez alejojperez closed this Oct 9, 2025
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