Skip to content

Conversation

tzolov
Copy link
Contributor

@tzolov tzolov commented Sep 25, 2025

Refactor resource template management from List-based to Map-based storage, add new resource template specification classes, and enhance resource/template handling with improved error handling and runtime management capabilities.

  1. New Resource Template Specification Classes:

    • Added AsyncResourceTemplateSpecification and SyncResourceTemplateSpecification to McpServerFeatures
    • Added AsyncResourceTemplateSpecification and SyncResourceTemplateSpecification to McpStatelessServerFeatures
    • Proper type safety and handler function definitions for resource templates
  2. Resource Template Handling Refactoring:

    • Changed from List<ResourceTemplate> to Map<String, ResourceTemplateSpecification>
    • Consistent with existing resource storage patterns across all server implementations
  3. Enhanced Runtime Management:

    • Added addResourceTemplate() and removeResourceTemplate() methods to all server classes
    • Added listResourceTemplates() for introspection
    • Proper notification handling for template changes
    • Support for both sync and async operations
  4. Error Handling Improvements:

    • Replaced McpError with appropriate IllegalArgumentException and IllegalStateException
    • Better error messages with contextual information
    • Consistent error handling across all server implementations
    • Graceful handling of non-existent resources/templates
  5. McpSchema Enhancements:

    • Added ResourceTemplate.Builder class for easier template construction
    • Enhanced interface hierarchy with Meta, Identifier, and Annotated interfaces
    • Improved type constants for reference types
    • Add toString() implementation for McpError.
  6. Test Coverage:

    • New dedicated test class for resource template management
    • Updated existing tests to work with Map-based storage and new specification classes
    • Comprehensive error scenario testing
    • Tests for both sync and async resource template operations

- Convert resource templates from List to Map-based storage for better lookup performance
- Add runtime resource template management methods (add/remove/list)
- Replace McpError with more appropriate exception types (IllegalArgumentException, IllegalStateException)
- Enhance completion request handling with better validation and error messages
- Add type-safe constants for reference types (PromptReference.TYPE, ResourceReference.TYPE)
- Improve separation between static resources and dynamic resource templates
- Add comprehensive resource and resource template listing capabilities
- Reorganize imports and improve code structure across server implementations
- Update test cases to reflect new API patterns
- Add tests for listing, removing, and managing resources
- Add tests for resource template operations (add, remove, list)
- Include capability validation tests for resource templates
- Cover edge cases like removing nonexistent resources/templates
- Apply changes to both async and sync server test suites
- Reorganize imports for better code organization

This refactoring improves type safety, performance, and maintainability while
providing a more consistent API for resource template management across all
server implementations (Async, Sync, Stateless variants).

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
@tzolov tzolov marked this pull request as ready for review September 25, 2025 16:39
@tzolov tzolov added this to the 0.14.0 milestone Sep 25, 2025
@tzolov tzolov added the enhancement New feature or request label Sep 25, 2025
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Copy link
Contributor

@sdelamo sdelamo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for this fix

Copy link
Contributor

@LucaButBoring LucaButBoring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listResourceTemplates appears to be typed incorrectly - looks good beyond that.

.stream()
.map(McpServerFeatures.AsyncResourceSpecification::resource)
.filter(resource -> !resource.uri().contains("{"))
// .filter(resource -> !resource.uri().contains("{"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: leftover comment should be deleted

Comment on lines 636 to 638
public Flux<McpSchema.Resource> listResourceTemplates() {
return Flux.fromIterable(this.resources.values()).map(McpServerFeatures.AsyncResourceSpecification::resource);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be this.resourceTemplates and return a Flux<McpSchema.ResourceTemplate>? Looks like this is correct in McpStatelessAsyncServer, but not here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! will be fixed.
Though this is a blunder it doesn't affect the MPC spec.
The listResource() and listResourceTemplates() are an extension of the Server's runtime API to compliment the addResource()/removeResource() and addResourceTemplate()/removeResourceTemplate().
In separate PR we will add similar list methods for tools, prompts, completions as well.

* List all registered resource templates.
* @return A list of all registered resource templates
*/
public List<McpSchema.Resource> listResourceTemplates() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List<McpSchema.ResourceTemplate> - the return type was wrong in McpAsyncServer as well.


private Map<String, Object> meta;

public Builder uri(String uri) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uriTemplate

Comment on lines 527 to 530
// Note: Based on the current implementation, listResourceTemplates() returns
// List<Resource>
// This appears to be a bug in the implementation that should return
// List<ResourceTemplate>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +544 to +547
// Note: Based on the current implementation, listResourceTemplates() returns
// Flux<Resource>
// This appears to be a bug in the implementation that should return
// Flux<ResourceTemplate>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 526 to 529
// Note: Based on the current implementation, listResourceTemplates() returns
// List<Resource>
// This appears to be a bug in the implementation that should return
// List<ResourceTemplate>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
@tzolov
Copy link
Contributor Author

tzolov commented Sep 27, 2025

Thank you @LucaButBoring . Last commit should address the above-mentioned issues

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
tzolov added a commit that referenced this pull request Sep 28, 2025
- Provide separation of concerns between static resources and parameterized resource templates.
- Add AsyncResourceTemplateSpecification and SyncResourceTemplateSpecification for both McpServerFeatures and McpStatelessServerFeatures
- Change resource template storage from List to Map to acomodate the resource read handler.
- Add runtime management methods: addResourceTemplate(), removeResourceTemplate(), listResourceTemplates()
- Improve error handling by using IllegalArgumentException/IllegalStateException instead of McpError
- Add new interfaces (Meta, Identifier) and reorganize schema hierarchy
- Enhance completion request validation with better error messages
- Add ResourceTemplate.Builder for easier template construction
- Update all server implementations (Async, Sync, Stateless) consistently
- Add type-safe constants for reference types (PromptReference.TYPE, ResourceReference.TYPE)
- Add tests for new resource template management functionality
- Clean up imports and remove unused dependencies

Co-authored-by: Pascal Vantrepote <pascal@confluent.io>

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
@tzolov
Copy link
Contributor Author

tzolov commented Sep 28, 2025

Rebased, ,squashed and merged at ff98e29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants