Metadata-Version: 2.4
Name: splent_feature_mcp
Version: 0.1.2
Summary: SPLENT feature: the product as a Model Context Protocol (MCP) server for assistants
License-Expression: LGPL-3.0-or-later
Requires-Python: >=3.13
Description-Content-Type: text/markdown
License-File: COPYING
License-File: COPYING.LESSER
Dynamic: license-file

# splent_feature_mcp

The product as a [Model Context Protocol](https://modelcontextprotocol.io)
server. An assistant that speaks MCP (Claude, for example) connects to
`/mcp` with an API token of `splent_feature_apikeys` and uses the tools the
other features of the product declare, each as the account the token
belongs to: it sees what that person sees on the site, and nothing else.

The feature holds no tool of its own. A tool belongs to the feature whose
domain it reaches, the way an apikeys scope belongs to the feature whose
routes check it.

## Declaring tools in a feature

A feature declares its tools as data in `<feature>/mcp_tools.py`, a module
that imports nothing from this feature, so the feature does not depend on it
and the UVL model needs no constraint for it:

```python
from flask_babel import lazy_gettext as _l


def free_rooms(user, day):
    if not day:
        raise ValueError("Say which day.")
    return {"day": day, "rooms": ["A", "B"]}


MCP_TOOLS = [
    {
        "name": "rooms_free",
        "title": _l("Free rooms"),
        "description": "The rooms free on a day.",
        "input_schema": {
            "type": "object",
            "properties": {"day": {"type": "string", "description": "YYYY-MM-DD"}},
            "required": ["day"],
            "additionalProperties": False,
        },
        "scope": "rooms:read",
        "scope_label": _l("See the rooms and their bookings"),
        "read_only": True,
        "handler": free_rooms,
    },
]
```

- `name`: 1 to 64 letters, digits, `_` or `-`. Prefix it with the feature's
  name, so two features never claim the same one.
- `description`: what the assistant reads to decide when to call it. Plain
  English, precise about units, formats and time zones.
- `input_schema`: a JSON Schema object. The server checks what the tools
  here need of it before calling the handler: required properties, the type
  of each (an integer is never a boolean), `enum`, `minimum`, `maximum`,
  and unknown properties when `additionalProperties` is false.
- `handler(user, **arguments)`: called with the token's account and the
  arguments by name. It answers something JSON can carry; a dict also goes
  to the client as structured content. It raises `ValueError` with a message
  for the person to refuse what it was asked, and the assistant gets it as a
  tool error. Any other exception is logged and answered as a failure of the
  tool, without its details.
- `scope`: what the token has to carry. It is registered with apikeys from
  `scope_label` when the tool is found, so the token form offers it. A tool
  that only reads (`read_only` true, the default) registers a default
  scope. One that changes things registers a scope that writes, which a
  token only gets when someone asks for it, and which only an account that
  may act sensitively is granted; mark it `destructive` when it deletes or
  undoes, so the client asks before calling it.

The tools are found once per app, at its first request, among the features
that registered a blueprint. `registry.register_mcp_tool` declares one from
code, for a feature that does depend on this one.

## The endpoint

`POST /mcp`: JSON-RPC 2.0 over Streamable HTTP, answered as plain JSON in the
body of the same response. The server keeps no session and opens no stream,
so `GET` and `DELETE` are 405, and any worker of any process answers any
request. It answers `initialize` (protocol revisions 2025-06-18, 2025-03-26
and 2024-11-05), `ping`, `tools/list` and `tools/call`, and accepts any
notification with 202.

- Every request carries `Authorization: Bearer <token>`. Without a usable
  token the answer is apikeys' refusal, 401 or 403, with a
  `WWW-Authenticate: Bearer` challenge.
- A token lists and calls only the tools whose scope it carries.
- A request with an `Origin` header from another host is refused (403), so
  a page of another site cannot reach the endpoint through a visitor's
  network.

`/mcp/connect`, in the Developer section of the menu, tells a person how to
connect an assistant: the address, the token to create, the command for
Claude Code, and the tools the site offers.

```bash
claude mcp add --transport http <product>-mcp https://<host>/mcp \
  --header "Authorization: Bearer <token>"
```

## Configuration

| Variable           | Default | Meaning |
|--------------------|---------|---------|
| `MCP_TOOLS`        | empty   | The tools offered, comma separated. Empty offers every tool declared |
| `MCP_SERVER_NAME`  | empty   | The name the server gives itself. Empty is `<SPLENT_APP>-mcp` |
| `MCP_INSTRUCTIONS` | empty   | What the server tells the assistant when it connects |

A product that narrows apikeys' offer with `APIKEYS_SCOPES` lists the scopes
of the tools it wants to offer there too.

## Requires

`auth` and `apikeys`.
