ruby-sdk/lib/mcp/resource.rb
Koichi ITO 0c729d0a2d Use autoload to defer loading of unused subsystems
## Motivation and Context

`lib/mcp.rb` currently declares all `require_relative` statements in one place.
As a result, Server and Client are both loaded, the stdio and Streamable HTTP
transports are both loaded, and Tool, Prompt, and Resource are all loaded,
regardless of which components are actually used.

These components are selected by the user depending on their use case,
so loading everything upfront is unnecessary.

Using `autoload` for selected constants defers loading until the constant
is actually referenced, so only the subsystems required by the user are loaded.

## How Has This Been Tested?

All existing tests pass unchanged.

## Breaking Change

None. This change only defers loading of unused components.
2026-03-12 14:08:27 +09:00

30 lines
700 B
Ruby

# frozen_string_literal: true
require_relative "resource/contents"
require_relative "resource/embedded"
module MCP
class Resource
attr_reader :uri, :name, :title, :description, :icons, :mime_type
def initialize(uri:, name:, title: nil, description: nil, icons: [], mime_type: nil)
@uri = uri
@name = name
@title = title
@description = description
@icons = icons
@mime_type = mime_type
end
def to_h
{
uri: uri,
name: name,
title: title,
description: description,
icons: icons&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
mimeType: mime_type,
}.compact
end
end
end