Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions docs/_server/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/mcp/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
67 changes: 67 additions & 0 deletions test/mcp/content_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down