@orellbuehler/homeassistant-mcp 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +142 -39
- package/bin/homeassistant-mcp.js +51 -0
- package/binaries/darwin-arm64/homeassistant-mcp +0 -0
- package/binaries/darwin-x64/homeassistant-mcp +0 -0
- package/binaries/linux-arm64/homeassistant-mcp +0 -0
- package/binaries/linux-x64/homeassistant-mcp +0 -0
- package/binaries/win32-x64/homeassistant-mcp.exe +0 -0
- package/package.json +5 -38
- package/dist/config.js +0 -26
- package/dist/hass/format.js +0 -38
- package/dist/hass/rest.js +0 -50
- package/dist/hass/ws.js +0 -80
- package/dist/index.js +0 -17
- package/dist/server.js +0 -9
- package/dist/tools/auth.js +0 -191
- package/dist/tools/automations.js +0 -62
- package/dist/tools/backup.js +0 -375
- package/dist/tools/control.js +0 -133
- package/dist/tools/dashboards.js +0 -206
- package/dist/tools/diagnostics.js +0 -537
- package/dist/tools/energy.js +0 -235
- package/dist/tools/entities.js +0 -57
- package/dist/tools/hacs.js +0 -52
- package/dist/tools/helpers.js +0 -98
- package/dist/tools/history.js +0 -57
- package/dist/tools/integrations.js +0 -538
- package/dist/tools/lists.js +0 -216
- package/dist/tools/organization.js +0 -548
- package/dist/tools/raw.js +0 -41
- package/dist/tools/registry.js +0 -195
- package/dist/tools/reload.js +0 -40
- package/dist/tools/resources.js +0 -56
- package/dist/tools/scripts.js +0 -206
- package/dist/tools/services.js +0 -33
- package/dist/tools/statistics.js +0 -363
- package/dist/tools/system.js +0 -57
- package/dist/tools/templates.js +0 -21
- package/dist/tools/trace.js +0 -74
- package/dist/tools/zha.js +0 -121
- package/dist/toolsets.js +0 -356
package/README.md
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
[](https://www.npmjs.com/package/@orellbuehler/homeassistant-mcp)
|
|
4
4
|
[](https://www.npmjs.com/package/@orellbuehler/homeassistant-mcp)
|
|
5
5
|
[](https://github.com/OrellBuehler/homeassistant-mcp/actions/workflows/ci.yml)
|
|
6
|
-
[](https://nodejs.org)
|
|
7
6
|
[](./LICENSE)
|
|
8
7
|
[](https://github.com/OrellBuehler/homeassistant-mcp/actions/workflows/ha-api-drift.yml)
|
|
9
8
|
|
|
@@ -32,8 +31,9 @@ Point Claude (or any MCP client) at it and the agent can:
|
|
|
32
31
|
`fire_event`) and the raw API passthrough are **off by default**; a read-only mode removes every
|
|
33
32
|
write tool.
|
|
34
33
|
|
|
35
|
-
216 tools in 25 toolsets (172 enabled by default),
|
|
36
|
-
|
|
34
|
+
216 tools in 25 toolsets (172 enabled by default), a single native binary that runs through `npx`
|
|
35
|
+
or as a [downloaded file](#prebuilt-binary), and works with Claude Code, Claude Desktop, Cursor,
|
|
36
|
+
and any other MCP client. The tools are
|
|
37
37
|
[checked against every Home Assistant release](#api-drift-detection).
|
|
38
38
|
|
|
39
39
|
## Example prompts
|
|
@@ -77,7 +77,7 @@ It is built for **using** the home (read state, call actions); this project is b
|
|
|
77
77
|
| Check config, Energy dashboard, ZHA groups, HACS | no | yes |
|
|
78
78
|
| Limit access to entities exposed to Assist | yes (Device control set) | no (sees what the token's user sees) |
|
|
79
79
|
| Choose which tools the agent gets | per tool set, plus read-only mode | per toolset and per tool, plus read-only mode |
|
|
80
|
-
| Connection | HTTP at `/api/mcp`, OAuth (Claude Code needs HTTPS) | local stdio
|
|
80
|
+
| Connection | HTTP at `/api/mcp`, OAuth (Claude Code needs HTTPS) | local stdio, native binary, long-lived token |
|
|
81
81
|
|
|
82
82
|
Compared against the built-in tool sets as of Home Assistant 2026.11.3. Use the built-in server for an
|
|
83
83
|
assistant that operates the home, scoped to the entities you expose to voice assistants. Use this
|
|
@@ -85,9 +85,15 @@ one for an agent that maintains it — most of that work has no action to call,
|
|
|
85
85
|
|
|
86
86
|
## Install
|
|
87
87
|
|
|
88
|
+
The server is a single native binary; every option below installs the same one.
|
|
89
|
+
|
|
90
|
+
### npx
|
|
91
|
+
|
|
88
92
|
The package is published as
|
|
89
93
|
[`@orellbuehler/homeassistant-mcp`](https://www.npmjs.com/package/@orellbuehler/homeassistant-mcp)
|
|
90
|
-
and runs directly with `npx
|
|
94
|
+
and runs directly with `npx`, no clone or build needed. It needs Node.js 18 or newer and bundles
|
|
95
|
+
the prebuilt binaries for Linux (x64, arm64), macOS (x64, arm64) and Windows (x64) behind a small
|
|
96
|
+
launcher.
|
|
91
97
|
|
|
92
98
|
```bash
|
|
93
99
|
claude mcp add homeassistant \
|
|
@@ -97,8 +103,92 @@ claude mcp add homeassistant \
|
|
|
97
103
|
```
|
|
98
104
|
|
|
99
105
|
See [Usage with Claude Code](#usage-with-claude-code) for the equivalent JSON config. For any other
|
|
100
|
-
MCP client, run the package directly
|
|
101
|
-
`HASS_TOKEN` set in the environment.
|
|
106
|
+
MCP client, run the package directly: `npx -y @orellbuehler/homeassistant-mcp` with `HASS_URL` and
|
|
107
|
+
`HASS_TOKEN` set in the environment. Pin a version (`@orellbuehler/homeassistant-mcp@1.1.0`) if a
|
|
108
|
+
new release should only be picked up when you choose to update.
|
|
109
|
+
|
|
110
|
+
### Prebuilt binary
|
|
111
|
+
|
|
112
|
+
Download the file for your system from the
|
|
113
|
+
[latest GitHub Release](https://github.com/OrellBuehler/homeassistant-mcp/releases/latest):
|
|
114
|
+
|
|
115
|
+
| System | File |
|
|
116
|
+
| ------------------------------ | --------------------------------- |
|
|
117
|
+
| Linux x64 (static, any distro) | `homeassistant-mcp-linux-x64` |
|
|
118
|
+
| Linux arm64 (static) | `homeassistant-mcp-linux-arm64` |
|
|
119
|
+
| macOS, Intel | `homeassistant-mcp-darwin-x64` |
|
|
120
|
+
| macOS, Apple silicon | `homeassistant-mcp-darwin-arm64` |
|
|
121
|
+
| Windows x64 | `homeassistant-mcp-win32-x64.exe` |
|
|
122
|
+
|
|
123
|
+
Check it against `SHA256SUMS` from the same release, and make it executable (not needed on
|
|
124
|
+
Windows):
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
sha256sum --check --ignore-missing SHA256SUMS # Linux
|
|
128
|
+
grep homeassistant-mcp-darwin-arm64 SHA256SUMS | shasum -a 256 --check # macOS
|
|
129
|
+
chmod +x homeassistant-mcp-linux-x64
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
On Windows, compare `(Get-FileHash .\homeassistant-mcp-win32-x64.exe).Hash` with the matching line of
|
|
133
|
+
`SHA256SUMS`. On macOS a browser download may carry a quarantine flag; remove it with
|
|
134
|
+
`xattr -d com.apple.quarantine homeassistant-mcp-darwin-arm64`.
|
|
135
|
+
|
|
136
|
+
Register the path of the binary instead of `npx`. The `--env` flags must come before the `--`
|
|
137
|
+
separator:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
claude mcp add homeassistant \
|
|
141
|
+
--env HASS_URL=http://homeassistant.local:8123 \
|
|
142
|
+
--env HASS_TOKEN=your-long-lived-access-token \
|
|
143
|
+
-- /path/to/homeassistant-mcp-linux-x64
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
```json
|
|
147
|
+
{
|
|
148
|
+
"mcpServers": {
|
|
149
|
+
"homeassistant": {
|
|
150
|
+
"command": "/path/to/homeassistant-mcp-linux-x64",
|
|
151
|
+
"env": {
|
|
152
|
+
"HASS_URL": "http://homeassistant.local:8123",
|
|
153
|
+
"HASS_TOKEN": "your-long-lived-access-token"
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Pointing `command` at the binary avoids the Node launcher process that `npx` keeps running next to
|
|
161
|
+
it.
|
|
162
|
+
|
|
163
|
+
### From source
|
|
164
|
+
|
|
165
|
+
Needs Rust 1.85 or newer:
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
cargo install --git https://github.com/OrellBuehler/homeassistant-mcp
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
This puts `homeassistant-mcp` in `~/.cargo/bin`; use that path as the `command` of your MCP client.
|
|
172
|
+
|
|
173
|
+
## Upgrading from the TypeScript releases
|
|
174
|
+
|
|
175
|
+
Version 1.1.0 replaces the TypeScript implementation with a Rust one. Tool names, descriptions,
|
|
176
|
+
input schemas, validation messages, outputs, the environment variables and the read-only and
|
|
177
|
+
toolset gating are unchanged, so existing configurations keep working, including
|
|
178
|
+
`npx -y @orellbuehler/homeassistant-mcp`. User-visible differences:
|
|
179
|
+
|
|
180
|
+
- The npm package now carries prebuilt binaries instead of `dist/index.js`. It needs Node.js 18 or
|
|
181
|
+
newer (previously 20) and only supports the five platforms listed above; other systems build
|
|
182
|
+
from source with `cargo install`. If you ran the server with `node /path/to/dist/index.js`,
|
|
183
|
+
switch to `npx`, the downloaded binary or the `cargo install` binary.
|
|
184
|
+
- `tools/list` no longer carries the per-tool `execution` field (`taskSupport: "forbidden"`) that
|
|
185
|
+
the TypeScript SDK added. It only tells clients that a tool cannot run as a task, which is also
|
|
186
|
+
what an absent field means.
|
|
187
|
+
- Malformed JSON-RPC requests get different error codes and messages. For example, `tools/call`
|
|
188
|
+
without parameters, or with `arguments` that are not an object, is rejected with `-32601`
|
|
189
|
+
instead of a validation message, and `arguments: null` is accepted instead of rejected. Unknown
|
|
190
|
+
methods name the method in the error message. Requests from a normal MCP client are not
|
|
191
|
+
affected.
|
|
102
192
|
|
|
103
193
|
## Getting a token
|
|
104
194
|
|
|
@@ -144,8 +234,8 @@ Add the server to `~/.claude/settings.json` (or a project `.mcp.json`):
|
|
|
144
234
|
}
|
|
145
235
|
```
|
|
146
236
|
|
|
147
|
-
|
|
148
|
-
`"args"
|
|
237
|
+
To run a downloaded or `cargo install`ed binary instead, set `"command"` to its path and drop
|
|
238
|
+
`"args"` (see [Prebuilt binary](#prebuilt-binary)). Restart Claude Code and the tools are
|
|
149
239
|
available immediately. Verify with `claude mcp list` (should show `homeassistant ✓ connected`) or
|
|
150
240
|
`/mcp` inside a session.
|
|
151
241
|
|
|
@@ -392,48 +482,59 @@ integration you still reference in YAML will break that config, so check usage f
|
|
|
392
482
|
## Development
|
|
393
483
|
|
|
394
484
|
```bash
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
npm
|
|
404
|
-
npm run site # write site/tools.json for the website (after a build)
|
|
485
|
+
cargo build --release # target/release/homeassistant-mcp
|
|
486
|
+
cargo test # unit and integration tests (wiremock, no real Home Assistant)
|
|
487
|
+
cargo fmt --check # cargo fmt to fix
|
|
488
|
+
cargo clippy --all-targets -- -D warnings
|
|
489
|
+
node scripts/ha-api.mjs check # every command/route the tools call exists in ha-api/snapshot.json
|
|
490
|
+
node scripts/ha-api.mjs uncovered # Home Assistant commands/routes no tool uses yet
|
|
491
|
+
node scripts/tool-docs.mjs # regenerate TOOLS.md from the registered tools
|
|
492
|
+
node scripts/tool-docs.mjs site # write site/tools.json for the website
|
|
493
|
+
scripts/check-npm-package.sh # pack the npm package and start it (needs binaries/linux-x64/homeassistant-mcp)
|
|
405
494
|
```
|
|
406
495
|
|
|
407
|
-
|
|
496
|
+
The Node scripts have no dependencies. `tool-docs.mjs` runs `cargo run -- --tool-table`, a hidden
|
|
497
|
+
flag that prints the toolset table as JSON; set `TOOL_DOCS_BIN` to use an existing binary instead.
|
|
498
|
+
|
|
499
|
+
Run a single test file or pattern:
|
|
408
500
|
|
|
409
501
|
```bash
|
|
410
|
-
|
|
502
|
+
cargo test --test entities
|
|
503
|
+
cargo test --test entities list_entities
|
|
411
504
|
```
|
|
412
505
|
|
|
413
506
|
## CI / Releasing
|
|
414
507
|
|
|
415
|
-
- **CI** (`.github/workflows/ci.yml`) runs on every push to `main` and on pull requests:
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
- **
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
(
|
|
424
|
-
|
|
508
|
+
- **CI** (`.github/workflows/ci.yml`) runs on every push to `main` and on pull requests: `cargo fmt
|
|
509
|
+
--check`, `clippy`, `cargo test`, the HA API check, a check that `TOOLS.md` is up to date, a build
|
|
510
|
+
of all five targets (`build.yml`, a reusable workflow also used by `publish.yml`) and
|
|
511
|
+
`scripts/check-npm-package.sh`.
|
|
512
|
+
- **HA API drift** (`.github/workflows/ha-api-drift.yml`): see [below](#api-drift-detection).
|
|
513
|
+
- **Pages** (`.github/workflows/pages.yml`) generates the tool data from the binary and deploys
|
|
514
|
+
`site/` to GitHub Pages on pushes to `main`.
|
|
515
|
+
- **Publish** (`.github/workflows/publish.yml`) runs when a GitHub Release is published. It builds
|
|
516
|
+
the five targets (Linux x64 and arm64 as static musl, macOS x64 and arm64, Windows x64), runs the
|
|
517
|
+
tests and `scripts/check-npm-package.sh`, and publishes to npm using
|
|
518
|
+
[trusted publishing](https://docs.npmjs.com/trusted-publishers) (OIDC): **no `NPM_TOKEN` secret
|
|
519
|
+
required**, with provenance generated automatically. It skips publishing if that version is
|
|
520
|
+
already on npm. For releases it then attaches `homeassistant-mcp-<platform>[.exe]` and
|
|
521
|
+
`SHA256SUMS` to the GitHub Release.
|
|
425
522
|
|
|
426
523
|
One-time setup on npmjs.com: open the package's **Settings → Trusted Publisher** and add a GitHub
|
|
427
524
|
Actions publisher for repository `OrellBuehler/homeassistant-mcp` with workflow `publish.yml`. If the
|
|
428
525
|
package doesn't exist on npm yet, do one manual `npm publish --access public` (after `npm login`) to
|
|
429
526
|
create it, then add the trusted publisher for all future releases.
|
|
430
527
|
|
|
431
|
-
Cut a release
|
|
528
|
+
Cut a release. `Cargo.toml` and `package.json` must carry the same version
|
|
529
|
+
(`scripts/check-npm-package.sh` checks it):
|
|
432
530
|
|
|
433
531
|
```bash
|
|
434
|
-
|
|
532
|
+
# bump `version` in Cargo.toml and package.json, then
|
|
533
|
+
cargo update -w
|
|
534
|
+
git commit -am "release 1.2.0"
|
|
535
|
+
git tag v1.2.0
|
|
435
536
|
git push --follow-tags
|
|
436
|
-
gh release create
|
|
537
|
+
gh release create v1.2.0 --generate-notes
|
|
437
538
|
```
|
|
438
539
|
|
|
439
540
|
## API drift detection
|
|
@@ -442,9 +543,11 @@ Home Assistant's WebSocket API has no published schema, so `scripts/ha-api.mjs`
|
|
|
442
543
|
Home Assistant core source: every WebSocket command with its parameters and every REST route. The
|
|
443
544
|
result is committed as `ha-api/snapshot.json`, pinned to a Home Assistant version.
|
|
444
545
|
|
|
445
|
-
- `
|
|
446
|
-
snapshot.
|
|
447
|
-
`
|
|
546
|
+
- `node scripts/ha-api.mjs check` (part of CI) fails if a tool calls a command or route that is not
|
|
547
|
+
in the snapshot. It finds them by scanning `src/tools/*.rs` for `.command("…")` calls and
|
|
548
|
+
`"/api/…"` string literals. Commands from custom integrations that are not in core (HACS) are
|
|
549
|
+
listed in `ha-api/allow.json`, and so are commands and routes the tools build at runtime
|
|
550
|
+
(under `used`), which the scan cannot see.
|
|
448
551
|
- The weekly **HA API drift** workflow compares the snapshot with the latest Home Assistant release.
|
|
449
552
|
When something changed it opens a pull request that updates the snapshot and lists added, removed
|
|
450
553
|
and changed commands, and it fails if a command this server uses was removed or changed. It needs
|
|
@@ -452,8 +555,8 @@ result is committed as `ha-api/snapshot.json`, pinned to a Home Assistant versio
|
|
|
452
555
|
it the report is filed as an issue instead.
|
|
453
556
|
- Run it for a specific version from the Actions tab (`workflow_dispatch`, input `version`).
|
|
454
557
|
|
|
455
|
-
The extraction is static, so commands whose names are built at runtime can be missed
|
|
456
|
-
parameter type shows up only as "schema body changed".
|
|
558
|
+
The extraction is static, so commands whose names are built at runtime can be missed (list them in
|
|
559
|
+
`ha-api/allow.json`), and a changed parameter type shows up only as "schema body changed".
|
|
457
560
|
|
|
458
561
|
## License
|
|
459
562
|
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const { spawn } = require("node:child_process");
|
|
5
|
+
const { existsSync, chmodSync } = require("node:fs");
|
|
6
|
+
const { join } = require("node:path");
|
|
7
|
+
|
|
8
|
+
const target = `${process.platform}-${process.arch}`;
|
|
9
|
+
const binary = join(
|
|
10
|
+
__dirname,
|
|
11
|
+
"..",
|
|
12
|
+
"binaries",
|
|
13
|
+
target,
|
|
14
|
+
process.platform === "win32" ? "homeassistant-mcp.exe" : "homeassistant-mcp",
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
if (!existsSync(binary)) {
|
|
18
|
+
console.error(
|
|
19
|
+
`homeassistant-mcp: no prebuilt binary for ${target}. Download one or build from source: https://github.com/OrellBuehler/homeassistant-mcp/releases`,
|
|
20
|
+
);
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (process.platform !== "win32") {
|
|
25
|
+
try {
|
|
26
|
+
chmodSync(binary, 0o755);
|
|
27
|
+
} catch {}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const child = spawn(binary, process.argv.slice(2), { stdio: "inherit", env: process.env });
|
|
31
|
+
|
|
32
|
+
const forwarders = ["SIGINT", "SIGTERM", "SIGHUP"].map((signal) => {
|
|
33
|
+
const forward = () => child.kill(signal);
|
|
34
|
+
process.on(signal, forward);
|
|
35
|
+
return [signal, forward];
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
child.on("error", (error) => {
|
|
39
|
+
console.error(`homeassistant-mcp: failed to start ${binary}: ${error.message}`);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
child.on("exit", (code, signal) => {
|
|
44
|
+
for (const [name, forward] of forwarders) process.removeListener(name, forward);
|
|
45
|
+
if (signal) {
|
|
46
|
+
process.kill(process.pid, signal);
|
|
47
|
+
setTimeout(() => process.exit(1), 100);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
process.exit(code ?? 1);
|
|
51
|
+
});
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,32 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orellbuehler/homeassistant-mcp",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Model Context Protocol server for Home Assistant: introspect entities, services, events, registries, render templates and validate configuration to help AI agents author HA configs and automations.",
|
|
5
|
-
"type": "module",
|
|
6
5
|
"bin": {
|
|
7
|
-
"homeassistant-mcp": "
|
|
6
|
+
"homeassistant-mcp": "bin/homeassistant-mcp.js"
|
|
8
7
|
},
|
|
9
8
|
"files": [
|
|
10
|
-
"
|
|
11
|
-
"
|
|
9
|
+
"bin/homeassistant-mcp.js",
|
|
10
|
+
"binaries"
|
|
12
11
|
],
|
|
13
|
-
"scripts": {
|
|
14
|
-
"build": "tsc -p tsconfig.build.json",
|
|
15
|
-
"start": "node dist/index.js",
|
|
16
|
-
"test": "vitest run",
|
|
17
|
-
"test:watch": "vitest",
|
|
18
|
-
"typecheck": "tsc --noEmit",
|
|
19
|
-
"lint": "eslint src",
|
|
20
|
-
"format": "prettier --write .",
|
|
21
|
-
"format:check": "prettier --check .",
|
|
22
|
-
"docs": "npm run build && node scripts/tool-docs.mjs",
|
|
23
|
-
"site": "node scripts/tool-docs.mjs site",
|
|
24
|
-
"api:check": "node scripts/ha-api.mjs check",
|
|
25
|
-
"api:uncovered": "node scripts/ha-api.mjs uncovered",
|
|
26
|
-
"prepare": "npm run build && prek install"
|
|
27
|
-
},
|
|
28
12
|
"engines": {
|
|
29
|
-
"node": ">=
|
|
13
|
+
"node": ">=18"
|
|
30
14
|
},
|
|
31
15
|
"keywords": [
|
|
32
16
|
"mcp",
|
|
@@ -55,22 +39,5 @@
|
|
|
55
39
|
},
|
|
56
40
|
"publishConfig": {
|
|
57
41
|
"access": "public"
|
|
58
|
-
},
|
|
59
|
-
"dependencies": {
|
|
60
|
-
"@modelcontextprotocol/sdk": "^1.12.1",
|
|
61
|
-
"ws": "^8.18.0",
|
|
62
|
-
"zod": "^3.24.4"
|
|
63
|
-
},
|
|
64
|
-
"devDependencies": {
|
|
65
|
-
"@eslint/js": "^10.0.1",
|
|
66
|
-
"@j178/prek": "^0.4.4",
|
|
67
|
-
"@types/node": "^26.6.7",
|
|
68
|
-
"@types/ws": "^8.5.12",
|
|
69
|
-
"eslint": "^10.4.1",
|
|
70
|
-
"eslint-config-prettier": "^10.1.8",
|
|
71
|
-
"prettier": "^3.8.3",
|
|
72
|
-
"typescript": "^5.8.3",
|
|
73
|
-
"typescript-eslint": "^8.60.0",
|
|
74
|
-
"vitest": "^5.1.1"
|
|
75
42
|
}
|
|
76
43
|
}
|
package/dist/config.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { HassClient } from "./hass/rest.js";
|
|
2
|
-
import { HassWsClient } from "./hass/ws.js";
|
|
3
|
-
const rawUrl = (process.env.HASS_URL || process.env.HASS_SERVER || "").trim();
|
|
4
|
-
const token = (process.env.HASS_TOKEN || "").trim();
|
|
5
|
-
if (!rawUrl || !token) {
|
|
6
|
-
console.error("HASS_URL (or HASS_SERVER) and HASS_TOKEN environment variables are required. " +
|
|
7
|
-
"HASS_TOKEN must be a Home Assistant long-lived access token.");
|
|
8
|
-
process.exit(1);
|
|
9
|
-
}
|
|
10
|
-
function normalizeBaseUrl(url) {
|
|
11
|
-
const withScheme = /^https?:\/\//i.test(url) ? url : `http://${url}`;
|
|
12
|
-
return withScheme.replace(/\/+$/, "");
|
|
13
|
-
}
|
|
14
|
-
export function deriveWsUrl(baseUrl) {
|
|
15
|
-
const ws = baseUrl.replace(/^https:/i, "wss:").replace(/^http:/i, "ws:");
|
|
16
|
-
return `${ws}/api/websocket`;
|
|
17
|
-
}
|
|
18
|
-
const baseUrl = normalizeBaseUrl(rawUrl);
|
|
19
|
-
export const config = {
|
|
20
|
-
baseUrl,
|
|
21
|
-
wsUrl: deriveWsUrl(baseUrl),
|
|
22
|
-
token,
|
|
23
|
-
transport: "stdio",
|
|
24
|
-
};
|
|
25
|
-
export const client = new HassClient(baseUrl, token);
|
|
26
|
-
export const wsClient = new HassWsClient(config.wsUrl, token);
|
package/dist/hass/format.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
export function buildQS(params) {
|
|
2
|
-
const sp = new URLSearchParams();
|
|
3
|
-
for (const [k, v] of Object.entries(params)) {
|
|
4
|
-
if (v === undefined || v === null)
|
|
5
|
-
continue;
|
|
6
|
-
if (Array.isArray(v)) {
|
|
7
|
-
if (v.length === 0)
|
|
8
|
-
continue;
|
|
9
|
-
sp.set(k, v.join(","));
|
|
10
|
-
}
|
|
11
|
-
else {
|
|
12
|
-
sp.set(k, String(v));
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
const s = sp.toString();
|
|
16
|
-
return s ? `?${s}` : "";
|
|
17
|
-
}
|
|
18
|
-
export function ok(data) {
|
|
19
|
-
const text = typeof data === "string" ? data : JSON.stringify(data);
|
|
20
|
-
return { content: [{ type: "text", text }] };
|
|
21
|
-
}
|
|
22
|
-
export function err(e) {
|
|
23
|
-
return { content: [{ type: "text", text: String(e) }], isError: true };
|
|
24
|
-
}
|
|
25
|
-
export function domainOf(entityId) {
|
|
26
|
-
return entityId.split(".")[0];
|
|
27
|
-
}
|
|
28
|
-
export function summarizeState(s) {
|
|
29
|
-
const a = s.attributes ?? {};
|
|
30
|
-
const out = { entity_id: s.entity_id, state: s.state };
|
|
31
|
-
if (a.friendly_name !== undefined)
|
|
32
|
-
out.friendly_name = a.friendly_name;
|
|
33
|
-
if (a.device_class !== undefined)
|
|
34
|
-
out.device_class = a.device_class;
|
|
35
|
-
if (a.unit_of_measurement !== undefined)
|
|
36
|
-
out.unit = a.unit_of_measurement;
|
|
37
|
-
return out;
|
|
38
|
-
}
|
package/dist/hass/rest.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
export class HassClient {
|
|
2
|
-
baseUrl;
|
|
3
|
-
token;
|
|
4
|
-
timeoutMs;
|
|
5
|
-
constructor(baseUrl, token, timeoutMs = 15000) {
|
|
6
|
-
this.baseUrl = baseUrl.replace(/\/+$/, "");
|
|
7
|
-
this.token = token;
|
|
8
|
-
this.timeoutMs = timeoutMs;
|
|
9
|
-
}
|
|
10
|
-
headers(options) {
|
|
11
|
-
return {
|
|
12
|
-
Authorization: `Bearer ${this.token}`,
|
|
13
|
-
...(typeof options.body === "string" ? { "Content-Type": "application/json" } : {}),
|
|
14
|
-
...(options.headers ?? {}),
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
async request(path, options) {
|
|
18
|
-
let res;
|
|
19
|
-
try {
|
|
20
|
-
res = await fetch(`${this.baseUrl}${path}`, {
|
|
21
|
-
...options,
|
|
22
|
-
headers: this.headers(options),
|
|
23
|
-
signal: options.signal ?? AbortSignal.timeout(this.timeoutMs),
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
catch (e) {
|
|
27
|
-
if (e instanceof DOMException && e.name === "TimeoutError") {
|
|
28
|
-
throw new Error(`Home Assistant request timed out after ${this.timeoutMs}ms`, { cause: e });
|
|
29
|
-
}
|
|
30
|
-
throw e;
|
|
31
|
-
}
|
|
32
|
-
if (!res.ok) {
|
|
33
|
-
const body = await res.text();
|
|
34
|
-
throw new Error(`${res.status} ${res.statusText}: ${body}`);
|
|
35
|
-
}
|
|
36
|
-
return res;
|
|
37
|
-
}
|
|
38
|
-
async fetch(path, options = {}) {
|
|
39
|
-
const res = await this.request(path, options);
|
|
40
|
-
if (res.status === 204)
|
|
41
|
-
return { success: true };
|
|
42
|
-
const ct = res.headers.get("content-type") ?? "";
|
|
43
|
-
if (ct.includes("application/json"))
|
|
44
|
-
return res.json();
|
|
45
|
-
return res.text();
|
|
46
|
-
}
|
|
47
|
-
async fetchText(path, options = {}) {
|
|
48
|
-
return (await this.request(path, options)).text();
|
|
49
|
-
}
|
|
50
|
-
}
|
package/dist/hass/ws.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import WebSocket from "ws";
|
|
2
|
-
export class HassWsClient {
|
|
3
|
-
wsUrl;
|
|
4
|
-
token;
|
|
5
|
-
timeoutMs;
|
|
6
|
-
constructor(wsUrl, token, timeoutMs = 15000) {
|
|
7
|
-
this.wsUrl = wsUrl;
|
|
8
|
-
this.token = token;
|
|
9
|
-
this.timeoutMs = timeoutMs;
|
|
10
|
-
}
|
|
11
|
-
command(type, payload = {}) {
|
|
12
|
-
return new Promise((resolve, reject) => {
|
|
13
|
-
const ws = new WebSocket(this.wsUrl);
|
|
14
|
-
const id = 1;
|
|
15
|
-
let settled = false;
|
|
16
|
-
const timer = setTimeout(() => {
|
|
17
|
-
if (settled)
|
|
18
|
-
return;
|
|
19
|
-
settled = true;
|
|
20
|
-
ws.terminate();
|
|
21
|
-
reject(new Error(`Home Assistant WebSocket timed out after ${this.timeoutMs}ms`));
|
|
22
|
-
}, this.timeoutMs);
|
|
23
|
-
const finish = (run) => {
|
|
24
|
-
if (settled)
|
|
25
|
-
return;
|
|
26
|
-
settled = true;
|
|
27
|
-
clearTimeout(timer);
|
|
28
|
-
try {
|
|
29
|
-
ws.close();
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
/* ignore close errors */
|
|
33
|
-
}
|
|
34
|
-
run();
|
|
35
|
-
};
|
|
36
|
-
ws.on("message", (raw) => {
|
|
37
|
-
let msg;
|
|
38
|
-
try {
|
|
39
|
-
msg = JSON.parse(raw.toString());
|
|
40
|
-
}
|
|
41
|
-
catch (e) {
|
|
42
|
-
finish(() => reject(new Error(`Invalid Home Assistant WebSocket message: ${String(e)}`)));
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
if (msg.type === "auth_required") {
|
|
46
|
-
ws.send(JSON.stringify({ type: "auth", access_token: this.token }));
|
|
47
|
-
}
|
|
48
|
-
else if (msg.type === "auth_invalid") {
|
|
49
|
-
finish(() => reject(new Error(`Home Assistant authentication failed: ${msg.message ?? "invalid token"}`)));
|
|
50
|
-
}
|
|
51
|
-
else if (msg.type === "auth_ok") {
|
|
52
|
-
ws.send(JSON.stringify({ id, type, ...payload }));
|
|
53
|
-
}
|
|
54
|
-
else if (msg.type === "result" && msg.id === id) {
|
|
55
|
-
if (msg.success) {
|
|
56
|
-
finish(() => resolve(msg.result));
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
finish(() => {
|
|
60
|
-
const error = new Error(`Home Assistant WebSocket error: ${msg.error?.message ?? "unknown error"}`);
|
|
61
|
-
if (msg.error?.code)
|
|
62
|
-
error.code = msg.error.code;
|
|
63
|
-
reject(error);
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
ws.on("error", (e) => {
|
|
69
|
-
finish(() => reject(new Error(`Home Assistant WebSocket connection error: ${String(e)}`)));
|
|
70
|
-
});
|
|
71
|
-
ws.on("close", () => {
|
|
72
|
-
if (settled)
|
|
73
|
-
return;
|
|
74
|
-
settled = true;
|
|
75
|
-
clearTimeout(timer);
|
|
76
|
-
reject(new Error("Home Assistant WebSocket closed before a result was received"));
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
package/dist/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
|
-
import { client, wsClient } from "./config.js";
|
|
4
|
-
import { createServer } from "./server.js";
|
|
5
|
-
import { parseToolOptions } from "./toolsets.js";
|
|
6
|
-
const options = parseToolOptions(process.env);
|
|
7
|
-
for (const warning of options.warnings)
|
|
8
|
-
console.error(warning);
|
|
9
|
-
const server = createServer(client, wsClient, options);
|
|
10
|
-
const transport = new StdioServerTransport();
|
|
11
|
-
try {
|
|
12
|
-
await server.connect(transport);
|
|
13
|
-
}
|
|
14
|
-
catch (e) {
|
|
15
|
-
console.error(`Failed to start homeassistant-mcp: ${String(e)}`);
|
|
16
|
-
process.exit(1);
|
|
17
|
-
}
|
package/dist/server.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
2
|
-
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
-
import { parseToolOptions, registerToolsets } from "./toolsets.js";
|
|
4
|
-
const { version } = createRequire(import.meta.url)("../package.json");
|
|
5
|
-
export function createServer(client, wsClient, options = parseToolOptions({})) {
|
|
6
|
-
const server = new McpServer({ name: "homeassistant-mcp", version });
|
|
7
|
-
registerToolsets(server, client, wsClient, options);
|
|
8
|
-
return server;
|
|
9
|
-
}
|