Snodo.Tool.GenServer (snodo v0.4.3)

Copy Markdown View Source

Generates MCP tools for explicitly declared GenServer calls and casts.

Import genserver_call/2 and genserver_cast/2 into a Snodo.Server module. Each declaration generates an ordinary Snodo.Tool module and registers it through the server DSL. The target is a fixed, registered module name; MCP clients cannot choose a process, message, or Elixir term.

defmodule CounterServer do
  use Snodo.Server, name: "counter", version: "1.0.0"
  import Snodo.Tool.GenServer

  genserver_call "counter_get",
    target: Counter,
    input_schema: %{"type" => "object", "additionalProperties" => false},
    message: :get,
    encode_reply: fn count -> %{"value" => count} end
end

:message may be a fixed term, a one-argument function receiving the validated string-keyed input map, or a two-argument function receiving that map and the request's Snodo.Context. The context carries the caller's auth, so a builder can decide by argument value as well as by tool:

genserver_call "peer_send",
  target: Mesh,
  input_schema: %{...},
  message: fn %{"peer" => peer} = args, context ->
    if peer in allowed_peers(context.auth),
      do: {:send, peer, args["body"]},
      else: {:error, Snodo.Error.authorization(-32_003, "Peer not allowed")}
  end,
  encode_reply: &%{"result" => &1}

A builder function that returns {:error, %Snodo.Error{}} sends nothing, and the request fails with that error. Calls require :encode_reply, a one-argument function that returns a JSON object. :timeout defaults to 5,000 milliseconds and must be a positive integer literal. The options are read from the macro's syntax at compile time, so they must be written as a literal keyword list; a variable or a composed list such as base_opts ++ [message: :get] fails compilation. Failed validation, message construction, unavailable targets, timeouts, and non-JSON replies become tool error results.

Input schemas may use the assertions enforced by Snodo.Schema.Validator.Basic and the annotations title, description, default, and x-mcp-header. Unsupported keywords fail compilation so a declaration cannot advertise a constraint that this adapter ignores.

A cast result means only that GenServer.cast/2 accepted the message for sending. An absent target is reported as a tool error, but the target can stop after the availability check. A successful cast does not establish that the target received or processed it.

Summary

Functions

Defines and registers a tool that sends a bounded GenServer.call/3.

Defines and registers a tool that sends GenServer.cast/2.

Functions

genserver_call(name, opts)

(macro)

Defines and registers a tool that sends a bounded GenServer.call/3.

genserver_cast(name, opts)

(macro)

Defines and registers a tool that sends GenServer.cast/2.