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:

  1. Hyperia finds the matching resource definition.

  2. If the resource is dynamic (a function), it executes.

  3. 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

import 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: • strTextResourceContents text/plaindict/list/BaseModel → JSON string application/jsonbytes → base64 BlobResourceContents (specify mime_type!) • None → empty list (204‑like).


Custom Metadata

  • uri (required)

  • name, description override inferred values

  • mime_type explicit for non‑text

  • tags help 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()

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)

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