Resource Templates
3 minute read
Resource templates allow you to expose an entire directory tree of files dynamically without defining each file individually. Clients can discover available templates using resources/templates/list and read specific files using resources/read by supplying a constructed URI.
Examples
Project Documentation Template
You can configure a template dedicated to documentation files with a custom mimeType. Note that allowedPaths can also use relative paths (such as ./docs), which are automatically resolved relative to the directory containing your configuration file:
kind: resourceTemplate
name: project_docs
type: file
title: "Project Documentation"
description: "Markdown documentation for the project."
uriTemplate: "file:///docs/{path}"
allowedPaths:
- "/docs"
mimeType: "text/markdown"
When queried via resources/templates/list, the response includes the defined mimeType:
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"resourceTemplates": [
{
"name": "project_docs",
"title": "Project Documentation",
"uriTemplate": "file:///docs/{path}",
"description": "Markdown documentation for the project.",
"mimeType": "text/markdown",
"annotations": {
"priority": 1
}
}
]
}
}
The client can then retrieve markdown documentation using resources/read:
{
"jsonrpc": "2.0",
"id": 2,
"method": "resources/read",
"params": {
"uri": "file:///docs/guide.md"
}
}
Reference
Resource Template Schema
| field | type | required | description |
|---|---|---|---|
name | string | Yes | Unique name for the resource template. |
type | string | Yes | Must be "file". |
uriTemplate | string | Yes | RFC 6570 URI template. Must contain the {path} variable (e.g., file:///logs/{path}). |
allowedPaths | []string | No | Allowed directory trees on disk. Any path resolving outside these boundaries is blocked. |
maxSize | int64 / string | No | Maximum allowed file size in bytes (defaults to 5MB / 5242880 bytes). |
description | string | No | A brief explanation of what the resource template exposes. |
title | string | No | Human-readable title for client display. |
mimeType | string | No | Default MIME type for content returned by this template. |
annotations | Annotations | No | Metadata annotations (priority, audience, lastModified). |
Guardrails & Security Model
Strict
{path}Variable Requirement: TheuriTemplatemust follow the RFC 6570 specification and must contain only the{path}variable. Any other template variables (e.g.,{id},{filename}) will fail validation at startup.Global System Access by Default: If
allowedPathsis omitted, the template will have no sandbox. It will be able to read any file on the entire system that possesses an allowed extension (e.g..txt,.json). It is highly recommended to always specifyallowedPathsto sandbox the template to a specific directory tree.Path Traversal Prevention:
- If
allowedPathsis defined, the target path must resolve strictly within one of the specified allowed directories. - Path traversal attempts using relative segments (such as
../) or symlink escapes will be blocked with a security violation error.
- If
Hidden File Protection:
- If
allowedPathsis omitted, access to hidden files and directories (files or directories beginning with.) is automatically blocked to prevent accidental exposure of.envfiles,.sshkeys, or.gitdirectories. - If
allowedPathsis specified, hidden files are permitted, provided they reside within the allowed directory boundaries.
- If
Allowed Extensions:
- The requested URI and the resolved disk path must both have an allowed text file extension:
.txt,.md,.csv,.json,.yaml,.yml,.xml,.sql
- Requests for files with unsupported extensions are rejected.
- The requested URI and the resolved disk path must both have an allowed text file extension:
Regular Files Only:
resources/readcan only read regular files. If a client provides a URI that resolves to a directory, block device, socket, or pipe, an error is returned.
Size Limits & Truncation (
maxSize):- If a matched file exceeds
maxSize(defaults to 5MB /5242880bytes, configurable up to 1GB), Toolbox does not error. Instead, it reads up tomaxSizebytes, safely cleans incomplete multi-byte UTF-8 sequences at the cut-off boundary, and appends a clear truncation notice:This prevents memory exhaustion while still delivering the initial content of large files to the LLM....[TRUNCATED BY SERVER: Payload exceeded <limit> byte safety limit]...
- If a matched file exceeds
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.