Resources
Expose data sources and dynamic content generators to your MCP clients.
Resources give an LLM read‑only access to text, JSON, binaries, or files. Templates extend the idea by letting clients request parameterised URIs that generate data on the fly. In Hyperia both are primarily declared with the @mcp.resource decorator (for dynamic content) or mcp.add_resource() (for static assets).
What Are Resources?
When a client issues resources/read for a URI:
Hyperia finds the matching resource definition.
If the resource is dynamic (a function), it executes.
The returned content is packaged (text, JSON, bytes…) and streamed back.
This mechanism lets LLMs inspect configuration, fetch user docs, read database rows, or access freshly computed summaries relevant to a conversation.
Defining Resources with @resource
@resourceimport json
from hyperia import Hyperia
mcp = Hyperia("DataServer")
# Static string
@mcp.resource("resource://greeting")
def greeting() -> str:
return "Hello from Hyperia!"
# JSON dict auto‑serialised
@mcp.resource("data://config")
def cfg() -> dict:
return {"theme": "dark", "version": "1.2.0"}Key points
URI – the unique identifier (
resource://greeting).Lazy – functions run only when the URI is requested.
Metadata – name & description inferred from the function by default.
Return mapping → content: •
str→TextResourceContentstext/plain•dict/list/BaseModel→ JSON stringapplication/json•bytes→ base64BlobResourceContents(specifymime_type!) •None→ empty list (204‑like).
Custom Metadata
uri(required)name,descriptionoverride inferred valuesmime_typeexplicit for non‑texttagshelp clients filter/group
Accessing MCP Context
Inject a Context param for request metadata, logging, resource reads, sampling, etc.
Async Resources
Use async def for I/O‑heavy reads.
Static Assets with mcp.add_resource()
mcp.add_resource()Common classes: TextResource, BinaryResource, FileResource, HttpResource (requires httpx), DirectoryResource.
Custom Storage Key
Resource Templates (Parameterized URIs)
Templates let you embed placeholders in the URI that map directly to function parameters.
Request
weather://london/current→{city="london"}Request
repos://Hyperia/hyperia/info→{owner, repo}
Wildcard Params {param*} (Hyperia extension)
{param*} (Hyperia extension)Matches any depth: path://docs/api/v1/index.md.
Defaults & Multiple Templates
Error Handling
Raise exceptions or hyperia.ResourceError.
Server Behaviour Flags
Duplicate URIs
Options: "warn" (default), "error", "replace", "ignore".
Last updated