credential-use-mcp 0.0.0-stage → 0.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/CHANGELOG.md +26 -0
- package/LICENSE +21 -0
- package/README.md +168 -2
- package/dist/src/approvals.js +90 -0
- package/dist/src/config.js +70 -0
- package/dist/src/events.js +149 -0
- package/dist/src/fields.js +112 -0
- package/dist/src/index.js +20 -0
- package/dist/src/kdbx.js +343 -0
- package/dist/src/page.js +170 -0
- package/dist/src/proxy.js +171 -0
- package/dist/src/server.js +105 -0
- package/dist/src/store.js +147 -0
- package/dist/src/tools.js +481 -0
- package/package.json +60 -4
- package/skills/credential-use/SKILL.md +81 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `credential-use-mcp`. Format based on
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/); versioning follows [SemVer](https://semver.org/).
|
|
5
|
+
|
|
6
|
+
## [Unreleased]
|
|
7
|
+
|
|
8
|
+
## [0.1.0] — 2026-12-05
|
|
9
|
+
|
|
10
|
+
First version.
|
|
11
|
+
|
|
12
|
+
- KeePassXC (KDBX) store, opened fresh for every operation so KeePassXC can edit the file at any time. The
|
|
13
|
+
master password comes from `CREDUSE_KDBX_PASSWORD`, `CREDUSE_KDBX_PASSWORD_ENV` (the name of another
|
|
14
|
+
variable), `CREDUSE_KDBX_PASSWORD_FILE` or a key file.
|
|
15
|
+
- `cred_list`, `cred_add`, `cred_edit`, `cred_remove`, `cred_reveal`: credential CRUD over MCP elicitation,
|
|
16
|
+
run by the agent and approved by the user. What the agent proposes (name, kind, URL, username, header, notes,
|
|
17
|
+
and a secret it already has) comes prefilled for the user to check or edit before submitting; without a
|
|
18
|
+
proposed secret the user types it and the agent never sees it. `cred_reveal` gives the agent a value after
|
|
19
|
+
the user approves that one read. Preferred mode (`form` or `url`) set by `CREDUSE_SECRET_INPUT`; the other
|
|
20
|
+
one is used when the client cannot show it. URL pages are served on loopback and, with `CREDUSE_URL_ACCESS`,
|
|
21
|
+
on an OpenTunnel route; a proposed secret is shown in clear there, a typed one in a password field.
|
|
22
|
+
- `proxy_open`, `proxy_list`, `proxy_close`: local HTTP proxies that add a credential's authentication (bearer,
|
|
23
|
+
basic, API-key header), approved by the user for some minutes and/or a number of requests.
|
|
24
|
+
- `credentials.changed` event (MCP Events extension) on additions, updates and removals, also from file edits.
|
|
25
|
+
- Server instructions and the `credential-use` skill (MCP skills extension).
|
|
26
|
+
- Both protocol eras over stdio: 2026-07-28 and 2025 (`initialize`, questions sent as `elicitation/create`).
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Antonio Orizondo Leyva
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,169 @@
|
|
|
1
|
-
#
|
|
1
|
+
# credential-use-mcp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An [MCP](https://modelcontextprotocol.io) server that lets an AI agent manage and **use** passwords, tokens and
|
|
4
|
+
API keys, **with your approval at every step** and, whenever possible, **without seeing them**.
|
|
5
|
+
|
|
6
|
+
- Credentials live in a **KeePassXC / KeePass (KDBX) file** you can open and edit yourself.
|
|
7
|
+
- When a task needs a secret, the agent asks for it with `cred_add`: **you** type it in a form or a web page,
|
|
8
|
+
and it goes straight to the file. The agent only learns the name, kind, host and username.
|
|
9
|
+
- The agent can run the whole CRUD itself: what it proposes (even a secret it already has, such as a password
|
|
10
|
+
it generated) comes **prefilled** in the form, and you check or edit it before submitting. Reading a secret
|
|
11
|
+
(`cred_reveal`) needs your approval each time.
|
|
12
|
+
- To call an API, the agent opens a **local proxy** (`proxy_open`). You approve it for some minutes and/or a
|
|
13
|
+
number of requests; the agent sends requests to `http://127.0.0.1:<port>` and the proxy adds the
|
|
14
|
+
authentication. The secret never enters the conversation.
|
|
15
|
+
- An MCP event, `credentials.changed`, fires when a credential is added, updated or removed (also when you edit
|
|
16
|
+
the file in KeePassXC).
|
|
17
|
+
- The server tells the agent how to use it (MCP instructions) and ships a skill with worked examples.
|
|
18
|
+
|
|
19
|
+
It speaks stdio and both MCP protocol eras (2026-07-28 and 2025), so it works with OpenCode, Claude Code,
|
|
20
|
+
Hermes, VS Code and any client that supports **elicitation** (forms).
|
|
21
|
+
|
|
22
|
+
## Quick start
|
|
23
|
+
|
|
24
|
+
Requires Node 22 or later.
|
|
25
|
+
|
|
26
|
+
```jsonc
|
|
27
|
+
// OpenCode (opencode.json)
|
|
28
|
+
{
|
|
29
|
+
"mcp": {
|
|
30
|
+
"servers": {
|
|
31
|
+
"credential-use": {
|
|
32
|
+
"type": "local",
|
|
33
|
+
"command": ["npx", "-y", "credential-use-mcp"],
|
|
34
|
+
"environment": {
|
|
35
|
+
"CREDUSE_KDBX_PATH": "~/.config/credential-use/credentials.kdbx",
|
|
36
|
+
"CREDUSE_KDBX_PASSWORD_FILE": "~/.config/credential-use/password"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
# Claude Code
|
|
46
|
+
claude mcp add credential-use -e CREDUSE_KDBX_PASSWORD_FILE=~/.config/credential-use/password -- npx -y credential-use-mcp
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The file is created on first use if it does not exist. Put the master password in a file only you can read
|
|
50
|
+
(`chmod 600`), or pass it in `CREDUSE_KDBX_PASSWORD`.
|
|
51
|
+
|
|
52
|
+
Then ask the agent something like *"store my GitHub token and list my repos"*.
|
|
53
|
+
|
|
54
|
+
## Tools
|
|
55
|
+
|
|
56
|
+
| Tool | What it does |
|
|
57
|
+
| ------------- | ------------------------------------------------------------------------------------------------ |
|
|
58
|
+
| `cred_list` | Names, kinds, hosts and usernames of the stored credentials. Never a secret. |
|
|
59
|
+
| `cred_add` | A new credential, in a form prefilled with what the agent proposes (name, kind, URL, username, header, notes, and the secret only if it already has it). Without a proposed secret you type it. |
|
|
60
|
+
| `cred_edit` | Changes one: the form shows its public fields plus the agent's proposed changes. An empty secret keeps it. |
|
|
61
|
+
| `cred_remove` | Deletes one after you confirm. |
|
|
62
|
+
| `cred_reveal` | Gives the agent a secret's value, after you approve that one read (you see its reason). |
|
|
63
|
+
| `proxy_open` | Opens a local proxy to an HTTP credential's service, after your approval. |
|
|
64
|
+
| `proxy_list` | Open proxies and approvals still valid. |
|
|
65
|
+
| `proxy_close` | Closes a proxy and ends its approval. |
|
|
66
|
+
|
|
67
|
+
Credential kinds and what the proxy sends:
|
|
68
|
+
|
|
69
|
+
| Kind | Header |
|
|
70
|
+
| ------------- | ----------------------------------------------------------- |
|
|
71
|
+
| `http-bearer` | `Authorization: Bearer <secret>` |
|
|
72
|
+
| `http-basic` | `Authorization: Basic <username:secret>` |
|
|
73
|
+
| `api-key` | `<header>: <secret>` (header name defaults to `x-api-key`) |
|
|
74
|
+
| `generic` | bearer, or basic when it has a username |
|
|
75
|
+
| `ssh`, `database` | stored, but not proxied yet |
|
|
76
|
+
|
|
77
|
+
## How you are asked: form or page
|
|
78
|
+
|
|
79
|
+
| Mode | What you see | Secret while typing |
|
|
80
|
+
| ------ | ------------------------------------------------------------------ | ------------------- |
|
|
81
|
+
| `form` | A form inside your agent app | Visible |
|
|
82
|
+
| `url` | A link to a page served by this server, with a real password field | Hidden |
|
|
83
|
+
|
|
84
|
+
`CREDUSE_SECRET_INPUT` picks the one you prefer. If your client cannot show it (or the page cannot be served),
|
|
85
|
+
the other one is used. Pages live on `127.0.0.1`, use a random single-use link that expires in thirty minutes, and
|
|
86
|
+
the answer goes straight to this process. For an agent on another machine, `CREDUSE_URL_ACCESS=opentunnel`
|
|
87
|
+
(or `both`) also publishes the page on an [OpenTunnel](https://opentunnel.xyz) route.
|
|
88
|
+
|
|
89
|
+
## Approvals
|
|
90
|
+
|
|
91
|
+
Nothing is stored, changed, deleted or revealed until you submit the form or page. When the agent proposes a
|
|
92
|
+
secret, it is shown in clear (the agent already knows it) so you can check it; a secret you type stays hidden on
|
|
93
|
+
the page and is never shown to the agent. The answer tells the agent whether you kept its proposed secret, never
|
|
94
|
+
the value you typed instead.
|
|
95
|
+
|
|
96
|
+
`proxy_open` asks you, every time there is no valid approval for that credential:
|
|
97
|
+
|
|
98
|
+
- allow for some minutes, for a number of requests, or both;
|
|
99
|
+
- allow one request;
|
|
100
|
+
- deny.
|
|
101
|
+
|
|
102
|
+
While an approval lasts, `proxy_open` returns the same proxy without asking, and every request through the proxy
|
|
103
|
+
uses one. When it runs out the proxy answers `403` and the agent must call `proxy_open` again. Approvals and
|
|
104
|
+
proxies live in memory: they end with the server process (your agent session). A proxy also closes when its
|
|
105
|
+
credential is changed or removed.
|
|
106
|
+
|
|
107
|
+
The proxy only reaches the credential's own origin (scheme, host and port), drops the agent's `Authorization`,
|
|
108
|
+
`Cookie` and `Proxy-Authorization` headers, and replaces the secret with `[REDACTED]` if a text response
|
|
109
|
+
echoes it.
|
|
110
|
+
|
|
111
|
+
## Configuration
|
|
112
|
+
|
|
113
|
+
All settings are environment variables.
|
|
114
|
+
|
|
115
|
+
| Variable | Default | Meaning |
|
|
116
|
+
| ---------------------------- | ------------------------------------------- | ---------------------------------------------------------- |
|
|
117
|
+
| `CREDUSE_KDBX_PATH` | `~/.config/credential-use/credentials.kdbx` | The KDBX file |
|
|
118
|
+
| `CREDUSE_KDBX_PASSWORD` | | Master password |
|
|
119
|
+
| `CREDUSE_KDBX_PASSWORD_ENV` | | Name of another variable that holds the master password |
|
|
120
|
+
| `CREDUSE_KDBX_PASSWORD_FILE` | | File whose first line is the master password |
|
|
121
|
+
| `CREDUSE_KDBX_KEYFILE` | | KeePass key file (alone or with a password) |
|
|
122
|
+
| `CREDUSE_KDBX_GROUP` | `credential-use` | Group where new entries go |
|
|
123
|
+
| `CREDUSE_KDBX_CREATE` | `true` | Create the file when missing |
|
|
124
|
+
| `CREDUSE_MIN_SECRET_LENGTH` | `6` | Shorter secrets are refused |
|
|
125
|
+
| `CREDUSE_SECRET_INPUT` | `form` | Preferred way to ask: `form` or `url` |
|
|
126
|
+
| `CREDUSE_URL_ACCESS` | `loopback` | Where pages are offered: `loopback`, `opentunnel`, `both` |
|
|
127
|
+
| `CREDUSE_APPROVAL_MINUTES` | `15` | Minutes proposed when approving a proxy |
|
|
128
|
+
| `CREDUSE_APPROVAL_USES` | `0` | Requests proposed (0: propose a time limit only) |
|
|
129
|
+
|
|
130
|
+
One of the password, password file or key file is required; without it every tool explains what to set.
|
|
131
|
+
|
|
132
|
+
## Entries in the file
|
|
133
|
+
|
|
134
|
+
Each credential is an entry whose **title** is its name: username, password (the secret), URL and notes are the
|
|
135
|
+
standard fields. The kind is the custom field `apex:kind` and the API-key header the custom field `header`
|
|
136
|
+
(compatible with files created by [opencode-apex](https://github.com/aorizondo/opencode-apex)). You can edit
|
|
137
|
+
entries in KeePassXC; the server notices and emits `credentials.changed`.
|
|
138
|
+
|
|
139
|
+
## Events
|
|
140
|
+
|
|
141
|
+
With the MCP Events extension (protocol 2026-07-28) a client can subscribe to `credentials.changed`
|
|
142
|
+
(`events/stream`, arguments `{ action?, name? }`). In OpenCode it can trigger an automation:
|
|
143
|
+
|
|
144
|
+
```json
|
|
145
|
+
{ "type": "event", "server": "credential-use", "event": "credentials.changed", "args": { "action": "added" } }
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Security notes
|
|
149
|
+
|
|
150
|
+
- In **form** mode the secret is visible on screen while you type, and passes through your agent app's memory
|
|
151
|
+
(never the model). Use **url** mode to type it in a hidden field.
|
|
152
|
+
- The KDBX file is written atomically with mode `0600`. Keep its password out of config files and shell
|
|
153
|
+
history: prefer `CREDUSE_KDBX_PASSWORD_FILE`, or `CREDUSE_KDBX_PASSWORD_ENV` naming a variable your agent app
|
|
154
|
+
already has (local MCP servers inherit its environment).
|
|
155
|
+
- Anything that can reach `127.0.0.1` on your machine can use an open proxy while its approval lasts. Keep
|
|
156
|
+
approvals short on shared machines.
|
|
157
|
+
- Responses from the proxied API reach the agent as they are (except the secret itself, scrubbed).
|
|
158
|
+
|
|
159
|
+
## Development
|
|
160
|
+
|
|
161
|
+
```sh
|
|
162
|
+
npm install
|
|
163
|
+
npm test # end-to-end over stdio (Bun), build, smoke test of the build under Node
|
|
164
|
+
npm run typecheck
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## License
|
|
168
|
+
|
|
169
|
+
MIT
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The user's approvals to use a credential through a proxy: for some minutes, for a number of
|
|
3
|
+
* requests, or both. While one is valid the agent can (re)open the proxy without asking again;
|
|
4
|
+
* every request through the proxy uses one. They live in memory, as long as this process.
|
|
5
|
+
*/
|
|
6
|
+
import { count, text } from "./fields.js";
|
|
7
|
+
export class Approvals {
|
|
8
|
+
#grants = new Map();
|
|
9
|
+
/** The credential's approval if it still allows a request. */
|
|
10
|
+
valid(credential, now = Date.now()) {
|
|
11
|
+
const grant = this.#grants.get(credential);
|
|
12
|
+
if (!grant)
|
|
13
|
+
return undefined;
|
|
14
|
+
if ((grant.expiresAt !== null && grant.expiresAt <= now) || grant.usesLeft === 0) {
|
|
15
|
+
this.#grants.delete(credential);
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
return grant;
|
|
19
|
+
}
|
|
20
|
+
/** Records the user's answer; null for "deny". */
|
|
21
|
+
grant(credential, answer, now = Date.now()) {
|
|
22
|
+
if (answer.decision === "deny") {
|
|
23
|
+
this.#grants.delete(credential);
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const timed = answer.decision === "time" || answer.decision === "both";
|
|
27
|
+
const counted = answer.decision === "uses" || answer.decision === "both";
|
|
28
|
+
const grant = {
|
|
29
|
+
credential,
|
|
30
|
+
createdAt: now,
|
|
31
|
+
expiresAt: timed ? now + Math.max(1, answer.minutes ?? 15) * 60_000 : null,
|
|
32
|
+
usesLeft: answer.decision === "once" ? 1 : counted ? Math.max(1, answer.uses ?? 1) : null,
|
|
33
|
+
};
|
|
34
|
+
this.#grants.set(credential, grant);
|
|
35
|
+
return grant;
|
|
36
|
+
}
|
|
37
|
+
/** Spends one request of the approval; false when there is none left. */
|
|
38
|
+
consume(credential) {
|
|
39
|
+
const grant = this.valid(credential);
|
|
40
|
+
if (!grant)
|
|
41
|
+
return false;
|
|
42
|
+
if (grant.usesLeft !== null)
|
|
43
|
+
grant.usesLeft--;
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
revoke(credential) {
|
|
47
|
+
return this.#grants.delete(credential);
|
|
48
|
+
}
|
|
49
|
+
list() {
|
|
50
|
+
return [...this.#grants.keys()].map((name) => this.valid(name)).filter((grant) => grant !== undefined);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** The approval form. Defaults come from CREDUSE_APPROVAL_MINUTES / CREDUSE_APPROVAL_USES. */
|
|
54
|
+
export function approvalFields(defaults) {
|
|
55
|
+
return [
|
|
56
|
+
{
|
|
57
|
+
key: "decision",
|
|
58
|
+
label: "Decision",
|
|
59
|
+
type: "choice",
|
|
60
|
+
required: true,
|
|
61
|
+
default: defaults.uses > 0 ? "both" : "time",
|
|
62
|
+
options: [
|
|
63
|
+
{ value: "time", label: "Allow for some minutes" },
|
|
64
|
+
{ value: "uses", label: "Allow a number of requests" },
|
|
65
|
+
{ value: "both", label: "Allow for some minutes, up to a number of requests" },
|
|
66
|
+
{ value: "once", label: "Allow one request" },
|
|
67
|
+
{ value: "deny", label: "Deny" },
|
|
68
|
+
],
|
|
69
|
+
},
|
|
70
|
+
{ key: "minutes", label: "Minutes", type: "number", minimum: 1, default: defaults.minutes },
|
|
71
|
+
{ key: "uses", label: "Requests", type: "number", minimum: 1, default: defaults.uses > 0 ? defaults.uses : 20 },
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
export function readDecision(values) {
|
|
75
|
+
const decision = text(values.decision);
|
|
76
|
+
const known = ["deny", "once", "time", "uses", "both"];
|
|
77
|
+
return {
|
|
78
|
+
decision: known.includes(decision) ? decision : "deny",
|
|
79
|
+
minutes: count(values.minutes),
|
|
80
|
+
uses: count(values.uses),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export function describeGrant(grant, now = Date.now()) {
|
|
84
|
+
const parts = [];
|
|
85
|
+
if (grant.expiresAt !== null)
|
|
86
|
+
parts.push(`${Math.max(0, Math.round((grant.expiresAt - now) / 60_000))} min left`);
|
|
87
|
+
if (grant.usesLeft !== null)
|
|
88
|
+
parts.push(`${grant.usesLeft} request(s) left`);
|
|
89
|
+
return parts.length > 0 ? parts.join(", ") : "no limit";
|
|
90
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration, from environment variables only (an MCP client passes them in the server's `env`).
|
|
3
|
+
*
|
|
4
|
+
* | Variable | Default |
|
|
5
|
+
* | ------------------------------ | ---------------------------------------------- |
|
|
6
|
+
* | CREDUSE_KDBX_PATH | ~/.config/credential-use/credentials.kdbx |
|
|
7
|
+
* | CREDUSE_KDBX_PASSWORD | (one of password / password file / key file) |
|
|
8
|
+
* | CREDUSE_KDBX_PASSWORD_ENV | name of another variable holding the password |
|
|
9
|
+
* | CREDUSE_KDBX_PASSWORD_FILE | file holding the password (first line) |
|
|
10
|
+
* | CREDUSE_KDBX_KEYFILE | KeePass key file |
|
|
11
|
+
* | CREDUSE_KDBX_GROUP | credential-use (group for new entries) |
|
|
12
|
+
* | CREDUSE_KDBX_CREATE | true (create the file when missing) |
|
|
13
|
+
* | CREDUSE_MIN_SECRET_LENGTH | 6 |
|
|
14
|
+
* | CREDUSE_SECRET_INPUT | form · or url (preferred; falls back) |
|
|
15
|
+
* | CREDUSE_URL_ACCESS | loopback · or opentunnel, both |
|
|
16
|
+
* | CREDUSE_APPROVAL_MINUTES | 15 (default offered when approving a proxy) |
|
|
17
|
+
* | CREDUSE_APPROVAL_USES | 0 (0 = no limit on requests) |
|
|
18
|
+
*/
|
|
19
|
+
import { readFileSync } from "node:fs";
|
|
20
|
+
import { homedir } from "node:os";
|
|
21
|
+
import { join } from "node:path";
|
|
22
|
+
export function loadConfig(env = process.env) {
|
|
23
|
+
const password = env.CREDUSE_KDBX_PASSWORD ||
|
|
24
|
+
(env.CREDUSE_KDBX_PASSWORD_ENV ? env[env.CREDUSE_KDBX_PASSWORD_ENV] : undefined) ||
|
|
25
|
+
readFirstLine(env.CREDUSE_KDBX_PASSWORD_FILE);
|
|
26
|
+
const keyFile = env.CREDUSE_KDBX_KEYFILE ? expandHome(env.CREDUSE_KDBX_KEYFILE) : undefined;
|
|
27
|
+
return {
|
|
28
|
+
kdbx: {
|
|
29
|
+
path: expandHome(env.CREDUSE_KDBX_PATH || join(homedir(), ".config", "credential-use", "credentials.kdbx")),
|
|
30
|
+
...(password ? { password } : {}),
|
|
31
|
+
...(keyFile ? { keyFile } : {}),
|
|
32
|
+
group: env.CREDUSE_KDBX_GROUP || "credential-use",
|
|
33
|
+
createIfMissing: env.CREDUSE_KDBX_CREATE !== "false",
|
|
34
|
+
},
|
|
35
|
+
minSecretLength: positiveInt(env.CREDUSE_MIN_SECRET_LENGTH, 6),
|
|
36
|
+
secretInput: env.CREDUSE_SECRET_INPUT === "url" ? "url" : "form",
|
|
37
|
+
urlAccess: env.CREDUSE_URL_ACCESS === "opentunnel" || env.CREDUSE_URL_ACCESS === "both" ? env.CREDUSE_URL_ACCESS : "loopback",
|
|
38
|
+
approval: {
|
|
39
|
+
minutes: positiveInt(env.CREDUSE_APPROVAL_MINUTES, 15),
|
|
40
|
+
uses: nonNegativeInt(env.CREDUSE_APPROVAL_USES, 0),
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** Why the store cannot open, or null when it has a key. */
|
|
45
|
+
export function storeProblem(config) {
|
|
46
|
+
if (config.kdbx.password || config.kdbx.keyFile)
|
|
47
|
+
return null;
|
|
48
|
+
return "No KDBX key: set CREDUSE_KDBX_PASSWORD, CREDUSE_KDBX_PASSWORD_ENV, CREDUSE_KDBX_PASSWORD_FILE or CREDUSE_KDBX_KEYFILE in the MCP server's environment (CREDUSE_KDBX_PASSWORD_ENV must name a variable that is set).";
|
|
49
|
+
}
|
|
50
|
+
export function expandHome(path) {
|
|
51
|
+
return path === "~" || path.startsWith("~/") ? join(homedir(), path.slice(1)) : path;
|
|
52
|
+
}
|
|
53
|
+
function readFirstLine(file) {
|
|
54
|
+
if (!file)
|
|
55
|
+
return undefined;
|
|
56
|
+
try {
|
|
57
|
+
return readFileSync(expandHome(file), "utf8").split(/\r?\n/)[0]?.trim() || undefined;
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function positiveInt(value, fallback) {
|
|
64
|
+
const n = Number(value);
|
|
65
|
+
return Number.isInteger(n) && n > 0 ? n : fallback;
|
|
66
|
+
}
|
|
67
|
+
function nonNegativeInt(value, fallback) {
|
|
68
|
+
const n = Number(value);
|
|
69
|
+
return Number.isInteger(n) && n >= 0 ? n : fallback;
|
|
70
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Events extension (io.modelcontextprotocol/events, protocol 2026-07-28): one event type,
|
|
3
|
+
* `credentials.changed`, pushed when a credential is added, updated or removed, through this
|
|
4
|
+
* server or in the file itself (KeePassXC). A client can subscribe (`events/stream`) or use it as
|
|
5
|
+
* an automation trigger, e.g. in OpenCode:
|
|
6
|
+
*
|
|
7
|
+
* { type: "event", server: "credential-use", event: "credentials.changed", args: { action: "added" } }
|
|
8
|
+
*
|
|
9
|
+
* The payload carries public metadata only (name, kind, host, username), never a secret.
|
|
10
|
+
* Recent events stay in a small in-memory buffer so a stream can resume from its cursor.
|
|
11
|
+
*/
|
|
12
|
+
import { randomBytes } from "node:crypto";
|
|
13
|
+
import { ProtocolError } from "@modelcontextprotocol/server";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
export const EVENTS_EXTENSION = "io.modelcontextprotocol/events";
|
|
16
|
+
export const EVENT_NAME = "credentials.changed";
|
|
17
|
+
const SUBSCRIPTION_ID = "io.modelcontextprotocol/subscriptionId";
|
|
18
|
+
const BUFFER_SIZE = 200;
|
|
19
|
+
const NOT_FOUND = -32023;
|
|
20
|
+
const INVALID_PARAMS = -32602;
|
|
21
|
+
export const EVENT_TYPE = {
|
|
22
|
+
name: EVENT_NAME,
|
|
23
|
+
description: "A credential was added, updated or removed (through this server or by editing the KDBX file). " +
|
|
24
|
+
"Filter with arguments { action?: added|updated|removed, name? }.",
|
|
25
|
+
delivery: ["push"],
|
|
26
|
+
inputSchema: {
|
|
27
|
+
type: "object",
|
|
28
|
+
properties: {
|
|
29
|
+
action: { type: "string", enum: ["added", "updated", "removed"] },
|
|
30
|
+
name: { type: "string", description: "Only this credential" },
|
|
31
|
+
},
|
|
32
|
+
additionalProperties: false,
|
|
33
|
+
},
|
|
34
|
+
payloadSchema: {
|
|
35
|
+
type: "object",
|
|
36
|
+
properties: {
|
|
37
|
+
id: { type: "string" },
|
|
38
|
+
action: { type: "string", enum: ["added", "updated", "removed"] },
|
|
39
|
+
name: { type: "string" },
|
|
40
|
+
credential: { type: "object", additionalProperties: true, description: "Public metadata (absent when removed)" },
|
|
41
|
+
source: { type: "string", enum: ["tool", "file"] },
|
|
42
|
+
at: { type: "string", description: "ISO-8601" },
|
|
43
|
+
},
|
|
44
|
+
required: ["id", "action", "name", "source", "at"],
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
/** Process-wide: every server instance (one per connection) streams from the same hub. */
|
|
48
|
+
export class EventHub {
|
|
49
|
+
#epoch = String(Date.now());
|
|
50
|
+
#buffer = [];
|
|
51
|
+
#listeners = new Set();
|
|
52
|
+
#seq = 0;
|
|
53
|
+
publish(change) {
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
const event = {
|
|
56
|
+
seq: ++this.#seq,
|
|
57
|
+
at: now,
|
|
58
|
+
data: { ...change, id: `ev_${randomBytes(6).toString("hex")}`, at: new Date(now).toISOString() },
|
|
59
|
+
};
|
|
60
|
+
this.#buffer.push(event);
|
|
61
|
+
if (this.#buffer.length > BUFFER_SIZE)
|
|
62
|
+
this.#buffer.shift();
|
|
63
|
+
for (const listener of this.#listeners)
|
|
64
|
+
listener(event);
|
|
65
|
+
}
|
|
66
|
+
/** Registers events/list and events/stream on a server. */
|
|
67
|
+
attach(server) {
|
|
68
|
+
server.setRequestHandler("events/list", { params: z.looseObject({ cursor: z.string().optional() }) }, async () => ({
|
|
69
|
+
events: [EVENT_TYPE],
|
|
70
|
+
}));
|
|
71
|
+
server.setRequestHandler("events/stream", {
|
|
72
|
+
params: z.looseObject({
|
|
73
|
+
name: z.string(),
|
|
74
|
+
arguments: z.record(z.string(), z.unknown()).optional(),
|
|
75
|
+
cursor: z.string().nullish(),
|
|
76
|
+
maxAgeMs: z.number().optional(),
|
|
77
|
+
}),
|
|
78
|
+
}, async (params, ctx) => {
|
|
79
|
+
if (params.name !== EVENT_NAME)
|
|
80
|
+
throw new ProtocolError(NOT_FOUND, `Unknown event type ${params.name}`);
|
|
81
|
+
const filter = parseFilter(params.arguments ?? {});
|
|
82
|
+
const meta = { [SUBSCRIPTION_ID]: ctx.mcpReq.id };
|
|
83
|
+
let chain = Promise.resolve();
|
|
84
|
+
const notify = (method, body) => {
|
|
85
|
+
chain = chain.then(() => ctx.mcpReq.notify({ method, params: { ...body, _meta: meta } })).catch(() => undefined);
|
|
86
|
+
};
|
|
87
|
+
const push = (event) => {
|
|
88
|
+
if (!matches(filter, event))
|
|
89
|
+
return;
|
|
90
|
+
notify("notifications/events/event", {
|
|
91
|
+
eventId: event.data.id,
|
|
92
|
+
name: EVENT_NAME,
|
|
93
|
+
timestamp: event.data.at,
|
|
94
|
+
data: event.data,
|
|
95
|
+
cursor: `${this.#epoch}.${event.seq}`,
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
const backlog = this.#replay(params.cursor, params.maxAgeMs);
|
|
99
|
+
this.#listeners.add(push);
|
|
100
|
+
notify("notifications/events/active", {
|
|
101
|
+
heartbeatMs: 15_000,
|
|
102
|
+
cursor: params.cursor ?? `${this.#epoch}.${this.#seq}`,
|
|
103
|
+
...(backlog.truncated ? { truncated: true } : {}),
|
|
104
|
+
});
|
|
105
|
+
for (const event of backlog.events)
|
|
106
|
+
push(event);
|
|
107
|
+
const heartbeat = setInterval(() => notify("notifications/events/heartbeat", { cursor: `${this.#epoch}.${this.#seq}` }), 15_000);
|
|
108
|
+
heartbeat.unref();
|
|
109
|
+
try {
|
|
110
|
+
await new Promise((resolve) => {
|
|
111
|
+
if (ctx.mcpReq.signal.aborted)
|
|
112
|
+
return resolve();
|
|
113
|
+
ctx.mcpReq.signal.addEventListener("abort", () => resolve(), { once: true });
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
clearInterval(heartbeat);
|
|
118
|
+
this.#listeners.delete(push);
|
|
119
|
+
}
|
|
120
|
+
return {};
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
#replay(cursor, maxAgeMs) {
|
|
124
|
+
if (cursor === null || cursor === undefined)
|
|
125
|
+
return { events: [], truncated: false };
|
|
126
|
+
const [from, at] = cursor.split(".");
|
|
127
|
+
const after = Number(at);
|
|
128
|
+
const fresh = (event) => maxAgeMs === undefined || event.at >= Date.now() - maxAgeMs;
|
|
129
|
+
if (from !== this.#epoch || !Number.isInteger(after))
|
|
130
|
+
return { events: this.#buffer.filter(fresh), truncated: true };
|
|
131
|
+
const oldest = this.#buffer[0]?.seq ?? this.#seq + 1;
|
|
132
|
+
return { events: this.#buffer.filter((event) => event.seq > after && fresh(event)), truncated: after < oldest - 1 };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function parseFilter(args) {
|
|
136
|
+
const unknown = Object.keys(args).filter((key) => key !== "action" && key !== "name");
|
|
137
|
+
if (unknown.length > 0)
|
|
138
|
+
throw new ProtocolError(INVALID_PARAMS, `Unknown arguments: ${unknown.join(", ")} (use action, name)`);
|
|
139
|
+
for (const key of ["action", "name"])
|
|
140
|
+
if (args[key] !== undefined && typeof args[key] !== "string")
|
|
141
|
+
throw new ProtocolError(INVALID_PARAMS, `${key} must be a string`);
|
|
142
|
+
return {
|
|
143
|
+
...(typeof args.action === "string" ? { action: args.action } : {}),
|
|
144
|
+
...(typeof args.name === "string" ? { name: args.name } : {}),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function matches(filter, event) {
|
|
148
|
+
return (filter.action === undefined || filter.action === event.data.action) && (filter.name === undefined || filter.name === event.data.name);
|
|
149
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One description of a form, two renderings: the JSON Schema of a form-mode elicitation, and the
|
|
3
|
+
* HTML page of a URL-mode one (where a secret gets a real password field). Answers come back as
|
|
4
|
+
* untrusted values in both cases and are read with `text()` / `count()`.
|
|
5
|
+
*/
|
|
6
|
+
/** The `requestedSchema` of a form-mode elicitation. */
|
|
7
|
+
export function elicitationSchema(fields) {
|
|
8
|
+
const properties = Object.fromEntries(fields.map((field) => [field.key, property(field)]));
|
|
9
|
+
const required = fields.filter((field) => field.required).map((field) => field.key);
|
|
10
|
+
return { type: "object", properties, ...(required.length > 0 ? { required } : {}) };
|
|
11
|
+
}
|
|
12
|
+
function property(field) {
|
|
13
|
+
const base = {
|
|
14
|
+
title: field.label,
|
|
15
|
+
...(field.help ? { description: field.help } : {}),
|
|
16
|
+
...(field.default !== undefined && field.default !== "" ? { default: field.default } : {}),
|
|
17
|
+
};
|
|
18
|
+
if (field.type === "boolean")
|
|
19
|
+
return { type: "boolean", ...base };
|
|
20
|
+
if (field.type === "number")
|
|
21
|
+
return { type: "integer", ...base, ...(field.minimum !== undefined ? { minimum: field.minimum } : {}) };
|
|
22
|
+
if (field.type === "choice")
|
|
23
|
+
return { type: "string", ...base, oneOf: (field.options ?? []).map((option) => ({ const: option.value, title: option.label })) };
|
|
24
|
+
return { type: "string", ...base, ...(field.minLength ? { minLength: field.minLength } : {}) };
|
|
25
|
+
}
|
|
26
|
+
/** The answers a page or form must have before they are accepted, or the first problem. */
|
|
27
|
+
export function validate(fields, values) {
|
|
28
|
+
for (const field of fields) {
|
|
29
|
+
const value = values[field.key];
|
|
30
|
+
const given = field.type === "boolean" ? typeof value === "boolean" : text(value) !== undefined;
|
|
31
|
+
if (field.required && !given)
|
|
32
|
+
return `${field.label} is required.`;
|
|
33
|
+
if (field.minLength && given && (text(value) ?? "").length < field.minLength)
|
|
34
|
+
return `${field.label} must have at least ${field.minLength} characters.`;
|
|
35
|
+
if (field.type === "choice" && given && !(field.options ?? []).some((option) => option.value === text(value)))
|
|
36
|
+
return `${field.label}: choose one of the options.`;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* An HTML page for the fields. Values are escaped. A secret the user typed is never put back in the
|
|
42
|
+
* page; one the agent proposed (`reveal`) is shown in clear so the user can check it.
|
|
43
|
+
*/
|
|
44
|
+
export function renderPage(input) {
|
|
45
|
+
const rows = input.fields.map((field) => fieldHtml(field, input.values?.[field.key])).join("\n");
|
|
46
|
+
return `<!doctype html>
|
|
47
|
+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
48
|
+
<meta name="referrer" content="no-referrer"><title>${escape(input.title)}</title>
|
|
49
|
+
<style>
|
|
50
|
+
body{font:15px/1.5 system-ui,sans-serif;max-width:34rem;margin:2rem auto;padding:0 1rem;color:#1d1d1f;background:#fafafa}
|
|
51
|
+
h1{font-size:1.25rem}label{display:block;margin:.9rem 0 .2rem;font-weight:600}
|
|
52
|
+
small{display:block;color:#666;font-weight:400}input,select,textarea{width:100%;box-sizing:border-box;padding:.5rem;font:inherit;border:1px solid #bbb;border-radius:6px}
|
|
53
|
+
input[type=checkbox]{width:auto}.problem{background:#fde8e8;border:1px solid #f5b5b5;padding:.6rem;border-radius:6px}
|
|
54
|
+
button{margin-top:1.2rem;padding:.6rem 1.2rem;font:inherit;border:0;border-radius:6px;background:#1f6feb;color:#fff;cursor:pointer}
|
|
55
|
+
</style></head><body>
|
|
56
|
+
<h1>${escape(input.title)}</h1><p>${escape(input.message)}</p>
|
|
57
|
+
${input.problem ? `<p class="problem">${escape(input.problem)}</p>` : ""}
|
|
58
|
+
<form method="post" action="${escape(input.action)}" autocomplete="off">
|
|
59
|
+
${rows}
|
|
60
|
+
<button type="submit">Submit</button>
|
|
61
|
+
</form></body></html>`;
|
|
62
|
+
}
|
|
63
|
+
export function donePage(message) {
|
|
64
|
+
return `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Done</title>
|
|
65
|
+
<style>body{font:15px/1.5 system-ui,sans-serif;max-width:34rem;margin:2rem auto;padding:0 1rem}</style></head>
|
|
66
|
+
<body><h1>Done</h1><p>${escape(message)}</p></body></html>`;
|
|
67
|
+
}
|
|
68
|
+
function fieldHtml(field, current) {
|
|
69
|
+
const id = `f-${field.key}`;
|
|
70
|
+
const help = field.help ? `<small>${escape(field.help)}</small>` : "";
|
|
71
|
+
const label = `<label for="${id}">${escape(field.label)}${field.required ? " *" : ""}${help}</label>`;
|
|
72
|
+
const value = field.type === "secret" ? (field.reveal ? String(field.default ?? "") : "") : String(current ?? field.default ?? "");
|
|
73
|
+
const required = field.required ? " required" : "";
|
|
74
|
+
if (field.type === "choice") {
|
|
75
|
+
const options = (field.options ?? [])
|
|
76
|
+
.map((option) => `<option value="${escape(option.value)}"${option.value === value ? " selected" : ""}>${escape(option.label)}</option>`)
|
|
77
|
+
.join("");
|
|
78
|
+
return `${label}<select id="${id}" name="${field.key}"${required}>${options}</select>`;
|
|
79
|
+
}
|
|
80
|
+
if (field.type === "boolean")
|
|
81
|
+
return `${label}<input id="${id}" type="checkbox" name="${field.key}" value="true"${current === true || (current === undefined && field.default === true) ? " checked" : ""}>`;
|
|
82
|
+
if (field.type === "textarea")
|
|
83
|
+
return `${label}<textarea id="${id}" name="${field.key}" rows="3">${escape(value)}</textarea>`;
|
|
84
|
+
const type = field.type === "secret" && !field.reveal ? "password" : field.type === "number" ? "number" : "text";
|
|
85
|
+
const extra = `${field.minLength ? ` minlength="${field.minLength}"` : ""}${field.minimum !== undefined ? ` min="${field.minimum}"` : ""}`;
|
|
86
|
+
return `${label}<input id="${id}" type="${type}" name="${field.key}" value="${escape(value)}"${required}${extra}${field.type === "secret" ? ' autocomplete="new-password"' : ""}>`;
|
|
87
|
+
}
|
|
88
|
+
/** Reads a submitted page body (`application/x-www-form-urlencoded`) as the form answers would arrive. */
|
|
89
|
+
export function pageValues(fields, body) {
|
|
90
|
+
const params = new URLSearchParams(body);
|
|
91
|
+
return Object.fromEntries(fields.map((field) => {
|
|
92
|
+
const raw = params.get(field.key);
|
|
93
|
+
if (field.type === "boolean")
|
|
94
|
+
return [field.key, raw === "true"];
|
|
95
|
+
if (field.type === "number")
|
|
96
|
+
return [field.key, raw === null || raw === "" ? undefined : Number(raw)];
|
|
97
|
+
return [field.key, raw ?? undefined];
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
export function text(value) {
|
|
101
|
+
if (typeof value !== "string")
|
|
102
|
+
return undefined;
|
|
103
|
+
const trimmed = value.trim();
|
|
104
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
105
|
+
}
|
|
106
|
+
export function count(value) {
|
|
107
|
+
const n = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN;
|
|
108
|
+
return Number.isInteger(n) && n >= 0 ? n : undefined;
|
|
109
|
+
}
|
|
110
|
+
function escape(value) {
|
|
111
|
+
return value.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c]);
|
|
112
|
+
}
|