AshAi.McpResource (ash_ai v0.4.0)

View Source

An MCP resource to expose via the Model Context Protocol (MCP).

MCP resources provide LLMs with access to static or dynamic content like UI components, data files, or images. Unlike tools which perform actions, resources return content that the LLM can read and reference.

Example

defmodule MyApp.Blog do
  use Ash.Domain, extensions: [AshAi]

  mcp_resources do
    # Description inherited from :render_card action
    mcp_resource :post_card, "file://ui/post_card.html", Post, :render_card,
      mime_type: "text/html"

    # Custom description overrides action description
    mcp_resource :post_data, "file://data/post.json", Post, :to_json,
      description: "JSON metadata including author, tags, and timestamps",
      mime_type: "application/json"
  end
end

The action is called when an MCP client requests the resource, and its return value (which must be a string) is sent to the client with the specified MIME type.

Description Behavior

Resource descriptions default to the action's description. You can provide a custom description option in the DSL which takes precedence over the action description. This helps LLMs understand when to use each resource.

Summary

Types

t()

@type t() :: %AshAi.McpResource{
  __spark_metadata__: term(),
  action: atom() | Ash.Resource.Actions.Action.t(),
  description: String.t(),
  domain: module() | nil,
  mime_type: String.t(),
  name: atom(),
  resource: Ash.Resource.t(),
  title: String.t(),
  uri: String.t()
}