# Lune auth.md

> How MCP clients and connectors authenticate to Lune's MCP server. People add Lune to their AI app as a connector or plugin, or add the MCP server to a local agent. This document is for the software doing the connecting.

Lune gives AI agents research tools: paper search, full text, citation trails, comparison tables, claim checks, and curated research guidance. One MCP server serves them all. It accepts two credentials: an OAuth access token or an access key. Human-readable docs for the same setup are at https://luneresearch.com/docs/mcp.

## At a glance

| Item                                     | Value                                                            |
| ---------------------------------------- | ---------------------------------------------------------------- |
| MCP server                               | `https://mcp.luneresearch.com` (older installs use `/mcp` or `/v1/mcp`)        |
| Transport                                | Streamable HTTP, stateless, `POST` only                          |
| Protocol versions                        | 2026-07-28, 2025-11-25, 2025-06-18, 2025-03-26, 2024-11-05       |
| Credential header                        | `Authorization: Bearer <OAuth access token or lune_ access key>` |
| Protected resource metadata (RFC 9728)   | `https://mcp.luneresearch.com/.well-known/oauth-protected-resource`            |
| Issuer                                   | `https://api.luneresearch.com`                                                 |
| Authorization server metadata (RFC 8414) | `https://api.luneresearch.com/.well-known/oauth-authorization-server`          |
| Registration endpoint (RFC 7591)         | `https://api.luneresearch.com/oauth/register`                                  |
| Authorization endpoint                   | `https://api.luneresearch.com/oauth/authorize`                                 |
| Token endpoint                           | `https://api.luneresearch.com/oauth/token`                                     |
| Revocation endpoint (RFC 7009)           | `https://api.luneresearch.com/oauth/revoke`                                    |
| JWKS                                     | `https://api.luneresearch.com/.well-known/jwks.json`                           |
| Scopes to request                        | `papers:read guidance:read account:read`                         |
| Resource indicator (RFC 8707)            | `https://mcp.luneresearch.com`                                                 |
| Access keys                              | `https://luneresearch.com/dashboard/settings/credentials`                 |
| Local stdio package                      | `@retrograde-labs/lune-mcp-server`                               |
| MCP Registry manifest                    | `https://mcp.luneresearch.com/.well-known/mcp/server.json`                     |

## Choose a credential

- **OAuth 2.1 with PKCE:** for connectors and any client that can open a browser. The user approves once. The client gets a one-hour access token and a rotating refresh token.
- **Access key:** for clients that can only send a fixed header, local stdio servers, and CI. The user creates it in the dashboard and pastes it into the client. It starts with `lune_`.

Once a client has one, it sends it in the same header on every MCP request:

```http
Authorization: Bearer <credential>
```

Discovery (`initialize`, `server/discover`, `tools/list`, `prompts/list` and `resources/list`) is answered without one, so an agent can see the tools before the user signs in. Running a tool needs the credential.

## OAuth discovery

Start from the MCP server URL, not from hard-coded OAuth URLs. A tool call (`tools/call`) without a credential returns `401` with a pointer to the resource metadata, which is also served at the well-known path below:

```http
WWW-Authenticate: Bearer resource_metadata="https://mcp.luneresearch.com/.well-known/oauth-protected-resource", scope="papers:read guidance:read account:read"
```

Fetch that document, read `authorization_servers[0]`, and fetch the authorization server's metadata:

```bash
curl -sS https://mcp.luneresearch.com/.well-known/oauth-protected-resource
curl -sS https://api.luneresearch.com/.well-known/oauth-authorization-server
```

The pointer follows the address the client called. A client on `https://mcp.luneresearch.com/mcp` is pointed at `https://mcp.luneresearch.com/.well-known/oauth-protected-resource/mcp`, whose `resource` is `https://mcp.luneresearch.com/mcp`. Use the `resource` value from the document you fetched; the examples below use the canonical `https://mcp.luneresearch.com`.

Ignore `https://api.luneresearch.com/.well-known/oauth-protected-resource`: it describes the API, and the MCP server refuses a token bound to its resource.

