# Stashr auth.md

You are an agent. This document explains how to obtain a credential for Stashr and how to use it against the REST API at https://stashr.me/v1 and the MCP server at https://stashr.me/mcp on the same origin. Follow the steps in order.

Stashr does not offer unattended agent registration today. `agent_auth.identity_types_supported` is empty: there is no identity endpoint, no anonymous or verified-email registration, and no ID-JAG exchange. Do not POST to a registration endpoint. Every credential is issued by a signed-in human, either through an interactive OAuth 2.1 consent screen (Step 3) or by creating an API key in Settings (Step 4). This document conforms to the [auth.md protocol](https://github.com/workos/auth.md); the block will list new methods the day they ship.

Real hosts:

- REST API resource server: https://stashr.me/v1 ([reference](https://stashr.me/docs/api), [OpenAPI](https://stashr.me/openapi.json))
- MCP resource server: https://stashr.me/mcp ([docs](https://stashr.me/docs/mcp), [server card](https://stashr.me/.well-known/mcp/server-card.json))
- OAuth 2.1 authorization server (issuer): https://stashr.me/api/auth 

## Step 1: Discover

An unauthenticated request to either resource server returns a 401 whose `WWW-Authenticate` header names the Protected Resource Metadata:

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://stashr.me/.well-known/oauth-protected-resource/v1"
```

### 1a. Protected Resource Metadata (RFC 9728)

- REST API: GET https://stashr.me/.well-known/oauth-protected-resource/v1
- MCP server: GET https://stashr.me/.well-known/oauth-protected-resource/mcp
- Origin: GET https://stashr.me/.well-known/oauth-protected-resource (describes https://stashr.me as a whole; use the two above for the actual resource servers)

Both name https://stashr.me/api/auth as the authorization server, list the scopes `read`, `write`, `destructive`, `offline_access`, and `bearer_methods_supported: ["header"]`.

### 1b. Authorization Server Metadata (RFC 8414)

```http
GET https://stashr.me/.well-known/oauth-authorization-server/api/auth
```

The same document is also served at https://stashr.me/.well-known/oauth-authorization-server and carries the standard endpoints (`authorization_endpoint`, `token_endpoint`, `registration_endpoint`, `revocation_endpoint`, `introspection_endpoint`, `jwks_uri`), `code_challenge_methods_supported: ["S256"]`, `client_id_metadata_document_supported: true`, and this block:

```json
{
  "agent_auth": {
    "skill": "https://stashr.me/auth.md",
    "identity_types_supported": []
  }
}
```

## Step 2: Pick a method

1. You can open a browser for the user (desktop app, IDE, chat client, CLI on a workstation): use OAuth, Step 3. Every hosted MCP client (Claude, ChatGPT, Cursor, Codex) does this automatically when pointed at the MCP server URL above.
2. You run headless (CI, cron, a server-side agent) or cannot show the user a URL: use an API key, Step 4.
3. You hold an ID-JAG, a verified-email assertion, or want anonymous registration: not supported. Fall back to 1 or 2.

## Step 3: OAuth 2.1 (interactive)

### 3a. Identify your client

If your client publishes a Client ID Metadata Document, use its HTTPS URL as `client_id` and skip registration (`client_id_metadata_document_supported: true`). Otherwise, dynamic client registration (RFC 7591) is open and needs no initial access token. Public clients with PKCE are the normal case: send `token_endpoint_auth_method: "none"`, because a registration that omits it creates a confidential client and returns a `client_secret` you must then use.

```http
POST https://stashr.me/api/auth/oauth2/register
Content-Type: application/json

{
  "client_name": "Your agent, named honestly",
  "redirect_uris": ["http://127.0.0.1:PORT/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none",
  "scope": "read write offline_access"
}
```

Persist the returned `client_id`. `client_name` is what the user sees on the consent screen, so name the agent honestly. Requested scopes must be a subset of `read`, `write`, `destructive`, `offline_access`.

### 3b. Send the user to authorize

Build the URL and hand it to the user; the state and PKCE verifier stay with you.

```http
GET https://stashr.me/api/auth/oauth2/authorize
  ?response_type=code
  &client_id=<client_id>
  &redirect_uri=<redirect_uri>
  &scope=read%20write%20offline_access
  &state=<random>
  &code_challenge=<S256 challenge>
  &code_challenge_method=S256
  &resource=https://stashr.me/v1
```

`resource` (RFC 8707) binds the access token to one resource server: https://stashr.me/v1 for the REST API or https://stashr.me/mcp for MCP. A token minted for one is rejected by the other. The user signs in to Stashr (Google or a passwordless email code) and approves the requested scopes. Ask for `offline_access` only if you need a refresh token, and for `destructive` only if the user has asked for collection deletion.

### 3c. Exchange the code

```http
POST https://stashr.me/api/auth/oauth2/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=<code>&redirect_uri=<redirect_uri>&client_id=<client_id>&code_verifier=<verifier>&resource=https://stashr.me/v1
```

Response: a JWT `access_token` valid for one hour, the granted `scope`, and a `refresh_token` valid for 30 days when `offline_access` was granted. Refresh with `grant_type=refresh_token` at the same endpoint. Tokens are signed by the keys at https://stashr.me/api/auth/jwks and carry an `iss` claim equal to the issuer above.

## Step 4: API key (headless)

The user creates the key at [Settings, API keys](https://stashr.me/settings/api-keys). Keys start with `stashr_`, are shown once, and carry one access level:

| Level | Scopes | Use for |
| --- | --- | --- |
| Read | `read` | search and read the library |
| Write | `read`, `write` | plus saving, editing, tagging, archiving, restoring |
| Full | `read`, `write`, `destructive` | plus collection deletion |

Ask the user to place the key in your secret store or the `STASHR_API_KEY` environment variable. Never ask them to paste it into chat, and never log it. The official CLI (`npm install --global @stashr/cli`) reads the same variable, or runs the OAuth flow above with `stashr login`.

## Step 5: Use the credential

Send it as a bearer token on every request:

```http
GET https://stashr.me/v1/me
Authorization: Bearer <access_token or stashr_ key>
```

GET https://stashr.me/v1/me confirms the connection and works on every plan. Required scope follows the HTTP method: `GET` needs `read`, `POST` and `PATCH` need `write`, `DELETE` needs `destructive`. On MCP, `ping`, `tools/list`, and the results-view template (`resources/list`, `resources/read`) need no credential; `initialize` and `tools/call` return the 401 challenge that starts OAuth.

Errors are always JSON with a stable `error.code`:

| Status | `error.code` | What to do |
| --- | --- | --- |
| 401 | `unauthorized` | Credential missing, invalid, expired, or bound to the other resource. OAuth: refresh once, then restart at Step 3. API key: stop and tell the user. |
| 402 | `payment_required` | The account's trial ended without a subscription. Send the user to https://stashr.me/pricing and do not retry. |
| 403 | `forbidden` | The credential lacks the scope. Re-authorize with the scope or ask for a key with a higher level. |
| 429 | `rate_limited` | Wait `Retry-After` seconds. Limits per account: 240 requests per minute for OAuth tokens, 300 for API keys. Every response carries IETF `RateLimit` headers. |

Every account starts a 14-day trial with no card; the API and MCP server are included in the trial and in Pro. Sign-up is at https://stashr.me/signup and is a human step.

## Revocation

- OAuth: the user revokes a client at [Settings, AI & apps](https://stashr.me/settings/authorized-apps); an issued access token keeps working for up to one hour. Confidential clients can also POST to `revocation_endpoint` (RFC 7009).
- API keys: the user deletes the key at [Settings, API keys](https://stashr.me/settings/api-keys); it stops working immediately.
- On a 401 for a previously working credential, drop it and go back to Step 2. Do not keep retrying.

## Related

- [Agent skill (SKILL.md)](https://stashr.me/docs/agent-skill): the workflow to follow once you are authenticated
- [llms.txt](https://stashr.me/llms.txt): product overview and documentation index
- [API catalog](https://stashr.me/.well-known/api-catalog): every API on this origin
