Connect Wallet

Let your users connect their Haplotype Wallet to your app, then read their data with their permission.

Updated October 18, 2026

How it works

A user clicks the Connect button on your site. Your server sends them to our consent screen. They sign in to Haplotype Wallet, see your app's name and what it asks for, and approve. They come back to your server with a code, and your server trades it for tokens. With the access token, your server calls tools on our MCP server as that user.

The protocol is OAuth 2.0 with the authorization code flow. Any OAuth or OpenID Connect library can do it.

Only server-side apps can connect, because the tokens give access to genetic data and must stay on your server. Your app authenticates with a client secret. Apps that run only in a browser or on a phone are not supported.

The access token works only for the MCP endpoint. There is no separate REST API.

Get credentials

We set up each app ourselves. There is no self-serve signup. Write to support@haplotype-labs.com and tell us these things.

  • your app's name and a square logo, which the consent screen shows
  • the redirect URIs your app uses, for example https://yourapp.example/callback
  • which scopes you need, and why
  • who we should contact about the integration

You get a client ID and a client secret. We send the secret as a one-time link. Keep it on your server. A redirect URI on http://localhost is fine while you develop.

You start on our test environment, with its own credentials. Its endpoints differ from the production ones below only in their host names, and we send them with the credentials. When your integration works, we set your app up in production with your production redirect URIs.

Your app can ask only for the scopes we approved. Asking for any other scope fails before the user sees anything (see Errors).

Endpoints

Issuerhttps://authkit.haplotype.ai
OIDC discoveryhttps://authkit.haplotype.ai/.well-known/openid-configuration
Authorizehttps://authkit.haplotype.ai/oauth2/authorize
Tokenhttps://authkit.haplotype.ai/oauth2/token
resourcehttps://api.haplotype.ai/mcp
MCP endpointhttps://api.haplotype.ai/mcp

The resource value and the MCP endpoint are the same URL. Most libraries read the authorize and token endpoints from OIDC discovery.

When the user clicks the button, your server creates a random state and a PKCE pair (S256), saves both in the user's session, and redirects the browser to the authorize endpoint.

https://authkit.haplotype.ai/oauth2/authorize
  ?response_type=code
  &client_id=<your client ID>
  &redirect_uri=https://yourapp.example/callback
  &scope=openid offline_access samples:read scores:read
  &state=<random>
  &code_challenge=<S256 challenge>
  &code_challenge_method=S256
  &resource=https://api.haplotype.ai/mcp

Encode each value as usual. Three parameters need care.

  • scope lists the wallet scopes you want, plus openid. Add offline_access to get a refresh token. Without it you get none, and your app has to send the user through this step again every 15 minutes. Add email if you need the user's email address. The ID token has no email without it.
  • resource goes on this request, on the token request and on every refresh. See Exchange the code for why.
  • PKCE is used in addition to your client secret, not instead of it. Send both.

The user signs in and sees the consent screen once. While your app holds a grant, the next authorize request goes straight back to your callback. If you ask for a scope the grant does not have yet, the consent screen appears again and lists every scope.

Handle the callback

The user comes back to your redirect URI. Check that state matches the one you saved, then look at the query.

QueryMeaning
code=...&state=...The user approved. Exchange the code.
error=access_denied&state=...The user cancelled on the consent screen.
error=invalid_scope&state=...You asked for a scope your app is not approved for. There is no error_description. The user saw no consent screen.

Exchange the code

Post the code to the token endpoint with your client secret, the PKCE verifier and resource. The client ID and secret go in the form body.

curl https://authkit.haplotype.ai/oauth2/token \
  -d grant_type=authorization_code \
  -d code="$CODE" \
  -d code_verifier="$CODE_VERIFIER" \
  -d client_id="$CLIENT_ID" \
  -d client_secret="$CLIENT_SECRET" \
  --data-urlencode redirect_uri=https://yourapp.example/callback \
  --data-urlencode resource=https://api.haplotype.ai/mcp

resource here is what puts the MCP endpoint in the access token's audience (aud). Leave it out and you get a token that looks valid, but /mcp answers 401. This is the most common mistake.

The authorization server checks the PKCE verifier even though your app has a secret. A wrong verifier fails with invalid_grant.

The response has access_token, expires_in, id_token, and refresh_token if you asked for offline_access. It has no scope field. To see what the user granted, read the scope claim of the access token, which is a JWT. You got it straight from the token endpoint over TLS, so you can decode the payload without checking the signature.