There is no OpenID Connect: `/.well-known/openid-configuration` returns `404`, and Lune issues no ID token.

Most connectors run the rest of this flow on their own. The steps below are for client implementers.

## OAuth flow

Clients are public. There is no client secret, and the token endpoint's auth method is `none`. PKCE with `S256` is required.

### 1. Register the client

Dynamic Client Registration is the only way to get a `client_id`.

```bash
curl -sS -X POST https://api.luneresearch.com/oauth/register \
  -H "Content-Type: application/json" \
  -d '{
    "client_name": "My MCP Client",
    "redirect_uris": ["http://127.0.0.1:13373/callback"],
    "grant_types": ["authorization_code", "refresh_token"],
    "scope": "papers:read guidance:read account:read"
  }'
```

The `201` response carries a `client_id` that starts with `lune_oauth_`, plus `token_endpoint_auth_method: "none"`. Redirect URIs must be one of:

- `https` on any host
- `http` on `127.0.0.1`, `[::1]`, or `localhost` only
- a private-use scheme such as `cursor://` or `vscode://`

The authorization request must use a registered URI exactly, except that a registered `http` loopback URI matches on any port (RFC 8252 section 7.3).

A URI with a fragment (`#`), or with a scheme such as `javascript:` or `data:`, is refused with `400` and `"error": "invalid_redirect_uri"`. A request without `redirect_uris` fails with `422`, and an empty list with `400`. Native clients should register a loopback URI and listen there for one callback.

### 2. Send the user to authorize

Generate a high-entropy `code_verifier` and derive its challenge:

```text
code_challenge = BASE64URL(SHA256(code_verifier))
```

Open this URL (one line):

```text
https://api.luneresearch.com/oauth/authorize
  ?response_type=code
  &client_id=<client_id>
  &redirect_uri=http://127.0.0.1:13373/callback
  &scope=papers:read%20guidance:read%20account:read
  &resource=https://mcp.luneresearch.com
  &state=<random_state>
  &code_challenge=<code_challenge>
  &code_challenge_method=S256
```

The server requires every parameter except `scope`, `resource` and `state`. A request with no `scope`, or with no scope Lune supports, asks for `papers:read guidance:read account:read`. Send `resource` and `state` anyway: `resource` binds the token to the MCP server (see step 3), and `state` lets you match the callback to this request. `redirect_uri` must match a registered URI exactly (a registered `http` loopback URI may use any port), or the request fails with `400` and no redirect. The user signs in on Lune's site, sees the requested scopes, and approves. Scopes Lune doesn't support are dropped before the consent screen.

The callback receives `code`, `iss=https://api.luneresearch.com`, and the `state` you sent. Check both before using the code. A denial arrives as `error=access_denied` with the same `state` and `iss`.

An authorization code lasts 10 minutes and works once. Every OAuth grant belongs to the user's personal team, so that team's plan, credits, and settings apply to every call made with the token.

### 3. Exchange the code

The token endpoint takes form-encoded bodies with `client_id` in the body. It doesn't read HTTP Basic credentials, and a JSON body gets `422`.

```bash
curl -sS -X POST https://api.luneresearch.com/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d grant_type=authorization_code \
  -d code=<code> \
  -d client_id=<client_id> \
  -d redirect_uri=http://127.0.0.1:13373/callback \
  -d code_verifier=<code_verifier> \
  -d resource=https://mcp.luneresearch.com
```

The response:

```json
{
  "access_token": "<RS256 JWT>",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "papers:read guidance:read account:read",
  "refresh_token": "<opaque token>"
}
```

Send `resource=https://mcp.luneresearch.com` on both the authorization request and this call. It sets the access token's audience to the MCP server. A token request that leaves `resource` out inherits it from the authorization request; if neither carries it, the audience is the `client_id`. Current MCP protocol versions reject such a token, and older versions accept it only for now. A `resource` that differs from the one on the authorization request fails with `invalid_target`.

Call the MCP server with `Authorization: Bearer <access_token>`.

### 4. Refresh the connection

