# `Snodo.Skills.Client`
[🔗](https://github.com/joshrotenberg/snodo/blob/v0.5.0/lib/snodo/skills/client.ex#L1)

Client functions for the Skills extension (`io.modelcontextprotocol/skills`)
over a `Snodo.Client`.

Declare the extension when connecting, so a server that serves skills to
declaring clients only, as `Snodo.Skills` does, answers:

    {:ok, client} =
      Snodo.Client.connect({:http, url},
        client_capabilities: Snodo.Skills.Client.capabilities()
      )

    {:ok, [entry | _rest]} = Snodo.Skills.Client.list(client)
    {:ok, skill_md} = Snodo.Skills.Client.load(client, entry)

    {:ok, forms} =
      Snodo.Skills.Client.read(client, entry, "skill://pdf-processing/references/FORMS.md")

`list/2` and `get/3` only fetch entries; neither reads a skill file. A
host reads files when it needs them: `load/3` reads a skill's SKILL.md
and `read/4` any file the entry lists.

## Verification

Every read is checked against the entry before the content is returned:

  1. The URI must be listed in the entry's `"resources"`.
  2. The listed size must be at most `:max_file_bytes`, before anything is
     read.
  3. The content's byte length must equal the listed `"size"`.
  4. The SHA-256 digest of the bytes must equal the listed `"digest"`.
  5. For the skill's SKILL.md, the frontmatter is parsed with
     `Snodo.Skills.Frontmatter` and compared field by field with the
     entry's `"frontmatter"`. A field that is missing on one side and
     `null` on the other is the same.

An entry whose `"resources"` is `"dynamic"` publishes no digests, so steps
1 to 4 are skipped and only step 5 applies.

A read that fails a check returns `{:error, %Snodo.Error{}}` with code
-32000, kind `:extension`, and data `%{"uri" => uri, "reason" => reason}`,
where `reason` is one of `"not_listed"`, `"too_large"`, `"size_mismatch"`,
`"digest_mismatch"`, `"frontmatter_unreadable"`, `"frontmatter_mismatch"`
(data also has `"fields"`), `"missing_content"`, or `"malformed_entry"`.
The content is not returned, and nothing more is read.

## Options

Each function also takes the options of `Snodo.Client.request/4`, and:

  * `:max_file_bytes` - the largest listed file size `read/4` and `load/3`
    fetch. Defaults to 16,777,216 (16 MiB). The encoded response must also
    fit the transport's limit (`:max_response_bytes` over HTTP,
    `:max_line_bytes` over stdio, both 16 MiB by default), or the read
    fails with that transport error. A `Snodo.Skills` server with the
    default `:max_response_bytes` keeps each file's `"contents"` array
    within 16 MiB less 4,096 bytes on HTTP and stdio, so the response fits
    unless the rest of it is over those 4,096 bytes, which with any
    request id the core accepts happens only when the server's encoded
    `server_info` is over 2,408 − d bytes, d being the number of digits
    of the server's `resources/read` `ttlMs` (2,407 bytes for a `ttlMs`
    of 0 to 9). See the Skills limits in the transports guide.
  * `:frontmatter_parser` - a `t:Snodo.Skills.Frontmatter.parser/0` for
    reading SKILL.md frontmatter.
  * `:max_pages` - for `list/2`, the most `skills/list` pages to request.
    Defaults to the client's `:max_pages`. A listing that still has a
    `nextCursor`, or repeats one, returns a -32000 transport error.

# `capabilities`

```elixir
@spec capabilities() :: map()
```

Returns client capabilities that declare the Skills extension.

# `get`

```elixir
@spec get(Snodo.Client.t(), String.t(), keyword()) ::
  {:ok, map()} | {:error, Snodo.Error.t()}
```

Gets one skill's entry by the URI of its SKILL.md.

# `list`

```elixir
@spec list(Snodo.Client.t(), keyword()) :: {:ok, [map()]} | {:error, Snodo.Error.t()}
```

Lists every skill the server serves, following `nextCursor`. Returns the
skill entries.

# `load`

```elixir
@spec load(Snodo.Client.t(), map(), keyword()) ::
  {:ok, map()} | {:error, Snodo.Error.t()}
```

Reads and verifies a skill's SKILL.md, the first step of loading a skill.
Returns the resource content map the server sent.

# `read`

```elixir
@spec read(Snodo.Client.t(), map(), String.t(), keyword()) ::
  {:ok, map()} | {:error, Snodo.Error.t()}
```

Reads and verifies one file the skill entry lists. Returns the resource
content map the server sent.

# `verify`

```elixir
@spec verify(map(), String.t(), map(), keyword()) ::
  {:ok, map()} | {:error, Snodo.Error.t()}
```

Verifies a `resources/read` result for `uri` against a skill entry without
sending anything, as `read/4` does after reading. Returns the content map.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