{
  "iss": "https://authkit.haplotype.ai",
  "aud": "https://api.haplotype.ai/mcp",
  "sub": "user_01...",
  "client_id": "client_01...",
  "scope": "offline_access openid samples:read scores:read"
}

sub identifies the user. Store it if you need to know which of your users connected which wallet.

The granted scopes can differ from what you asked for. A later authorize request with fewer scopes does not narrow an existing grant: the new token still carries every scope granted so far. Always read the scopes from the token.

Refresh

An access token lasts 15 minutes (expires_in says exactly). Refresh it shortly before then, or when /mcp answers 401.

curl https://authkit.haplotype.ai/oauth2/token \
  -d grant_type=refresh_token \
  -d refresh_token="$REFRESH_TOKEN" \
  -d client_id="$CLIENT_ID" \
  -d client_secret="$CLIENT_SECRET" \
  --data-urlencode resource=https://api.haplotype.ai/mcp

Send resource again. A refresh without it returns a token with the wrong audience, and /mcp answers 401. Keep the newest refresh token the response returns.

Scopes

Each scope unlocks some tools. The consent screen shows the user the sentence under each scope. This table is generated from the server's own scope list.

Scope, and what the user seesTools it unlocks
samples:read

See which genotype files you have and how they were processed

variants:read

Read your genotype at the positions it asks about

scores:read

Read your polygenic scores and analysis runs

runs:write

Start new analyses on your genotypes and send questions to biobanks

Ask only for what your app needs. A tool your token has no scope for is left out of tools/list, and calling it fails (see Errors).

These tools read public reference data, not the user's data. They work with any valid token.

get_algorithm, list_algorithms, list_brava_associations, list_brava_traits, list_gwas_associations, list_pgs_categories, list_reports, lookup_clingen, lookup_cpic, lookup_variant_references, search_pgs_scores

tools/list also shows report_issue. It is for AI assistants to report bugs on behalf of a user. Your app does not need it.

Call a tool

The MCP server speaks JSON-RPC 2.0 over HTTP. It is stateless: there is no session to open and no initialize call to make first. Send each call with the access token.

curl https://api.haplotype.ai/mcp \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"list_samples","arguments":{}}}'

The Accept header must list both types, or the server answers 406. The answer comes back as a server-sent event, with the JSON-RPC message on the data: line. A tool that returns a list puts it in structuredContent.result.

event: message
data: {"jsonrpc":"2.0","id":1,"result":{"content":[...],"structuredContent":{"result":[...]},"isError":false}}

You can also use an MCP SDK. Calling a tool has examples in Python and TypeScript. The tool reference lists every parameter and response field.

Errors

At the authorize step

What you getWhyWhat to do
error=invalid_scope on your callbackscope names a scope your app is not approved for.Remove it, or ask us to approve it.
error=access_denied on your callbackThe user cancelled.Show the Connect button again.

At the token endpoint

What you getWhyWhat to do
400 with {"error":"invalid_grant"} on a refreshThe user disconnected your app, or the refresh token is no longer valid.Forget the tokens and ask the user to connect again.
400 with {"error":"invalid_grant"} on a code exchangeThe code was already used or has expired, or the PKCE verifier does not match.Start the authorization again.

At the MCP endpoint

A rejected token gets 401 and no JSON-RPC result.

HTTP/1.1 401 Unauthorized
www-authenticate: Bearer error="invalid_token", ...

The token has expired, or it was issued without resource and has the wrong audience. Refresh once, with resource, and retry. If the new token is refused too, check that resource is on your token and refresh requests.

In a tool result

A tool that fails still answers with HTTP 200. The result has "isError": true, a message in its first content block, and a code in _meta, under the key ai.haplotype/error. A call to a tool your token has no scope for fails like this.

{
  "content": [
    {
      "type": "text",
      "text": "This app was not granted the variants:read permission for Haplotype Wallet, so it cannot call query_variants. This is not a bug."
    }
  ],
  "isError": true,
  "_meta": {
    "ai.haplotype/error": {
      "code": "missing_scope",
      "retryable": false,
      "scope": "variants:read"
    }
  }
}

