ExMCP.DSL.Resource (ex_mcp v0.9.2)

View Source

Simplified DSL for defining MCP resources.

Provides the defresource macro for defining resources with metadata and MIME types.

Summary

Functions

Validates a resource definition at compile time.

Sets annotations for the current resource.

Defines a resource with its URI and metadata.

Extracts variables from a URI based on a template pattern.

Sets icons for the current resource (new in 2025-11-25).

Marks the resource as a list pattern (contains wildcards like *).

Sets the MIME type for the current resource.

Sets the expected size of the resource content in bytes.

Marks the resource as subscribable for change notifications.

Checks if a URI matches a resource pattern.

Functions

__validate_resource_definition__(uri, meta)

Validates a resource definition at compile time.

This function is called during the defresource macro expansion to ensure the resource definition is complete and valid.

annotations(annotations)

(macro)

Sets annotations for the current resource.

defresource(uri, list)

(macro)

Defines a resource with its URI and metadata.

Examples

defresource "config://app/settings" do
  meta do
    name "Application Settings"
    description "Current application configuration"
  end

  mime_type "application/json"
  annotations %{
    audience: ["admin"],
    priority: 0.8
  }
end

defresource "file://logs/*.log" do
  meta do
    name "Log Files"
    description "Application log files"
  end

  mime_type "text/plain"
  list_pattern true
  subscribable true
end

extract_variables(uri, template)

Extracts variables from a URI based on a template pattern.

Templates use {variable_name} syntax to indicate variable segments. Returns a map of variable names to their values from the URI.

Examples

iex> ExMCP.DSL.Resource.extract_variables("repos/octocat/hello/issues/42", "repos/{owner}/{repo}/issues/{id}")
%{"owner" => "octocat", "repo" => "hello", "id" => "42"}

iex> ExMCP.DSL.Resource.extract_variables("api/v2/users/123", "api/v{version}/users/{id}")
%{"version" => "2", "id" => "123"}

iex> ExMCP.DSL.Resource.extract_variables("static/path", "static/path")
%{}

icons(icon_list)

(macro)

Sets icons for the current resource (new in 2025-11-25).

list_pattern(enabled)

(macro)

Marks the resource as a list pattern (contains wildcards like *).

mime_type(type)

(macro)

Sets the MIME type for the current resource.

size(bytes)

(macro)

Sets the expected size of the resource content in bytes.

subscribable(enabled)

(macro)

Marks the resource as subscribable for change notifications.

uri_matches?(uri, pattern)

Checks if a URI matches a resource pattern.

Supports glob-style patterns with * wildcard.

Examples

iex> ExMCP.DSL.Resource.uri_matches?("file://logs/app.log", "file://logs/*.log")
true

iex> ExMCP.DSL.Resource.uri_matches?("file://data/config.json", "file://logs/*.log")
false