TypeScriptResources

Resources

Expose data that clients and models can read

Resources are data your server makes available to read: files, records, configuration, or a widget's HTML. Where a tool performs an action, a resource is addressed by a URI and reading it never changes anything. A client reads resources to give the model context.

Basic Resource Definition

Resources are defined using the @Resource decorator on class methods:

import { ResourceDecorator as Resource, ExecutionContext } from '@nitrostack/core';

export class ProductResources {
  constructor(private productService: ProductService) {}

  @Resource({
    uri: 'product://{id}',
    name: 'Product Details',
    description: 'Retrieve detailed product information including pricing and inventory',
    mimeType: 'application/json'
  })
  async getProduct(uri: string, context: ExecutionContext) {
    const id = this.extractId(uri, 'product://');
    const product = await this.productService.findById(id);

    if (!product) {
      throw new Error(`Product not found: ${id}`);
    }

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

  private extractId(uri: string, prefix: string): string {
    return uri.replace(prefix, '');
  }
}

Resource topics

On this page