Act on the code, not the message. We write the message for AI assistants and may reword it. The object under ai.haplotype/error has these fields.

  • code is one of the codes in the table below. Treat a code you do not know as internal_error.
  • retryable is true when the same call can succeed later without changes.
  • scope comes with missing_scope and names the scope the tool needs.
  • manage_url comes with trial_ended, subscription_ended and limit_reached. It is where the user manages their plan. See When the user's plan refuses.
  • ref is an error reference such as 5f0c2a1e, on failures we log. Quote it when you write to us.

The MCP SDKs pass _meta through as is: result.meta in Python, result._meta in TypeScript. An error result has no structuredContent.

CodeWhat it means, and what to do
missing_scope

The token has no permission for this tool. scope names the one it needs.

Send the user through consent again, asking for scope as well.

account_not_found

The user has never opened Haplotype Wallet, so there is no data to share.

Ask the user to sign in to Haplotype Wallet once.

waitlisted

The user's account is waiting for approval.

Tell the user. The same call works once the account is approved.

trial_ended

The user's free trial has ended.

Send the user to manage_url to subscribe.

subscription_ended

The user's subscription has ended.

Send the user to manage_url to subscribe again.

limit_reached

The call would go over the user's plan allowance, for example the polygenic score models schedule_run may compute.

Ask for less, or send the user to manage_url.

not_found

The sample, run, algorithm or biobank request does not exist or is not the user's, or the tool is not available to this user.

Check the ID. List the user's samples, runs or requests again.

invalid_argument

The arguments do not fit the tool. The message names the parameters, never their values.

Fix the call. Check it against the tool's input schema.

rate_limited

Retrying can help

Too many calls of this kind in a short time, or, for submit_biobank_request, too many open requests.

Wait, then retry.

upstream_unavailable

Retrying can help

A service behind the wallet failed or did not answer.

Retry after a short wait. If it persists, quote ref to us.

internal_error

A bug on our side. The detail is withheld.

Quote ref when you report it to us.

Errors has more on the messages and error references.

When the user's plan refuses

Users pay for Haplotype Wallet themselves, and your app's access follows their plan. When the free trial or the subscription has ended, every tool refuses, reference tools included. schedule_run also refuses a run that would go over the plan's polygenic score allowance.

CodeWhat the user sees in the walletWhat your app shows
trial_ended"Your free trial has ended", with a Subscribe button."Your Haplotype Wallet trial has ended", and a link to manage_url.
subscription_ended"Your subscription has ended", with a Subscribe button."Your Haplotype Wallet plan has ended", and a link to manage_url.
limit_reachedTheir plan, with how much of it they have used. A trial user can subscribe for a larger allowance.Ask for fewer models, or link to manage_url to change the plan.

Your app does not take payment for us. Send the user to manage_url, our account page. It carries your app's client ID:

https://www.haplotype.ai/app/account/?client_id=<your client ID>

To bring the user back to your app when they are done, add return_to, set to one of your app's registered redirect URIs, exactly as registered. The page then shows a "Back to" button with your app's name. It ignores any other address, so register a separate return URI with us if you do not want users back on your OAuth callback.

https://www.haplotype.ai/app/account/?client_id=<your client ID>&return_to=https%3A%2F%2Fyourapp.example%2Fcallback

Once the user has paid, the same calls work again with the tokens you hold. You do not need to connect again.

Disconnect and token lifetime

A user can disconnect your app at any time, under "Connected apps" in Haplotype Wallet. We can also revoke its access. Either way, two things happen.

  • Refresh fails at once with invalid_grant.
  • An access token already issued keeps working until it expires, at most 15 minutes. Then /mcp answers 401.

So when a refresh fails with invalid_grant, delete the tokens you stored and show the Connect button again. Connecting again creates a new grant, and the user sees the consent screen.

The button

Use our button rather than drawing your own, so users recognise it.

Connect Haplotype Wallet
Light pages · SVG · PNG · PNG @2x
Connect Haplotype Wallet
Dark pages · SVG · PNG · PNG @2x

Connect button has the file URLs and the rules for size, clear space and backgrounds.

The contract file

connect-contract.json holds the facts on this page as data: the OAuth and MCP endpoints, the access token lifetime, the scopes with their consent text and tools, the reference tools, and the input and output schema of every scoped tool. It is generated from the server code and changes when the server does.

Generate your constants and types from it instead of copying them by hand. Its contract_version changes only when the file's shape changes.

Example apps

Two small server-side apps run the whole flow. Each shows the button, gets the user's consent, refreshes the token, lists the user's samples, and asks the user to connect again after a disconnect. Each runs locally with your client ID and secret.