Class: ActionModelContext::Resource

Inherits:
Object
  • Object
show all
Includes:
Primitive
Defined in:
lib/action_model_context/resource.rb

Overview

Base class of the resources. An application subclasses it through ApplicationResource, under app/mcp/resources.

Examples:

class UserResource < ApplicationResource
  uri 'users://{id}'

  def read(id:) = { id:, viewer: context[:user] }
end

Instance Attribute Summary

Attributes included from Primitive

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Primitive

description, title

Class Method Details

.mime_type(value = nil) ⇒ String?

Declares the MIME type, or reads it. Subclasses inherit it, and it overrides the one derived from #read.

Parameters:

  • value (String, nil) (defaults to: nil) —

    the MIME type to declare

Returns:

  • (String, nil) —

    nil when none is declared

.resource_name(name = nil) ⇒ String?

Declares the name, or reads it.

Parameters:

  • name (String, nil) (defaults to: nil) —

    the name to declare

Returns:

  • (String, nil) —

    the declared name, else the underscored class name without its Resource suffix

.uri(value = nil) ⇒ String?

Declares the URI, or reads it. A {variable} makes the resource a template, and subclasses do not inherit it.

Parameters:

  • value (String, nil) (defaults to: nil) —

    the URI to declare

Returns:

  • (String, nil) —

    nil when the class declares none

Instance Method Details

#read ⇒ String, ...

Reads the resource. A subclass defines it, with one keyword per variable of the URI.

Returns:

  • (String, Hash, Array) —

    the contents: a String goes out as text/plain, the others as application/json

Raises:

  • (Error) —

    to answer an invalid_params JSON-RPC error

  • (NotImplementedError) —

    when no subclass defines it