API Reference
resource()
Expose read-only files, data, and context templates.
resource()
Defines a static or dynamic data source that clients can load into the LLM context. Resources are read-only and identified by URIs.
import { resource } from "mcpcraft-sdk"
// Static resource
const systemInfo = resource({
name: "system_info",
description: "Exposes system information",
uri: "info://system",
mimeType: "application/json",
fetch: async () => {
return { platform: process.platform, arch: process.arch }
}
})
// Dynamic resource template
const userProfile = resource({
name: "user_profile",
description: "Dynamic profile loader",
uri: "users://{userId}/profile",
mimeType: "application/json",
fetch: async (params) => {
const { userId } = params
return { id: userId, username: `user_${userId}` }
}
})Options
| Parameter | Type | Required | Description |
|---|---|---|---|
name | string | Yes | Identifies the resource |
description | string | Yes | Describes the data returned to the LLM |
uri | string | Yes | URI identifier (supports {variable} templates) |
mimeType | string | No | MIME type, e.g. application/json or text/plain |
fetch | (params) => any | Yes | Resolver that returns the resource data |
URI Templates
Use {variable} syntax in URIs to create dynamic resources that accept parameters:
| URI Pattern | Example Call | params Received |
|---|---|---|
info://system | info://system | {} |
users://{userId}/profile | users://42/profile | { userId: "42" } |
orders://{orgId}/items/{itemId} | orders://acme/items/99 | { orgId: "acme", itemId: "99" } |
Variables are automatically parsed from the URI path and passed to your fetch handler.
Full Example
import { createServer, resource } from "mcpcraft-sdk"
const server = createServer({ name: "data-server" })
server.add(resource({
name: "server_status",
description: "Current server health metrics",
uri: "status://health",
mimeType: "application/json",
fetch: async () => ({
uptime: process.uptime(),
memory: process.memoryUsage(),
cpu: process.cpuUsage()
})
}))
server.add(resource({
name: "document",
description: "Fetches a document by ID",
uri: "docs://{docId}",
mimeType: "text/plain",
fetch: async ({ docId }) => {
return `Content of document ${docId}`
}
}))
server.start()