# Marqly Authentication & Scoped Permissions

> OAuth 2.1 + PKCE (`S256`) authentication guide and declared permission scopes for Marqly MCP and API integrations.

## 1. Discover

Send an unauthenticated request to `https://mcp.marqly.com/ai/mcp`. Marqly responds with HTTP `401` and a `WWW-Authenticate` header pointing to RFC 9728 Protected Resource Metadata:

```http
WWW-Authenticate: Bearer realm="marqly-mcp", resource_metadata="https://mcp.marqly.com/.well-known/oauth-protected-resource/ai/mcp"
```

Fetch both discovery documents:
- **Protected Resource Metadata (`oauth-protected-resource`, RFC 9728):** `https://mcp.marqly.com/.well-known/oauth-protected-resource/ai/mcp`
- **Authorization Server Metadata (`oauth-authorization-server`, RFC 8414):** `https://api.marqly.com/.well-known/oauth-authorization-server`
- **OpenAPI 3.1 Security Schemes:** `https://www.marqly.com/openapi.json`

## 2. Pick a method

Marqly supports **OAuth 2.1 Authorization Code flow with mandatory PKCE (`S256`)** for public clients (`token_endpoint_auth_method: "none"`). Static API keys are not required.

- Use **read-only scopes** (`bookmarks:read`, `boards:read`, `tags:read`, `highlights:read`, `account:read`, `conversations`) for search and retrieval agents.
- Request **write scopes** (`bookmarks:write`, `boards:write`, `tags:write`, `highlights:write`) only when the user explicitly wants the agent to create, tag, move, or soft-delete items.

## 3. Register

Register a dynamic OAuth client (RFC 7591) at `POST https://api.marqly.com/oauth/register` with `token_endpoint_auth_method: "none"`, `grant_types: ["authorization_code", "refresh_token"]`, and your `redirect_uris`.

## 4. Claim

Generate a PKCE `code_verifier` and `S256` `code_challenge`, then direct the user to:

```text
https://api.marqly.com/oauth/authorize?response_type=code&client_id=<client_id>&redirect_uri=<redirect_uri>&code_challenge=<challenge>&code_challenge_method=S256&scope=bookmarks:read%20boards:read%20tags:read%20highlights:read%20account:read
```

The user approves the requested scopes in their browser and is redirected back with `?code=<authorization_code>`.

## 5. Exchange

Exchange the authorization code for an `access_token` and `refresh_token` at `POST https://api.marqly.com/oauth/token` with `grant_type=authorization_code`, `code`, `redirect_uri`, `client_id`, and `code_verifier`. Refresh expired tokens using `grant_type=refresh_token`.

## 6. Use the access_token

Send the Bearer token in the `Authorization` header on every MCP request to `https://mcp.marqly.com/ai/mcp`:

```http
Authorization: Bearer <access_token>
Content-Type: application/json
```

### Declared OAuth Scopes (13)

- `bookmarks:read` — Search and read saved bookmarks, summaries, and full content
- `bookmarks:write` — Create, update, tag, move, and soft-delete bookmarks
- `boards:read` — List and inspect boards
- `boards:write` — Create, update, merge, and soft-delete boards
- `tags:read` — List and inspect tags
- `tags:write` — Rename, merge, and delete tags
- `highlights:read` — Search and read webpage highlights and notes
- `highlights:write` — Create, update, and delete webpage highlights
- `account:read` — Read authenticated user profile and plan status
- `bookmarks` — Combined read/write access to bookmarks
- `boards` — Combined read/write access to boards
- `tags` — Combined read/write access to tags
- `conversations` — Search and read saved ChatVault AI conversations

## 7. Errors

Error responses return structured JSON (`application/problem+json` or `application/json`) with `status`, `code`, `title`, `detail`, and `retryable`:
- `401 Unauthorized` — Missing, invalid, or expired token; re-read `WWW-Authenticate` and refresh or re-authorize.
- `403 Forbidden` — Insufficient OAuth scope or per-app write toggle is disabled in Marqly Connected Apps.
- `429 Too Many Requests` — Rate limit exceeded; inspect rate-limit headers and retry with exponential backoff.

## 8. Revocation

Revoke access or refresh tokens programmatically via `POST https://api.marqly.com/oauth/revoke`, or revoke the connected app with one click inside Marqly Connected Apps settings.
