@authn8/mcp-server 1.0.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/Dockerfile +6 -0
- package/LICENSE +21 -0
- package/README.md +127 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +339 -0
- package/package.json +32 -0
- package/src/client.ts +153 -0
- package/src/index.ts +119 -0
- package/src/tools.ts +171 -0
- package/tsconfig.json +16 -0
- package/tsup.config.ts +12 -0
package/Dockerfile
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Authn8
|
|
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
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# @authn8/mcp-server
|
|
2
|
+
|
|
3
|
+
MCP server that provides AI agents access to Authn8 2FA codes via PAT authentication.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- An [Authn8](https://authn8.com) account
|
|
8
|
+
- A Personal Access Token (PAT) created in the Authn8 dashboard
|
|
9
|
+
|
|
10
|
+
## Quick Start
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npx @authn8/mcp-server
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Set the `AUTHN8_API_KEY` environment variable to your PAT token.
|
|
17
|
+
|
|
18
|
+
## Docker
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
docker run -e AUTHN8_API_KEY=pat_xxx ghcr.io/authn8/mcp-server
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Configuration
|
|
25
|
+
|
|
26
|
+
### Claude Desktop
|
|
27
|
+
|
|
28
|
+
Add to your Claude Desktop configuration file:
|
|
29
|
+
|
|
30
|
+
**macOS:** `~/Library/Application Support/Claude/claude_desktop_config.json`
|
|
31
|
+
**Windows:** `%APPDATA%\Claude\claude_desktop_config.json`
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
{
|
|
35
|
+
"mcpServers": {
|
|
36
|
+
"authn8": {
|
|
37
|
+
"command": "npx",
|
|
38
|
+
"args": ["-y", "@authn8/mcp-server"],
|
|
39
|
+
"env": {
|
|
40
|
+
"AUTHN8_API_KEY": "pat_your_token_here"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Cursor
|
|
48
|
+
|
|
49
|
+
Add to your Cursor MCP settings:
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"mcpServers": {
|
|
54
|
+
"authn8": {
|
|
55
|
+
"command": "npx",
|
|
56
|
+
"args": ["-y", "@authn8/mcp-server"],
|
|
57
|
+
"env": {
|
|
58
|
+
"AUTHN8_API_KEY": "pat_your_token_here"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Environment Variables
|
|
66
|
+
|
|
67
|
+
| Variable | Required | Default | Description |
|
|
68
|
+
|----------|----------|---------|-------------|
|
|
69
|
+
| `AUTHN8_API_KEY` | Yes | - | Your PAT token from Authn8 |
|
|
70
|
+
| `AUTHN8_API_URL` | No | `https://api.authn8.com` | API endpoint URL |
|
|
71
|
+
|
|
72
|
+
## Available Tools
|
|
73
|
+
|
|
74
|
+
### list_accounts
|
|
75
|
+
|
|
76
|
+
Returns all 2FA accounts accessible to this token.
|
|
77
|
+
|
|
78
|
+
**Example response:**
|
|
79
|
+
```json
|
|
80
|
+
[
|
|
81
|
+
{
|
|
82
|
+
"id": "924c52a6-4457-4970-a39f-4dc620217683",
|
|
83
|
+
"name": "AWS Production",
|
|
84
|
+
"group": "HR",
|
|
85
|
+
"issuer": "Amazon"
|
|
86
|
+
}
|
|
87
|
+
]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### get_otp
|
|
91
|
+
|
|
92
|
+
Generates a TOTP code for a specific account.
|
|
93
|
+
|
|
94
|
+
**Parameters:**
|
|
95
|
+
- `account_id` (string, optional) - UUID of the account
|
|
96
|
+
- `account_name` (string, optional) - Name to search for (partial match)
|
|
97
|
+
|
|
98
|
+
Provide either `account_id` or `account_name`. If multiple accounts match the name, the tool returns a list of matches.
|
|
99
|
+
|
|
100
|
+
**Example response:**
|
|
101
|
+
```json
|
|
102
|
+
{
|
|
103
|
+
"account": "AWS Production",
|
|
104
|
+
"otp": "483920",
|
|
105
|
+
"expires_in_seconds": 18
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### whoami
|
|
110
|
+
|
|
111
|
+
Returns information about the current token.
|
|
112
|
+
|
|
113
|
+
**Example response:**
|
|
114
|
+
```json
|
|
115
|
+
{
|
|
116
|
+
"business": "Bytecode Solutions",
|
|
117
|
+
"token_name": "MCP Server 2",
|
|
118
|
+
"scoped_groups": ["HR"],
|
|
119
|
+
"account_count": 1,
|
|
120
|
+
"expires_at": "2025-12-25T23:59:59Z"
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Links
|
|
125
|
+
|
|
126
|
+
- [Authn8](https://authn8.com) - Create an account and manage 2FA tokens
|
|
127
|
+
- [GitHub Repository](https://github.com/authn8/mcp-server)
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
5
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
6
|
+
|
|
7
|
+
// src/client.ts
|
|
8
|
+
var API_URL = process.env.AUTHN8_API_URL || "https://api.authn8.com";
|
|
9
|
+
var API_KEY = process.env.AUTHN8_API_KEY;
|
|
10
|
+
var USER_AGENT = "authn8-mcp/1.0.0";
|
|
11
|
+
var Authn8Error = class extends Error {
|
|
12
|
+
constructor(message, statusCode) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.statusCode = statusCode;
|
|
15
|
+
this.name = "Authn8Error";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var accountsCache = null;
|
|
19
|
+
var CACHE_TTL_MS = 60 * 1e3;
|
|
20
|
+
async function apiRequest(endpoint, method = "GET") {
|
|
21
|
+
if (!API_KEY) {
|
|
22
|
+
throw new Authn8Error(
|
|
23
|
+
"AUTHN8_API_KEY environment variable is not set. Please set it to your PAT token from the Authn8 dashboard."
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
const url = `${API_URL}${endpoint}`;
|
|
27
|
+
const response = await fetch(url, {
|
|
28
|
+
method,
|
|
29
|
+
headers: {
|
|
30
|
+
Authorization: `Bearer ${API_KEY}`,
|
|
31
|
+
"User-Agent": USER_AGENT,
|
|
32
|
+
"Content-Type": "application/json"
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
if (!response.ok) {
|
|
36
|
+
if (response.status === 401) {
|
|
37
|
+
throw new Authn8Error(
|
|
38
|
+
"Token is invalid or expired. Please check your token in the Authn8 dashboard.",
|
|
39
|
+
401
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
if (response.status === 403) {
|
|
43
|
+
throw new Authn8Error(
|
|
44
|
+
"Token does not have permission to access this resource.",
|
|
45
|
+
403
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
if (response.status === 404) {
|
|
49
|
+
throw new Authn8Error("Resource not found.", 404);
|
|
50
|
+
}
|
|
51
|
+
if (response.status === 429) {
|
|
52
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
53
|
+
throw new Authn8Error(
|
|
54
|
+
`Rate limited. ${retryAfter ? `Retry after ${retryAfter} seconds.` : "Please try again later."}`,
|
|
55
|
+
429
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
throw new Authn8Error(
|
|
59
|
+
`API request failed with status ${response.status}`,
|
|
60
|
+
response.status
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return response.json();
|
|
64
|
+
}
|
|
65
|
+
async function getTokenInfo() {
|
|
66
|
+
return apiRequest("/api/pat/me");
|
|
67
|
+
}
|
|
68
|
+
async function listAccounts() {
|
|
69
|
+
if (accountsCache && Date.now() - accountsCache.timestamp < CACHE_TTL_MS) {
|
|
70
|
+
return accountsCache.accounts;
|
|
71
|
+
}
|
|
72
|
+
const accounts = await apiRequest("/api/pat/accounts");
|
|
73
|
+
accountsCache = {
|
|
74
|
+
accounts,
|
|
75
|
+
timestamp: Date.now()
|
|
76
|
+
};
|
|
77
|
+
return accounts;
|
|
78
|
+
}
|
|
79
|
+
async function getOtp(accountId) {
|
|
80
|
+
return apiRequest(`/api/pat/otp/${accountId}`);
|
|
81
|
+
}
|
|
82
|
+
async function findAccountByName(name) {
|
|
83
|
+
const accounts = await listAccounts();
|
|
84
|
+
const lowerName = name.toLowerCase();
|
|
85
|
+
const matches = accounts.filter(
|
|
86
|
+
(acc) => acc.name.toLowerCase().includes(lowerName) || acc.issuer.toLowerCase().includes(lowerName)
|
|
87
|
+
);
|
|
88
|
+
if (matches.length === 0) {
|
|
89
|
+
throw new Authn8Error(
|
|
90
|
+
`No account found matching "${name}". Available accounts:
|
|
91
|
+
${accounts.map((a) => ` - ${a.name} (${a.issuer})`).join("\n")}`
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
if (matches.length === 1) {
|
|
95
|
+
return { account: matches[0] };
|
|
96
|
+
}
|
|
97
|
+
return { matches };
|
|
98
|
+
}
|
|
99
|
+
async function validateToken() {
|
|
100
|
+
try {
|
|
101
|
+
return await getTokenInfo();
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (error instanceof Authn8Error) {
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
throw new Authn8Error(
|
|
107
|
+
`Failed to connect to Authn8 API. Please check AUTHN8_API_URL (${API_URL}). Error: ${error instanceof Error ? error.message : String(error)}`
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// src/tools.ts
|
|
113
|
+
import { z } from "zod";
|
|
114
|
+
var toolDefinitions = {
|
|
115
|
+
list_accounts: {
|
|
116
|
+
description: "Returns all 2FA accounts accessible to this token. Use this to see what accounts are available before requesting an OTP code.",
|
|
117
|
+
inputSchema: z.object({})
|
|
118
|
+
},
|
|
119
|
+
get_otp: {
|
|
120
|
+
description: "Generates a TOTP code for a specific account. You can provide either the account_id (UUID) or account_name (partial match supported). If multiple accounts match the name, you'll get a list of matches to be more specific.",
|
|
121
|
+
inputSchema: z.object({
|
|
122
|
+
account_id: z.string().uuid().optional().describe("UUID of the account"),
|
|
123
|
+
account_name: z.string().optional().describe("Name to search for (partial match)")
|
|
124
|
+
})
|
|
125
|
+
},
|
|
126
|
+
whoami: {
|
|
127
|
+
description: "Returns information about the current token and what it has access to, including the business name, token name, scoped groups, account count, and expiration date.",
|
|
128
|
+
inputSchema: z.object({})
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
async function handleListAccounts() {
|
|
132
|
+
const accounts = await listAccounts();
|
|
133
|
+
if (accounts.length === 0) {
|
|
134
|
+
return {
|
|
135
|
+
content: [
|
|
136
|
+
{
|
|
137
|
+
type: "text",
|
|
138
|
+
text: "No accounts are accessible with this token."
|
|
139
|
+
}
|
|
140
|
+
]
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const formatted = accounts.map((acc) => ({
|
|
144
|
+
id: acc.id,
|
|
145
|
+
name: acc.name,
|
|
146
|
+
group: acc.group,
|
|
147
|
+
issuer: acc.issuer
|
|
148
|
+
}));
|
|
149
|
+
return {
|
|
150
|
+
content: [
|
|
151
|
+
{
|
|
152
|
+
type: "text",
|
|
153
|
+
text: JSON.stringify(formatted, null, 2)
|
|
154
|
+
}
|
|
155
|
+
]
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
async function handleGetOtp(args) {
|
|
159
|
+
const { account_id, account_name } = args;
|
|
160
|
+
if (!account_id && !account_name) {
|
|
161
|
+
return {
|
|
162
|
+
content: [
|
|
163
|
+
{
|
|
164
|
+
type: "text",
|
|
165
|
+
text: "Error: Either account_id or account_name must be provided."
|
|
166
|
+
}
|
|
167
|
+
]
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
let targetAccountId;
|
|
171
|
+
let accountName;
|
|
172
|
+
if (account_id) {
|
|
173
|
+
targetAccountId = account_id;
|
|
174
|
+
const accounts = await listAccounts();
|
|
175
|
+
const account = accounts.find((a) => a.id === account_id);
|
|
176
|
+
if (!account) {
|
|
177
|
+
return {
|
|
178
|
+
content: [
|
|
179
|
+
{
|
|
180
|
+
type: "text",
|
|
181
|
+
text: `Error: Account with ID "${account_id}" not found. Use list_accounts to see available accounts.`
|
|
182
|
+
}
|
|
183
|
+
]
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
accountName = account.name;
|
|
187
|
+
} else {
|
|
188
|
+
const result = await findAccountByName(account_name);
|
|
189
|
+
if ("matches" in result) {
|
|
190
|
+
return {
|
|
191
|
+
content: [
|
|
192
|
+
{
|
|
193
|
+
type: "text",
|
|
194
|
+
text: `Multiple accounts match "${account_name}". Please be more specific:
|
|
195
|
+
${result.matches.map((a) => ` - ${a.name} (${a.issuer}) - ID: ${a.id}`).join("\n")}`
|
|
196
|
+
}
|
|
197
|
+
]
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
targetAccountId = result.account.id;
|
|
201
|
+
accountName = result.account.name;
|
|
202
|
+
}
|
|
203
|
+
const otp = await getOtp(targetAccountId);
|
|
204
|
+
return {
|
|
205
|
+
content: [
|
|
206
|
+
{
|
|
207
|
+
type: "text",
|
|
208
|
+
text: JSON.stringify(
|
|
209
|
+
{
|
|
210
|
+
account: accountName,
|
|
211
|
+
otp: otp.otp,
|
|
212
|
+
expires_in_seconds: otp.expires_in_seconds
|
|
213
|
+
},
|
|
214
|
+
null,
|
|
215
|
+
2
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
]
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
async function handleWhoami() {
|
|
222
|
+
const info = await getTokenInfo();
|
|
223
|
+
return {
|
|
224
|
+
content: [
|
|
225
|
+
{
|
|
226
|
+
type: "text",
|
|
227
|
+
text: JSON.stringify(
|
|
228
|
+
{
|
|
229
|
+
business: info.business,
|
|
230
|
+
token_name: info.token_name,
|
|
231
|
+
scoped_groups: info.scoped_groups,
|
|
232
|
+
account_count: info.account_count,
|
|
233
|
+
expires_at: info.expires_at
|
|
234
|
+
},
|
|
235
|
+
null,
|
|
236
|
+
2
|
|
237
|
+
)
|
|
238
|
+
}
|
|
239
|
+
]
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// src/index.ts
|
|
244
|
+
function formatDate(isoDate) {
|
|
245
|
+
const date = new Date(isoDate);
|
|
246
|
+
return date.toLocaleDateString("en-US", {
|
|
247
|
+
month: "short",
|
|
248
|
+
day: "numeric",
|
|
249
|
+
year: "numeric"
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
function displayStartupInfo(info) {
|
|
253
|
+
console.error("Authn8 MCP Server");
|
|
254
|
+
console.error(`Business: ${info.business}`);
|
|
255
|
+
console.error(`Token: ${info.token_name}`);
|
|
256
|
+
console.error(`Accounts: ${info.account_count}`);
|
|
257
|
+
console.error(`Expires: ${formatDate(info.expires_at)}`);
|
|
258
|
+
console.error("");
|
|
259
|
+
}
|
|
260
|
+
async function main() {
|
|
261
|
+
let tokenInfo;
|
|
262
|
+
try {
|
|
263
|
+
tokenInfo = await validateToken();
|
|
264
|
+
} catch (error) {
|
|
265
|
+
console.error(
|
|
266
|
+
"Error:",
|
|
267
|
+
error instanceof Error ? error.message : String(error)
|
|
268
|
+
);
|
|
269
|
+
process.exit(1);
|
|
270
|
+
}
|
|
271
|
+
displayStartupInfo(tokenInfo);
|
|
272
|
+
const server = new McpServer({
|
|
273
|
+
name: "authn8-mcp",
|
|
274
|
+
version: "1.0.0"
|
|
275
|
+
});
|
|
276
|
+
server.tool(
|
|
277
|
+
"list_accounts",
|
|
278
|
+
toolDefinitions.list_accounts.description,
|
|
279
|
+
toolDefinitions.list_accounts.inputSchema.shape,
|
|
280
|
+
async () => {
|
|
281
|
+
try {
|
|
282
|
+
return await handleListAccounts();
|
|
283
|
+
} catch (error) {
|
|
284
|
+
return {
|
|
285
|
+
content: [
|
|
286
|
+
{
|
|
287
|
+
type: "text",
|
|
288
|
+
text: `Error: ${error instanceof Error ? error.message : String(error)}`
|
|
289
|
+
}
|
|
290
|
+
]
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
);
|
|
295
|
+
server.tool(
|
|
296
|
+
"get_otp",
|
|
297
|
+
toolDefinitions.get_otp.description,
|
|
298
|
+
toolDefinitions.get_otp.inputSchema.shape,
|
|
299
|
+
async (args) => {
|
|
300
|
+
try {
|
|
301
|
+
return await handleGetOtp(args);
|
|
302
|
+
} catch (error) {
|
|
303
|
+
return {
|
|
304
|
+
content: [
|
|
305
|
+
{
|
|
306
|
+
type: "text",
|
|
307
|
+
text: `Error: ${error instanceof Error ? error.message : String(error)}`
|
|
308
|
+
}
|
|
309
|
+
]
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
);
|
|
314
|
+
server.tool(
|
|
315
|
+
"whoami",
|
|
316
|
+
toolDefinitions.whoami.description,
|
|
317
|
+
toolDefinitions.whoami.inputSchema.shape,
|
|
318
|
+
async () => {
|
|
319
|
+
try {
|
|
320
|
+
return await handleWhoami();
|
|
321
|
+
} catch (error) {
|
|
322
|
+
return {
|
|
323
|
+
content: [
|
|
324
|
+
{
|
|
325
|
+
type: "text",
|
|
326
|
+
text: `Error: ${error instanceof Error ? error.message : String(error)}`
|
|
327
|
+
}
|
|
328
|
+
]
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
);
|
|
333
|
+
const transport = new StdioServerTransport();
|
|
334
|
+
await server.connect(transport);
|
|
335
|
+
}
|
|
336
|
+
main().catch((error) => {
|
|
337
|
+
console.error("Fatal error:", error);
|
|
338
|
+
process.exit(1);
|
|
339
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@authn8/mcp-server",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MCP server for Authn8 2FA code access",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"authn8-mcp": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsup src/index.ts --format esm --dts",
|
|
12
|
+
"dev": "node --loader ts-node/esm src/index.ts",
|
|
13
|
+
"start": "node dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"keywords": ["mcp", "2fa", "totp", "authn8", "authenticator"],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/authn8/mcp-server"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://authn8.com",
|
|
21
|
+
"author": "",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@modelcontextprotocol/sdk": "^1.24.3",
|
|
25
|
+
"zod": "^4.1.13"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^24.10.1",
|
|
29
|
+
"tsup": "^8.5.1",
|
|
30
|
+
"typescript": "^5.9.3"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
const API_URL = process.env.AUTHN8_API_URL || "https://api.authn8.com";
|
|
2
|
+
const API_KEY = process.env.AUTHN8_API_KEY;
|
|
3
|
+
const USER_AGENT = "authn8-mcp/1.0.0";
|
|
4
|
+
|
|
5
|
+
export interface TokenInfo {
|
|
6
|
+
business: string;
|
|
7
|
+
token_name: string;
|
|
8
|
+
scoped_groups: string[];
|
|
9
|
+
account_count: number;
|
|
10
|
+
expires_at: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface Account {
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
group: string;
|
|
17
|
+
issuer: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface OtpResponse {
|
|
21
|
+
account: string;
|
|
22
|
+
otp: string;
|
|
23
|
+
expires_in_seconds: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class Authn8Error extends Error {
|
|
27
|
+
constructor(
|
|
28
|
+
message: string,
|
|
29
|
+
public statusCode?: number
|
|
30
|
+
) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = "Authn8Error";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let accountsCache: { accounts: Account[]; timestamp: number } | null = null;
|
|
37
|
+
const CACHE_TTL_MS = 60 * 1000; // 60 seconds
|
|
38
|
+
|
|
39
|
+
async function apiRequest<T>(
|
|
40
|
+
endpoint: string,
|
|
41
|
+
method: string = "GET"
|
|
42
|
+
): Promise<T> {
|
|
43
|
+
if (!API_KEY) {
|
|
44
|
+
throw new Authn8Error(
|
|
45
|
+
"AUTHN8_API_KEY environment variable is not set. Please set it to your PAT token from the Authn8 dashboard."
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const url = `${API_URL}${endpoint}`;
|
|
50
|
+
|
|
51
|
+
const response = await fetch(url, {
|
|
52
|
+
method,
|
|
53
|
+
headers: {
|
|
54
|
+
Authorization: `Bearer ${API_KEY}`,
|
|
55
|
+
"User-Agent": USER_AGENT,
|
|
56
|
+
"Content-Type": "application/json",
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
if (response.status === 401) {
|
|
62
|
+
throw new Authn8Error(
|
|
63
|
+
"Token is invalid or expired. Please check your token in the Authn8 dashboard.",
|
|
64
|
+
401
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
if (response.status === 403) {
|
|
68
|
+
throw new Authn8Error(
|
|
69
|
+
"Token does not have permission to access this resource.",
|
|
70
|
+
403
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
if (response.status === 404) {
|
|
74
|
+
throw new Authn8Error("Resource not found.", 404);
|
|
75
|
+
}
|
|
76
|
+
if (response.status === 429) {
|
|
77
|
+
const retryAfter = response.headers.get("Retry-After");
|
|
78
|
+
throw new Authn8Error(
|
|
79
|
+
`Rate limited. ${retryAfter ? `Retry after ${retryAfter} seconds.` : "Please try again later."}`,
|
|
80
|
+
429
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
throw new Authn8Error(
|
|
84
|
+
`API request failed with status ${response.status}`,
|
|
85
|
+
response.status
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return response.json() as Promise<T>;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function getTokenInfo(): Promise<TokenInfo> {
|
|
93
|
+
return apiRequest<TokenInfo>("/api/pat/me");
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function listAccounts(): Promise<Account[]> {
|
|
97
|
+
// Check cache
|
|
98
|
+
if (accountsCache && Date.now() - accountsCache.timestamp < CACHE_TTL_MS) {
|
|
99
|
+
return accountsCache.accounts;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const accounts = await apiRequest<Account[]>("/api/pat/accounts");
|
|
103
|
+
|
|
104
|
+
// Update cache
|
|
105
|
+
accountsCache = {
|
|
106
|
+
accounts,
|
|
107
|
+
timestamp: Date.now(),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return accounts;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export async function getOtp(accountId: string): Promise<OtpResponse> {
|
|
114
|
+
return apiRequest<OtpResponse>(`/api/pat/otp/${accountId}`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export async function findAccountByName(
|
|
118
|
+
name: string
|
|
119
|
+
): Promise<{ account: Account } | { matches: Account[] }> {
|
|
120
|
+
const accounts = await listAccounts();
|
|
121
|
+
const lowerName = name.toLowerCase();
|
|
122
|
+
|
|
123
|
+
const matches = accounts.filter(
|
|
124
|
+
(acc) =>
|
|
125
|
+
acc.name.toLowerCase().includes(lowerName) ||
|
|
126
|
+
acc.issuer.toLowerCase().includes(lowerName)
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
if (matches.length === 0) {
|
|
130
|
+
throw new Authn8Error(
|
|
131
|
+
`No account found matching "${name}". Available accounts:\n${accounts.map((a) => ` - ${a.name} (${a.issuer})`).join("\n")}`
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (matches.length === 1) {
|
|
136
|
+
return { account: matches[0] };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return { matches };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export async function validateToken(): Promise<TokenInfo> {
|
|
143
|
+
try {
|
|
144
|
+
return await getTokenInfo();
|
|
145
|
+
} catch (error) {
|
|
146
|
+
if (error instanceof Authn8Error) {
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
throw new Authn8Error(
|
|
150
|
+
`Failed to connect to Authn8 API. Please check AUTHN8_API_URL (${API_URL}). Error: ${error instanceof Error ? error.message : String(error)}`
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
|
+
import { validateToken, type TokenInfo } from "./client.js";
|
|
4
|
+
import {
|
|
5
|
+
toolDefinitions,
|
|
6
|
+
handleListAccounts,
|
|
7
|
+
handleGetOtp,
|
|
8
|
+
handleWhoami,
|
|
9
|
+
} from "./tools.js";
|
|
10
|
+
|
|
11
|
+
function formatDate(isoDate: string): string {
|
|
12
|
+
const date = new Date(isoDate);
|
|
13
|
+
return date.toLocaleDateString("en-US", {
|
|
14
|
+
month: "short",
|
|
15
|
+
day: "numeric",
|
|
16
|
+
year: "numeric",
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function displayStartupInfo(info: TokenInfo): void {
|
|
21
|
+
console.error("Authn8 MCP Server");
|
|
22
|
+
console.error(`Business: ${info.business}`);
|
|
23
|
+
console.error(`Token: ${info.token_name}`);
|
|
24
|
+
console.error(`Accounts: ${info.account_count}`);
|
|
25
|
+
console.error(`Expires: ${formatDate(info.expires_at)}`);
|
|
26
|
+
console.error("");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function main(): Promise<void> {
|
|
30
|
+
// Validate token on startup
|
|
31
|
+
let tokenInfo: TokenInfo;
|
|
32
|
+
try {
|
|
33
|
+
tokenInfo = await validateToken();
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error(
|
|
36
|
+
"Error:",
|
|
37
|
+
error instanceof Error ? error.message : String(error)
|
|
38
|
+
);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
displayStartupInfo(tokenInfo);
|
|
43
|
+
|
|
44
|
+
// Create MCP server
|
|
45
|
+
const server = new McpServer({
|
|
46
|
+
name: "authn8-mcp",
|
|
47
|
+
version: "1.0.0",
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Register tools
|
|
51
|
+
server.tool(
|
|
52
|
+
"list_accounts",
|
|
53
|
+
toolDefinitions.list_accounts.description,
|
|
54
|
+
toolDefinitions.list_accounts.inputSchema.shape,
|
|
55
|
+
async () => {
|
|
56
|
+
try {
|
|
57
|
+
return await handleListAccounts();
|
|
58
|
+
} catch (error) {
|
|
59
|
+
return {
|
|
60
|
+
content: [
|
|
61
|
+
{
|
|
62
|
+
type: "text" as const,
|
|
63
|
+
text: `Error: ${error instanceof Error ? error.message : String(error)}`,
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
server.tool(
|
|
72
|
+
"get_otp",
|
|
73
|
+
toolDefinitions.get_otp.description,
|
|
74
|
+
toolDefinitions.get_otp.inputSchema.shape,
|
|
75
|
+
async (args) => {
|
|
76
|
+
try {
|
|
77
|
+
return await handleGetOtp(args as { account_id?: string; account_name?: string });
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return {
|
|
80
|
+
content: [
|
|
81
|
+
{
|
|
82
|
+
type: "text" as const,
|
|
83
|
+
text: `Error: ${error instanceof Error ? error.message : String(error)}`,
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
server.tool(
|
|
92
|
+
"whoami",
|
|
93
|
+
toolDefinitions.whoami.description,
|
|
94
|
+
toolDefinitions.whoami.inputSchema.shape,
|
|
95
|
+
async () => {
|
|
96
|
+
try {
|
|
97
|
+
return await handleWhoami();
|
|
98
|
+
} catch (error) {
|
|
99
|
+
return {
|
|
100
|
+
content: [
|
|
101
|
+
{
|
|
102
|
+
type: "text" as const,
|
|
103
|
+
text: `Error: ${error instanceof Error ? error.message : String(error)}`,
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// Connect via stdio transport
|
|
112
|
+
const transport = new StdioServerTransport();
|
|
113
|
+
await server.connect(transport);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
main().catch((error) => {
|
|
117
|
+
console.error("Fatal error:", error);
|
|
118
|
+
process.exit(1);
|
|
119
|
+
});
|
package/src/tools.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import {
|
|
3
|
+
getTokenInfo,
|
|
4
|
+
listAccounts,
|
|
5
|
+
getOtp,
|
|
6
|
+
findAccountByName,
|
|
7
|
+
type TokenInfo,
|
|
8
|
+
type Account,
|
|
9
|
+
type OtpResponse,
|
|
10
|
+
} from "./client.js";
|
|
11
|
+
|
|
12
|
+
export const toolDefinitions = {
|
|
13
|
+
list_accounts: {
|
|
14
|
+
description:
|
|
15
|
+
"Returns all 2FA accounts accessible to this token. Use this to see what accounts are available before requesting an OTP code.",
|
|
16
|
+
inputSchema: z.object({}),
|
|
17
|
+
},
|
|
18
|
+
get_otp: {
|
|
19
|
+
description:
|
|
20
|
+
"Generates a TOTP code for a specific account. You can provide either the account_id (UUID) or account_name (partial match supported). If multiple accounts match the name, you'll get a list of matches to be more specific.",
|
|
21
|
+
inputSchema: z.object({
|
|
22
|
+
account_id: z
|
|
23
|
+
.string()
|
|
24
|
+
.uuid()
|
|
25
|
+
.optional()
|
|
26
|
+
.describe("UUID of the account"),
|
|
27
|
+
account_name: z
|
|
28
|
+
.string()
|
|
29
|
+
.optional()
|
|
30
|
+
.describe("Name to search for (partial match)"),
|
|
31
|
+
}),
|
|
32
|
+
},
|
|
33
|
+
whoami: {
|
|
34
|
+
description:
|
|
35
|
+
"Returns information about the current token and what it has access to, including the business name, token name, scoped groups, account count, and expiration date.",
|
|
36
|
+
inputSchema: z.object({}),
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export async function handleListAccounts(): Promise<{
|
|
41
|
+
content: Array<{ type: "text"; text: string }>;
|
|
42
|
+
}> {
|
|
43
|
+
const accounts = await listAccounts();
|
|
44
|
+
|
|
45
|
+
if (accounts.length === 0) {
|
|
46
|
+
return {
|
|
47
|
+
content: [
|
|
48
|
+
{
|
|
49
|
+
type: "text",
|
|
50
|
+
text: "No accounts are accessible with this token.",
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const formatted = accounts.map((acc) => ({
|
|
57
|
+
id: acc.id,
|
|
58
|
+
name: acc.name,
|
|
59
|
+
group: acc.group,
|
|
60
|
+
issuer: acc.issuer,
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
content: [
|
|
65
|
+
{
|
|
66
|
+
type: "text",
|
|
67
|
+
text: JSON.stringify(formatted, null, 2),
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function handleGetOtp(args: {
|
|
74
|
+
account_id?: string;
|
|
75
|
+
account_name?: string;
|
|
76
|
+
}): Promise<{ content: Array<{ type: "text"; text: string }> }> {
|
|
77
|
+
const { account_id, account_name } = args;
|
|
78
|
+
|
|
79
|
+
if (!account_id && !account_name) {
|
|
80
|
+
return {
|
|
81
|
+
content: [
|
|
82
|
+
{
|
|
83
|
+
type: "text",
|
|
84
|
+
text: "Error: Either account_id or account_name must be provided.",
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let targetAccountId: string;
|
|
91
|
+
let accountName: string;
|
|
92
|
+
|
|
93
|
+
if (account_id) {
|
|
94
|
+
targetAccountId = account_id;
|
|
95
|
+
// Get the account name for the response
|
|
96
|
+
const accounts = await listAccounts();
|
|
97
|
+
const account = accounts.find((a) => a.id === account_id);
|
|
98
|
+
if (!account) {
|
|
99
|
+
return {
|
|
100
|
+
content: [
|
|
101
|
+
{
|
|
102
|
+
type: "text",
|
|
103
|
+
text: `Error: Account with ID "${account_id}" not found. Use list_accounts to see available accounts.`,
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
accountName = account.name;
|
|
109
|
+
} else {
|
|
110
|
+
// Search by name
|
|
111
|
+
const result = await findAccountByName(account_name!);
|
|
112
|
+
|
|
113
|
+
if ("matches" in result) {
|
|
114
|
+
return {
|
|
115
|
+
content: [
|
|
116
|
+
{
|
|
117
|
+
type: "text",
|
|
118
|
+
text: `Multiple accounts match "${account_name}". Please be more specific:\n${result.matches.map((a) => ` - ${a.name} (${a.issuer}) - ID: ${a.id}`).join("\n")}`,
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
targetAccountId = result.account.id;
|
|
125
|
+
accountName = result.account.name;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const otp = await getOtp(targetAccountId);
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
content: [
|
|
132
|
+
{
|
|
133
|
+
type: "text",
|
|
134
|
+
text: JSON.stringify(
|
|
135
|
+
{
|
|
136
|
+
account: accountName,
|
|
137
|
+
otp: otp.otp,
|
|
138
|
+
expires_in_seconds: otp.expires_in_seconds,
|
|
139
|
+
},
|
|
140
|
+
null,
|
|
141
|
+
2
|
|
142
|
+
),
|
|
143
|
+
},
|
|
144
|
+
],
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export async function handleWhoami(): Promise<{
|
|
149
|
+
content: Array<{ type: "text"; text: string }>;
|
|
150
|
+
}> {
|
|
151
|
+
const info = await getTokenInfo();
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
content: [
|
|
155
|
+
{
|
|
156
|
+
type: "text",
|
|
157
|
+
text: JSON.stringify(
|
|
158
|
+
{
|
|
159
|
+
business: info.business,
|
|
160
|
+
token_name: info.token_name,
|
|
161
|
+
scoped_groups: info.scoped_groups,
|
|
162
|
+
account_count: info.account_count,
|
|
163
|
+
expires_at: info.expires_at,
|
|
164
|
+
},
|
|
165
|
+
null,
|
|
166
|
+
2
|
|
167
|
+
),
|
|
168
|
+
},
|
|
169
|
+
],
|
|
170
|
+
};
|
|
171
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"rootDir": "./src",
|
|
11
|
+
"declaration": true,
|
|
12
|
+
"resolveJsonModule": true
|
|
13
|
+
},
|
|
14
|
+
"include": ["src/**/*"],
|
|
15
|
+
"exclude": ["node_modules", "dist"]
|
|
16
|
+
}
|