Skip to main content

MCP Resources

mcpfy provides a simple API for exposing MCP resources from a server. Resources allow an MCP server to make data available to MCP clients through URIs. mcpfy supports both:
  • Static resources — resources with a fixed URI.
  • Resource templates — dynamic resources whose URI contains variables.
Resource handlers can return standard MCP resource results or use mcpfy’s content helpers such as text(), markdown(), and object().

1. Overview

A resource represents data that can be read by an MCP client. A static resource has a fixed URI:
A resource template can generate resources dynamically:
The basic static-resource pattern is:

2. Importing Resource APIs

Resource functionality is available from:

3. Static Resources

A static resource has a predefined URI. Example:
The resource can then be read by an MCP client using its URI.

4. Resource Definition

The ResourceDefinition interface is:

Properties

A callback must be supplied either through readCallback or as the second argument to .resource().

5. Registering a Resource

The standard API is:
Example:

6. Defining the Callback Inside the Definition

The callback can also be provided through readCallback:
Both styles are supported.

7. Resource Callback

A static resource callback receives the mcpfy ToolContext:
Example:
The context allows the resource implementation to use the contextual capabilities provided by mcpfy.

8. Returning Standard MCP Resource Results

A resource callback can return a standard MCP ReadResourceResult. For example:
When a standard ReadResourceResult is returned, mcpfy passes it through directly.

9. Using text()

For simple text resources, use the text() helper:
Example:
mcpfy converts the returned content into the appropriate MCP resource format.

10. Using markdown()

Markdown resources can be created using:
Example:

11. Using object()

Structured data can be returned using the object() helper:
Example:
mcpfy converts content-helper results into an MCP-compatible resource response.

12. MIME Types

A resource can specify its MIME type:
For example:
If a MIME type is supplied in the resource definition, mcpfy uses it when converting content-helper results. For text content without an explicit MIME type, mcpfy falls back to:

13. Resource Templates

Resource templates are used when the resource URI contains dynamic variables. For example:
Instead of registering every user individually, a single resource template can handle all users.

14. Resource Template Definition

The FlatResourceTemplateDefinition interface is:

Properties


15. Registering a Resource Template

Use:
Example:

16. Template Callback

A resource-template callback receives three arguments:
Its type is:

Arguments

uri

The complete requested resource URI.

params

The variables extracted from the URI template.

ctx

The mcpfy ToolContext.

17. Template Example

Consider:
A client requesting:
causes the callback to receive variables corresponding to:
Example:

18. Template Variables and schema

A resource template can optionally specify a Zod schema:
In the current implementation, the schema is a type hint only. It is not used for runtime validation of template variables. The URI template itself is matched using the official MCP SDK’s ResourceTemplate implementation.

19. Dynamic Data Example

Resource templates are useful when resource content depends on the requested URI.
A request for:
can produce:

20. Returning Text From a Template


21. Returning Markdown From a Template


22. Returning Images or Audio

mcpfy’s resource-result conversion also supports content items representing images and audio. For image or audio content, the resulting MCP resource contains binary data through the resource blob field and uses the item’s MIME type. This allows resource handlers to expose non-text content when the corresponding content result is available.

23. Automatic Result Conversion

Resource callbacks can return either a standard MCP ReadResourceResult or mcpfy content-helper results. Conceptually:
For text content:
is converted into a resource content entry containing:
For image and audio content, the data is represented as a resource blob.

24. Resource URI

A resource must have a URI. Example:
Resource templates instead define a URI pattern:
Use a static resource when the URI is fixed and a resource template when part of the URI needs to be resolved dynamically.

25. Static Resource vs Resource Template


26. Resource Subscriptions

MCP resources can support subscriptions when clients need to be notified that a resource has changed. Subscriptions are useful for resources whose contents can change while the server is running. A client can subscribe to a resource using the MCP resource-subscription mechanism. When the resource changes, the server can notify subscribed clients by refreshing the resource. mcpfy exposes resource refresh methods on MCPServer for this purpose.

Refreshing a Specific Resource

Use:
For example:
The refresh operation tells the underlying MCP server that the resource has changed so subscribed clients can request the latest contents.

Refreshing Multiple Resources

When multiple resources need to be refreshed, use:
For example:
Use refreshResource() when one resource changes and refreshResources() when several resources need to be invalidated together.
Resource subscriptions are useful only when the MCP client supports the corresponding subscription capability.

27. Missing Callback

A resource must have a read callback. This is invalid:
mcpfy throws an error indicating that the resource has no read callback. The same rule applies to resource templates:
A callback must be supplied either through readCallback or the second argument.

28. Complete Static Resource Example


29. Complete Resource Template Example


30. How Resources Work Internally

When a static resource is registered:
For a resource template:
For subscribed resources:

31. Best Practices

Use meaningful resource names

Prefer:
over:

Use descriptive URIs

Prefer:
over:
when the resource represents user profiles.

Provide descriptions

Descriptions help clients understand what a resource represents.

Set an appropriate MIME type

For example:
for JSON-like data and:
for Markdown content.

Use templates for dynamic resources

If many resources follow the same URI pattern, use resourceTemplate() instead of registering each URI individually.

Refresh changing resources

If a resource changes while the server is running and clients may subscribe to it, call:
or:
after the underlying data changes.

32. API Summary

server.resource()

Registers a static MCP resource.

server.resourceTemplate()

Registers a dynamic MCP resource template.

server.refreshResource()

Notifies the underlying MCP server that a specific resource has changed.

server.refreshResources()

Refreshes multiple resources.

ResourceDefinition

ReadResourceCallback

FlatResourceTemplateDefinition

ReadResourceTemplateCallback


33. Summary

mcpfy simplifies MCP resource development by providing:
  • Static resource registration with server.resource()
  • Dynamic resource templates with server.resourceTemplate()
  • Optional resource metadata
  • MIME type support
  • Zod type hints for resource-template variables
  • ToolContext access
  • Standard MCP ReadResourceResult support
  • text(), markdown(), and object() content helpers
  • Automatic conversion of content results into MCP resource contents
  • Resource subscriptions through the MCP resource-subscription mechanism
  • server.refreshResource() for refreshing an individual resource
  • server.refreshResources() for refreshing multiple resources
For fixed data, use:
For dynamic URI-based data, use:
For a resource whose contents can change while the server is running, use the resource subscription mechanism together with:
or:
Both resource APIs ultimately integrate with the official MCP server implementation while providing a cleaner developer experience.