```bash
curl -sS -X POST https://api.luneresearch.com/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d grant_type=refresh_token \
  -d refresh_token=<refresh_token> \
  -d client_id=<client_id> \
  -d resource=https://mcp.luneresearch.com
```

The response holds a new access token and a new refresh token. Replace the stored refresh token atomically after every successful refresh, because the one you just used is spent. Presenting a spent refresh token again fails with `invalid_grant`. If more than 30 seconds have passed since it was spent, Lune also revokes every token in its chain, and the user has to connect again; the 30-second window keeps two racing refreshes from ending the connection. Each new refresh token is valid for 90 days, so a connector in regular use stays connected, while one left idle for 90 days has to sign in again.

### 5. Revoke the connection

```bash
printf 'token=%s' "$REFRESH_TOKEN" |
  curl -sS -X POST https://api.luneresearch.com/oauth/revoke --data @- -d client_id=<client_id>
```

Revoking a refresh token ends its whole chain, and the endpoint answers `200` whether or not it knew the token. Posting an access token instead changes nothing. An access token that was already issued keeps working until it expires, at most one hour later. Users can also revoke a connection on the dashboard's Credentials page, under **OAuth clients**.

### Token endpoint errors

A form-encoded request that fails comes back as `400` with a flat body, `{"error": "...", "error_description": "..."}`. A body that isn't form-encoded, or that lacks `grant_type`, gets `422` with a `{"detail": [...]}` list instead.

| `error`                  | Cause                                                                                                                                                            | What to do                                                                        |
| ------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| `invalid_request`        | A required field is missing                                                                                                                                      | Send `code`, `client_id`, `redirect_uri`, and `code_verifier`, or `refresh_token` |
| `invalid_grant`          | The code expired, was already used, or doesn't match the `client_id`, `redirect_uri`, or PKCE verifier; or the refresh token expired, was revoked, or was reused | Restart the authorization                                                         |
| `invalid_target`         | `resource` differs from the value on the authorization request or on the refresh token                                                                           | Send the same `resource` every time                                               |
| `unsupported_grant_type` | `grant_type` is neither `authorization_code` nor `refresh_token`                                                                                                 | Use one of the two                                                                |

## Access key flow

The user signs in at `https://luneresearch.com/dashboard/settings/credentials`, opens the **API keys** tab, clicks **New key**, and copies the key, which is shown once. Keys look like this:

```text
lune_xxxxxxxxxxxxxxxxxxxxxxxx
```

The exact pattern is `^lune_[A-Za-z0-9]{24}$`. A key created in the dashboard carries `papers:read`, `guidance:read`, `subs:rw`, and `account:read`, and doesn't expire. The Lune command line app can create keys with other scopes or an expiry: `lune keys create --name <name> --scopes ... --expires ...`, where `--name` is required. A key bills the team it was created in. No key can create, list, or revoke other keys. A revoked key stops working at once.

Send a key to the remote server like any bearer credential. These examples read it from `LUNE_API_KEY`, set from your password manager or a file only you can read:

```bash
printf 'Authorization: Bearer %s\n' "$LUNE_API_KEY" |
  curl -sS https://mcp.luneresearch.com \
    -H @- \
    -H "Content-Type: application/json" \
    -H "Accept: application/json, text/event-stream" \
    -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
```

The key reaches curl on its input, never its command line, which the shell's history and every local user's process list keep.

A request without an `MCP-Protocol-Version` header is served as 2025-era, so the answer arrives as `text/event-stream`: one `event: message` whose `data:` line is the JSON-RPC response. A 2026-07-28 request needs the `_meta` envelope and an `Mcp-Method` header, and is answered as `application/json`.

Or run the local stdio server, which reads the same variable and needs Node.js 22 or later:

```bash
npx -y @retrograde-labs/lune-mcp-server
```

