---
name: anymcp
description: Add anymcp.chat to a web app, so each signed-in user can turn on their own Claude chat agent that works through the app's MCP server. Use when asked to add anymcp, an agent bar, or a chat agent backed by the app's MCP.
---

# Add anymcp.chat to this app

anymcp.chat gives each of the app's users their own Claude agent in a chat bar on the page. The agent runs on the user's own Fly Sprite with their own Claude account, and its only tools are this app's MCP tools, called as that user. There's no signup: anymcp reads the app's config from its own domain.

First check what the app already has, then add only what's missing. Ask the human before you create an MCP server or change how sign-in works.

## 1. An MCP server, with a token per user

- A Streamable HTTP MCP endpoint (for example `https://<host>/mcp`) that accepts `Authorization: Bearer <token>` and acts as the user who owns that token.
- A way for a signed-in user to create a token for it, and to revoke it, in the app's settings. Store only a hash of the token.
- Mark read-only tools with the `readOnlyHint: true` annotation. After any other tool succeeds, the bar fires `anymcp:changed` so the page can refresh.
- Optional: MCP server `instructions` that explain the data model to the agent.

## 2. Session JWTs and a JWKS

The page must be able to get the signed-in user's session JWT in the browser. It must be ES256 or RS256, have `sub`, `email` and `exp` claims, and be verifiable with a public JWKS URL. Supabase, Auth0, Clerk, Firebase and most OIDC providers already do this. Don't build your own auth for this.

## 3. `/.well-known/anymcp.json`

Serve this file at `https://<host>/.well-known/anymcp.json`, where `<host>` is the exact hostname the bar runs on. Serve it directly with a 200, not a redirect. Serving it is how anymcp knows the app is yours, and only `https://<host>` may call anymcp for it.

```json
{
  "name": "Your App",
  "mcpUrl": "https://<host>/mcp",
  "mcpName": "yourapp",
  "jwksUrl": "https://<auth provider>/.well-known/jwks.json",
  "iss": "<the JWT's iss claim>",
  "aud": "<the JWT's aud claim>",
  "confirm": ["publish", "send_email"],
  "model": "sonnet",
  "maxBudgetUsd": 1,
  "instructions": "One or two sentences on how to use the tools well."
}
```

- Required: `mcpUrl` and `jwksUrl` (both https). Set `iss` and `aud` whenever your JWTs have them.
- `confirm`: tools that can't run until the user says "Yes" (publishing, sending things, deleting, payments).
- `model` is `sonnet`, `haiku` or `opus`. `maxBudgetUsd` is per message, at most 2, and is paid from the user's own Claude plan.
- anymcp caches the file for about 5 minutes. An agent keeps the settings it was turned on with, so a user picks up changes by turning the agent off and on again.

## 4. The bar

Add this to the pages where signed-in users should have the agent, and return the current session JWT from `token`:

```html
<script>window.anymcp = { token: async () => /* the user's current session JWT, or null */ };</script>
<script src="https://anymcp.chat/bar.js" defer></script>
```

- The bar stays hidden until the user turns the agent on.
- If `token` only works after sign-in finishes, call `anymcp.refresh()` after sign-in and after sign-out.
- Optional attributes: `data-label` (the idle text), `data-theme="light|dark"`, `data-accent="#hex"`.
- Listen for `window.addEventListener("anymcp:changed", ...)` and reload the data on screen.

## 5. An "Enable agent" switch

In the user's settings, add a control that creates a new MCP token for that user and calls `anymcp.enable(token)`. The bar opens and walks them through connecting Fly.io and Claude, which takes a couple of minutes. Explain that it's optional and that they can turn it off from the bar's menu. When they turn it off, revoke the token.

## Check it

1. Open `https://<host>/.well-known/anymcp.json`. It should return the JSON above.
2. While signed in, run this in the browser console. It should return `{"agent":null,"flow":null}`, not a 401 or 404:
   `fetch("https://anymcp.chat/api/agent?tenant=" + location.hostname, { headers: { authorization: "Bearer " + await anymcp.token() } }).then(r => r.json())`
3. Tell the human to try the Enable switch with their own account.
