Ectomancer.RouteIntrospection (Ectomancer v2.0.0)

Copy Markdown View Source

Phoenix route introspection for Ectomancer.

This module requires Plug to be available at runtime for route execution.

Summary

Functions

Builds a tool name from a route.

Exposes Phoenix routes as MCP tools.

Filters routes based on options.

Extracts routes from a Phoenix router module.

Parses a Phoenix route path and extracts parameters.

Functions

build_tool_name(route)

@spec build_tool_name({String.t(), String.t(), module(), atom()}) :: atom()

Builds a tool name from a route.

build_tool_name(arg, namespace)

@spec build_tool_name({String.t(), String.t(), module(), atom()}, atom() | nil) ::
  atom()

expose_routes(router_module, opts \\ [])

(macro)

Exposes Phoenix routes as MCP tools.

Authorization

Mutating routes (POST, PUT, PATCH, DELETE, and catch-all *) require effective authorization. At compile time expose_routes/2 raises if a mutating route is exposed without an :authorize option and without an explicit public opt-out (authorize: :none/:public). Authorization inherited from use Ectomancer, authorize: ... satisfies the guard. Read-only (GET) routes are always allowed; restrict exposure to them with methods: ["GET"].

expose_routes MyAppWeb.Router, authorize: fn actor, _action -> actor.role == :admin end

# Explicit public opt-in (mutating routes allowed)
expose_routes MyAppWeb.Router, authorize: :none

Options

  • :only - Only expose matching paths
  • :except - Skip matching paths
  • :methods - Only expose matching HTTP methods
  • :namespace - Prefix tool names with a namespace
  • :authorize - Authorization handler (function, policy module, or :none/:public)

filter_routes(routes, opts)

@spec filter_routes(
  [{String.t(), String.t(), module(), atom()}],
  keyword()
) :: [{String.t(), String.t(), module(), atom()}]

Filters routes based on options.

get_routes(router_module)

@spec get_routes(module()) :: [{String.t(), String.t(), module(), atom()}]

Extracts routes from a Phoenix router module.

parse_path_params(path)

@spec parse_path_params(String.t()) :: {String.t(), [{atom(), :param | :glob}]}

Parses a Phoenix route path and extracts parameters.