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.
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:2. Importing Resource APIs
Resource functionality is available from:3. Static Resources
A static resource has a predefined URI. Example:4. Resource Definition
TheResourceDefinition 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:6. Defining the Callback Inside the Definition
The callback can also be provided throughreadCallback:
7. Resource Callback
A static resource callback receives the mcpfyToolContext:
8. Returning Standard MCP Resource Results
A resource callback can return a standard MCPReadResourceResult.
For example:
ReadResourceResult is returned, mcpfy passes it through directly.
9. Using text()
For simple text resources, use the text() helper:
10. Using markdown()
Markdown resources can be created using:
11. Using object()
Structured data can be returned using the object() helper:
12. MIME Types
A resource can specify its MIME type:13. Resource Templates
Resource templates are used when the resource URI contains dynamic variables. For example:14. Resource Template Definition
TheFlatResourceTemplateDefinition interface is:
Properties
15. Registering a Resource Template
Use:16. Template Callback
A resource-template callback receives three arguments:Arguments
uri
The complete requested resource URI.
params
The variables extracted from the URI template.
ctx
The mcpfy ToolContext.
17. Template Example
Consider:18. Template Variables and schema
A resource template can optionally specify a Zod schema:
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.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 resourceblob 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 MCPReadResourceResult or mcpfy content-helper results.
Conceptually:
blob.
24. Resource URI
A resource must have a URI. Example: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 onMCPServer for this purpose.
Refreshing a Specific Resource
Use:Refreshing Multiple Resources
When multiple resources need to be refreshed, 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: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:31. Best Practices
Use meaningful resource names
Prefer:Use descriptive URIs
Prefer:Provide descriptions
Descriptions help clients understand what a resource represents.Set an appropriate MIME type
For example:Use templates for dynamic resources
If many resources follow the same URI pattern, useresourceTemplate() 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: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
ToolContextaccess- Standard MCP
ReadResourceResultsupport text(),markdown(), andobject()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 resourceserver.refreshResources()for refreshing multiple resources

