Metadata-Version: 2.5
Name: esaalat-mcp
Version: 0.1.6
Summary: Model Context Protocol server for Esaalat merchant accounts
Project-URL: Homepage, https://esaalat.com
Project-URL: Documentation, https://github.com/Esaalat/esaalat-mcp#readme
Project-URL: Source, https://github.com/Esaalat/esaalat-mcp
Project-URL: Issues, https://github.com/Esaalat/esaalat-mcp/issues
License: Proprietary
Keywords: checkout,esaalat,mcp,model-context-protocol,payments,yemen
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Office/Business :: Financial
Requires-Python: >=3.10
Requires-Dist: httpx>=0.27
Requires-Dist: mcp<3,>=2.3
Provides-Extra: dev
Requires-Dist: pytest>=8; extra == 'dev'
Description-Content-Type: text/markdown

# esaalat-mcp

Local Model Context Protocol server for Esaalat. It runs on the merchant's own
machine (stdio) and calls the public Esaalat API with a personal access token —
it holds no business logic and never imports Django.

Run it with:

```bash
ESAALAT_API_TOKEN=esa_... uvx esaalat-mcp
```

`uvx` fetches the published package (Python 3.10 or newer) on first run and
keeps it in its own cache; there is nothing to install. From a checkout,
`uvx --from . esaalat-mcp` runs the working copy instead.

Create the token in the dashboard under **Connect AI**. Setup for Claude
Desktop, Claude Code, Cursor and VS Code — plus every tool it exposes — lives
in [`../MCP.md`](../MCP.md).

## Streamable HTTP

The same tools over HTTP, for clients that cannot launch a local process:

```bash
uvx --from esaalat-mcp esaalat-mcp-http
```

It serves `/mcp` (Streamable HTTP), `/healthz` and the RFC 9728
protected-resource metadata at `/.well-known/oauth-protected-resource`. The last
two are public; everything under `/mcp` requires an OAuth access token. The
production deployment (nginx, systemd, probe) is in
[`deploy/`](deploy/README.md); the shared API/MCP settings are in
[`../docs/MCP-ENVIRONMENT.md`](../docs/MCP-ENVIRONMENT.md). A
missing, malformed, unsigned, badly signed, expired or wrongly issued token gets
`401` with `WWW-Authenticate: Bearer resource_metadata="…"`; a valid token
without the required scope gets `403`.

The token is also the merchant. Tools call `api.esaalat.com` with the bearer
token that arrived with the request, so this process needs no
`ESAALAT_API_TOKEN` and two merchants on one server never share credentials.

It binds `127.0.0.1:8000` unless `ESAALAT_MCP_HOST` / `ESAALAT_MCP_PORT` say
otherwise. `ESAALAT_MCP_ALLOWED_HOSTS` and `ESAALAT_MCP_ALLOWED_ORIGINS` extend
the Host/Origin allowlist when it runs behind a proxy.

| Variable | Default | Meaning |
|---|---|---|
| `ESAALAT_MCP_RESOURCE` | `http://127.0.0.1:8000` | Canonical resource identifier, and the origin the metadata is served from (`https://mcp.esaalat.com` in production) |
| `ESAALAT_OAUTH_ISSUER` | `https://api.esaalat.com` | `iss` a token must carry; also advertised as the authorization server |
| `ESAALAT_OAUTH_JWKS_URI` | `<issuer>/oauth/jwks.json` | Where signing keys are published |
| `ESAALAT_OAUTH_AUDIENCE` | the resource identifier | `aud` a token must carry |
| `ESAALAT_MCP_REQUIRED_SCOPE` | `read` | Scope every request must grant |
| `ESAALAT_MCP_JWKS_TTL` | `300` | Seconds a fetched key set is reused |
| `ESAALAT_MCP_JWKS_MIN_REFRESH` | `30` | Back-off after a key id nobody publishes |
| `ESAALAT_MCP_OAUTH_TIMEOUT` | `5` | JWKS fetch timeout, seconds |
| `ESAALAT_MCP_MAX_CLIENTS` | `32` | Maximum merchant HTTP clients retained in memory |
| `ESAALAT_MCP_CLIENT_TTL` | `180` | Seconds an idle merchant HTTP client is reused |

## Operating the HTTP transport

- **Rate limit.** 120 requests per minute per verified `sub`, sliding window.
  Set `ESAALAT_MCP_RATE_LIMIT` to change it (`0` disables it). A refused call
  is `429` with `Retry-After`; it does not consume budget, so retrying cannot
  extend the wait. The counter is in-process: with multiple replicas, each
  worker enforces its own budget unless the limit is disabled or moved to a
  shared store.
- **Request log.** One JSON line per `/mcp` request on the
  `esaalat_mcp.http` logger: `request_id`, `sub`, tool name, scope, status and
  duration. The request body and the token are never logged; at most 64 KiB of
  the body is buffered to read the tool name. An incoming `X-Request-ID` is
  echoed; otherwise one is generated and returned.
- **Upstream budget.** `ESAALAT_TIMEOUT` is the per-call budget (default 30s).
  A timeout reaches the assistant as a `504` message, and a `429` from the API
  stays a `429` with `retry_after` under `_meta["esaalat/upstream"]`.
- **Memory.** The per-merchant client cache is bounded to 32 entries for 180
  seconds by default, and each client has a 10-connection pool with five
  keep-alive connections. The systemd unit also caps the process at 256 MiB;
  lower `ESAALAT_MCP_MAX_CLIENTS` if the host has less headroom.
- **Stateless mode.** Set `ESAALAT_MCP_STATELESS_HTTP=1` when running more than
  one replica behind a load balancer. The MCP session is then carried by the
  client's token and each request can land on a different worker; leave it off
  for a single process, where the default session handling is cheaper.
- **Audit.** OAuth write calls create an `MCPAuditLog` row linked to the grant
  (personal tokens link to the token). Reads are not audited; re-consent and
  revocation are enforced on every call by the grant row.

## Widgets

The four MCP Apps pages are plain HTML with shared CSS/JS inlined; there is no
runtime bundler and no network dependency except the payment link's QR image.
Edit `widgets/src/*` and build:

```bash
cd widgets && node build.mjs
```

That writes `src/esaalat_mcp/ui_assets/*.html`, which the server serves as four
versioned resources:

| Resource | Opened by |
|---|---|
| `ui://esaalat/payment-link/v1.html` | `create_payment_link` |
| `ui://esaalat/payment-tracker/v1.html` | `get_payment` |
| `ui://esaalat/unmatched/v1.html` | `list_unmatched_payments` |
| `ui://esaalat/device-code/v1.html` | `create_device` |

Each tool result carries `_meta.ui = {resourceUri, csp}`. The CSP is empty
except for the payment link, which allows the QR image host.

### Where the other half lives

The authorization server is `be/ypg/esaalat/apps/mcp_oauth`, and the API accepts
the tokens it mints through `MCPOAuthTokenAuthentication`. That authenticator
re-checks the grant on every request, so disconnecting the app stops the next
call even though the access token itself lives for an hour.

The consent page is backed by the dashboard's JWT consent endpoint, so a
signed-in merchant can approve the app without a second login. The hosted
deployment contract lives in `../docs/MCP-ENVIRONMENT.md`; the review-facing
assets, prompts and screenshots are in `submission/README.md`.
