YmerNode.Mcp.Tools.Helpers (Ymer Node v0.2.1)

Copy Markdown View Source

The four helpers a tool's action and format layers share, and nothing else.

Deliberately narrow. This is the node's first module of its kind, and a grab-bag named Helpers grows without anyone deciding it should: each function here earns its place by being needed by more than one tool layer and by having no home of its own. Wymcp offers no public equivalent of any of them.

Summary

Functions

Drops nil values and empty lists from a map — the absent-when-empty wire contract, so a response carries no keys whose only content is "nothing here".

Renders a changeset's errors as one string a caller can act on, with each message's interpolations already applied.

Copies params[param_key] into map under attr_key, but only when param_key is PRESENT in params.

The wrong-typed twin's answer: what a boundary action gives back when a present key holds a value of the wrong type — the key, what was expected, what arrived, and the verb the message is rendered under.

Functions

compact(map)

Drops nil values and empty lists from a map — the absent-when-empty wire contract, so a response carries no keys whose only content is "nothing here".

Examples

iex> YmerNode.Mcp.Tools.Helpers.compact(%{a: 1, b: nil, c: [], d: [1]})
%{a: 1, d: [1]}

A boolean is content, so a flag that is false stays — the write mark and existing depend on that:

iex> YmerNode.Mcp.Tools.Helpers.compact(%{write: false, existing: false})
%{existing: false, write: false}

format_changeset_errors(changeset)

Renders a changeset's errors as one string a caller can act on, with each message's interpolations already applied.

Total over whatever the changeset holds: a placeholder the opts do not name stays as written — %{count} rather than a bare count, so a lost opt is visible — and a named value with no String.Chars implementation renders through inspect/1 rather than raising.

maybe_put_if_present(map, params, param_key, attr_key)

Copies params[param_key] into map under attr_key, but only when param_key is PRESENT in params.

Presence, not truthiness, and that is the whole point: an explicit JSON null is a value the caller meant to send — clearing a description by passing null has to keep working — so a null travels through to the changeset, which is where its meaning is decided. Filtering nils here would silently turn "clear this field" into "leave it alone".

wrong_type(key, expected, got, verb)

The wrong-typed twin's answer: what a boundary action gives back when a present key holds a value of the wrong type — the key, what was expected, what arrived, and the verb the message is rendered under.

One shape across every tool, so each tool's Errors reads one map and a twin is one line at its head rather than a tuple spelled by hand.

Examples

iex> YmerNode.Mcp.Tools.Helpers.wrong_type("sql", "a string", 7, "run query")
{:error, {{:wrong_type, %{key: "sql", expected: "a string", got: 7}}, %{action_verb: "run query"}}}