Skip to content

Conversation

arcaputo3
Copy link
Contributor

Fix: Filter template resources from standard resource listing

  • Add filter to exclude resources with template parameters ({}) from resources/list
  • Template resources should only appear in resources/templates/list per MCP spec
  • Add comprehensive test coverage for resource/template separation
  • Update mock transport utilities for better test support

🤖 Generated with Claude Code

This PR fixes the resource listing behavior to properly separate regular resources from resource templates according to the MCP specification. Resources containing template parameters (identified by {} in their URIs) are now filtered out of the standard resources/list response.

Motivation and Context

Fixes #319

The MCP specification requires that:

  • Regular resources (without template parameters) should only appear in resources/list
  • Template resources (containing {} parameters) should only appear in resources/templates/list

Previously, the Java SDK was returning all resources in both endpoints, which violated the protocol specification. This could cause issues for MCP clients that expect proper separation between static resources and parameterized resource templates.

How Has This Been Tested?

  • Added comprehensive unit tests in ResourceTemplateListingTest.java that verify:
    • Template resources (with {}) are filtered from regular resource listings
    • Mixed resource scenarios work correctly
    • The filter logic properly identifies template vs. regular resources
  • All existing tests continue to pass (103 async server tests)
  • Tested with Maven: ./mvnw test -pl mcp

Breaking Changes

No breaking changes. This is a bug fix that brings the implementation in line with the MCP specification. Users who were incorrectly relying on template resources appearing in resources/list should update their code to use resources/templates/list instead.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Implementation Details:

  • Added a simple filter in McpAsyncServer.java line 597 that excludes any resource with { in its URI from the standard listing
  • The filter is applied during the stream processing of resources, maintaining efficiency
  • Mock transport utilities were enhanced to support better testing scenarios

Test Coverage:

  • testTemplateResourcesFilteredFromRegularListing() - Verifies basic filtering logic
  • testResourceListingWithMixedResources() - Tests filtering with both regular and template resources
  • testResourceTemplatesListedSeparately() - Ensures proper separation between the two resource types

This fix ensures the Java SDK properly implements the MCP specification for resource discovery, improving compatibility with MCP clients that rely on correct protocol behavior.

- Add filter to exclude resources with template parameters ({}) from resources/list
- Template resources should only appear in resources/templates/list per MCP spec
- Add comprehensive test coverage for resource/template separation
- Update mock transport utilities for better test support

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
tzolov pushed a commit that referenced this pull request Sep 4, 2025
- Add filter to exclude resources with template parameters ({}) from resources/list
- Template resources should only appear in resources/templates/list per MCP spec
- Add comprehensive test coverage for resource/template separation
- Update mock transport utilities for better test support

Signed-off-by: Christian Tzolov <[email protected]>
@tzolov
Copy link
Contributor

tzolov commented Sep 4, 2025

Thanks @arcaputo3

@tzolov
Copy link
Contributor

tzolov commented Sep 4, 2025

Rebased and merged at 2314238

@tzolov tzolov closed this Sep 4, 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.

Bug: resources/list endpoint incorrectly exposes resource templates

2 participants