Package com.tnsai.mcp.server
Interface ResourceProvider
public interface ResourceProvider
Interface for providing resources to an MCP server.
Implement this interface to expose resources (files, data, etc.) to MCP clients. Resources are identified by URIs and can contain text or binary content. Providers may also expose resource templates for parameterized URI patterns.
Example implementation:
public class FileResourceProvider implements ResourceProvider {
private final Path rootDir;
@Override
public List<Resource> listResources() {
return Files.walk(rootDir)
.filter(Files::isRegularFile)
.map(path -> Resource.builder()
.uri("file://" + path.toString())
.name(path.getFileName().toString())
.mimeType(Files.probeContentType(path))
.build())
.collect(Collectors.toList());
}
@Override
public List<ResourceTemplate> listResourceTemplates() {
return List.of(ResourceTemplate.builder()
.uriTemplate("file:///{path}")
.name("File by path")
.description("Access any file by its path")
.build());
}
@Override
public List<ResourceContent> readResource(String uri) {
Path path = Paths.get(URI.create(uri));
String content = Files.readString(path);
ResourceContent rc = new ResourceContent();
rc.setUri(uri);
rc.setText(content);
rc.setMimeType("text/plain");
return List.of(rc);
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceListener for resource change notifications.static classException thrown when a resource is not found. -
Method Summary
Modifier and TypeMethodDescriptionReturns the list of resources provided by this provider.default List<ResourceTemplate> Returns the list of resource templates provided by this provider.readResource(String uri) Reads the content of a resource.default voidsubscribe(String uri, ResourceProvider.ResourceChangeListener listener) Subscribes to changes for a resource.default booleanChecks if this provider supports resource subscriptions.default voidunsubscribe(String uri) Unsubscribes from changes for a resource.
-
Method Details
-
listResources
Returns the list of resources provided by this provider.- Returns:
- list of resource definitions
-
listResourceTemplates
Returns the list of resource templates provided by this provider. Templates define parameterized URI patterns that clients can use to construct resource URIs.- Returns:
- list of resource templates (empty by default)
-
readResource
Reads the content of a resource.- Parameters:
uri- the resource URI- Returns:
- list of resource contents (may contain multiple parts)
- Throws:
ResourceProvider.ResourceNotFoundException- if the resource is not found
-
supportsSubscription
default boolean supportsSubscription()Checks if this provider supports resource subscriptions. Override to return true if your provider can notify clients of changes.- Returns:
- true if subscriptions are supported
-
subscribe
Subscribes to changes for a resource. Only called if supportsSubscription() returns true.- Parameters:
uri- the resource URIlistener- the listener to notify of changes
-
unsubscribe
Unsubscribes from changes for a resource.- Parameters:
uri- the resource URI
-