Skip to content

Resources

Dick Davis edited this page Jan 27, 2026 · 1 revision

Resources

The ModelContextProtocol::Server::Resource base class allows subclasses to define a resource that the MCP client can use.

Define the resource properties and optionally annotations, then implement the call method to build your resource. Use the respond_with instance method to ensure your resource responds with appropriately formatted response data.

You can also send MCP log messages to clients from within your resource by calling a valid logger level method on the client_logger and passing a string message. For server-side debugging and monitoring, use the server_logger to write logs that are not sent to clients.

Resource Definition

Use the define block to set resource properties and configure annotations.

Property Description
name The name of the resource
title Human-readable display name
description Short description of what the resource contains
mime_type MIME type of the resource content
uri URI identifier for the resource
annotations Block for defining resource annotations

Annotation Definition

Define any resource annotations using an annotations block nested within the define block.

Property Description
audience Target audience for the resource (array of symbols like [:user, :assistant])
priority Priority level (numeric value, e.g., 0.9)
last_modified Last modified timestamp (ISO 8601 string)

Resource Methods

Define your resource properties and annotations, implement the call method to build resource content and respond_with to serialize the response. You can wrap long running operations in a cancellable block to allow clients to cancel the request. Also, you can automatically send progress notifications to clients by wrapping long-running operations in a progressable block.

Method Context Description
define Class definition Block for defining resource metadata and annotations
call Instance method Main method to implement resource logic and build response
cancellable Within call Wrap long-running operations to allow client cancellation (e.g., cancellable { slow_operation })
progressable Within call Wrap long-running operations to send clients progress notifications (e.g., progressable { slow_operation })
respond_with Within call Return properly formatted response data (e.g., respond_with text: or respond_with binary:)

Available Instance Variables

Resources have access to their configured properties and server context.

Variable Context Description
mime_type Within call The configured MIME type for this resource
uri Within call The configured URI identifier for this resource
client_logger Within call Client logger instance for sending MCP log messages (e.g., client_logger.info("message"))
server_logger Within call Server logger instance for debugging and monitoring (e.g., server_logger.debug("message"))
context Within call Hash containing server configuration context values

Examples

Text Resource

This is an example resource that returns a text response:

class TestResource < ModelContextProtocol::Server::Resource
  define do
    name "top-secret-plans.txt"
    title "Top Secret Plans"
    description "Top secret plans to do top secret things"
    mime_type "text/plain"
    uri "file:///top-secret-plans.txt"
  end

  def call
    respond_with text: "I'm finna eat all my wife's leftovers."
  end
end

Annotated Resource

This is an example resource with annotations:

class TestAnnotatedResource < ModelContextProtocol::Server::Resource
  define do
    name "annotated-document.md"
    description "A document with annotations showing priority and audience"
    mime_type "text/markdown"
    uri "file:///docs/annotated-document.md"
    annotations do
      audience [:user, :assistant]
      priority 0.9
      last_modified "2025-01-12T15:00:58Z"
    end
  end

  def call
    respond_with text: "# Annotated Document\n\nThis document has annotations."
  end
end

Binary Resource

This is an example resource that returns binary data:

class TestBinaryResource < ModelContextProtocol::Server::Resource
  define do
    name "project-logo.png"
    description "The logo for the project"
    mime_type "image/png"
    uri "file:///project-logo.png"
  end

  def call
    # In a real implementation, we would retrieve the binary resource
    # This is a small valid base64 encoded string (represents "test")
    data = "dGVzdA=="
    respond_with binary: data
  end
end

Clone this wiki locally