Skip to content

Commit aa194e1

Browse files
Add oauth 2.1 documentation (#460)
* docs: add comprehensive OAuth 2.1 documentation - Update Claude.ai client docs with full OAuth setup guide - Two authentication methods: OAuth (recommended) vs token-based - Step-by-step OAuth server setup (Docker, uvx) - Environment variables documentation - Claude.ai connector setup instructions - Update Docker deployment docs with OAuth mode - New "For Remote Access (OAuth Mode)" section - OAuth environment variables table - Encryption key generation instructions - Server mode comparison table - Update FAQ with OAuth troubleshooting - OAuth vs token-based comparison - Common OAuth errors and solutions - OAuth environment variables reference - Update README with OAuth feature mention Closes #245 * docs: add port clarification and add-on OAuth notes - Clarify HA port (8123) vs ha-mcp port (8086/9583) - Document that add-on does not support OAuth mode yet - Add HAOS Docker workaround for OAuth users * docs: fix HAOS Docker instructions (not possible) HAOS does not allow direct Docker CLI access. Updated documentation with accurate alternatives: - Separate device on network (Pi, NAS, PC) - Cloud/VPS deployment - HA Container installation (has Docker access) * docs: clarify OAuth vs token provides same API permissions Add note explaining that both authentication methods provide identical Home Assistant API access. Switching auth methods won't resolve API errors like 405 - those are HA Core issues. * test: add HACS availability check before install tests Add pre-flight HACS availability check using ha_hacs_info before attempting HACS install operations. This prevents flaky test failures when HACS is rate-limited or temporarily unavailable. Affects: - test_install_mcp_tools_basic - test_install_mcp_tools_idempotent - test_check_mcp_tools_in_hacs --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent c4e048b commit aa194e1

5 files changed

Lines changed: 351 additions & 11 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Spend less time configuring, more time enjoying your smart home.
108108
| **🔧 Manage** | Automations, scripts, helpers, dashboards, areas, zones, groups, calendars, blueprints |
109109
| **📊 Monitor** | History, statistics, camera snapshots, automation traces, ZHA devices |
110110
| **💾 System** | Backup/restore, updates, add-ons, device registry |
111+
| **🔐 Auth** | OAuth 2.1 with DCR for secure multi-user access (Claude.ai), token-based for local clients |
111112

112113
<details>
113114
<summary><b>🛠️ Complete Tool List (82 tools)</b></summary>

docs/FAQ.md

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ You can also use ha-mcp with other AI clients. See the [Setup Wizard](https://ho
2626

2727
Built-in = operate devices. ha-mcp = administer your system.
2828

29+
### What's the difference between OAuth and token-based authentication?
30+
31+
| Feature | Token-Based | OAuth 2.1 |
32+
|---------|-------------|-----------|
33+
| **Setup** | Configure token in server environment | Users enter credentials via consent form |
34+
| **Best for** | Single user, local clients (Claude Desktop) | Multi-user, remote clients (Claude.ai) |
35+
| **Credentials stored** | In server config | Encrypted in user's session token |
36+
| **Server command** | `ha-mcp` or `ha-mcp-web` | `ha-mcp-oauth` |
37+
| **HTTPS required** | No (but recommended) | Yes (for production) |
38+
39+
**When to use OAuth:**
40+
- Hosting ha-mcp for multiple users
41+
- Using Claude.ai (web) as your client
42+
- Exposing ha-mcp over the internet
43+
44+
**When to use token-based:**
45+
- Single-user setup
46+
- Local/LAN access only
47+
- Claude Desktop or other local clients
48+
49+
> **Note:** Both methods provide **identical API permissions**. OAuth is about *how* you authenticate, not *what* you can do. The underlying Home Assistant REST API access is the same. If you encounter API errors (like 405 Method Not Allowed), switching auth methods won't help - the issue is with the HA API itself, not the authentication.
50+
2951
---
3052

3153
## Try Without Your Own Home Assistant
@@ -129,16 +151,108 @@ source ~/.zshrc
129151

130152
---
131153

154+
## OAuth Troubleshooting
155+
156+
### OAuth consent form not loading
157+
158+
1. **Check MCP_BASE_URL** - Must be set to your public HTTPS URL
159+
2. **Verify HTTPS** - OAuth requires HTTPS in production
160+
3. **Check tunnel** - Ensure Cloudflare Tunnel or similar is running
161+
162+
### "Invalid credentials" on consent form
163+
164+
1. **Verify Home Assistant URL:**
165+
- Include protocol: `http://` or `https://`
166+
- Include port if not default: `:8123`
167+
- Don't include trailing slash
168+
- Example: `http://homeassistant.local:8123`
169+
170+
2. **Verify Long-Lived Access Token:**
171+
- Generate a fresh token in HA → Profile → Security → Long-lived access tokens
172+
- Copy the full token (it's long!)
173+
- Token is validated against HA's `/api/config` endpoint
174+
175+
3. **Check network connectivity:**
176+
- The ha-mcp server must be able to reach your Home Assistant
177+
- If using Docker, ensure HA is accessible from the container
178+
179+
### OAuth session expired / logged out unexpectedly
180+
181+
1. **Check OAUTH_ENCRYPTION_KEY:**
182+
- If not set, tokens are invalidated on server restart
183+
- Generate a persistent key:
184+
```bash
185+
python3 -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
186+
```
187+
- Add to your environment: `OAUTH_ENCRYPTION_KEY=your-generated-key`
188+
189+
2. **Token expiration times:**
190+
- Access tokens: 1 hour (auto-refresh)
191+
- Refresh tokens: 7 days
192+
- Re-authenticate if refresh token expires
193+
194+
### OAuth redirect loop or "invalid_grant" error
195+
196+
1. **Clear browser cookies** for your ha-mcp domain
197+
2. **Check MCP_BASE_URL matches your actual URL** (including https://)
198+
3. **Verify no URL path mismatch** between MCP_BASE_URL and tunnel config
199+
200+
### Claude.ai shows "401 Unauthorized"
201+
202+
1. **Ensure you're using OAuth mode:** Server should run `ha-mcp-oauth`
203+
2. **Check the connector URL in Claude.ai** ends with `/mcp`
204+
3. **Try removing and re-adding the connector**
205+
206+
### Understanding the ports (HA vs ha-mcp)
207+
208+
There are **two different services** with different ports:
209+
210+
| Service | Default Port | Purpose |
211+
|---------|--------------|---------|
212+
| **Home Assistant** | 8123 | HA web UI and REST API |
213+
| **ha-mcp server** | 8086 (Docker/uvx) or 9583 (add-on) | MCP server for AI clients |
214+
215+
- In the **OAuth consent form**, you enter your **Home Assistant URL** (port 8123)
216+
- In **Claude.ai connector settings**, you enter the **ha-mcp server URL** (via HTTPS tunnel)
217+
218+
### Can I use OAuth with the Home Assistant add-on?
219+
220+
**Not currently.** The ha-mcp Home Assistant add-on runs in token mode only, using the Supervisor API for authentication.
221+
222+
**Important:** HAOS does not allow running custom Docker containers directly - there's no `docker` CLI access. To use OAuth mode with HAOS, you need to run ha-mcp on a separate device:
223+
224+
**Alternatives for HAOS users:**
225+
226+
1. **Separate device on your network** - Run ha-mcp OAuth on a Raspberry Pi, NAS, or always-on PC that has Docker installed
227+
2. **Cloud/VPS** - Deploy ha-mcp OAuth on a cloud server with HTTPS
228+
3. **Switch to HA Container** - If you use [Home Assistant Container](https://www.home-assistant.io/installation/linux#docker-compose) instead of HAOS, you have full Docker access
229+
230+
In all cases, the ha-mcp OAuth server needs network access to your Home Assistant instance to validate credentials.
231+
232+
---
233+
132234
## Configuration Options
133235

134-
### Environment Variables
236+
### Environment Variables (Token Mode)
135237

136238
| Variable | Description | Default | Required |
137239
|----------|-------------|---------|----------|
138240
| `HOMEASSISTANT_URL` | Your Home Assistant URL | - | Yes |
139241
| `HOMEASSISTANT_TOKEN` | Long-lived access token (or `demo` for demo env) | - | Yes |
140242
| `BACKUP_HINT` | Backup recommendation level | `normal` | No |
141243

244+
### Environment Variables (OAuth Mode)
245+
246+
| Variable | Description | Default | Required |
247+
|----------|-------------|---------|----------|
248+
| `MCP_BASE_URL` | Public HTTPS URL of your server | `http://localhost:8086` | Yes (production) |
249+
| `MCP_PORT` | Server port | `8086` | No |
250+
| `MCP_SECRET_PATH` | MCP endpoint path | `/mcp` | No |
251+
| `OAUTH_ENCRYPTION_KEY` | 32-byte base64 key for token encryption | Auto-generated | Recommended |
252+
| `LOG_LEVEL` | Logging verbosity (DEBUG, INFO, WARNING, ERROR) | `INFO` | No |
253+
254+
> **Note:** In OAuth mode, `HOMEASSISTANT_URL` and `HOMEASSISTANT_TOKEN` are NOT required - users provide credentials via the consent form.
255+
142256
### Backup Hint Modes
143257

144258
| Mode | Behavior |

site/src/content/clients/claude-ai.md

Lines changed: 142 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,171 @@ order: 8
99
httpNote: Requires HTTPS - Remote deployment required
1010
---
1111

12-
## Configuration
12+
## Overview
1313

14-
Claude.ai (web interface) supports MCP servers via the Connectors feature.
14+
Claude.ai (web interface) supports MCP servers via the Connectors feature. **ha-mcp supports OAuth 2.1 authentication**, providing a seamless zero-config experience for Claude.ai users.
1515

1616
**Requirements:**
1717
- HTTPS URL (HTTP not supported)
1818
- Claude Pro, Max, Team, or Enterprise subscription
1919
- Remote server with secure tunnel
2020

21-
### Setup Steps
21+
## Authentication Methods
22+
23+
| Method | Best For | Credentials Stored |
24+
|--------|----------|-------------------|
25+
| **OAuth 2.1** (Recommended) | Multiple users, secure access | Per-user, via consent form |
26+
| **Pre-configured Token** | Single user, simpler setup | In server environment |
27+
28+
---
29+
30+
## Option 1: OAuth Mode (Recommended)
31+
32+
OAuth mode provides **secure, zero-config authentication**. Users enter their Home Assistant credentials via a consent form when connecting.
33+
34+
### How OAuth Works
35+
36+
1. You add ha-mcp as a connector in Claude.ai
37+
2. Claude.ai redirects to the ha-mcp consent form
38+
3. You enter your Home Assistant URL and Long-Lived Access Token
39+
4. Credentials are validated against your HA instance
40+
5. You're connected! Credentials are encrypted in your session token
41+
42+
### Server Setup
43+
44+
**Using Docker:**
45+
46+
```bash
47+
docker run -d --name ha-mcp-oauth \
48+
-p 8086:8086 \
49+
-e MCP_BASE_URL=https://your-public-url.com \
50+
-e OAUTH_ENCRYPTION_KEY=your-32-byte-base64-key \
51+
ghcr.io/homeassistant-ai/ha-mcp:latest \
52+
ha-mcp-oauth
53+
```
54+
55+
**Using uvx:**
56+
57+
```bash
58+
MCP_BASE_URL=https://your-public-url.com \
59+
OAUTH_ENCRYPTION_KEY=your-32-byte-base64-key \
60+
uvx ha-mcp@latest ha-mcp-oauth
61+
```
62+
63+
### Environment Variables for OAuth
64+
65+
| Variable | Description | Default | Required |
66+
|----------|-------------|---------|----------|
67+
| `MCP_BASE_URL` | Public HTTPS URL of your server | `http://localhost:8086` | **Yes** (for production) |
68+
| `MCP_PORT` | Server port | `8086` | No |
69+
| `MCP_SECRET_PATH` | MCP endpoint path | `/mcp` | No |
70+
| `OAUTH_ENCRYPTION_KEY` | 32-byte base64 key for token encryption | Auto-generated | **Recommended** |
71+
| `LOG_LEVEL` | Logging verbosity | `INFO` | No |
72+
73+
> **Note:** If `OAUTH_ENCRYPTION_KEY` is not set, a temporary key is generated. Tokens will be invalidated on server restart. For production, generate a persistent key:
74+
> ```bash
75+
> python3 -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
76+
> ```
77+
78+
### Expose Server with HTTPS
79+
80+
Claude.ai requires HTTPS. Use a secure tunnel:
81+
82+
**Quick Tunnel (Testing):**
83+
```bash
84+
cloudflared tunnel --url http://localhost:8086
85+
# Gives you: https://random-words.trycloudflare.com
86+
# MCP URL: https://random-words.trycloudflare.com/mcp
87+
```
88+
89+
**Persistent Tunnel:** See [Cloudflare Tunnel documentation](/setup?connection=remote&deployment=cloudflared)
90+
91+
### Connect in Claude.ai
2292

2393
1. Open [Claude.ai](https://claude.ai)
2494
2. Go to **Settings****Connectors**
2595
3. Click **Add custom connector**
2696
4. Enter:
2797
- **Name:** Home Assistant
28-
- **URL:** `{{MCP_SERVER_URL}}` (must be HTTPS)
98+
- **URL:** `https://your-public-url.com/mcp`
2999
5. Click **Add**
30-
6. Authenticate if OAuth is required
100+
6. You'll be redirected to the ha-mcp consent form
101+
7. Enter your Home Assistant URL (e.g., `http://homeassistant.local:8123`)
102+
8. Enter your Long-Lived Access Token ([How to get a token](/faq#token-invalid-or-authentication-errors))
103+
9. Click **Authorize**
104+
10. You're connected!
105+
106+
### Understanding the Ports
107+
108+
When using OAuth, there are **two different services** with different ports:
31109

32-
### Important
110+
| Service | Default Port | What You Enter |
111+
|---------|--------------|----------------|
112+
| **Home Assistant** | 8123 | In the OAuth consent form (your HA URL) |
113+
| **ha-mcp server** | 8086 | In Claude.ai connector settings (via HTTPS tunnel) |
33114

34-
Claude.ai **only supports HTTPS** - you cannot use HTTP URLs. You'll need to set up a secure tunnel (see Remote deployment options).
115+
The consent form asks for your **Home Assistant URL** (port 8123) - this is where ha-mcp makes API calls to control your smart home. The ha-mcp server itself runs on port 8086 (or custom).
116+
117+
### Home Assistant Add-on Note
118+
119+
> **Important:** The ha-mcp Home Assistant add-on does **not currently support OAuth mode**. The add-on runs in token mode using the Supervisor API for authentication.
120+
>
121+
> **Note:** HAOS does not allow running custom Docker containers directly (no `docker` CLI access). To use OAuth mode, you'll need one of these alternatives:
122+
>
123+
> **Option A: Separate device on your network**
124+
> Run ha-mcp OAuth on another device (Raspberry Pi, NAS, always-on PC):
125+
> ```bash
126+
> docker run -d --name ha-mcp-oauth \
127+
> -p 8086:8086 \
128+
> -e MCP_BASE_URL=https://your-cloudflare-tunnel.com \
129+
> -e OAUTH_ENCRYPTION_KEY=your-key \
130+
> ghcr.io/homeassistant-ai/ha-mcp:latest \
131+
> ha-mcp-oauth
132+
> ```
133+
>
134+
> **Option B: Cloud/VPS**
135+
> Deploy ha-mcp OAuth on a cloud server (AWS, DigitalOcean, etc.) with HTTPS.
136+
>
137+
> **Option C: Home Assistant Container installation**
138+
> If you're using [HA Container](https://www.home-assistant.io/installation/linux#docker-compose) (not HAOS), you have full Docker access and can run ha-mcp OAuth alongside HA.
139+
>
140+
> In all cases, the ha-mcp OAuth server needs network access to your Home Assistant instance.
141+
142+
---
143+
144+
## Option 2: Pre-configured Token Mode
145+
146+
For single-user setups where you want to skip the OAuth consent form.
147+
148+
### Server Setup
149+
150+
```bash
151+
docker run -d --name ha-mcp \
152+
-p 8086:8086 \
153+
-e HOMEASSISTANT_URL=http://homeassistant.local:8123 \
154+
-e HOMEASSISTANT_TOKEN=your_long_lived_token \
155+
ghcr.io/homeassistant-ai/ha-mcp:latest \
156+
ha-mcp-web
157+
```
158+
159+
### Connect in Claude.ai
160+
161+
1. Expose with HTTPS (see above)
162+
2. Add connector with URL: `https://your-public-url.com/mcp`
163+
3. No OAuth flow - connects directly using pre-configured credentials
164+
165+
---
35166
36167
## Supported Transports
37168
169+
- **Streamable HTTP** - Supported (recommended, used by OAuth mode)
38170
- **SSE (Server-Sent Events)** - Supported
39-
- **Streamable HTTP** - Supported (recommended)
40171
41172
## Notes
42173
43174
- Web-based configuration only (no config file)
44-
- Requires HTTPS endpoint (Remote deployment required)
175+
- Requires HTTPS endpoint (remote deployment required)
45176
- Remote MCP Connectors are currently in beta
46177
- Use the "Search and tools" button in chat to enable/disable specific tools
178+
- OAuth tokens expire after 1 hour (auto-refresh supported)
179+
- Refresh tokens valid for 7 days

0 commit comments

Comments
 (0)