The [Install page](https://luneresearch.com/dashboard/install) has copy-paste configuration for each supported client.

## Scopes

Request exactly `papers:read guidance:read account:read`. Together they cover every MCP tool. Never request `subs:rw` or `keys:rw`. No MCP tool uses them, and `keys:rw` lets the client create access keys that keep working after the user disconnects it.

| Scope           | OAuth        | Access key | Grants                                                                                                                             |
| --------------- | ------------ | ---------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `papers:read`   | Yes          | Yes        | Paper search, full text, citations, related papers, extraction, claim checks, evidence                                             |
| `guidance:read` | Yes          | Yes        | Research guidance search and documents                                                                                             |
| `account:read`  | Yes          | Yes        | The credits, daily usage and subscription of the team the credential was issued for; naming its own team and scopes needs no scope |
| `subs:rw`       | If requested | Yes        | Which venues the dashboard hides from results; no MCP tool uses it                                                                 |
| `keys:rw`       | If requested | No         | Creating access keys in the user's teams; the Lune command line app uses it. It can't list or revoke keys                          |

`list_conferences` and `get_conference_papers` need no scope, and `list_conferences` is free. The full reference is at `https://luneresearch.com/docs/concepts/scopes`.

## Errors an MCP client sees

| Signal                                                                                          | Meaning                                                                                        | What to do                                                                                          |
| ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
| HTTP `401`, `WWW-Authenticate` without `error`                                                  | A tool was called with no credential                                                           | Start OAuth discovery from `resource_metadata`                                                      |
| HTTP `401`, `error="invalid_token"`                                                             | The access token expired or failed verification, or the key is invalid or revoked              | Refresh the token or restart OAuth; for a key, ask the user for a new one                           |
| HTTP `403`, `error="insufficient_scope"`                                                        | The OAuth token lacks the scope named in `scope=`                                              | Authorize again, requesting all three scopes: a new grant carries only the scopes its request names |
| Tool error naming a missing scope                                                               | The access key lacks the scope                                                                 | Ask the user for a key that includes it                                                             |
| HTTP `401` that survives a fresh sign-in, or a stdio tool error saying the account is suspended | The account is suspended                                                                       | Stop retrying; the user must appeal (the stdio message names the address)                           |
| Tool error with `quota_reason=no_capacity`                                                      | Daily allowance and credits are both spent                                                     | Stop and show the user `buy_credits_url=`; the allowance resets at `quota_resets_at=`               |
| Tool error with `quota_reason=batch_too_large`                                                  | The batch costs more than what's left                                                          | Retry with fewer items                                                                              |
| Tool error with `quota_reason=retry_now`                                                        | Capacity came back                                                                             | Retry once                                                                                          |
| Tool error with `retry_after_seconds=`                                                          | The per-second burst guard fired                                                               | Wait that long, then retry the same call                                                            |
| HTTP `403`, "origin not allowed"                                                                | The request came from a web page with an `Origin` header                                       | Call from a server or native client                                                                 |
| HTTP `405`                                                                                      | `GET` or `DELETE` on the endpoint                                                              | Use `POST`                                                                                          |
| HTTP `406`                                                                                      | A 2025-era request whose `Accept` doesn't list both `application/json` and `text/event-stream` | Send both                                                                                           |
| HTTP `415`                                                                                      | `Content-Type` is not `application/json`                                                       | Send `Content-Type: application/json`                                                               |
| HTTP `413`                                                                                      | The request body is larger than 1 MB                                                           | Send a smaller request                                                                              |
| HTTP `400`, parse error                                                                         | The body isn't valid JSON                                                                      | Fix the JSON                                                                                        |
| HTTP `400`, batch too large                                                                     | A JSON-RPC batch of more than 50 messages                                                      | Split the batch                                                                                     |
| HTTP `400` on a request that sends `MCP-Protocol-Version: 2026-07-28`                           | The request lacks the `_meta` envelope or the `Mcp-Method` header                              | Send both, or drop the version header to use 2025-era framing                                       |

Quota, burst, and access-key scope errors arrive as tool results with `isError: true`, not as HTTP errors. The text ends with one line of `key=value` fields, such as `quota_reason=`, `quota_resets_at=`, `buy_credits_url=`, `retry_after_seconds=`, and `http_status=`.

Questions or trouble: support@luneresearch.com
