diff --git a/docs/_server/tools.md b/docs/_server/tools.md index f62e3d33..c902f4f0 100644 --- a/docs/_server/tools.md +++ b/docs/_server/tools.md @@ -437,11 +437,11 @@ class WeatherTool < MCP::Tool end ``` -## Tool Responses with Image, Audio, and Embedded Resources +## Tool Responses with Image, Audio, Resource Links, and Embedded Resources -Tool responses are not limited to text. The `MCP::Content` module provides `Image`, `Audio`, and `EmbeddedResource` content types, -which serialize to the `image`, `audio`, and `resource` content blocks defined by the MCP spec. Image and audio data is passed as -a base64-encoded string together with its MIME type: +Tool responses are not limited to text. The `MCP::Content` module provides `Image`, `Audio`, `ResourceLink`, and `EmbeddedResource` +content types, which serialize to the `image`, `audio`, `resource_link`, and `resource` content blocks defined by the MCP spec. +Image and audio data is passed as a base64-encoded string together with its MIME type: ```ruby class ChartTool < MCP::Tool @@ -466,6 +466,30 @@ class SpeechTool < MCP::Tool end ``` +A resource link wraps an `MCP::Resource`, allowing a tool to point the client at a resource to read or subscribe to instead of returning +its contents: + +```ruby +class EntryPointTool < MCP::Tool + description "Locate the application entry point" + + def self.call(server_context:) + resource = MCP::Resource.new( + uri: "file:///project/src/main.rs", + name: "main.rs", + description: "Primary application entry point", + mime_type: "text/x-rust", + ) + + MCP::Tool::Response.new([MCP::Content::ResourceLink.new(resource).to_h]) + end +end +``` + +The linked resource does not have to be one the server returns from `resources/list`. Returning a link does not make the resource +readable: a `resources/read` of its URI goes through the read handler like any other request, so the handler is where the read is +authorized. See the [resources page](/server/resources/) for serving reads. + An [embedded resource](/server/resources/) wraps `MCP::Resource::TextContents` or `MCP::Resource::BlobContents`, allowing a tool to return resource contents inline: ```ruby diff --git a/lib/mcp/content.rb b/lib/mcp/content.rb index 591ecb4b..dae5e524 100644 --- a/lib/mcp/content.rb +++ b/lib/mcp/content.rb @@ -46,6 +46,18 @@ def to_h end end + class ResourceLink + attr_reader :resource + + def initialize(resource) + @resource = resource + end + + def to_h + resource.to_h.merge(type: "resource_link") + end + end + class EmbeddedResource attr_reader :resource, :annotations, :meta diff --git a/test/mcp/content_test.rb b/test/mcp/content_test.rb index fc2103ee..4082cae1 100644 --- a/test/mcp/content_test.rb +++ b/test/mcp/content_test.rb @@ -143,6 +143,73 @@ def resource.to_h end end + class ResourceLinkTest < ActiveSupport::TestCase + test "#to_h returns correct format per MCP spec" do + resource = Resource.new( + uri: "file:///project/src/main.rs", + name: "main.rs", + description: "Primary application entry point", + mime_type: "text/x-rust", + ) + + result = ResourceLink.new(resource).to_h + + assert_equal( + { + uri: "file:///project/src/main.rs", + name: "main.rs", + description: "Primary application entry point", + mimeType: "text/x-rust", + type: "resource_link", + }, + result, + ) + end + + test "#to_h carries the optional fields of the resource" do + resource = Resource.new( + uri: "file:///project/src/main.rs", + name: "main.rs", + title: "Main", + icons: [Icon.new(mime_type: "image/png", sizes: ["48x48"], src: "https://example.com/icon.png", theme: "light")], + annotations: Annotations.new(audience: ["user"], priority: 0.5, last_modified: "2025-01-12T15:00:58Z"), + size: 0, + meta: { "example" => "value" }, + ) + + result = ResourceLink.new(resource).to_h + + assert_equal( + { + uri: "file:///project/src/main.rs", + name: "main.rs", + title: "Main", + icons: [{ mimeType: "image/png", sizes: ["48x48"], src: "https://example.com/icon.png", theme: "light" }], + annotations: { audience: ["user"], priority: 0.5, lastModified: "2025-01-12T15:00:58Z" }, + size: 0, + _meta: { "example" => "value" }, + type: "resource_link", + }, + result, + ) + end + + test "#to_h with a class-based resource" do + resource_class = Class.new(Resource) do + uri "file:///docs/user_guide.md" + resource_name "user_guide" + mime_type "text/markdown" + end + + result = ResourceLink.new(resource_class).to_h + + assert_equal( + { uri: "file:///docs/user_guide.md", name: "user_guide", mimeType: "text/markdown", type: "resource_link" }, + result, + ) + end + end + class TextTest < ActiveSupport::TestCase test "#to_h returns correct format per MCP spec" do text = Text.new("hello")