Skip to main content

Basic Usage

import { Resource, ResourceContext } from '@frontmcp/sdk';

@Resource({
  name: 'app-config',
  uri: 'config://app',
  mimeType: 'application/json',
  description: 'Application configuration',
})
class AppConfigResource extends ResourceContext {
  async execute(uri: string) {
    return {
      contents: [{
        uri,
        mimeType: 'application/json',
        text: JSON.stringify({ version: '1.0.0', debug: false }),
      }],
    };
  }
}

Signature

function Resource(providedMetadata: ResourceMetadata): ClassDecorator

Configuration Options

Required Properties

PropertyTypeDescription
namestringUnique resource identifier
uristringResource URI (must be valid per RFC 3986)

Optional Properties

PropertyTypeDescription
titlestringHuman-readable title
descriptionstringResource description
mimeTypestringMIME type (e.g., ‘application/json’)
iconsIcon[]Icons for display

URI Schemes

Resources can use any valid URI scheme:
// File scheme
@Resource({
  name: 'readme',
  uri: 'file:///docs/README.md',
  mimeType: 'text/markdown',
})

// Custom scheme
@Resource({
  name: 'database-schema',
  uri: 'db://schema/users',
  mimeType: 'application/json',
})

// HTTPS scheme
@Resource({
  name: 'api-spec',
  uri: 'https://api.example.com/openapi.json',
  mimeType: 'application/json',
})

Return Format

Resources must return ReadResourceResult:
interface ReadResourceResult {
  contents: Array<{
    uri: string;
    mimeType?: string;
    text?: string;      // Text content
    blob?: string;      // Base64-encoded binary content
  }>;
}

Text Content

@Resource({ name: 'text-file', uri: 'file://readme.txt' })
class TextResource extends ResourceContext {
  async execute(uri: string) {
    return {
      contents: [{
        uri,
        mimeType: 'text/plain',
        text: 'Hello, World!',
      }],
    };
  }
}

Binary Content

@Resource({ name: 'image', uri: 'file://logo.png', mimeType: 'image/png' })
class ImageResource extends ResourceContext {
  async execute(uri: string) {
    const imageBuffer = await readFile('/path/to/logo.png');
    return {
      contents: [{
        uri,
        mimeType: 'image/png',
        blob: imageBuffer.toString('base64'),
      }],
    };
  }
}

Function-Based Alternative

import { resource } from '@frontmcp/sdk';

const appConfig = resource({
  name: 'app-config',
  uri: 'config://app',
  mimeType: 'application/json',
})((uri, params) => ({
  contents: [{
    uri,
    mimeType: 'application/json',
    text: JSON.stringify({ version: '1.0.0' }),
  }],
}));

Context Methods

The ResourceContext base class provides:
async execute(uri: string, params: Record<string, string>) {
  // Dependency injection
  const db = this.get(DatabaseToken);

  // Logging
  this.logger.info('Reading resource', { uri });

  // Authentication
  const auth = this.getAuthInfo();

  // Access scope
  const tools = this.scope.tools;

  // Error handling
  if (!exists) {
    this.fail(new ResourceNotFoundError(uri));
  }

  // Early return
  this.respond({ contents: [...] });
}

Full Example

import { Resource, ResourceContext, App, FrontMcp } from '@frontmcp/sdk';

@Resource({
  name: 'system-status',
  uri: 'status://system',
  title: 'System Status',
  description: 'Current system health and metrics',
  mimeType: 'application/json',
})
class SystemStatusResource extends ResourceContext {
  async execute(uri: string) {
    this.mark('fetching-metrics');

    const metrics = {
      uptime: process.uptime(),
      memory: process.memoryUsage(),
      timestamp: new Date().toISOString(),
    };

    return {
      contents: [{
        uri,
        mimeType: 'application/json',
        text: JSON.stringify(metrics, null, 2),
      }],
    };
  }
}

@App({
  name: 'monitoring',
  resources: [SystemStatusResource],
})
class MonitoringApp {}

@FrontMcp({
  info: { name: 'Monitor', version: '1.0.0' },
  apps: [MonitoringApp],
})
export default class MonitorServer {}

@ResourceTemplate

Dynamic URI templates

ResourceContext

Context class details

ResourceRegistry

Resource registry API

Resource Errors

Resource-related errors