mcpcraft-sdk
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

ParameterTypeRequiredDescription
namestringYesIdentifies the resource
descriptionstringYesDescribes the data returned to the LLM
uristringYesURI identifier (supports {variable} templates)
mimeTypestringNoMIME type, e.g. application/json or text/plain
fetch(params) => anyYesResolver that returns the resource data

URI Templates

Use {variable} syntax in URIs to create dynamic resources that accept parameters:

URI PatternExample Callparams Received
info://systeminfo://system{}
users://{userId}/profileusers://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()