TemplateFileProvider

class TemplateFileProvider(fileSystem: FileSystem, rootDir: Path, name: String = "Arbitrary local file access", description: String = "Allows reading any file by specifying {path}", mimeTypeDetector: MimeTypeDetector = MimeTypeDetector { "text/plain" }) : ResourceProvider

A ResourceProvider that exposes rootDir through the URI template:

file:///{path}

to the client, where path is a relative path under rootDir.

Example usage:

val templateProvider = TemplateFileProvider(
fileSystem = FileSystem.SYSTEM,
rootDir = "/app/resources".toPath(),
)

The client can then read resources such as file:///sub/folder/example.txt, which will be resolved to /app/resources/sub/folder/example.txt.

Constructors

Link copied to clipboard
constructor(fileSystem: FileSystem, rootDir: Path, name: String = "Arbitrary local file access", description: String = "Allows reading any file by specifying {path}", mimeTypeDetector: MimeTypeDetector = MimeTypeDetector { "text/plain" })

Properties

Link copied to clipboard
var onResourceChange: suspend (uri: String) -> Unit
Link copied to clipboard
var onResourcesListChanged: suspend () -> Unit
Link copied to clipboard
open override val supportsSubscriptions: Boolean = true

Functions

Link copied to clipboard
open fun attachCallbacks(onResourceChange: suspend (String) -> Unit, onResourcesListChanged: suspend () -> Unit)
Link copied to clipboard
open suspend override fun listResources(): List<Resource>

We have no discrete listing of resources; everything is driven by the template, so listResources returns empty by default.

Link copied to clipboard
open suspend override fun listResourceTemplates(): List<ResourceTemplate>

Returns the resource template defined in the constructor

Link copied to clipboard
open suspend override fun readResource(uri: String): ResourceContents?

Given a 'file:///{path}', we parse the actual path from the URI. Then we do a minimal check (normalize & trivial sandbox check) and read the file from disk if it exists.